mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 16:10:36 +00:00
fix(item): port GetItemCount to the engine's inventory search
C_Item.GetItemCount now follows 3.3.5's Script_GetItemCount on 1.12's own CGBag_C::FindItem ancestors (0x622130 stack count, 0x622180 charge count, 0x6223F0 walk), which share 3.3.5's search-flag bits. - Counts equipped bags and the keyring, skips broken items, and reads the bank through flag 0x08 without an open bank window. - includeUses uses 3.3.5's gate (non-stackable, negative base charges) and its reagent-uses override. - A name resolves to the ID of the first carried match, as 3.3.5's s_itemByName does. - Flags are read with the engine's StringToBOOL, so "0" is false. Item::Count::InInventory uses the same engine count, so IsUsableItem, the totem tool check and GetSpellCastCount now count what the engine's reagent and totem gate (0x6E4000) counts. GetSpellCastCount no longer stops at the first empty reagent slot.
This commit is contained in:
+31
-41
@@ -2743,7 +2743,7 @@ without scanning tooltips. A ClassicAPI extension.
|
||||
|
||||
### `C_Container.GetContainerItemCharges(containerIndex, slotIndex)`
|
||||
|
||||
Per-slot equivalent of
|
||||
Per-slot counterpart of
|
||||
[`C_Item.GetItemCount`](#c_itemgetitemcountiteminfo-includebank-includeuses)'s
|
||||
`includeCharges=true` mode — returns the total uses available in
|
||||
*this single slot*, where `GetItemCount` totals the same value
|
||||
@@ -2780,7 +2780,7 @@ Returns `nil` only when the slot is empty.
|
||||
local charges = C_Container.GetContainerItemCharges(0, 1)
|
||||
print("Wand charges remaining: " .. charges)
|
||||
|
||||
-- "how many drinks across the bags?" — same as GetItemCount(water, false, true)
|
||||
-- "how many drinks across the bags?"
|
||||
local total = 0
|
||||
for bag = 0, 4 do
|
||||
for slot = 1, GetContainerNumSlots(bag) do
|
||||
@@ -10038,60 +10038,50 @@ local ilvl = C_Item.GetCurrentItemLevel({equipmentSlotIndex = INVSLOT_HEAD})
|
||||
|
||||
### `C_Item.GetItemCount(itemInfo, [includeBank], [includeUses])`
|
||||
|
||||
Returns the player's total count of `itemInfo` — equipped items +
|
||||
bags, and optionally bank.
|
||||
Returns the number of `itemInfo` that the player carries.
|
||||
|
||||
```
|
||||
count = C_Item.GetItemCount(itemInfo [, includeBank [, includeUses]])
|
||||
```
|
||||
|
||||
- `itemInfo` — a numeric `itemID`, a string that contains `"item:NNN"`
|
||||
(full chat links work), or an item name. A name is compared, without
|
||||
regard to case, with the full name of each item that you carry (the
|
||||
suffix included, so `"Foo of the Owl"` matches and `"Foo"` does not).
|
||||
- `includeBank` *(optional, default false)* — also walk bank slots
|
||||
(bag `-1` for the main bank, bags `5..10` for bank-bag slots).
|
||||
- `includeUses` *(optional, default false)* — when `true`, multiplies
|
||||
each match by the item's spell-charges count. A wand with 50 charges
|
||||
contributes 50 instead of 1. Items without charges pass their plain
|
||||
stack count through unchanged, so the flag is a no-op for them.
|
||||
(full chat links work), or an item name. The name must match the
|
||||
full name of an item that you carry, with the suffix (`"Foo of the
|
||||
Owl"`). Case is not important. The count is for the item ID of the
|
||||
first match.
|
||||
- `includeBank` *(optional, default false)* — also count the bank and
|
||||
the bank bags. The bank does not have to be open.
|
||||
- `includeUses` *(optional, default false)* — for an item that does
|
||||
not stack and that has charges, return the total charges instead of
|
||||
the item count. If the item's use spell needs reagents, return the
|
||||
number of casts that the carried reagents allow. For all other
|
||||
items, this flag has no effect.
|
||||
|
||||
The count includes:
|
||||
|
||||
- equipped items
|
||||
- equipped bags (the bag items themselves)
|
||||
- the backpack, the contents of the equipped bags, and the keyring
|
||||
- the bank and the bank bags, if `includeBank` is true
|
||||
|
||||
Broken items (durability 0) are not counted. `includeUses` does not
|
||||
count bank items.
|
||||
|
||||
```lua
|
||||
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
|
||||
local n = C_Item.GetItemCount(2589) -- Linen Cloth that you carry
|
||||
local n = C_Item.GetItemCount(2589, true) -- also the bank
|
||||
local n = C_Item.GetItemCount("item:2589") -- string form
|
||||
local n = C_Item.GetItemCount("Linen Cloth") -- by name
|
||||
|
||||
-- Equipped items count toward the total:
|
||||
-- Equipped items count:
|
||||
local trinketID = GetInventoryItemID("player", INVSLOT_TRINKET1)
|
||||
C_Item.GetItemCount(trinketID) -- 1
|
||||
|
||||
-- includeUses multiplies by charges for charged items:
|
||||
C_Item.GetItemCount(wandID, false, true) -- 50 for one 50-charge wand
|
||||
C_Item.GetItemCount(linenID, false, true) -- same as stack count (no charges)
|
||||
-- includeUses returns the total charges of an item with charges:
|
||||
C_Item.GetItemCount(chargedItemID, false, true) -- the sum of the charges
|
||||
C_Item.GetItemCount(linenID, false, true) -- the stack count (no charges)
|
||||
```
|
||||
|
||||
> **Bank works cold — no banker visit required.** The server
|
||||
> sends bank inventory at login alongside the rest of the player's
|
||||
> data; only the engine's own `GetItemBySlot` gates bank slots until
|
||||
> the window opens. We bypass that gate by reading the GUID array
|
||||
> directly out of the player invMgr and resolving each entry via the
|
||||
> engine's object resolver — same path `GetItemBySlot` would take
|
||||
> internally if the gate let us through. Counts are correct from
|
||||
> session start.
|
||||
|
||||
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
|
||||
directly off the CGItem's m_objectFields descriptor at +0x20
|
||||
(`ITEM_FIELD_STACK_COUNT`, verified by decoding
|
||||
`Script_GetContainerItemInfo` at `0x004F9670`).
|
||||
|
||||
Equivalent to the global `GetItemCount` and `C_Item.GetItemCount`.
|
||||
|
||||
### `C_Item.GetItemData(itemLocation)` / `C_Item.GetItemDataByID(item)`
|
||||
|
||||
ClassicAPI-only kitchen-sink reader: returns a single table with every
|
||||
|
||||
+27
-14
@@ -4814,8 +4814,7 @@ enum Offsets {
|
||||
// `m_stackable` — max stack size for this item type (1 for non-
|
||||
// stackable; 5/20/200/etc. for stackable). The instance-level
|
||||
// current stack count is a different field (`OFF_DESCRIPTOR_STACK_COUNT`
|
||||
// at +0x20 on the CGItem's m_objectFields descriptor — see
|
||||
// `Item::Count.cpp`).
|
||||
// at +0x20 on the CGItem's m_objectFields descriptor).
|
||||
OFF_ITEMSTATS_STACKABLE = 0x60,
|
||||
// Bag-only fields. `m_containerSlots` (slot count) and `m_bagFamily`
|
||||
// (the BagFamily bitfield — quiver=1, ammo pouch=2, soul bag=4 in
|
||||
@@ -6224,20 +6223,34 @@ enum Offsets {
|
||||
FUN_ACTION_BAR_PRUNE_SPELL = 0x004E5E20,
|
||||
// Count of `itemID` the player carries, as `UseAction` caches per
|
||||
// item-by-ID slot into VAR_ACTION_ITEM_COUNTS — `uint __fastcall(uint
|
||||
// itemID)`. Charged items (ItemStats `SPELL_CHARGES[0]` set) sum charges
|
||||
// via FUN_INVMGR_COUNT_ITEM_BY_ID, everything else sums stacks
|
||||
// (`FUN_00622130`). The inner GetActionCount (`0x004E6C70`) only reads
|
||||
// the cached array, item-by-ID slots only.
|
||||
// itemID)`. Charged items (ItemStats `SPELL_CHARGES[0]` not 0 / -1) sum
|
||||
// charges via FUN_INVMGR_COUNT_CHARGES_BY_ID, everything else sums stacks
|
||||
// via FUN_INVMGR_COUNT_STACKS_BY_ID. The inner GetActionCount
|
||||
// (`0x004E6C70`) only reads the cached array, item-by-ID slots only.
|
||||
FUN_ACTION_ITEM_COUNT = 0x004E6D20,
|
||||
VAR_ACTION_ITEM_COUNTS = 0x00BC6390, // uint[120]
|
||||
// Inventory-manager searches behind the action-bar item branches —
|
||||
// `__thiscall(invMgr, uint itemID, uint flags)`. Both drive the slot
|
||||
// visitor `FUN_00622420`, which with `flags == 0` on the player
|
||||
// inventory manager walks equipment (linear 0..18), bag slots (19..22),
|
||||
// backpack (23..38), keyring (81..112) and recurses into each equipped
|
||||
// bag — never the bank.
|
||||
FUN_INVMGR_FIND_ITEM_BY_ID = 0x00622270, // → CGItem *, first match
|
||||
FUN_INVMGR_COUNT_ITEM_BY_ID = 0x00622180, // → summed charges of the matches
|
||||
// Inventory-manager searches (3.3.5/4.3.4 `CGBag_C::FindItem` family) —
|
||||
// `__thiscall(invMgr, uint itemID, uint flags)`. All drive the slot
|
||||
// visitor `FUN_00622420`, which skips broken items (max durability set,
|
||||
// durability 0, ITEM_FIELD_FLAGS 0x8 clear) and items with field flag
|
||||
// 0x10 — there is no 3.3.5 `BAG_SEARCH_IGNORE_ITEM_STATE` (0x80) bypass.
|
||||
// On the player inventory manager a `flags` with none of 0x47 set gets
|
||||
// 0x47 ORed in: equipment (linear 0..18), bag slots (19..22), backpack
|
||||
// (23..38), keyring (81..112), recursing into each bag — 3.3.5's
|
||||
// `BAG_SEARCH_DEFAULT_LOCATIONS` minus currency. The bag items in 19..22
|
||||
// reach the callback themselves before their contents.
|
||||
FUN_INVMGR_FIND_ITEM_BY_ID = 0x00622270, // → CGItem *, first match
|
||||
FUN_INVMGR_COUNT_STACKS_BY_ID = 0x00622130, // → summed ITEM_FIELD_STACK_COUNT (3.3.5 GetItemTypeCount)
|
||||
FUN_INVMGR_COUNT_CHARGES_BY_ID = 0x00622180, // → summed abs(SPELL_CHARGES[0]) (3.3.5 GetItemTypeCharges)
|
||||
// The walk with a caller callback — `CGItem *__thiscall(invMgr,
|
||||
// int (__fastcall *cb)(CGItem *item, void *ctx), void *ctx, uint flags)`,
|
||||
// returning the first item `cb` answered nonzero for (null = none).
|
||||
// `cb` gets the item in ECX and `ctx` in EDX (`0x0062253C`).
|
||||
FUN_INVMGR_WALK = 0x006223F0,
|
||||
// `flags` bit adding the bank: main bank (39..62) and the bank-bag slots
|
||||
// (63..68), recursing into each bag. Reads the GUID array directly, so
|
||||
// unlike FUN_ITEMMGR_GET_ITEM_BY_SLOT it needs no open bank window.
|
||||
INVMGR_SEARCH_BANK = 0x08,
|
||||
// The registered action-bar Lua C functions that `Action::ItemState`
|
||||
// re-registers in front of. Tail-call these with the untouched stack for
|
||||
// every slot we don't own (they raise their own usage errors).
|
||||
|
||||
+2
-3
@@ -18,13 +18,12 @@
|
||||
namespace Item::Charges {
|
||||
|
||||
// Pushes the total uses available in this single slot — the
|
||||
// per-slot equivalent of `GetItemCount(_, _, includeCharges=true)`.
|
||||
// per-slot counterpart of `GetItemCount(_, _, includeCharges=true)`.
|
||||
// Returns `1` (pushed one value) on success, `0` (pushes nothing,
|
||||
// caller surfaces as `nil`) when the item is missing or the
|
||||
// descriptor is null.
|
||||
//
|
||||
// Math is `stack * usesPerItem` where `usesPerItem` follows
|
||||
// `Item::Count::GetUsesPerItem`:
|
||||
// Math is `stack * usesPerItem` where `usesPerItem` is:
|
||||
// - `SPELL_CHARGES[0] >= 0` → `1` (no destroy-on-zero behavior;
|
||||
// stack count alone reflects "how many uses").
|
||||
// - `SPELL_CHARGES[0] < 0` → `abs(value)` (consume-on-use items;
|
||||
|
||||
+87
-215
@@ -11,240 +11,116 @@
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
// `C_Item.GetItemCount(itemInfo [, includeBank [, includeUses]])` — a
|
||||
// straight port of 3.3.5's `Script_GetItemCount` (0x0051C2E0; 4.3.4's is
|
||||
// identical) onto 1.12's own inventory searches, which are the ancestors of
|
||||
// 3.3.5's `CGBag_C::FindItem` family with the same flag bits:
|
||||
//
|
||||
// total = GetItemTypeCount(itemID, includeBank ? BANK : 0)
|
||||
// → FUN_INVMGR_COUNT_STACKS_BY_ID
|
||||
// includeUses, for a non-stackable item whose base charges are negative:
|
||||
// total = GetItemTypeCharges(itemID, 0) → FUN_INVMGR_COUNT_CHARGES_BY_ID
|
||||
// and if the use spell's reagents allow N casts, total = N.
|
||||
//
|
||||
// So the count is the engine's: equipment, the equipped bags themselves,
|
||||
// backpack, keyring and bag contents (plus the bank, read without a banker),
|
||||
// broken items skipped. 1.12's walk has no 3.3.5 IGNORE_ITEM_STATE (0x80)
|
||||
// bit, so includeUses can't count broken items the way 3.3.5 does. 3.3.5's
|
||||
// use-enchant branch (`FindUseEnchantItemPtr`) is not ported; base charges
|
||||
// and the base use spell stand for it.
|
||||
|
||||
#include "item/Count.h"
|
||||
|
||||
#include "Game.h"
|
||||
#include "Offsets.h"
|
||||
#include "item/Arg.h"
|
||||
#include "item/CGItem.h"
|
||||
#include "item/ID.h"
|
||||
#include "item/Location.h"
|
||||
#include "object/Resolve.h"
|
||||
#include "item/Record.h"
|
||||
#include "spell/CastCount.h"
|
||||
#include "unit/Identity.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
namespace Item::Count {
|
||||
|
||||
namespace {
|
||||
|
||||
// Reads ITEM_FIELD_STACK_COUNT off a CGItem's m_objectFields
|
||||
// descriptor at +0x20. Returns 0 if the descriptor is null. Stack
|
||||
// counts are always positive — non-stackable items have count = 1.
|
||||
int GetStackCount(const uint8_t *cgItem) {
|
||||
if (cgItem == nullptr)
|
||||
return 0;
|
||||
auto *descriptor = Item::ObjectFields(cgItem);
|
||||
if (descriptor == nullptr)
|
||||
return 0;
|
||||
return static_cast<int>(*reinterpret_cast<const uint32_t *>(
|
||||
descriptor + Offsets::OFF_DESCRIPTOR_STACK_COUNT));
|
||||
}
|
||||
using CountByID_t = int(__thiscall *)(const void *invMgr, int itemID, uint32_t flags);
|
||||
using WalkCallback_t = int(__fastcall *)(const uint8_t *item, void *ctx);
|
||||
using Walk_t = const uint8_t *(__thiscall *)(const void *invMgr, WalkCallback_t cb, void *ctx,
|
||||
uint32_t flags);
|
||||
using LooseBool_t = int(__fastcall *)(void *L, int idx, int defaultValue);
|
||||
|
||||
// Per-item uses count for `includeUses` mode. Reads
|
||||
// ITEM_FIELD_SPELL_CHARGES[0] (signed dword). Only negative values
|
||||
// count — they mark "consume-on-use, destroyed at 0" items
|
||||
// (Healthstone, Mana Gem, etc.). Positive or zero returns 1, so the
|
||||
// multiplier is neutral for stacked consumables and ordinary items.
|
||||
// Mirrors 3.3.5's `Script_GetItemCount` charge gate at
|
||||
// `FUN_0051c2e0` — it only swaps to the charge-summing walker when
|
||||
// `SPELL_CHARGES[0] < 0`.
|
||||
int GetUsesPerItem(const uint8_t *cgItem) {
|
||||
if (cgItem == nullptr)
|
||||
return 1;
|
||||
auto *descriptor = Item::ObjectFields(cgItem);
|
||||
if (descriptor == nullptr)
|
||||
return 1;
|
||||
const int32_t raw = *reinterpret_cast<const int32_t *>(
|
||||
descriptor + Offsets::OFF_DESCRIPTOR_SPELL_CHARGES_0);
|
||||
if (raw >= 0)
|
||||
return 1;
|
||||
return -raw;
|
||||
}
|
||||
|
||||
// Combined per-slot contribution to GetItemCount. `includeUses=false`
|
||||
// gives plain stack count (matches old behavior); `true` multiplies by
|
||||
// abs(SPELL_CHARGES[0]) for charged items.
|
||||
int GetItemContribution(const uint8_t *cgItem, bool includeUses) {
|
||||
const int stack = GetStackCount(cgItem);
|
||||
if (!includeUses)
|
||||
return stack;
|
||||
return stack * GetUsesPerItem(cgItem);
|
||||
}
|
||||
|
||||
// Walks `bagID` and accumulates per-slot contributions for items
|
||||
// matching `arg` (itemID, or name via `MatchesArg`). Skips empty slots.
|
||||
// Each `ResolveBag` call clobbers Lua stack[1]/[2]; we own the stack
|
||||
// inside the callback.
|
||||
int CountInBag(void *L, int bagID, const Item::Arg::Resolved &arg, bool includeUses) {
|
||||
int total = 0;
|
||||
const int slotCount = Item::Location::GetBagSlotCount(bagID);
|
||||
for (int slot = 1; slot <= slotCount; slot++) {
|
||||
const uint8_t *item = Item::Location::ResolveBag(L, bagID, slot);
|
||||
if (item == nullptr)
|
||||
continue;
|
||||
if (!Item::Location::MatchesArg(item, arg))
|
||||
continue;
|
||||
total += GetItemContribution(item, includeUses);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// Direct GUID-array bypass for bank slots — sidesteps the gate at
|
||||
// `0x006228C1` that `GetItemBySlot` applies to slots 39..68 when no
|
||||
// bank window is open. The slot data in `invMgr+0x04` is populated at
|
||||
// login (verified empirically — see `Offsets::OFF_INVMGR_GUID_ARRAY`),
|
||||
// so we can read GUIDs directly and resolve them via the engine's own
|
||||
// object resolver `FUN_OBJECT_RESOLVE_BY_GUID` (the same function
|
||||
// `GetItemBySlot` would call internally if the gate let us through).
|
||||
|
||||
const uint8_t *ResolveByGuid(int type, uint64_t guid) {
|
||||
if (guid == 0)
|
||||
return nullptr;
|
||||
return static_cast<const uint8_t *>(
|
||||
Object::ByGuid(type, guid, "ItemMgr", 0x172));
|
||||
}
|
||||
|
||||
// Walks linear slots `[firstSlot, lastSlot]` of `invMgr`'s GUID array
|
||||
// at `+0x04`, resolves each non-zero GUID as a TYPE_ITEM, sums
|
||||
// contributions of items matching `targetItemID`. Used for both the
|
||||
// main bank (slots 39..62 in player invMgr) and individual bank-bag
|
||||
// invMgrs (slots 0..N-1).
|
||||
int CountInGuidArray(const uint8_t *invMgr, int firstSlot, int lastSlot,
|
||||
const Item::Arg::Resolved &arg, bool includeUses) {
|
||||
int CountByID(uintptr_t fn, int itemID, uint32_t flags) {
|
||||
const uint8_t *invMgr = Unit::Identity::PlayerInventoryManager();
|
||||
if (invMgr == nullptr)
|
||||
return 0;
|
||||
auto *guidArray = *reinterpret_cast<const uint64_t *const *>(
|
||||
invMgr + Offsets::OFF_INVMGR_GUID_ARRAY);
|
||||
if (guidArray == nullptr)
|
||||
return reinterpret_cast<CountByID_t>(fn)(invMgr, itemID, flags);
|
||||
}
|
||||
|
||||
int __fastcall MatchesName(const uint8_t *item, void *ctx) {
|
||||
return Item::Location::MatchesArg(item, *static_cast<const Item::Arg::Resolved *>(ctx)) ? 1
|
||||
: 0;
|
||||
}
|
||||
|
||||
// 3.3.5 resolves a name through `s_itemByName`, filled with each item
|
||||
// object's decorated name as it is created; 1.12 has no such map. The first
|
||||
// item carried under that name (same walk, same flags) gives the same ID.
|
||||
int IDForName(const char *name, uint32_t flags) {
|
||||
const uint8_t *invMgr = Unit::Identity::PlayerInventoryManager();
|
||||
if (invMgr == nullptr)
|
||||
return 0;
|
||||
int total = 0;
|
||||
for (int slot = firstSlot; slot <= lastSlot; slot++) {
|
||||
const uint8_t *item = ResolveByGuid(Offsets::TYPEMASK_ITEM, guidArray[slot]);
|
||||
if (item == nullptr)
|
||||
continue;
|
||||
if (!Item::Location::MatchesArg(item, arg))
|
||||
continue;
|
||||
total += GetItemContribution(item, includeUses);
|
||||
Item::Arg::Resolved arg{0, 0, name};
|
||||
const uint8_t *item =
|
||||
reinterpret_cast<Walk_t>(Offsets::FUN_INVMGR_WALK)(invMgr, &MatchesName, &arg, flags);
|
||||
return item != nullptr ? Item::ID::FromCGItem(item) : 0;
|
||||
}
|
||||
|
||||
// 3.3.5's `CGItem_C::NumBaseCharges` / `GetUseSpell`: the first on-use
|
||||
// spell slot's charges and spell.
|
||||
struct UseSpell {
|
||||
int spellID;
|
||||
int charges;
|
||||
};
|
||||
|
||||
UseSpell FirstUseSpell(const uint8_t *record) {
|
||||
for (int i = 0; i < Offsets::ITEMSTATS_SPELL_SLOT_COUNT; ++i) {
|
||||
const int spellID = Game::Read<int>(record, Offsets::OFF_ITEMSTATS_SPELL_ID + i * 4);
|
||||
const uint32_t trigger =
|
||||
Game::Read<uint32_t>(record, Offsets::OFF_ITEMSTATS_SPELL_TRIGGER + i * 4);
|
||||
if (spellID != 0 && trigger == Offsets::ITEM_SPELLTRIGGER_ON_USE)
|
||||
return {spellID, Game::Read<int>(record, Offsets::OFF_ITEMSTATS_SPELL_CHARGES + i * 4)};
|
||||
}
|
||||
return {0, 0};
|
||||
}
|
||||
|
||||
int CountUses(int itemID, int total) {
|
||||
const uint8_t *record = Item::PeekRecord(static_cast<uint32_t>(itemID));
|
||||
if (record == nullptr || Game::Read<int>(record, Offsets::OFF_ITEMSTATS_STACKABLE) > 1)
|
||||
return total;
|
||||
const UseSpell use = FirstUseSpell(record);
|
||||
if (use.charges < 0)
|
||||
total = CountByID(Offsets::FUN_INVMGR_COUNT_CHARGES_BY_ID, itemID, 0);
|
||||
if (const int casts = ::Spell::CastCount::ForSpell(use.spellID))
|
||||
total = casts;
|
||||
return total;
|
||||
}
|
||||
|
||||
// For bank bag CONTENTS: the equipped bank bags themselves live at
|
||||
// linear slots 63..68 in the player invMgr. Each is a CGContainer with
|
||||
// its own invMgr accessed via `vtable[+0x10]` (`CGContainer::GetInvMgr`
|
||||
// or similar). We resolve the bag, get its invMgr, and walk that.
|
||||
//
|
||||
// Layout: bag invMgr+0x00 = max slot count (the bag's size); bag
|
||||
// invMgr+0x04 = its GUID array. Same shape as the player invMgr.
|
||||
int CountInBankBags(const Item::Arg::Resolved &arg, bool includeUses) {
|
||||
auto *playerInvMgr = Unit::Identity::PlayerInventoryManager();
|
||||
if (playerInvMgr == nullptr)
|
||||
return 0;
|
||||
auto *playerGuidArray = *reinterpret_cast<const uint64_t *const *>(
|
||||
playerInvMgr + Offsets::OFF_INVMGR_GUID_ARRAY);
|
||||
if (playerGuidArray == nullptr)
|
||||
return 0;
|
||||
|
||||
int total = 0;
|
||||
for (int slot = Offsets::INVMGR_BANK_BAG_FIRST_SLOT;
|
||||
slot <= Offsets::INVMGR_BANK_BAG_LAST_SLOT; slot++) {
|
||||
const uint8_t *bag = ResolveByGuid(Offsets::TYPEMASK_CONTAINER,
|
||||
playerGuidArray[slot]);
|
||||
if (bag == nullptr)
|
||||
continue;
|
||||
auto *bagInvMgr =
|
||||
static_cast<const uint8_t *>(Item::Location::ContainerInventory(bag));
|
||||
if (bagInvMgr == nullptr)
|
||||
continue;
|
||||
const int bagSlots = static_cast<int>(*reinterpret_cast<const uint32_t *>(bagInvMgr));
|
||||
if (bagSlots <= 0)
|
||||
continue;
|
||||
total += CountInGuidArray(bagInvMgr, 0, bagSlots - 1, arg, includeUses);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// Walks the player's 19 equipment slots and accumulates contributions
|
||||
// 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(const Item::Arg::Resolved &arg, bool includeUses) {
|
||||
int total = 0;
|
||||
for (int slot = 1; slot <= 19; slot++) {
|
||||
const uint8_t *item = Item::Location::ResolveEquipmentSlot(slot);
|
||||
if (item == nullptr)
|
||||
continue;
|
||||
if (!Item::Location::MatchesArg(item, arg))
|
||||
continue;
|
||||
total += GetItemContribution(item, includeUses);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// `C_Item.GetItemCount(itemInfo [, includeBank [, includeUses]])` —
|
||||
// returns the player's total count of `itemInfo` across equipped
|
||||
// items + bags (and optionally bank). `itemInfo` is a number, an
|
||||
// `"item:NNN"` string / link, or an item NAME — a plain string is matched
|
||||
// case-insensitively against each carried item's decorated display name
|
||||
// (`Item::Location::MatchesArg`, the same rule `C_Item.UseItemByName`
|
||||
// uses), which is what lets the `/cast` / `/use` handler decide
|
||||
// "is this an item I carry" before falling back to a spell cast.
|
||||
//
|
||||
// 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` (arg 3, boolean) multiplies each match by the item's
|
||||
// `abs(SPELL_CHARGES[0])` — so a wand with 50 charges contributes 50
|
||||
// instead of 1. Items without charges contribute their plain stack
|
||||
// count (via the `or 1` neutralizer in `GetUsesPerItem`), so the flag
|
||||
// is a no-op for them.
|
||||
int __fastcall Script_C_Item_GetItemCount(void *L) {
|
||||
Item::Arg::Resolved arg = Item::Arg::Resolve(L, 1);
|
||||
// A plain string is a name. Copy it off the Lua string heap before the
|
||||
// bag walk — `ResolveBag` clears the stack.
|
||||
char nameBuf[128];
|
||||
if (arg.itemID <= 0) {
|
||||
if (arg.name == nullptr || arg.name[0] == '\0') {
|
||||
Game::Lua::PushNumber(L, 0);
|
||||
return 1;
|
||||
}
|
||||
std::snprintf(nameBuf, sizeof(nameBuf), "%s", arg.name);
|
||||
arg.name = nameBuf;
|
||||
}
|
||||
// Read both flags before the bag walk — `ResolveBag` clobbers
|
||||
// the Lua stack via SetTop(0) and would invalidate any later
|
||||
// `ToBoolean` reads.
|
||||
const bool includeBank = Game::Lua::ToBoolean(L, 2) != 0;
|
||||
const bool includeUses = Game::Lua::ToBoolean(L, 3) != 0;
|
||||
const auto looseBool = reinterpret_cast<LooseBool_t>(Offsets::FUN_LUA_TO_BOOLEAN_LOOSE);
|
||||
const bool includeBank = looseBool(L, 2, 0) != 0;
|
||||
const bool includeUses = looseBool(L, 3, 0) != 0;
|
||||
const uint32_t flags = includeBank ? Offsets::INVMGR_SEARCH_BANK : 0;
|
||||
|
||||
int total = CountEquipped(arg, includeUses);
|
||||
for (int bag = 0; bag <= 4; bag++)
|
||||
total += CountInBag(L, bag, arg, includeUses);
|
||||
if (includeBank) {
|
||||
// Direct GUID-array reads — bypass the bank gate so this works
|
||||
// without the bank window having ever been opened in the
|
||||
// session.
|
||||
total += CountInGuidArray(Unit::Identity::PlayerInventoryManager(),
|
||||
Offsets::INVMGR_BANK_MAIN_FIRST_SLOT,
|
||||
Offsets::INVMGR_BANK_MAIN_LAST_SLOT,
|
||||
arg, includeUses);
|
||||
total += CountInBankBags(arg, includeUses);
|
||||
}
|
||||
const Item::Arg::Resolved arg = Item::Arg::Resolve(L, 1);
|
||||
int itemID = arg.itemID;
|
||||
if (itemID <= 0 && arg.name != nullptr && arg.name[0] != '\0')
|
||||
itemID = IDForName(arg.name, flags);
|
||||
|
||||
int total = itemID > 0 ? CountByID(Offsets::FUN_INVMGR_COUNT_STACKS_BY_ID, itemID, flags) : 0;
|
||||
if (total != 0 && includeUses)
|
||||
total = CountUses(itemID, total);
|
||||
|
||||
Game::Lua::SetTop(L, 0);
|
||||
Game::Lua::PushNumber(L, static_cast<double>(total));
|
||||
@@ -253,14 +129,10 @@ int __fastcall Script_C_Item_GetItemCount(void *L) {
|
||||
|
||||
} // namespace
|
||||
|
||||
int InInventory(void *L, int itemID) {
|
||||
int InInventory(int itemID) {
|
||||
if (itemID <= 0)
|
||||
return 0;
|
||||
const Item::Arg::Resolved arg{itemID, 0, nullptr};
|
||||
int total = CountEquipped(arg, /*includeUses*/ false);
|
||||
for (int bag = 0; bag <= 4; bag++)
|
||||
total += CountInBag(L, bag, arg, /*includeUses*/ false);
|
||||
return total;
|
||||
return CountByID(Offsets::FUN_INVMGR_COUNT_STACKS_BY_ID, itemID, 0);
|
||||
}
|
||||
|
||||
static void RegisterLuaFunctions() {
|
||||
|
||||
+5
-6
@@ -15,11 +15,10 @@
|
||||
|
||||
namespace Item::Count {
|
||||
|
||||
// Total plain stack count of `itemID` across the player's equipped slots
|
||||
// and bags 0..4 (no bank, no charge multiplier) — the "carried on me"
|
||||
// count. Needs the Lua state `L`: the bag walk uses the engine's slot
|
||||
// packer, which reads and clears the Lua stack, so the caller must have
|
||||
// already consumed any stack arguments it still needs before calling.
|
||||
int InInventory(void *L, int itemID);
|
||||
// Total stack count of `itemID` the player carries, through the engine's
|
||||
// own search (FUN_INVMGR_COUNT_STACKS_BY_ID, flags 0): equipment, equipped
|
||||
// bags, backpack, keyring and bag contents, no bank, broken items skipped —
|
||||
// the count the engine's reagent and totem gate uses.
|
||||
int InInventory(int itemID);
|
||||
|
||||
} // namespace Item::Count
|
||||
|
||||
+4
-6
@@ -60,9 +60,7 @@ struct Usability {
|
||||
bool noMana;
|
||||
};
|
||||
|
||||
// `L` is needed for the carried count (see Item::Count::InInventory); the
|
||||
// caller has already read its arguments.
|
||||
Usability ComputeUsability(void *L, int itemID) {
|
||||
Usability ComputeUsability(int itemID) {
|
||||
Usability r{false, false};
|
||||
if (itemID <= 0 || Unit::Identity::PlayerObject() == nullptr)
|
||||
return r;
|
||||
@@ -71,7 +69,7 @@ Usability ComputeUsability(void *L, int itemID) {
|
||||
if (record == nullptr)
|
||||
return r; // not cached → sync "false", no load fired
|
||||
|
||||
if (Item::Count::InInventory(L, itemID) <= 0)
|
||||
if (Item::Count::InInventory(itemID) <= 0)
|
||||
return r;
|
||||
if (reinterpret_cast<ItemSpellHeld_t>(Offsets::FUN_ITEM_SPELL_HELD)(itemID) != 0)
|
||||
return r;
|
||||
@@ -101,7 +99,7 @@ Usability ComputeUsability(void *L, int itemID) {
|
||||
// `IsUsableSpell` convention.
|
||||
int __fastcall Script_IsUsableItem(void *L) {
|
||||
const int itemID = Item::Arg::ResolveItemID(L, 1);
|
||||
const Usability r = ComputeUsability(L, itemID);
|
||||
const Usability r = ComputeUsability(itemID);
|
||||
Game::Lua::SetTop(L, 0);
|
||||
if (r.usable)
|
||||
Game::Lua::PushNumber(L, 1.0);
|
||||
@@ -118,7 +116,7 @@ int __fastcall Script_IsUsableItem(void *L) {
|
||||
// proper booleans (`usable`, `noMana`) per the `C_Item.*` convention.
|
||||
int __fastcall Script_C_Item_IsUsableItem(void *L) {
|
||||
const int itemID = Item::Arg::ResolveItemID(L, 1);
|
||||
const Usability r = ComputeUsability(L, itemID);
|
||||
const Usability r = ComputeUsability(itemID);
|
||||
Game::Lua::SetTop(L, 0);
|
||||
Game::Lua::PushBool(L, r.usable);
|
||||
Game::Lua::PushBool(L, r.noMana);
|
||||
|
||||
+18
-22
@@ -15,16 +15,17 @@
|
||||
// `C_SpellBook.GetSpellBookItemCastCount(slotIndex, spellBank)` — how many
|
||||
// times a spell can be cast with the reagents the player carries.
|
||||
//
|
||||
// There is no engine helper to mirror: 1.12's action-button count
|
||||
// (`Script_GetActionCount` → 0x004E6C70, `Script_IsConsumableAction` →
|
||||
// 0x004E5250) is ITEM-only — both gate on the action tag `0x80000000` and
|
||||
// read the item stack cache — so a reagent-based count was never computed
|
||||
// client-side. This is the server's own cast gate (`Spell::CheckItems` →
|
||||
// `Player::HasItemCount(reagent, count)`) restated as a count: the minimum
|
||||
// over the spell's reagent slots of floor(carried / required), where
|
||||
// "carried" is equipped + bags 0..4 — the inventory the server checks; bank
|
||||
// contents never satisfy a reagent. Spells with no reagents return 0,
|
||||
// matching retail's `GetSpellCastCount`.
|
||||
// 1.12 has no reagent-uses helper (3.3.5's `Spell_C_GetSpellReagentUses`),
|
||||
// and its action-button count (`Script_GetActionCount` → 0x004E6C70) is
|
||||
// ITEM-only. 3.3.5's helper is the engine's own cast gate restated as a
|
||||
// count, and 1.12 has that gate whole: `CheckSpellTotemsAndReagents`
|
||||
// (0x006E4000) walks the 8 reagent slots, skips empty ones, and compares
|
||||
// `FUN_INVMGR_COUNT_STACKS_BY_ID(invMgr, reagent, 0)` — no bank — against
|
||||
// the required count. The count is the minimum over those slots of
|
||||
// floor(carried / required). Spells with no reagents return 0, matching
|
||||
// retail's `GetSpellCastCount`.
|
||||
|
||||
#include "CastCount.h"
|
||||
|
||||
#include "Arg.h"
|
||||
#include "Lookup.h"
|
||||
@@ -37,12 +38,7 @@
|
||||
|
||||
namespace Spell::CastCount {
|
||||
|
||||
namespace {
|
||||
|
||||
// Reagent-limited casts of `spellID`; 0 for unknown spells and for spells
|
||||
// with no reagents. Clobbers the Lua stack (the bag walk goes through the
|
||||
// engine's slot packer), so callers read their arguments first.
|
||||
int ForSpell(void *L, int spellID) {
|
||||
int ForSpell(int spellID) {
|
||||
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
|
||||
if (record == nullptr)
|
||||
return 0;
|
||||
@@ -51,20 +47,20 @@ int ForSpell(void *L, int spellID) {
|
||||
Game::Ptr<const int32_t>(record, Offsets::OFF_SPELL_REAGENT_COUNT);
|
||||
int casts = -1;
|
||||
for (int i = 0; i < Offsets::SPELL_MAX_REAGENTS; ++i) {
|
||||
if (ids[i] == 0)
|
||||
break; // engine convention: the list ends at the first empty slot
|
||||
if (counts[i] <= 0)
|
||||
if (ids[i] == 0 || counts[i] <= 0)
|
||||
continue;
|
||||
const int possible = Item::Count::InInventory(L, ids[i]) / counts[i];
|
||||
const int possible = Item::Count::InInventory(ids[i]) / counts[i];
|
||||
if (casts < 0 || possible < casts)
|
||||
casts = possible;
|
||||
}
|
||||
return casts < 0 ? 0 : casts;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
int __fastcall Script_C_Spell_GetSpellCastCount(void *L) {
|
||||
const int spellID = Spell::Arg::ResolveSpellID(L, 1);
|
||||
const int casts = ForSpell(L, spellID);
|
||||
const int casts = ForSpell(spellID);
|
||||
Game::Lua::SetTop(L, 0);
|
||||
Game::Lua::PushNumber(L, static_cast<double>(casts));
|
||||
return 1;
|
||||
@@ -72,7 +68,7 @@ int __fastcall Script_C_Spell_GetSpellCastCount(void *L) {
|
||||
|
||||
int __fastcall Script_C_SpellBook_GetSpellBookItemCastCount(void *L) {
|
||||
const int spellID = Spell::Lookup::SpellbookItemArgsToID(L, 1, 2);
|
||||
const int casts = ForSpell(L, spellID);
|
||||
const int casts = ForSpell(spellID);
|
||||
Game::Lua::SetTop(L, 0);
|
||||
Game::Lua::PushNumber(L, static_cast<double>(casts));
|
||||
return 1;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// This file is part of ClassicAPI.
|
||||
//
|
||||
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
|
||||
// of the GNU General Public License as published by the Free Software Foundation, either
|
||||
// version 3 of the License, or (at your option) any later version.
|
||||
//
|
||||
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
// PURPOSE. See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Spell::CastCount {
|
||||
|
||||
// Reagent-limited casts of `spellID` — 3.3.5's `Spell_C_GetSpellReagentUses`.
|
||||
// 0 for an unknown spell and for a spell with no reagents.
|
||||
int ForSpell(int spellID);
|
||||
|
||||
} // namespace Spell::CastCount
|
||||
@@ -373,11 +373,10 @@ int __fastcall Script_GetTotemInfo(void *L) {
|
||||
|
||||
// Tool presence — independent of the summoned state. The tool item
|
||||
// (resolved from the totem spell's Totem[0] on SPELLS_CHANGED, not
|
||||
// hardcoded) is checked against the bag. The bag walk reads the Lua
|
||||
// stack, so it runs before we push any return values (`slot` captured).
|
||||
// hardcoded) is checked against the bag.
|
||||
const uint32_t tool = g_toolItem[slot - 1];
|
||||
const bool haveTotem =
|
||||
tool != 0 && Item::Count::InInventory(L, static_cast<int>(tool)) > 0;
|
||||
tool != 0 && Item::Count::InInventory(static_cast<int>(tool)) > 0;
|
||||
|
||||
const Slot &t = g_slots[slot - 1];
|
||||
if (!t.active) {
|
||||
|
||||
Reference in New Issue
Block a user