Files
ClassicAPI/src/container/AutoStoreItem.cpp
T
Brues db88fca050 feat(container): add C_Container.AutoStoreItem
Moves an item and lets the server pick the destination slot, which is
worth having because the server does not merely find an empty one: it
first merges the stack into existing stacks of the same item, splitting
it across several when that is what fits, and places a remainder only if
one is left. So one call consolidates a partial stack.

Two engine builders back it, because the wire has two autostore opcodes.
FUN_005E12E0 sends CMSG_AUTOSTORE_BAG_ITEM (0x10B) and takes a
destination container, so it can confine an item to one equipped bag or
let the server search the whole inventory. FUN_005E18F0 sends
CMSG_AUTOSTORE_BANK_ITEM (0x282) and takes no destination at all - the
server derives the direction from the source, so a bank source travels
to the inventory and an inventory source to the bank. AutoStore picks
between them and accepts only the pairs the wire can honestly serve.
Bank-to-bank is not among them, so consolidating stacks within the bank
still needs per-pair moves.

Both builders' stack-arg counts come from their RET, not from a
decompiler parameter list. 0x10B's last slot is never read by the body
but is still popped, and declaring seven args instead of eight made the
callee pop four bytes we never pushed - which shifted the caller's frame
and surfaced as a garbage Lua state pointer in the next function to run,
several frames from the call. Noted at both offsets, since this family
is not uniform in arity.

Three fixes to the shared bagID encoder found while doing it:

- EncodeBagSlot had no case for the keyring (bagID -2), so SwapItems,
  MoveItem and AutoStoreItem all rejected keyring slots. The engine's
  own map in FUN_PACK_BAG_SLOT has had it all along, and the linear base
  was already recorded in the invMgr slot map.
- Its bank constants restated named Offsets::INVMGR_* values, so they
  now derive from them, and the slot map is fully named rather than half
  constants and half comment. Each player-container range is bounded by
  both its own last linear slot and the live invMgr count - stricter
  than before, where the backpack had no upper bound at all.
- FUN_PACK_BAG_SLOT's fourth out-param was labelled outUnused. It is the
  bank flag, and it is what makes right-clicking a bank item send 0x282
  instead of using the item.
2026-09-07 01:38:34 -05:00

75 lines
3.2 KiB
C++

// 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/>.
// `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<int>(Game::Lua::ToNumber(L, 1));
const int srcSlot = static_cast<int>(Game::Lua::ToNumber(L, 2));
const int dstBag = Game::Lua::IsNumber(L, 3)
? static_cast<int>(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