Files
ClassicAPI/src/spell/Lookup.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

101 lines
3.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.
//
// You should have received a copy of the GNU Lesser General Public License along with
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
#include "Lookup.h"
#include "Offsets.h"
#include "dbc/Lookup.h"
namespace Spell::Lookup {
const uint8_t *RecordForID(int spellID) {
if (spellID <= 0)
return nullptr;
return DBC::Record(Offsets::VAR_SPELL_RECORDS,
Offsets::VAR_SPELL_RECORD_COUNT,
static_cast<uint32_t>(spellID));
}
int SpellbookSlotToID(int slot1Based, int bookType) {
const int slot = slot1Based - 1;
if (slot < 0 || slot >= Offsets::SPELLBOOK_MAX_SLOTS)
return 0;
const auto base = (bookType == 1) ? Offsets::VAR_PET_SPELLBOOK
: Offsets::VAR_PLAYER_SPELLBOOK;
auto *array = reinterpret_cast<const int *>(static_cast<uintptr_t>(base));
return array[slot];
}
int RecipeSlotSpellID(uintptr_t entriesVar, uintptr_t countVar, int slotIndex0) {
if (slotIndex0 < 0)
return 0;
const int count = static_cast<int>(
*reinterpret_cast<const uint32_t *>(countVar));
if (slotIndex0 >= count)
return 0;
auto **entries = *reinterpret_cast<const uint8_t ***>(entriesVar);
if (entries == nullptr)
return 0;
auto *entry = entries[slotIndex0];
if (entry == nullptr)
return 0;
return static_cast<int>(*reinterpret_cast<const uint32_t *>(
entry + Offsets::OFF_CRAFT_ENTRY_SPELL_ID));
}
int NthRecipeReagentItemID(const uint8_t *spellRecord, int reagentIndex1) {
if (spellRecord == nullptr || reagentIndex1 < 1)
return 0;
auto *reagents = reinterpret_cast<const uint32_t *>(
spellRecord + Offsets::OFF_SPELL_REAGENT_ID);
int found = 0;
for (int i = 0; i < Offsets::SPELL_RECIPE_MAX_REAGENTS; ++i) {
const int itemID = static_cast<int>(reagents[i]);
if (itemID == 0)
return 0;
if (++found == reagentIndex1)
return itemID;
}
return 0;
}
int FindSpellbookSlot(int spellID, int *outBookType) {
if (spellID <= 0)
return 0;
// Walk the full SPELLBOOK_MAX_SLOTS range rather than break-on-zero
// — the BSS arrays are zero-padded past the populated count, so any
// 0 entry is "empty slot" and won't match a positive spellID. Cost
// is 1024 dword reads per book = 8KB of cache-friendly memory.
auto *playerArray = reinterpret_cast<const int *>(
static_cast<uintptr_t>(Offsets::VAR_PLAYER_SPELLBOOK));
for (int i = 0; i < Offsets::SPELLBOOK_MAX_SLOTS; i++) {
if (playerArray[i] == spellID) {
if (outBookType != nullptr)
*outBookType = 0;
return i + 1;
}
}
auto *petArray = reinterpret_cast<const int *>(
static_cast<uintptr_t>(Offsets::VAR_PET_SPELLBOOK));
for (int i = 0; i < Offsets::SPELLBOOK_MAX_SLOTS; i++) {
if (petArray[i] == spellID) {
if (outBookType != nullptr)
*outBookType = 1;
return i + 1;
}
}
return 0;
}
} // namespace Spell::Lookup