Help with Torches Please

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Mysterious
Posts: 226
Joined: Wed Nov 06, 2013 8:31 am

Help with Torches Please

Post by Mysterious »

Hi all :)

I am trying to do what Desperate Gamers did in the 2nd level of their Mod. The below code works if the player puts the right torches in the right holders but if they put say torch2_1 into th1 the editor crashes. I do not know why. See torch2_1 has to go into th2 and so on.. The editor tries to delete torch1_1 for some reason...

When the player puts the right torches into the right holders, the holder and torch are destroyed to indicate they have used the right torch holders in the room. The exception is that their mod does not destroy the torch or holder..

Here is what I have ATM. This code works if the Torches are placed in the right Holders eg:

I have defined 4 new torches called (torch1 to torch4). The GUI Name for the torches are (Torches Of Light 1 to 4) that I have put into the (items.lua).
Holder Names = th1 to th4
Torch Names = torch1_1 to torch4_1
counter Name = unlock1Counter
------------------------------------------------------

The room has 4 torch holders, each torch holder needs a certain torch as above, here is the code that I modified from Desperate Gamers. Oh and the Torch Holders are set to activate not any.

----------------------------------------------------
unlock1Counter = 0

function addUnlock2()

playSound("secret")
unlock1Counter = unlock1Counter + 1

if unlock1Counter==1 then
hudPrint("Torch 1 lit.")
th1:destroy() ---Name of the Torch Holder---
torch1_1:destroy() ---Name of the Torch---

elseif unlock1Counter==2 then
hudPrint("Torch 2 lit.")
th2:destroy()
torch2_1:destroy()

elseif unlock1Counter==3 then
hudPrint("Torch 3 lit.")
th3:destroy()
torch3_1:destroy()

elseif unlock1Counter==4 then
hudPrint("Torch 4 lit.")

findEntity("dungeon_door_iron_3"):open()
hudPrint("VOICE: The way is open")
hudPrint("")
hudPrint("You gain 45 Xp")

for i = 1,4 do party:getChampion(i):gainExp(45)
end

playSound("level_up")

th4:destroy()
torch4_1:destroy()
end
end

Also if Torches are on the floor when the player puts a torch into one of the torch holders, some of the other torches are deleted from the editor.

The torches in this coding have to be placed in the correct order otherwise the editor crashes. I could put the torches and holders into separately locked rooms to avoid the error problem but I want them all in the one room.

At the end of the day all I want is when the player puts a torch into the holder text appears to tell them they lit the right torch and when all torches are lit the door opens, XP granted and they can continue..

I hope this makes sense to someone out there :)
alois
Posts: 112
Joined: Mon Feb 18, 2013 7:29 am

Re: Help with Torches Please

Post by alois »

Looking at the code, it seems to me that you are not checking whether torch holder 1 has torch 1 and so on, but simply whether someone inserted a torch somewhere (my guess is that the editor crashes after you place the second torch, after you already deleted one...). Indeed, your counter does not check torch holder 'counter', but torch holder 1 after you inserted the first torch, torch holder 2 after you inserted the second one and so on. Moreover, once you destroy the torch holder, you also destroy the torch inside it (I think); therefore, if you put torch_2 in torch holder 1, you destroy the torch holder, the torch (2) [due to the destroying of the torch holder], and the torch (1) - due to the line of code; while, if you put something in torch holder 2 as first thing, you destroy torch holder 1, torch 1, and not the one you used, but you destroy them nonetheless!

Alois :)
User avatar
DesperateGames
Posts: 90
Joined: Sun Oct 06, 2013 1:54 pm

Re: Help with Torches Please

Post by DesperateGames »

The problem is that the script i sent you via PM was based on locks where it did not matter which key you put in which lock, "as long as the key fits". With the torches it is a little bit different especially if you want a certain torch to "unlock" a certain torch holder. It is very easy to check whether there is ANY torch in a certain torch holder:
TorchHolder:hasTorch()
Returns true if there is a torch in the torch holder.
Things get difficult if you want to check whether a specific torch is in a torch holder, because there is no easy way to check which torch exactly has been put into the holder. This thread discusses some workarounds:

