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).
This commit is contained in:
Brues
2026-10-25 02:30:14 -05:00
parent 443c78c453
commit 3e83827ae9
6 changed files with 86 additions and 40 deletions
+5 -4
View File
@@ -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
+22
View File
@@ -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
+4 -2
View File
@@ -12,8 +12,10 @@
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
// `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
+4 -2
View File
@@ -166,8 +166,10 @@ static bool ReadSpellInfo(int spellID, int pet, SpellInfoData &out) {
const int rangeIndex = Game::Read<int>(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<const float *>(rangeRec + 4);
out.maxRange = *reinterpret_cast<const float *>(rangeRec + 8);
out.minRange =
*reinterpret_cast<const float *>(rangeRec + Offsets::OFF_SPELLRANGE_RECORD_MIN);
out.maxRange =
*reinterpret_cast<const float *>(rangeRec + Offsets::OFF_SPELLRANGE_RECORD_MAX);
}
return true;
+44 -29
View File
@@ -15,60 +15,75 @@
#include "Game.h"
#include "Offsets.h"
#include "dbc/Lookup.h"
#include "unit/Position.h"
#include <cmath>
#include <cstdint>
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<GetMinMaxRange_t>(
Offsets::FUN_GET_MIN_MAX_RANGE);
getMinMax(caster, static_cast<uint32_t>(spellID), &minRange, &maxRange,
target);
const float epsilon = Game::Read<float>(
static_cast<uintptr_t>(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<uint32_t>(spellID));
if (rec == nullptr)
return false;
const uint32_t rangeIdx =
Game::Read<uint32_t>(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<const float *>(rangeRec + OFF_SPELLRANGE_MIN);
const float maxRange =
*reinterpret_cast<const float *>(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) ||
+7 -3
View File
@@ -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