Torch dampening script

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Torch dampening script

Post by Komag »

Ah yes of course, I'm thinking of spawned items
Finished Dungeons - complete mods to play
Alcator
Posts: 37
Joined: Fri Apr 13, 2012 9:59 am

Re: Torch dampening script

Post by Alcator »

:D :D :D :D :D

OK! My trick with "tostring()" works, give me a minute and I'll upload a demo map and script that demonstrates it.

:D :D :D :D :D
User avatar
Damonya
Posts: 134
Joined: Thu Feb 28, 2013 1:16 pm
Location: France
Contact:

Re: Torch dampening script

Post by Damonya »

That's very good news. I admit I tested your suggestion, but it' impossible to me to know what to change in the script. :D
Orwak - MOD - Beta version 1.0
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Torch dampening script

Post by Komag »

Alcator wrote::D :D :D :D :D

OK! My trick with "tostring()" works, give me a minute and I'll upload a demo map and script that demonstrates it.

:D :D :D :D :D
Awesome! Make sure to test it with spawned torches just to be sure (such as spawning one with the console or with a script when pushing a button or something)
Finished Dungeons - complete mods to play
Alcator
Posts: 37
Joined: Fri Apr 13, 2012 9:59 am

Re: Torch dampening script

Post by Alcator »

OK, so, here it is:

Download source + exported dungeon - proof of concept:
http://www.fileden.com/files/2006/8/10/ ... ngTest.zip

The key lies in using tostring(torch.id) to change an ID into a STRING,
and then, conversely, using findEntity(stringID) to get a handle for the Object.

Explanation of code:

This code creates 5 randomly filled torches and puts them into an alcove. This will verify how the code handles torches with "automatic" ID:

Code: Select all

function createTorches()
  for i = 1,5 do
    local prgTorch = spawn("torch")
    prgTorch:setFuel(math.random(150,600))
    torchAlcove:addItem(prgTorch)
  end
end

createTorches()
"Global" variables:

Code: Select all

-- how many torches shall we remember (at most)? Originally was 25 for some reason.
CONST_MAX_TORCH = 100
-- we store torch fuel in this array:    
TF = {} 
-- we store torch "id" in this array:
TID = {}
Dampening script:

Code: Select all

function torchout()
      for i=1,4 do
        for j=1,31 do
          local t = party:getChampion(i):getItem(j)
          if t then
             if string.sub(t.name,1,5) == "torch" and t:getFuel() > 0 then
                print(tostring(t.id).." has: "..tostring(t:getFuel()).." fuel left")
                table.insert(TID,tostring(t.id))
                local f = t:getFuel()
                table.insert(TF,f)
                t:setFuel(0)     
             end
          end     
        end

       -- optional section: if you wish, we can also dampen all torches in the whole level:
       if true then   -- change "true" to "false" to disable this behavior.
         for itm in allEntities(party.level) do
           if itm and (itm.name == "torch") then
             print(tostring(itm.id).." has: "..tostring(itm:getFuel()).." fuel left")
             table.insert(TID,tostring(itm.id))
             local f = itm:getFuel()
             table.insert(TF,f)
             itm:setFuel(0)               
           end  
         end
       end
      end

      local mt = getMouseItem()
      if mt and (string.sub(mt.name,1,5) == "torch") then
         print(tostring(mt.id).." has "..tostring(mt:getFuel()).." fuel left")
         table.insert(TID,tostring(mt.id))
         local f = mt:getFuel()
         table.insert(TF,f)
         getMouseItem():setFuel(0)
      end
    end
The function has three sections. From top to bottom they are:
1. Find and "dampen" torches in inventory
2. Find and dampen torches everywhere else in the level
3. Check the mouseItem

The following sub-routine was written to find specific torch in the inventory.
It simply goes through all champions, and their inventory, and looks for one specific torch.
The intended fuel to be restored to that torch is passed as the second parameter.
If it is not found in inventory, the script also checks the mouseItem, just in case.
It returns TRUE if it was found, or FALSE if it wasn't found.

Code: Select all

   function restoreParticularTorch(torchID,torchFuel)
     -- look for the torch in inventory/equipped in hands:
     for iteratorParty = 1,4 do
       if party:getChampion(iteratorParty) ~= nil then
         for iteratorInventory = 1,31 do
           local thisItem
           if party:getChampion(iteratorParty):getItem(iteratorInventory) ~= nil then
             thisItem = party:getChampion(iteratorParty):getItem(iteratorInventory)
             if thisItem.id == torchID then
               thisItem:setFuel(torchFuel)
               return true
             end
           end  
         end
       end
     end
     -- if we got here, that means it's not in the inventory of any hero -- check mouseItem:
     local mt = getMouseItem()
     if mt and (string.sub(mt.name,1,5) == "torch") and (mt.id == torchID) then
       mt:setFuel(torchFuel)
       return true 
     end
     -- the torch was destroyed/removed from game (perhaps by a different script?)
     return false
   end
So, finally, we can look at the restore script, which goes through the list of remembered torches and looks for each of them:

Code: Select all

function torchrestore()
      for i=1,CONST_MAX_TORCH do
        if TID[i] ~= nil then
          print(TID[i].." has "..tostring(TF[i]).." fuel stored in memory ")
         -- look for torches somewhere in the level.
          local tor = findEntity(TID[i])
          if tor ~= nil then
            -- OK the player has placed the dampened torch somewhere into the level and we can restore it.
             tor:setFuel(TF[i])
             print("... and I found it in the level and restored it!")
          else
            -- it is in the party inventory or hand. Go through all inventories 
            -- of all heroes and look for an object with this ID,
            -- then change the torch's fuel:
            if restoreParticularTorch(TID[i],TF[i]) then
              print(" it was hiding in your inventory, but I found it!")
            else
              print(" now where the hell is it???")
            end            
          end
        end 
      end
    end
Small issue: When the torches are restored, they don't get "turned on", until the player picks them up with the mouse and puts them down again. Personally, I don't think this is such a problem, because gameplay-wise, the player is actually igniting the torches just before he or she puts them in their hands, so it makes sense this has to be done also for "dried-out" torches. In the test map, this stands out "badly", if you have 8 torches in hands and have to click on each of them to "light it up", but in the intended game scenario, the torches would likely be in the backpack, with perhaps 1 torch in hand. Easy to deal with using a single "line of dialogue".

Enjoy!
User avatar
Damonya
Posts: 134
Joined: Thu Feb 28, 2013 1:16 pm
Location: France
Contact:

Re: Torch dampening script

Post by Damonya »

Awesome. :o
I tested your mod test and it runs great. You have even improved with the torches on the walls. Congratulation. :)
Orwak - MOD - Beta version 1.0
Alcator
Posts: 37
Joined: Fri Apr 13, 2012 9:59 am

Re: Torch dampening script

Post by Alcator »

Returning to the community what it has given me :-) Everyone is free to use that.
User avatar
akroma222
Posts: 1029
Joined: Thu Oct 04, 2012 10:08 am

Re: Torch dampening script

Post by akroma222 »

This has been quite a good read, thank you :)
Post Reply