mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-15 23:50:38 +00:00
C_Container.SwapItems
This commit is contained in:
@@ -23,7 +23,7 @@ Full per-function reference: **[docs/API.md](docs/API.md)**.
|
||||
| [Class](docs/API.md#class) | `FillLocalizedClassList` |
|
||||
| [Combat](docs/API.md#combat) | `InCombatLockdown` |
|
||||
| [CVar](docs/API.md#cvar) | `C_CVar.GetCVarBool` |
|
||||
| [Container](docs/API.md#container) | `C_Container.GetContainerItemDurability`, `C_Container.GetContainerItemID`, `C_Container.GetContainerItemRepairCost`, `C_Container.GetContainerNumFreeSlots`, `C_Container.GetItemCooldown`, `C_Container.PlayerHasHearthstone`, `C_Container.UseHearthstone`, `GetItemCooldown` |
|
||||
| [Container](docs/API.md#container) | `C_Container.GetContainerItemDurability`, `C_Container.GetContainerItemID`, `C_Container.GetContainerItemRepairCost`, `C_Container.GetContainerNumFreeSlots`, `C_Container.GetItemCooldown`, `C_Container.PlayerHasHearthstone`, `C_Container.SwapItems`, `C_Container.UseHearthstone`, `GetItemCooldown` |
|
||||
| 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` |
|
||||
| [EquipmentSet](docs/API.md#equipmentset) | `C_EquipmentSet.CanUseEquipmentSets`, `C_EquipmentSet.ClearIgnoredSlotsForSave`, `C_EquipmentSet.CreateEquipmentSet`, `C_EquipmentSet.DeleteEquipmentSet`, `C_EquipmentSet.EquipmentSetContainsLockedItems`, `C_EquipmentSet.GetEquipmentSetID`, `C_EquipmentSet.GetEquipmentSetIDs`, `C_EquipmentSet.GetEquipmentSetInfo`, `C_EquipmentSet.GetIgnoredSlots`, `C_EquipmentSet.GetItemIDs`, `C_EquipmentSet.GetItemLocations`, `C_EquipmentSet.GetNumEquipmentSets`, `C_EquipmentSet.IgnoreSlotForSave`, `C_EquipmentSet.IsSlotIgnoredForSave`, `C_EquipmentSet.ModifyEquipmentSet`, `C_EquipmentSet.SaveEquipmentSet`, `C_EquipmentSet.UnignoreSlotForSave`, `C_EquipmentSet.UseEquipmentSet` (sets persist to `WTF\Account\...\ClassicAPI_EquipmentSets.txt`) |
|
||||
| [Events](docs/API.md#events) | `C_EventUtils.IsEventValid` |
|
||||
|
||||
+38
@@ -37,6 +37,7 @@ build instructions.
|
||||
- [`C_Container.GetContainerNumFreeSlots(bagID)`](#c_containergetcontainernumfreeslotsbagid)
|
||||
- [`C_Container.PlayerHasHearthstone()`](#c_containerplayerhashearthstone)
|
||||
- [`C_Container.UseHearthstone()`](#c_containerusehearthstone)
|
||||
- [`C_Container.SwapItems(srcBag, srcSlot, dstBag, dstSlot)`](#c_containerswapitemssrcbag-srcslot-dstbag-dstslot)
|
||||
- [`C_Item.UseItemByName(itemInfo)`](#c_itemuseitembynameiteminfo)
|
||||
|
||||
- [CVar](#cvar)
|
||||
@@ -848,6 +849,43 @@ is skipped. Bypassing the dispatcher avoids the Lua-stack roundtrip
|
||||
of pushing `(bag, slot)` only for `Script_UseContainerItem` to re-parse
|
||||
and re-resolve the same item.
|
||||
|
||||
### `C_Container.SwapItems(srcBag, srcSlot, dstBag, dstSlot)`
|
||||
|
||||
Atomically swaps two bag slots on the server, no cursor involved.
|
||||
Returns `true` on send, `false` for bad args (missing bag, out-of-range
|
||||
slot, empty source).
|
||||
|
||||
```lua
|
||||
-- Move slot 3 of bag 1 to slot 7 of the backpack
|
||||
C_Container.SwapItems(1, 3, 0, 7)
|
||||
|
||||
-- Swap two backpack slots
|
||||
C_Container.SwapItems(0, 1, 0, 2)
|
||||
```
|
||||
|
||||
Bag IDs match the rest of `C_Container.*`: `0` is the backpack, `1..4`
|
||||
are the equipped bags. Slots are 1-based. The engine accepts both
|
||||
directions:
|
||||
|
||||
- Same container (e.g. backpack ↔ backpack, bag1 ↔ bag1) sends opcode
|
||||
`0x10D` `CMSG_SWAP_INV_ITEM` with the two linear slot bytes.
|
||||
- Cross-container (e.g. backpack ↔ bag1, bag2 ↔ bag3) sends opcode
|
||||
`0x10C` `CMSG_SWAP_ITEM` with `(srcBag, srcSlot, dstBag, dstSlot)`.
|
||||
|
||||
Destination empty becomes a move; destination occupied becomes an
|
||||
atomic swap.
|
||||
|
||||
> **ClassicAPI-only.** Modern Classic Era has no direct swap-two-slots
|
||||
> call — addons there drive the cursor with two `PickupContainerItem`
|
||||
> calls in sequence. This single-call form bypasses the cursor
|
||||
> entirely (same path
|
||||
> [`C_EquipmentSet.UseEquipmentSet`](#c_equipmentsetuseequipmentsetsetid)
|
||||
> uses for its swap chain).
|
||||
|
||||
Send is fire-and-forget. Server confirms via the normal
|
||||
`BAG_UPDATE` / `SMSG_INVENTORY_CHANGE_FAILURE` flow; this call does not
|
||||
wait for that round-trip.
|
||||
|
||||
### `C_Item.UseItemByName(itemInfo)`
|
||||
|
||||
Finds the first item in the player's bags matching `itemInfo` and
|
||||
|
||||
+71
-34
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "item/Swap.h"
|
||||
|
||||
#include "Game.h"
|
||||
#include "Offsets.h"
|
||||
#include "item/Location.h"
|
||||
|
||||
@@ -63,6 +64,39 @@ constexpr int FIRST_BAG_CONTAINER_LINEAR = 19;
|
||||
// player invMgr — i.e. 1-based bag-0 slot S maps to linear 22+S.
|
||||
constexpr int BACKPACK_LINEAR_OFFSET = 22;
|
||||
|
||||
// (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 = BACKPACK_LINEAR_OFFSET + slot
|
||||
// bagID 1..4 → container = equipped bag's CGContainer, linear = slot - 1
|
||||
bool EncodeBagSlot(int bagID, int slotInBag,
|
||||
uint32_t *containerLo, uint32_t *containerHi,
|
||||
uint32_t *linearSlot) {
|
||||
if (slotInBag < 1)
|
||||
return false;
|
||||
if (bagID == 0) {
|
||||
void *player = ResolvePlayer();
|
||||
if (player == nullptr)
|
||||
return false;
|
||||
if (!ReadGuid(player, containerLo, containerHi))
|
||||
return false;
|
||||
*linearSlot = static_cast<uint32_t>(BACKPACK_LINEAR_OFFSET + slotInBag);
|
||||
return true;
|
||||
}
|
||||
if (bagID >= 1 && bagID <= 4) {
|
||||
const uint8_t *bag = Item::Location::ResolveEquipmentSlot(
|
||||
FIRST_BAG_CONTAINER_LINEAR + bagID);
|
||||
if (bag == nullptr)
|
||||
return false;
|
||||
if (!ReadGuid(bag, containerLo, containerHi))
|
||||
return false;
|
||||
*linearSlot = static_cast<uint32_t>(slotInBag - 1);
|
||||
return true;
|
||||
}
|
||||
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).
|
||||
@@ -100,41 +134,10 @@ bool SendSwap(const void *srcItem,
|
||||
} // namespace
|
||||
|
||||
bool FromBag(const void *cgItem, int bagID, int slotInBag, int dstPaperdollSlot) {
|
||||
if (slotInBag < 1)
|
||||
uint32_t containerLo = 0, containerHi = 0, linearSlot = 0;
|
||||
if (!EncodeBagSlot(bagID, slotInBag, &containerLo, &containerHi, &linearSlot))
|
||||
return false;
|
||||
|
||||
if (bagID == 0) {
|
||||
// Backpack: container is the player, linear slot is in the
|
||||
// 23..38 range of the player invMgr.
|
||||
void *player = ResolvePlayer();
|
||||
if (player == nullptr)
|
||||
return false;
|
||||
uint32_t playerLo = 0, playerHi = 0;
|
||||
if (!ReadGuid(player, &playerLo, &playerHi))
|
||||
return false;
|
||||
return SendSwap(cgItem,
|
||||
playerLo, playerHi,
|
||||
static_cast<uint32_t>(BACKPACK_LINEAR_OFFSET + slotInBag),
|
||||
dstPaperdollSlot);
|
||||
}
|
||||
|
||||
if (bagID >= 1 && bagID <= 4) {
|
||||
// Equipped bag: container is the bag's CGContainer (a CGItem),
|
||||
// linear slot is 0-based within that bag.
|
||||
const uint8_t *bag = Item::Location::ResolveEquipmentSlot(
|
||||
FIRST_BAG_CONTAINER_LINEAR + bagID);
|
||||
if (bag == nullptr)
|
||||
return false;
|
||||
uint32_t bagLo = 0, bagHi = 0;
|
||||
if (!ReadGuid(bag, &bagLo, &bagHi))
|
||||
return false;
|
||||
return SendSwap(cgItem,
|
||||
bagLo, bagHi,
|
||||
static_cast<uint32_t>(slotInBag - 1),
|
||||
dstPaperdollSlot);
|
||||
}
|
||||
|
||||
return false;
|
||||
return SendSwap(cgItem, containerLo, containerHi, linearSlot, dstPaperdollSlot);
|
||||
}
|
||||
|
||||
bool FromPaperdoll(const void *cgItem, int srcPaperdollSlot, int dstPaperdollSlot) {
|
||||
@@ -153,4 +156,38 @@ bool FromPaperdoll(const void *cgItem, int srcPaperdollSlot, int dstPaperdollSlo
|
||||
dstPaperdollSlot);
|
||||
}
|
||||
|
||||
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;
|
||||
if (!EncodeBagSlot(srcBag, srcSlot,
|
||||
&srcContainerLo, &srcContainerHi, &srcLinear))
|
||||
return false;
|
||||
if (!EncodeBagSlot(dstBag, dstSlot,
|
||||
&dstContainerLo, &dstContainerHi, &dstLinear))
|
||||
return false;
|
||||
|
||||
// Look up the source CGItem so we can put its GUID on the wire.
|
||||
// This stomps the Lua stack — caller must have already read every
|
||||
// arg it cares about.
|
||||
const uint8_t *srcItem = Item::Location::ResolveBag(L, srcBag, srcSlot);
|
||||
if (srcItem == nullptr)
|
||||
return false; // empty source slot
|
||||
|
||||
void *player = ResolvePlayer();
|
||||
if (player == nullptr)
|
||||
return false;
|
||||
|
||||
uint32_t itemLo = 0, itemHi = 0;
|
||||
if (!ReadGuid(srcItem, &itemLo, &itemHi))
|
||||
return false;
|
||||
|
||||
auto fn = reinterpret_cast<SwapFn_t>(Offsets::FUN_INVENTORY_SWAP);
|
||||
fn(player,
|
||||
itemLo, itemHi,
|
||||
srcContainerLo, srcContainerHi, srcLinear,
|
||||
dstContainerLo, dstContainerHi, dstLinear,
|
||||
0);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Item::Swap
|
||||
|
||||
@@ -44,4 +44,24 @@ bool FromBag(const void *cgItem, int bagID, int slotInBag, int dstPaperdollSlot)
|
||||
|
||||
bool FromPaperdoll(const void *cgItem, int srcPaperdollSlot, int dstPaperdollSlot);
|
||||
|
||||
// General bag-to-bag swap — neither side is constrained to the
|
||||
// paperdoll. Same engine helper (`FUN_INVENTORY_SWAP`), which routes
|
||||
// to opcode 0x10D (same-container slot swap, e.g. backpack ↔ backpack)
|
||||
// or 0x10C (cross-container swap, e.g. backpack → equipped bag) based
|
||||
// on whether the two container GUIDs match.
|
||||
//
|
||||
// bagID convention matches `C_Container.GetContainerItemInfo`:
|
||||
// 0 = backpack
|
||||
// 1..4 = equipped bags (in equipment slots 20..23)
|
||||
// slot is 1-based. Returns false if either bag is unequipped, either
|
||||
// slot is out of bounds, or the source slot is empty. Destination is
|
||||
// allowed to be empty — the engine treats that as a move; or occupied
|
||||
// — engine atomically swaps.
|
||||
//
|
||||
// Requires a valid Lua state pointer because the source-side CGItem
|
||||
// lookup goes through `Item::Location::ResolveBag`, which uses the
|
||||
// engine's `PackBagSlot` helper. Stomps the Lua stack — callers must
|
||||
// read every arg they need before calling.
|
||||
bool Containers(void *L, int srcBag, int srcSlot, int dstBag, int dstSlot);
|
||||
|
||||
} // namespace Item::Swap
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
// This file is part of ClassicAPI.
|
||||
//
|
||||
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
|
||||
// of the GNU Lesser 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License along with
|
||||
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
// `C_Container.SwapItems(srcBag, srcSlot, dstBag, dstSlot)` — atomic
|
||||
// server-side swap of two bag slots, no cursor involvement. This is a
|
||||
// ClassicAPI-only extension, not a modern API mirror; modern Classic
|
||||
// has no direct swap-two-slots call (addons there string two
|
||||
// `PickupContainerItem` calls together to drive the cursor). Same
|
||||
// engine primitive `C_EquipmentSet.UseEquipmentSet` and our paperdoll-
|
||||
// swap path use — see [[item/Swap.h]] `Item::Swap::Containers`.
|
||||
//
|
||||
// Returns true on send, false on bad args (missing bag, out-of-range
|
||||
// slot, empty source). Send is fire-and-forget — server confirms via
|
||||
// the normal SMSG_INVENTORY_CHANGE_FAILURE / BAG_UPDATE flow.
|
||||
|
||||
#include "Game.h"
|
||||
#include "item/Swap.h"
|
||||
|
||||
namespace Item::SwapContainerItems {
|
||||
|
||||
namespace {
|
||||
|
||||
int __fastcall Script_C_Container_SwapItems(void *L) {
|
||||
if (!Game::Lua::IsNumber(L, 1) || !Game::Lua::IsNumber(L, 2) ||
|
||||
!Game::Lua::IsNumber(L, 3) || !Game::Lua::IsNumber(L, 4)) {
|
||||
Game::Lua::Error(L,
|
||||
"Usage: C_Container.SwapItems(srcBag, srcSlot, dstBag, dstSlot)");
|
||||
return 0;
|
||||
}
|
||||
// Read all four args before invoking Containers — its internal
|
||||
// ResolveBag 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 = static_cast<int>(Game::Lua::ToNumber(L, 3));
|
||||
const int dstSlot = static_cast<int>(Game::Lua::ToNumber(L, 4));
|
||||
|
||||
const bool ok = Item::Swap::Containers(L, srcBag, srcSlot, dstBag, dstSlot);
|
||||
Game::Lua::PushBool(L, ok);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void RegisterLuaFunctions() {
|
||||
Game::Lua::RegisterTableFunction("C_Container", "SwapItems",
|
||||
&Script_C_Container_SwapItems);
|
||||
}
|
||||
|
||||
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
|
||||
|
||||
} // namespace
|
||||
|
||||
} // namespace Item::SwapContainerItems
|
||||
Reference in New Issue
Block a user