viewtopic.php?f=14&t=4751

Would it change the puzzle a lot if you would allow any torches to be used? Could you imagine to change the puzzle to use certain items on altars or alcoves? These would be easier to implement, as the altars and alcoves can be checked for certain items easily.
User avatar
Mysterious
Posts: 226
Joined: Wed Nov 06, 2013 8:31 am

Re: Help with Torches Please

Post by Mysterious »

Hi guys :)

It does not mater if the torches are the same all i want is if the player light all 4 torches in the 4 holders the door opens, but would like some text after each torch is lit eg:

Torch 1 lit, Torch 2 Lit and son on.. Is this hard to do I am not sure?? And is it possible to destroy the torch and holder as well?? What would the coding look like??

I have used altars for other puzzels and wanted something different in this room. Also used Alcoves as well for puzzles.

Anyway thank you for the help guys, but could use some coding on this so i can study it :) I am still new to lua and have ordered some lua programming books to help me learn.. :)
User avatar
DesperateGames
Posts: 90
Joined: Sun Oct 06, 2013 1:54 pm

Re: Help with Torches Please

Post by DesperateGames »

OK, if the torches are all the same, it is easier to do the check whether there is a torch in the holder or not. There are some difficulties with destroying the holder AND the torch. Destroying the holder is easy, but then you still have one torch flying around in the air :? because you need to explicitly destroy the torch as well....you could do so by destroying all torches on that field, but what if the player has put some torches on the ground on that field...hmmm... I have come up with this example: Assuming you have torch holders named torch_holder_1 to torch_holder_4 that all execute one function checkTorch() as below:

Code: Select all

torchCount=0

function checkTorch()

	countAndDestroy("torch_holder_1")
	countAndDestroy("torch_holder_2")
	countAndDestroy("torch_holder_3")
	countAndDestroy("torch_holder_4")
	
	if torchCount==1 then
		hudPrint("1 torch lit")
	elseif torchCount==2 then
		hudPrint("2 torches lit")
	elseif torchCount==3 then
		hudPrint("3 torches lit")
	elseif torchCount==4 then
		hudPrint("4 torches lit")
		dungeon_door_iron_1:open()
	end
end

function countAndDestroy(idOfHolder)
	--does the torch holder still exist?
	if findEntity(idOfHolder) then
		--store the torch holder in a local variable so we can access it easily
		local tempHolder = findEntity(idOfHolder)
		--does the torch holder have a torch in it?
		if tempHolder:hasTorch() then
			--it has a torch, we need to increment the counter
			torchCount = torchCount +1
			--we will use this variable to memorize how many torches have been destroyed
			destroyCount=0
			--we go through each entity at the exact position of the torch holder
			for e in entitiesAt(tempHolder.level,tempHolder.x,tempHolder.y) do
				--is the entity a torch and does it face in the same direction as a torch in the torch holder would?
				if e.name=="torch" and e.facing==tempHolder.facing then
					--we destroy the torch
					e:destroy()
					--we count the destroyed torch
					destroyCount = destroyCount + 1
				end
			end
			
			--if we have destroyed more than one torch, there were additional torches lying on the floor we have to "restore" them
			if destroyCount > 1 then
				--we count from 1 to destroyCount-1 
				for i=1,destroyCount-1 do
					--spawn a torch on the floor with the same facing as the holder
					spawn("torch",tempHolder.level,tempHolder.x, tempHolder.y, tempHolder.facing)
				end
			end
			--now we can destroy the torch holder
			tempHolder:destroy()
		end	
	end
