diff options
author | Jude Brown <bookofjude@users.sourceforge.net> | 2009-11-22 15:21:55 +1000 |
---|---|---|
committer | Jude Brown <bookofjude@users.sourceforge.net> | 2009-11-22 15:21:55 +1000 |
commit | 17e64a4967e5311ea38d014d942c87e2f7cd4b65 (patch) | |
tree | 390c3b19bf0e75a7f58bc0267ea96178e05d6ccd /crawl-ref/source/dat/clua/iter.lua | |
parent | 47ed5643726083a8b438bd11a0efdc9f28dcb94a (diff) | |
download | crawl-ref-17e64a4967e5311ea38d014d942c87e2f7cd4b65.tar.gz crawl-ref-17e64a4967e5311ea38d014d942c87e2f7cd4b65.zip |
Iteration-related functions: stack_search and stack_destroy.
The first will return the first item whose name matches the search term
in the relevant stack, while the second will destroy each item in the
stack. Hopefully the precautions in stack_destroy will be enough.
Diffstat (limited to 'crawl-ref/source/dat/clua/iter.lua')
-rw-r--r-- | crawl-ref/source/dat/clua/iter.lua | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/crawl-ref/source/dat/clua/iter.lua b/crawl-ref/source/dat/clua/iter.lua index 95bdc863e3..17dd95ffac 100644 --- a/crawl-ref/source/dat/clua/iter.lua +++ b/crawl-ref/source/dat/clua/iter.lua @@ -220,3 +220,66 @@ function iter.circle_iterator (radius, ic, filter, center) return iter.rect_iterator(top_corner, bottom_corner, check_dist) end + +------------------------------------------------------------------------------- +-- item stack-related functions +-- not necessarily iterators +------------------------------------------------------------------------------- + +function iter.stack_search (coord, term, extra) + local _x, _y + if extra ~= nil then + _x = coord + _y = term + term = extra + elseif coord == nil then + error("stack_search requires a location") + else + _x, _y = coord:xy() + end + + if term == nil then + error("stack_search requires a search term") + end + + local stack = dgn.items_at(_x, _y) + if #stack == 0 then + error("no stack at " .. _x .. "/" .. _y) + end + + for _, item in ipairs(stack) do + if string.find(items.name(item), (term)) then + return item + end + end + + return false +end + +function iter.stack_destroy(coord, extra) + local _x, _y + if extra ~= nil then + _x = coord + _y = extra + elseif coord == nil then + error("stack_search requires a location") + else + _x, _y = coord:xy() + end + + local stack = dgn.items_at(_x, _y) + + while #stack ~= 0 do + if items.destroy(stack[1]) then + if #stack >= dgn.items_at(_x, _y) then + error("destroyed an item ('" .. items.name(stack[1]) .. "'), but the " + .. "stack size is the same") + return + end + stack = dgn.items_at(_x, _y) + else + error("couldn't destroy item '" .. items.name(stack[1]) .. "'") + return false + end + end +end |