-
Notifications
You must be signed in to change notification settings - Fork 804
/
inv.cpp
2264 lines (1935 loc) · 70.3 KB
/
inv.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 inv.cpp
*
* Implementation of player inventory.
*/
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <optional>
#include <utility>
#include <fmt/format.h>
#include "DiabloUI/ui_flags.hpp"
#include "controls/plrctrls.h"
#include "cursor.h"
#include "engine/backbuffer_state.hpp"
#include "engine/clx_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/palette.h"
#include "engine/render/clx_render.hpp"
#include "engine/render/text_render.hpp"
#include "engine/size.hpp"
#include "hwcursor.hpp"
#include "inv_iterators.hpp"
#include "levels/town.h"
#include "minitext.h"
#include "options.h"
#include "panels/ui_panels.hpp"
#include "player.h"
#include "plrmsg.h"
#include "qol/stash.h"
#include "stores.h"
#include "towners.h"
#include "utils/format_int.hpp"
#include "utils/language.h"
#include "utils/sdl_geometry.h"
#include "utils/str_cat.hpp"
#include "utils/utf8.hpp"
namespace devilution {
bool invflag;
/**
* Maps from inventory slot to screen position. The inventory slots are
* arranged as follows:
*
* @code{.unparsed}
* 00 00
* 00 00 03
*
* 04 04 06 06 05 05
* 04 04 06 06 05 05
* 04 04 06 06 05 05
*
* 01 02
*
* 07 08 09 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
* @endcode
*/
const Rectangle InvRect[] = {
// clang-format off
//{ X, Y }, { W, H }
{ { 132, 2 }, { 58, 59 } }, // helmet
{ { 47, 177 }, { 28, 29 } }, // left ring
{ { 248, 177 }, { 28, 29 } }, // right ring
{ { 205, 32 }, { 28, 29 } }, // amulet
{ { 17, 75 }, { 58, 86 } }, // left hand
{ { 248, 75 }, { 58, 87 } }, // right hand
{ { 132, 75 }, { 58, 87 } }, // chest
{ { 17, 222 }, { 29, 29 } }, // inv row 1
{ { 46, 222 }, { 29, 29 } }, // inv row 1
{ { 75, 222 }, { 29, 29 } }, // inv row 1
{ { 104, 222 }, { 29, 29 } }, // inv row 1
{ { 133, 222 }, { 29, 29 } }, // inv row 1
{ { 162, 222 }, { 29, 29 } }, // inv row 1
{ { 191, 222 }, { 29, 29 } }, // inv row 1
{ { 220, 222 }, { 29, 29 } }, // inv row 1
{ { 249, 222 }, { 29, 29 } }, // inv row 1
{ { 278, 222 }, { 29, 29 } }, // inv row 1
{ { 17, 251 }, { 29, 29 } }, // inv row 2
{ { 46, 251 }, { 29, 29 } }, // inv row 2
{ { 75, 251 }, { 29, 29 } }, // inv row 2
{ { 104, 251 }, { 29, 29 } }, // inv row 2
{ { 133, 251 }, { 29, 29 } }, // inv row 2
{ { 162, 251 }, { 29, 29 } }, // inv row 2
{ { 191, 251 }, { 29, 29 } }, // inv row 2
{ { 220, 251 }, { 29, 29 } }, // inv row 2
{ { 249, 251 }, { 29, 29 } }, // inv row 2
{ { 278, 251 }, { 29, 29 } }, // inv row 2
{ { 17, 280 }, { 29, 29 } }, // inv row 3
{ { 46, 280 }, { 29, 29 } }, // inv row 3
{ { 75, 280 }, { 29, 29 } }, // inv row 3
{ { 104, 280 }, { 29, 29 } }, // inv row 3
{ { 133, 280 }, { 29, 29 } }, // inv row 3
{ { 162, 280 }, { 29, 29 } }, // inv row 3
{ { 191, 280 }, { 29, 29 } }, // inv row 3
{ { 220, 280 }, { 29, 29 } }, // inv row 3
{ { 249, 280 }, { 29, 29 } }, // inv row 3
{ { 278, 280 }, { 29, 29 } }, // inv row 3
{ { 17, 309 }, { 29, 29 } }, // inv row 4
{ { 46, 309 }, { 29, 29 } }, // inv row 4
{ { 75, 309 }, { 29, 29 } }, // inv row 4
{ { 104, 309 }, { 29, 29 } }, // inv row 4
{ { 133, 309 }, { 29, 29 } }, // inv row 4
{ { 162, 309 }, { 29, 29 } }, // inv row 4
{ { 191, 309 }, { 29, 29 } }, // inv row 4
{ { 220, 309 }, { 29, 29 } }, // inv row 4
{ { 249, 309 }, { 29, 29 } }, // inv row 4
{ { 278, 309 }, { 29, 29 } }, // inv row 4
{ { 205, 5 }, { 29, 29 } }, // belt
{ { 234, 5 }, { 29, 29 } }, // belt
{ { 263, 5 }, { 29, 29 } }, // belt
{ { 292, 5 }, { 29, 29 } }, // belt
{ { 321, 5 }, { 29, 29 } }, // belt
{ { 350, 5 }, { 29, 29 } }, // belt
{ { 379, 5 }, { 29, 29 } }, // belt
{ { 408, 5 }, { 29, 29 } } // belt
// clang-format on
};
namespace {
OptionalOwnedClxSpriteList pInvCels;
/**
* @brief Adds an item to a player's InvGrid array
* @param player The player reference
* @param invGridIndex Item's position in InvGrid (this should be the item's topleft grid tile)
* @param invListIndex The item's InvList index (it's expected this already has +1 added to it since InvGrid can't store a 0 index)
* @param itemSize Size of item
*/
void AddItemToInvGrid(Player &player, int invGridIndex, int invListIndex, Size itemSize, bool sendNetworkMessage)
{
const int pitch = 10;
for (int y = 0; y < itemSize.height; y++) {
int rowGridIndex = invGridIndex + pitch * y;
for (int x = 0; x < itemSize.width; x++) {
if (x == 0 && y == itemSize.height - 1)
player.InvGrid[rowGridIndex + x] = invListIndex;
else
player.InvGrid[rowGridIndex + x] = -invListIndex;
}
}
if (sendNetworkMessage) {
NetSendCmdChInvItem(false, invGridIndex);
}
}
/**
* @brief Checks whether the given item can fit in a belt slot (i.e. the item's size in inventory cells is 1x1).
* @param item The item to be checked.
* @return 'True' in case the item can fit a belt slot and 'False' otherwise.
*/
bool FitsInBeltSlot(const Item &item)
{
return GetInventorySize(item) == Size { 1, 1 };
}
/**
* @brief Checks whether the given item can be equipped. Since this overload doesn't take player information, it only considers
* general aspects about the item, like if its requirements are met and if the item's target location is valid for the body.
* @param item The item to check.
* @return 'True' in case the item could be equipped in a player, and 'False' otherwise.
*/
bool CanEquip(const Item &item)
{
return item.isEquipment()
&& item._iStatFlag;
}
/**
* @brief A specialized version of 'CanEquip(int, Item&, int)' that specifically checks whether the item can be equipped
* in one/both of the player's hands.
* @param player The player whose inventory will be checked for compatibility with the item.
* @param item The item to check.
* @return 'True' if the player can currently equip the item in either one of his hands (i.e. the required hands are empty and
* allow the item), and 'False' otherwise.
*/
bool CanWield(Player &player, const Item &item)
{
if (!CanEquip(item) || IsNoneOf(player.GetItemLocation(item), ILOC_ONEHAND, ILOC_TWOHAND))
return false;
Item &leftHandItem = player.InvBody[INVLOC_HAND_LEFT];
Item &rightHandItem = player.InvBody[INVLOC_HAND_RIGHT];
if (leftHandItem.isEmpty() && rightHandItem.isEmpty()) {
return true;
}
if (!leftHandItem.isEmpty() && !rightHandItem.isEmpty()) {
return false;
}
Item &occupiedHand = !leftHandItem.isEmpty() ? leftHandItem : rightHandItem;
// Bard can dual wield swords and maces, so we allow equiping one-handed weapons in her free slot as long as her occupied
// slot is another one-handed weapon.
if (player._pClass == HeroClass::Bard) {
bool occupiedHandIsOneHandedSwordOrMace = player.GetItemLocation(occupiedHand) == ILOC_ONEHAND
&& IsAnyOf(occupiedHand._itype, ItemType::Sword, ItemType::Mace);
bool weaponToEquipIsOneHandedSwordOrMace = player.GetItemLocation(item) == ILOC_ONEHAND
&& IsAnyOf(item._itype, ItemType::Sword, ItemType::Mace);
if (occupiedHandIsOneHandedSwordOrMace && weaponToEquipIsOneHandedSwordOrMace) {
return true;
}
}
return player.GetItemLocation(item) == ILOC_ONEHAND
&& player.GetItemLocation(occupiedHand) == ILOC_ONEHAND
&& item._iClass != occupiedHand._iClass;
}
/**
* @brief Checks whether the specified item can be equipped in the desired body location on the player.
* @param player The player whose inventory will be checked for compatibility with the item.
* @param item The item to check.
* @param bodyLocation The location in the inventory to be checked against.
* @return 'True' if the player can currently equip the item in the specified body location (i.e. the body location is empty and
* allows the item), and 'False' otherwise.
*/
bool CanEquip(Player &player, const Item &item, inv_body_loc bodyLocation)
{
if (!CanEquip(item) || player._pmode > PM_WALK_SIDEWAYS || !player.InvBody[bodyLocation].isEmpty()) {
return false;
}
switch (bodyLocation) {
case INVLOC_AMULET:
return item._iLoc == ILOC_AMULET;
case INVLOC_CHEST:
return item._iLoc == ILOC_ARMOR;
case INVLOC_HAND_LEFT:
case INVLOC_HAND_RIGHT:
return CanWield(player, item);
case INVLOC_HEAD:
return item._iLoc == ILOC_HELM;
case INVLOC_RING_LEFT:
case INVLOC_RING_RIGHT:
return item._iLoc == ILOC_RING;
default:
return false;
}
}
void ChangeEquipment(Player &player, inv_body_loc bodyLocation, const Item &item, bool sendNetworkMessage)
{
player.InvBody[bodyLocation] = item;
if (sendNetworkMessage) {
NetSendCmdChItem(false, bodyLocation, true);
}
}
bool AutoEquip(Player &player, const Item &item, inv_body_loc bodyLocation, bool persistItem, bool sendNetworkMessage)
{
if (!CanEquip(player, item, bodyLocation)) {
return false;
}
if (persistItem) {
ChangeEquipment(player, bodyLocation, item, sendNetworkMessage);
if (sendNetworkMessage && *sgOptions.Audio.autoEquipSound) {
PlaySFX(ItemInvSnds[ItemCAnimTbl[item._iCurs]]);
}
CalcPlrInv(player, true);
}
return true;
}
int FindTargetSlotUnderItemCursor(Point cursorPosition, Size itemSize)
{
Displacement panelOffset = Point { 0, 0 } - GetRightPanel().position;
for (int r = SLOTXY_EQUIPPED_FIRST; r <= SLOTXY_EQUIPPED_LAST; r++) {
if (InvRect[r].contains(cursorPosition + panelOffset))
return r;
}
for (int r = SLOTXY_INV_FIRST; r <= SLOTXY_INV_LAST; r++) {
if (InvRect[r].contains(cursorPosition + panelOffset)) {
// When trying to paste into the inventory we need to determine the top left cell of the nearest area that could fit the item, not the slot under the center/hot pixel.
if (itemSize.height <= 1 && itemSize.width <= 1) {
// top left cell of a 1x1 item is the same cell as the hot pixel, no work to do
return r;
}
// Otherwise work out how far the central cell is from the top-left cell
Displacement hotPixelCellOffset = { (itemSize.width - 1) / 2, (itemSize.height - 1) / 2 };
// For even dimension items we need to work out if the cursor is in the left/right (or top/bottom) half of the central cell and adjust the offset so the item lands in the area most covered by the cursor.
if (itemSize.width % 2 == 0 && InvRect[r].contains(cursorPosition + panelOffset + Displacement { INV_SLOT_HALF_SIZE_PX, 0 })) {
// hot pixel was in the left half of the cell, so we want to increase the offset to preference the column to the left
hotPixelCellOffset.deltaX++;
}
if (itemSize.height % 2 == 0 && InvRect[r].contains(cursorPosition + panelOffset + Displacement { 0, INV_SLOT_HALF_SIZE_PX })) {
// hot pixel was in the top half of the cell, so we want to increase the offset to preference the row above
hotPixelCellOffset.deltaY++;
}
// Then work out the top left cell of the nearest area that could fit this item (as pasting on the edge of the inventory would otherwise put it out of bounds)
int hotPixelCell = r - SLOTXY_INV_FIRST;
int targetRow = std::clamp((hotPixelCell / InventorySizeInSlots.width) - hotPixelCellOffset.deltaY, 0, InventorySizeInSlots.height - itemSize.height);
int targetColumn = std::clamp((hotPixelCell % InventorySizeInSlots.width) - hotPixelCellOffset.deltaX, 0, InventorySizeInSlots.width - itemSize.width);
return SLOTXY_INV_FIRST + targetRow * InventorySizeInSlots.width + targetColumn;
}
}
panelOffset = Point { 0, 0 } - GetMainPanel().position;
for (int r = SLOTXY_BELT_FIRST; r <= SLOTXY_BELT_LAST; r++) {
if (InvRect[r].contains(cursorPosition + panelOffset))
return r;
}
return NUM_XY_SLOTS;
}
void ChangeBodyEquipment(Player &player, int slot, item_equip_type location)
{
const inv_body_loc bodyLocation = [&slot](item_equip_type location) {
switch (location) {
case ILOC_HELM:
return INVLOC_HEAD;
case ILOC_RING:
return (slot == SLOTXY_RING_LEFT ? INVLOC_RING_LEFT : INVLOC_RING_RIGHT);
case ILOC_AMULET:
return INVLOC_AMULET;
case ILOC_ARMOR:
return INVLOC_CHEST;
default:
app_fatal("Unexpected equipment type");
}
}(location);
const Item previouslyEquippedItem = player.InvBody[slot];
ChangeEquipment(player, bodyLocation, player.HoldItem.pop(), &player == MyPlayer);
if (!previouslyEquippedItem.isEmpty()) {
player.HoldItem = previouslyEquippedItem;
}
}
void ChangeEquippedItem(Player &player, uint8_t slot)
{
const inv_body_loc selectedHand = slot == SLOTXY_HAND_LEFT ? INVLOC_HAND_LEFT : INVLOC_HAND_RIGHT;
const inv_body_loc otherHand = slot == SLOTXY_HAND_LEFT ? INVLOC_HAND_RIGHT : INVLOC_HAND_LEFT;
const bool pasteIntoSelectedHand = (player.InvBody[otherHand].isEmpty() || player.InvBody[otherHand]._iClass != player.HoldItem._iClass)
|| (player._pClass == HeroClass::Bard && player.InvBody[otherHand]._iClass == ICLASS_WEAPON && player.HoldItem._iClass == ICLASS_WEAPON);
const bool dequipTwoHandedWeapon = (!player.InvBody[otherHand].isEmpty() && player.GetItemLocation(player.InvBody[otherHand]) == ILOC_TWOHAND);
const inv_body_loc pasteHand = pasteIntoSelectedHand ? selectedHand : otherHand;
const Item previouslyEquippedItem = dequipTwoHandedWeapon ? player.InvBody[otherHand] : player.InvBody[pasteHand];
if (dequipTwoHandedWeapon) {
RemoveEquipment(player, otherHand, false);
}
ChangeEquipment(player, pasteHand, player.HoldItem.pop(), &player == MyPlayer);
if (!previouslyEquippedItem.isEmpty()) {
player.HoldItem = previouslyEquippedItem;
}
}
void ChangeTwoHandItem(Player &player)
{
if (!player.InvBody[INVLOC_HAND_LEFT].isEmpty() && !player.InvBody[INVLOC_HAND_RIGHT].isEmpty()) {
inv_body_loc locationToUnequip = INVLOC_HAND_LEFT;
if (player.InvBody[INVLOC_HAND_RIGHT]._itype == ItemType::Shield) {
locationToUnequip = INVLOC_HAND_RIGHT;
}
if (!AutoPlaceItemInInventory(player, player.InvBody[locationToUnequip])) {
return;
}
if (locationToUnequip == INVLOC_HAND_RIGHT) {
RemoveEquipment(player, INVLOC_HAND_RIGHT, false);
} else {
player.InvBody[INVLOC_HAND_LEFT].clear();
}
}
if (player.InvBody[INVLOC_HAND_RIGHT].isEmpty()) {
Item previouslyEquippedItem = player.InvBody[INVLOC_HAND_LEFT];
ChangeEquipment(player, INVLOC_HAND_LEFT, player.HoldItem.pop(), &player == MyPlayer);
if (!previouslyEquippedItem.isEmpty()) {
player.HoldItem = previouslyEquippedItem;
}
} else {
Item previouslyEquippedItem = player.InvBody[INVLOC_HAND_RIGHT];
RemoveEquipment(player, INVLOC_HAND_RIGHT, false);
ChangeEquipment(player, INVLOC_HAND_LEFT, player.HoldItem, &player == MyPlayer);
player.HoldItem = previouslyEquippedItem;
}
}
int8_t CheckOverlappingItems(int slot, const Player &player, Size itemSize)
{
// check that the item we're pasting only overlaps one other item (or is going into empty space)
const unsigned originCell = static_cast<unsigned>(slot - SLOTXY_INV_FIRST);
int8_t overlappingId = 0;
for (unsigned rowOffset = 0; rowOffset < static_cast<unsigned>(itemSize.height * InventorySizeInSlots.width); rowOffset += InventorySizeInSlots.width) {
for (unsigned columnOffset = 0; columnOffset < static_cast<unsigned>(itemSize.width); columnOffset++) {
unsigned testCell = originCell + rowOffset + columnOffset;
// FindTargetSlotUnderItemCursor returns the top left slot of the inventory region that fits the item, we can be confident this calculation is not going to read out of range.
assert(testCell < sizeof(player.InvGrid));
if (player.InvGrid[testCell] != 0) {
int8_t iv = std::abs(player.InvGrid[testCell]);
if (overlappingId != 0) {
if (overlappingId != iv) {
// Found two different items that would be displaced by the held item, can't paste the item here.
return -1;
}
} else {
overlappingId = iv;
}
}
}
}
return overlappingId;
}
int8_t GetPrevItemId(int slot, const Player &player, const Size &itemSize)
{
if (player.HoldItem._itype != ItemType::Gold)
return CheckOverlappingItems(slot, player, itemSize);
int8_t item_cell_begin = player.InvGrid[slot - SLOTXY_INV_FIRST];
if (item_cell_begin == 0)
return 0;
if (item_cell_begin <= 0)
return -item_cell_begin;
if (player.InvList[item_cell_begin - 1]._itype != ItemType::Gold)
return item_cell_begin;
return 0;
}
bool ChangeInvItem(Player &player, int slot, Size itemSize)
{
int8_t prevItemId = GetPrevItemId(slot, player, itemSize);
if (prevItemId < 0) return false;
if (player.HoldItem._itype == ItemType::Gold && prevItemId == 0) {
const int ii = slot - SLOTXY_INV_FIRST;
if (player.InvGrid[ii] > 0) {
const int invIndex = player.InvGrid[ii] - 1;
const int gt = player.InvList[invIndex]._ivalue;
int ig = player.HoldItem._ivalue + gt;
if (ig <= MaxGold) {
player.InvList[invIndex]._ivalue = ig;
SetPlrHandGoldCurs(player.InvList[invIndex]);
player._pGold += player.HoldItem._ivalue;
player.HoldItem.clear();
} else {
ig = MaxGold - gt;
player._pGold += ig;
player.HoldItem._ivalue -= ig;
SetPlrHandGoldCurs(player.HoldItem);
player.InvList[invIndex]._ivalue = MaxGold;
player.InvList[invIndex]._iCurs = ICURS_GOLD_LARGE;
}
} else {
const int invIndex = player._pNumInv;
player._pGold += player.HoldItem._ivalue;
player.InvList[invIndex] = player.HoldItem.pop();
player._pNumInv++;
player.InvGrid[ii] = player._pNumInv;
}
if (&player == MyPlayer) {
NetSendCmdChInvItem(false, ii);
}
} else {
if (prevItemId == 0) {
player.InvList[player._pNumInv] = player.HoldItem.pop();
player._pNumInv++;
prevItemId = player._pNumInv;
} else {
const int invIndex = prevItemId - 1;
if (player.HoldItem._itype == ItemType::Gold)
player._pGold += player.HoldItem._ivalue;
std::swap(player.InvList[invIndex], player.HoldItem);
if (player.HoldItem._itype == ItemType::Gold)
player._pGold = CalculateGold(player);
for (int8_t &itemIndex : player.InvGrid) {
if (itemIndex == prevItemId)
itemIndex = 0;
if (itemIndex == -prevItemId)
itemIndex = 0;
}
}
AddItemToInvGrid(player, slot - SLOTXY_INV_FIRST, prevItemId, itemSize, &player == MyPlayer);
}
return true;
}
void ChangeBeltItem(Player &player, int slot)
{
const int ii = slot - SLOTXY_BELT_FIRST;
if (player.SpdList[ii].isEmpty()) {
player.SpdList[ii] = player.HoldItem.pop();
} else {
std::swap(player.SpdList[ii], player.HoldItem);
if (player.HoldItem._itype == ItemType::Gold)
player._pGold = CalculateGold(player);
}
if (&player == MyPlayer) {
NetSendCmdChBeltItem(false, ii);
}
RedrawComponent(PanelDrawComponent::Belt);
}
item_equip_type GetItemEquipType(const Player &player, int slot, item_equip_type desiredLocation)
{
if (slot == SLOTXY_HEAD)
return ILOC_HELM;
if (slot == SLOTXY_RING_LEFT || slot == SLOTXY_RING_RIGHT)
return ILOC_RING;
if (slot == SLOTXY_AMULET)
return ILOC_AMULET;
if (slot == SLOTXY_HAND_LEFT || slot == SLOTXY_HAND_RIGHT) {
if (desiredLocation == ILOC_TWOHAND)
return ILOC_TWOHAND;
return ILOC_ONEHAND;
}
if (slot == SLOTXY_CHEST)
return ILOC_ARMOR;
if (slot >= SLOTXY_BELT_FIRST && slot <= SLOTXY_BELT_LAST)
return ILOC_BELT;
return ILOC_UNEQUIPABLE;
}
void CheckInvPaste(Player &player, Point cursorPosition)
{
const Size itemSize = GetInventorySize(player.HoldItem);
const int slot = FindTargetSlotUnderItemCursor(cursorPosition, itemSize);
if (slot == NUM_XY_SLOTS)
return;
const item_equip_type desiredLocation = player.GetItemLocation(player.HoldItem);
const item_equip_type location = GetItemEquipType(player, slot, desiredLocation);
if (location == ILOC_BELT) {
if (!CanBePlacedOnBelt(player, player.HoldItem)) return;
} else if (location != ILOC_UNEQUIPABLE) {
if (desiredLocation != location) return;
}
if (IsNoneOf(location, ILOC_UNEQUIPABLE, ILOC_BELT)) {
if (!player.CanUseItem(player.HoldItem)) {
player.Say(HeroSpeech::ICantUseThisYet);
return;
}
if (player._pmode > PM_WALK_SIDEWAYS)
return;
}
// Select the parameters that go into
// ChangeEquipment and add it to post switch
switch (location) {
case ILOC_HELM:
case ILOC_RING:
case ILOC_AMULET:
case ILOC_ARMOR:
ChangeBodyEquipment(player, slot, location);
break;
case ILOC_ONEHAND:
ChangeEquippedItem(player, slot);
break;
case ILOC_TWOHAND:
ChangeTwoHandItem(player);
break;
case ILOC_UNEQUIPABLE:
if (!ChangeInvItem(player, slot, itemSize)) return;
break;
case ILOC_BELT:
ChangeBeltItem(player, slot);
break;
case ILOC_NONE:
case ILOC_INVALID:
break;
}
CalcPlrInv(player, true);
if (&player == MyPlayer) {
PlaySFX(ItemInvSnds[ItemCAnimTbl[player.HoldItem._iCurs]]);
NewCursor(player.HoldItem);
}
}
inv_body_loc MapSlotToInvBodyLoc(inv_xy_slot slot)
{
assert(slot <= SLOTXY_CHEST);
return static_cast<inv_body_loc>(slot);
}
std::optional<inv_xy_slot> FindSlotUnderCursor(Point cursorPosition)
{
Point testPosition = static_cast<Point>(cursorPosition - GetRightPanel().position);
for (std::underlying_type_t<inv_xy_slot> r = SLOTXY_EQUIPPED_FIRST; r != SLOTXY_BELT_FIRST; r++) {
// check which body/inventory rectangle the mouse is in, if any
if (InvRect[r].contains(testPosition)) {
return static_cast<inv_xy_slot>(r);
}
}
testPosition = static_cast<Point>(cursorPosition - GetMainPanel().position);
for (std::underlying_type_t<inv_xy_slot> r = SLOTXY_BELT_FIRST; r != NUM_XY_SLOTS; r++) {
// check which belt rectangle the mouse is in, if any
if (InvRect[r].contains(testPosition)) {
return static_cast<inv_xy_slot>(r);
}
}
return {};
}
/**
* @brief Checks whether an item of the given size can be placed on the specified player's inventory slot.
* @param player The player whose inventory will be checked.
* @param slotIndex The 0-based index of the slot to put the item on.
* @param itemSize The size of the item to be checked.
* @param itemIndexToIgnore can be used to check if an item of the given size would fit if the item with the given (positive) ID was removed.
* @return 'True' in case the item can be placed on the specified player's inventory slot and 'False' otherwise.
*/
bool CheckItemFitsInInventorySlot(const Player &player, int slotIndex, const Size &itemSize, int itemIndexToIgnore)
{
int yy = (slotIndex > 0) ? (10 * (slotIndex / 10)) : 0;
for (int j = 0; j < itemSize.height; j++) {
if (yy >= InventoryGridCells) {
return false;
}
int xx = (slotIndex > 0) ? (slotIndex % 10) : 0;
for (int i = 0; i < itemSize.width; i++) {
if (xx >= 10 || !(player.InvGrid[xx + yy] == 0 || std::abs(player.InvGrid[xx + yy]) - 1 == itemIndexToIgnore)) {
// The item is too wide to fit in the specified column, or one of the cells is occupied (and not by the item we're planning on removing)
return false;
}
xx++;
}
yy += 10;
}
return true;
}
/**
* @brief Finds the first slot that could fit an item of the given size
* @param player Player whose inventory will be checked.
* @param itemSize Dimensions of the item.
* @param itemIndexToIgnore Can be used if you want to find whether the new item would fit with this item removed, without performing unnecessary actions.
* @return The first slot that could fit the item or an empty optional.
*/
std::optional<int> FindSlotForItem(const Player &player, const Size &itemSize, int itemIndexToIgnore = -1)
{
if (itemSize.height == 1) {
for (int i = 30; i <= 39; i++) {
if (CheckItemFitsInInventorySlot(player, i, itemSize, itemIndexToIgnore))
return i;
}
for (int x = 9; x >= 0; x--) {
for (int y = 2; y >= 0; y--) {
if (CheckItemFitsInInventorySlot(player, 10 * y + x, itemSize, itemIndexToIgnore))
return 10 * y + x;
}
}
return {};
}
if (itemSize.height == 2) {
for (int x = 10 - itemSize.width; x >= 0; x--) {
for (int y = 0; y < 3; y++) {
if (CheckItemFitsInInventorySlot(player, 10 * y + x, itemSize, itemIndexToIgnore))
return 10 * y + x;
}
}
return {};
}
if (itemSize == Size { 1, 3 }) {
for (int i = 0; i < 20; i++) {
if (CheckItemFitsInInventorySlot(player, i, itemSize, itemIndexToIgnore))
return i;
}
return {};
}
if (itemSize == Size { 2, 3 }) {
for (int i = 0; i < 9; i++) {
if (CheckItemFitsInInventorySlot(player, i, itemSize, itemIndexToIgnore))
return i;
}
for (int i = 10; i < 19; i++) {
if (CheckItemFitsInInventorySlot(player, i, itemSize, itemIndexToIgnore))
return i;
}
return {};
}
app_fatal(StrCat("Unknown item size: ", itemSize.width, "x", itemSize.height));
}
/**
* @brief Checks if the given item could be placed on the specified players inventory if the other item was removed.
* @param player The player whose inventory will be checked.
* @param item The item to be checked.
* @param itemIndexToIgnore The inventory index of the item that we assume will be removed.
* @return 'True' if the item could fit with the other item removed and 'False' otherwise.
*/
bool CouldFitItemInInventory(const Player &player, const Item &item, int itemIndexToIgnore)
{
return static_cast<bool>(FindSlotForItem(player, GetInventorySize(item), itemIndexToIgnore));
}
void CheckInvCut(Player &player, Point cursorPosition, bool automaticMove, bool dropItem)
{
if (player._pmode > PM_WALK_SIDEWAYS) {
return;
}
CloseGoldDrop();
std::optional<inv_xy_slot> maybeSlot = FindSlotUnderCursor(cursorPosition);
if (!maybeSlot) {
// not on an inventory slot rectangle
return;
}
inv_xy_slot r = *maybeSlot;
Item &holdItem = player.HoldItem;
holdItem.clear();
bool attemptedMove = false;
bool automaticallyMoved = false;
SfxID successSound = SfxID::None;
HeroSpeech failedSpeech = HeroSpeech::ICantDoThat; // Default message if the player attempts to automove an item that can't go anywhere else
if (r >= SLOTXY_HEAD && r <= SLOTXY_CHEST) {
inv_body_loc invloc = MapSlotToInvBodyLoc(r);
if (!player.InvBody[invloc].isEmpty()) {
if (automaticMove) {
attemptedMove = true;
automaticallyMoved = AutoPlaceItemInInventory(player, player.InvBody[invloc]);
if (automaticallyMoved) {
successSound = ItemInvSnds[ItemCAnimTbl[player.InvBody[invloc]._iCurs]];
RemoveEquipment(player, invloc, false);
} else {
failedSpeech = HeroSpeech::IHaveNoRoom;
}
} else {
holdItem = player.InvBody[invloc];
RemoveEquipment(player, invloc, false);
}
}
}
if (r >= SLOTXY_INV_FIRST && r <= SLOTXY_INV_LAST) {
unsigned ig = r - SLOTXY_INV_FIRST;
int iv = std::abs(player.InvGrid[ig]) - 1;
if (iv >= 0) {
if (automaticMove) {
attemptedMove = true;
if (CanBePlacedOnBelt(player, player.InvList[iv])) {
automaticallyMoved = AutoPlaceItemInBelt(player, player.InvList[iv], true, &player == MyPlayer);
if (automaticallyMoved) {
successSound = SfxID::GrabItem;
player.RemoveInvItem(iv, false);
} else {
failedSpeech = HeroSpeech::IHaveNoRoom;
}
} else if (CanEquip(player.InvList[iv])) {
failedSpeech = HeroSpeech::IHaveNoRoom; // Default to saying "I have no room" if auto-equip fails
/*
* If the player shift-clicks an item in the inventory we want to swap it with whatever item may be
* equipped in the target slot. Lifting the item to the hand unconditionally would be ideal, except
* we don't want to leave the item on the hand if the equip attempt failed. We would end up
* generating wasteful network messages if we did the lift first. Instead we work out whatever slot
* needs to be unequipped (if any):
*/
int invloc = NUM_INVLOC;
switch (player.GetItemLocation(player.InvList[iv])) {
case ILOC_ARMOR:
invloc = INVLOC_CHEST;
break;
case ILOC_HELM:
invloc = INVLOC_HEAD;
break;
case ILOC_AMULET:
invloc = INVLOC_AMULET;
break;
case ILOC_ONEHAND:
if (!player.InvBody[INVLOC_HAND_LEFT].isEmpty()
&& (player.InvList[iv]._iClass == player.InvBody[INVLOC_HAND_LEFT]._iClass
|| player.GetItemLocation(player.InvBody[INVLOC_HAND_LEFT]) == ILOC_TWOHAND)) {
// The left hand is not empty and we're either trying to equip the same type of item or
// it's holding a two handed weapon, so it must be unequipped
invloc = INVLOC_HAND_LEFT;
} else if (!player.InvBody[INVLOC_HAND_RIGHT].isEmpty() && player.InvList[iv]._iClass == player.InvBody[INVLOC_HAND_RIGHT]._iClass) {
// The right hand is not empty and we're trying to equip the same type of item, so we need
// to unequip that item
invloc = INVLOC_HAND_RIGHT;
}
// otherwise one hand is empty (and we can let the auto-equip code put the target item into
// that hand) or we're playing a bard with two swords equipped and we're trying to auto-equip
// a shield (in which case the attempt will fail).
break;
case ILOC_TWOHAND:
// Moving a two-hand item from inventory to InvBody requires emptying both hands.
if (player.InvBody[INVLOC_HAND_RIGHT].isEmpty()) {
// If the right hand is empty then we can simply try equipping this item in the left hand,
// we'll let the common code take care of unequipping anything held there.
invloc = INVLOC_HAND_LEFT;
} else if (player.InvBody[INVLOC_HAND_LEFT].isEmpty()) {
// We have an item in the right hand but nothing in the left, so let the common code
// take care of unequipping whatever is held in the right hand. The auto-equip code
// picks the most appropriate location for the item type (which in this case will be
// the left hand), invloc isn't used there.
invloc = INVLOC_HAND_RIGHT;
} else {
// Both hands are holding items, we must unequip one of the items and check that there's
// space for the other before trying to auto-equip
inv_body_loc mainHand = INVLOC_HAND_LEFT;
inv_body_loc offHand = INVLOC_HAND_RIGHT;
if (!AutoPlaceItemInInventory(player, player.InvBody[offHand])) {
// No space to move right hand item to inventory, can we move the left instead?
std::swap(mainHand, offHand);
if (!AutoPlaceItemInInventory(player, player.InvBody[offHand])) {
break;
}
}
if (!CouldFitItemInInventory(player, player.InvBody[mainHand], iv)) {
// No space for the main hand item. Move the other item back to the off hand and abort.
player.InvBody[offHand] = player.InvList[player._pNumInv - 1];
player.RemoveInvItem(player._pNumInv - 1, false);
break;
}
RemoveEquipment(player, offHand, false);
invloc = mainHand;
}
break;
default:
// If the player is trying to equip a ring we want to say "I can't do that" if they don't already have a ring slot free.
failedSpeech = HeroSpeech::ICantDoThat;
break;
}
// Then empty the identified InvBody slot (invloc) and hand over to AutoEquip
if (invloc != NUM_INVLOC
&& !player.InvBody[invloc].isEmpty()
&& CouldFitItemInInventory(player, player.InvBody[invloc], iv)) {
holdItem = player.InvBody[invloc].pop();
}
automaticallyMoved = AutoEquip(player, player.InvList[iv], true, &player == MyPlayer);
if (automaticallyMoved) {
successSound = ItemInvSnds[ItemCAnimTbl[player.InvList[iv]._iCurs]];
player.RemoveInvItem(iv, false);
// If we're holding an item at this point we just lifted it from a body slot to make room for the original item, so we need to put it into the inv
if (!holdItem.isEmpty() && AutoPlaceItemInInventory(player, holdItem)) {
holdItem.clear();
} // there should never be a situation where holdItem is not empty but we fail to place it into the inventory given the checks earlier... leave it on the hand in this case.
} else if (!holdItem.isEmpty()) {
// We somehow failed to equip the item in the slot we already checked should hold it? Better put this item back...
player.InvBody[invloc] = holdItem.pop();
}
}
} else {
holdItem = player.InvList[iv];
player.RemoveInvItem(iv, false);
}
}
}
if (r >= SLOTXY_BELT_FIRST) {
Item &beltItem = player.SpdList[r - SLOTXY_BELT_FIRST];
if (!beltItem.isEmpty()) {
if (automaticMove) {
attemptedMove = true;
automaticallyMoved = AutoPlaceItemInInventory(player, beltItem);
if (automaticallyMoved) {
successSound = SfxID::GrabItem;
player.RemoveSpdBarItem(r - SLOTXY_BELT_FIRST);
} else {
failedSpeech = HeroSpeech::IHaveNoRoom;
}
} else {
holdItem = beltItem;
player.RemoveSpdBarItem(r - SLOTXY_BELT_FIRST);
}
}
}
if (!holdItem.isEmpty()) {
if (holdItem._itype == ItemType::Gold) {
player._pGold = CalculateGold(player);
}
CalcPlrInv(player, true);
holdItem._iStatFlag = player.CanUseItem(holdItem);
if (&player == MyPlayer) {
PlaySFX(SfxID::GrabItem);
NewCursor(holdItem);
}
if (dropItem) {
TryDropItem();
}
} else if (automaticMove) {
if (automaticallyMoved) {
CalcPlrInv(player, true);
}
if (attemptedMove && &player == MyPlayer) {
if (automaticallyMoved) {
PlaySFX(successSound);
} else {
player.SaySpecific(failedSpeech);
}
}
}
}
void TryCombineNaKrulNotes(Player &player, Item ¬eItem)
{
int idx = noteItem.IDidx;
_item_indexes notes[] = { IDI_NOTE1, IDI_NOTE2, IDI_NOTE3 };
if (IsNoneOf(idx, IDI_NOTE1, IDI_NOTE2, IDI_NOTE3)) {
return;
}
for (_item_indexes note : notes) {
if (idx != note && !HasInventoryItemWithId(player, note)) {
return; // the player doesn't have all notes
}
}
MyPlayer->Say(HeroSpeech::JustWhatIWasLookingFor, 10);
for (_item_indexes note : notes) {
if (idx != note) {
RemoveInventoryItemById(player, note);
}
}
Point position = noteItem.position; // copy the position to restore it after re-initialising the item
noteItem = {};
GetItemAttrs(noteItem, IDI_FULLNOTE, 16);
SetupItem(noteItem);
noteItem.position = position; // this ensures CleanupItem removes the entry in the dropped items lookup table
}
void CheckQuestItem(Player &player, Item &questItem)
{
Player &myPlayer = *MyPlayer;
if (Quests[Q_BLIND]._qactive == QUEST_ACTIVE
&& (questItem.IDidx == IDI_OPTAMULET
|| (Quests[Q_BLIND].IsAvailable() && questItem.position == (SetPiece.position.megaToWorld() + Displacement { 5, 5 })))) {
Quests[Q_BLIND]._qactive = QUEST_DONE;
NetSendCmdQuest(true, Quests[Q_BLIND]);
}
if (questItem.IDidx == IDI_MUSHROOM && Quests[Q_MUSHROOM]._qactive == QUEST_ACTIVE && Quests[Q_MUSHROOM]._qvar1 == QS_MUSHSPAWNED) {
player.Say(HeroSpeech::NowThatsOneBigMushroom, 10); // BUGFIX: Voice for this quest might be wrong in MP
Quests[Q_MUSHROOM]._qvar1 = QS_MUSHPICKED;
NetSendCmdQuest(true, Quests[Q_MUSHROOM]);
}
if (questItem.IDidx == IDI_ANVIL && Quests[Q_ANVIL]._qactive != QUEST_NOTAVAIL) {
if (Quests[Q_ANVIL]._qactive == QUEST_INIT) {
Quests[Q_ANVIL]._qactive = QUEST_ACTIVE;
NetSendCmdQuest(true, Quests[Q_ANVIL]);
}
if (Quests[Q_ANVIL]._qlog) {
myPlayer.Say(HeroSpeech::INeedToGetThisToGriswold, 10);
}
}
if (questItem.IDidx == IDI_GLDNELIX && Quests[Q_VEIL]._qactive != QUEST_NOTAVAIL) {
myPlayer.Say(HeroSpeech::INeedToGetThisToLachdanan, 30);
}