mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
fix(spell): make IsUsableSpell the engine's own usability verdict
IsUsableSpell hand-rolled five checks (known, alive, off cooldown, power, reagents) and diverged from IsUsableAction exactly where the engine does more or less: it never tested stance, so Whirlwind in Battle Stance reported usable, and it tested cooldown first, so a 0-rage Whirlwind on cooldown reported (nil, nil) instead of (nil, 1). IsUsableAction reads a per-slot cache that FUN_004E5050 fills by calling FUN_SPELL_IS_USABLE (0x006E3D60) on a player spell's record, or by comparing the pet's power against the spell cost for a pet spell. Run those same two paths live, plus the knowledge gate the helper lacks. Cooldown is not part of usability: the helper has no cooldown branch and FrameXML greys and swipes as separate states. Item::Usable had been calling the helper all along. A pet-book spell given by ID now takes the pet path instead of always reporting not usable. Offsets.h: add VAR_PET_GUID and FUN_PET_ACTIONS_USABLE (verified via Script_HasPetUI, Script_GetPetActionsUsable and the recompute's pet branch); replace the FUN_SPELL_IS_USABLE comment with the decompiled check list. Fixes #51
This commit is contained in:
@@ -1120,51 +1120,50 @@ 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.
|
||||
|
||||
**Two engine-cache approaches investigated and discarded:**
|
||||
**Reworked for issue #51 (2026-09-12) — it now IS the engine's
|
||||
verdict.** `IsUsableAction` reads the per-slot cache
|
||||
(`0x00BC6B60` usable / `0x00BC67A0` noMana, reader `FUN_004E5230`),
|
||||
which the recompute `FUN_004E5050` fills per slot. For a player spell
|
||||
slot that recompute is one call: `FUN_SPELL_IS_USABLE(record,
|
||||
&noMana)` (`0x006E3D60`). For a pet spell slot it is `FUN_PET_ACTIONS_
|
||||
USABLE()` then the pet's power vs `FUN_GET_SPELL_COST(spellID, 0)`.
|
||||
`Spell::Usable` now runs exactly those two paths live (plus a
|
||||
knowledge gate on `VAR_PLAYER_SPELL_BITMAP`, since the helper is also
|
||||
fed item on-use spells and never checks knowledge). Verified check
|
||||
list of the helper is on `FUN_SPELL_IS_USABLE` in `Offsets.h`.
|
||||
|
||||
**The first version hand-rolled five checks instead** (known, alive,
|
||||
off cooldown, power, reagents) and diverged from `IsUsableAction`
|
||||
exactly where the helper does more or less — the issue #51 repro
|
||||
(Warrior, Whirlwind):
|
||||
|
||||
| State | `IsUsableAction` | old `IsUsableSpell` |
|
||||
|---|---|---|
|
||||
| Berserker, 0 rage, on CD | `nil, 1` | `nil, nil` (we tested cooldown first — the engine never does) |
|
||||
| Battle Stance, 25 rage | `nil, nil` | `1, nil` (we never tested stance) |
|
||||
|
||||
Cooldown is deliberately NOT part of usability: FrameXML greys
|
||||
(`IsUsableAction`) and swipes (`GetActionCooldown`) as two separate
|
||||
states, and the engine helper has no cooldown branch. Rule #1 case:
|
||||
`Item::Usable` had been calling the helper all along.
|
||||
|
||||
**Two engine-cache approaches investigated and discarded before the
|
||||
first version** (still true, kept for the record):
|
||||
|
||||
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.
|
||||
not real-time usability bools. False trail. (Later identified as
|
||||
the macro entry's cached primary spell — see the `#showtooltip`
|
||||
notes.)
|
||||
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.
|
||||
modern `IsUsableSpell(spellID)` is action-bar-agnostic. The fix
|
||||
above sidesteps this by calling what FILLS the cache, not the
|
||||
cache.
|
||||
|
||||
**Important offset correction along the way.** Initial implementation
|
||||
read POWER1 at descriptor `+0x5C` based on the CMaNGOS-documented
|
||||
|
||||
+46
-35
@@ -14603,59 +14603,70 @@ GetSpellBonusHealing() -- your +Healing
|
||||
|
||||
### `IsUsableSpell(spell)` / `IsUsableSpell(slot, bookType)`
|
||||
|
||||
Returns `(usable, noMana)` for a spell. 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`/`nil`
|
||||
return convention of the existing `Script_IsUsableAction`.
|
||||
Returns `(usable, noMana)` for a spell. The result is the same verdict
|
||||
that `IsUsableAction` gives for an action slot that holds this spell,
|
||||
read live. Returns `(1, nil)` when you can cast the spell now,
|
||||
`(nil, 1)` when only power stops you, and `(nil, nil)` for any other
|
||||
block. The `1`/`nil` pairs match `IsUsableAction`.
|
||||
|
||||
Two arg shapes accepted:
|
||||
Two argument shapes:
|
||||
|
||||
- **`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.
|
||||
- **`IsUsableSpell(spellID)`** — a spellID. A spell that you do not
|
||||
know returns `(nil, nil)`. A pet spell given by ID (Growl, Cower) is
|
||||
tested for the pet.
|
||||
- **`IsUsableSpell(slot, bookType)`** — a spellbook slot. `bookType`
|
||||
is `"spell"` for the player book or `"pet"` for the pet book.
|
||||
|
||||
```lua
|
||||
IsUsableSpell(133) -- Fireball: 1, nil if you have mana, nil, 1 if not
|
||||
IsUsableSpell(133) -- Fireball: 1, nil with mana; nil, 1 without
|
||||
IsUsableSpell(1, "spell") -- player spellbook slot 1
|
||||
IsUsableSpell(1, "pet") -- pet spellbook slot 1
|
||||
```
|
||||
|
||||
> **What this function checks:**
|
||||
> **What the result includes.** Every condition that greys an action
|
||||
> button:
|
||||
>
|
||||
> 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]).
|
||||
> - The player is alive, unless the spell is castable while dead.
|
||||
> - The player has control. When control is lost (fear, charm), only
|
||||
> spells that work in that state pass.
|
||||
> - The required stance or form. Whirlwind in Battle Stance is not
|
||||
> usable.
|
||||
> - Reagents and totems in the bags.
|
||||
> - An equipped weapon of the required type, and ammo for ranged
|
||||
> spells.
|
||||
> - Combo points for finishers.
|
||||
> - Stealth-only and out-of-combat-only spells.
|
||||
> - Aura states on the player and on the current target.
|
||||
> - A toggle spell that is already active (Stealth while stealthed) is
|
||||
> not usable.
|
||||
> - Power. The cost of the spell, with your talents applied, against
|
||||
> your current power. This is the only test that sets `noMana`.
|
||||
>
|
||||
> **What this function doesn't check** (different concerns): silence,
|
||||
> GCD, stance/form, range, target type, line-of-sight, casting state.
|
||||
> **What the result does not include.** Cooldown, silence, and school
|
||||
> lockouts. An action button greys for usability and swipes for
|
||||
> cooldown as two separate states. This function keeps that split.
|
||||
> Read cooldown with
|
||||
> [`C_Spell.GetSpellCooldown`](#c_spellgetspellcooldownspellidentifier)
|
||||
> and lockouts with
|
||||
> [`C_LossOfControl.GetActiveLossOfControlData`](#c_lossofcontrolgetactivelossofcontroldataindex).
|
||||
>
|
||||
> 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.
|
||||
> For a pet spell, the test is the current power of the pet against
|
||||
> the spell cost, after the pet can act (not stunned, feared, or
|
||||
> confused).
|
||||
|
||||
### `C_Spell.IsSpellUsable(spellID)`
|
||||
|
||||
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.
|
||||
Table-namespace form. Same result as
|
||||
[`IsUsableSpell(spellID)`](#isusablespellspell--isusablespellslot-booktype),
|
||||
but returns booleans (`isUsable`, `insufficientPower`) per the
|
||||
`C_Spell.*` convention. Accepts a spellID, a spell link, or a spell
|
||||
name.
|
||||
|
||||
```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
|
||||
-- usable=false, noMana=false → unknown spell, wrong stance, dead, or another block
|
||||
```
|
||||
|
||||
### `C_Spell.GetSpellCooldown(spellIdentifier)`
|
||||
|
||||
+44
-14
@@ -1883,20 +1883,33 @@ enum Offsets {
|
||||
// folds in the descriptor cost mods).
|
||||
FUN_GET_SPELL_COST = 0x006E31B0,
|
||||
|
||||
// Full spell-castability check for the LOCAL player:
|
||||
// `char __fastcall(const uint8_t *spellRecord /*ecx*/,
|
||||
// int *outNoMana /*edx*/)`.
|
||||
// Resolves the local player internally (no unit arg), then gates
|
||||
// on casting state, stun/confuse flags, mechanic immunities,
|
||||
// shapeshift/form + required stances, aura-state requirements,
|
||||
// combo points (finishers), spell RequiredSkill, and finally the
|
||||
// power check: `cost(FUN_GET_SPELL_COST) <= currentPower` → usable,
|
||||
// else sets `*outNoMana = 1`. Returns nonzero (low byte) when
|
||||
// usable. Does NOT check spell knowledge — so it's valid to feed
|
||||
// an item's on-use spell record (which the player never "knows").
|
||||
// This is the helper the action-usability recompute uses for
|
||||
// player spell slots; `Item::Usable` reuses it for item on-use
|
||||
// spells. Returns 0 cleanly pre-world (no player).
|
||||
// The engine's spell-castability verdict for the LOCAL player — the
|
||||
// function behind `IsUsableAction` for a player spell slot (the spell
|
||||
// branch of the per-slot recompute `FUN_004E5050`) and behind the
|
||||
// spellbook grey-out recompute (`FUN_004B31C0`, which fires
|
||||
// SPELL_UPDATE_USABLE):
|
||||
// `char __fastcall(const uint8_t *spellRecord /*ecx*/,
|
||||
// int *outNoMana /*edx*/)`.
|
||||
// Resolves the player internally (no unit arg). Verified check order
|
||||
// (decompiled): Effect[0] == ATTACK → always usable; dead / ghost unless
|
||||
// CASTABLE_WHILE_DEAD (0x800000); the player-control flag
|
||||
// (`DAT_00B4B3E4`, toggled with PLAYER_CONTROL_LOST / _GAINED) — with
|
||||
// control lost only spells usable while fleeing / confused / charmed
|
||||
// pass; totems + reagents in bags (`FUN_006E4000`); equipped-item
|
||||
// class / subclass, main-hand / off-hand and ammo (`FUN_006E40E0`);
|
||||
// combo points for finishers (AttributesEx 0x500000); Stances /
|
||||
// StancesNot against the shapeshift byte (`FUN_00612480`, the
|
||||
// Battle-Stance-Whirlwind case); ONLY_STEALTHED (0x20000);
|
||||
// CANT_USED_IN_COMBAT (0x10000000); CasterAuraState; TargetAuraState
|
||||
// (+ can-attack / can-assist on the current target); DISABLED_WHILE_
|
||||
// ACTIVE (0x2000000) while the spell is active; and LAST
|
||||
// `cost(FUN_GET_SPELL_COST) <= currentPower` (PowerType -2 = health) —
|
||||
// the ONLY branch that sets `*outNoMana = 1`. Returns nonzero (low
|
||||
// byte) when usable. Does NOT check cooldown, spell knowledge, silence
|
||||
// or school lockouts — a knowledge gate belongs in the caller, and it's
|
||||
// valid to feed an item's on-use spell record (which the player never
|
||||
// "knows"). Read by `Spell::Usable` (IsUsableSpell) and `Item::Usable`
|
||||
// (item on-use spells). Returns 0 cleanly pre-world (no player).
|
||||
FUN_SPELL_IS_USABLE = 0x006E3D60,
|
||||
|
||||
// Effective spell/channel duration (ms):
|
||||
@@ -4725,6 +4738,23 @@ enum Offsets {
|
||||
// firing LEARNED_SPELL_IN_TAB. Read by `Spell::Learn`.
|
||||
VAR_PLAYER_SPELLBOOK_COUNT = 0x00B7116C,
|
||||
|
||||
// The player's pet GUID as a u64 (lo @ +0, hi @ +4). Verified three
|
||||
// ways: `Script_HasPetUI` (0x004BE670) resolves it with TYPEMASK_UNIT
|
||||
// through FUN_OBJECT_RESOLVE_BY_GUID, FUN_PET_ACTIONS_USABLE compares it
|
||||
// against the CGPlayer pet field (`[player+0xE68]+0x830`), and the pet
|
||||
// branch of the action-usability recompute `FUN_004E5050` resolves the
|
||||
// pet from it before reading the pet's power.
|
||||
VAR_PET_GUID = 0x00B714A0,
|
||||
// "The pet can act" — the core of `Script_GetPetActionsUsable`
|
||||
// (0x004BE0B0), `int()` with no args. True when the player isn't
|
||||
// charmed, the CGPlayer pet field matches VAR_PET_GUID, the pet
|
||||
// resolves, its charmer / summoner is the player, it's not stunned /
|
||||
// fleeing / confused, and the pet-state flag `[0x00B71468] & 0x8000000`
|
||||
// is clear. The pet branch of `FUN_004E5050` gates on it before the
|
||||
// pet power check; `Spell::Usable` mirrors that branch for pet-book
|
||||
// spells.
|
||||
FUN_PET_ACTIONS_USABLE = 0x004BCF70,
|
||||
|
||||
// Spellbook TABS — what `GetNumSpellTabs` / `GetSpellTabInfo` read.
|
||||
// `[VAR_SPELL_TAB_ENTRIES]` is a `SpellTab **` (heap array of pointers,
|
||||
// grown by `FUN_LEARN_SPELL` as new skill lines appear); `[VAR_SPELL_TAB_
|
||||
|
||||
+111
-153
@@ -1,4 +1,4 @@
|
||||
// This file is part of ClassicAPI.
|
||||
// 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
|
||||
@@ -11,11 +11,35 @@
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
// `IsUsableSpell(spell)` / `IsUsableSpell(slot, bookType)` and
|
||||
// `C_Spell.IsSpellUsable(spell)` — `(usable, noMana)` for a spell.
|
||||
//
|
||||
// The verdict is the engine's own. `IsUsableAction` reads a per-slot cache
|
||||
// that the recompute `FUN_004E5050` fills by calling `FUN_SPELL_IS_USABLE`
|
||||
// on the slot's spell record (player spells) or by comparing the pet's power
|
||||
// against the spell cost (pet spells). This module runs the same two paths
|
||||
// live, so `IsUsableSpell(id)` agrees with `IsUsableAction(slot)` for a slot
|
||||
// holding that spell — and is never stale, since it doesn't go through the
|
||||
// cache. Whatever the helper decides (stance / form, reagents, equipped
|
||||
// weapon and ammo, combo points, aura states, control loss, power) is what
|
||||
// we return; see the `FUN_SPELL_IS_USABLE` note in Offsets.h for the full
|
||||
// verified list. Like the engine, this does NOT fold in cooldown — that is a
|
||||
// separate concern (`GetSpellCooldown`), and FrameXML greys / swipes the two
|
||||
// independently.
|
||||
//
|
||||
// An earlier version hand-rolled five checks (known, alive, off cooldown,
|
||||
// power, reagents) instead of calling the helper. It diverged from
|
||||
// `IsUsableAction` exactly where the helper does more (stance) or less
|
||||
// (cooldown) — GitHub issue #51. Don't reintroduce a parallel check list.
|
||||
//
|
||||
// The one thing added on top of the helper is a knowledge gate: the helper
|
||||
// doesn't check whether the player knows the spell (it's also fed item
|
||||
// on-use spells), and the action bar only ever holds known spells, so a
|
||||
// spell the player hasn't learned reports `(nil, nil)` here.
|
||||
|
||||
#include "Game.h"
|
||||
#include "Offsets.h"
|
||||
#include "item/CGItem.h"
|
||||
#include "item/ID.h"
|
||||
#include "item/Location.h"
|
||||
#include "object/Resolve.h"
|
||||
#include "spell/Arg.h"
|
||||
#include "spell/Lookup.h"
|
||||
|
||||
@@ -25,17 +49,15 @@ namespace Spell::Usable {
|
||||
|
||||
namespace {
|
||||
|
||||
using SpellIsUsable_t = int(__fastcall *)(const uint8_t *spellRecord, int *outNoMana);
|
||||
using GetSpellCost_t = uint32_t(__fastcall *)(int spellID, int unit);
|
||||
|
||||
// Spell.dbc record field offsets we read for usability. Fully documented
|
||||
// in CLAUDE.md.
|
||||
using PetActionsUsable_t = int(__cdecl *)();
|
||||
|
||||
// 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.
|
||||
// abilities, talents, racials, profession recipes, and spells that are
|
||||
// not on any action bar.
|
||||
bool PlayerKnowsSpell(int spellID) {
|
||||
if (spellID <= 0)
|
||||
return false;
|
||||
@@ -50,158 +72,92 @@ bool PlayerKnowsSpell(int spellID) {
|
||||
return (bitmap[spellID >> 5] & (1u << (spellID & 31))) != 0;
|
||||
}
|
||||
|
||||
const uint8_t *PlayerDescriptor() {
|
||||
auto *player = static_cast<const uint8_t *>(Game::ResolveUnitToken("player"));
|
||||
if (player == nullptr)
|
||||
return nullptr;
|
||||
return Game::Read<const uint8_t *>(
|
||||
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, *duration, *start, *enable)` —
|
||||
// duration is the engine's raw cooldown length in milliseconds (the
|
||||
// Lua-side `Script_GetSpellCooldown` multiplies by 0.001 before
|
||||
// pushing), start is the absolute engine tick count when the
|
||||
// cooldown began. Both are 0 when no cooldown is active.
|
||||
using QueryCooldown_t = void(__fastcall *)(int spellID, int bookType,
|
||||
int *outDuration,
|
||||
int *outStart,
|
||||
int *outEnable);
|
||||
|
||||
bool IsOnCooldown(int spellID) {
|
||||
auto fn = reinterpret_cast<QueryCooldown_t>(Offsets::FUN_SPELL_QUERY_COOLDOWN);
|
||||
int duration = 0, start = 0, enable = 0;
|
||||
fn(spellID, 0 /* bookType=player */, &duration, &start, &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) {
|
||||
int total = 0;
|
||||
for (int bag = 0; bag <= 4; bag++) {
|
||||
const int slots = Item::Location::GetBagSlotCount(bag);
|
||||
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 = Item::ObjectFields(item);
|
||||
if (itemDesc == nullptr)
|
||||
continue;
|
||||
total += static_cast<int>(Game::Read<uint32_t>(
|
||||
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<int>(Game::Read<int32_t>(
|
||||
record, Offsets::OFF_SPELL_REAGENT_ID + i * 4));
|
||||
const int reagentCount = static_cast<int>(Game::Read<int32_t>(
|
||||
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 the spell's *effective* cost (talent
|
||||
// reductions applied) of its `PowerType`. Only this 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) {
|
||||
// Player spell — the spell branch of `FUN_004E5050`: the engine helper on
|
||||
// the record, `noMana` from its out-param.
|
||||
Usability ComputePlayer(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 = Game::Read<int>(
|
||||
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. Use the engine's
|
||||
// effective-cost helper (op-14 SpellMod + descriptor power-cost mods +
|
||||
// ManaCostPercent), so talent reductions like Frost Channeling count —
|
||||
// not just the base ManaCost. Falls back to base if the engine can't
|
||||
// resolve a cost (shouldn't happen here: the player descriptor
|
||||
// resolved above, so it has a player context).
|
||||
const int powerType = static_cast<int>(Game::Read<int8_t>(
|
||||
record, Offsets::OFF_SPELL_RECORD_POWER_TYPE));
|
||||
if (powerType >= 0 && powerType <= 4) {
|
||||
auto getCost = reinterpret_cast<GetSpellCost_t>(Offsets::FUN_GET_SPELL_COST);
|
||||
uint32_t cost = getCost(spellID, 0 /* local player */);
|
||||
if (cost == 0xFFFFFFFF)
|
||||
cost = Game::Read<uint32_t>(record, Offsets::OFF_SPELL_RECORD_MANA_COST);
|
||||
if (cost > 0) {
|
||||
const int currentPower = Game::Read<int>(
|
||||
desc, Offsets::OFF_UNIT_FIELD_POWER1 + powerType * 4);
|
||||
if (currentPower < static_cast<int>(cost)) {
|
||||
r.noMana = true;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!HasReagents(L, record))
|
||||
return r;
|
||||
|
||||
r.usable = true;
|
||||
auto isUsable = reinterpret_cast<SpellIsUsable_t>(Offsets::FUN_SPELL_IS_USABLE);
|
||||
int noMana = 0;
|
||||
r.usable = (isUsable(record, &noMana) & 0xFF) != 0;
|
||||
r.noMana = noMana != 0;
|
||||
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) {
|
||||
// Pet spell — the pet branch of `FUN_004E5050`, mirrored step for step:
|
||||
// the pet must be able to act (`FUN_PET_ACTIONS_USABLE`), then the pet's
|
||||
// current power of the spell's PowerType (-2 = health) is compared against
|
||||
// the cost from `FUN_GET_SPELL_COST(spellID, 0)` — the engine passes unit
|
||||
// 0 here too. `cost <= power` is a signed compare, as in the engine.
|
||||
Usability ComputePet(int spellID) {
|
||||
Usability r{false, false};
|
||||
auto *record = Spell::Lookup::RecordForID(spellID);
|
||||
if (record == nullptr)
|
||||
return r;
|
||||
|
||||
auto petCanAct = reinterpret_cast<PetActionsUsable_t>(Offsets::FUN_PET_ACTIONS_USABLE);
|
||||
if (petCanAct() == 0)
|
||||
return r;
|
||||
|
||||
const uint64_t petGuid = Game::Read<uint64_t>(
|
||||
static_cast<uintptr_t>(Offsets::VAR_PET_GUID));
|
||||
auto *pet = static_cast<const uint8_t *>(
|
||||
Object::ByGuid(Offsets::TYPEMASK_UNIT, petGuid));
|
||||
if (pet == nullptr)
|
||||
return r;
|
||||
auto *desc = Game::Read<const uint8_t *>(pet, Offsets::OFF_UNIT_DESCRIPTOR);
|
||||
if (desc == nullptr)
|
||||
return r;
|
||||
|
||||
const int powerType = Game::Read<int>(record, Offsets::OFF_SPELL_RECORD_POWER_TYPE);
|
||||
int power;
|
||||
if (powerType == -2) {
|
||||
power = Game::Read<int>(desc, Offsets::OFF_UNIT_FIELD_HEALTH);
|
||||
} else if (powerType >= 0 && powerType <= 4) {
|
||||
power = Game::Read<int>(desc, Offsets::OFF_UNIT_FIELD_POWER1 + powerType * 4);
|
||||
} else {
|
||||
return r; // not a power type Spell.dbc can hold; the engine would index garbage
|
||||
}
|
||||
|
||||
auto getCost = reinterpret_cast<GetSpellCost_t>(Offsets::FUN_GET_SPELL_COST);
|
||||
const int cost = static_cast<int>(getCost(spellID, 0));
|
||||
if (cost <= power) {
|
||||
r.usable = true;
|
||||
return r;
|
||||
}
|
||||
r.noMana = true;
|
||||
return r;
|
||||
}
|
||||
|
||||
// `bookType` 1 = pet book (the `(slot, "pet")` arg shape). A spell given
|
||||
// by ID takes the player path when the player knows it; otherwise, if it
|
||||
// sits in the pet book (Growl, Cower, …), the pet path.
|
||||
Usability Compute(int spellID, int bookType) {
|
||||
if (bookType == 1)
|
||||
return ComputePet(spellID);
|
||||
if (PlayerKnowsSpell(spellID))
|
||||
return ComputePlayer(spellID);
|
||||
int foundBook = 0;
|
||||
if (Spell::Lookup::FindSpellbookSlot(spellID, &foundBook) != 0 && foundBook == 1)
|
||||
return ComputePet(spellID);
|
||||
return Usability{false, false};
|
||||
}
|
||||
|
||||
// `IsUsableSpell(spellID)` or `IsUsableSpell(slot, bookType)`. Writes the
|
||||
// engine bookType (0 player, 1 pet) to `*outBookType`; returns the spellID
|
||||
// (0 when the slot is empty / out of range).
|
||||
int ResolveSpellArg(void *L, int *outBookType) {
|
||||
*outBookType = 0;
|
||||
if (!Game::Lua::IsNumber(L, 1))
|
||||
return 0;
|
||||
const int arg1 = static_cast<int>(Game::Lua::ToNumber(L, 1));
|
||||
@@ -213,6 +169,7 @@ int ResolveSpellArg(void *L) {
|
||||
(book[1] == 'e' || book[1] == 'E') &&
|
||||
(book[2] == 't' || book[2] == 'T') &&
|
||||
book[3] == 0);
|
||||
*outBookType = bookType;
|
||||
return Spell::Lookup::SpellbookSlotToID(arg1, bookType);
|
||||
}
|
||||
return arg1;
|
||||
@@ -226,8 +183,9 @@ int __fastcall Script_IsUsableSpell(void *L) {
|
||||
Game::Lua::Error(L, "Usage: IsUsableSpell(spellID) or IsUsableSpell(slot, bookType)");
|
||||
return 0;
|
||||
}
|
||||
const int spellID = ResolveSpellArg(L);
|
||||
Usability r = ComputeUsability(L, spellID);
|
||||
int bookType = 0;
|
||||
const int spellID = ResolveSpellArg(L, &bookType);
|
||||
const Usability r = Compute(spellID, bookType);
|
||||
if (r.usable) {
|
||||
Game::Lua::PushNumber(L, 1.0);
|
||||
Game::Lua::PushNil(L);
|
||||
@@ -241,12 +199,12 @@ int __fastcall Script_IsUsableSpell(void *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.
|
||||
// `C_Spell.IsSpellUsable(spell)` — modern table-namespace form. Accepts
|
||||
// a spellID, link, or name. Returns proper booleans (`isUsable`,
|
||||
// `insufficientPower`) per the `C_Spell.*` convention, not 1/nil pairs.
|
||||
int __fastcall Script_C_Spell_IsSpellUsable(void *L) {
|
||||
const int spellID = Spell::Arg::ResolveSpellID(L, 1);
|
||||
Usability r = ComputeUsability(L, spellID);
|
||||
const Usability r = Compute(spellID, 0);
|
||||
Game::Lua::PushBool(L, r.usable);
|
||||
Game::Lua::PushBool(L, r.noMana);
|
||||
return 2;
|
||||
|
||||
Reference in New Issue
Block a user