From 53188851372391c7bbfa0ebcb35258579660fa3f Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Wed, 13 May 2026 11:06:15 -0500 Subject: [PATCH] GetItemCount should include equipped items --- docs/API.md | 20 +++++++++------- src/item/Count.cpp | 58 +++++++++++++++++++++++++++++----------------- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/docs/API.md b/docs/API.md index c8e29b48..bbb80a4b 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1590,8 +1590,8 @@ modern `C_Item.GetItemFamily` introduced in 10.x. ### `C_Item.GetItemCount(itemInfo, [includeBank], [includeUses])` -Returns the player's total count of `itemInfo` across bags (and -optionally bank slots). +Returns the player's total count of `itemInfo` — equipped items + +bags, and optionally bank. ``` count = C_Item.GetItemCount(itemInfo [, includeBank [, includeUses]]) @@ -1607,9 +1607,13 @@ count = C_Item.GetItemCount(itemInfo [, includeBank [, includeUses]]) See note below. ```lua -local n = C_Item.GetItemCount(2589) -- Linen Cloth in bags -local n = C_Item.GetItemCount(2589, true) -- bags + bank +local n = C_Item.GetItemCount(2589) -- Linen Cloth in bags + equipped +local n = C_Item.GetItemCount(2589, true) -- + bank local n = C_Item.GetItemCount("item:2589") -- string form works too + +-- Equipped items count toward the total: +local trinketID = GetInventoryItemID("player", INVSLOT_TRINKET1) +C_Item.GetItemCount(trinketID) -- 1 ``` > **`includeUses` not yet supported.** Modern semantics: when `true`, @@ -1621,10 +1625,6 @@ local n = C_Item.GetItemCount("item:2589") -- string form works too > `false` produce the same number (sum of stack counts). Track > [TODO #28] for charges support. -> **Equipped items don't count.** Matches modern API. A wand in your -> ranged slot won't be counted; only items in bags and (optionally) -> bank. - > **Bank works cold — no banker visit required.** The 1.12 server > sends bank inventory at login alongside the rest of the player's > data; only the engine's own `GetItemBySlot` gates bank slots until @@ -1634,7 +1634,9 @@ local n = C_Item.GetItemCount("item:2589") -- string form works too > internally if the gate let us through. Counts are correct from > session start. -Walks via the same `Item::Location::ResolveBag` chain +Walks the 19 equipment slots via +`Item::Location::ResolveEquipmentSlot`, then bags via the same +`Item::Location::ResolveBag` chain [`C_Container.GetContainerItemID`](#c_containergetcontaineritemidbagindex-slotindex) uses. Slot counts come from the engine's `Script_GetContainerNumSlots` so custom server bag sizes work transparently. Stack counts read diff --git a/src/item/Count.cpp b/src/item/Count.cpp index 506f3346..ca3e49a2 100644 --- a/src/item/Count.cpp +++ b/src/item/Count.cpp @@ -173,24 +173,44 @@ int CountInBankBags(int targetItemID) { return total; } +// Walks the player's 19 equipment slots and accumulates stack counts +// for items matching `targetItemID`. Equipped items are part of the +// player's total inventory in the modern API — `GetItemCount` of a +// currently-equipped trinket returns 1, not 0. +int CountEquipped(int targetItemID) { + int total = 0; + for (int slot = 1; slot <= 19; slot++) { + const uint8_t *item = Item::Location::ResolveEquipmentSlot(slot); + if (item == nullptr) + continue; + if (Item::ID::FromCGItem(item) != targetItemID) + continue; + total += GetStackCount(item); + } + return total; +} + // `C_Item.GetItemCount(itemInfo [, includeBank [, includeUses]])` — -// returns the player's total count of `itemInfo` across bags -// (and optionally bank). `itemInfo` is a number or `"item:NNN"` -// string. Item names are NOT accepted (vanilla itself has no -// name → ID resolver). +// returns the player's total count of `itemInfo` across equipped +// items + bags (and optionally bank). `itemInfo` is a number or +// `"item:NNN"` string. Item names are NOT accepted (vanilla itself +// has no name → ID resolver). // -// Bag walks: -// - bags 0..4 always (backpack + 4 equipped bag slots) — live walk -// via `Item::Location::ResolveBag`. -// - if `includeBank=true`: main bank (linear slots 39..62 in player -// invMgr) + each equipped bank bag's contents (slots 63..68 in -// player invMgr). Bank walks **bypass `GetItemBySlot`** by reading -// the GUID array at `invMgr+0x04` directly and resolving via -// `FUN_OBJECT_RESOLVE_BY_GUID`. This works without the bank window -// ever being opened — the GUIDs are populated from server data at -// login. The engine gates `GetItemBySlot` for bank slots via a -// banker-GUID check at `VAR_BANK_GATE_GUID`, but the underlying -// data is always present. +// What's counted: +// - **Equipped items** (slots 1..19) — always. Matches modern +// behavior: a currently-equipped trinket returns 1. +// - **Bags 0..4** (backpack + 4 equipped bag slots) — always. +// Live walk via `Item::Location::ResolveBag`. +// - **Bank** (if `includeBank=true`): main bank (linear slots +// 39..62 in player invMgr) + each equipped bank bag's contents +// (slots 63..68 in player invMgr). Bank walks **bypass +// `GetItemBySlot`** by reading the GUID array at `invMgr+0x04` +// directly and resolving via `FUN_OBJECT_RESOLVE_BY_GUID`. This +// works without the bank window ever being opened — the GUIDs +// are populated from server data at login. The engine gates +// `GetItemBySlot` for bank slots via a banker-GUID check at +// `VAR_BANK_GATE_GUID`, but the underlying data is always +// present. // // `includeUses` (charges-multiplier mode) is **accepted but currently // ignored**. Modern semantics: when true, multiplies each match by @@ -199,10 +219,6 @@ int CountInBankBags(int targetItemID) { // descriptor offset, which hasn't been verified empirically yet. // Most callers don't use this flag; for now, both `true` and `false` // produce identical results (stack counts only). -// -// Equipped items are NOT counted, matching modern API behavior. -// Anyone equipping a wand to count its charges should use -// `GetInventoryItemID` + a separate charges read. int __fastcall Script_C_Item_GetItemCount(void *L) { const int itemID = Item::Arg::ResolveItemID(L, 1); if (itemID <= 0) { @@ -214,7 +230,7 @@ int __fastcall Script_C_Item_GetItemCount(void *L) { // clobbers the Lua stack via SetTop(0) inside ResolveBag. const bool includeBank = Game::Lua::ToBoolean(L, 2) != 0; - int total = 0; + int total = CountEquipped(itemID); for (int bag = 0; bag <= 4; bag++) total += CountInBag(L, bag, itemID); if (includeBank) {