Repository navigation
Expand file tree
/
Copy pathLookup.cpp
More file actions
213 lines (188 loc) · 7.72 KB
/
Copy pathLookup.cpp
File metadata and controls
213 lines (188 loc) · 7.72 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
// This file is part of ClassicAPI.
//
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
// of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
#include "Lookup.h"
#include "Game.h"
#include "Offsets.h"
#include "baselib/Ascii.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));
}
const char *Name(const uint8_t *spellRecord) {
return DBC::LocalizedString(spellRecord, Offsets::OFF_SPELL_NAMES);
}
const char *Rank(const uint8_t *spellRecord) {
return DBC::LocalizedString(spellRecord, Offsets::OFF_SPELL_RECORD_RANK);
}
int DurationMs(const uint8_t *spellRecord, bool applyMods) {
// `pet = 0` selects the player as the caster; the third argument is skipMod.
using GetDuration_t = int(__fastcall *)(const uint8_t *spellRecord, int pet, int skipMod);
return reinterpret_cast<GetDuration_t>(static_cast<uintptr_t>(
Offsets::FUN_GET_SPELL_DURATION))(spellRecord, 0, applyMods ? 0 : 1);
}
const char *IconPath(const uint8_t *spellRecord, bool active) {
if (spellRecord == nullptr)
return nullptr;
const uint32_t iconID = Game::Read<uint32_t>(
spellRecord, active ? Offsets::OFF_SPELL_RECORD_ACTIVE_ICON_ID
: Offsets::OFF_SPELL_RECORD_ICON_ID);
return DBC::StringField(Offsets::VAR_SPELL_ICON_RECORDS,
Offsets::VAR_SPELL_ICON_COUNT, iconID,
Offsets::OFF_SPELLICON_PATH);
}
const char *DisplayIconPath(const uint8_t *spellRecord) {
if (spellRecord == nullptr)
return nullptr;
using WeaponIconPath_t = const char *(__cdecl *)();
if (Game::Read<int32_t>(spellRecord, Offsets::OFF_SPELL_RECORD_EFFECT) ==
Offsets::SPELL_EFFECT_ATTACK)
return reinterpret_cast<WeaponIconPath_t>(
static_cast<uintptr_t>(Offsets::FUN_ATTACK_ICON_PATH))();
if ((Game::Read<uint32_t>(spellRecord, Offsets::OFF_SPELL_RECORD_ATTRIBUTES) &
Offsets::SPELL_ATTR_RANGED) != 0 &&
(Game::Read<uint32_t>(spellRecord, Offsets::OFF_SPELL_RECORD_ATTRIBUTES_EX2) &
Offsets::SPELL_ATTR_EX2_AUTOREPEAT_FLAG) != 0) {
if (const char *ranged = reinterpret_cast<WeaponIconPath_t>(
static_cast<uintptr_t>(Offsets::FUN_RANGED_ICON_PATH))())
return ranged;
}
return IconPath(spellRecord, false);
}
bool IsFitToFamily(const uint8_t *spellRecord, uint32_t family,
uint64_t flagMask) {
if (spellRecord == nullptr)
return false;
if (*reinterpret_cast<const uint32_t *>(
spellRecord + Offsets::OFF_SPELL_RECORD_FAMILY_NAME) != family)
return false;
const uint64_t flags = *reinterpret_cast<const uint64_t *>(
spellRecord + Offsets::OFF_SPELL_RECORD_FAMILY_FLAGS);
return (flags & flagMask) != 0;
}
bool HasEffect(const uint8_t *spellRecord, int effect) {
if (spellRecord == nullptr)
return false;
for (int i = 0; i < Offsets::SPELL_RECORD_EFFECT_COUNT; ++i) {
if (Game::Read<int>(spellRecord, Offsets::OFF_SPELL_RECORD_EFFECT + i * 4) == effect)
return true;
}
return false;
}
int CastTimeMs(int spellID, bool pet, bool allowNegative) {
using Fn = int(__fastcall *)(int spellID, int pet, int allowNegative);
return reinterpret_cast<Fn>(Offsets::FUN_GET_CAST_TIME)(spellID, pet ? 1 : 0, allowNegative ? 1 : 0);
}
int32_t PowerCost(int spellID, bool pet) {
using Fn = int32_t(__fastcall *)(int spellID, int pet);
return reinterpret_cast<Fn>(Offsets::FUN_GET_SPELL_COST)(spellID, pet ? 1 : 0);
}
int FindAuraEffect(const uint8_t *spellRecord, int aura) {
if (spellRecord == nullptr)
return -1;
for (int i = 0; i < Offsets::SPELL_RECORD_EFFECT_COUNT; ++i) {
if (Game::Read<int>(spellRecord, Offsets::OFF_SPELL_RECORD_EFFECT_APPLY_AURA_NAME + i * 4) ==
aura)
return i;
}
return -1;
}
bool HasAuraEffect(const uint8_t *spellRecord, int aura) {
return FindAuraEffect(spellRecord, aura) >= 0;
}
int SpellbookSlotToID(int slot1Based, int bookType) {
const int slot = slot1Based - 1;
if (slot < 0 || slot >= Offsets::SPELLBOOK_MAX_SLOTS)
return 0;
return BookArray(bookType)[slot];
}
const int *BookArray(int bookType) {
return reinterpret_cast<const int *>(static_cast<uintptr_t>(
bookType == 1 ? Offsets::VAR_PET_SPELLBOOK : Offsets::VAR_PLAYER_SPELLBOOK));
}
int BookCount(int bookType) {
const int count = Game::Read<int>(static_cast<uintptr_t>(
bookType == 1 ? Offsets::VAR_PET_SPELLBOOK_COUNT : Offsets::VAR_PLAYER_SPELLBOOK_COUNT));
return count < 0 ? 0 : (count > Offsets::SPELLBOOK_MAX_SLOTS ? Offsets::SPELLBOOK_MAX_SLOTS : count);
}
int BookTypeArg(void *L, int idx) {
if (!Game::Lua::IsString(L, idx))
return 0;
return Ascii::EqualCI(Game::Lua::ToString(L, idx), "pet") ? 1 : 0;
}
int SpellBankArgToBookType(void *L, int idx) {
if (!Game::Lua::IsNumber(L, idx))
return 0;
return static_cast<int>(Game::Lua::ToNumber(L, idx)) == 1 ? 1 : 0;
}
int SpellbookItemArgsToID(void *L, int slotIdx, int bankIdx) {
if (!Game::Lua::IsNumber(L, slotIdx))
return 0;
const int slot = static_cast<int>(Game::Lua::ToNumber(L, slotIdx));
return SpellbookSlotToID(slot, SpellBankArgToBookType(L, bankIdx));
}
int RecipeRowSpellID(uintptr_t entriesVar, uintptr_t countVar, double index1) {
const uint32_t count = *reinterpret_cast<const uint32_t *>(countVar);
if (!(index1 >= 1.0 && index1 < static_cast<double>(count) + 1.0))
return 0;
auto **entries = *reinterpret_cast<const uint8_t ***>(entriesVar);
if (entries == nullptr)
return 0;
auto *entry = entries[static_cast<int>(index1) - 1];
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, double reagentIndex1) {
if (spellRecord == nullptr ||
!(reagentIndex1 >= 1.0 &&
reagentIndex1 < static_cast<double>(Offsets::SPELL_RECIPE_MAX_REAGENTS) + 1.0))
return 0;
const int n = static_cast<int>(reagentIndex1);
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 == n)
return itemID;
}
return 0;
}
int FindSpellbookSlot(int spellID, int *outBookType) {
if (spellID <= 0)
return 0;
for (int bookType = 0; bookType <= 1; ++bookType) {
int slot = 0;
ForEachBookSpell(bookType, [&](int i, int id) {
if (id != spellID)
return true;
slot = i + 1;
return false;
});
if (slot != 0) {
if (outBookType != nullptr)
*outBookType = bookType;
return slot;
}
}
return 0;
}
} // namespace Spell::Lookup