From adde060772b506642cf15f7452144e2c29e64ff7 Mon Sep 17 00:00:00 2001 From: Matthew Cline Date: Sat, 14 Nov 2009 04:36:31 -0800 Subject: l_dgnit.cc: dgn.stash_items() function --- crawl-ref/source/l_dgnit.cc | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'crawl-ref/source') diff --git a/crawl-ref/source/l_dgnit.cc b/crawl-ref/source/l_dgnit.cc index 7d17571356..6512c3233e 100644 --- a/crawl-ref/source/l_dgnit.cc +++ b/crawl-ref/source/l_dgnit.cc @@ -12,6 +12,7 @@ #include "dungeon.h" #include "items.h" #include "mapdef.h" +#include "stash.h" #define ITEMLIST_METATABLE "crawldgn.item_list" @@ -87,12 +88,63 @@ static int dgn_create_item(lua_State *ls) return (0); } +// Returns two arrays: one of floor items, one of shop items. +static int dgn_stash_items(lua_State *ls) +{ + unsigned min_value = lua_isnumber(ls, 1) ? luaL_checkint(ls, 1) : 0; + bool skip_stackable = lua_isboolean(ls, 2) ? lua_toboolean(ls, 2) + : false; + std::vector floor_items; + std::vector shop_items; + + for (ST_ItemIterator stii; stii; ++stii) + { + if (skip_stackable && is_stackable_item(*stii)) + continue; + if (min_value > 0) + { + if (stii.shop()) + { + if (stii.price() < min_value) + continue; + } + else if (item_value(*stii, true) < min_value) + continue; + } + if (stii.shop()) + shop_items.push_back(&(*stii)); + else + floor_items.push_back(&(*stii)); + } + + lua_newtable(ls); + int index = 0; + + for (unsigned int i = 0; i < floor_items.size(); i++) + { + lua_pushlightuserdata(ls, const_cast(floor_items[i])); + lua_rawseti(ls, -2, ++index); + } + + lua_newtable(ls); + index = 0; + + for (unsigned int i = 0; i < shop_items.size(); i++) + { + lua_pushlightuserdata(ls, const_cast(floor_items[i])); + lua_rawseti(ls, -2, ++index); + } + + return (2); +} + const struct luaL_reg dgn_item_dlib[] = { { "item_from_index", dgn_item_from_index }, { "items_at", dgn_items_at }, { "create_item", dgn_create_item }, { "item_spec", _dgn_item_spec }, +{ "stash_items", dgn_stash_items }, { NULL, NULL } }; -- cgit v1.2.3-54-g00ecf