Files
ClassicAPI/src/spell/Reagents.cpp
T
Brues 24b0153287 C_Spell.GetSpellReagents + fix Spell.dbc reagent offsets
Returns the spell's reagent list as an array of {itemID, count}
tables, one entry per non-empty reagent slot. Reads directly from
Spell.dbc; stops at the first empty slot, matching the engine's
own tooltip reagent loop.

Discovered while implementing that the reagent offsets we had in
Offsets.h were wrong:

  Old: OFF_SPELL_REAGENT_ID = 0x110, OFF_SPELL_REAGENT_COUNT = 0x130
  New: OFF_SPELL_REAGENT_ID = 0xA8,  OFF_SPELL_REAGENT_COUNT = 0xC8

The old values came from a CMaNGOS-style SpellEntry schema that
doesn't match 1.12.1's actual record layout for reagent fields.
Verified empirically by decompiling BuildSpellTooltip (0x0052E610) —
its reagent loop iterates `localRec + 0x2A` (dword arithmetic =
byte +0xA8) as itemIDs and indexes [+8] (= byte +0xC8) for counts.
We already had a separate OFF_SPELL_RECORD_REAGENTS = 0xA8 entry
used by the tradeskill/craft scrapers (correct value), so it was
silently inconsistent — they were reading the same field by two
different names, one of which was wrong.

Fixed latent IsUsableSpell bug as a side effect: the reagent gate
was reading zeros from +0x110 / +0x130 and concluding "no reagents
required" for every spell. Now correctly returns (false, false) for
spells whose reagent the player doesn't have in their bags.
Verified: GetSpellReagents(23028) = {{itemID=17020, count=1}};
IsUsableSpell(23028)=1 with Arcane Powder in inventory.

Consolidated: removed the now-duplicate OFF_SPELL_RECORD_REAGENTS
alias; tradeskill scraper points at the canonical OFF_SPELL_REAGENT_ID.
2026-05-21 16:12:13 -05:00

73 lines
2.6 KiB
C++

// 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.
// `C_Spell.GetSpellReagents(spellIdentifier)` — modern accessor for
// a spell's reagent list. Returns an array of `{itemID, count}`
// tables, one per non-empty reagent slot. Reagents apply mostly to
// crafting recipes and a few class spells (Soulshatter / Soulstone
// items, paladin Symbol of Kings, etc.).
//
// Reads directly out of `Spell.dbc` at the documented offsets:
// - `+0x110`: int32 itemID[8]
// - `+0x130`: int32 count[8]
// Empty slots have itemID == 0; iteration stops at the first empty
// slot, matching how the engine itself walks the reagent table
// (`Script_GetTradeSkillReagentInfo` etc.).
#include "Arg.h"
#include "Lookup.h"
#include "Game.h"
#include "Offsets.h"
#include <cstdint>
namespace Spell::Reagents {
namespace {
int __fastcall Script_C_Spell_GetSpellReagents(void *L) {
const int spellID = Spell::Arg::ResolveSpellID(L, 1);
if (spellID <= 0)
return 0;
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
if (record == nullptr)
return 0;
auto *ids = reinterpret_cast<const int32_t *>(
record + Offsets::OFF_SPELL_REAGENT_ID);
auto *counts = reinterpret_cast<const int32_t *>(
record + Offsets::OFF_SPELL_REAGENT_COUNT);
Game::Lua::NewTable(L); // outer array
int nextKey = 1;
for (int i = 0; i < Offsets::SPELL_RECIPE_MAX_REAGENTS; ++i) {
if (ids[i] == 0)
break; // engine convention: stop at first empty slot
Game::Lua::PushNumber(L, static_cast<double>(nextKey++));
Game::Lua::NewTable(L); // {itemID = ?, count = ?}
Game::Lua::SetFieldNumber(L, "itemID", static_cast<double>(ids[i]));
Game::Lua::SetFieldNumber(L, "count", static_cast<double>(counts[i]));
Game::Lua::SetTable(L, -3);
}
return 1;
}
} // namespace
static void RegisterLuaFunctions() {
Game::Lua::RegisterTableFunction("C_Spell", "GetSpellReagents",
&Script_C_Spell_GetSpellReagents);
}
static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace Spell::Reagents