-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfo.cpp
More file actions
612 lines (550 loc) · 26.1 KB
/
Info.cpp
File metadata and controls
612 lines (550 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// 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 "Game.h"
#include "Offsets.h"
#include "spell/Arg.h"
#include "spell/Lookup.h"
#include <cstdint>
#include <cstdio>
namespace Spell::Info {
// Spell.dbc record offsets (from BuildSpellTooltip / Script_GetSpellName / Script_GetSpellTexture).
static constexpr int OFF_ATTRIBUTES_EX = 0x1C;
static constexpr int OFF_CASTING_TIME_INDEX = 0x48;
static constexpr int OFF_POWER_TYPE = 0x7C;
static constexpr int OFF_MANA_COST = 0x80;
static constexpr int OFF_RANGE_INDEX = 0x90;
static constexpr int OFF_ICON_ID = 0x1D4;
static constexpr int OFF_NAME = 0x1E0;
static constexpr int OFF_RANK = 0x204;
// Spell.dbc Attributes (+0x18) and AttributesEx (+0x1C) flag bits we read.
// Both bits are 0x40, but on different fields:
// Attributes bit 6 = SPELL_ATTR_PASSIVE — passive spell (no cast bar,
// applies its effect as soon as learned/equipped)
// AttributesEx bit 6 = SPELL_ATTR_EX_FUNNEL_PERCENT — funnel channel
static constexpr int OFF_ATTRIBUTES = 0x18;
static constexpr uint32_t SPELL_ATTR_PASSIVE = 0x40;
static constexpr uint32_t SPELL_ATTR_EX_FUNNEL = 0x40;
// Spell.dbc effect-target arrays. Each spell has 3 effects, each
// with an implicit target A and an implicit target B (the latter
// often 0). Used by IsSpellHarmful / IsSpellHelpful — vanilla has
// no dedicated "positive"/"negative" attribute flag (AttributesEx
// bit 0x80 = NEGATIVE is sparsely set — most damage spells don't
// have it), so we classify by walking effect targets and checking
// whether any falls into a known hostile-target or friendly-target
// set. Same algorithm CMaNGOS uses in `SpellMgr::IsPositiveSpell`.
static constexpr int OFF_EFFECT_IMPLICIT_TARGET_A = 0x148; // uint32[3]
static constexpr int OFF_EFFECT_IMPLICIT_TARGET_B = 0x154; // uint32[3]
// Target IDs from vanilla 1.12's `SpellTarget` enum that mark a
// spell as hostile-targeted (covers single-target damage,
// debuffs, AoE damage, etc.). List is conservative — anything not
// in here defaults to non-hostile.
static bool IsHostileTarget(uint32_t t) {
switch (t) {
case 6: // TARGET_CHAIN_DAMAGE (Fireball, Polymorph, …)
case 15: // TARGET_ALL_ENEMY_IN_AREA
case 16: // TARGET_ALL_ENEMY_IN_AREA_INSTANT
case 25: // TARGET_DUELVSPLAYER
case 28: // TARGET_ALL_ENEMY_IN_AREA_CHANNELED
case 36: // TARGET_ALL_HOSTILE_UNITS_AROUND_CASTER
case 47: // TARGET_SINGLE_ENEMY
case 53: // TARGET_LARGE_FRONTAL_CONE (hostile cone)
case 54: // TARGET_NARROW_FRONTAL_CONE
return true;
default:
return false;
}
}
// Friendly-targeted set (including self-cast). Same caveat — list
// is curated, false-negatives are possible for edge cases.
static bool IsFriendlyTarget(uint32_t t) {
switch (t) {
case 1: // TARGET_SELF
case 5: // TARGET_PET
case 20: // TARGET_ALL_PARTY_AROUND_CASTER
case 21: // TARGET_SINGLE_FRIEND
case 30: // TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
case 31: // TARGET_ALL_FRIENDLY_UNITS_IN_AREA
case 33: // TARGET_ALL_PARTY
case 34: // TARGET_ALL_PARTY_AROUND_CASTER_2
case 35: // TARGET_SINGLE_PARTY
case 37: // TARGET_AREAEFFECT_PARTY
case 40: // TARGET_CHAIN_HEAL
case 45: // TARGET_DYNAMIC_OBJECT_RIGHT_SIDE (friendly buffs use this)
case 56: // TARGET_RANDOM_NEARBY_LOC (totem placement)
case 57: // TARGET_RANDOM_CIRCUMFERENCE_POINT
case 61: // TARGET_RANDOM_FRIEND_AROUND_CASTER
case 68: // TARGET_NONCOMBAT_PET
case 77: // TARGET_SINGLE_FRIEND_2
case 80: // TARGET_AREAEFFECT_PARTY_AND_CLASS
return true;
default:
return false;
}
}
template <typename T>
static T ReadGlobal(uintptr_t addr) {
return *reinterpret_cast<T *>(addr);
}
// Sub-DBC lookup (SpellIcon, SpellCastTimes, SpellRange) — these are the
// "indirected fields" of a Spell.dbc record. Different base/count
// addresses than the main Spell.dbc, so they don't go through
// `Spell::Lookup::RecordForID`.
static const uint8_t *LookupSubRecord(uintptr_t baseAddr, uintptr_t countAddr, int id) {
if (id <= 0)
return nullptr;
const int count = ReadGlobal<int>(countAddr);
if (id > count)
return nullptr;
const uint8_t *const *records = ReadGlobal<const uint8_t *const *>(baseAddr);
if (records == nullptr)
return nullptr;
return records[id];
}
struct SpellInfoData {
int spellID;
const char *name; // localized; nullable if record's locale slot is unset
const char *rank; // localized; same nullability as name
const char *iconPath; // SpellIcon.dbc path; null if icon record missing
int cost; // base ManaCost (no per-level scaling)
bool isFunnel;
int powerType; // 0=mana, 1=rage, 2=focus, 3=energy, 4=happiness
int castTimeMs; // base time from SpellCastTimes.dbc
float minRange;
float maxRange;
};
static bool ReadSpellInfo(int spellID, SpellInfoData &out) {
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
if (record == nullptr)
return false;
const int locale = ReadGlobal<int>(Offsets::VAR_LOCALE_INDEX);
out.spellID = spellID;
out.name = *reinterpret_cast<const char *const *>(record + OFF_NAME + locale * 4);
out.rank = *reinterpret_cast<const char *const *>(record + OFF_RANK + locale * 4);
out.iconPath = nullptr;
const int iconID = *reinterpret_cast<const int *>(record + OFF_ICON_ID);
if (auto *iconRec = LookupSubRecord(Offsets::VAR_SPELL_ICON_RECORDS,
Offsets::VAR_SPELL_ICON_COUNT, iconID)) {
out.iconPath = *reinterpret_cast<const char *const *>(iconRec + 4);
}
out.cost = *reinterpret_cast<const int *>(record + OFF_MANA_COST);
const uint32_t attrEx = *reinterpret_cast<const uint32_t *>(record + OFF_ATTRIBUTES_EX);
out.isFunnel = (attrEx & SPELL_ATTR_EX_FUNNEL) != 0;
out.powerType = *reinterpret_cast<const int *>(record + OFF_POWER_TYPE);
out.castTimeMs = 0;
const int castIndex = *reinterpret_cast<const int *>(record + OFF_CASTING_TIME_INDEX);
if (auto *castRec = LookupSubRecord(Offsets::VAR_SPELL_CAST_TIMES_RECORDS,
Offsets::VAR_SPELL_CAST_TIMES_COUNT, castIndex)) {
out.castTimeMs = *reinterpret_cast<const int *>(castRec + 4);
}
out.minRange = 0.0f;
out.maxRange = 0.0f;
const int rangeIndex = *reinterpret_cast<const int *>(record + OFF_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);
}
return true;
}
// Case-insensitive match against the literal string "pet". Anything else
// (including the explicit "spell") maps to the player spellbook —
// matches 1.12's `Script_GetSpellName` behavior, which `SStrCmpI`s the
// input against "pet" and treats every other value as the player book.
static bool BookTypeIsPet(const char *s) {
if (s == nullptr)
return false;
auto lc = [](char c) -> char {
return (c >= 'A' && c <= 'Z') ? static_cast<char>(c + 32) : c;
};
return lc(s[0]) == 'p' && lc(s[1]) == 'e' && lc(s[2]) == 't' && s[3] == '\0';
}
// Resolves the Lua args to a spellID, supporting:
// GetSpellInfo(spellID) -- arg2 absent or non-string
// GetSpellInfo(slot, "spell"|"pet") -- arg2 string → spellbook lookup
// Returns 0 for invalid/empty inputs (Lua side surfaces 0 as nil since
// spellID 0 is never valid). Calls `lua_error` if arg1 isn't a number,
// matching the engine's own `lua_error`-on-misuse style.
static int ResolveLuaArgsToSpellID(void *L) {
if (!Game::Lua::IsNumber(L, 1)) {
Game::Lua::Error(L, "Usage: GetSpellInfo(spellID) or GetSpellInfo(slot, bookType)");
return 0;
}
const int arg1 = static_cast<int>(Game::Lua::ToNumber(L, 1));
if (Game::Lua::Type(L, 2) == Game::Lua::TYPE_STRING) {
const char *book = Game::Lua::ToString(L, 2);
const int bookType = static_cast<int>(BookTypeIsPet(book));
return Spell::Lookup::SpellbookSlotToID(arg1, bookType);
}
return arg1;
}
static int __fastcall Script_GetSpellInfo(void *L) {
const int spellID = ResolveLuaArgsToSpellID(L);
if (spellID <= 0)
return 0;
SpellInfoData info;
if (!ReadSpellInfo(spellID, info))
return 0;
// PushString tail-calls PushNil for null pointers, so unset locale
// slots (rare, but possible) come through as nil rather than crash.
Game::Lua::PushString(L, info.name); // 1. name
Game::Lua::PushString(L, info.rank); // 2. rank
Game::Lua::PushString(L, info.iconPath); // 3. icon (path string)
Game::Lua::PushNumber(L, static_cast<double>(info.cost)); // 4. cost
Game::Lua::PushBool(L, info.isFunnel); // 5. isFunnel
Game::Lua::PushNumber(L, static_cast<double>(info.powerType)); // 6. powerType
Game::Lua::PushNumber(L, static_cast<double>(info.castTimeMs)); // 7. castTime (ms)
Game::Lua::PushNumber(L, static_cast<double>(info.minRange)); // 8. minRange
Game::Lua::PushNumber(L, static_cast<double>(info.maxRange)); // 9. maxRange
Game::Lua::PushNumber(L, static_cast<double>(info.spellID)); // 10. spellID (NEW)
return 10;
}
// Helpers for building the C_Spell.GetSpellInfo result table. Each
// expects the table at stack[-3] before the call (key + value get
// pushed onto top, then `RawSet(L, -3)` pops them and sets the field).
static void SetField(void *L, const char *key, const char *value) {
Game::Lua::PushString(L, key);
Game::Lua::PushString(L, value); // PushString handles NULL -> nil
Game::Lua::RawSet(L, -3);
}
static void SetField(void *L, const char *key, double value) {
Game::Lua::PushString(L, key);
Game::Lua::PushNumber(L, value);
Game::Lua::RawSet(L, -3);
}
static void SetFieldBool(void *L, const char *key, bool value) {
Game::Lua::PushString(L, key);
Game::Lua::PushBool(L, value);
Game::Lua::RawSet(L, -3);
}
static int __fastcall Script_C_GetSpellInfo(void *L) {
const int spellID = Spell::Arg::ResolveSpellID(L, 1);
SpellInfoData info;
if (!ReadSpellInfo(spellID, info))
return 0; // nil for unknown spellID
Game::Lua::NewTable(L);
SetField(L, "name", info.name);
// `iconID` deviates from modern's fileID:number — vanilla has no
// fileID system, so we surface the icon path string instead.
// Practical: feed it directly to texture:SetTexture(...).
SetField(L, "iconID", info.iconPath);
SetField(L, "castTime", static_cast<double>(info.castTimeMs));
SetField(L, "minRange", static_cast<double>(info.minRange));
SetField(L, "maxRange", static_cast<double>(info.maxRange));
SetField(L, "spellID", static_cast<double>(info.spellID));
// Vanilla extras beyond the modern signature — present in 1.12's
// Spell.dbc, no harm including them for addons backporting from
// 3.3.5 where the same data was exposed positionally.
SetField(L, "rank", info.rank);
SetField(L, "cost", static_cast<double>(info.cost));
SetFieldBool(L, "isFunnel", info.isFunnel);
SetField(L, "powerType", static_cast<double>(info.powerType));
return 1;
}
static int __fastcall Script_C_GetSpellName(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 unknown spellID
const int locale = ReadGlobal<int>(Offsets::VAR_LOCALE_INDEX);
const char *name = *reinterpret_cast<const char *const *>(record + OFF_NAME + locale * 4);
if (name == nullptr || *name == '\0')
return 0; // empty / no name in current locale → nil
Game::Lua::PushString(L, name);
return 1;
}
static int __fastcall Script_C_GetSpellTexture(void *L) {
const int spellID = Spell::Arg::ResolveSpellID(L, 1);
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
if (record == nullptr)
return 0;
const int iconID = *reinterpret_cast<const int *>(record + OFF_ICON_ID);
auto *iconRec = LookupSubRecord(Offsets::VAR_SPELL_ICON_RECORDS,
Offsets::VAR_SPELL_ICON_COUNT, iconID);
if (iconRec == nullptr)
return 0;
const char *path = *reinterpret_cast<const char *const *>(iconRec + 4);
if (path == nullptr || *path == '\0')
return 0;
Game::Lua::PushString(L, path);
return 1;
}
// Builds the spell hyperlink string `|cff71d5ff|Hspell:ID:0|h[Name]|h|r`
// into `out`. The trailing `:0` after the spellID matches modern's
// hyperlink format (the field's a sub-data slot used for pet-spellbook
// flags etc. in later expansions); 1.12 ignores it during link parsing
// but addons grepping with `|Hspell:(%d+):` patterns will pick it up.
//
// Returns false if the spellID is invalid, the locale-resolved name is
// missing, or the output buffer is too small (the caller's 256-byte
// stack buffer is comfortable for any vanilla spell name).
static bool BuildSpellLink(int spellID, char *out, size_t outLen) {
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
if (record == nullptr)
return false;
const int locale = ReadGlobal<int>(Offsets::VAR_LOCALE_INDEX);
const char *name = *reinterpret_cast<const char *const *>(record + OFF_NAME + locale * 4);
if (name == nullptr || *name == '\0')
return false;
const int n = std::snprintf(out, outLen, "|cff71d5ff|Hspell:%d:0|h[%s]|h|r",
spellID, name);
return n > 0 && static_cast<size_t>(n) < outLen;
}
// `GetSpellLink(spellID)` / `GetSpellLink(slot, bookType)` — same arg
// shape as `GetSpellInfo`. Returns `(linkString, spellID)`. The second
// return is what makes the spellbook overload useful: callers can pass
// in `(slot, "spell")` and get back both the link AND the resolved
// spellID without a separate lookup.
static int __fastcall Script_GetSpellLink(void *L) {
const int spellID = ResolveLuaArgsToSpellID(L);
if (spellID <= 0)
return 0;
char buf[256];
if (!BuildSpellLink(spellID, buf, sizeof(buf)))
return 0;
Game::Lua::PushString(L, buf);
Game::Lua::PushNumber(L, static_cast<double>(spellID));
return 2;
}
// `C_Spell.GetSpellLink(spellID)` — table-namespace version. Returns
// only the link string (modern omits the spellID echo since the caller
// already had it on hand to call this).
static int __fastcall Script_C_GetSpellLink(void *L) {
const int spellID = Spell::Arg::ResolveSpellID(L, 1);
if (spellID <= 0)
return 0;
char buf[256];
if (!BuildSpellLink(spellID, buf, sizeof(buf)))
return 0;
Game::Lua::PushString(L, buf);
return 1;
}
// `FindSpellBookSlotByID(spellID)` — inverse of `GetSpellName(slot,
// bookType)`. Searches the player spellbook first, then the pet
// spellbook, for a slot whose spellID matches. Returns
// `(slot, bookType)` so callers can feed both directly into the
// existing slot-and-bookType API surface (`GetSpellName`,
// `GetSpellTexture`, etc.). Returns nil if the spellID isn't currently
// in either book.
static int __fastcall Script_FindSpellBookSlotByID(void *L) {
if (!Game::Lua::IsNumber(L, 1)) {
Game::Lua::Error(L, "Usage: FindSpellBookSlotByID(spellID)");
return 0;
}
const int spellID = static_cast<int>(Game::Lua::ToNumber(L, 1));
int bookType = 0;
const int slot = Spell::Lookup::FindSpellbookSlot(spellID, &bookType);
if (slot == 0)
return 0;
Game::Lua::PushNumber(L, static_cast<double>(slot));
Game::Lua::PushString(L, bookType == 1 ? "pet" : "spell");
return 2;
}
// Shared back-end for both passive-spell query forms. Reads `Attributes`
// (+0x18) bit 6 (`SPELL_ATTR_PASSIVE`) off the Spell.dbc record and
// pushes the boolean. Returns 1 with a boolean on the Lua stack for a
// valid spellID, 0 (= nil to Lua) for an invalid one.
static int PushIsPassive(void *L, int spellID) {
if (spellID <= 0)
return 0;
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
if (record == nullptr)
return 0;
const uint32_t attr = *reinterpret_cast<const uint32_t *>(record + OFF_ATTRIBUTES);
Game::Lua::PushBool(L, (attr & SPELL_ATTR_PASSIVE) != 0);
return 1;
}
// `IsPassiveSpell(spellID)` / `IsPassiveSpell(slot, bookType)` — same
// arg shape as `GetSpellInfo`. The 3.0-era global form; takes either a
// spellID directly or a spellbook slot + bookType (`"spell"` / `"pet"`).
static int __fastcall Script_IsPassiveSpell(void *L) {
return PushIsPassive(L, ResolveLuaArgsToSpellID(L));
}
// `C_Spell.IsSpellPassive(spellID)` — modern table-namespace form
// (10.0+; word order flipped from the older `IsPassiveSpell`).
// Takes a spellID only — `C_Spell.*` calls don't accept the spellbook
// slot+bookType shape.
static int __fastcall Script_C_IsSpellPassive(void *L) {
return PushIsPassive(L, Spell::Arg::ResolveSpellID(L, 1));
}
// `IsPlayerSpell(spellID)` — returns true if the player currently
// "knows" the given spellID. Covers everything: trained class abilities,
// racials, talent passives, profession recipes (including ones learned
// from vendors / discovered tradeskill recipes), and anything else
// granted by SMSG_LEARNED_SPELL.
//
// Implementation: single bitmap lookup at `[VAR_PLAYER_SPELL_BITMAP]`.
// The engine maintains a dword bitmap of every spellID the player
// knows (one bit per spellID, indexed by `spellID`); learning/unlearning
// updates this bitmap. We just consult the same bit the engine itself
// reads via the helper at `0x0060C740` — no walks, no profession
// caching, no per-source data structure.
//
// Trade-offs vs. the older spellbook+talent walk implementation:
// - Faster (one memory access vs hundreds of comparisons).
// - Broader (covers profession recipes that are not in the spellbook
// arrays and not in the talent tree).
// - Same shape modern WoW (5.4.8+) uses for the same function — see
// `[0x011C25D8]` in 5.4.8's `Wow.exe`.
static int __fastcall Script_IsPlayerSpell(void *L) {
if (!Game::Lua::IsNumber(L, 1)) {
Game::Lua::Error(L, "Usage: IsPlayerSpell(spellID)");
return 0;
}
const int spellID = static_cast<int>(Game::Lua::ToNumber(L, 1));
if (spellID < 1) {
Game::Lua::PushBool(L, 0);
return 1;
}
const int spellCount = *reinterpret_cast<const int *>(
static_cast<uintptr_t>(Offsets::VAR_SPELL_RECORD_COUNT));
if (spellID > spellCount) {
Game::Lua::PushBoolean(L, 0);
return 1;
}
auto *bitmap = *reinterpret_cast<const uint32_t *const *>(
static_cast<uintptr_t>(Offsets::VAR_PLAYER_SPELL_BITMAP));
if (bitmap == nullptr) {
Game::Lua::PushBoolean(L, 0);
return 1;
}
const uint32_t mask = 1u << (spellID & 31);
const bool isKnown = (bitmap[spellID >> 5] & mask) != 0;
Game::Lua::PushBoolean(L, isKnown);
return 1;
}
// `IsSpellKnown(spellID, [isPet])` — strict spellbook check, scoped to
// the player or pet spellbook depending on `isPet` (default false).
//
// Critically NOT the same as `IsPlayerSpell`: this is the narrower
// modern-semantics function. Returns true only for spells the player
// has as a usable spellbook entry (trained class abilities, active
// talent grants, racials that appear in the spellbook). Returns false
// for talent passives, profession recipes, and anything else that's
// "known" but not displayable as a spellbook button.
//
// Verified against 3.3.5's `Script_IsSpellKnown` at `0x0053C3A0`,
// whose inner function at `0x0053B4E0` walks the spellbook arrays
// (`[0x00BE6D88]` player / `[0x00BE7D98]` pet) with their respective
// counts — strict array scan, not a bitmap. Same shape applies in
// 1.12: the spellbook arrays at `VAR_PLAYER_SPELLBOOK` /
// `VAR_PET_SPELLBOOK` are the authoritative spellbook display data,
// so we walk those via the existing `Spell::Lookup::FindSpellbookSlot`
// and filter by the book type the caller asked for.
//
// Use `IsPlayerSpell(spellID)` for the broader "any kind of known"
// check (covers recipes, passives, etc.).
static int __fastcall Script_IsSpellKnown(void *L) {
if (!Game::Lua::IsNumber(L, 1)) {
Game::Lua::Error(L, "Usage: IsSpellKnown(spellID, [isPet])");
return 0;
}
const int spellID = static_cast<int>(Game::Lua::ToNumber(L, 1));
if (spellID < 1) {
Game::Lua::PushBool(L, 0);
return 1;
}
const int wantBookType = static_cast<int>(Game::Lua::ToBoolean(L, 2) != 0);
int bookType = -1;
const int slot = Spell::Lookup::FindSpellbookSlot(spellID, &bookType);
const bool found = (slot > 0 && bookType == wantBookType);
Game::Lua::PushBoolean(L, found);
return 1;
}
// Walks the spell's 3 effects' implicit target IDs (both A and B
// slots) and returns true if `pred` matches any of them. Returns
// false for null records.
template <typename Pred>
static bool AnyEffectTarget(const uint8_t *record, Pred pred) {
if (record == nullptr)
return false;
auto *targetsA = reinterpret_cast<const uint32_t *>(
record + OFF_EFFECT_IMPLICIT_TARGET_A);
auto *targetsB = reinterpret_cast<const uint32_t *>(
record + OFF_EFFECT_IMPLICIT_TARGET_B);
for (int i = 0; i < 3; ++i) {
if (pred(targetsA[i]) || pred(targetsB[i]))
return true;
}
return false;
}
// `IsSpellHarmful(spellID)` — true iff any effect of the spell
// targets a hostile-target type (chain damage, single enemy, AoE
// enemy, etc. — see `IsHostileTarget`). Same algorithm CMaNGOS uses
// to classify positive vs negative spells, since vanilla 1.12 has
// no dedicated "harmful" attribute bit (the `SPELL_ATTR_EX_NEGATIVE`
// flag is sparsely set — most damage spells don't have it).
static bool ComputeIsHarmful(int spellID) {
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
return AnyEffectTarget(record, &IsHostileTarget);
}
// `IsSpellHelpful(spellID)` — true iff any effect targets a
// friendly-target type (self, party, single friend, chain heal,
// etc.). Disjoint from harmful in practice for vanilla spells —
// most spells are clearly one or the other — but they aren't
// strict inverses: a few utility/geometry-targeted spells (script-
// driven, totem placement, etc.) return false for both.
static bool ComputeIsHelpful(int spellID) {
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
return AnyEffectTarget(record, &IsFriendlyTarget);
}
// `IsHarmfulSpell(spellID)` / `IsHarmfulSpell(slot, bookType)` —
// modern global. Accepts either form via the same
// `ResolveLuaArgsToSpellID` path `GetSpellInfo` uses.
static int __fastcall Script_IsHarmfulSpell(void *L) {
const int spellID = ResolveLuaArgsToSpellID(L);
Game::Lua::PushBool(L, ComputeIsHarmful(spellID));
return 1;
}
// `IsHelpfulSpell(spellID)` / `IsHelpfulSpell(slot, bookType)` —
// modern global. True for spells that exist and aren't marked
// harmful. Vanilla 1.12 has no dedicated "helpful" flag in
// Spell.dbc, so "non-harmful and present" is the best approximation
// without parsing every effect's implicit target.
static int __fastcall Script_IsHelpfulSpell(void *L) {
const int spellID = ResolveLuaArgsToSpellID(L);
Game::Lua::PushBool(L, ComputeIsHelpful(spellID));
return 1;
}
// `C_Spell.IsSpellHarmful(spellID)` — direct-by-ID modern signature.
// No spellbook-slot variant; takes a numeric spellID only.
static int __fastcall Script_C_Spell_IsSpellHarmful(void *L) {
Game::Lua::PushBool(L, ComputeIsHarmful(Spell::Arg::ResolveSpellID(L, 1)));
return 1;
}
// `C_Spell.IsSpellHelpful(spellID)` — direct-by-ID modern signature.
static int __fastcall Script_C_Spell_IsSpellHelpful(void *L) {
Game::Lua::PushBool(L, ComputeIsHelpful(Spell::Arg::ResolveSpellID(L, 1)));
return 1;
}
static void RegisterLuaFunctions() {
Game::Lua::RegisterGlobalFunction("GetSpellInfo", &Script_GetSpellInfo);
Game::Lua::RegisterGlobalFunction("GetSpellLink", &Script_GetSpellLink);
Game::Lua::RegisterGlobalFunction("FindSpellBookSlotByID",
&Script_FindSpellBookSlotByID);
Game::Lua::RegisterGlobalFunction("IsPassiveSpell", &Script_IsPassiveSpell);
Game::Lua::RegisterGlobalFunction("IsPlayerSpell", &Script_IsPlayerSpell);
Game::Lua::RegisterGlobalFunction("IsSpellKnown", &Script_IsSpellKnown);
Game::Lua::RegisterGlobalFunction("IsHarmfulSpell", &Script_IsHarmfulSpell);
Game::Lua::RegisterGlobalFunction("IsHelpfulSpell", &Script_IsHelpfulSpell);
Game::Lua::RegisterTableFunction("C_Spell", "GetSpellLink", &Script_C_GetSpellLink);
Game::Lua::RegisterTableFunction("C_Spell", "GetSpellInfo", &Script_C_GetSpellInfo);
Game::Lua::RegisterTableFunction("C_Spell", "GetSpellName", &Script_C_GetSpellName);
Game::Lua::RegisterTableFunction("C_Spell", "GetSpellTexture", &Script_C_GetSpellTexture);
Game::Lua::RegisterTableFunction("C_Spell", "IsSpellPassive", &Script_C_IsSpellPassive);
Game::Lua::RegisterTableFunction("C_Spell", "IsSpellHarmful",
&Script_C_Spell_IsSpellHarmful);
Game::Lua::RegisterTableFunction("C_Spell", "IsSpellHelpful",
&Script_C_Spell_IsSpellHelpful);
}
static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace Spell::Info