-
Notifications
You must be signed in to change notification settings - Fork 800
/
stores.cpp
2776 lines (2400 loc) · 67.9 KB
/
stores.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 stores.cpp
*
* Implementation of functionality for stores and towner dialogs.
*/
#include "stores.h"
#include <algorithm>
#include <cstdint>
#include <string_view>
#include <fmt/format.h>
#include "controls/plrctrls.h"
#include "cursor.h"
#include "engine/backbuffer_state.hpp"
#include "engine/load_cel.hpp"
#include "engine/random.hpp"
#include "engine/render/clx_render.hpp"
#include "engine/render/text_render.hpp"
#include "engine/trn.hpp"
#include "init.h"
#include "minitext.h"
#include "options.h"
#include "panels/info_box.hpp"
#include "qol/stash.h"
#include "towners.h"
#include "utils/format_int.hpp"
#include "utils/language.h"
#include "utils/str_cat.hpp"
#include "utils/utf8.hpp"
namespace devilution {
TalkID ActiveStore;
int CurrentItemIndex;
int8_t PlayerItemIndexes[48];
Item PlayerItems[48];
Item SmithItems[SMITH_ITEMS];
int PremiumItemCount;
int PremiumItemLevel;
Item PremiumItems[SMITH_PREMIUM_ITEMS];
Item HealerItems[20];
Item WitchItems[WITCH_ITEMS];
int BoyItemLevel;
Item BoyItem;
namespace {
/** The current towner being interacted with */
_talker_id TownerId;
/** Is the current dialog full size */
bool IsTextFullSize;
/** Number of text lines in the current dialog */
int NumTextLines;
/** Remember currently selected text line from TextLine while displaying a dialog */
int OldTextLine;
/** Currently selected text line from TextLine */
int CurrentTextLine;
struct STextStruct {
enum Type : uint8_t {
Label,
Divider,
Selectable,
};
std::string text;
int _sval;
int y;
UiFlags flags;
Type type;
uint8_t _sx;
uint8_t _syoff;
int cursId;
bool cursIndent;
[[nodiscard]] bool isDivider() const
{
return type == Divider;
}
[[nodiscard]] bool isSelectable() const
{
return type == Selectable;
}
[[nodiscard]] bool hasText() const
{
return !text.empty();
}
};
/** Text lines */
STextStruct TextLine[STORE_LINES];
/** Whether to render the player's gold amount in the top left */
bool RenderGold;
/** Does the current panel have a scrollbar */
bool HasScrollbar;
/** Remember last scroll position */
int OldScrollPos;
/** Scroll position */
int ScrollPos;
/** Next scroll position */
int NextScrollPos;
/** Previous scroll position */
int PreviousScrollPos;
/** Countdown for the push state of the scroll up button */
int8_t CountdownScrollUp;
/** Countdown for the push state of the scroll down button */
int8_t CountdownScrollDown;
/** Remember current store while displaying a dialog */
TalkID OldActiveStore;
/** Temporary item used to hold the item being traded */
Item TempItem;
/** Maps from towner IDs to NPC names. */
const char *const TownerNames[] = {
N_("Griswold"),
N_("Pepin"),
"",
N_("Ogden"),
N_("Cain"),
N_("Farnham"),
N_("Adria"),
N_("Gillian"),
N_("Wirt"),
};
constexpr int PaddingTop = 32;
// For most languages, line height is always 12.
// This includes blank lines and divider line.
constexpr int SmallLineHeight = 12;
constexpr int SmallTextHeight = 12;
// For larger small fonts (Chinese and Japanese), text lines are
// taller and overflow.
// We space out blank lines a bit more to give space to 3-line store items.
constexpr int LargeLineHeight = SmallLineHeight + 1;
constexpr int LargeTextHeight = 18;
/**
* The line index with the Back / Leave button.
* This is a special button that is always the last line.
*
* For lists with a scrollbar, it is not selectable (mouse-only).
*/
int BackButtonLine()
{
if (IsSmallFontTall()) {
return HasScrollbar ? 21 : 20;
}
return 22;
}
int LineHeight()
{
return IsSmallFontTall() ? LargeLineHeight : SmallLineHeight;
}
int TextHeight()
{
return IsSmallFontTall() ? LargeTextHeight : SmallTextHeight;
}
void CalculateLineHeights()
{
TextLine[0].y = 0;
if (IsSmallFontTall()) {
for (int i = 1; i < STORE_LINES; ++i) {
// Space out consecutive text lines, unless they are both selectable (never the case currently).
if (TextLine[i].hasText() && TextLine[i - 1].hasText() && !(TextLine[i].isSelectable() && TextLine[i - 1].isSelectable())) {
TextLine[i].y = TextLine[i - 1].y + LargeTextHeight;
} else {
TextLine[i].y = i * LargeLineHeight;
}
}
} else {
for (int i = 1; i < STORE_LINES; ++i) {
TextLine[i].y = i * SmallLineHeight;
}
}
}
void DrawSTextBack(const Surface &out)
{
const Point uiPosition = GetUIRectangle().position;
ClxDraw(out, { uiPosition.x + 320 + 24, 327 + uiPosition.y }, (*pSTextBoxCels)[0]);
DrawHalfTransparentRectTo(out, uiPosition.x + 347, uiPosition.y + 28, 265, 297);
}
void DrawSSlider(const Surface &out, int y1, int y2)
{
const Point uiPosition = GetUIRectangle().position;
int yd1 = y1 * 12 + 44 + uiPosition.y;
int yd2 = y2 * 12 + 44 + uiPosition.y;
if (CountdownScrollUp != -1)
ClxDraw(out, { uiPosition.x + 601, yd1 }, (*pSTextSlidCels)[11]);
else
ClxDraw(out, { uiPosition.x + 601, yd1 }, (*pSTextSlidCels)[9]);
if (CountdownScrollDown != -1)
ClxDraw(out, { uiPosition.x + 601, yd2 }, (*pSTextSlidCels)[10]);
else
ClxDraw(out, { uiPosition.x + 601, yd2 }, (*pSTextSlidCels)[8]);
yd1 += 12;
int yd3 = yd1;
for (; yd3 < yd2; yd3 += 12) {
ClxDraw(out, { uiPosition.x + 601, yd3 }, (*pSTextSlidCels)[13]);
}
if (CurrentTextLine == BackButtonLine())
yd3 = OldTextLine;
else
yd3 = CurrentTextLine;
if (CurrentItemIndex > 1)
yd3 = 1000 * (ScrollPos + ((yd3 - PreviousScrollPos) / 4)) / (CurrentItemIndex - 1) * (y2 * 12 - y1 * 12 - 24) / 1000;
else
yd3 = 0;
ClxDraw(out, { uiPosition.x + 601, (y1 + 1) * 12 + 44 + uiPosition.y + yd3 }, (*pSTextSlidCels)[12]);
}
void AddSLine(size_t y)
{
TextLine[y]._sx = 0;
TextLine[y]._syoff = 0;
TextLine[y].text.clear();
TextLine[y].text.shrink_to_fit();
TextLine[y].type = STextStruct::Divider;
TextLine[y].cursId = -1;
TextLine[y].cursIndent = false;
}
void AddSTextVal(size_t y, int val)
{
TextLine[y]._sval = val;
}
void AddSText(uint8_t x, size_t y, std::string_view text, UiFlags flags, bool sel, int cursId = -1, bool cursIndent = false)
{
TextLine[y]._sx = x;
TextLine[y]._syoff = 0;
TextLine[y].text.clear();
TextLine[y].text.append(text);
TextLine[y].flags = flags;
TextLine[y].type = sel ? STextStruct::Selectable : STextStruct::Label;
TextLine[y].cursId = cursId;
TextLine[y].cursIndent = cursIndent;
}
void AddOptionsBackButton()
{
const int line = BackButtonLine();
AddSText(0, line, _("Back"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
TextLine[line]._syoff = IsSmallFontTall() ? 0 : 6;
}
void AddItemListBackButton(bool selectable = false)
{
const int line = BackButtonLine();
std::string_view text = _("Back");
if (!selectable && IsSmallFontTall()) {
AddSText(0, line, text, UiFlags::ColorWhite | UiFlags::AlignRight, selectable);
} else {
AddSLine(line - 1);
AddSText(0, line, text, UiFlags::ColorWhite | UiFlags::AlignCenter, selectable);
TextLine[line]._syoff = 6;
}
}
void PrintStoreItem(const Item &item, int l, UiFlags flags, bool cursIndent = false)
{
std::string productLine;
if (item._iIdentified) {
if (item._iMagical != ITEM_QUALITY_UNIQUE) {
if (item._iPrePower != -1) {
productLine.append(PrintItemPower(item._iPrePower, item));
}
}
if (item._iSufPower != -1) {
if (!productLine.empty())
productLine.append(_(", "));
productLine.append(PrintItemPower(item._iSufPower, item));
}
}
if (item._iMiscId == IMISC_STAFF && item._iMaxCharges != 0) {
if (!productLine.empty())
productLine.append(_(", "));
productLine.append(fmt::format(fmt::runtime(_("Charges: {:d}/{:d}")), item._iCharges, item._iMaxCharges));
}
if (!productLine.empty()) {
AddSText(40, l, productLine, flags, false, -1, cursIndent);
l++;
productLine.clear();
}
if (item._itype != ItemType::Misc) {
if (item._iClass == ICLASS_WEAPON)
productLine = fmt::format(fmt::runtime(_("Damage: {:d}-{:d} ")), item._iMinDam, item._iMaxDam);
else if (item._iClass == ICLASS_ARMOR)
productLine = fmt::format(fmt::runtime(_("Armor: {:d} ")), item._iAC);
if (item._iMaxDur != DUR_INDESTRUCTIBLE && item._iMaxDur != 0)
productLine += fmt::format(fmt::runtime(_("Dur: {:d}/{:d}")), item._iDurability, item._iMaxDur);
else
productLine.append(_("Indestructible"));
}
int8_t str = item._iMinStr;
uint8_t mag = item._iMinMag;
int8_t dex = item._iMinDex;
if (str != 0 || mag != 0 || dex != 0) {
if (!productLine.empty())
productLine.append(_(", "));
productLine.append(_("Required:"));
if (str != 0)
productLine.append(fmt::format(fmt::runtime(_(" {:d} Str")), str));
if (mag != 0)
productLine.append(fmt::format(fmt::runtime(_(" {:d} Mag")), mag));
if (dex != 0)
productLine.append(fmt::format(fmt::runtime(_(" {:d} Dex")), dex));
}
AddSText(40, l++, productLine, flags, false, -1, cursIndent);
}
bool StoreAutoPlace(Item &item, bool persistItem)
{
Player &player = *MyPlayer;
if (AutoEquipEnabled(player, item) && AutoEquip(player, item, persistItem, true)) {
return true;
}
if (AutoPlaceItemInBelt(player, item, persistItem, true)) {
return true;
}
if (persistItem) {
return AutoPlaceItemInInventory(player, item, true);
}
return CanFitItemInInventory(player, item);
}
void ScrollVendorStore(Item *itemData, int storeLimit, int idx, int selling = true)
{
ClearSText(5, 21);
PreviousScrollPos = 5;
for (int l = 5; l < 20 && idx < storeLimit; l += 4) {
const Item &item = itemData[idx];
if (!item.isEmpty()) {
UiFlags itemColor = item.getTextColorWithStatCheck();
AddSText(20, l, item.getName(), itemColor, true, item._iCurs, true);
AddSTextVal(l, item._iIdentified ? item._iIvalue : item._ivalue);
PrintStoreItem(item, l + 1, itemColor, true);
NextScrollPos = l;
} else {
l -= 4;
}
idx++;
}
if (selling) {
if (CurrentTextLine != -1 && !TextLine[CurrentTextLine].isSelectable() && CurrentTextLine != BackButtonLine())
CurrentTextLine = NextScrollPos;
} else {
NumTextLines = std::max(static_cast<int>(storeLimit) - 4, 0);
}
}
void StartSmith()
{
IsTextFullSize = false;
HasScrollbar = false;
AddSText(0, 1, _("Welcome to the"), UiFlags::ColorWhitegold | UiFlags::AlignCenter, false);
AddSText(0, 3, _("Blacksmith's shop"), UiFlags::ColorWhitegold | UiFlags::AlignCenter, false);
AddSText(0, 7, _("Would you like to:"), UiFlags::ColorWhitegold | UiFlags::AlignCenter, false);
AddSText(0, 10, _("Talk to Griswold"), UiFlags::ColorBlue | UiFlags::AlignCenter, true);
AddSText(0, 12, _("Buy basic items"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSText(0, 14, _("Buy premium items"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSText(0, 16, _("Sell items"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSText(0, 18, _("Repair items"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSText(0, 20, _("Leave the shop"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSLine(5);
CurrentItemIndex = 20;
}
void ScrollSmithBuy(int idx)
{
ScrollVendorStore(SmithItems, static_cast<int>(std::size(SmithItems)), idx);
}
uint32_t TotalPlayerGold()
{
return MyPlayer->_pGold + Stash.gold;
}
// TODO: Change `_iIvalue` to be unsigned instead of passing `int` here.
bool PlayerCanAfford(int price)
{
return TotalPlayerGold() >= static_cast<uint32_t>(price);
}
void StartSmithBuy()
{
IsTextFullSize = true;
HasScrollbar = true;
ScrollPos = 0;
RenderGold = true;
AddSText(20, 1, _("I have these items for sale:"), UiFlags::ColorWhitegold, false);
AddSLine(3);
ScrollSmithBuy(ScrollPos);
AddItemListBackButton();
CurrentItemIndex = 0;
for (Item &item : SmithItems) {
if (item.isEmpty())
continue;
item._iStatFlag = MyPlayer->CanUseItem(item);
CurrentItemIndex++;
}
NumTextLines = std::max(CurrentItemIndex - 4, 0);
}
void ScrollSmithPremiumBuy(int boughtitems)
{
int idx = 0;
for (; boughtitems != 0; idx++) {
if (!PremiumItems[idx].isEmpty())
boughtitems--;
}
ScrollVendorStore(PremiumItems, static_cast<int>(std::size(PremiumItems)), idx);
}
bool StartSmithPremiumBuy()
{
CurrentItemIndex = 0;
for (Item &item : PremiumItems) {
if (item.isEmpty())
continue;
item._iStatFlag = MyPlayer->CanUseItem(item);
CurrentItemIndex++;
}
if (CurrentItemIndex == 0) {
StartStore(TalkID::Smith);
CurrentTextLine = 14;
return false;
}
IsTextFullSize = true;
HasScrollbar = true;
ScrollPos = 0;
RenderGold = true;
AddSText(20, 1, _("I have these premium items for sale:"), UiFlags::ColorWhitegold, false);
AddSLine(3);
AddItemListBackButton();
NumTextLines = std::max(CurrentItemIndex - 4, 0);
ScrollSmithPremiumBuy(ScrollPos);
return true;
}
bool SmithSellOk(int i)
{
Item *pI;
if (i >= 0) {
pI = &MyPlayer->InvList[i];
} else {
pI = &MyPlayer->SpdList[-(i + 1)];
}
if (pI->isEmpty())
return false;
if (pI->_iMiscId > IMISC_OILFIRST && pI->_iMiscId < IMISC_OILLAST)
return true;
if (pI->_itype == ItemType::Misc)
return false;
if (pI->_itype == ItemType::Gold)
return false;
if (pI->_itype == ItemType::Staff && (!gbIsHellfire || IsValidSpell(pI->_iSpell)))
return false;
if (pI->_iClass == ICLASS_QUEST)
return false;
if (pI->IDidx == IDI_LAZSTAFF)
return false;
return true;
}
void ScrollSmithSell(int idx)
{
ScrollVendorStore(PlayerItems, CurrentItemIndex, idx, false);
}
void StartSmithSell()
{
IsTextFullSize = true;
bool sellOk = false;
CurrentItemIndex = 0;
for (auto &item : PlayerItems) {
item.clear();
}
const Player &myPlayer = *MyPlayer;
for (int8_t i = 0; i < myPlayer._pNumInv; i++) {
if (CurrentItemIndex >= 48)
break;
if (SmithSellOk(i)) {
sellOk = true;
PlayerItems[CurrentItemIndex] = myPlayer.InvList[i];
if (PlayerItems[CurrentItemIndex]._iMagical != ITEM_QUALITY_NORMAL && PlayerItems[CurrentItemIndex]._iIdentified)
PlayerItems[CurrentItemIndex]._ivalue = PlayerItems[CurrentItemIndex]._iIvalue;
PlayerItems[CurrentItemIndex]._ivalue = std::max(PlayerItems[CurrentItemIndex]._ivalue / 4, 1);
PlayerItems[CurrentItemIndex]._iIvalue = PlayerItems[CurrentItemIndex]._ivalue;
PlayerItemIndexes[CurrentItemIndex] = i;
CurrentItemIndex++;
}
}
for (int i = 0; i < MaxBeltItems; i++) {
if (CurrentItemIndex >= 48)
break;
if (SmithSellOk(-(i + 1))) {
sellOk = true;
PlayerItems[CurrentItemIndex] = myPlayer.SpdList[i];
if (PlayerItems[CurrentItemIndex]._iMagical != ITEM_QUALITY_NORMAL && PlayerItems[CurrentItemIndex]._iIdentified)
PlayerItems[CurrentItemIndex]._ivalue = PlayerItems[CurrentItemIndex]._iIvalue;
PlayerItems[CurrentItemIndex]._ivalue = std::max(PlayerItems[CurrentItemIndex]._ivalue / 4, 1);
PlayerItems[CurrentItemIndex]._iIvalue = PlayerItems[CurrentItemIndex]._ivalue;
PlayerItemIndexes[CurrentItemIndex] = -(i + 1);
CurrentItemIndex++;
}
}
if (!sellOk) {
HasScrollbar = false;
RenderGold = true;
AddSText(20, 1, _("You have nothing I want."), UiFlags::ColorWhitegold, false);
AddSLine(3);
AddItemListBackButton(/*selectable=*/true);
return;
}
HasScrollbar = true;
ScrollPos = 0;
NumTextLines = myPlayer._pNumInv;
RenderGold = true;
AddSText(20, 1, _("Which item is for sale?"), UiFlags::ColorWhitegold, false);
AddSLine(3);
ScrollSmithSell(ScrollPos);
AddItemListBackButton();
}
bool SmithRepairOk(int i)
{
const Player &myPlayer = *MyPlayer;
const Item &item = myPlayer.InvList[i];
if (item.isEmpty())
return false;
if (item._itype == ItemType::Misc)
return false;
if (item._itype == ItemType::Gold)
return false;
if (item._iDurability == item._iMaxDur)
return false;
if (item._iMaxDur == DUR_INDESTRUCTIBLE)
return false;
return true;
}
void StartSmithRepair()
{
IsTextFullSize = true;
CurrentItemIndex = 0;
for (auto &item : PlayerItems) {
item.clear();
}
Player &myPlayer = *MyPlayer;
auto &helmet = myPlayer.InvBody[INVLOC_HEAD];
if (!helmet.isEmpty() && helmet._iDurability != helmet._iMaxDur) {
AddStoreHoldRepair(&helmet, -1);
}
auto &armor = myPlayer.InvBody[INVLOC_CHEST];
if (!armor.isEmpty() && armor._iDurability != armor._iMaxDur) {
AddStoreHoldRepair(&armor, -2);
}
auto &leftHand = myPlayer.InvBody[INVLOC_HAND_LEFT];
if (!leftHand.isEmpty() && leftHand._iDurability != leftHand._iMaxDur) {
AddStoreHoldRepair(&leftHand, -3);
}
auto &rightHand = myPlayer.InvBody[INVLOC_HAND_RIGHT];
if (!rightHand.isEmpty() && rightHand._iDurability != rightHand._iMaxDur) {
AddStoreHoldRepair(&rightHand, -4);
}
for (int i = 0; i < myPlayer._pNumInv; i++) {
if (CurrentItemIndex >= 48)
break;
if (SmithRepairOk(i)) {
AddStoreHoldRepair(&myPlayer.InvList[i], i);
}
}
if (CurrentItemIndex == 0) {
HasScrollbar = false;
RenderGold = true;
AddSText(20, 1, _("You have nothing to repair."), UiFlags::ColorWhitegold, false);
AddSLine(3);
AddItemListBackButton(/*selectable=*/true);
return;
}
HasScrollbar = true;
ScrollPos = 0;
NumTextLines = myPlayer._pNumInv;
RenderGold = true;
AddSText(20, 1, _("Repair which item?"), UiFlags::ColorWhitegold, false);
AddSLine(3);
ScrollSmithSell(ScrollPos);
AddItemListBackButton();
}
void FillManaPlayer()
{
if (!*sgOptions.Gameplay.adriaRefillsMana)
return;
Player &myPlayer = *MyPlayer;
if (myPlayer._pMana != myPlayer._pMaxMana) {
PlaySFX(SfxID::CastHealing);
}
myPlayer._pMana = myPlayer._pMaxMana;
myPlayer._pManaBase = myPlayer._pMaxManaBase;
RedrawComponent(PanelDrawComponent::Mana);
}
void StartWitch()
{
FillManaPlayer();
IsTextFullSize = false;
HasScrollbar = false;
AddSText(0, 2, _("Witch's shack"), UiFlags::ColorWhitegold | UiFlags::AlignCenter, false);
AddSText(0, 9, _("Would you like to:"), UiFlags::ColorWhitegold | UiFlags::AlignCenter, false);
AddSText(0, 12, _("Talk to Adria"), UiFlags::ColorBlue | UiFlags::AlignCenter, true);
AddSText(0, 14, _("Buy items"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSText(0, 16, _("Sell items"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSText(0, 18, _("Recharge staves"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSText(0, 20, _("Leave the shack"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSLine(5);
CurrentItemIndex = 20;
}
void ScrollWitchBuy(int idx)
{
ScrollVendorStore(WitchItems, static_cast<int>(std::size(WitchItems)), idx);
}
void WitchBookLevel(Item &bookItem)
{
if (bookItem._iMiscId != IMISC_BOOK)
return;
bookItem._iMinMag = GetSpellData(bookItem._iSpell).minInt;
uint8_t spellLevel = MyPlayer->_pSplLvl[static_cast<int8_t>(bookItem._iSpell)];
while (spellLevel > 0) {
bookItem._iMinMag += 20 * bookItem._iMinMag / 100;
spellLevel--;
if (bookItem._iMinMag + 20 * bookItem._iMinMag / 100 > 255) {
bookItem._iMinMag = 255;
spellLevel = 0;
}
}
}
void StartWitchBuy()
{
IsTextFullSize = true;
HasScrollbar = true;
ScrollPos = 0;
NumTextLines = 20;
RenderGold = true;
AddSText(20, 1, _("I have these items for sale:"), UiFlags::ColorWhitegold, false);
AddSLine(3);
ScrollWitchBuy(ScrollPos);
AddItemListBackButton();
CurrentItemIndex = 0;
for (Item &item : WitchItems) {
if (item.isEmpty())
continue;
WitchBookLevel(item);
item._iStatFlag = MyPlayer->CanUseItem(item);
CurrentItemIndex++;
}
NumTextLines = std::max(CurrentItemIndex - 4, 0);
}
bool WitchSellOk(int i)
{
Item *pI;
bool rv = false;
if (i >= 0)
pI = &MyPlayer->InvList[i];
else
pI = &MyPlayer->SpdList[-(i + 1)];
if (pI->_itype == ItemType::Misc)
rv = true;
if (pI->_iMiscId > 29 && pI->_iMiscId < 41)
rv = false;
if (pI->_iClass == ICLASS_QUEST)
rv = false;
if (pI->_itype == ItemType::Staff && (!gbIsHellfire || IsValidSpell(pI->_iSpell)))
rv = true;
if (pI->IDidx >= IDI_FIRSTQUEST && pI->IDidx <= IDI_LASTQUEST)
rv = false;
if (pI->IDidx == IDI_LAZSTAFF)
rv = false;
return rv;
}
void StartWitchSell()
{
IsTextFullSize = true;
bool sellok = false;
CurrentItemIndex = 0;
for (auto &item : PlayerItems) {
item.clear();
}
const Player &myPlayer = *MyPlayer;
for (int i = 0; i < myPlayer._pNumInv; i++) {
if (CurrentItemIndex >= 48)
break;
if (WitchSellOk(i)) {
sellok = true;
PlayerItems[CurrentItemIndex] = myPlayer.InvList[i];
if (PlayerItems[CurrentItemIndex]._iMagical != ITEM_QUALITY_NORMAL && PlayerItems[CurrentItemIndex]._iIdentified)
PlayerItems[CurrentItemIndex]._ivalue = PlayerItems[CurrentItemIndex]._iIvalue;
PlayerItems[CurrentItemIndex]._ivalue = std::max(PlayerItems[CurrentItemIndex]._ivalue / 4, 1);
PlayerItems[CurrentItemIndex]._iIvalue = PlayerItems[CurrentItemIndex]._ivalue;
PlayerItemIndexes[CurrentItemIndex] = i;
CurrentItemIndex++;
}
}
for (int i = 0; i < MaxBeltItems; i++) {
if (CurrentItemIndex >= 48)
break;
if (!myPlayer.SpdList[i].isEmpty() && WitchSellOk(-(i + 1))) {
sellok = true;
PlayerItems[CurrentItemIndex] = myPlayer.SpdList[i];
if (PlayerItems[CurrentItemIndex]._iMagical != ITEM_QUALITY_NORMAL && PlayerItems[CurrentItemIndex]._iIdentified)
PlayerItems[CurrentItemIndex]._ivalue = PlayerItems[CurrentItemIndex]._iIvalue;
PlayerItems[CurrentItemIndex]._ivalue = std::max(PlayerItems[CurrentItemIndex]._ivalue / 4, 1);
PlayerItems[CurrentItemIndex]._iIvalue = PlayerItems[CurrentItemIndex]._ivalue;
PlayerItemIndexes[CurrentItemIndex] = -(i + 1);
CurrentItemIndex++;
}
}
if (!sellok) {
HasScrollbar = false;
RenderGold = true;
AddSText(20, 1, _("You have nothing I want."), UiFlags::ColorWhitegold, false);
AddSLine(3);
AddItemListBackButton(/*selectable=*/true);
return;
}
HasScrollbar = true;
ScrollPos = 0;
NumTextLines = myPlayer._pNumInv;
RenderGold = true;
AddSText(20, 1, _("Which item is for sale?"), UiFlags::ColorWhitegold, false);
AddSLine(3);
ScrollSmithSell(ScrollPos);
AddItemListBackButton();
}
bool WitchRechargeOk(int i)
{
const auto &item = MyPlayer->InvList[i];
if (item._itype == ItemType::Staff && item._iCharges != item._iMaxCharges) {
return true;
}
if ((item._iMiscId == IMISC_UNIQUE || item._iMiscId == IMISC_STAFF) && item._iCharges < item._iMaxCharges) {
return true;
}
return false;
}
void AddStoreHoldRecharge(Item itm, int8_t i)
{
PlayerItems[CurrentItemIndex] = itm;
PlayerItems[CurrentItemIndex]._ivalue += GetSpellData(itm._iSpell).staffCost();
PlayerItems[CurrentItemIndex]._ivalue = PlayerItems[CurrentItemIndex]._ivalue * (PlayerItems[CurrentItemIndex]._iMaxCharges - PlayerItems[CurrentItemIndex]._iCharges) / (PlayerItems[CurrentItemIndex]._iMaxCharges * 2);
PlayerItems[CurrentItemIndex]._iIvalue = PlayerItems[CurrentItemIndex]._ivalue;
PlayerItemIndexes[CurrentItemIndex] = i;
CurrentItemIndex++;
}
void StartWitchRecharge()
{
IsTextFullSize = true;
bool rechargeok = false;
CurrentItemIndex = 0;
for (auto &item : PlayerItems) {
item.clear();
}
const Player &myPlayer = *MyPlayer;
const auto &leftHand = myPlayer.InvBody[INVLOC_HAND_LEFT];
if ((leftHand._itype == ItemType::Staff || leftHand._iMiscId == IMISC_UNIQUE) && leftHand._iCharges != leftHand._iMaxCharges) {
rechargeok = true;
AddStoreHoldRecharge(leftHand, -1);
}
for (int i = 0; i < myPlayer._pNumInv; i++) {
if (CurrentItemIndex >= 48)
break;
if (WitchRechargeOk(i)) {
rechargeok = true;
AddStoreHoldRecharge(myPlayer.InvList[i], i);
}
}
if (!rechargeok) {
HasScrollbar = false;
RenderGold = true;
AddSText(20, 1, _("You have nothing to recharge."), UiFlags::ColorWhitegold, false);
AddSLine(3);
AddItemListBackButton(/*selectable=*/true);
return;
}
HasScrollbar = true;
ScrollPos = 0;
NumTextLines = myPlayer._pNumInv;
RenderGold = true;
AddSText(20, 1, _("Recharge which item?"), UiFlags::ColorWhitegold, false);
AddSLine(3);
ScrollSmithSell(ScrollPos);
AddItemListBackButton();
}
void StoreNoMoney()
{
StartStore(OldActiveStore);
HasScrollbar = false;
IsTextFullSize = true;
RenderGold = true;
ClearSText(5, 23);
AddSText(0, 14, _("You do not have enough gold"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
}
void StoreNoRoom()
{
StartStore(OldActiveStore);
HasScrollbar = false;
ClearSText(5, 23);
AddSText(0, 14, _("You do not have enough room in inventory"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
}
void StoreConfirm(Item &item)
{
StartStore(OldActiveStore);
HasScrollbar = false;
ClearSText(5, 23);
UiFlags itemColor = item.getTextColorWithStatCheck();
AddSText(20, 8, item.getName(), itemColor, false);
AddSTextVal(8, item._iIvalue);
PrintStoreItem(item, 9, itemColor);
std::string_view prompt;
switch (OldActiveStore) {
case TalkID::BoyBuy:
prompt = _("Do we have a deal?");
break;
case TalkID::StorytellerIdentify:
prompt = _("Are you sure you want to identify this item?");
break;
case TalkID::HealerBuy:
case TalkID::SmithPremiumBuy:
case TalkID::WitchBuy:
case TalkID::SmithBuy:
prompt = _("Are you sure you want to buy this item?");
break;
case TalkID::WitchRecharge:
prompt = _("Are you sure you want to recharge this item?");
break;
case TalkID::SmithSell:
case TalkID::WitchSell:
prompt = _("Are you sure you want to sell this item?");
break;
case TalkID::SmithRepair:
prompt = _("Are you sure you want to repair this item?");
break;
default:
app_fatal(StrCat("Unknown store dialog ", static_cast<int>(OldActiveStore)));
}
AddSText(0, 15, prompt, UiFlags::ColorWhite | UiFlags::AlignCenter, false);
AddSText(0, 18, _("Yes"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSText(0, 20, _("No"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
}
void StartBoy()
{
IsTextFullSize = false;
HasScrollbar = false;
AddSText(0, 2, _("Wirt the Peg-legged boy"), UiFlags::ColorWhitegold | UiFlags::AlignCenter, false);
AddSLine(5);
if (!BoyItem.isEmpty()) {
AddSText(0, 8, _("Talk to Wirt"), UiFlags::ColorBlue | UiFlags::AlignCenter, true);
AddSText(0, 12, _("I have something for sale,"), UiFlags::ColorWhitegold | UiFlags::AlignCenter, false);
AddSText(0, 14, _("but it will cost 50 gold"), UiFlags::ColorWhitegold | UiFlags::AlignCenter, false);
AddSText(0, 16, _("just to take a look. "), UiFlags::ColorWhitegold | UiFlags::AlignCenter, false);
AddSText(0, 18, _("What have you got?"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
AddSText(0, 20, _("Say goodbye"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
} else {
AddSText(0, 12, _("Talk to Wirt"), UiFlags::ColorBlue | UiFlags::AlignCenter, true);
AddSText(0, 18, _("Say goodbye"), UiFlags::ColorWhite | UiFlags::AlignCenter, true);
}
}
void SStartBoyBuy()
{
IsTextFullSize = true;
HasScrollbar = false;
RenderGold = true;
AddSText(20, 1, _("I have this item for sale:"), UiFlags::ColorWhitegold, false);
AddSLine(3);
BoyItem._iStatFlag = MyPlayer->CanUseItem(BoyItem);
UiFlags itemColor = BoyItem.getTextColorWithStatCheck();
AddSText(20, 10, BoyItem.getName(), itemColor, true, BoyItem._iCurs, true);
if (gbIsHellfire)