-
Notifications
You must be signed in to change notification settings - Fork 804
/
loadsave.cpp
2819 lines (2496 loc) · 94.8 KB
/
loadsave.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 loadsave.cpp
*
* Implementation of save game functionality.
*/
#include "loadsave.h"
#include <climits>
#include <cstdint>
#include <cstring>
#include <numeric>
#include <SDL.h>
#include <ankerl/unordered_dense.h>
#include <fmt/core.h>
#include "automap.h"
#include "codec.h"
#include "control.h"
#include "cursor.h"
#include "dead.h"
#include "doom.h"
#include "engine.h"
#include "engine/point.hpp"
#include "engine/random.hpp"
#include "init.h"
#include "inv.h"
#include "lighting.h"
#include "menu.h"
#include "missiles.h"
#include "monster.h"
#include "mpq/mpq_common.hpp"
#include "pfile.h"
#include "playerdat.hpp"
#include "qol/stash.h"
#include "stores.h"
#include "utils/algorithm/container.hpp"
#include "utils/endian.hpp"
#include "utils/language.h"
namespace devilution {
bool gbIsHellfireSaveGame;
uint8_t giNumberOfLevels;
namespace {
constexpr size_t MaxMissilesForSaveGame = 125;
uint8_t giNumberQuests;
uint8_t giNumberOfSmithPremiumItems;
template <class T>
T SwapLE(T in)
{
switch (sizeof(T)) {
case 2:
return SDL_SwapLE16(in);
case 4:
return SDL_SwapLE32(in);
case 8:
return SDL_SwapLE64(in);
default:
return in;
}
}
template <class T>
T SwapBE(T in)
{
switch (sizeof(T)) {
case 2:
return SDL_SwapBE16(in);
case 4:
return SDL_SwapBE32(in);
case 8:
return static_cast<T>(SDL_SwapBE64(in));
default:
return in;
}
}
class LoadHelper {
std::unique_ptr<std::byte[]> m_buffer_;
size_t m_cur_ = 0;
size_t m_size_;
template <class T>
T Next()
{
const auto size = sizeof(T);
if (!IsValid(size))
return 0;
T value;
memcpy(&value, &m_buffer_[m_cur_], size);
m_cur_ += size;
return value;
}
public:
LoadHelper(std::optional<SaveReader> archive, const char *szFileName)
{
if (archive)
m_buffer_ = ReadArchive(*archive, szFileName, &m_size_);
else
m_buffer_ = nullptr;
}
bool IsValid(size_t size = 1)
{
return m_buffer_ != nullptr
&& m_size_ >= (m_cur_ + size);
}
template <typename T>
constexpr void Skip(size_t count = 1)
{
Skip(sizeof(T) * count);
}
void Skip(size_t size)
{
m_cur_ += size;
}
void NextBytes(void *bytes, size_t size)
{
if (!IsValid(size))
return;
memcpy(bytes, &m_buffer_[m_cur_], size);
m_cur_ += size;
}
template <class T>
T NextLE()
{
return SwapLE(Next<T>());
}
template <class T>
T NextBE()
{
return SwapBE(Next<T>());
}
template <class TSource, class TDesired>
TDesired NextLENarrow(TSource modifier = 0)
{
static_assert(sizeof(TSource) > sizeof(TDesired), "Can only narrow to a smaller type");
TSource value = SwapLE(Next<TSource>()) + modifier;
return static_cast<TDesired>(std::clamp<TSource>(value, std::numeric_limits<TDesired>::min(), std::numeric_limits<TDesired>::max()));
}
bool NextBool8()
{
return Next<uint8_t>() != 0;
}
bool NextBool32()
{
return Next<uint32_t>() != 0;
}
};
class SaveHelper {
SaveWriter &m_mpqWriter;
const char *m_szFileName_;
std::unique_ptr<std::byte[]> m_buffer_;
size_t m_cur_ = 0;
size_t m_capacity_;
public:
SaveHelper(SaveWriter &mpqWriter, const char *szFileName, size_t bufferLen)
: m_mpqWriter(mpqWriter)
, m_szFileName_(szFileName)
, m_buffer_(new std::byte[codec_get_encoded_len(bufferLen)])
, m_capacity_(bufferLen)
{
}
bool IsValid(size_t len = 1)
{
return m_buffer_ != nullptr
&& m_capacity_ >= (m_cur_ + len);
}
template <typename T>
constexpr void Skip(size_t count = 1)
{
Skip(sizeof(T) * count);
}
void Skip(size_t len)
{
std::memset(&m_buffer_[m_cur_], 0, len);
m_cur_ += len;
}
void WriteBytes(const void *bytes, size_t len)
{
if (!IsValid(len))
return;
memcpy(&m_buffer_[m_cur_], bytes, len);
m_cur_ += len;
}
template <class T>
void WriteLE(T value)
{
value = SwapLE(value);
WriteBytes(&value, sizeof(value));
}
template <class T>
void WriteBE(T value)
{
value = SwapBE(value);
WriteBytes(&value, sizeof(value));
}
~SaveHelper()
{
const auto encodedLen = codec_get_encoded_len(m_cur_);
const char *const password = pfile_get_password();
codec_encode(m_buffer_.get(), m_cur_, encodedLen, password);
m_mpqWriter.WriteFile(m_szFileName_, m_buffer_.get(), encodedLen);
}
};
struct MonsterConversionData {
int8_t monsterLevel;
uint16_t experience;
uint8_t toHitSpecial;
};
struct LevelConversionData {
MonsterConversionData monsterConversionData[MaxMonsters];
};
void LoadItemData(LoadHelper &file, Item &item)
{
item._iSeed = file.NextLE<uint32_t>();
item._iCreateInfo = file.NextLE<uint16_t>();
file.Skip(2); // Alignment
item._itype = static_cast<ItemType>(file.NextLE<uint32_t>());
item.position.x = file.NextLE<int32_t>();
item.position.y = file.NextLE<int32_t>();
item._iAnimFlag = file.NextBool32();
file.Skip(4); // Skip pointer _iAnimData
item.AnimInfo = {};
item.AnimInfo.numberOfFrames = file.NextLENarrow<int32_t, int8_t>();
item.AnimInfo.currentFrame = file.NextLENarrow<int32_t, int8_t>(-1);
file.Skip(8); // Skip _iAnimWidth and _iAnimWidth2
file.Skip(4); // Unused since 1.02
item.selectionRegion = static_cast<SelectionRegion>(file.NextLE<uint8_t>());
file.Skip(3); // Alignment
item._iPostDraw = file.NextBool32();
item._iIdentified = file.NextBool32();
item._iMagical = static_cast<item_quality>(file.NextLE<int8_t>());
file.NextBytes(item._iName, 64);
file.NextBytes(item._iIName, 64);
item._iLoc = static_cast<item_equip_type>(file.NextLE<int8_t>());
item._iClass = static_cast<item_class>(file.NextLE<uint8_t>());
file.Skip(1); // Alignment
item._iCurs = file.NextLE<int32_t>();
item._ivalue = file.NextLE<int32_t>();
item._iIvalue = file.NextLE<int32_t>();
item._iMinDam = file.NextLE<int32_t>();
item._iMaxDam = file.NextLE<int32_t>();
item._iAC = file.NextLE<int32_t>();
item._iFlags = static_cast<ItemSpecialEffect>(file.NextLE<uint32_t>());
item._iMiscId = static_cast<item_misc_id>(file.NextLE<int32_t>());
item._iSpell = static_cast<SpellID>(file.NextLE<int32_t>());
item._iCharges = file.NextLE<int32_t>();
item._iMaxCharges = file.NextLE<int32_t>();
item._iDurability = file.NextLE<int32_t>();
item._iMaxDur = file.NextLE<int32_t>();
item._iPLDam = file.NextLE<int32_t>();
item._iPLToHit = file.NextLE<int32_t>();
item._iPLAC = file.NextLE<int32_t>();
item._iPLStr = file.NextLE<int32_t>();
item._iPLMag = file.NextLE<int32_t>();
item._iPLDex = file.NextLE<int32_t>();
item._iPLVit = file.NextLE<int32_t>();
item._iPLFR = file.NextLE<int32_t>();
item._iPLLR = file.NextLE<int32_t>();
item._iPLMR = file.NextLE<int32_t>();
item._iPLMana = file.NextLE<int32_t>();
item._iPLHP = file.NextLE<int32_t>();
item._iPLDamMod = file.NextLE<int32_t>();
item._iPLGetHit = file.NextLE<int32_t>();
item._iPLLight = file.NextLE<int32_t>();
item._iSplLvlAdd = file.NextLE<int8_t>();
item._iRequest = file.NextBool8();
file.Skip(2); // Alignment
item._iUid = file.NextLE<int32_t>();
item._iFMinDam = file.NextLE<int32_t>();
item._iFMaxDam = file.NextLE<int32_t>();
item._iLMinDam = file.NextLE<int32_t>();
item._iLMaxDam = file.NextLE<int32_t>();
item._iPLEnAc = file.NextLE<int32_t>();
item._iPrePower = static_cast<item_effect_type>(file.NextLE<int8_t>());
item._iSufPower = static_cast<item_effect_type>(file.NextLE<int8_t>());
file.Skip(2); // Alignment
item._iVAdd1 = file.NextLE<int32_t>();
item._iVMult1 = file.NextLE<int32_t>();
item._iVAdd2 = file.NextLE<int32_t>();
item._iVMult2 = file.NextLE<int32_t>();
item._iMinStr = file.NextLE<int8_t>();
item._iMinMag = file.NextLE<uint8_t>();
item._iMinDex = file.NextLE<int8_t>();
file.Skip(1); // Alignment
item._iStatFlag = file.NextBool32();
item.IDidx = static_cast<_item_indexes>(file.NextLE<int32_t>());
if (gbIsSpawn) {
item.IDidx = RemapItemIdxFromSpawn(item.IDidx);
}
if (!gbIsHellfireSaveGame) {
item.IDidx = RemapItemIdxFromDiablo(item.IDidx);
}
item.dwBuff = file.NextLE<uint32_t>();
if (gbIsHellfireSaveGame)
item._iDamAcFlags = static_cast<ItemSpecialEffectHf>(file.NextLE<uint32_t>());
else
item._iDamAcFlags = ItemSpecialEffectHf::None;
UpdateHellfireFlag(item, item._iIName);
}
void LoadAndValidateItemData(LoadHelper &file, Item &item)
{
LoadItemData(file, item);
RemoveInvalidItem(item);
}
void LoadPlayer(LoadHelper &file, Player &player)
{
player._pmode = static_cast<PLR_MODE>(file.NextLE<int32_t>());
for (int8_t &step : player.walkpath) {
step = file.NextLE<int8_t>();
}
player.plractive = file.NextBool8();
file.Skip(2); // Alignment
player.destAction = static_cast<action_id>(file.NextLE<int32_t>());
player.destParam1 = file.NextLE<int32_t>();
player.destParam2 = file.NextLE<int32_t>();
player.destParam3 = file.NextLE<int32_t>();
player.destParam4 = file.NextLE<int32_t>();
player.setLevel(file.NextLE<uint32_t>());
player.position.tile.x = file.NextLE<int32_t>();
player.position.tile.y = file.NextLE<int32_t>();
player.position.future.x = file.NextLE<int32_t>();
player.position.future.y = file.NextLE<int32_t>();
file.Skip<uint32_t>(2); // Skip _ptargx and _ptargy
player.position.last.x = file.NextLE<int32_t>();
player.position.last.y = file.NextLE<int32_t>();
player.position.old.x = file.NextLE<int32_t>();
player.position.old.y = file.NextLE<int32_t>();
file.Skip<int32_t>(4); // Skip offset and velocity
player._pdir = static_cast<Direction>(file.NextLE<int32_t>());
file.Skip(4); // Unused
player._pgfxnum = file.NextLENarrow<uint32_t, uint8_t>();
file.Skip<uint32_t>(); // Skip pointer pData
player.AnimInfo = {};
player.AnimInfo.ticksPerFrame = file.NextLENarrow<int32_t, int8_t>(1);
player.AnimInfo.tickCounterOfCurrentFrame = file.NextLENarrow<int32_t, int8_t>();
player.AnimInfo.numberOfFrames = file.NextLENarrow<int32_t, int8_t>();
player.AnimInfo.currentFrame = file.NextLENarrow<int32_t, int8_t>(-1);
file.Skip<uint32_t>(3); // Skip _pAnimWidth, _pAnimWidth2, _peflag
player.lightId = file.NextLE<int32_t>();
file.Skip<int32_t>(); // _pvid
player.queuedSpell.spellId = static_cast<SpellID>(file.NextLE<int32_t>());
player.queuedSpell.spellType = static_cast<SpellType>(file.NextLE<int8_t>());
auto spellFrom = file.NextLE<int8_t>();
if (!IsValidSpellFrom(spellFrom))
spellFrom = 0;
player.spellFrom = spellFrom;
player.queuedSpell.spellFrom = spellFrom;
file.Skip(2); // Alignment
player.inventorySpell = static_cast<SpellID>(file.NextLE<int32_t>());
file.Skip<int8_t>(); // Skip _pTSplType
file.Skip(3); // Alignment
player._pRSpell = static_cast<SpellID>(file.NextLE<int32_t>());
player._pRSplType = static_cast<SpellType>(file.NextLE<int8_t>());
file.Skip(3); // Alignment
player._pSBkSpell = static_cast<SpellID>(file.NextLE<int32_t>());
file.Skip<int8_t>(); // Skip _pSBkSplType
// Only read spell levels for learnable spells
for (int i = 0; i < static_cast<int>(SpellID::LAST); i++) {
auto spl = static_cast<SpellID>(i);
if (GetSpellData(spl).sBookLvl != -1)
player._pSplLvl[i] = file.NextLE<uint8_t>();
else
file.Skip<uint8_t>();
}
// Skip indices that are unused
for (int i = static_cast<int>(SpellID::LAST); i < 64; i++)
file.Skip<uint8_t>();
// These spells are unavailable in Diablo as learnable spells
if (!gbIsHellfire) {
player._pSplLvl[static_cast<uint8_t>(SpellID::Apocalypse)] = 0;
player._pSplLvl[static_cast<uint8_t>(SpellID::Nova)] = 0;
}
file.Skip(7); // Alignment
player._pMemSpells = file.NextLE<uint64_t>();
player._pAblSpells = file.NextLE<uint64_t>();
player._pScrlSpells = file.NextLE<uint64_t>();
player._pSpellFlags = static_cast<SpellFlag>(file.NextLE<uint8_t>());
file.Skip(3); // Alignment
// Extra hotkeys: to keep single player save compatibility, read only 4 hotkeys here, rely on LoadHotkeys for the rest
for (size_t i = 0; i < 4; i++) {
player._pSplHotKey[i] = static_cast<SpellID>(file.NextLE<int32_t>());
}
for (size_t i = 0; i < 4; i++) {
player._pSplTHotKey[i] = static_cast<SpellType>(file.NextLE<uint8_t>());
}
file.Skip<int32_t>(); // Skip _pwtype
player._pBlockFlag = file.NextBool8();
player._pInvincible = file.NextBool8();
player._pLightRad = file.NextLE<int8_t>();
player._pLvlChanging = file.NextBool8();
file.NextBytes(player._pName, PlayerNameLength);
player._pClass = static_cast<HeroClass>(file.NextLE<int8_t>());
file.Skip(3); // Alignment
player._pStrength = file.NextLE<int32_t>();
player._pBaseStr = file.NextLE<int32_t>();
player._pMagic = file.NextLE<int32_t>();
player._pBaseMag = file.NextLE<int32_t>();
player._pDexterity = file.NextLE<int32_t>();
player._pBaseDex = file.NextLE<int32_t>();
player._pVitality = file.NextLE<int32_t>();
player._pBaseVit = file.NextLE<int32_t>();
player._pStatPts = file.NextLE<int32_t>();
player._pDamageMod = file.NextLE<int32_t>();
file.Skip<int32_t>(); // Skip _pBaseToBlk - always a copy of PlayerData.blockBonus
player._pHPBase = file.NextLE<int32_t>();
player._pMaxHPBase = file.NextLE<int32_t>();
player._pHitPoints = file.NextLE<int32_t>();
player._pMaxHP = file.NextLE<int32_t>();
file.Skip<int32_t>(); // Skip _pHPPer - always derived from hp and maxHP.
player._pManaBase = file.NextLE<int32_t>();
player._pMaxManaBase = file.NextLE<int32_t>();
player._pMana = file.NextLE<int32_t>();
player._pMaxMana = file.NextLE<int32_t>();
file.Skip<int32_t>(); // Skip _pManaPer - always derived from mana and maxMana
player.setCharacterLevel(file.NextLE<uint8_t>());
file.Skip<uint8_t>(); // Skip _pMaxLevel - unused
file.Skip(2); // Alignment
player._pExperience = file.NextLE<uint32_t>();
file.Skip<uint32_t>(); // Skip _pMaxExp - unused
file.Skip<uint32_t>(); // Skip _pNextExper, we retrieve it when needed based on _pLevel
player._pArmorClass = file.NextLE<int8_t>();
player._pMagResist = file.NextLE<int8_t>();
player._pFireResist = file.NextLE<int8_t>();
player._pLghtResist = file.NextLE<int8_t>();
player._pGold = file.NextLE<int32_t>();
player._pInfraFlag = file.NextBool32();
int32_t tempPositionX = file.NextLE<int32_t>();
int32_t tempPositionY = file.NextLE<int32_t>();
if (player._pmode == PM_WALK_NORTHWARDS) {
// These values are saved as offsets to remain consistent with old savefiles
tempPositionX += player.position.tile.x;
tempPositionY += player.position.tile.y;
}
player.position.temp.x = static_cast<WorldTileCoord>(tempPositionX);
player.position.temp.y = static_cast<WorldTileCoord>(tempPositionY);
player.tempDirection = static_cast<Direction>(file.NextLE<int32_t>());
player.queuedSpell.spellLevel = file.NextLE<int32_t>();
file.Skip<uint32_t>(); // skip _pVar5, was used for storing position of a tile which should have its HorizontalMovingPlayer flag removed after walking
file.Skip<int32_t>(2); // skip offset2;
file.Skip<uint32_t>(); // Skip actionFrame
for (uint8_t i = 0; i < giNumberOfLevels; i++)
player._pLvlVisited[i] = file.NextBool8();
for (uint8_t i = 0; i < giNumberOfLevels; i++)
player._pSLvlVisited[i] = file.NextBool8();
file.Skip(2); // Alignment
file.Skip<uint32_t>(); // skip _pGFXLoad
file.Skip<uint32_t>(8); // Skip pointers _pNAnim
player._pNFrames = file.NextLENarrow<int32_t, int8_t>();
file.Skip<uint32_t>(); // skip _pNWidth
file.Skip<uint32_t>(8); // Skip pointers _pWAnim
player._pWFrames = file.NextLENarrow<int32_t, int8_t>();
file.Skip<uint32_t>(); // skip _pWWidth
file.Skip<uint32_t>(8); // Skip pointers _pAAnim
player._pAFrames = file.NextLENarrow<int32_t, int8_t>();
file.Skip<uint32_t>(); // skip _pAWidth
player._pAFNum = file.NextLENarrow<int32_t, int8_t>();
file.Skip<uint32_t>(8); // Skip pointers _pLAnim
file.Skip<uint32_t>(8); // Skip pointers _pFAnim
file.Skip<uint32_t>(8); // Skip pointers _pTAnim
player._pSFrames = file.NextLENarrow<int32_t, int8_t>();
file.Skip<uint32_t>(); // skip _pSWidth
player._pSFNum = file.NextLENarrow<int32_t, int8_t>();
file.Skip<uint32_t>(8); // Skip pointers _pHAnim
player._pHFrames = file.NextLENarrow<int32_t, int8_t>();
file.Skip<uint32_t>(); // skip _pHWidth
file.Skip<uint32_t>(8); // Skip pointers _pDAnim
player._pDFrames = file.NextLENarrow<int32_t, int8_t>();
file.Skip<uint32_t>(); // skip _pDWidth
file.Skip<uint32_t>(8); // Skip pointers _pBAnim
player._pBFrames = file.NextLENarrow<int32_t, int8_t>();
file.Skip<uint32_t>(); // skip _pBWidth
for (Item &item : player.InvBody)
LoadAndValidateItemData(file, item);
for (Item &item : player.InvList)
LoadAndValidateItemData(file, item);
player._pNumInv = file.NextLE<int32_t>();
for (int8_t &cell : player.InvGrid)
cell = file.NextLE<int8_t>();
for (Item &item : player.SpdList)
LoadAndValidateItemData(file, item);
LoadAndValidateItemData(file, player.HoldItem);
player._pIMinDam = file.NextLE<int32_t>();
player._pIMaxDam = file.NextLE<int32_t>();
player._pIAC = file.NextLE<int32_t>();
player._pIBonusDam = file.NextLE<int32_t>();
player._pIBonusToHit = file.NextLE<int32_t>();
player._pIBonusAC = file.NextLE<int32_t>();
player._pIBonusDamMod = file.NextLE<int32_t>();
file.Skip(4); // Alignment
player._pISpells = file.NextLE<uint64_t>();
player._pIFlags = static_cast<ItemSpecialEffect>(file.NextLE<int32_t>());
player._pIGetHit = file.NextLE<int32_t>();
player._pISplLvlAdd = file.NextLE<int8_t>();
file.Skip(1); // Unused
file.Skip(2); // Alignment
file.Skip<int32_t>(); // _pISplDur
player._pIEnAc = file.NextLE<int32_t>();
player._pIFMinDam = file.NextLE<int32_t>();
player._pIFMaxDam = file.NextLE<int32_t>();
player._pILMinDam = file.NextLE<int32_t>();
player._pILMaxDam = file.NextLE<int32_t>();
player._pOilType = static_cast<item_misc_id>(file.NextLE<int32_t>());
player.pTownWarps = file.NextLE<uint8_t>();
player.pDungMsgs = file.NextLE<uint8_t>();
player.pLvlLoad = file.NextLE<uint8_t>();
if (gbIsHellfireSaveGame) {
player.pDungMsgs2 = file.NextLE<uint8_t>();
} else {
player.pDungMsgs2 = 0;
file.Skip(1); // pBattleNet
}
player.pManaShield = file.NextBool8();
if (gbIsHellfireSaveGame) {
player.pOriginalCathedral = file.NextBool8();
} else {
file.Skip(1);
player.pOriginalCathedral = true;
}
file.Skip(2); // Available bytes
player.wReflections = file.NextLE<uint16_t>();
file.Skip(14); // Available bytes
player.pDiabloKillLevel = file.NextLE<uint32_t>();
sgGameInitInfo.nDifficulty = static_cast<_difficulty>(file.NextLE<uint32_t>());
player.pDamAcFlags = static_cast<ItemSpecialEffectHf>(file.NextLE<uint32_t>());
file.Skip(20); // Available bytes
CalcPlrItemVals(player, false);
player.executedSpell = player.queuedSpell; // Ensures backwards compatibility
// Omit pointer _pNData
// Omit pointer _pWData
// Omit pointer _pAData
// Omit pointer _pLData
// Omit pointer _pFData
// Omit pointer _pTData
// Omit pointer _pHData
// Omit pointer _pDData
// Omit pointer _pBData
// Omit pointer pReserved
// Ensure plrIsOnSetLevel and plrlevel is correctly initialized, because in vanilla sometimes plrlevel is not updated to setlvlnum
if (setlevel)
player.setLevel(setlvlnum);
else
player.setLevel(currlevel);
}
bool gbSkipSync = false;
void LoadMonster(LoadHelper *file, Monster &monster, MonsterConversionData *monsterConversionData = nullptr)
{
monster.levelType = file->NextLE<int32_t>();
monster.mode = static_cast<MonsterMode>(file->NextLE<int32_t>());
monster.goal = static_cast<MonsterGoal>(file->NextLE<uint8_t>());
file->Skip(3); // Alignment
monster.goalVar1 = file->NextLENarrow<int32_t, int16_t>();
monster.goalVar2 = file->NextLENarrow<int32_t, int8_t>();
monster.goalVar3 = file->NextLENarrow<int32_t, int8_t>();
file->Skip(4); // Unused
monster.pathCount = file->NextLE<uint8_t>();
file->Skip(3); // Alignment
monster.position.tile.x = file->NextLE<int32_t>();
monster.position.tile.y = file->NextLE<int32_t>();
monster.position.future.x = file->NextLE<int32_t>();
monster.position.future.y = file->NextLE<int32_t>();
monster.position.old.x = file->NextLE<int32_t>();
monster.position.old.y = file->NextLE<int32_t>();
file->Skip<int32_t>(4); // Skip offset and velocity
monster.direction = static_cast<Direction>(file->NextLE<int32_t>());
monster.enemy = file->NextLE<int32_t>();
monster.enemyPosition.x = file->NextLE<uint8_t>();
monster.enemyPosition.y = file->NextLE<uint8_t>();
file->Skip(2); // Unused
file->Skip(4); // Skip pointer _mAnimData
monster.animInfo = {};
monster.animInfo.ticksPerFrame = file->NextLENarrow<int32_t, int8_t>();
// Ensure that we can increase the tickCounterOfCurrentFrame at least once without overflow (needed for backwards compatibility for sitting gargoyles)
monster.animInfo.tickCounterOfCurrentFrame = file->NextLENarrow<int32_t, int8_t>(1) - 1;
monster.animInfo.numberOfFrames = file->NextLENarrow<int32_t, int8_t>();
monster.animInfo.currentFrame = file->NextLENarrow<int32_t, int8_t>(-1);
file->Skip(4); // Skip _meflag
monster.isInvalid = file->NextBool32();
monster.var1 = file->NextLENarrow<int32_t, int16_t>();
monster.var2 = file->NextLENarrow<int32_t, int16_t>();
monster.var3 = file->NextLENarrow<int32_t, int8_t>();
monster.position.temp.x = file->NextLENarrow<int32_t, WorldTileCoord>();
monster.position.temp.y = file->NextLENarrow<int32_t, WorldTileCoord>();
file->Skip<int32_t>(2); // skip offset2;
file->Skip(4); // Skip actionFrame
monster.maxHitPoints = file->NextLE<int32_t>();
monster.hitPoints = file->NextLE<int32_t>();
monster.ai = static_cast<MonsterAIID>(file->NextLE<uint8_t>());
monster.intelligence = file->NextLE<uint8_t>();
file->Skip(2); // Alignment
monster.flags = file->NextLE<uint32_t>();
monster.activeForTicks = file->NextLE<uint8_t>();
file->Skip(3); // Alignment
file->Skip(4); // Unused
monster.position.last.x = file->NextLE<int32_t>();
monster.position.last.y = file->NextLE<int32_t>();
monster.rndItemSeed = file->NextLE<uint32_t>();
monster.aiSeed = file->NextLE<uint32_t>();
file->Skip(4); // Unused
monster.uniqueType = static_cast<UniqueMonsterType>(file->NextLE<uint8_t>() - 1);
monster.uniqTrans = file->NextLE<uint8_t>();
monster.corpseId = file->NextLE<int8_t>();
monster.whoHit = file->NextLE<int8_t>();
if (monsterConversionData != nullptr)
monsterConversionData->monsterLevel = file->NextLE<int8_t>();
else
file->Skip(1); // Skip level - now calculated on the fly
file->Skip(1); // Alignment
if (monsterConversionData != nullptr)
monsterConversionData->experience = file->NextLE<uint16_t>();
else
file->Skip(2); // Skip exp - now calculated from monstdat when the monster dies
if (monster.isPlayerMinion()) // Don't skip for golems
monster.toHit = file->NextLE<uint8_t>();
else
file->Skip(1); // Skip hit as it's already initialized
monster.minDamage = file->NextLE<uint8_t>();
monster.maxDamage = file->NextLE<uint8_t>();
if (monsterConversionData != nullptr)
monsterConversionData->toHitSpecial = file->NextLE<uint8_t>();
else
file->Skip(1); // Skip toHitSpecial as it's already initialized
monster.minDamageSpecial = file->NextLE<uint8_t>();
monster.maxDamageSpecial = file->NextLE<uint8_t>();
monster.armorClass = file->NextLE<uint8_t>();
file->Skip(1); // Alignment
monster.resistance = file->NextLE<uint16_t>();
file->Skip(2); // Alignment
monster.talkMsg = static_cast<_speech_id>(file->NextLE<int32_t>());
if (monster.talkMsg == TEXT_KING1) // Fix original bad mapping of NONE for monsters
monster.talkMsg = TEXT_NONE;
monster.leader = file->NextLE<uint8_t>();
if (monster.leader == 0)
monster.leader = Monster::NoLeader; // Golems shouldn't be leaders of other monsters
monster.leaderRelation = static_cast<LeaderRelation>(file->NextLE<uint8_t>());
monster.packSize = file->NextLE<uint8_t>();
monster.lightId = file->NextLE<int8_t>();
if (monster.lightId == 0)
monster.lightId = NO_LIGHT; // Correct incorrect values in old saves
// Omit pointer name;
if (monster.mode == MonsterMode::Petrified)
monster.animInfo.isPetrified = true;
}
/**
* @brief Recalculate the pack size of monster group that may have underflown
*/
void SyncPackSize(Monster &leader)
{
if (!leader.isUnique())
return;
if (leader.ai != MonsterAIID::Scavenger)
return;
leader.packSize = 0;
for (size_t i = 0; i < ActiveMonsterCount; i++) {
Monster &minion = Monsters[ActiveMonsters[i]];
if (minion.leaderRelation == LeaderRelation::Leashed && minion.getLeader() == &leader)
leader.packSize++;
}
}
void LoadMissile(LoadHelper *file)
{
Missile missile = {};
missile._mitype = static_cast<MissileID>(file->NextLE<int32_t>());
missile.position.tile.x = file->NextLE<int32_t>();
missile.position.tile.y = file->NextLE<int32_t>();
missile.position.offset.deltaX = file->NextLE<int32_t>();
missile.position.offset.deltaY = file->NextLE<int32_t>();
missile.position.velocity.deltaX = file->NextLE<int32_t>();
missile.position.velocity.deltaY = file->NextLE<int32_t>();
missile.position.start.x = file->NextLE<int32_t>();
missile.position.start.y = file->NextLE<int32_t>();
missile.position.traveled.deltaX = file->NextLE<int32_t>();
missile.position.traveled.deltaY = file->NextLE<int32_t>();
missile._mimfnum = file->NextLE<int32_t>();
missile._mispllvl = file->NextLE<int32_t>();
missile._miDelFlag = file->NextBool32();
missile._miAnimType = static_cast<MissileGraphicID>(file->NextLE<uint8_t>());
file->Skip(3); // Alignment
missile._miAnimFlags = static_cast<MissileGraphicsFlags>(file->NextLE<int32_t>());
file->Skip(4); // Skip pointer _miAnimData
missile._miAnimDelay = file->NextLE<int32_t>();
missile._miAnimLen = file->NextLE<int32_t>();
missile._miAnimWidth = file->NextLE<int32_t>();
missile._miAnimWidth2 = file->NextLE<int32_t>();
missile._miAnimCnt = file->NextLE<int32_t>();
missile._miAnimAdd = file->NextLE<int32_t>();
missile._miAnimFrame = file->NextLE<int32_t>();
missile._miDrawFlag = file->NextBool32();
missile._miLightFlag = file->NextBool32();
missile._miPreFlag = file->NextBool32();
missile._miUniqTrans = file->NextLE<uint32_t>();
missile.duration = file->NextLE<int32_t>();
missile._misource = file->NextLE<int32_t>();
missile._micaster = static_cast<mienemy_type>(file->NextLE<int32_t>());
missile._midam = file->NextLE<int32_t>();
missile._miHitFlag = file->NextBool32();
missile._midist = file->NextLE<int32_t>();
missile._mlid = file->NextLE<int32_t>();
missile._mirnd = file->NextLE<int32_t>();
missile.var1 = file->NextLE<int32_t>();
missile.var2 = file->NextLE<int32_t>();
missile.var3 = file->NextLE<int32_t>();
missile.var4 = file->NextLE<int32_t>();
missile.var5 = file->NextLE<int32_t>();
missile.var6 = file->NextLE<int32_t>();
missile.var7 = file->NextLE<int32_t>();
missile.limitReached = file->NextBool32();
missile.lastCollisionTargetHash = 0;
if (Missiles.size() < Missiles.max_size()) {
Missiles.push_back(missile);
}
}
_object_id ConvertFromHellfireObject(_object_id type)
{
if (leveltype == DTYPE_NEST) {
switch (type) {
case OBJ_BARREL:
return OBJ_POD;
case OBJ_BARRELEX:
return OBJ_PODEX;
default:
break;
}
}
if (leveltype == DTYPE_CRYPT) {
switch (type) {
case OBJ_BARREL:
return OBJ_URN;
case OBJ_BARRELEX:
return OBJ_URNEX;
case OBJ_STORYBOOK:
return OBJ_L5BOOKS;
case OBJ_STORYCANDLE:
return OBJ_L5CANDLE;
case OBJ_L1LDOOR:
return OBJ_L5LDOOR;
case OBJ_L1RDOOR:
return OBJ_L5RDOOR;
case OBJ_LEVER:
return OBJ_L5LEVER;
case OBJ_SARC:
return OBJ_L5SARC;
default:
break;
}
}
return type;
}
void LoadObject(LoadHelper &file, Object &object)
{
object._otype = ConvertFromHellfireObject(static_cast<_object_id>(file.NextLE<int32_t>()));
object.position.x = file.NextLE<int32_t>();
object.position.y = file.NextLE<int32_t>();
object.applyLighting = file.NextBool32();
object._oAnimFlag = file.NextBool32();
file.Skip(4); // Skip pointer _oAnimData
object._oAnimDelay = file.NextLE<int32_t>();
object._oAnimCnt = file.NextLE<int32_t>();
object._oAnimLen = file.NextLE<uint32_t>();
object._oAnimFrame = file.NextLE<uint32_t>();
object._oAnimWidth = static_cast<uint16_t>(file.NextLE<int32_t>());
file.Skip(4); // Skip _oAnimWidth2
object._oDelFlag = file.NextBool32();
object._oBreak = file.NextLE<int8_t>();
file.Skip(3); // Alignment
object._oSolidFlag = file.NextBool32();
object._oMissFlag = file.NextBool32();
object.selectionRegion = static_cast<SelectionRegion>(file.NextLE<int8_t>());
file.Skip(3); // Alignment
object._oPreFlag = file.NextBool32();
object._oTrapFlag = file.NextBool32();
object._oDoorFlag = file.NextBool32();
object._olid = file.NextLE<int32_t>();
object._oRndSeed = file.NextLE<uint32_t>();
object._oVar1 = file.NextLE<int32_t>();
object._oVar2 = file.NextLE<int32_t>();
object._oVar3 = file.NextLE<int32_t>();
object._oVar4 = file.NextLE<int32_t>();
object._oVar5 = file.NextLE<int32_t>();
object._oVar6 = file.NextLE<uint32_t>();
object.bookMessage = static_cast<_speech_id>(file.NextLE<int32_t>());
object._oVar8 = file.NextLE<int32_t>();
}
void LoadItem(LoadHelper &file, Item &item)
{
LoadAndValidateItemData(file, item);
GetItemFrm(item);
}
void LoadPremium(LoadHelper &file, int i)
{
LoadAndValidateItemData(file, PremiumItems[i]);
}
void LoadQuest(LoadHelper *file, int i)
{
auto &quest = Quests[i];
quest._qlevel = file->NextLE<uint8_t>();
file->Skip<uint8_t>(); // _qtype, identical to _qidx
quest._qactive = static_cast<quest_state>(file->NextLE<uint8_t>());
quest._qlvltype = static_cast<dungeon_type>(file->NextLE<uint8_t>());
quest.position.x = file->NextLE<int32_t>();
quest.position.y = file->NextLE<int32_t>();
quest._qslvl = static_cast<_setlevels>(file->NextLE<uint8_t>());
quest._qidx = static_cast<quest_id>(file->NextLE<uint8_t>());
if (gbIsHellfireSaveGame) {
file->Skip(2); // Alignment
quest._qmsg = static_cast<_speech_id>(file->NextLE<int32_t>());
} else {
quest._qmsg = static_cast<_speech_id>(file->NextLE<uint8_t>());
}
quest._qvar1 = file->NextLE<uint8_t>();
quest._qvar2 = file->NextLE<uint8_t>();
file->Skip(2); // Alignment
if (!gbIsHellfireSaveGame)
file->Skip(1); // Alignment
quest._qlog = file->NextBool32();
ReturnLvlPosition.x = file->NextBE<int32_t>();
ReturnLvlPosition.y = file->NextBE<int32_t>();
ReturnLevel = file->NextBE<int32_t>();
ReturnLevelType = static_cast<dungeon_type>(file->NextBE<int32_t>());
file->Skip(sizeof(int32_t)); // Skip DoomQuestState
}
void LoadLighting(LoadHelper *file, Light *pLight)
{
pLight->position.tile.x = file->NextLE<int32_t>();
pLight->position.tile.y = file->NextLE<int32_t>();
pLight->radius = file->NextLE<int32_t>();
file->Skip<int32_t>(); // _lid
pLight->isInvalid = file->NextBool32();
pLight->hasChanged = file->NextBool32();
file->Skip(4); // Unused
pLight->position.old.x = file->NextLE<int32_t>();
pLight->position.old.y = file->NextLE<int32_t>();
pLight->oldRadius = file->NextLE<int32_t>();
pLight->position.offset.deltaX = file->NextLE<int32_t>();
pLight->position.offset.deltaY = file->NextLE<int32_t>();
file->Skip<uint32_t>(); // _lflags
}
void LoadPortal(LoadHelper *file, int i)
{
Portal *pPortal = &Portals[i];
pPortal->open = file->NextBool32();
pPortal->position.x = file->NextLE<int32_t>();
pPortal->position.y = file->NextLE<int32_t>();
pPortal->level = file->NextLE<int32_t>();
pPortal->ltype = static_cast<dungeon_type>(file->NextLE<int32_t>());
pPortal->setlvl = file->NextBool32();
if (!pPortal->setlvl)
pPortal->ltype = GetLevelType(pPortal->level);
}
void GetLevelNames(std::string_view prefix, char *out)
{
char suf;
uint8_t num;
if (setlevel) {
suf = 's';
num = static_cast<uint8_t>(setlvlnum);
} else {
suf = 'l';
num = currlevel;
}
*fmt::format_to(out, "{}{}{:02d}", prefix, suf, num) = '\0';
}
void GetTempLevelNames(char *szTemp)
{
return GetLevelNames("temp", szTemp);
}
void GetPermLevelNames(char *szPerm)
{
return GetLevelNames("perm", szPerm);
}
bool LevelFileExists(SaveWriter &archive)
{
char szName[MaxMpqPathSize];
GetTempLevelNames(szName);
if (archive.HasFile(szName))
return true;
GetPermLevelNames(szName);
return archive.HasFile(szName);
}
bool IsShopPriceValid(const Item &item)
{
const int boyPriceLimit = 90000;
if (!gbIsHellfire && (item._iCreateInfo & CF_BOY) != 0 && item._iIvalue > boyPriceLimit)
return false;
const int premiumPriceLimit = 140000;
if (!gbIsHellfire && (item._iCreateInfo & CF_SMITHPREMIUM) != 0 && item._iIvalue > premiumPriceLimit)
return false;
const uint16_t smithOrWitch = CF_SMITH | CF_WITCH;
const int smithAndWitchPriceLimit = gbIsHellfire ? 200000 : 140000;
if ((item._iCreateInfo & smithOrWitch) != 0 && item._iIvalue > smithAndWitchPriceLimit)
return false;
return true;
}
void LoadMatchingItems(LoadHelper &file, const Player &player, const int n, Item *pItem)
{
Item heroItem;
for (int i = 0; i < n; i++) {
Item &unpackedItem = pItem[i];
LoadItemData(file, heroItem);
if (unpackedItem.isEmpty() || heroItem.isEmpty())
continue;
if (unpackedItem._iSeed != heroItem._iSeed)
continue;