Files
ClassicAPI/src/spell/AutoAttack.cpp
T
Brues a51225cb84 ranged auto-attack: detect via SPELL_ATTR_AUTO_REPEAT bit
Drop the hardcoded {75, 5019} ID list in favor of testing
Spell.dbc.Attributes bit 1. Auto Shot (Attr=0x00050012) and Shoot
wand (Attr=0x00000012) share that bit; on-next-swing Heroic Strike
and other ranged spells don't. Future-proofs against private
servers adding new auto-repeating spells.
2026-05-26 00:34:11 -05:00

122 lines
4.3 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/>.
// `C_Spell.IsAutoAttackSpell` / `C_Spell.IsRangedAutoAttackSpell` and
// the spellbook-slot variants.
//
// Melee — there's only one spell that triggers melee swings (`6603` —
// "Auto Attack"), and no `Spell.dbc` attribute uniquely identifies it
// vs. on-next-swing abilities like Heroic Strike or Maul. We test
// against the engine constant directly.
//
// Ranged — both vanilla ranged auto-attacks (`75` Auto Shot, `5019`
// Shoot wand) set bit 1 of `Spell.dbc.Attributes` (the
// `SPELL_ATTR_AUTO_REPEAT` flag at value `0x02`). Verified empirically
// against the in-game cache: Attr `0x00050012` (Auto Shot) and
// `0x00000012` (Shoot) share that bit; on-next-swing Heroic Strike
// (`0x00050014`) and other ranged spells like Aimed Shot / Fireball do
// NOT. The attribute check naturally extends to any future
// auto-repeating spell a private server might add.
#include "Lookup.h"
#include "Game.h"
#include "Offsets.h"
#include "dbc/Lookup.h"
#include <cstdint>
#include <cstring>
namespace Spell::AutoAttack {
namespace {
constexpr int SPELL_AUTO_ATTACK = 6603;
constexpr int OFF_SPELL_ATTRIBUTES = 0x18;
constexpr uint32_t SPELL_ATTR_AUTO_REPEAT = 0x00000002;
bool IsMelee(int spellID) {
return spellID == SPELL_AUTO_ATTACK;
}
bool IsRanged(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 attr = *reinterpret_cast<const uint32_t *>(
rec + OFF_SPELL_ATTRIBUTES);
return (attr & SPELL_ATTR_AUTO_REPEAT) != 0;
}
int ReadSpellID(void *L) {
if (!Game::Lua::IsNumber(L, 1))
return 0;
return static_cast<int>(Game::Lua::ToNumber(L, 1));
}
int ReadSpellBookSlotID(void *L) {
if (!Game::Lua::IsNumber(L, 1))
return 0;
const int slot = static_cast<int>(Game::Lua::ToNumber(L, 1));
int bookType = 0;
if (Game::Lua::IsString(L, 2)) {
const char *s = Game::Lua::ToString(L, 2);
if (s != nullptr && _stricmp(s, "pet") == 0)
bookType = 1;
} else if (Game::Lua::IsNumber(L, 2)) {
bookType = static_cast<int>(Game::Lua::ToNumber(L, 2));
}
return Spell::Lookup::SpellbookSlotToID(slot, bookType);
}
} // namespace
static int __fastcall Script_IsAutoAttackSpell(void *L) {
Game::Lua::PushBool(L, IsMelee(ReadSpellID(L)));
return 1;
}
static int __fastcall Script_IsRangedAutoAttackSpell(void *L) {
Game::Lua::PushBool(L, IsRanged(ReadSpellID(L)));
return 1;
}
static int __fastcall Script_IsAutoAttackSpellBookItem(void *L) {
Game::Lua::PushBool(L, IsMelee(ReadSpellBookSlotID(L)));
return 1;
}
static int __fastcall Script_IsRangedAutoAttackSpellBookItem(void *L) {
Game::Lua::PushBool(L, IsRanged(ReadSpellBookSlotID(L)));
return 1;
}
static void RegisterLuaFunctions() {
Game::Lua::RegisterTableFunction("C_Spell", "IsAutoAttackSpell",
&Script_IsAutoAttackSpell);
Game::Lua::RegisterTableFunction("C_Spell", "IsRangedAutoAttackSpell",
&Script_IsRangedAutoAttackSpell);
Game::Lua::RegisterTableFunction("C_SpellBook", "IsAutoAttackSpellBookItem",
&Script_IsAutoAttackSpellBookItem);
Game::Lua::RegisterTableFunction("C_SpellBook", "IsRangedAutoAttackSpellBookItem",
&Script_IsRangedAutoAttackSpellBookItem);
}
static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace Spell::AutoAttack