diff options
author | Adam Borowski <kilobyte@angband.pl> | 2012-04-25 16:35:56 +0200 |
---|---|---|
committer | Adam Borowski <kilobyte@angband.pl> | 2012-04-25 16:35:56 +0200 |
commit | e8da5e6c01950dd649f90030a2c77654480e4369 (patch) | |
tree | b8749886499f49f092be6362fe1e42bbabd53bfd /crawl-ref/source/l_item.cc | |
parent | b15ddc7bf0397d5034f0514c2d76e717da0819ba (diff) | |
parent | fb959798448db7e13e6485f7fece472404f21f0e (diff) | |
download | crawl-ref-e8da5e6c01950dd649f90030a2c77654480e4369.tar.gz crawl-ref-e8da5e6c01950dd649f90030a2c77654480e4369.zip |
Merge branch 'master' into portal_branches
Diffstat (limited to 'crawl-ref/source/l_item.cc')
-rw-r--r-- | crawl-ref/source/l_item.cc | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/crawl-ref/source/l_item.cc b/crawl-ref/source/l_item.cc index ac04faef85..7d76d762ba 100644 --- a/crawl-ref/source/l_item.cc +++ b/crawl-ref/source/l_item.cc @@ -11,7 +11,9 @@ #include "l_libs.h" #include "artefact.h" +#include "cluautil.h" #include "colour.h" +#include "coord.h" #include "command.h" #include "env.h" #include "enum.h" @@ -20,10 +22,12 @@ #include "item_use.h" #include "itemprop.h" #include "items.h" +#include "l_defs.h" #include "output.h" #include "player.h" #include "skills2.h" #include "spl-summoning.h" +#include "stash.h" #include "stuff.h" ///////////////////////////////////////////////////////////////////// @@ -32,6 +36,7 @@ struct item_wrapper { item_def *item; + bool temp; // Does item need to be freed when the wrapper is GCed? int turn; bool valid() const { return turn == you.num_turns; } @@ -41,6 +46,17 @@ void clua_push_item(lua_State *ls, item_def *item) { item_wrapper *iw = clua_new_userdata<item_wrapper>(ls, ITEM_METATABLE); iw->item = item; + iw->temp = false; + iw->turn = you.num_turns; +} + +// Push a (wrapped) temporary item_def. A copy of the item will be allocated, +// then deleted when the wrapper is GCed. +void clua_push_item_temp(lua_State *ls, const item_def &item) +{ + item_wrapper *iw = clua_new_userdata<item_wrapper>(ls, ITEM_METATABLE); + iw->item = new item_def(item); + iw->temp = true; iw->turn = you.num_turns; } @@ -871,9 +887,7 @@ static int dmx_get_qty(lua_State *ls, int ndx, int subndx) lua_pop(ls, 1); } else if (lua_isnumber(ls, ndx)) - { qty = luaL_checkint(ls, ndx); - } return (qty); } @@ -984,6 +998,33 @@ static int l_item_inslot(lua_State *ls) return (1); } +static int l_item_get_items_at(lua_State *ls) +{ + coord_def s; + s.x = luaL_checkint(ls, 1); + s.y = luaL_checkint(ls, 2); + coord_def p = player2grid(s); + if (!map_bounds(p)) + return 0; + + item_def* top = env.map_knowledge(p).item(); + if (!top || !top->defined()) + return 0; + + lua_newtable(ls); + + const std::vector<item_def> items = item_list_in_stash(p); + int index = 0; + for (std::vector<item_def>::const_iterator i = items.begin(); + i != items.end(); ++i) + { + clua_push_item_temp(ls, *i); + lua_rawseti(ls, -2, ++index); + } + + return (1); +} + struct ItemAccessor { const char *attribute; @@ -1060,9 +1101,18 @@ static const struct luaL_reg item_lib[] = { "equipped_at", l_item_equipped_at }, { "fired_item", l_item_fired_item }, { "inslot", l_item_inslot }, + { "get_items_at", l_item_get_items_at }, { NULL, NULL }, }; +static int _delete_wrapped_item(lua_State *ls) +{ + item_wrapper *iw = static_cast<item_wrapper*>(lua_touserdata(ls, 1)); + if (iw && iw->temp) + delete iw->item; + return (0); +} + void cluaopen_item(lua_State *ls) { luaL_newmetatable(ls, ITEM_METATABLE); @@ -1070,6 +1120,10 @@ void cluaopen_item(lua_State *ls) lua_pushcfunction(ls, item_get); lua_settable(ls, -3); + lua_pushstring(ls, "__gc"); + lua_pushcfunction(ls, _delete_wrapped_item); + lua_settable(ls, -3); + // Pop the metatable off the stack. lua_pop(ls, 1); |