From 147a699a9720328aa65a36fcea6d28c4ec8c63ca Mon Sep 17 00:00:00 2001 From: ANAND Date: Thu, 11 Apr 2019 20:36:23 +0530 Subject: [PATCH] InvRef: Add convenience method add_list for bulk transfer of items --- src/script/common/c_content.cpp | 20 ++++++++++++------- src/script/common/c_content.h | 3 +++ src/script/lua_api/l_inventory.cpp | 31 ++++++++++++++++++++++++++++++ src/script/lua_api/l_inventory.h | 3 +++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 361db226e050..bb3f3a84ffc3 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1276,19 +1276,25 @@ void push_tool_capabilities(lua_State *L, } /******************************************************************************/ -void push_inventory_list(lua_State *L, Inventory *inv, const char *name) +void push_inventory_list_raw(lua_State *L, InventoryList *invlist) { - InventoryList *invlist = inv->getList(name); - if(invlist == NULL){ - lua_pushnil(L); - return; - } std::vector items; - for(u32 i=0; igetSize(); i++) + for (u32 i = 0; i < invlist->getSize(); i++) items.push_back(invlist->getItem(i)); + push_items(L, items); } +/******************************************************************************/ +void push_inventory_list(lua_State *L, Inventory *inv, const char *name) +{ + InventoryList *invlist = inv->getList(name); + if (!invlist) + lua_pushnil(L); + else + push_inventory_list_raw(L, invlist); +} + /******************************************************************************/ void read_inventory_list(lua_State *L, int tableindex, Inventory *inv, const char *name, Server* srv, int forcesize) diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index f3a653682ab9..1818694f5e06 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -53,6 +53,7 @@ struct ObjectProperties; struct SimpleSoundSpec; struct ServerSoundParams; class Inventory; +class InventoryList; struct NodeBox; struct ContentFeatures; struct TileDef; @@ -112,6 +113,8 @@ void read_object_properties (lua_State *L, int index, void push_object_properties (lua_State *L, ObjectProperties *prop); +void push_inventory_list_raw (lua_State *L, + InventoryList *invlist); void push_inventory_list (lua_State *L, Inventory *inv, const char *name); diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index 6e7afa4a4e67..868cc053e00a 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -241,6 +241,36 @@ int InvRef::l_set_list(lua_State *L) return 0; } +// add_list(self, listname, list) +int InvRef::l_add_list(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + InvRef *ref = checkobject(L, 1); + Inventory *inv = getinv(L, ref); + if (!inv) + return 0; + + const char *dst_listname = luaL_checkstring(L, 2); + const char *src_listname = luaL_checkstring(L, 3); + + InventoryList *dst_list = inv->getList(dst_listname); + InventoryList *src_list = inv->getList(src_listname); + + // Create new list to store leftovers + InventoryList *leftover_list = new InventoryList("leftover", + src_list->getSize(), getServer(L)->getItemDefManager()); + + for (u32 i = 0; i < src_list->getSize(); i++) { + ItemStack leftover = dst_list->addItem(src_list->getItem(i)); + if (!leftover.empty()) + leftover_list->addItem(leftover); + } + + push_inventory_list_raw(L, leftover_list); + delete leftover_list; + return 1; +} + // get_lists(self) -> list of InventoryLists int InvRef::l_get_lists(lua_State *L) { @@ -472,6 +502,7 @@ const luaL_Reg InvRef::methods[] = { luamethod(InvRef, set_stack), luamethod(InvRef, get_list), luamethod(InvRef, set_list), + luamethod(InvRef, add_list), luamethod(InvRef, get_lists), luamethod(InvRef, set_lists), luamethod(InvRef, add_item), diff --git a/src/script/lua_api/l_inventory.h b/src/script/lua_api/l_inventory.h index 94f670c9d51d..09b9ad2e7a76 100644 --- a/src/script/lua_api/l_inventory.h +++ b/src/script/lua_api/l_inventory.h @@ -78,6 +78,9 @@ class InvRef : public ModApiBase { // set_list(self, listname, list) static int l_set_list(lua_State *L); + // add_list(self, listname, list) + static int l_add_list(lua_State *L); + // get_lists(self) -> list of InventoryLists static int l_get_lists(lua_State *L);