From 3e83827ae9647b1f58219bedda4e653e2df0be64 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:29:05 -0500 Subject: [PATCH] fix(spell): Route spell has-range gate through the engine's GetMinMaxRange SpellIDHasRange now calls 0x6E3480 and tests both bounds against the ActionHasRange epsilon instead of reading raw SpellRange.dbc values, covering Attributes & 0x404 spells, melee reach folding, and the stationary/item/modifier bonuses. Centralizes the SpellRange record min/max offsets in Offsets.h (used by GetSpellInfo). Updates API.md (SpellHasRange mechanism, Fireball 35yd). --- docs/API.md | 9 +++--- src/Offsets.h | 22 +++++++++++++ src/spell/HasRange.cpp | 6 ++-- src/spell/Info.cpp | 6 ++-- src/spell/Range.cpp | 73 +++++++++++++++++++++++++----------------- src/spell/Range.h | 10 ++++-- 6 files changed, 86 insertions(+), 40 deletions(-) diff --git a/docs/API.md b/docs/API.md index 6d03e45c..8f7a6259 100644 --- a/docs/API.md +++ b/docs/API.md @@ -17536,14 +17536,15 @@ it's a targeted ability you cast at a unit / location rather than a self-cast (which has range `0`). ```lua -C_Spell.SpellHasRange(133) -- Fireball → true (40yd) +C_Spell.SpellHasRange(133) -- Fireball → true (35yd) C_Spell.SpellHasRange(1006) -- Inner Fire → false (self buff) SpellHasRange(10, "spell") -- 10th spellbook slot in player book ``` -Looks up `Spell.dbc.RangeIndex` (`+0x90`) → `SpellRange.dbc` row, -then tests `minRange > 0 or maxRange > 0`. The stock client doesn't ship -this function. We expose both the namespaced form (any +It tells you whether a spell has a usable range. It uses the same +range that the game uses when it checks distance, so bonuses that +change range also change this answer. Spells that you cast on +yourself return false. We expose both the namespaced form (any `SpellIdentifier`) and a positional `SpellHasRange(slot, bookType)` matching the dual-signature shape used elsewhere in this backport (`GetSpellInfo`, `GetSpellLink`, etc.). The `bookType` argument diff --git a/src/Offsets.h b/src/Offsets.h index 697415f9..03b169b1 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -3008,6 +3008,26 @@ enum Offsets { // nil cleanly). FUN_SPELL_RANGE_CHECK = 0x006E47B0, + // Spell min/max range resolver: `void __fastcall(void *caster /*ecx*/, + // uint32_t spellID /*edx*/, float *minRange, float *maxRange, + // void *target)`. Zeroes both and returns for a null caster or an + // unknown spell; forces `max = 100.0` (`DAT_008118d4`) for + // `Attributes & 0x404` without reading any range row; otherwise folds + // the `SpellRange.dbc` row with both units' combat reach, the + // stationary +8/3 bonus, the slot-17 ranged-item scale, and the op-5 + // spell modifier. Null-safe on the caster (unlike + // `FUN_SPELL_RANGE_CHECK`, which needs both positions readable). This + // is what `FUN_SPELL_RANGE_CHECK` calls for its window, and what + // `ActionHasRange` (`0x4E75D0`) gates on. + FUN_GET_MIN_MAX_RANGE = 0x006E3480, + + // Near-zero range epsilon `ActionHasRange` (`0x4E75D0`) compares + // against: `|min|`/`|max|` below this counts as rangeless, while a NaN + // counts as ranged (`fcomp` then `test ah,5; jp` at `0x4E5885` + // passes unordered). Verified by a direct float dump of the image: + // exactly 2^-22 (0x34800000) at this address. Read live, not hardcoded. + VAR_HAS_RANGE_EPSILON = 0x008029D4, + // The two floats `FUN_006e3480`'s COMBAT-RANGE branch folds into its // max-range output: `casterReach + targetReach + LEEWAY`, floored at // MIN. Verified by a direct float dump of the image (1.33333 / 5.0); @@ -7166,6 +7186,8 @@ enum Offsets { VAR_SPELL_RANGE_RECORDS = 0x00C0D79C, // SpellRange.dbc VAR_SPELL_RANGE_COUNT = 0x00C0D7A0, + OFF_SPELLRANGE_RECORD_MIN = 0x04, // float min range (column 1) + OFF_SPELLRANGE_RECORD_MAX = 0x08, // float max range (column 2) VAR_SPELL_ICON_RECORDS = 0x00C0D7EC, // SpellIcon.dbc VAR_SPELL_ICON_COUNT = 0x00C0D7F0, VAR_SPELL_CAST_TIMES_RECORDS = 0x00C0D878, // SpellCastTimes.dbc diff --git a/src/spell/HasRange.cpp b/src/spell/HasRange.cpp index ebe44fea..6147a780 100644 --- a/src/spell/HasRange.cpp +++ b/src/spell/HasRange.cpp @@ -12,8 +12,10 @@ // ClassicAPI. If not, see . // `C_Spell.SpellHasRange(spellIdentifier)` and the parallel -// `SpellHasRange(slot, bookType)` — true iff the spell's -// `SpellRange.dbc` row has a non-zero min or max range. +// `SpellHasRange(slot, bookType)` — true iff the engine's own range +// resolver (`FUN_GET_MIN_MAX_RANGE`) reports a non-near-zero min or max +// for the spell, mirroring vanilla `ActionHasRange` (see +// `Spell::Range::SpellIDHasRange`). // // Vanilla 1.12 doesn't ship `SpellHasRange` as a global at all; 3.3.5+ // added it but with the spellID-only signature. We provide the modern diff --git a/src/spell/Info.cpp b/src/spell/Info.cpp index 67354d98..99f5a0fa 100644 --- a/src/spell/Info.cpp +++ b/src/spell/Info.cpp @@ -166,8 +166,10 @@ static bool ReadSpellInfo(int spellID, int pet, SpellInfoData &out) { const int rangeIndex = Game::Read(record, Offsets::OFF_SPELL_RECORD_RANGE_INDEX); if (auto *rangeRec = LookupSubRecord(Offsets::VAR_SPELL_RANGE_RECORDS, Offsets::VAR_SPELL_RANGE_COUNT, rangeIndex)) { - out.minRange = *reinterpret_cast(rangeRec + 4); - out.maxRange = *reinterpret_cast(rangeRec + 8); + out.minRange = + *reinterpret_cast(rangeRec + Offsets::OFF_SPELLRANGE_RECORD_MIN); + out.maxRange = + *reinterpret_cast(rangeRec + Offsets::OFF_SPELLRANGE_RECORD_MAX); } return true; diff --git a/src/spell/Range.cpp b/src/spell/Range.cpp index 465b2166..9d96a624 100644 --- a/src/spell/Range.cpp +++ b/src/spell/Range.cpp @@ -15,60 +15,75 @@ #include "Game.h" #include "Offsets.h" -#include "dbc/Lookup.h" #include "unit/Position.h" +#include +#include + namespace Spell::Range { namespace { -// Spell.dbc RangeIndex + SpellRange.dbc min/max offsets (verified, -// documented in CLAUDE.md under "Sub-DBC record layouts"). -constexpr int OFF_SPELLRANGE_MIN = 0x04; -constexpr int OFF_SPELLRANGE_MAX = 0x08; - // FUN_SPELL_RANGE_CHECK: char __fastcall(caster, target, spellID, outFlag). using SpellRangeCheck_t = char(__fastcall *)(void *caster, void *target, int spellID, char *outFlag); +// FUN_GET_MIN_MAX_RANGE: void __fastcall(caster, spellID, min, max, target). +using GetMinMaxRange_t = void(__fastcall *)(void *caster, uint32_t spellID, + float *minRange, float *maxRange, + void *target); + +// The engine's own has-range notion, mirroring `ActionHasRange` +// (`0x4E75D0`): either bound at or above the near-zero epsilon counts as +// ranged. The negated `<` (rather than `>=`) reproduces the engine's +// `fcomp` + `test ah,5; jp` exactly — a NaN bound counts as ranged, +// matching vanilla. Unlike the raw `SpellRange.dbc` read this replaces, +// the engine call also covers `Attributes & 0x404` spells (flat 100-yard +// max with no range row), melee rows whose max folds combat reach, and +// the stationary / ranged-item / spell-mod bonuses. +bool EngineHasRange(void *caster, int spellID, void *target) { + float minRange = 0.0f; + float maxRange = 0.0f; + auto getMinMax = reinterpret_cast( + Offsets::FUN_GET_MIN_MAX_RANGE); + getMinMax(caster, static_cast(spellID), &minRange, &maxRange, + target); + const float epsilon = Game::Read( + static_cast(Offsets::VAR_HAS_RANGE_EPSILON)); + return !(std::fabs(minRange) < epsilon) || + !(std::fabs(maxRange) < epsilon); +} + } // namespace bool SpellIDHasRange(int spellID) { if (spellID <= 0) return false; - const uint8_t *rec = DBC::Record(Offsets::VAR_SPELL_RECORDS, - Offsets::VAR_SPELL_RECORD_COUNT, - static_cast(spellID)); - if (rec == nullptr) - return false; - const uint32_t rangeIdx = - Game::Read(rec, Offsets::OFF_SPELL_RECORD_RANGE_INDEX); - if (rangeIdx == 0) - return false; - const uint8_t *rangeRec = DBC::Record(Offsets::VAR_SPELL_RANGE_RECORDS, - Offsets::VAR_SPELL_RANGE_COUNT, rangeIdx); - if (rangeRec == nullptr) - return false; - const float minRange = - *reinterpret_cast(rangeRec + OFF_SPELLRANGE_MIN); - const float maxRange = - *reinterpret_cast(rangeRec + OFF_SPELLRANGE_MAX); - return minRange > 0.0f || maxRange > 0.0f; + // Null target: the engine falls back to the caster's current-target + // GUID (`[caster+0xC48]`), else the caster itself, for the reach fold. + // A null caster zeroes both bounds, which reads as rangeless. + return EngineHasRange(Unit::Position::ResolveToken("player"), spellID, + nullptr); } int PlayerVsUnit(int spellID, const char *unitToken) { - // No answer when the range check can't apply: unknown/rangeless spell, - // or no unit token given. - if (spellID <= 0 || unitToken == nullptr || !SpellIDHasRange(spellID)) + // No answer when the range check can't apply: unknown spell or no + // unit token given. + if (spellID <= 0 || unitToken == nullptr) return -1; void *caster = Unit::Position::ResolveToken("player"); void *target = Unit::Position::ResolveToken(unitToken); + if (caster == nullptr || target == nullptr) + return -1; + // Rangeless spell (self buff, etc.) has no meaningful "in range" answer. + if (!EngineHasRange(caster, spellID, target)) + return -1; // Guard positions before handing the objects to the engine core: // FUN_SPELL_RANGE_CHECK dereferences each GetPosition result without a - // null check, and a resolved-but-position-less object (or an absent - // token → null) would otherwise crash. A failed read → no answer. + // null check, and a resolved-but-position-less object would otherwise + // crash. A failed read → no answer. float casterPos[3]; float targetPos[3]; if (!Unit::Position::Read(caster, casterPos) || diff --git a/src/spell/Range.h b/src/spell/Range.h index 9c7534d6..2ae0760f 100644 --- a/src/spell/Range.h +++ b/src/spell/Range.h @@ -21,9 +21,13 @@ // on-use spell) `C_Item.IsItemInRange`. namespace Spell::Range { -// True iff the spell's `SpellRange.dbc` row has a non-zero min or max -// range. Rangeless spells (self buffs, etc.) have no meaningful "in -// range" answer. +// True iff the engine's own range resolver (`FUN_GET_MIN_MAX_RANGE`) +// reports a non-near-zero min or max for the spell — the same notion +// vanilla's `ActionHasRange` uses. Covers what a raw `SpellRange.dbc` +// read misses: `Attributes & 0x404` spells (flat 100-yard max), melee +// rows whose max folds combat reach, and the stationary / ranged-item / +// spell-mod bonuses. Rangeless spells (self buffs, etc.) have no +// meaningful "in range" answer. bool SpellIDHasRange(int spellID); // Range check of the local player's cast of `spellID` against