end
The checkTorch() function itself is easy, the "magic" happens in the countAndDestroy()-function. The idea behind this function is that it will count the torch if there is any. Then it will destroy ALL torches on this field with the same facing as the torch holder. The function will count all torches that will be destroyed. If there was more than one torch destroyed that means there were torches lying around on the floor under the torch holder. So the function will "respawn" them right after the destruction ;-)

This might seem overly complicated but I don't know if there is any better way to do this because as far as I know there is no way to access / identify the torch object in a holder unlike it is possible with the altar or the alcoves. The downside of my workaround is that you can see that the torches are "respawned" because they move on the floor. This is only visible IF the player has placed torches on the floor in the first place and it is not game-breaking, but it is still noticeable.

Another workaround to resolve this would be to create a custom alcove object that just looks and acts like an torch holder so you would be able to identify / access the torch inside the holder via the alcove functions.

Or if it doesn't matter, you could just keep the torches and the holder on the wall ;) In this case, the above script would be far less complicated (you would need to set the torch holder event type to "any" for the function below to work):

Code: Select all

function checkTorch()

torchCount=0

	if torch_holder_1:hasTorch() then
		torchCount = torchCount +1
	end
	if torch_holder_2:hasTorch() then
		torchCount = torchCount +1
	end
	if torch_holder_3:hasTorch() then
		torchCount = torchCount +1
	end
	if torch_holder_4:hasTorch() then
		torchCount = torchCount +1
	end

	
	if torchCount==1 then
		hudPrint("1 torch lit")
		dungeon_door_iron_1:close()
	elseif torchCount==2 then
		hudPrint("2 torches lit")
		dungeon_door_iron_1:close()
	elseif torchCount==3 then
		hudPrint("3 torches lit")
		dungeon_door_iron_1:close()
	elseif torchCount==4 then
		hudPrint("4 torches lit")
		dungeon_door_iron_1:open()
	end
end
Hope this was helpful!
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Help with Torches Please

Post by JohnWordsworth »

With regards to destroying a torch holder and ensuring that the contained torch is also destroyed, I would consider the following solution too. It's completely untested, but it's just come to mind as an idea...
SpoilerShow

Code: Select all

function destroyTorchHolder(torchHolderId)
  local torchHolder = local tempHolder = findEntity(torchHolderId);

  if torchHolder:hasTorch() then
    local existingTorches = {};
    for e in entitiesAt(torchHolder.level, torchHolder.x, torchHolder.y) do 
      if ( e.name == "torch" ) then
        existingTorches[e.id] = true;
      end
    end

    torchHolder:destroy();

    for e in entitiesAt(torchHolder.level, torchHolder.x, torchHolder.y) do 
      if ( e.name == "torch" and existingTorches[e.id] ~= true ) then
        e:destroy();
      end
    end
  else 
    torchHolder:destroy();
  end
end
The idea (if the code I've just bashed out above doesn't work) is to take an inventory of every torch in the space of the torch holder, then destroy the torch holder and then see if any more torches exist in that space that didn't exist before. I'm assuming (perhaps incorrectly) that the torch in the holder doesn't appear in the entitiesAt() list of torches, before the holder is destroyed - if it does, then this solution will not work.

Just an idea :)
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
Mysterious
Posts: 226
Joined: Wed Nov 06, 2013 8:31 am

Re: Help with Torches Please

Post by Mysterious »

Hi Guys :)

Wow errrr.. I cant wait till those Lua Books come in. Thank you 2 for the coding, I now will sit down and try to figure out how all this coding works. ATM to me it looks complicated but I will try my best to figure it out.

I will also test these 2 pieces of code straight away. I think study them hard would help me answer a lot of questions I have about Lua :)

It seem coming to this place for question was the right choice. Thank you again :)
User avatar
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Help with Torches Please

Post by Eleven Warrior »

Hi guys..

Wow I have to say this is awesome coding. I have been trying to do this myself, but with no luck.. I hope you guys don't mind me pinching the Code..

Good work Desperate Gamers and John.W. Bloody good work. Love it...
Post Reply