forked from diasurgical/DevilutionX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspell_list.cpp
More file actions
417 lines (355 loc) · 12.4 KB
/
spell_list.cpp
File metadata and controls
417 lines (355 loc) · 12.4 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
#include "panels/spell_list.hpp"
#include <cstdint>
#include <fmt/format.h>
#include "control.h"
#include "controls/control_mode.hpp"
#include "controls/plrctrls.h"
#include "engine/backbuffer_state.hpp"
#include "engine/palette.h"
#include "engine/render/primitive_render.hpp"
#include "engine/render/text_render.hpp"
#include "inv_iterators.hpp"
#include "options.h"
#include "panels/spell_icons.hpp"
#include "player.h"
#include "spells.h"
#include "utils/algorithm/container.hpp"
#include "utils/language.h"
#include "utils/str_cat.hpp"
#include "utils/utf8.hpp"
#define SPLROWICONLS 10
namespace devilution {
namespace {
void PrintSBookSpellType(const Surface &out, Point position, std::string_view text, uint8_t rectColorIndex)
{
DrawLargeSpellIconBorder(out, position, rectColorIndex);
// Align the spell type text with bottom of spell icon
position += Displacement { SPLICONLENGTH / 2 - GetLineWidth(text) / 2, (IsSmallFontTall() ? -19 : -15) };
// Then draw the text over the top
DrawString(out, text, position, { .flags = UiFlags::ColorWhite | UiFlags::Outlined });
}
void PrintSBookHotkey(const Surface &out, Point position, const std::string_view text)
{
// Align the hot key text with the top-right corner of the spell icon
position += Displacement { SPLICONLENGTH - (GetLineWidth(text.data()) + 5), 5 - SPLICONLENGTH };
// Then draw the text over the top
DrawString(out, text, position, { .flags = UiFlags::ColorWhite | UiFlags::Outlined });
}
bool GetSpellListSelection(SpellID &pSpell, SpellType &pSplType)
{
pSpell = SpellID::Invalid;
pSplType = SpellType::Invalid;
Player &myPlayer = *MyPlayer;
for (auto &spellListItem : GetSpellListItems()) {
if (spellListItem.isSelected) {
pSpell = spellListItem.id;
pSplType = spellListItem.type;
if (myPlayer._pClass == HeroClass::Monk && spellListItem.id == SpellID::Search)
pSplType = SpellType::Skill;
return true;
}
}
return false;
}
std::optional<std::string_view> GetHotkeyName(SpellID spellId, SpellType spellType, bool useShortName = false)
{
Player &myPlayer = *MyPlayer;
for (size_t t = 0; t < NumHotkeys; t++) {
if (myPlayer._pSplHotKey[t] != spellId || myPlayer._pSplTHotKey[t] != spellType)
continue;
auto quickSpellActionKey = StrCat("QuickSpell", t + 1);
if (ControlMode == ControlTypes::Gamepad)
return GetOptions().Padmapper.InputNameForAction(quickSpellActionKey, useShortName);
return GetOptions().Keymapper.KeyNameForAction(quickSpellActionKey);
}
return {};
}
void PrintSBookRemainingCharges(const Surface &out, Point pos)
{
const Player &myPlayer = *MyPlayer;
if (myPlayer._pRSplType != SpellType::Charges)
return;
const Item &staff = myPlayer.InvBody[INVLOC_HAND_LEFT];
UiFlags color = (staff._iCharges == staff._iMaxCharges ? UiFlags::ColorGold : UiFlags::ColorBlue);
auto drawStringWithShadow = [out, color](std::string_view text, Point pos) {
DrawString(out, text, pos + Displacement { -1, -1 },
{ .flags = UiFlags::ColorBlack | UiFlags::KerningFitSpacing, .spacing = 0 });
DrawString(out, text, pos,
{ .flags = color | UiFlags::KerningFitSpacing, .spacing = 0 });
};
std::string currText = StrCat(staff._iCharges);
drawStringWithShadow(currText, pos + Displacement { 2, -12 });
}
} // namespace
void DrawSpell(const Surface &out)
{
Player &myPlayer = *MyPlayer;
SpellID spl = myPlayer._pRSpell;
SpellType st = myPlayer._pRSplType;
if (!IsValidSpell(spl)) {
st = SpellType::Invalid;
spl = SpellID::Null;
}
if (st == SpellType::Spell) {
int tlvl = myPlayer.GetSpellLevel(spl);
if (CheckSpell(*MyPlayer, spl, st, true) != SpellCheckResult::Success)
st = SpellType::Invalid;
if (tlvl <= 0)
st = SpellType::Invalid;
}
if (leveltype == DTYPE_TOWN && st != SpellType::Invalid && !GetSpellData(spl).isAllowedInTown())
st = SpellType::Invalid;
SetSpellTrans(st);
const Point position = GetMainPanel().position + Displacement { 565, 119 };
DrawLargeSpellIcon(out, position, spl);
std::optional<std::string_view> hotkeyName = GetHotkeyName(spl, myPlayer._pRSplType, true);
if (hotkeyName)
PrintSBookHotkey(out, position, *hotkeyName);
if (*GetOptions().Gameplay.showRemainingCharges) {
PrintSBookRemainingCharges(out, position);
}
}
void DrawSpellList(const Surface &out)
{
InfoString = StringOrView {};
const Player &myPlayer = *MyPlayer;
for (auto &spellListItem : GetSpellListItems()) {
const SpellID spellId = spellListItem.id;
SpellType transType = spellListItem.type;
int spellLevel = 0;
const SpellData &spellDataItem = GetSpellData(spellListItem.id);
if (leveltype == DTYPE_TOWN && !spellDataItem.isAllowedInTown()) {
transType = SpellType::Invalid;
}
if (spellListItem.type == SpellType::Spell) {
spellLevel = myPlayer.GetSpellLevel(spellListItem.id);
if (spellLevel == 0)
transType = SpellType::Invalid;
}
SetSpellTrans(transType);
DrawLargeSpellIcon(out, spellListItem.location, spellId);
std::optional<std::string_view> shortHotkeyName = GetHotkeyName(spellId, spellListItem.type, true);
if (shortHotkeyName)
PrintSBookHotkey(out, spellListItem.location, *shortHotkeyName);
if (!spellListItem.isSelected)
continue;
uint8_t spellColor = PAL16_GRAY + 5;
switch (spellListItem.type) {
case SpellType::Skill:
spellColor = PAL16_YELLOW - 46;
PrintSBookSpellType(out, spellListItem.location, _("Skill"), spellColor);
InfoString = fmt::format(fmt::runtime(_("{:s} Skill")), pgettext("spell", spellDataItem.sNameText));
break;
case SpellType::Spell:
if (!myPlayer.isOnLevel(0)) {
spellColor = PAL16_BLUE + 5;
}
PrintSBookSpellType(out, spellListItem.location, _("Spell"), spellColor);
InfoString = fmt::format(fmt::runtime(_("{:s} Spell")), pgettext("spell", spellDataItem.sNameText));
if (spellId == SpellID::HolyBolt) {
AddInfoBoxString(_("Damages undead only"));
}
if (spellLevel == 0)
AddInfoBoxString(_("Spell Level 0 - Unusable"));
else
AddInfoBoxString(fmt::format(fmt::runtime(_("Spell Level {:d}")), spellLevel));
break;
case SpellType::Scroll: {
if (!myPlayer.isOnLevel(0)) {
spellColor = PAL16_RED - 59;
}
PrintSBookSpellType(out, spellListItem.location, _("Scroll"), spellColor);
InfoString = fmt::format(fmt::runtime(_("Scroll of {:s}")), pgettext("spell", spellDataItem.sNameText));
const int scrollCount = c_count_if(InventoryAndBeltPlayerItemsRange { myPlayer }, [spellId](const Item &item) {
return item.isScrollOf(spellId);
});
AddInfoBoxString(fmt::format(fmt::runtime(ngettext("{:d} Scroll", "{:d} Scrolls", scrollCount)), scrollCount));
} break;
case SpellType::Charges: {
if (!myPlayer.isOnLevel(0)) {
spellColor = PAL16_ORANGE + 5;
}
PrintSBookSpellType(out, spellListItem.location, _("Staff"), spellColor);
InfoString = fmt::format(fmt::runtime(_("Staff of {:s}")), pgettext("spell", spellDataItem.sNameText));
int charges = myPlayer.InvBody[INVLOC_HAND_LEFT]._iCharges;
AddInfoBoxString(fmt::format(fmt::runtime(ngettext("{:d} Charge", "{:d} Charges", charges)), charges));
} break;
case SpellType::Invalid:
break;
}
std::optional<std::string_view> fullHotkeyName = GetHotkeyName(spellId, spellListItem.type);
if (fullHotkeyName) {
AddInfoBoxString(fmt::format(fmt::runtime(_("Spell Hotkey {:s}")), *fullHotkeyName));
}
}
}
std::vector<SpellListItem> GetSpellListItems()
{
std::vector<SpellListItem> spellListItems;
uint64_t mask;
const Point mainPanelPosition = GetMainPanel().position;
int x = mainPanelPosition.x + 12 + SPLICONLENGTH * SPLROWICONLS;
int y = mainPanelPosition.y - 17;
for (auto i : enum_values<SpellType>()) {
Player &myPlayer = *MyPlayer;
switch (static_cast<SpellType>(i)) {
case SpellType::Skill:
mask = myPlayer._pAblSpells;
break;
case SpellType::Spell:
mask = myPlayer._pMemSpells;
break;
case SpellType::Scroll:
mask = myPlayer._pScrlSpells;
break;
case SpellType::Charges:
mask = myPlayer._pISpells;
break;
default:
continue;
}
int8_t j = static_cast<int8_t>(SpellID::Firebolt);
for (uint64_t spl = 1; j < MAX_SPELLS; spl <<= 1, j++) {
if ((mask & spl) == 0)
continue;
int lx = x;
int ly = y - SPLICONLENGTH;
bool isSelected = (MousePosition.x >= lx && MousePosition.x < lx + SPLICONLENGTH && MousePosition.y >= ly && MousePosition.y < ly + SPLICONLENGTH);
spellListItems.emplace_back(SpellListItem { { x, y }, static_cast<SpellType>(i), static_cast<SpellID>(j), isSelected });
x -= SPLICONLENGTH;
if (x == mainPanelPosition.x + 12 - SPLICONLENGTH) {
x = mainPanelPosition.x + 12 + SPLICONLENGTH * SPLROWICONLS;
y -= SPLICONLENGTH;
}
}
if (mask != 0 && x != mainPanelPosition.x + 12 + SPLICONLENGTH * SPLROWICONLS)
x -= SPLICONLENGTH;
if (x == mainPanelPosition.x + 12 - SPLICONLENGTH) {
x = mainPanelPosition.x + 12 + SPLICONLENGTH * SPLROWICONLS;
y -= SPLICONLENGTH;
}
}
return spellListItems;
}
void SetSpell()
{
SpellID pSpell;
SpellType pSplType;
SpellSelectFlag = false;
if (!GetSpellListSelection(pSpell, pSplType)) {
return;
}
Player &myPlayer = *MyPlayer;
myPlayer._pRSpell = pSpell;
myPlayer._pRSplType = pSplType;
RedrawEverything();
}
void SetSpeedSpell(size_t slot)
{
SpellID pSpell;
SpellType pSplType;
if (!GetSpellListSelection(pSpell, pSplType)) {
return;
}
Player &myPlayer = *MyPlayer;
if (myPlayer._pSplHotKey[slot] == pSpell && myPlayer._pSplTHotKey[slot] == pSplType) {
// Unset spell hotkey
myPlayer._pSplHotKey[slot] = SpellID::Invalid;
return;
}
for (size_t i = 0; i < NumHotkeys; ++i) {
if (myPlayer._pSplHotKey[i] == pSpell && myPlayer._pSplTHotKey[i] == pSplType)
myPlayer._pSplHotKey[i] = SpellID::Invalid;
}
myPlayer._pSplHotKey[slot] = pSpell;
myPlayer._pSplTHotKey[slot] = pSplType;
}
bool IsValidSpeedSpell(size_t slot)
{
uint64_t spells;
Player &myPlayer = *MyPlayer;
const SpellID spellId = myPlayer._pSplHotKey[slot];
if (!IsValidSpell(spellId)) {
return false;
}
switch (myPlayer._pSplTHotKey[slot]) {
case SpellType::Skill:
spells = myPlayer._pAblSpells;
break;
case SpellType::Spell:
spells = myPlayer._pMemSpells;
break;
case SpellType::Scroll:
spells = myPlayer._pScrlSpells;
break;
case SpellType::Charges:
spells = myPlayer._pISpells;
break;
case SpellType::Invalid:
return false;
}
return (spells & GetSpellBitmask(spellId)) != 0;
}
void ToggleSpell(size_t slot)
{
if (IsValidSpeedSpell(slot)) {
Player &myPlayer = *MyPlayer;
myPlayer._pRSpell = myPlayer._pSplHotKey[slot];
myPlayer._pRSplType = myPlayer._pSplTHotKey[slot];
RedrawEverything();
}
}
void DoSpeedBook()
{
SpellSelectFlag = true;
const Point mainPanelPosition = GetMainPanel().position;
int xo = mainPanelPosition.x + 12 + SPLICONLENGTH * 10;
int yo = mainPanelPosition.y - 17;
int x = xo + SPLICONLENGTH / 2;
int y = yo - SPLICONLENGTH / 2;
Player &myPlayer = *MyPlayer;
if (IsValidSpell(myPlayer._pRSpell)) {
for (auto i : enum_values<SpellType>()) {
uint64_t spells;
switch (static_cast<SpellType>(i)) {
case SpellType::Skill:
spells = myPlayer._pAblSpells;
break;
case SpellType::Spell:
spells = myPlayer._pMemSpells;
break;
case SpellType::Scroll:
spells = myPlayer._pScrlSpells;
break;
case SpellType::Charges:
spells = myPlayer._pISpells;
break;
default:
continue;
}
uint64_t spell = 1;
for (int j = 1; j < MAX_SPELLS; j++) {
if ((spell & spells) != 0) {
if (j == static_cast<int8_t>(myPlayer._pRSpell) && static_cast<SpellType>(i) == myPlayer._pRSplType) {
x = xo + SPLICONLENGTH / 2;
y = yo - SPLICONLENGTH / 2;
}
xo -= SPLICONLENGTH;
if (xo == mainPanelPosition.x + 12 - SPLICONLENGTH) {
xo = mainPanelPosition.x + 12 + SPLICONLENGTH * SPLROWICONLS;
yo -= SPLICONLENGTH;
}
}
spell <<= 1ULL;
}
if (spells != 0 && xo != mainPanelPosition.x + 12 + SPLICONLENGTH * SPLROWICONLS)
xo -= SPLICONLENGTH;
if (xo == mainPanelPosition.x + 12 - SPLICONLENGTH) {
xo = mainPanelPosition.x + 12 + SPLICONLENGTH * SPLROWICONLS;
yo -= SPLICONLENGTH;
}
}
}
SetCursorPos({ x, y });
}
} // namespace devilution