diff --git a/README.md b/README.md index 806f4044..f0e97ec0 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Full per-function reference: **[docs/API.md](docs/API.md)**. | [Combat](docs/API.md#combat) | `InCombatLockdown` | | [CVar](docs/API.md#cvar) | `C_CVar.GetCVarBool` | | [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.GetContainerItemRepairCost`, `C_Container.GetContainerNumFreeSlots`, `C_Container.GetItemCooldown`, `C_Container.IsContainerItemOpenable`, `C_Container.MoveItem`, `C_Container.PlayerHasHearthstone`, `C_Container.SwapItems`, `C_Container.UseHearthstone`, `GetItemCooldown` | +| [Container](docs/API.md#container) | `C_Container.CalculateTotalNumberOfFreeBagSlots`, `C_Container.GetContainerItemCharges`, `C_Container.GetContainerItemDurability`, `C_Container.GetContainerItemID`, `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` | | [Coroutine](docs/API.md#coroutine) | `coroutine.create`, `coroutine.resume`, `coroutine.yield`, `coroutine.status`, `coroutine.wrap` | | [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` | | 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 8fe86b79..8848a3d9 100644 --- a/docs/API.md +++ b/docs/API.md @@ -40,6 +40,7 @@ build instructions. - [Container](#container) - [`C_Container.GetContainerItemID(bagIndex, slotIndex)`](#c_containergetcontaineritemidbagindex-slotindex) + - [`C_Container.HasContainerItem(bagIndex, slotIndex)`](#c_containerhascontaineritembagindex-slotindex) - [`GetItemCooldown(itemInfo)` / `C_Container.GetItemCooldown(itemID)`](#getitemcooldowniteminfo--c_containergetitemcooldownitemid) - [`C_Container.GetContainerItemDurability(containerIndex, slotIndex)`](#c_containergetcontaineritemdurabilitycontainerindex-slotindex) - [`C_Container.GetContainerItemRepairCost(containerIndex, slotIndex)`](#c_containergetcontaineritemrepaircostcontainerindex-slotindex) @@ -1061,6 +1062,25 @@ for slot = 1, 16 do end ``` +### `C_Container.HasContainerItem(bagIndex, slotIndex)` + +Returns `true` if the given bag slot currently holds an item, `false` +otherwise (empty slot or out-of-range indices). The occupancy sibling of +`GetContainerItemID` — same slot resolution, but a plain presence check. + +- `bagIndex = 0` — the player's main backpack; `1..4` — equipped bag slots. +- `slotIndex` — 1-based. + +Presence only: it does **not** require the item's static data to be cached, +so it returns `true` for a just-looted item even before `GetContainerItemID` +/ `GetItemInfo` resolve for it. + +```lua +if C_Container.HasContainerItem(0, 1) then + -- backpack slot 1 is occupied +end +``` + ### `GetItemCooldown(itemInfo)` / `C_Container.GetItemCooldown(itemID)` Returns `(startTime, duration, enable)` for the cooldown of the diff --git a/src/container/HasItem.cpp b/src/container/HasItem.cpp new file mode 100644 index 00000000..eccdac37 --- /dev/null +++ b/src/container/HasItem.cpp @@ -0,0 +1,52 @@ +// 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.HasContainerItem(bagIndex, slotIndex)` — returns whether the +// given bag slot currently holds an item (a Classic Era addition). It's the +// occupancy sibling of `GetContainerItemID`: resolves the slot to its CGItem +// the same way (`Item::Location::ResolveBag` → engine `PackBagSlot` → +// `GetItemBySlot`), and a populated slot yields a non-null pointer while an +// empty slot yields null. Presence only — it does not depend on the item's +// static data being cached. Returns `false` for empty slots and invalid +// bag/slot indices. + +#include "Game.h" +#include "item/Location.h" + +namespace Container::HasItem { + +namespace { + +int __fastcall Script_C_Container_HasContainerItem(void *L) { + if (!Game::Lua::IsNumber(L, 1) || !Game::Lua::IsNumber(L, 2)) { + Game::Lua::Error(L, "Usage: C_Container.HasContainerItem(bagIndex, slotIndex)"); + return 0; + } + const int bagID = static_cast(Game::Lua::ToNumber(L, 1)); + const int slotIndex = static_cast(Game::Lua::ToNumber(L, 2)); + + const bool hasItem = Item::Location::ResolveBag(L, bagID, slotIndex) != nullptr; + Game::Lua::PushBool(L, hasItem); + return 1; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterTableFunction("C_Container", "HasContainerItem", + &Script_C_Container_HasContainerItem); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +} // namespace Container::HasItem