Re: Torch dampening script
Posted: Tue Mar 19, 2013 2:32 pm
Ah yes of course, I'm thinking of spawned items
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)Alcator wrote:![]()
![]()
![]()
![]()
![]()
OK! My trick with "tostring()" works, give me a minute and I'll upload a demo map and script that demonstrates it.
![]()
![]()
![]()
![]()
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()
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 = {}
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
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
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