diff --git a/docs/API.md b/docs/API.md index 1ce2a578..0e03746c 100644 --- a/docs/API.md +++ b/docs/API.md @@ -768,6 +768,7 @@ build instructions. - [PaperDollInfo](#paperdollinfo) - [`C_PaperDollInfo.GetInventorySlotInfoForInvSlot(invSlot)`](#c_paperdollinfogetinventoryslotinfoforinvslotinvslot) + - [`C_PaperDollInfo.GetTemporaryEnchantmentInfo(slot)`](#c_paperdollinfogettemporaryenchantmentinfoslot) - [PartyInfo](#partyinfo) - [`InviteUnit(targetName)`](#inviteunittargetname) @@ -12679,6 +12680,10 @@ That enchant is a separate field. An empty slot returns `false, 0, 0, 0`. An item you do not own returns the same, and so does a location that resolves to nothing. +For an equipped item, +[`C_PaperDollInfo.GetTemporaryEnchantmentInfo`](#c_paperdollinfogettemporaryenchantmentinfoslot) +gives the same enchant as a table. + This call is a ClassicAPI extension. ### `C_Item.GetEnchantInfo(enchantID)` @@ -17790,6 +17795,29 @@ is not a number raises an error. -- 18, "interface\\paperdoll\\UI-PaperDoll-Slot-Ranged.blp", true, "RangedSlot" ``` +### `C_PaperDollInfo.GetTemporaryEnchantmentInfo(slot)` + +Returns the temporary enchant on an equipped item as a table. `slot` is an +`INVSLOT_` constant, for example `INVSLOT_MAINHAND`. If the slot has no +temporary enchant, the call returns nothing. + +| Field | Value | +|-------|-------| +| `enchantID` | The enchant. [`C_Item.GetEnchantInfo`](#c_itemgetenchantinfoenchantid) gives its name and effects. | +| `remainingTimeMs` | The time left in milliseconds. If the enchant has no time limit, the value is `0`. | +| `chargesRemaining` | The charges left. An oil has `0`. | +| `hasExpirationTime` | `true` if the enchant has a time limit. | + +```lua +local info = C_PaperDollInfo.GetTemporaryEnchantmentInfo(INVSLOT_MAINHAND) +if info then + print(info.enchantID, info.remainingTimeMs, info.chargesRemaining) +end +``` + +[`C_Item.GetItemTempEnchantInfo`](#c_itemgetitemtempenchantinfoitemlocation) +reads the same enchant on a weapon in a bag. + ## PartyInfo These functions invite players to your group, remove players from your diff --git a/src/item/WeaponEnchant.cpp b/src/item/WeaponEnchant.cpp index a35e1a6f..3c733618 100644 --- a/src/item/WeaponEnchant.cpp +++ b/src/item/WeaponEnchant.cpp @@ -28,6 +28,7 @@ constexpr int INVSLOT_RANGED = 18; struct EnchantInfo { bool has; + bool hasExpiration; uint32_t expirationMs; uint32_t charges; uint32_t enchantID; @@ -47,7 +48,7 @@ struct EnchantInfo { // — the same block `spell/BonusDamage.cpp` walks for enchant-granted // spell power. EnchantInfo ReadTempEnchant(const uint8_t *cgItem) { - EnchantInfo r{false, 0, 0, 0}; + EnchantInfo r{false, false, 0, 0, 0}; if (cgItem == nullptr) return r; const uint8_t *desc = Item::ObjectFields(cgItem); @@ -66,6 +67,7 @@ EnchantInfo ReadTempEnchant(const uint8_t *cgItem) { desc, kSlot + Offsets::DESCRIPTOR_ENCHANTMENT_CHARGES_DELTA); if (Game::Read( desc, kSlot + Offsets::DESCRIPTOR_ENCHANTMENT_DURATION_DELTA) != 0) { + r.hasExpiration = true; using TimeLeft_t = uint32_t(__fastcall *)(const uint8_t *item, void *edx, int slot); r.expirationMs = reinterpret_cast( static_cast(Offsets::FUN_ITEM_ENCHANTMENT_TIME_LEFT))( @@ -157,11 +159,60 @@ int __fastcall Script_C_Item_GetItemTempEnchantInfo(void *L) { return 4; } +// `C_PaperDollInfo.GetTemporaryEnchantmentInfo(slot)` — retail's per-slot +// temporary enchant, the one its deprecated `GetWeaponEnchantInfo` is now +// built from. `slot` is an equipment slot (INVSLOT_*). Returns a +// `TemporaryItemEnchantInfo` table, or nothing when the slot has no +// temporary enchant (or no item, or is not an equipment slot). +// `hasExpirationTime` is the descriptor duration gate `ReadTempEnchant` +// reads; without it `remainingTimeMs` is 0. +const Game::Doc::Field kTemporaryItemEnchantInfoFields[] = { + Game::Doc::Req("enchantID", "number"), + Game::Doc::Req("remainingTimeMs", "number"), + Game::Doc::Req("chargesRemaining", "number"), + Game::Doc::Req("hasExpirationTime", "bool"), +}; +const Game::Doc::Structure kTemporaryItemEnchantInfo{ + "TemporaryItemEnchantInfo", "PaperDollInfo", kTemporaryItemEnchantInfoFields}; + +const Game::Doc::Field kGetTemporaryEnchantmentInfoArgs[] = { + Game::Doc::Req("slot", "number", "An INVSLOT_ constant."), +}; +const Game::Doc::Field kGetTemporaryEnchantmentInfoRets[] = { + Game::Doc::Req("enchantInfo", "TemporaryItemEnchantInfo"), +}; +const Game::Doc::Function kGetTemporaryEnchantmentInfo{ + "Returns the temporary enchantment on an equipped item, or nothing if there is none.", + kGetTemporaryEnchantmentInfoArgs, kGetTemporaryEnchantmentInfoRets}; + +int __fastcall Script_GetTemporaryEnchantmentInfo(void *L) { + if (!Game::Lua::IsNumber(L, 1)) { + Game::Lua::Error(L, "Usage: C_PaperDollInfo.GetTemporaryEnchantmentInfo(slot)"); + return 0; + } + const int slot = static_cast(Game::Lua::ToNumber(L, 1)); + if (slot < Offsets::EQUIPMENT_SLOT_FIRST || slot > Offsets::EQUIPMENT_SLOT_LAST) + return 0; + + const EnchantInfo e = ReadEquippedTempEnchant(slot); + if (!e.has) + return 0; + Game::Lua::NewTable(L); + Game::Lua::SetFieldNumber(L, "enchantID", static_cast(e.enchantID)); + Game::Lua::SetFieldNumber(L, "remainingTimeMs", static_cast(e.expirationMs)); + Game::Lua::SetFieldNumber(L, "chargesRemaining", static_cast(e.charges)); + Game::Lua::SetFieldBool(L, "hasExpirationTime", e.hasExpiration); + return 1; +} + void RegisterLuaFunctions() { Game::Lua::RegisterTableFunction("C_Item", "GetWeaponEnchantInfo", &Script_C_Item_GetWeaponEnchantInfo); Game::Lua::RegisterTableFunction("C_Item", "GetItemTempEnchantInfo", &Script_C_Item_GetItemTempEnchantInfo); + Game::Lua::RegisterTableFunction("C_PaperDollInfo", "GetTemporaryEnchantmentInfo", + &Script_GetTemporaryEnchantmentInfo, + &kGetTemporaryEnchantmentInfo); } const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};