diff --git a/README.md b/README.md index 04a02441..36425bb1 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ reference in **[docs/API.md](docs/API.md)**. | [Console](docs/API.md#console) | `CalculateStringEditDistance`, `ConsoleEcho`, `ConsoleExec`, `ConsoleGetAllCommands`, `ConsoleGetColorFromType`, `ConsoleGetFontHeight`, `ConsoleIsActive`, `ConsolePrintAllMatchingCommands`, `SetConsoleKey` | | [CVar](docs/API.md#cvar) | `C_CVar.AreCVarsLoaded`, `C_CVar.DoesCVarExist`, `C_CVar.GetCVarBitfield`, `C_CVar.GetCVarBool`, `C_CVar.GetCVarInfo`, `C_CVar.SetCVarBitfield` | | [Cursor](docs/API.md#cursor) | `GetCursorInfo` | -| [Container](docs/API.md#container) | `C_Container.CalculateTotalNumberOfFreeBagSlots`, `C_Container.GetContainerItemCharges`, `C_Container.GetContainerItemDurability`, `C_Container.GetContainerItemID`, `C_Container.GetContainerItemInfo`, `C_Container.GetContainerItemRepairCost`, `C_Container.GetContainerNumFreeSlots`, `C_Container.GetItemCooldown`, `C_Container.HasContainerItem`, `C_Container.IsContainerItemOpenable`, `C_Container.MoveItem`, `C_Container.PlayerHasHearthstone`, `C_Container.SwapItems`, `C_Container.UseHearthstone`, `GetItemCooldown` | +| [Container](docs/API.md#container) | `C_Container.AutoStoreItem`, `C_Container.CalculateTotalNumberOfFreeBagSlots`, `C_Container.GetContainerItemCharges`, `C_Container.GetContainerItemDurability`, `C_Container.GetContainerItemID`, `C_Container.GetContainerItemInfo`, `C_Container.GetContainerItemRepairCost`, `C_Container.GetContainerNumFreeSlots`, `C_Container.GetItemCooldown`, `C_Container.HasContainerItem`, `C_Container.IsContainerItemOpenable`, `C_Container.MoveItem`, `C_Container.PlayerHasHearthstone`, `C_Container.SwapItems`, `C_Container.UseHearthstone`, `GetItemCooldown` | | [Creature](docs/API.md#creature) | `C_CreatureInfo.GetCreatureID`, `C_CreatureInfo.GetCreatureInfoByID`, `C_CreatureInfo.RequestLoadCreatureByID`, `C_CreatureInfo.GetRaceInfo`, `C_CreatureInfo.GetClassInfo`, `C_CreatureInfo.GetCreatureFamilyInfo`, `C_CreatureInfo.GetCreatureFamilyIDs`, `C_CreatureInfo.GetFactionInfo`, `C_CreatureInfo.GetCreatureTypeInfo`, `C_CreatureInfo.GetCreatureTypeIDs` | | [Currency](docs/API.md#currency) | `GetCoinTextureString`, `C_CurrencyInfo.GetCoinTextureString` | | EncodingUtil | `C_EncodingUtil.CompressString`, `C_EncodingUtil.DecompressString`, `C_EncodingUtil.EncodeBase64`, `C_EncodingUtil.DecodeBase64`, `C_EncodingUtil.EncodeHex`, `C_EncodingUtil.DecodeHex`, `C_EncodingUtil.SerializeJSON`, `C_EncodingUtil.DeserializeJSON`, `C_EncodingUtil.SerializeCBOR`, `C_EncodingUtil.DeserializeCBOR` | diff --git a/docs/API.md b/docs/API.md index 88ab0cb9..35d0b117 100644 --- a/docs/API.md +++ b/docs/API.md @@ -87,6 +87,7 @@ build instructions. - [`C_Container.UseHearthstone()`](#c_containerusehearthstone) - [`C_Container.SwapItems(srcBag, srcSlot, dstBag, dstSlot)`](#c_containerswapitemssrcbag-srcslot-dstbag-dstslot) - [`C_Container.MoveItem(srcBag, srcSlot, dstBag, dstSlot, count)`](#c_containermoveitemsrcbag-srcslot-dstbag-dstslot-count) + - [`C_Container.AutoStoreItem(srcBag, srcSlot [, dstBag])`](#c_containerautostoreitemsrcbag-srcslot--dstbag) - [Creature](#creature) - [`C_CreatureInfo.GetCreatureID(guid)`](#c_creatureinfogetcreatureidguid) @@ -2293,6 +2294,74 @@ constraint as `SwapItems`. Send is fire-and-forget (same as `SwapItems`). +### `C_Container.AutoStoreItem(srcBag, srcSlot [, dstBag])` + +Moves an item and lets the server pick the destination slot. Returns +`true` on send, `false` for bad args (missing bag, out-of-range slot, +empty source, or a source and destination the call cannot serve). + +The server does not just look for an empty slot. It first merges the +item into existing stacks of the same item, and splits it across +several of them when that is what fits. It places a remainder in a +free slot only if some is left over. So one call consolidates a +partial stack: + +```lua +-- Two partial stacks of item 12662: slot 1 holds 5, slot 2 holds 3. +C_Container.AutoStoreItem(0, 2) +-- Slot 1 now holds 8, and slot 2 is empty. + +-- Confine the item to bag 2 +C_Container.AutoStoreItem(0, 5, 2) +``` + +`dstBag` says where you want the item, and defaults to `0`: + +- `0` — anywhere in the main inventory that it fits. This means the + whole inventory, not the backpack. The backpack cannot be named + separately as a destination. +- `1..4` — that equipped bag only. +- `-1` — anywhere in the bank that it fits. + +Deposits and withdrawals both work, and both merge on arrival: + +```lua +-- Deposit, merging into stacks already in the bank +C_Container.AutoStoreItem(0, 3, -1) + +-- Withdraw bank slot 5 back into the bags +C_Container.AutoStoreItem(-1, 5, 0) +``` + +A bank item always travels back to your bags, so you cannot ask for a +new slot inside the bank. These three combinations are accepted, and +every other pair returns `false`: + +| Source | `dstBag` | Result | +|---|---|---| +| inventory | `0..4` | stays in the inventory | +| inventory | `-1` | goes to the bank | +| bank | `0` | comes back to the inventory | + +To consolidate stacks inside the bank, use `C_Container.MoveItem` for +each pair. Individual bank bags (`5..10`) are not valid destinations. + +Bank slots need the bank window open, the same as `SwapItems` and +`MoveItem`. + +> **Prefer this over merging stacks yourself.** This call needs no +> stack-size lookup, because the server decides what fits. A merge you +> compute in Lua has to size each stack first with +> `C_Item.GetItemMaxStackSizeByID`, which returns `nil` until that +> item's data has arrived. A merge written that way silently skips any +> stack it cannot size. + +There is no way to name the destination slot, which is the point of the +call. Use `C_Container.SwapItems` or `C_Container.MoveItem` when you +need to choose it. + +Send is fire-and-forget (same as `SwapItems` and `MoveItem`). + ## Creature ### `C_CreatureInfo.GetCreatureID(guid)` diff --git a/src/Offsets.h b/src/Offsets.h index 980d299e..e1987887 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -799,8 +799,10 @@ enum Offsets { // `Script_EquipCursorItem` (0x00489660) uses after the cursor's // source location has been resolved. Sends opcode 0x10D // (CMSG_SWAP_INV_ITEM) for same-container swaps or 0x10C - // (CMSG_AUTOEQUIP_ITEM) for cross-container, then runs the - // packet through the engine's own send pipeline at FUN_005AB630. + // (CMSG_SWAP_ITEM) for cross-container, then runs the packet + // through the engine's own send pipeline at FUN_005AB630. + // (0x10C is SWAP_ITEM; CMSG_AUTOEQUIP_ITEM is 0x10A and belongs to + // the cursor/equip builder FUN_005E1480, not this one.) // // Signature: // void __thiscall( @@ -853,6 +855,84 @@ enum Offsets { // u32 count); // only low byte FUN_INVENTORY_SPLIT = 0x005E1210, + // Third sibling in the same packet-builder family — `__thiscall`, + // same shared bag-byte converter (`FUN_005E13B0`), same send + // pipeline (`FUN_005AB630`). Builds `CMSG_AUTOSTORE_BAG_ITEM` + // (opcode 0x10B): "take this item and put it wherever it belongs + // in that container", with the DESTINATION SLOT CHOSEN BY THE + // SERVER. Wire format: + // [0x10B, srcBag, srcLinearSlot, dstBag] + // + // Signature — EIGHT stack args, `RET 0x20`. The count is from the + // RET, not from a decompiler parameter list: the last slot + // (`EBP+0x24`) is never READ by the body, but it IS popped, so a + // 7-arg declaration leaves the callee popping four bytes too many + // and ESP walks on every call. Trailing-ignored-arg is this + // family's habit — it is where swap keeps its `flag` and split its + // `count` — but the family is NOT uniform in arity: those two take + // nine (`RET 0x24`), this one eight. + // void __thiscall( + // CGPlayer *this, + // u32 unused1, u32 unused2, // EBP+0x08/+0x0C, unread + // u32 srcContainerLo, u32 srcContainerHi, + // u32 srcLinearSlot, // only low byte hits the wire + // u32 dstContainerLo, u32 dstContainerHi, + // u32 unused3); // EBP+0x24, unread but popped + // + // Note there is no dst slot argument at all — that is the point of + // the opcode. Server-side (`HandleAutoStoreBagItemOpcode`) it runs + // `CanStoreItem(dstBag, NULL_SLOT, …)`, which is a TWO-PASS search: + // first "merge into existing non-full stacks of this item" (filling + // a position/count vector, so one stack can be distributed across + // several destinations), then "find a free slot" for whatever count + // is left. So this single packet performs partial-stack + // consolidation with no stack-size lookup on our side at all. + // + // Note the value in that is NOT that the client's stack size could + // be wrong: 1.12 has no item DBC, so stack size arrives from the + // server and is cached in itemcache.wdb, and the client's copy + // normally agrees by construction. The value is availability — + // `C_Item.GetItemMaxStackSizeByID` is nil until an item's data has + // arrived, so anything that sizes stacks first has a cold-cache + // hole, and the server never needs to size them. + // + // The converter returns `0xFF` for any GUID absent from the + // player's invMgr container array, and the player's own GUID is + // absent — so passing the PLAYER as the destination sends + // `dstBag = 0xFF` (INVENTORY_SLOT_BAG_0), which the server reads as + // "search every bag". The builder guards that case: a converted + // `0xFF` is only allowed through when the destination GUID really is + // the player's, otherwise it drops the send silently. Consequence: + // the backpack is not separately addressable as a destination (it + // IS the player container), so bagID 0 means "anywhere it fits". + FUN_INVENTORY_AUTOSTORE = 0x005E12E0, + + // Bank counterpart — `CMSG_AUTOSTORE_BANK_ITEM` (opcode 0x282). + // THREE stack args, `RET 0xC`. Wire: [0x282, srcBag, srcSlot]. + // void __thiscall( + // CGPlayer *this, + // u32 srcContainerLo, u32 srcContainerHi, + // u32 srcLinearSlot); // only low byte hits the wire + // + // No destination of any kind, because the server DERIVES THE + // DIRECTION FROM THE SOURCE (`HandleAutoStoreBankItemOpcode`): + // a bank source runs `CanStoreItem(NULL_BAG, NULL_SLOT, …)` and + // lands in the inventory; an inventory source runs + // `CanBankItem(NULL_BAG, NULL_SLOT, …)` and lands in the bank. + // Both are the same merge-into-existing-stacks-then-free-slot + // search as 0x10B, just aimed at the other side. + // + // So this opcode moves an item ACROSS the inventory/bank boundary + // and cannot move one within its own side. Bank-internal + // consolidation is not expressible through autostore at all — it + // needs per-pair moves (`FUN_INVENTORY_SPLIT` / `_SWAP`). + // + // Unlike its siblings this one validates the source itself before + // sending: resolves the container by GUID (`FUN_00468460` with + // typeMask 1), bounds-checks the slot against the container's item + // array, and bails when the GUID in that slot is zero. + FUN_INVENTORY_AUTOSTORE_BANK = 0x005E18F0, + // Registers a single global Lua function. __fastcall(name, func). FUN_FRAMESCRIPT_REGISTER_FUNCTION = 0x00704120, @@ -2516,9 +2596,17 @@ enum Offsets { VAR_GUILD_ROSTER_TOTAL_COUNT = 0x00B73118, OFF_GUILD_MEMBER_NAME = 0x08, - // PackBagSlot — __fastcall(L, void **outInvMgr, int *outLinearSlot, int *outUnused) → bool. + // PackBagSlot — __fastcall(L, void **outInvMgr, int *outLinearSlot, int *outIsBank) → bool. // Reads bagID at Lua stack[1] and slot at stack[2], validates them, and // returns the inventory manager + linear slot ready to feed into GetItemBySlot. + // + // The fourth out-param is NOT unused (it was labelled `outUnused` + // here until the bank-autostore work): it is set to 1 for a bank + // position — bagID -1 (main bank) and bagIDs 5..10 (bank bags) — + // and left 0 otherwise, keyring included. `Script_UseContainerItem` + // is what consumes it: a non-zero flag is what makes right-clicking + // a bank item send `CMSG_AUTOSTORE_BANK_ITEM` instead of using it. + // This is the engine's own definition of "is this a bank slot". FUN_PACK_BAG_SLOT = 0x004F9820, // Equipped-bag container-GUID getter — `uint64 __fastcall(uint bagIndex0)` // where `bagIndex0` is 0-based (Lua bagID 1..4 → 0..3; 4..9 are bank bags, @@ -2563,10 +2651,18 @@ enum Offsets { // it hides data that's present from boot. Reading the GUID array // directly recovers it without ever opening the bank window. OFF_INVMGR_GUID_ARRAY = 0x04, + INVMGR_BACKPACK_FIRST_SLOT = 23, + INVMGR_BACKPACK_LAST_SLOT = 38, INVMGR_BANK_MAIN_FIRST_SLOT = 39, INVMGR_BANK_MAIN_LAST_SLOT = 62, INVMGR_BANK_BAG_FIRST_SLOT = 63, INVMGR_BANK_BAG_LAST_SLOT = 68, + // Keyring (Lua bagID -2). No LAST constant: the engine bounds this + // range against the invMgr's own slot count (OFF_INVMGR_SLOT_COUNT) + // rather than a fixed size, since keyring capacity grows with level. + // Base confirmed from FUN_PACK_BAG_SLOT, which maps Lua bagID -2 to + // `slot - 1 + 0x51`. + INVMGR_KEYRING_FIRST_SLOT = 81, // Engine's `ObjectMgr::Get`-style resolver — given a type and GUID, // returns the resolved CGObject pointer (or null). Same function the // engine itself uses inside `GetItemBySlot` (called at `0x00622904`) diff --git a/src/container/AutoStoreItem.cpp b/src/container/AutoStoreItem.cpp new file mode 100644 index 00000000..302a929d --- /dev/null +++ b/src/container/AutoStoreItem.cpp @@ -0,0 +1,74 @@ +// 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 . + +// `C_Container.AutoStoreItem(srcBag, srcSlot [, dstBag])` — move an item +// and let the SERVER choose the destination slot. A ClassicAPI-only +// extension, like its siblings `SwapItems` and `MoveItem`; the modern +// API has no equivalent, because it has no need for one. +// +// The reason to want it: the server does not just find an empty slot. +// It first merges the stack into existing non-full stacks of the same +// item, splitting it across several of them when that is what fits, and +// only places a remainder if any is left over. So this is a one-call +// stack consolidator that needs no stack-size lookup — the caller never +// has to know what fits. A merge computed on this side has to size +// every stack first, and `C_Item.GetItemMaxStackSizeByID` is nil until +// that item's data has arrived, so a cold item cache silently skips +// exactly the stacks it could not size. +// +// `dstBag` defaults to 0, which means "anywhere in the main inventory +// that it fits" rather than "the backpack" — on the wire the backpack +// IS the player container, so the two cannot be distinguished. Pass +// 1..4 to confine the item to one equipped bag. Bank destinations are +// rejected (see [[item/Swap.h]] `AutoStore`). +// +// Returns true once the packet is away — like the other two, the send +// is fire-and-forget and the server confirms through the normal +// BAG_UPDATE / SMSG_INVENTORY_CHANGE_FAILURE flow. + +#include "Game.h" +#include "item/Swap.h" + +namespace Container::AutoStoreItem { + +namespace { + +int __fastcall Script_C_Container_AutoStoreItem(void *L) { + if (!Game::Lua::IsNumber(L, 1) || !Game::Lua::IsNumber(L, 2)) { + Game::Lua::Error(L, + "Usage: C_Container.AutoStoreItem(srcBag, srcSlot [, dstBag])"); + return 0; + } + // Read every arg before calling AutoStore — its source lookup goes + // through ResolveBag, which stomps the Lua stack via PackBagSlot. + const int srcBag = static_cast(Game::Lua::ToNumber(L, 1)); + const int srcSlot = static_cast(Game::Lua::ToNumber(L, 2)); + const int dstBag = Game::Lua::IsNumber(L, 3) + ? static_cast(Game::Lua::ToNumber(L, 3)) + : 0; + + const bool ok = Item::Swap::AutoStore(L, srcBag, srcSlot, dstBag); + Game::Lua::PushBool(L, ok); + return 1; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterTableFunction("C_Container", "AutoStoreItem", + &Script_C_Container_AutoStoreItem); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +} // namespace Container::AutoStoreItem diff --git a/src/item/Swap.cpp b/src/item/Swap.cpp index 139aa7f0..3cb45ef4 100644 --- a/src/item/Swap.cpp +++ b/src/item/Swap.cpp @@ -55,35 +55,53 @@ bool ReadGuid(const void *cgObject, uint32_t *lo, uint32_t *hi) { // the magic +19 in FromBag is explained at the call site. constexpr int FIRST_BAG_CONTAINER_LINEAR = 19; -// Backpack contents (bag 0) live at linear slots 23..38 in the -// player invMgr — i.e. 1-based bag-0 slot S maps to linear 22+S. -constexpr int BACKPACK_LINEAR_OFFSET = 22; +// Player-container linear-slot bases, DERIVED from the named +// first-slot constants in Offsets.h rather than restated. The whole map +// (paperdoll / backpack / bank / bank bags / keyring) is documented at +// `Offsets::OFF_INVMGR_GUID_ARRAY`; a 1-based slot S in one of these +// ranges maps to `base + S`, so each base is that range's first linear +// slot minus one. +constexpr int BACKPACK_LINEAR_OFFSET = Offsets::INVMGR_BACKPACK_FIRST_SLOT - 1; +constexpr int BANK_MAIN_LINEAR_OFFSET = Offsets::INVMGR_BANK_MAIN_FIRST_SLOT - 1; +constexpr int KEYRING_LINEAR_OFFSET = Offsets::INVMGR_KEYRING_FIRST_SLOT - 1; -// Main bank contents (bagID -1) live at linear slots 39..62 in the -// player invMgr — i.e. 1-based bank slot S maps to linear 38+S. -// Same encoding shape as the backpack (container is the player -// itself), different base offset. Range valid only when the bank -// window has been opened in the current session. -constexpr int BANK_MAIN_LINEAR_OFFSET = 38; -constexpr int BANK_MAIN_NUM_SLOTS = 24; - -// Bank bag containers occupy 1-based equipment slots 64..69 (linear -// 63..68). Addressed by bagID 5..10 — bagID 5 = first bank bag. +// Bank bag containers are addressed by bagID 5..10 — bagID 5 = first +// bank bag. `ResolveEquipmentSlot` takes a 1-based equipment slot, so +// the base is the bank-bag range's first linear slot plus one. // Requires bank window open; otherwise the engine's CGContainer for // the bag is unsynced and `ResolveEquipmentSlot` returns null. -constexpr int FIRST_BANK_BAG_EQ_SLOT = 64; +constexpr int FIRST_BANK_BAG_EQ_SLOT = Offsets::INVMGR_BANK_BAG_FIRST_SLOT + 1; constexpr int FIRST_BANK_BAG_ID = 5; constexpr int LAST_BANK_BAG_ID = 10; +// Bounds a player-container linear slot the way `FUN_PACK_BAG_SLOT` +// does — against the invMgr's own live slot count, not a hardcoded +// size. That is what makes the keyring addressable without knowing its +// capacity, which grows as the character unlocks slots. +bool LinearSlotInRange(const void *player, uint32_t linearSlot) { + const uint32_t count = Game::Read( + player, + Offsets::OFF_PLAYER_INVENTORY_MANAGER + Offsets::OFF_INVMGR_SLOT_COUNT); + return linearSlot < count; +} + // (bagID, 1-based slot) → (containerGUID, engine linear slot). Returns // false for unequipped bags or out-of-range slots; does NOT validate // that the slot is occupied (caller checks separately). // -// bagID 0 → container = player, linear = 22 + slot (backpack) -// bagID 1..4 → container = equipped bag's CGContainer, linear = slot - 1 -// bagID -1 → container = player, linear = 38 + slot (bank main) +// bagID 0 → container = player, linear = 22 + slot (backpack) +// bagID 1..4 → container = equipped bag's CGContainer, linear = slot - 1 +// bagID -1 → container = player, linear = 38 + slot (bank main) +// bagID -2 → container = player, linear = 80 + slot (keyring) // bagID 5..10 → container = bank bag's CGContainer, linear = slot - 1 // +// This mirrors the engine's own bagID→(container, linear slot) map in +// `FUN_PACK_BAG_SLOT`, minus that function's Lua coupling: it reads its +// bagID and slot off the Lua stack and stomps it, which is why this is +// a separate encoder rather than a call into it. Keep the two in step — +// the keyring row above exists because this encoder was missing it +// while the engine's map had it all along. +// // Bank-side encodings only resolve once the bank window has been // opened in the current session (the engine doesn't sync the bank // CGContainers or bank-slot inventory entries before then). @@ -92,15 +110,41 @@ bool EncodeBagSlot(int bagID, int slotInBag, uint32_t *linearSlot) { if (slotInBag < 1) return false; + + // Backpack, main bank and keyring all live directly in the player's + // invMgr and differ only in their base — and in whether the range + // has a fixed end. `lastLinear == 0` means "no fixed end", which is + // the keyring: its capacity grows, so the invMgr count is the only + // correct bound. + int base = 0; + int lastLinear = 0; + bool inPlayer = true; if (bagID == 0) { - void *player = const_cast(Unit::Identity::PlayerObject()); + base = BACKPACK_LINEAR_OFFSET; + lastLinear = Offsets::INVMGR_BACKPACK_LAST_SLOT; + } else if (bagID == -1) { + base = BANK_MAIN_LINEAR_OFFSET; + lastLinear = Offsets::INVMGR_BANK_MAIN_LAST_SLOT; + } else if (bagID == -2) { + base = KEYRING_LINEAR_OFFSET; + } else { + inPlayer = false; + } + if (inPlayer) { + const void *player = Unit::Identity::PlayerObject(); if (player == nullptr) return false; + const uint32_t linear = static_cast(base + slotInBag); + if (lastLinear != 0 && linear > static_cast(lastLinear)) + return false; // would alias into the next range + if (!LinearSlotInRange(player, linear)) + return false; if (!ReadGuid(player, containerLo, containerHi)) return false; - *linearSlot = static_cast(BACKPACK_LINEAR_OFFSET + slotInBag); + *linearSlot = linear; return true; } + if (bagID >= 1 && bagID <= 4) { const uint8_t *bag = Item::Location::ResolveEquipmentSlot( FIRST_BAG_CONTAINER_LINEAR + bagID); @@ -111,17 +155,6 @@ bool EncodeBagSlot(int bagID, int slotInBag, *linearSlot = static_cast(slotInBag - 1); return true; } - if (bagID == -1) { - if (slotInBag > BANK_MAIN_NUM_SLOTS) - return false; - void *player = const_cast(Unit::Identity::PlayerObject()); - if (player == nullptr) - return false; - if (!ReadGuid(player, containerLo, containerHi)) - return false; - *linearSlot = static_cast(BANK_MAIN_LINEAR_OFFSET + slotInBag); - return true; - } if (bagID >= FIRST_BANK_BAG_ID && bagID <= LAST_BANK_BAG_ID) { const uint8_t *bag = Item::Location::ResolveEquipmentSlot( FIRST_BANK_BAG_EQ_SLOT + (bagID - FIRST_BANK_BAG_ID)); @@ -135,6 +168,35 @@ bool EncodeBagSlot(int bagID, int slotInBag, return false; } +// Container GUID for a bagID, with no slot involved — what the +// autostore opcode needs, since it names a destination container and +// lets the server choose the slot inside it. Bags 0 and -1 resolve to +// the player, which the engine's converter reports as `0xFF` +// (INVENTORY_SLOT_BAG_0); the two are indistinguishable on the wire. +bool EncodeContainerGuid(int bagID, uint32_t *containerLo, uint32_t *containerHi) { + if (bagID == 0 || bagID == -1) { + void *player = const_cast(Unit::Identity::PlayerObject()); + if (player == nullptr) + return false; + return ReadGuid(player, containerLo, containerHi); + } + if (bagID >= 1 && bagID <= 4) { + const uint8_t *bag = Item::Location::ResolveEquipmentSlot( + FIRST_BAG_CONTAINER_LINEAR + bagID); + if (bag == nullptr) + return false; + return ReadGuid(bag, containerLo, containerHi); + } + if (bagID >= FIRST_BANK_BAG_ID && bagID <= LAST_BANK_BAG_ID) { + const uint8_t *bag = Item::Location::ResolveEquipmentSlot( + FIRST_BANK_BAG_EQ_SLOT + (bagID - FIRST_BANK_BAG_ID)); + if (bag == nullptr) + return false; + return ReadGuid(bag, containerLo, containerHi); + } + return false; +} + // Common send path. All paperdoll-side state (player ptr, player // GUID) is resolved here; caller supplies the source side // (already-encoded linear slot + the container GUID it lives in). @@ -285,6 +347,80 @@ bool MoveCount(void *L, int srcBag, int srcSlot, int dstBag, int dstSlot, int co return true; } +bool AutoStore(void *L, int srcBag, int srcSlot, int dstBag) { + const bool srcIsBank = + srcBag == -1 || (srcBag >= FIRST_BANK_BAG_ID && srcBag <= LAST_BANK_BAG_ID); + + // Which of the two autostore opcodes serves this pair — and whether + // either does. The bank opcode takes its direction from the source, + // so it can only cross the boundary; see the header note. + bool crossToBank = false; // use FUN_INVENTORY_AUTOSTORE_BANK + if (srcIsBank) { + if (dstBag != 0) + return false; // bank→bank, or a bank source aimed at one bag + crossToBank = true; // 0x282 sends a bank source to the inventory + } else if (dstBag == -1) { + crossToBank = true; // 0x282 sends an inventory source to the bank + } else if (dstBag < 0 || dstBag > 4) { + return false; // individual bank bags are not addressable + } + + uint32_t srcContainerLo = 0, srcContainerHi = 0, srcLinear = 0; + if (!EncodeBagSlot(srcBag, srcSlot, + &srcContainerLo, &srcContainerHi, &srcLinear)) + return false; + + uint32_t dstContainerLo = 0, dstContainerHi = 0; + if (!crossToBank && + !EncodeContainerGuid(dstBag, &dstContainerLo, &dstContainerHi)) + return false; + + // Confirm the source is occupied so an empty slot is a clean local + // `false` instead of a server reject. Stomps the Lua stack. + if (Item::Location::ResolveBag(L, srcBag, srcSlot) == nullptr) + return false; + + void *player = const_cast(Unit::Identity::PlayerObject()); + if (player == nullptr) + return false; + + if (crossToBank) { + // Three stack args, `RET 0xC`. No destination on the wire — the + // server reads the direction off the source position. + using AutoStoreBankFn_t = void(__thiscall *)( + void *thisPlayer, + uint32_t srcContainerLo, uint32_t srcContainerHi, + uint32_t srcSlot); + auto fnBank = reinterpret_cast( + Offsets::FUN_INVENTORY_AUTOSTORE_BANK); + fnBank(player, srcContainerLo, srcContainerHi, srcLinear); + return true; + } + + // No item GUID on the wire for this opcode — the server locates the + // item from (srcBag, srcLinearSlot). The first two args are the + // family's ABI padding, as with the split builder. + // + // EIGHT stack args, matching the callee's `RET 0x20`. The trailing + // one is never read by the body, but declaring seven makes it pop + // four bytes we never pushed, and the caller's stack shifts under + // it — which shows up much later as a garbage pointer in whoever + // runs next, not as a fault here. + using AutoStoreFn_t = void(__thiscall *)( + void *thisPlayer, + uint32_t unused1, uint32_t unused2, + uint32_t srcContainerLo, uint32_t srcContainerHi, uint32_t srcSlot, + uint32_t dstContainerLo, uint32_t dstContainerHi, + uint32_t unused3); + auto fn = reinterpret_cast(Offsets::FUN_INVENTORY_AUTOSTORE); + fn(player, + 0, 0, + srcContainerLo, srcContainerHi, srcLinear, + dstContainerLo, dstContainerHi, + 0); + return true; +} + bool Containers(void *L, int srcBag, int srcSlot, int dstBag, int dstSlot) { uint32_t srcContainerLo = 0, srcContainerHi = 0, srcLinear = 0; uint32_t dstContainerLo = 0, dstContainerHi = 0, dstLinear = 0; diff --git a/src/item/Swap.h b/src/item/Swap.h index 7142c591..6d68fb61 100644 --- a/src/item/Swap.h +++ b/src/item/Swap.h @@ -97,4 +97,48 @@ bool Containers(void *L, int srcBag, int srcSlot, int dstBag, int dstSlot); // Lua stack. bool MoveCount(void *L, int srcBag, int srcSlot, int dstBag, int dstSlot, int count); +// "Put this item away" — the server picks the destination slot +// (`CMSG_AUTOSTORE_BAG_ITEM`, opcode 0x10B). Unlike `Containers` and +// `MoveCount` there is no destination slot to supply, and that is the +// whole value of it: the server first merges the stack into existing +// non-full stacks of the same item (splitting it across several if +// needed), then places any remainder in a free slot. +// +// So this is the right primitive for consolidating partial stacks, +// because it needs no stack-size lookup on this side. Computing the +// merge here would mean sizing every stack first, and the max-stack +// getter is nil until that item's data has arrived from the server — +// so a cold item cache silently skips exactly the stacks it could not +// size. The server never has to look one up. +// +// `dstBag` says where you want it, and two different engine builders +// serve it because the wire has two autostore opcodes: +// dstBag 0 → anywhere in the main inventory that it fits +// dstBag 1..4 → that equipped bag only +// dstBag -1 → anywhere in the bank that it fits +// +// The backpack is not separately addressable, because on the wire the +// backpack IS the player container — so 0 means the whole inventory, +// not "the backpack". Individual bank bags (5..10) are likewise not +// addressable: the bank opcode carries no destination at all. +// +// WHAT IS NOT EXPRESSIBLE: moving an item to a fresh slot on the side +// it is already on, when that side is the bank. The bank opcode derives +// its direction from the source, so a bank-sourced item always travels +// to the inventory. Consolidating stacks WITHIN the bank therefore +// cannot use this call — use `MoveCount` per pair instead. The +// combinations this accepts are exactly: +// inventory source, dstBag 0..4 → stays in the inventory +// inventory source, dstBag -1 → goes to the bank +// bank source, dstBag 0 → comes back to the inventory +// Anything else returns false rather than sending a packet that would +// do something other than what was asked. +// +// Returns false on bad args, an unequipped bag, a rejected +// source/destination pair, or an empty source slot. Everything else is +// server-side; failures come back as `SMSG_INVENTORY_CHANGE_FAILURE` +// exactly as for the other two. Stomps the Lua stack for the same +// reason `Containers` does. +bool AutoStore(void *L, int srcBag, int srcSlot, int dstBag); + } // namespace Item::Swap