forked from diasurgical/devilutionX
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol.cpp
1792 lines (1616 loc) · 51.9 KB
/
control.cpp
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file control.cpp
*
* Implementation of the character and main control panels
*/
#include "control.h"
#include <array>
#include <cstddef>
#include <fmt/format.h>
#include "DiabloUI/art.h"
#include "DiabloUI/art_draw.h"
#include "DiabloUI/diabloui.h"
#include "automap.h"
#include "controls/keymapper.hpp"
#include "cursor.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/render/cel_render.hpp"
#include "engine/render/text_render.hpp"
#include "error.h"
#include "gamemenu.h"
#include "init.h"
#include "inv.h"
#include "lighting.h"
#include "minitext.h"
#include "missiles.h"
#include "panels/charpanel.hpp"
#include "panels/mainpanel.hpp"
#include "qol/xpbar.h"
#include "stores.h"
#include "towners.h"
#include "trigs.h"
#include "utils/sdl_geometry.h"
#include "options.h"
#ifdef _DEBUG
#include "debug.h"
#endif
namespace devilution {
bool dropGoldFlag;
bool chrbtn[4];
bool lvlbtndown;
int dropGoldValue;
bool chrbtnactive;
int pnumlines;
UiFlags InfoColor;
char tempstr[256];
int sbooktab;
int8_t initialDropGoldIndex;
bool talkflag;
bool sbookflag;
bool chrflag;
char infostr[64];
bool panelflag;
int initialDropGoldValue;
bool panbtndown;
bool spselflag;
Rectangle MainPanel;
Rectangle LeftPanel;
Rectangle RightPanel;
std::optional<OwnedSurface> pBtmBuff;
extern std::array<Keymapper::ActionIndex, 4> quickSpellActionIndexes;
/** Maps from attribute_id to the rectangle on screen used for attribute increment buttons. */
Rectangle ChrBtnsRect[4] = {
{ { 137, 138 }, { 41, 22 } },
{ { 137, 166 }, { 41, 22 } },
{ { 137, 195 }, { 41, 22 } },
{ { 137, 223 }, { 41, 22 } }
};
/** Positions of panel buttons. */
SDL_Rect PanBtnPos[7] = {
// clang-format off
{ 9, 9, 71, 19 }, // char button
{ 9, 35, 71, 19 }, // quests button
{ 9, 75, 71, 19 }, // map button
{ 9, 101, 71, 19 }, // menu button
{ 560, 9, 71, 19 }, // inv button
{ 560, 35, 71, 19 }, // spells button
{ 87, 91, 33, 32 }, // chat button
// clang-format on
};
namespace {
std::optional<OwnedSurface> pLifeBuff;
std::optional<OwnedSurface> pManaBuff;
std::optional<CelSprite> talkButtons;
std::optional<CelSprite> pDurIcons;
std::optional<CelSprite> multiButtons;
std::optional<CelSprite> pPanelButtons;
std::optional<CelSprite> pGBoxBuff;
std::optional<CelSprite> pSBkBtnCel;
std::optional<CelSprite> pSBkIconCels;
std::optional<CelSprite> pSpellBkCel;
std::optional<CelSprite> pSpellCels;
bool PanelButtons[7];
int PanelButtonIndex;
char TalkSave[8][80];
uint8_t TalkSaveIndex;
uint8_t NextTalkSave;
char TalkMessage[MAX_SEND_STR_LEN];
bool TalkButtonsDown[3];
int sgbPlrTalkTbl;
bool WhisperList[MAX_PLRS];
char panelstr[4][64];
uint8_t SplTransTbl[256];
/** Maps from spell_id to spelicon.cel frame number. */
char SpellITbl[] = {
27,
1,
2,
3,
4,
5,
6,
7,
8,
9,
28,
13,
12,
18,
16,
14,
18,
19,
11,
20,
15,
21,
23,
24,
25,
22,
26,
29,
37,
38,
39,
42,
41,
40,
10,
36,
30,
51,
51,
50,
46,
47,
43,
45,
48,
49,
44,
35,
35,
35,
35,
35,
};
enum panel_button_id {
PanelButtonCharinfo,
PanelButtonQlog,
PanelButtonAutomap,
PanelButtonMainmenu,
PanelButtonInventory,
PanelButtonSpellbook,
PanelButtonSendmsg,
};
/** Maps from panel_button_id to hotkey name. */
const char *const PanBtnHotKey[8] = { "'c'", "'q'", "Tab", "Esc", "'i'", "'b'", "Enter", nullptr };
/** Maps from panel_button_id to panel button description. */
const char *const PanBtnStr[8] = {
"Character Information",
"Quests log",
"Automap",
"Main Menu",
"Inventory",
"Spell book",
"Send Message",
"" // Player attack
};
/** Maps from spellbook page number and position to spell_id. */
spell_id SpellPages[6][7] = {
{ SPL_NULL, SPL_FIREBOLT, SPL_CBOLT, SPL_HBOLT, SPL_HEAL, SPL_HEALOTHER, SPL_FLAME },
{ SPL_RESURRECT, SPL_FIREWALL, SPL_TELEKINESIS, SPL_LIGHTNING, SPL_TOWN, SPL_FLASH, SPL_STONE },
{ SPL_RNDTELEPORT, SPL_MANASHIELD, SPL_ELEMENT, SPL_FIREBALL, SPL_WAVE, SPL_CHAIN, SPL_GUARDIAN },
{ SPL_NOVA, SPL_GOLEM, SPL_TELEPORT, SPL_APOCA, SPL_BONESPIRIT, SPL_FLARE, SPL_ETHEREALIZE },
{ SPL_LIGHTWALL, SPL_IMMOLAT, SPL_WARP, SPL_REFLECT, SPL_BERSERK, SPL_FIRERING, SPL_SEARCH },
{ SPL_INVALID, SPL_INVALID, SPL_INVALID, SPL_INVALID, SPL_INVALID, SPL_INVALID, SPL_INVALID }
};
#define SPLICONLENGTH 56
#define SPLROWICONLS 10
#define SPLICONLAST 52
void CalculatePanelAreas()
{
MainPanel = { { (gnScreenWidth - PANEL_WIDTH) / 2, gnScreenHeight - PANEL_HEIGHT }, { PANEL_WIDTH, PANEL_HEIGHT } };
LeftPanel = { { 0, 0 }, { SPANEL_WIDTH, SPANEL_HEIGHT } };
RightPanel = { { 0, 0 }, { SPANEL_WIDTH, SPANEL_HEIGHT } };
if (gnScreenWidth - 2 * SPANEL_WIDTH > PANEL_WIDTH) {
LeftPanel.position.x = (gnScreenWidth - 2 * SPANEL_WIDTH - PANEL_WIDTH) / 2;
} else {
LeftPanel.position.x = 0;
}
LeftPanel.position.y = (gnScreenHeight - LeftPanel.size.height - PANEL_HEIGHT) / 2;
RightPanel.position.x = gnScreenWidth - RightPanel.size.width - LeftPanel.position.x;
RightPanel.position.y = LeftPanel.position.y;
}
/**
* Draw spell cell onto the given buffer.
* @param out Output buffer
* @param position Buffer coordinates
* @param cel The CEL sprite
* @param nCel Index of the cel frame to draw. 0 based.
*/
void DrawSpellCel(const Surface &out, Point position, const CelSprite &cel, int nCel)
{
CelDrawLightTo(out, position, cel, nCel, SplTransTbl);
}
void SetSpellTrans(spell_type t)
{
if (t == RSPLTYPE_SKILL) {
for (int i = 0; i < 128; i++)
SplTransTbl[i] = i;
}
for (int i = 128; i < 256; i++)
SplTransTbl[i] = i;
SplTransTbl[255] = 0;
switch (t) {
case RSPLTYPE_SPELL:
SplTransTbl[PAL8_YELLOW] = PAL16_BLUE + 1;
SplTransTbl[PAL8_YELLOW + 1] = PAL16_BLUE + 3;
SplTransTbl[PAL8_YELLOW + 2] = PAL16_BLUE + 5;
for (int i = PAL16_BLUE; i < PAL16_BLUE + 16; i++) {
SplTransTbl[PAL16_BEIGE - PAL16_BLUE + i] = i;
SplTransTbl[PAL16_YELLOW - PAL16_BLUE + i] = i;
SplTransTbl[PAL16_ORANGE - PAL16_BLUE + i] = i;
}
break;
case RSPLTYPE_SCROLL:
SplTransTbl[PAL8_YELLOW] = PAL16_BEIGE + 1;
SplTransTbl[PAL8_YELLOW + 1] = PAL16_BEIGE + 3;
SplTransTbl[PAL8_YELLOW + 2] = PAL16_BEIGE + 5;
for (int i = PAL16_BEIGE; i < PAL16_BEIGE + 16; i++) {
SplTransTbl[PAL16_YELLOW - PAL16_BEIGE + i] = i;
SplTransTbl[PAL16_ORANGE - PAL16_BEIGE + i] = i;
}
break;
case RSPLTYPE_CHARGES:
SplTransTbl[PAL8_YELLOW] = PAL16_ORANGE + 1;
SplTransTbl[PAL8_YELLOW + 1] = PAL16_ORANGE + 3;
SplTransTbl[PAL8_YELLOW + 2] = PAL16_ORANGE + 5;
for (int i = PAL16_ORANGE; i < PAL16_ORANGE + 16; i++) {
SplTransTbl[PAL16_BEIGE - PAL16_ORANGE + i] = i;
SplTransTbl[PAL16_YELLOW - PAL16_ORANGE + i] = i;
}
break;
case RSPLTYPE_INVALID:
SplTransTbl[PAL8_YELLOW] = PAL16_GRAY + 1;
SplTransTbl[PAL8_YELLOW + 1] = PAL16_GRAY + 3;
SplTransTbl[PAL8_YELLOW + 2] = PAL16_GRAY + 5;
for (int i = PAL16_GRAY; i < PAL16_GRAY + 15; i++) {
SplTransTbl[PAL16_BEIGE - PAL16_GRAY + i] = i;
SplTransTbl[PAL16_YELLOW - PAL16_GRAY + i] = i;
SplTransTbl[PAL16_ORANGE - PAL16_GRAY + i] = i;
}
SplTransTbl[PAL16_BEIGE + 15] = 0;
SplTransTbl[PAL16_YELLOW + 15] = 0;
SplTransTbl[PAL16_ORANGE + 15] = 0;
break;
case RSPLTYPE_SKILL:
break;
}
}
void PrintSBookHotkey(const Surface &out, Point position, const std::string &text)
{
// Align the hot key text with the top-right corner of the spell icon
position += Displacement { SPLICONLENGTH - (GetLineWidth(text.c_str()) + 5), 5 - SPLICONLENGTH };
// Draw a drop shadow below and to the left of the text
DrawString(out, text, position + Displacement { -1, 1 }, UiFlags::ColorBlack);
// Then draw the text over the top
DrawString(out, text, position, UiFlags::ColorWhite);
}
/**
* Draws a section of the empty flask cel on top of the panel to create the illusion
* of the flask getting empty. This function takes a cel and draws a
* horizontal stripe of height (max-min) onto the given buffer.
* @param out Target buffer.
* @param position Buffer coordinate.
* @param celBuf Buffer of the empty flask cel.
* @param y0 Top of the flask cel section to draw.
* @param y1 Bottom of the flask cel section to draw.
*/
void DrawFlaskTop(const Surface &out, Point position, const Surface &celBuf, int y0, int y1)
{
out.BlitFrom(celBuf, SDL_Rect { 0, static_cast<decltype(SDL_Rect {}.y)>(y0), celBuf.w(), y1 - y0 }, position);
}
/**
* Draws the dome of the flask that protrudes above the panel top line.
* It draws a rectangle of fixed width 59 and height 'h' from the source buffer
* into the target buffer.
* @param out The target buffer.
* @param celBuf Buffer of the empty flask cel.
* @param sourcePosition Source buffer start coordinate.
* @param targetPosition Target buffer coordinate.
* @param h How many lines of the source buffer that will be copied.
*/
void DrawFlask(const Surface &out, const Surface &celBuf, Point sourcePosition, Point targetPosition, int h)
{
constexpr int FlaskWidth = 59;
out.BlitFromSkipColorIndexZero(celBuf, MakeSdlRect(sourcePosition.x, sourcePosition.y, FlaskWidth, h), targetPosition);
}
/**
* @brief Draws the part of the life/mana flasks protruding above the bottom panel
* @see DrawFlaskLower()
* @param out The display region to draw to
* @param sourceBuffer A sprite representing the appropriate background/empty flask style
* @param offset X coordinate offset for where the flask should be drawn
* @param fillPer How full the flask is (a value from 0 to 80)
*/
void DrawFlaskUpper(const Surface &out, const Surface &sourceBuffer, int offset, int fillPer)
{
// clamping because this function only draws the top 12% of the flask display
int emptyPortion = std::clamp(80 - fillPer, 0, 11) + 2; // +2 to account for the frame being included in the sprite
// Draw the empty part of the flask
DrawFlask(out, sourceBuffer, { 13, 3 }, { PANEL_LEFT + offset, PANEL_TOP - 13 }, emptyPortion);
if (emptyPortion < 13)
// Draw the filled part of the flask
DrawFlask(out, *pBtmBuff, { offset, emptyPortion + 3 }, { PANEL_LEFT + offset, PANEL_TOP - 13 + emptyPortion }, 13 - emptyPortion);
}
/**
* @brief Draws the part of the life/mana flasks inside the bottom panel
* @see DrawFlaskUpper()
* @param out The display region to draw to
* @param sourceBuffer A sprite representing the appropriate background/empty flask style
* @param offset X coordinate offset for where the flask should be drawn
* @param fillPer How full the flask is (a value from 0 to 80)
*/
void DrawFlaskLower(const Surface &out, const Surface &sourceBuffer, int offset, int fillPer)
{
int filled = std::clamp(fillPer, 0, 69);
if (filled < 69)
DrawFlaskTop(out, { PANEL_X + offset, PANEL_Y }, sourceBuffer, 16, 85 - filled);
// It appears that the panel defaults to having a filled flask and DrawFlaskTop only overlays the appropriate amount of empty space.
// This draw might not be necessary?
if (filled > 0)
DrawPanelBox(out, { offset, 85 - filled, 88, filled }, { PANEL_X + offset, PANEL_Y + 69 - filled });
}
void SetButtonStateDown(int btnId)
{
PanelButtons[btnId] = true;
panbtndown = true;
}
void PrintInfo(const Surface &out)
{
if (talkflag)
return;
const int LineStart[] = { 70, 58, 52, 48, 46 };
const int LineHeights[] = { 30, 24, 18, 15, 12 };
Rectangle line { { PANEL_X + 177, PANEL_Y + LineStart[pnumlines] }, { 288, 12 } };
if (infostr[0] != '\0') {
DrawString(out, infostr, line, InfoColor | UiFlags::AlignCenter | UiFlags::KerningFitSpacing, 2);
line.position.y += LineHeights[pnumlines];
}
for (int i = 0; i < pnumlines; i++) {
DrawString(out, panelstr[i], line, InfoColor | UiFlags::AlignCenter | UiFlags::KerningFitSpacing, 2);
line.position.y += LineHeights[pnumlines];
}
}
int CapStatPointsToAdd(int remainingStatPoints, const Player &player, CharacterAttribute attribute)
{
int pointsToReachCap = player.GetMaximumAttributeValue(attribute) - player.GetBaseAttributeValue(attribute);
return std::min(remainingStatPoints, pointsToReachCap);
}
int DrawDurIcon4Item(const Surface &out, Item &pItem, int x, int c)
{
if (pItem.isEmpty())
return x;
if (pItem._iDurability > 5)
return x;
if (c == 0) {
switch (pItem._itype) {
case ItemType::Sword:
c = 2;
break;
case ItemType::Axe:
c = 6;
break;
case ItemType::Bow:
c = 7;
break;
case ItemType::Mace:
c = 5;
break;
case ItemType::Staff:
c = 8;
break;
default:
c = 1;
break;
}
}
if (pItem._iDurability > 2)
c += 8;
CelDrawTo(out, { x, -17 + PANEL_Y }, *pDurIcons, c);
return x - 32 - 8;
}
void PrintSBookStr(const Surface &out, Point position, const char *text)
{
DrawString(out, text, { GetPanelPosition(UiPanels::Spell, { SPLICONLENGTH + position.x, position.y }), { 222, 0 } }, UiFlags::ColorWhite);
}
spell_type GetSBookTrans(spell_id ii, bool townok)
{
auto &myPlayer = Players[MyPlayerId];
if ((myPlayer._pClass == HeroClass::Monk) && (ii == SPL_SEARCH))
return RSPLTYPE_SKILL;
spell_type st = RSPLTYPE_SPELL;
if ((myPlayer._pISpells & GetSpellBitmask(ii)) != 0) {
st = RSPLTYPE_CHARGES;
}
if ((myPlayer._pAblSpells & GetSpellBitmask(ii)) != 0) {
st = RSPLTYPE_SKILL;
}
if (st == RSPLTYPE_SPELL) {
if (CheckSpell(MyPlayerId, ii, st, true) != SpellCheckResult::Success) {
st = RSPLTYPE_INVALID;
}
if ((char)(myPlayer._pSplLvl[ii] + myPlayer._pISplLvlAdd) <= 0) {
st = RSPLTYPE_INVALID;
}
}
if (townok && currlevel == 0 && st != RSPLTYPE_INVALID && !spelldata[ii].sTownSpell) {
st = RSPLTYPE_INVALID;
}
return st;
}
void ControlSetGoldCurs(Player &player)
{
SetPlrHandGoldCurs(player.HoldItem);
NewCursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM);
}
void ResetTalkMsg()
{
#ifdef _DEBUG
if (CheckDebugTextCommand(TalkMessage))
return;
#endif
uint32_t pmask = 0;
for (int i = 0; i < MAX_PLRS; i++) {
if (WhisperList[i])
pmask |= 1 << i;
}
NetSendCmdString(pmask, TalkMessage);
}
void ControlPressEnter()
{
if (TalkMessage[0] != 0) {
ResetTalkMsg();
uint8_t i = 0;
for (; i < 8; i++) {
if (strcmp(TalkSave[i], TalkMessage) == 0)
break;
}
if (i >= 8) {
strcpy(TalkSave[NextTalkSave], TalkMessage);
NextTalkSave++;
NextTalkSave &= 7;
} else {
uint8_t talkSave = NextTalkSave - 1;
talkSave &= 7;
if (i != talkSave) {
strcpy(TalkSave[i], TalkSave[talkSave]);
strcpy(TalkSave[talkSave], TalkMessage);
}
}
TalkMessage[0] = '\0';
TalkSaveIndex = NextTalkSave;
}
control_reset_talk();
}
void ControlUpDown(int v)
{
for (int i = 0; i < 8; i++) {
TalkSaveIndex = (v + TalkSaveIndex) & 7;
if (TalkSave[TalkSaveIndex][0] != 0) {
strcpy(TalkMessage, TalkSave[TalkSaveIndex]);
return;
}
}
}
void RemoveGold(Player &player, int goldIndex)
{
int gi = goldIndex - INVITEM_INV_FIRST;
player.InvList[gi]._ivalue -= dropGoldValue;
if (player.InvList[gi]._ivalue > 0)
SetPlrHandGoldCurs(player.InvList[gi]);
else
player.RemoveInvItem(gi);
SetPlrHandItem(player.HoldItem, IDI_GOLD);
SetGoldSeed(player, player.HoldItem);
player.HoldItem._ivalue = dropGoldValue;
player.HoldItem._iStatFlag = true;
ControlSetGoldCurs(player);
player._pGold = CalculateGold(player);
dropGoldValue = 0;
}
struct SpellListItem {
Point location;
spell_type type;
spell_id id;
bool isSelected;
};
std::vector<SpellListItem> GetSpellListItems()
{
std::vector<SpellListItem> spellListItems;
uint64_t mask;
int x = PANEL_X + 12 + SPLICONLENGTH * SPLROWICONLS;
int y = PANEL_Y - 17;
for (int i = RSPLTYPE_SKILL; i < RSPLTYPE_INVALID; i++) {
auto &myPlayer = Players[MyPlayerId];
switch ((spell_type)i) {
case RSPLTYPE_SKILL:
mask = myPlayer._pAblSpells;
break;
case RSPLTYPE_SPELL:
mask = myPlayer._pMemSpells;
break;
case RSPLTYPE_SCROLL:
mask = myPlayer._pScrlSpells;
break;
case RSPLTYPE_CHARGES:
mask = myPlayer._pISpells;
break;
case RSPLTYPE_INVALID:
break;
}
int8_t j = SPL_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 }, (spell_type)i, (spell_id)j, isSelected });
x -= SPLICONLENGTH;
if (x == PANEL_X + 12 - SPLICONLENGTH) {
x = PANEL_X + 12 + SPLICONLENGTH * SPLROWICONLS;
y -= SPLICONLENGTH;
}
}
if (mask != 0 && x != PANEL_X + 12 + SPLICONLENGTH * SPLROWICONLS)
x -= SPLICONLENGTH;
if (x == PANEL_X + 12 - SPLICONLENGTH) {
x = PANEL_X + 12 + SPLICONLENGTH * SPLROWICONLS;
y -= SPLICONLENGTH;
}
}
return spellListItems;
}
bool GetSpellListSelection(spell_id &pSpell, spell_type &pSplType)
{
pSpell = spell_id::SPL_INVALID;
pSplType = spell_type::RSPLTYPE_INVALID;
auto &myPlayer = Players[MyPlayerId];
for (auto &spellListItem : GetSpellListItems()) {
if (spellListItem.isSelected) {
pSpell = spellListItem.id;
pSplType = spellListItem.type;
if (myPlayer._pClass == HeroClass::Monk && spellListItem.id == SPL_SEARCH)
pSplType = RSPLTYPE_SKILL;
return true;
}
}
return false;
}
} // namespace
bool IsChatAvailable()
{
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
void DrawSpell(const Surface &out)
{
auto &myPlayer = Players[MyPlayerId];
spell_id spl = myPlayer._pRSpell;
spell_type st = myPlayer._pRSplType;
// BUGFIX: Move the next line into the if statement to avoid OOB (SPL_INVALID is -1) (fixed)
if (st == RSPLTYPE_SPELL && spl != SPL_INVALID) {
int tlvl = myPlayer._pISplLvlAdd + myPlayer._pSplLvl[spl];
if (CheckSpell(MyPlayerId, spl, st, true) != SpellCheckResult::Success)
st = RSPLTYPE_INVALID;
if (tlvl <= 0)
st = RSPLTYPE_INVALID;
}
if (currlevel == 0 && st != RSPLTYPE_INVALID && !spelldata[spl].sTownSpell)
st = RSPLTYPE_INVALID;
SetSpellTrans(st);
const int nCel = (spl != SPL_INVALID) ? SpellITbl[spl] : 27;
const Point position { PANEL_X + 565, PANEL_Y + 119 };
DrawSpellCel(out, position, *pSpellCels, nCel);
}
void DrawSpellList(const Surface &out)
{
infostr[0] = '\0';
ClearPanel();
auto &myPlayer = Players[MyPlayerId];
for (auto &spellListItem : GetSpellListItems()) {
spell_type transType = spellListItem.type;
int spellLevel = 0;
const SpellData &spellDataItem = spelldata[static_cast<size_t>(spellListItem.id)];
if (currlevel == 0 && !spellDataItem.sTownSpell) {
transType = RSPLTYPE_INVALID;
}
if (spellListItem.type == RSPLTYPE_SPELL) {
spellLevel = std::max(myPlayer._pISplLvlAdd + myPlayer._pSplLvl[spellListItem.id], 0);
if (spellLevel == 0)
transType = RSPLTYPE_INVALID;
}
SetSpellTrans(transType);
DrawSpellCel(out, spellListItem.location, *pSpellCels, SpellITbl[static_cast<size_t>(spellListItem.id)]);
if (!spellListItem.isSelected)
continue;
switch (spellListItem.type) {
case RSPLTYPE_SKILL:
DrawSpellCel(out, spellListItem.location, *pSpellCels, SPLICONLAST + 3);
strcpy(infostr, fmt::format("{:s} Skill", spellDataItem.sSkillText).c_str());
break;
case RSPLTYPE_SPELL:
DrawSpellCel(out, spellListItem.location, *pSpellCels, SPLICONLAST + 4);
strcpy(infostr, fmt::format("{:s} Spell", spellDataItem.sNameText).c_str());
if (spellListItem.id == SPL_HBOLT) {
strcpy(tempstr, "Damages undead only");
AddPanelString(tempstr);
}
if (spellLevel == 0)
strcpy(tempstr, "Spell Level 0 - Unusable");
else
strcpy(tempstr, fmt::format("Spell Level {:d}", spellLevel).c_str());
AddPanelString(tempstr);
break;
case RSPLTYPE_SCROLL: {
DrawSpellCel(out, spellListItem.location, *pSpellCels, SPLICONLAST + 1);
strcpy(infostr, fmt::format("Scroll of {:s}", spellDataItem.sNameText).c_str());
int v = 0;
for (int t = 0; t < myPlayer._pNumInv; t++) {
if (!myPlayer.InvList[t].isEmpty()
&& (myPlayer.InvList[t]._iMiscId == IMISC_SCROLL || myPlayer.InvList[t]._iMiscId == IMISC_SCROLLT)
&& myPlayer.InvList[t]._iSpell == spellListItem.id) {
v++;
}
}
for (auto &item : myPlayer.SpdList) {
if (!item.isEmpty()
&& (item._iMiscId == IMISC_SCROLL || item._iMiscId == IMISC_SCROLLT)
&& item._iSpell == spellListItem.id) {
v++;
}
}
strcpy(tempstr, fmt::format(v == 1 ? "{:d} Scroll" : "{:d} Scrolls", v).c_str());
AddPanelString(tempstr);
} break;
case RSPLTYPE_CHARGES: {
DrawSpellCel(out, spellListItem.location, *pSpellCels, SPLICONLAST + 2);
strcpy(infostr, fmt::format("Staff of {:s}", spellDataItem.sNameText).c_str());
int charges = myPlayer.InvBody[INVLOC_HAND_LEFT]._iCharges;
strcpy(tempstr, fmt::format(charges == 1 ? "{:d} Charge" : "{:d} Charges", charges).c_str());
AddPanelString(tempstr);
} break;
case RSPLTYPE_INVALID:
break;
}
for (int t = 0; t < 4; t++) {
if (myPlayer._pSplHotKey[t] == spellListItem.id && myPlayer._pSplTHotKey[t] == spellListItem.type) {
auto hotkeyName = keymapper.KeyNameForAction(quickSpellActionIndexes[t]);
PrintSBookHotkey(out, spellListItem.location, hotkeyName);
strcpy(tempstr, fmt::format("Spell Hotkey {:s}", hotkeyName.c_str()).c_str());
AddPanelString(tempstr);
}
}
}
}
void SetSpell()
{
spell_id pSpell;
spell_type pSplType;
spselflag = false;
if (!GetSpellListSelection(pSpell, pSplType)) {
return;
}
ClearPanel();
auto &myPlayer = Players[MyPlayerId];
myPlayer._pRSpell = pSpell;
myPlayer._pRSplType = pSplType;
}
void SetSpeedSpell(int slot)
{
spell_id pSpell;
spell_type pSplType;
if (!GetSpellListSelection(pSpell, pSplType)) {
return;
}
auto &myPlayer = Players[MyPlayerId];
for (int i = 0; i < 4; ++i) {
if (myPlayer._pSplHotKey[i] == pSpell && myPlayer._pSplTHotKey[i] == pSplType)
myPlayer._pSplHotKey[i] = SPL_INVALID;
}
myPlayer._pSplHotKey[slot] = pSpell;
myPlayer._pSplTHotKey[slot] = pSplType;
}
void ToggleSpell(int slot)
{
uint64_t spells;
auto &myPlayer = Players[MyPlayerId];
if (myPlayer._pSplHotKey[slot] == SPL_INVALID) {
return;
}
switch (myPlayer._pSplTHotKey[slot]) {
case RSPLTYPE_SKILL:
spells = myPlayer._pAblSpells;
break;
case RSPLTYPE_SPELL:
spells = myPlayer._pMemSpells;
break;
case RSPLTYPE_SCROLL:
spells = myPlayer._pScrlSpells;
break;
case RSPLTYPE_CHARGES:
spells = myPlayer._pISpells;
break;
case RSPLTYPE_INVALID:
return;
}
if ((spells & GetSpellBitmask(myPlayer._pSplHotKey[slot])) != 0) {
myPlayer._pRSpell = myPlayer._pSplHotKey[slot];
myPlayer._pRSplType = myPlayer._pSplTHotKey[slot];
}
}
void AddPanelString(const char *str)
{
strcpy(panelstr[pnumlines], str);
if (pnumlines < 4)
pnumlines++;
}
void ClearPanel()
{
pnumlines = 0;
}
Point GetPanelPosition(UiPanels panel, Point offset)
{
Displacement displacement { offset.x, offset.y };
switch (panel) {
case UiPanels::Main:
return MainPanel.position + displacement;
case UiPanels::Quest:
case UiPanels::Character:
return LeftPanel.position + displacement;
case UiPanels::Spell:
case UiPanels::Inventory:
return RightPanel.position + displacement;
default:
return MainPanel.position + displacement;
}
}
void DrawPanelBox(const Surface &out, SDL_Rect srcRect, Point targetPosition)
{
out.BlitFrom(*pBtmBuff, srcRect, targetPosition);
}
void DrawLifeFlaskUpper(const Surface &out)
{
constexpr int LifeFlaskUpperOffset = 109;
DrawFlaskUpper(out, *pLifeBuff, LifeFlaskUpperOffset, Players[MyPlayerId]._pHPPer);
}
void DrawManaFlaskUpper(const Surface &out)
{
constexpr int ManaFlaskUpperOffset = 475;
DrawFlaskUpper(out, *pManaBuff, ManaFlaskUpperOffset, Players[MyPlayerId]._pManaPer);
}
void DrawLifeFlaskLower(const Surface &out)
{
constexpr int LifeFlaskLowerOffset = 96;
DrawFlaskLower(out, *pLifeBuff, LifeFlaskLowerOffset, Players[MyPlayerId]._pHPPer);
}
void DrawManaFlaskLower(const Surface &out)
{
constexpr int ManaFlaskLowerOffeset = 464;
DrawFlaskLower(out, *pManaBuff, ManaFlaskLowerOffeset, Players[MyPlayerId]._pManaPer);
}
void control_update_life_mana()
{
Players[MyPlayerId].UpdateManaPercentage();
Players[MyPlayerId].UpdateHitPointPercentage();
}
void InitControlPan()
{
pBtmBuff.emplace(PANEL_WIDTH, (PANEL_HEIGHT + 16) * (IsChatAvailable() ? 2 : 1));
pManaBuff.emplace(88, 88);
pLifeBuff.emplace(88, 88);
LoadCharPanel();
pSpellCels = LoadCel("Data\\SpelIcon.CEL", SPLICONLENGTH);
SetSpellTrans(RSPLTYPE_SKILL);
CelDrawUnsafeTo(*pBtmBuff, { 0, (PANEL_HEIGHT + 16) - 1 }, LoadCel("CtrlPan\\Panel8.CEL", PANEL_WIDTH), 1);
{
const Point bulbsPosition { 0, 87 };
const CelSprite statusPanel = LoadCel("CtrlPan\\P8Bulbs.CEL", 88);
CelDrawUnsafeTo(*pLifeBuff, bulbsPosition, statusPanel, 1);
CelDrawUnsafeTo(*pManaBuff, bulbsPosition, statusPanel, 2);
}
talkflag = false;
if (IsChatAvailable()) {
CelDrawUnsafeTo(*pBtmBuff, { 0, (PANEL_HEIGHT + 16) * 2 - 1 }, LoadCel("CtrlPan\\TalkPanl.CEL", PANEL_WIDTH), 1);
multiButtons = LoadCel("CtrlPan\\P8But2.CEL", 33);
talkButtons = LoadCel("CtrlPan\\TalkButt.CEL", 61);
sgbPlrTalkTbl = 0;
TalkMessage[0] = '\0';
for (bool &whisper : WhisperList)
whisper = true;
for (bool &talkButtonDown : TalkButtonsDown)
talkButtonDown = false;
}
LoadMainPanel();
panelflag = false;
lvlbtndown = false;
pPanelButtons = LoadCel("CtrlPan\\Panel8bu.CEL", 71);
ClearPanBtn();
if (!IsChatAvailable())
PanelButtonIndex = 6;
else
PanelButtonIndex = 7;
pChrButtons = LoadCel("Data\\CharBut.CEL", 41);
for (bool &buttonEnabled : chrbtn)
buttonEnabled = false;
chrbtnactive = false;
pDurIcons = LoadCel("Items\\DurIcons.CEL", 32);
strcpy(infostr, "");
ClearPanel();
chrflag = false;
spselflag = false;
pSpellBkCel = LoadCel("Data\\SpellBk.CEL", SPANEL_WIDTH);
static const int SBkBtnWidths[] = { 0, 61, 61, 61, 61, 61, 76 };
pSBkBtnCel = LoadCel("Data\\SpellBkB.CEL", SBkBtnWidths);
pSBkIconCels = LoadCel("Data\\SpellI2.CEL", 37);
sbooktab = 0;
sbookflag = false;
auto &myPlayer = Players[MyPlayerId];
if (myPlayer._pClass == HeroClass::Warrior) {
SpellPages[0][0] = SPL_REPAIR;
} else if (myPlayer._pClass == HeroClass::Rogue) {
SpellPages[0][0] = SPL_DISARM;
} else if (myPlayer._pClass == HeroClass::Sorcerer) {
SpellPages[0][0] = SPL_RECHARGE;
} else if (myPlayer._pClass == HeroClass::Monk) {
SpellPages[0][0] = SPL_SEARCH;
} else if (myPlayer._pClass == HeroClass::Bard) {
SpellPages[0][0] = SPL_IDENTIFY;
} else if (myPlayer._pClass == HeroClass::Barbarian) {
SpellPages[0][0] = SPL_BLODBOIL;
}
pQLogCel = LoadCel("Data\\Quest.CEL", SPANEL_WIDTH);
pGBoxBuff = LoadCel("CtrlPan\\Golddrop.cel", 261);
dropGoldFlag = false;
dropGoldValue = 0;
initialDropGoldValue = 0;
initialDropGoldIndex = 0;
CalculatePanelAreas();
}
void DrawCtrlPan(const Surface &out)
{
DrawPanelBox(out, { 0, sgbPlrTalkTbl + 16, PANEL_WIDTH, PANEL_HEIGHT }, { PANEL_X, PANEL_Y });
DrawInfoBox(out);
}
void DrawCtrlBtns(const Surface &out)
{
for (int i = 0; i < 6; i++) {
if (!PanelButtons[i]) {
DrawPanelBox(out, { PanBtnPos[i].x, PanBtnPos[i].y + 16, 71, 20 }, { PanBtnPos[i].x + PANEL_X, PanBtnPos[i].y + PANEL_Y });
} else {
Point position { PanBtnPos[i].x + PANEL_X, PanBtnPos[i].y + PANEL_Y + 18 };
CelDrawTo(out, position, *pPanelButtons, i + 1);
DrawArt(out, position + Displacement { 4, -18 }, &PanelButtonDown, i);
}
}
if (PanelButtonIndex == 7) {
CelDrawTo(out, { 87 + PANEL_X, 122 + PANEL_Y }, *multiButtons, PanelButtons[6] ? 2 : 1);
}
}
void DoSpeedBook()
{
spselflag = true;
int xo = PANEL_X + 12 + SPLICONLENGTH * 10;
int yo = PANEL_Y - 17;
int x = xo + SPLICONLENGTH / 2;
int y = yo - SPLICONLENGTH / 2;
auto &myPlayer = Players[MyPlayerId];