diff --git a/README.md b/README.md index 8a36233f..32d7cccb 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Quick index: | Namespace | Calls | |-----------|-------| -| Spell | `GetSpellInfo` (with `(slot, bookType)` overload), `C_Spell.GetSpellInfo`, `C_Spell.GetSpellName`, `C_Spell.GetSpellTexture`, `GetSpellLink` (with `(slot, bookType)` overload), `C_Spell.GetSpellLink`, `FindSpellBookSlotByID`, `C_Spell.GetSpellDescription`, `IsPassiveSpell` (with `(slot, bookType)` overload), `C_Spell.IsSpellPassive`, `IsPlayerSpell`, `IsSpellKnown` | +| Spell | `GetSpellInfo` (with `(slot, bookType)` overload), `C_Spell.GetSpellInfo`, `C_Spell.GetSpellName`, `C_Spell.GetSpellTexture`, `GetSpellLink` (with `(slot, bookType)` overload), `C_Spell.GetSpellLink`, `FindSpellBookSlotByID`, `C_Spell.GetSpellDescription`, `IsPassiveSpell` (with `(slot, bookType)` overload), `C_Spell.IsSpellPassive`, `IsPlayerSpell`, `IsSpellKnown`, `IsUsableSpell` (with `(slot, bookType)` overload), `C_Spell.IsSpellUsable` | | GameTooltip | `GameTooltip:SetSpellByID`, `GameTooltip:SetItemByID`, `GameTooltip:SetUnitAura` | | Quest | `GetQuestIDFromLogIndex`, `C_QuestLog.RequestLoadQuestByID` (+ `QUEST_DATA_LOAD_RESULT` event), `C_QuestLog.GetTitleForQuestID` | | Faction | `GetFactionIDByIndex`, `GetFactionInfoByID`, `GetFactionParentID` | diff --git a/TODO.md b/TODO.md index a779cb89..28b3ce78 100644 --- a/TODO.md +++ b/TODO.md @@ -1059,17 +1059,83 @@ applies cleanly: Useful for any cooldown-tracking addon that wants to query talent abilities or off-spellbook spells. -## 49. `IsUsableSpell(spellID)` overload — easy +## ~~49. `IsUsableSpell(spell)` / `C_Spell.IsSpellUsable(spellID)`~~ — DONE -Same pattern as #48. Modern takes spellID; 1.12 takes (slot, bookType). -The engine has a usability-check helper that takes a spellID after the -slot resolution — find it via 5.4.8's modern overload and use it -directly. +Both shipped. `IsUsableSpell` accepts spellID or `(slot, bookType)` +via the same `Spell::Lookup::SpellbookSlotToID` resolver other +spell-API functions use. Returns `(1, nil)` / `(nil, 1)` / +`(nil, nil)` per legacy convention. `C_Spell.IsSpellUsable` is the +modern shape — same logic, returns proper booleans. -Returns `(usable, noMana)` like the existing function — usable=false -means the spell can't be cast right now (silenced, wrong shapeshift -form, etc.); noMana=true is the specific "you don't have enough mana" -sub-case. +**Two engine-cache approaches investigated and discarded:** + +1. **Per-spell cache at `0x004F0F40` returning fields at +0x564 + / +0x568** — initially looked promising (the action-bar + dispatcher at `0x004E5BA0` reads these), but the writers + (`0x004EFF17` cluster) showed they're parsed config integers, + not real-time usability bools. False trail. +2. **Action-bar per-slot caches at `0x00BC67A0` (noMana) and + `0x00BC6B60` (usable)** — these ARE real-time, but only updated + for the 120 action-bar slots. Spells off the bar stay + uninitialized. Action-bar-only is the wrong abstraction since + modern `IsUsableSpell(spellID)` is action-bar-agnostic. + +Settled on a **manual five-check approach**: +1. Player knows the spell (`VAR_PLAYER_SPELL_BITMAP` lookup — + covers profession recipes, talents, racials, etc.). +2. Player is alive (`HEALTH > 0`). +3. Spell is not on cooldown (`FUN_SPELL_QUERY_COOLDOWN` — + the engine helper `Script_GetSpellCooldown` calls internally + after slot resolution, queried with `bookType=0` for player). +4. Player has ≥ `ManaCost` of the spell's `PowerType` — + only this failure flips `noMana=true`. +5. Player has all required reagents in bags + (`Spell.dbc` Reagent[8] / ReagentCount[8] at `+0x110` / + `+0x130`, walked via `Item::Location::ResolveBag`). + +**Not checked** (deliberate, different concerns): silence, GCD, +stance/form, range, target type, line-of-sight, casting state. + +**Verified on Turtle WoW:** +- Mana check: Renew rank 3 (cost 105) reports usable at 144 mana + and unusable at 39 mana, transitioning exactly at the cost + boundary. +- Reagent check: SHIPPED BUT UNVERIFIED — the Reagent[8] / + ReagentCount[8] offsets at `+0x110` / `+0x130` are the + CMaNGOS-documented vanilla layout, and we know spell records + match CMaNGOS docs (PowerType=+0x7C and ManaCost=+0x80 both + match), so high confidence — but no in-game verification yet. + Should test with a reagent spell (e.g., Mage Teleport spell 3565 + needs Rune of Teleportation 17031). Failure mode if offsets are + wrong: reagent check becomes a no-op (always passes). +- Cooldown check: SHIPPED BUT UNVERIFIED in this implementation — + the helper signature was traced via `Script_GetSpellCooldown` at + `0x004B40A0` but not exercised in-game. Should test with a + cooldown'd spell. + +**Important offset correction along the way.** Initial implementation +read POWER1 at descriptor `+0x5C` based on the CMaNGOS-documented +field index `0x17`. That was *max* mana — current was at `+0x44` +(field index `0x11`, six fields earlier than the standard layout). +The whole UNIT_FIELD layout in 1.12.1 is shifted 0x18 / 6 fields +earlier than CMaNGOS documents: + +| Field | CMaNGOS docs | 1.12.1 actual | +|---------------|-------------:|--------------:| +| HEALTH | +0x58 | +0x40 | +| POWER1 (mana) | +0x5C | +0x44 | +| POWER2..5 | +0x60..+0x6C | +0x48..+0x54 | +| MAXHEALTH | +0x70 | +0x58 | +| MAXPOWER1..5 | +0x74..+0x80 | +0x5C..+0x6C | +| LEVEL | +0x88 | +0x70 | +| FLAGS | +0xA0 | +0xA0 ✓ | + +FLAGS still lands at +0xA0 (verified separately by +`Script_UnitPlayerControlled` and `Script_UnitAffectingCombat`). +Trust the binary, verify with `_classicapi_DescDump` if you need +to derive new ones. + +See [src/spell/Usable.cpp](src/spell/Usable.cpp). ## 50. Cross-binary technique applications worth picking up — meta diff --git a/docs/API.md b/docs/API.md index 3930c54c..37f4832b 100644 --- a/docs/API.md +++ b/docs/API.md @@ -19,6 +19,8 @@ build instructions. - [`C_Spell.IsSpellPassive(spellID)`](#c_spellisspellpassivespellid) - [`IsPlayerSpell(spellID)`](#isplayerspellspellid) - [`IsSpellKnown(spellID, [isPet])`](#isspellknownspellid-ispet) + - [`IsUsableSpell(spell)` / `IsUsableSpell(slot, bookType)`](#isusablespellspell--isusablespellslot-booktype) + - [`C_Spell.IsSpellUsable(spellID)`](#c_spellisspellusablespellid) - [GameTooltip](#gametooltip) - [`GameTooltip:SetSpellByID(spellID)`](#gametooltipsetspellbyidspellid) - [Quest](#quest) @@ -403,6 +405,70 @@ same shape just different addresses). Equivalent to the function of the same name introduced in 3.0. +### `IsUsableSpell(spell)` / `IsUsableSpell(slot, bookType)` + +Returns `(usable, noMana)` for a spell, matching the modern +3.0+ signature. Returns `(1, nil)` when the spell is castable, +`(nil, 1)` when only mana is preventing it, `(nil, nil)` for any +other reason (unknown spell, dead, etc.). Matches the 1.12-style +`1`/`nil` return convention used by the existing +`Script_IsUsableAction`. + +Two arg shapes accepted: + +- **`IsUsableSpell(spellID)`** — direct spellID lookup. +- **`IsUsableSpell(slot, bookType)`** — `bookType` is `"spell"` + (player) or `"pet"`. Resolves to a spellID via the same engine + spellbook arrays `GetSpellInfo`/`GetSpellLink` walk. + +```lua +IsUsableSpell(133) -- Fireball: 1, nil if you have mana, nil, 1 if not +IsUsableSpell(1, "spell") -- player spellbook slot 1 +``` + +> **What this function checks:** +> +> 1. Player knows the spell (engine's spell-knowledge bitmap — +> covers trained class abilities, talent passives, racials, +> profession recipes). +> 2. Player is alive (HEALTH > 0). +> 3. Spell is not on cooldown (engine's per-spell cooldown helper). +> 4. Player has enough of the spell's power type for the base cost +> (mana / rage / focus / energy / happiness) — *only* this +> failure sets `noMana=true`. +> 5. Player has all required reagents in bags (Spell.dbc +> Reagent[8] / ReagentCount[8]). +> +> **What this function doesn't check** (different concerns or +> post-vanilla concepts): silence, GCD, stance/form, range, target +> type, line-of-sight, casting state. +> +> Verified empirically on Turtle WoW for the mana branch: Renew rank +> 3 (cost 105) is reported usable at 144 mana and unusable at 39 +> mana, transitioning at exactly the cost boundary. Cooldown and +> reagent checks ship in the same implementation but haven't been +> exercised in-game; if you find an inconsistency, the reagent +> offsets (+0x110 / +0x130) and cooldown helper (`0x006E2EA0`) are +> the components to verify. + +Equivalent to the function of the same name introduced in 3.0. + +### `C_Spell.IsSpellUsable(spellID)` + +Modern table-namespace form. Same logic as +[`IsUsableSpell(spellID)`](#isusablespellspell--isusablespellslot-booktype) +but returns proper booleans (`isUsable`, `insufficientPower`) per +the `C_Spell.*` convention rather than `1`/`nil` pairs. + +```lua +local usable, noMana = C_Spell.IsSpellUsable(133) +-- usable=true, noMana=false → cast it +-- usable=false, noMana=true → drink up +-- usable=false, noMana=false → unknown spell, dead, or other block +``` + +Equivalent to the function of the same name introduced in 10.x. + ## GameTooltip ### `GameTooltip:SetItemByID(itemID)` diff --git a/src/Offsets.h b/src/Offsets.h index fb598a57..24f07772 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -95,6 +95,32 @@ enum Offsets { // `UNIT_FLAG_PLAYER_CONTROLLED`, which `Script_UnitPlayerControlled` // (`0x00516410`) tests via `mov eax, [m_objectFields + 0xA0]; // shr eax, 3; test al, 1`. + // CGUnit m_objectFields current/max stat offsets, **verified + // empirically** on Turtle WoW (1.12.1) by `_classicapi_DescDump` + // searching for the live `UnitMana` value at descriptor offsets: + // + // +0x40 HEALTH (current) + // +0x44 POWER1 (current mana — verified at multiple values) + // +0x48 POWER2 (current rage) + // +0x4C POWER3 (current focus) + // +0x50 POWER4 (current energy) + // +0x54 POWER5 (current happiness) + // +0x58 MAXHEALTH (= 807 at full HP in test data) + // +0x5C MAXPOWER1 (max mana — stays at 1435 even when current = 443) + // +0x60..+0x6C MAXPOWER2..5 + // +0x70 LEVEL (= 26 in test data) + // +0xA0 FLAGS (verified separately via `Script_UnitPlayerControlled`) + // + // **The 1.12.1 layout is offset 0x18 (= 6 fields) earlier than the + // CMaNGOS-documented vanilla layout** (which puts HEALTH at field + // 0x16 = +0x58). My initial implementation read MAXHEALTH/MAXMANA + // when I wanted current values — caused `IsUsableSpell` to falsely + // return usable even when mana was below cost. Trust the binary + // (and `_classicapi_DescDump` if you ever need to re-verify), not + // external emulator field tables. + OFF_UNIT_FIELD_HEALTH = 0x40, + OFF_UNIT_FIELD_POWER1 = 0x44, + OFF_UNIT_FIELD_LEVEL = 0x70, OFF_UNIT_FIELD_FLAGS = 0xA0, UNIT_FLAG_PLAYER_CONTROLLED = 0x08, // Bit 19 of UNIT_FIELD_FLAGS — `Script_UnitAffectingCombat` at @@ -477,6 +503,44 @@ enum Offsets { // size matches Spell.dbc's row count. Pre-login the slot is NULL. VAR_PLAYER_SPELL_BITMAP = 0x00B710FC, + // Per-spell cooldown query. `__fastcall(int spellID, int bookType, + // int *outStart, int *outDuration, int *outEnable)`. Same path + // `Script_GetSpellCooldown` (`0x004B40A0`) uses internally after + // resolving the (slot, bookType) Lua args to a spellID — we + // bypass the slot resolution and pass spellID directly with + // `bookType=0` (player) since that's the standard + // `IsUsableSpell(spellID)` use case. `outDuration > 0` means the + // spell is currently on cooldown. + FUN_SPELL_QUERY_COOLDOWN = 0x006E2EA0, + + // Spell.dbc reagent fields. Per CMaNGOS vanilla `SpellEntry` — + // Reagent[8] at +0x110 (itemIDs), ReagentCount[8] at +0x130 + // (counts). Used by `IsUsableSpell` to check that the player has + // each non-zero reagent in their bags. Unlike the unit-field + // offsets, spell-record offsets DO match the CMaNGOS-documented + // layout in 1.12.1 (verified previously: PowerType=+0x7C and + // ManaCost=+0x80 both match). + OFF_SPELL_REAGENT_ID = 0x110, + OFF_SPELL_REAGENT_COUNT = 0x130, + SPELL_MAX_REAGENTS = 8, + + // Per-spell *runtime* state cache, indexed by spellID via a hash + // table (mask at `[VAR_SPELL_STATE_HASH_MASK]`, base at + // `[VAR_SPELL_STATE_HASH_BASE]`). The engine maintains the cache as + // player state changes — cooldown, silence, GCD, mana balance, etc. + // each update flips the relevant byte. The action-bar usability + // path at `0x004E5BA0` reads `+0x564` (usable) and `+0x568` + // (noMana) directly off this cache; we do the same to back + // `IsUsableSpell` / `C_Spell.IsSpellUsable`. + // + // `FUN_SPELL_LOOKUP_STATE` is `__fastcall(int spellID) → void *` + // — the hash-walking helper. Returns null for spells the player + // doesn't know (cache only holds known spellIDs) or pre-login + // (when `[VAR_SPELL_STATE_HASH_MASK]` is `-1`). + FUN_SPELL_LOOKUP_STATE = 0x004F0F40, + OFF_SPELL_STATE_USABLE = 0x564, + OFF_SPELL_STATE_NO_MANA = 0x568, + // Spell description format helper. Reads the locale-resolved description // string from `record[+0x228 + locale*4]` and walks it character-by- // character substituting `$s1`/`$d`/`$o`/etc. placeholders with values diff --git a/src/spell/Usable.cpp b/src/spell/Usable.cpp new file mode 100644 index 00000000..b6df8193 --- /dev/null +++ b/src/spell/Usable.cpp @@ -0,0 +1,269 @@ +// 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 . + +#include "Game.h" +#include "Offsets.h" +#include "item/ID.h" +#include "item/Location.h" +#include "spell/Lookup.h" + +#include + +namespace Spell::Usable { + +namespace { + +using ResolveUnitToken_t = void *(__fastcall *)(const char *token); + +// Spell.dbc record field offsets we read for usability. Fully documented +// in CLAUDE.md. +constexpr int OFF_SPELL_POWER_TYPE = 0x7C; // int8: 0=mana, 1=rage, 2=focus, 3=energy, 4=happiness +constexpr int OFF_SPELL_MANA_COST = 0x80; // u32 base cost + +// Returns true if the player knows `spellID` per the engine's spell- +// knowledge bitmap at `[VAR_PLAYER_SPELL_BITMAP]`. Same check +// `IsPlayerSpell` (`src/spell/Info.cpp`) uses — covers trained +// abilities, talents, racials, profession recipes, etc. *Importantly*, +// covers spells that are **not** currently on an action bar — racials +// kept off the bar still register here. +bool PlayerKnowsSpell(int spellID) { + if (spellID <= 0) + return false; + auto *bitmap = *reinterpret_cast( + static_cast(Offsets::VAR_PLAYER_SPELL_BITMAP)); + if (bitmap == nullptr) + return false; + const int spellCount = *reinterpret_cast( + static_cast(Offsets::VAR_SPELL_RECORD_COUNT)); + if (spellID > spellCount) + return false; + return (bitmap[spellID >> 5] & (1u << (spellID & 31))) != 0; +} + +const uint8_t *PlayerDescriptor() { + auto resolve = reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); + auto *player = static_cast(resolve("player")); + if (player == nullptr) + return nullptr; + return *reinterpret_cast( + player + Offsets::OFF_UNIT_DESCRIPTOR); +} + +// Calls the engine's cooldown helper at `FUN_SPELL_QUERY_COOLDOWN` +// for the player spellbook (bookType=0). Returns true if the spell +// has an active cooldown. +// +// `__fastcall(spellID, bookType, *start, *duration, *enable)` — +// duration is the engine's raw value (milliseconds; the Lua-side +// `Script_GetSpellCooldown` multiplies by 0.001 before pushing). +// We don't care about the unit, just `> 0` vs `== 0`. +using QueryCooldown_t = void(__fastcall *)(int spellID, int bookType, + int *outStart, + int *outDuration, + int *outEnable); + +bool IsOnCooldown(int spellID) { + auto fn = reinterpret_cast(Offsets::FUN_SPELL_QUERY_COOLDOWN); + int start = 0, duration = 0, enable = 0; + fn(spellID, 0 /* bookType=player */, &start, &duration, &enable); + return duration > 0; +} + +// Walks player bags 0..4 counting items matching `targetItemID`. +// Returns the summed stack count. Same logic +// `Item::Count::CountInBag` uses; inlined here to avoid promoting +// it across modules just for one extra caller. +// +// Stomps the Lua stack — caller must own it. Stack contents on +// return are unspecified (caller should `SetTop` if needed). +int CountItemInBags(void *L, int targetItemID) { + using GetSlots_t = int(__fastcall *)(void *L); + auto getSlots = reinterpret_cast( + Offsets::FUN_SCRIPT_GET_CONTAINER_NUM_SLOTS); + int total = 0; + for (int bag = 0; bag <= 4; bag++) { + Game::Lua::SetTop(L, 0); + Game::Lua::PushNumber(L, static_cast(bag)); + getSlots(L); + if (!Game::Lua::IsNumber(L, -1)) + continue; + const int slots = static_cast(Game::Lua::ToNumber(L, -1)); + for (int slot = 1; slot <= slots; slot++) { + const uint8_t *item = Item::Location::ResolveBag(L, bag, slot); + if (item == nullptr) + continue; + if (Item::ID::FromCGItem(item) != targetItemID) + continue; + auto *itemDesc = *reinterpret_cast( + item + Offsets::OFF_ITEM_DESCRIPTOR); + if (itemDesc == nullptr) + continue; + total += static_cast(*reinterpret_cast( + itemDesc + Offsets::OFF_DESCRIPTOR_STACK_COUNT)); + } + } + return total; +} + +// Returns true iff the player has enough of every reagent the spell +// requires (Reagent[i] > 0 with corresponding ReagentCount[i] > 0). +// Spells with zero reagents trivially pass. +bool HasReagents(void *L, const uint8_t *record) { + for (int i = 0; i < Offsets::SPELL_MAX_REAGENTS; i++) { + const int reagentItemID = static_cast(*reinterpret_cast( + record + Offsets::OFF_SPELL_REAGENT_ID + i * 4)); + const int reagentCount = static_cast(*reinterpret_cast( + record + Offsets::OFF_SPELL_REAGENT_COUNT + i * 4)); + if (reagentItemID <= 0 || reagentCount <= 0) + continue; + if (CountItemInBags(L, reagentItemID) < reagentCount) + return false; + } + return true; +} + +// Computes (usable, noMana) for the local player. +// +// Checks performed (in order, short-circuiting on first failure): +// 1. Spell is known (engine bitmap — covers all sources: trained, +// talents, racials, profession recipes). +// 2. Spell record exists in Spell.dbc. +// 3. Player descriptor is reachable (post-login). +// 4. Player is alive (HEALTH > 0). +// 5. Spell is not on cooldown (engine cooldown helper). +// 6. Player has enough power for `ManaCost` of the spell's +// `PowerType`. Only this failure flips `noMana=true`. +// 7. Player has all required reagents in bags (Reagent[8] / +// ReagentCount[8] from the spell record). +// +// Not checked (deliberately — different concerns or post-vanilla +// concepts that don't apply): silence, GCD, stance/form, range, +// target type/validity, line-of-sight, casting state. +struct Usability { + bool usable; + bool noMana; +}; + +Usability ComputeUsability(void *L, int spellID) { + Usability r{false, false}; + + if (!PlayerKnowsSpell(spellID)) + return r; + + auto *record = Spell::Lookup::RecordForID(spellID); + if (record == nullptr) + return r; + + auto *desc = PlayerDescriptor(); + if (desc == nullptr) + return r; + + const int health = *reinterpret_cast( + desc + Offsets::OFF_UNIT_FIELD_HEALTH); + if (health <= 0) + return r; + + if (IsOnCooldown(spellID)) + return r; + + // Mana check is the only one that flips noMana. + const int powerType = static_cast(*reinterpret_cast( + record + OFF_SPELL_POWER_TYPE)); + if (powerType >= 0 && powerType <= 4) { + const int manaCost = static_cast(*reinterpret_cast( + record + OFF_SPELL_MANA_COST)); + if (manaCost > 0) { + const int currentPower = *reinterpret_cast( + desc + Offsets::OFF_UNIT_FIELD_POWER1 + powerType * 4); + if (currentPower < manaCost) { + r.noMana = true; + return r; + } + } + } + + if (!HasReagents(L, record)) + return r; + + r.usable = true; + return r; +} + +// Same arg-shape resolver `Spell::Info::ResolveLuaArgsToSpellID` uses — +// duplicated here because it's file-static there. Accepts spellID +// (number) or (slot, bookType) for spellbook lookups. +int ResolveSpellArg(void *L) { + if (!Game::Lua::IsNumber(L, 1)) + return 0; + const int arg1 = static_cast(Game::Lua::ToNumber(L, 1)); + if (Game::Lua::Type(L, 2) == Game::Lua::TYPE_STRING) { + const char *book = Game::Lua::ToString(L, 2); + const int bookType = (book != nullptr && + (book[0] == 'p' || book[0] == 'P') && + (book[1] == 'e' || book[1] == 'E') && + (book[2] == 't' || book[2] == 'T') && + book[3] == 0) ? 1 : 0; + return Spell::Lookup::SpellbookSlotToID(arg1, bookType); + } + return arg1; +} + +// `IsUsableSpell(spell)` / `IsUsableSpell(slot, bookType)` — vanilla- +// shaped legacy global. Returns `(usable, noMana)` as 1/nil pairs to +// match `Script_IsUsableAction`'s convention in stock 1.12. +int __fastcall Script_IsUsableSpell(void *L) { + if (!Game::Lua::IsNumber(L, 1)) { + Game::Lua::Error(L, "Usage: IsUsableSpell(spellID) or IsUsableSpell(slot, bookType)"); + return 0; + } + const int spellID = ResolveSpellArg(L); + Usability r = ComputeUsability(L, spellID); + if (r.usable) { + Game::Lua::PushNumber(L, 1.0); + Game::Lua::PushNil(L); + } else if (r.noMana) { + Game::Lua::PushNil(L); + Game::Lua::PushNumber(L, 1.0); + } else { + Game::Lua::PushNil(L); + Game::Lua::PushNil(L); + } + return 2; +} + +// `C_Spell.IsSpellUsable(spellID)` — modern table-namespace form. +// Returns proper booleans (`isUsable`, `insufficientPower`) per the +// `C_Spell.*` convention, not 1/nil pairs. +int __fastcall Script_C_Spell_IsSpellUsable(void *L) { + if (!Game::Lua::IsNumber(L, 1)) { + Game::Lua::Error(L, "Usage: C_Spell.IsSpellUsable(spellID)"); + return 0; + } + const int spellID = static_cast(Game::Lua::ToNumber(L, 1)); + Usability r = ComputeUsability(L, spellID); + Game::Lua::PushBoolean(L, r.usable ? 1 : 0); + Game::Lua::PushBoolean(L, r.noMana ? 1 : 0); + return 2; +} + +} // namespace + +static void RegisterLuaFunctions() { + Game::Lua::RegisterGlobalFunction("IsUsableSpell", &Script_IsUsableSpell); + Game::Lua::RegisterTableFunction("C_Spell", "IsSpellUsable", + &Script_C_Spell_IsSpellUsable); +} + +static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace Spell::Usable