From e665ae2f3888edada5dcf8ee0eb63bfb5e8fc613 Mon Sep 17 00:00:00 2001 From: arielcami <71854140+arielcami@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:35:37 -0500 Subject: [PATCH] feat(LuaEngine): Add GetInventoryFreeSlots and GetBankFreeSlots to Player class (#369) --- src/LuaEngine/LuaFunctions.cpp | 2 + src/LuaEngine/methods/PlayerMethods.h | 67 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/LuaEngine/LuaFunctions.cpp b/src/LuaEngine/LuaFunctions.cpp index 9c0ee65..36cb209 100644 --- a/src/LuaEngine/LuaFunctions.cpp +++ b/src/LuaEngine/LuaFunctions.cpp @@ -478,6 +478,8 @@ ALERegister UnitMethods[] = ALERegister PlayerMethods[] = { // Getters + { "GetInventoryFreeSlots", &LuaPlayer::GetInventoryFreeSlots }, + { "GetBankFreeSlots", &LuaPlayer::GetBankFreeSlots }, { "GetSelection", &LuaPlayer::GetSelection }, { "GetGMRank", &LuaPlayer::GetGMRank }, { "GetGuildId", &LuaPlayer::GetGuildId }, diff --git a/src/LuaEngine/methods/PlayerMethods.h b/src/LuaEngine/methods/PlayerMethods.h index f3f0ddf..d0730dd 100644 --- a/src/LuaEngine/methods/PlayerMethods.h +++ b/src/LuaEngine/methods/PlayerMethods.h @@ -316,6 +316,73 @@ namespace LuaPlayer return 1; } + /** + * Returns the number of free slots in the [Player]'s inventory (backpack and equipped bags). + * + * @return uint32 freeSlots + */ + int GetInventoryFreeSlots(lua_State* L, Player* player) + { + uint32 freeSlots = 0; + + // Backpack slots in INVENTORY_SLOT_BAG_0 (INVENTORY_SLOT_ITEM_START to INVENTORY_SLOT_ITEM_END) + for (uint8 i = INVENTORY_SLOT_ITEM_START; i < INVENTORY_SLOT_ITEM_END; ++i) + { + if (!player->GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + ++freeSlots; + } + + // Check equipped bags slots (INVENTORY_SLOT_BAG_START to INVENTORY_SLOT_BAG_END) + for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) + { + if (Bag* bag = player->GetBagByPos(i)) + { + for (uint32 j = 0; j < bag->GetBagSize(); ++j) + { + if (!player->GetItemByPos(i, j)) + ++freeSlots; + } + } + } + + ALE::Push(L, freeSlots); + return 1; + } + + /** + * Returns the number of free slots in the [Player]'s bank (main bank and bank bags). + * + * @return uint32 freeSlots + */ + int GetBankFreeSlots(lua_State* L, Player* player) + { + uint32 freeSlots = 0; + + // Check main bank slots (BANK_SLOT_ITEM_START to BANK_SLOT_ITEM_END) + for (uint8 i = BANK_SLOT_ITEM_START; i < BANK_SLOT_ITEM_END; ++i) + { + if (!player->GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + ++freeSlots; + } + + // Check bank bags slots (BANK_SLOT_BAG_START to BANK_SLOT_BAG_END) + for (uint8 i = BANK_SLOT_BAG_START; i < BANK_SLOT_BAG_END; ++i) + { + if (Bag* bag = player->GetBagByPos(i)) + { + for (uint32 j = 0; j < bag->GetBagSize(); ++j) + { + if (!player->GetItemByPos(i, j)) + ++freeSlots; + } + } + } + + ALE::Push(L, freeSlots); + return 1; + } + + /** * Returns `true` if the [Player] has a Tank Specialization, `false` otherwise. *