Iterators
Posted: Thu Mar 21, 2013 1:37 am
Hi everyone. Previously, I wrote a redefinition for allEntities to avoid a certain bug that was identified with the standard definition. I have also written some non-standard iterators for situations that I see discussed frequently on here:
Edit: There are some flaws with the code in this post, but this documentation remains accurate. Please see replies in this thread for details of flaws and updated code.
allEntities( level ) - Just another equivalent redefinition of the same thing.
entitiesWithin( level , corner1x , corner1y , corner2x , corner2y ) - You specify the coordinates for a bounding box (inclusive) and it gives you the entitiesAt each tile within that box.
carriedItems( champion ) - champion may be either a number or a champion returned by party:getChampion(slot) . If it is a number, then the code just calls party:getChampion(champion) to convert it. It gives you all the items carried by that champion in any equipment or backpack slot, and all the items that are contained within items carried by that champion.
allCarriedItems() - It gives you all the items carried by any champion in any equipment or backpack slot, and all the items that are contained within items carried by any champion, and the mouse item, and all items that are contained within the mouse item.
If you'd like to use any of these iterators, I would recommend pasting this code into a script_entity ( hereafter referred to as "iteratorScript" ) and then in your other script_entity where you want to use the iterators, follow either of these approaches:
or
As always, if you find any bugs or want to request any additional features, don't hesitate to post it here
Edit: allCarriedItems now includes items contained inside the mouse item.
Edit: There are some flaws with the code in this post, but this documentation remains accurate. Please see replies in this thread for details of flaws and updated code.
allEntities( level ) - Just another equivalent redefinition of the same thing.
entitiesWithin( level , corner1x , corner1y , corner2x , corner2y ) - You specify the coordinates for a bounding box (inclusive) and it gives you the entitiesAt each tile within that box.
carriedItems( champion ) - champion may be either a number or a champion returned by party:getChampion(slot) . If it is a number, then the code just calls party:getChampion(champion) to convert it. It gives you all the items carried by that champion in any equipment or backpack slot, and all the items that are contained within items carried by that champion.
allCarriedItems() - It gives you all the items carried by any champion in any equipment or backpack slot, and all the items that are contained within items carried by any champion, and the mouse item, and all items that are contained within the mouse item.
Code: Select all
allEntities = function( level )
return entitiesWithin( level , 0 , 0 , 31 , 31 )
end
entitiesWithin = function( level , corner1x , corner1y , corner2x , corner2y )
--The arguments should specify two corners of a rectangle,
--diagonally across from one another
if corner1x > corner2x then
corner1x , corner2x = corner2x , corner1x
end
if corner1y > corner2y then
corner1y , corner2y = corner2y , corner1y
end
local state = {}
state["offset"] = 0
local count = 0
for i = corner1x , corner2x do
for j = corner1y , corner2y do
for k in entitiesAt( level , i , j ) do
count = count + 1
state[count] = k
end
end
end
return advance , state , nil
end
carriedItems = function( champion )
--champion may be an actual champion, or a number
if type( champion ) == "number" then
champion = party:getChampion( champion )
end
if not champion then
return advance , { offset = 0 } , nil
end
local state = {}
state["offset"] = 0
local count = 0
for i = 1 , 31 do
local item = champion:getItem( i )
if item then
for containedItem in item:containedItems() do
count = count + 1
state[count] = containedItem
end
count = count + 1
state[count] = item
end
end
return advance , state , nil
end
allCarriedItems = function()
local state = {}
state["offset"] = 0
local count = 0
for i = 1 , 4 do
for item in carriedItems( i ) do
count = count + 1
state[count] = item
end
end
local mouseItem = getMouseItem()
if mouseItem then
for containedItem in mouseItem:containedItems() do
count = count + 1
state[count] = containedItem
end
count = count + 1
state[count] = mouseItem
end
return advance , state , nil
end
------------------------------------------
--[[Functions below this line are intended
for internal use only.
--]]
advance = function ( state , _ )
local offset = state["offset"] + 1
state["offset"] = offset
return state[offset]
end
Code: Select all
for item in iteratorScript.allCarriedItems() do
if item.name == "torch" then
--do something with that item
end
end
Code: Select all
--At the top of your script_entity
allCarriedItems = iteratorScript.allCarriedItems
--other code here
for item in allCarriedItems() do
if item.name == "torch" then
--do something with that item
end
end
Edit: allCarriedItems now includes items contained inside the mouse item.