mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 16:10:36 +00:00
Add C_Spell.GetSpellMechanicByID(spellID) -> (mechanicID, name)
Reads the spell-level Mechanic field straight out of Spell.dbc (record +0x14) and resolves the name from SpellMechanic.dbc. Covers every spell the client knows (not just the player's spellbook), with no caching or network round-trip since the DBCs are resident from boot. Replaces hand-maintained spellID -> mechanic tables addons keep because vanilla exposes no Lua reader for the field (e.g. SuperCleveRoidMacros' CCSpellMechanics, used for [cc:stun] conditionals). Returns: nil for an invalid spellID; (0, nil) for a known spell with no mechanic; (mechanicID, name) otherwise. The name is always the enUS column (locale-independent) since it's a stable token consumers string-match against; nil if the mechanic has no record / no English name (numeric ID still returned). Mechanic offset (+0x14) verified two ways: schema anchor off the verified Attributes@+0x18, and FUN_006e9ca0 (mechanic-immunity check) reading spellRec[+0x14] as the spell's mechanic. SpellMechanic.dbc name at record +0x04 confirmed in-game: 118 -> (17,"polymorphed"), 339 -> (7,"rooted"), 133 -> (0,nil). The .dbc has 27 rows; only the enUS name column is populated.
This commit is contained in:
@@ -2381,6 +2381,18 @@ enum Offsets {
|
||||
VAR_SPELL_RECORDS = 0x00C0D788, // SpellRecord *records[spellID]
|
||||
VAR_SPELL_RECORD_COUNT = 0x00C0D78C, // max spellID
|
||||
|
||||
// SpellMechanic.dbc — maps a SpellMechanic ID to its localized
|
||||
// name. Standard 5-DWORD DBC instance at 0x00C0D7BC; records ptr at
|
||||
// +0x08, count at +0x0C (see docs/DBCs.md). Records are 1-based
|
||||
// (records[mechanicID]); the engine only ever uses the table for
|
||||
// existence/bounds checks (FUN_006e9ca0, FUN_00612df0, FUN_006e8eb0
|
||||
// case 0x8d all do `records[id] != 0`), never reading the name — so
|
||||
// the name offset below follows the canonical 2-column
|
||||
// (ID + localized Name) DBC schema rather than an engine-reader anchor.
|
||||
VAR_SPELLMECHANIC_RECORDS = 0x00C0D7C4, // SpellMechanicRecord *records[mechanicID]
|
||||
VAR_SPELLMECHANIC_COUNT = 0x00C0D7C8, // max mechanic ID
|
||||
OFF_SPELLMECHANIC_NAME = 0x04, // char *name[9], locale-indexed
|
||||
|
||||
// Spell.dbc School field — 0-based integer at record +0x04.
|
||||
// Verified empirically against Fireball (133) → School=2 (Fire)
|
||||
// and Frostbolt (116) → School=4 (Frost) on Octo 1.12.1. Vanilla
|
||||
@@ -3710,6 +3722,15 @@ enum Offsets {
|
||||
OFF_SPELL_RECORD_EFFECT_APPLY_AURA_NAME = 0x16C, // int32[3]
|
||||
OFF_SPELL_RECORD_EFFECT_MISC_VALUE = 0x1A8, // int32[3]
|
||||
SPELL_RECORD_EFFECT_COUNT = 3,
|
||||
|
||||
// Spell.dbc Mechanic field — the spell-level SpellMechanic ID (→
|
||||
// SpellMechanic.dbc), 0 = none. Field 5 of the record
|
||||
// (ID, School, Category, castUI, Dispel, Mechanic), which lands at
|
||||
// +0x14 given Attributes (field 6) is verified at +0x18. Cross-
|
||||
// confirmed by FUN_006e9ca0 (the mechanic-immunity check), which
|
||||
// reads `spellRec[+0x14]` as the spell's mechanic when the per-
|
||||
// effect EffectMiscValue is 0. Read by C_Spell.GetSpellMechanicByID.
|
||||
OFF_SPELL_RECORD_MECHANIC = 0x14,
|
||||
SPELL_AURA_MOD_SHAPESHIFT = 36,
|
||||
SPELL_AURA_MOUNTED = 78,
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
// 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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
// `C_Spell.GetSpellMechanicByID(spellID)` -> (mechanicID, localizedName)
|
||||
//
|
||||
// Reads the spell-level Mechanic field straight out of Spell.dbc, so it
|
||||
// covers *every* spell the client knows — not just the player's
|
||||
// spellbook — with no caching or network round-trip (Spell.dbc is fully
|
||||
// resident from boot). This replaces hand-maintained spellID -> mechanic
|
||||
// tables (e.g. SuperCleveRoidMacros' `CCSpellMechanics`) that addons keep
|
||||
// solely because vanilla exposes no Lua reader for the field.
|
||||
//
|
||||
// Returns:
|
||||
// - nothing (nil) for an invalid / out-of-range spell ID
|
||||
// - (0, nil) for a known spell that carries no mechanic
|
||||
// - (mechanicID, name) otherwise. `name` is the enUS SpellMechanic.dbc
|
||||
// name (always English, locale-independent — it's a stable token
|
||||
// consumers match against). nil only if the mechanic has no record /
|
||||
// no English name; the numeric ID is still returned in that case.
|
||||
//
|
||||
// The mechanic numbering is WoW's standard SpellMechanic enum
|
||||
// (1 = Charm, 5 = Fear, 7 = Root, 12 = Stun, 17 = Polymorph, ...);
|
||||
// addons map those IDs to their own names as needed.
|
||||
|
||||
#include "Game.h"
|
||||
#include "Offsets.h"
|
||||
#include "dbc/Lookup.h"
|
||||
#include "spell/Arg.h"
|
||||
#include "spell/Lookup.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace Spell::Mechanic {
|
||||
|
||||
// SpellMechanic.dbc name — always the enUS (index 0) column, regardless
|
||||
// of client locale. The mechanic name is a stable token consumers key
|
||||
// off (`"rooted"`, `"stunned"`, …), so it must be identical on every
|
||||
// client; a localized value would break cross-locale string matching.
|
||||
// Vanilla only populates the enUS column of this table anyway. Returns
|
||||
// nullptr if the record is missing or the English slot is empty.
|
||||
static const char *MechanicName(uint32_t mechanicID) {
|
||||
const uint8_t *rec = DBC::Record(Offsets::VAR_SPELLMECHANIC_RECORDS,
|
||||
Offsets::VAR_SPELLMECHANIC_COUNT, mechanicID);
|
||||
if (rec == nullptr)
|
||||
return nullptr;
|
||||
auto names = reinterpret_cast<const char *const *>(rec + Offsets::OFF_SPELLMECHANIC_NAME);
|
||||
const char *s = names[0]; // enUS
|
||||
return (s != nullptr && s[0] != '\0') ? s : nullptr;
|
||||
}
|
||||
|
||||
static int __fastcall Script_GetSpellMechanicByID(void *L) {
|
||||
const int spellID = Spell::Arg::ResolveSpellID(L, 1);
|
||||
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
|
||||
if (record == nullptr)
|
||||
return 0; // nil for invalid / out-of-range spell IDs
|
||||
|
||||
const uint32_t mechanicID =
|
||||
*reinterpret_cast<const uint32_t *>(record + Offsets::OFF_SPELL_RECORD_MECHANIC);
|
||||
|
||||
Game::Lua::PushNumber(L, static_cast<double>(mechanicID));
|
||||
|
||||
if (mechanicID == 0) {
|
||||
Game::Lua::PushNil(L); // known spell, no mechanic
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Locale name with enUS fallback (nullptr → nil if even English is
|
||||
// empty); the caller still gets the numeric ID either way.
|
||||
Game::Lua::PushString(L, MechanicName(mechanicID)); // pushstring(NULL) → pushnil
|
||||
return 2;
|
||||
}
|
||||
|
||||
static void RegisterLuaFunctions() {
|
||||
Game::Lua::RegisterTableFunction("C_Spell", "GetSpellMechanicByID",
|
||||
&Script_GetSpellMechanicByID);
|
||||
}
|
||||
|
||||
static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
|
||||
|
||||
} // namespace Spell::Mechanic
|
||||
Reference in New Issue
Block a user