From 72f698f95ead15875ea4e330da5d9d36520873bd Mon Sep 17 00:00:00 2001 From: Mats391 Date: Tue, 30 Dec 2025 16:53:13 +0100 Subject: [PATCH] Getting content of specific bags only --- nampower/item_scripts.cpp | 143 +++++++++++++++++++++++++++----------- 1 file changed, 101 insertions(+), 42 deletions(-) diff --git a/nampower/item_scripts.cpp b/nampower/item_scripts.cpp index b8f0354..6fbeda1 100644 --- a/nampower/item_scripts.cpp +++ b/nampower/item_scripts.cpp @@ -767,6 +767,8 @@ namespace Nampower { } } + + uint32_t Script_GetBagItem(uintptr_t *luaState) { luaState = GetLuaStatePtr(); @@ -890,62 +892,57 @@ namespace Nampower { return 1; } - uint32_t Script_GetBagItems(uintptr_t *luaState) { - luaState = GetLuaStatePtr(); - + uint32_t GetSingleBagItems(uintptr_t *luaState, int32_t bagIndex) { auto const getContainerGuid = reinterpret_cast(Offsets::GetContainerGuid); auto const getBagItem = reinterpret_cast(Offsets::CGBag_C_GetItemAtSlot); - // Get or create reusable main table - GetTableRef(luaState, bagItemsTableRef); - auto playerGuid = game::ClntObjMgrGetActivePlayerGuid(); - auto player = game::GetObjectPtr(playerGuid); - auto inventory = game::GetPlayerInventoryPtr(player); + if (bagIndex == 0) { + const auto playerGuid = game::ClntObjMgrGetActivePlayerGuid(); + const auto player = game::GetObjectPtr(playerGuid); + const auto inventory = game::GetPlayerInventoryPtr(player); + GetTableRef(luaState, bagTableRefs[0]); - // Backpack (bag 0) - lua_pushnumber(luaState, static_cast(0)); - GetTableRef(luaState, bagTableRefs[0]); - - for (uint32_t slot = 23; slot <= 38; slot++) { - auto item = getBagItem(inventory, slot); - uint32_t adjustedSlot = slot - 0x17; - if (item) { - PushBagCGItemToTable(luaState, 0, slot, item); - } else { - // Check if item was removed from this slot - ClearBagSlot(luaState, 0, adjustedSlot); + for (uint32_t slot = 23; slot <= 38; slot++) { + auto item = getBagItem(inventory, slot); + uint32_t adjustedSlot = slot - 0x17; + if (item) { + PushBagCGItemToTable(luaState, 0, slot, item); + } else { + // Check if item was removed from this slot + ClearBagSlot(luaState, 0, adjustedSlot); + } } + return 1; } - lua_settable(luaState, -3); - - // Bags 1-4 - for (int32_t bagIndex = 1; bagIndex <= 4; bagIndex++) { + if (bagIndex >= 1 && bagIndex <= 4) { uint64_t containerGuid = getContainerGuid(bagIndex - 1); // bagIndex 1-4 maps to container 0-3 if (containerGuid == 0) { // Bag slot is empty, clear all cached items for this bag - lua_pushnumber(luaState, static_cast(bagIndex)); GetTableRef(luaState, bagTableRefs[bagIndex]); for (uint32_t slot = 0; slot < MAX_BAG_SLOTS; slot++) { ClearBagSlot(luaState, bagIndex, slot); } - lua_settable(luaState, -3); - continue; + return 1; } auto containerPtr = game::ClntObjMgrObjectPtr(game::TYPEMASK_CONTAINER, containerGuid); - if (!containerPtr) continue; + if (!containerPtr) { + return 0; + } auto bagPtr = GetBagPtrFromContainer(containerPtr); - if (!bagPtr) continue; + if (!bagPtr) { + return 0; + } - lua_pushnumber(luaState, static_cast(bagIndex)); GetTableRef(luaState, bagTableRefs[bagIndex]); auto bagSize = *bagPtr; // Check all possible slots up to bag size - for (uint32_t slot = 0; slot < bagSize && slot < MAX_BAG_SLOTS; slot++) { + for (uint32_t slot = 0; + slot < bagSize && slot < MAX_BAG_SLOTS; slot++) { auto item = getBagItem(bagPtr, slot); if (item) { PushBagCGItemToTable(luaState, bagIndex, slot, item); @@ -953,33 +950,33 @@ namespace Nampower { ClearBagSlot(luaState, bagIndex, slot); } } - - lua_settable(luaState, -3); + return 1; } - // Bank bags 5-9 - uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); - if (bankGuid > 0) { - for (int32_t bagIndex = 5; bagIndex <= 9; bagIndex++) { + if (bagIndex >= 5 && bagIndex <= 9) { + uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); + if (bankGuid > 0) { uint64_t containerGuid = getContainerGuid(bagIndex - 1); // bagIndex 5-9 maps to container 4-8 if (containerGuid == 0) { // Bag slot is empty, clear all cached items for this bag - lua_pushnumber(luaState, static_cast(bagIndex)); GetTableRef(luaState, bagTableRefs[bagIndex]); for (uint32_t slot = 0; slot < MAX_BAG_SLOTS; slot++) { ClearBagSlot(luaState, bagIndex, slot); } lua_settable(luaState, -3); - continue; + return 1; } auto containerPtr = game::ClntObjMgrObjectPtr(game::TYPEMASK_CONTAINER, containerGuid); - if (!containerPtr) continue; + if (!containerPtr) { + return 0; + } auto bagPtr = GetBagPtrFromContainer(containerPtr); - if (!bagPtr) continue; + if (!bagPtr) { + return 0; + } - lua_pushnumber(luaState, static_cast(bagIndex)); GetTableRef(luaState, bagTableRefs[bagIndex]); auto bagSize = *bagPtr; @@ -993,12 +990,74 @@ namespace Nampower { } } + return 1; + } + } + + + // TODO Error for not finding the bag + return 0; + } + + uint32_t GetAllBagItems(uintptr_t* luaState) { + // Get or create reusable main table + GetTableRef(luaState, bagItemsTableRef); + + // Backpack (bag 0) + lua_pushnumber(luaState, static_cast(0)); + GetSingleBagItems(luaState, 0); + + lua_settable(luaState, -3); + + // Bags 1-4 + for (int32_t bagIndex = 1; bagIndex <= 4; bagIndex++) { + + const auto top = lua_gettop(luaState); + lua_pushnumber(luaState, static_cast(bagIndex)); + const auto bagResult = GetSingleBagItems(luaState, bagIndex); + if (!bagResult) { + lua_settop(luaState, top); + } + else { lua_settable(luaState, -3); } } + // Bank bags 5-9 + uint64_t bankGuid = *reinterpret_cast(Offsets::BankGuid); + if (bankGuid > 0) { + for (int32_t bagIndex = 5; bagIndex <= 9; bagIndex++) { + const auto top = lua_gettop(luaState); + lua_pushnumber(luaState, static_cast(bagIndex)); + const auto bagResult = GetSingleBagItems(luaState, bagIndex); + if (!bagResult) { + lua_settop(luaState, top); + } + else { + lua_settable(luaState, -3); + } + } + } + return 1; } + uint32_t Script_GetBagItems(uintptr_t* luaState) { + luaState = GetLuaStatePtr(); + + if (lua_gettop(luaState) == 1) { + + if (!lua_isnumber(luaState, 1)) { + lua_error(luaState, "Bag index must be a number"); + return 0; + } + + const auto bagIndex = static_cast(lua_tonumber(luaState, 1)); + return GetSingleBagItems(luaState, bagIndex); + } + + return GetAllBagItems(luaState); + } + }