Skip to content

Commit

Permalink
New container method container:getItems(recursive) (otland#3160)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekiro authored Oct 18, 2020
1 parent 8263885 commit ec9b367
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,21 @@ std::map<uint32_t, uint32_t>& Container::getAllItemTypeCount(std::map<uint32_t,
return countMap;
}

ItemVector Container::getItems(bool recursive /*= false*/)
{
ItemVector containerItems;
if (recursive) {
for (ContainerIterator it = iterator(); it.hasNext(); it.advance()) {
containerItems.push_back(*it);
}
} else {
for (Item* item : itemlist) {
containerItems.push_back(item);
}
}
return containerItems;
}

Thing* Container::getThing(size_t index) const
{
return getItemByIndex(index);
Expand Down
3 changes: 3 additions & 0 deletions src/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "cylinder.h"
#include "item.h"
#include "tile.h"

class Container;
class DepotChest;
Expand Down Expand Up @@ -143,6 +144,8 @@ class Container : public Item, public Cylinder
std::map<uint32_t, uint32_t>& getAllItemTypeCount(std::map<uint32_t, uint32_t>& countMap) const override final;
Thing* getThing(size_t index) const override final;

ItemVector getItems(bool recursive = false);

void postAddNotification(Thing* thing, const Cylinder* oldParent, int32_t index, cylinderlink_t link = LINK_OWNER) override;
void postRemoveNotification(Thing* thing, const Cylinder* newParent, int32_t index, cylinderlink_t link = LINK_OWNER) override;

Expand Down
24 changes: 24 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,7 @@ void LuaScriptInterface::registerFunctions()
registerMethod("Container", "getCapacity", LuaScriptInterface::luaContainerGetCapacity);
registerMethod("Container", "getEmptySlots", LuaScriptInterface::luaContainerGetEmptySlots);
registerMethod("Container", "getContentDescription", LuaScriptInterface::luaContainerGetContentDescription);
registerMethod("Container", "getItems", LuaScriptInterface::luaContainerGetItems);
registerMethod("Container", "getItemHoldingCount", LuaScriptInterface::luaContainerGetItemHoldingCount);
registerMethod("Container", "getItemCountById", LuaScriptInterface::luaContainerGetItemCountById);

Expand Down Expand Up @@ -6741,6 +6742,29 @@ int LuaScriptInterface::luaContainerGetContentDescription(lua_State* L)
return 1;
}

int LuaScriptInterface::luaContainerGetItems(lua_State* L)
{
// container:getItems([recursive = false])
Container* container = getUserdata<Container>(L, 1);
if (!container) {
lua_pushnil(L);
return 1;
}

bool recursive = getBoolean(L, 2, false);
std::vector<Item*> items = container->getItems(recursive);

lua_createtable(L, items.size(), 0);

int index = 0;
for (Item* item : items) {
pushUserdata(L, item);
setItemMetatable(L, -1, item);
lua_rawseti(L, -2, ++index);
}
return 1;
}

// Teleport
int LuaScriptInterface::luaTeleportCreate(lua_State* L)
{
Expand Down
1 change: 1 addition & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ class LuaScriptInterface
static int luaContainerGetCapacity(lua_State* L);
static int luaContainerGetEmptySlots(lua_State* L);
static int luaContainerGetContentDescription(lua_State* L);
static int luaContainerGetItems(lua_State* L);
static int luaContainerGetItemHoldingCount(lua_State* L);
static int luaContainerGetItemCountById(lua_State* L);

Expand Down

0 comments on commit ec9b367

Please sign in to comment.