-
Notifications
You must be signed in to change notification settings - Fork 802
/
pack.cpp
639 lines (544 loc) · 25 KB
/
pack.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
/**
* @file pack.cpp
*
* Implementation of functions for minifying player data structure.
*/
#include "pack.h"
#include <cstdint>
#include "engine/random.hpp"
#include "init.h"
#include "loadsave.h"
#include "playerdat.hpp"
#include "plrmsg.h"
#include "stores.h"
#include "utils/endian.hpp"
#include "utils/log.hpp"
#include "utils/utf8.hpp"
#define ValidateField(logValue, condition) \
do { \
if (!(condition)) { \
LogFailedJoinAttempt(#condition, #logValue, logValue); \
EventFailedJoinAttempt(player._pName); \
return false; \
} \
} while (0)
#define ValidateFields(logValue1, logValue2, condition) \
do { \
if (!(condition)) { \
LogFailedJoinAttempt(#condition, #logValue1, logValue1, #logValue2, logValue2); \
EventFailedJoinAttempt(player._pName); \
return false; \
} \
} while (0)
namespace devilution {
namespace {
void EventFailedJoinAttempt(const char *playerName)
{
std::string message = fmt::format("Player '{}' sent invalid player data during attempt to join the game.", playerName);
EventPlrMsg(message);
}
template <typename T>
void LogFailedJoinAttempt(const char *condition, const char *name, T value)
{
LogDebug("Remote player validation failed: ValidateField({}: {}, {})", name, value, condition);
}
template <typename T1, typename T2>
void LogFailedJoinAttempt(const char *condition, const char *name1, T1 value1, const char *name2, T2 value2)
{
LogDebug("Remote player validation failed: ValidateFields({}: {}, {}: {}, {})", name1, value1, name2, value2, condition);
}
void VerifyGoldSeeds(Player &player)
{
for (int i = 0; i < player._pNumInv; i++) {
if (player.InvList[i].IDidx != IDI_GOLD)
continue;
for (int j = 0; j < player._pNumInv; j++) {
if (i == j)
continue;
if (player.InvList[j].IDidx != IDI_GOLD)
continue;
if (player.InvList[i]._iSeed != player.InvList[j]._iSeed)
continue;
player.InvList[i]._iSeed = AdvanceRndSeed();
j = -1;
}
}
}
void PackNetItem(const Item &item, ItemNetPack &packedItem)
{
packedItem.def.wIndx = static_cast<_item_indexes>(SDL_SwapLE16(item.IDidx));
packedItem.def.wCI = SDL_SwapLE16(item._iCreateInfo);
packedItem.def.dwSeed = SDL_SwapLE32(item._iSeed);
if (item.IDidx != IDI_EAR)
PrepareItemForNetwork(item, packedItem.item);
else
PrepareEarForNetwork(item, packedItem.ear);
}
bool hasMultipleFlags(uint16_t flags)
{
return (flags & (flags - 1)) > 0;
}
bool IsCreationFlagComboValid(uint16_t iCreateInfo)
{
iCreateInfo = iCreateInfo & ~CF_LEVEL;
const bool isTownItem = (iCreateInfo & CF_TOWN) != 0;
const bool isPregenItem = (iCreateInfo & CF_PREGEN) != 0;
const bool isUsefulItem = (iCreateInfo & CF_USEFUL) == CF_USEFUL;
if (isPregenItem)
return false;
if (isUsefulItem && (iCreateInfo & ~CF_USEFUL) != 0)
return false;
if (isTownItem && hasMultipleFlags(iCreateInfo))
return false;
return true;
}
bool IsTownItemValid(uint16_t iCreateInfo, const Player &player)
{
const uint8_t level = iCreateInfo & CF_LEVEL;
const bool isBoyItem = (iCreateInfo & CF_BOY) != 0;
if (isBoyItem && level <= player.getMaxCharacterLevel())
return true;
return level <= 30;
}
bool IsUniqueMonsterItemValid(uint16_t iCreateInfo, uint32_t dwBuff)
{
const uint8_t level = iCreateInfo & CF_LEVEL;
const bool isHellfireItem = (dwBuff & CF_HELLFIRE) != 0;
for (int i = 0; UniqueMonstersData[i].mName != nullptr; i++) {
const auto &uniqueMonsterData = UniqueMonstersData[i];
const auto &uniqueMonsterLevel = static_cast<uint8_t>(MonstersData[uniqueMonsterData.mtype].level);
if (!isHellfireItem && IsAnyOf(uniqueMonsterData.mtype, MT_HORKDMN, MT_DEFILER, MT_NAKRUL)) {
// These monsters don't appear in Diablo
continue;
}
if (level == uniqueMonsterLevel) {
return true;
}
}
return false;
}
bool IsDungeonItemValid(uint16_t iCreateInfo, uint32_t dwBuff)
{
const uint8_t level = iCreateInfo & CF_LEVEL;
const bool isHellfireItem = (dwBuff & CF_HELLFIRE) != 0;
for (int16_t i = 0; i < static_cast<int16_t>(NUM_MTYPES); i++) {
const auto &monsterData = MonstersData[i];
auto monsterLevel = static_cast<uint8_t>(monsterData.level);
if (i != MT_DIABLO && monsterData.availability == MonsterAvailability::Never) {
continue;
}
if (i == MT_DIABLO && !isHellfireItem) {
monsterLevel -= 15;
}
if (level == monsterLevel) {
return true;
}
}
return level <= 30;
}
bool UnPackNetItem(const Player &player, const ItemNetPack &packedItem, Item &item)
{
item = {};
_item_indexes idx = static_cast<_item_indexes>(SDL_SwapLE16(packedItem.def.wIndx));
if (idx < 0 || idx > IDI_LAST)
return true;
if (idx == IDI_EAR) {
RecreateEar(item, SDL_SwapLE16(packedItem.ear.wCI), SDL_SwapLE32(packedItem.ear.dwSeed), packedItem.ear.bCursval, packedItem.ear.heroname);
return true;
}
uint16_t creationFlags = SDL_SwapLE16(packedItem.item.wCI);
uint32_t dwBuff = SDL_SwapLE16(packedItem.item.dwBuff);
if (idx != IDI_GOLD)
ValidateField(creationFlags, IsCreationFlagComboValid(creationFlags));
if ((creationFlags & CF_TOWN) != 0)
ValidateField(creationFlags, IsTownItemValid(creationFlags, player));
else if ((creationFlags & CF_USEFUL) == CF_UPER15)
ValidateFields(creationFlags, dwBuff, IsUniqueMonsterItemValid(creationFlags, dwBuff));
else
ValidateFields(creationFlags, dwBuff, IsDungeonItemValid(creationFlags, dwBuff));
RecreateItem(player, packedItem.item, item);
return true;
}
} // namespace
void PackItem(ItemPack &packedItem, const Item &item, bool isHellfire)
{
packedItem = {};
// Arena potions don't exist in vanilla so don't save them to stay backward compatible
if (item.isEmpty() || item._iMiscId == IMISC_ARENAPOT) {
packedItem.idx = 0xFFFF;
} else {
auto idx = item.IDidx;
if (!isHellfire) {
idx = RemapItemIdxToDiablo(idx);
}
if (gbIsSpawn) {
idx = RemapItemIdxToSpawn(idx);
}
packedItem.idx = SDL_SwapLE16(idx);
if (item.IDidx == IDI_EAR) {
packedItem.iCreateInfo = SDL_SwapLE16(item._iIName[1] | (item._iIName[0] << 8));
packedItem.iSeed = SDL_SwapLE32(LoadBE32(&item._iIName[2]));
packedItem.bId = item._iIName[6];
packedItem.bDur = item._iIName[7];
packedItem.bMDur = item._iIName[8];
packedItem.bCh = item._iIName[9];
packedItem.bMCh = item._iIName[10];
packedItem.wValue = SDL_SwapLE16(item._ivalue | (item._iIName[11] << 8) | ((item._iCurs - ICURS_EAR_SORCERER) << 6));
packedItem.dwBuff = SDL_SwapLE32(LoadBE32(&item._iIName[12]));
} else {
packedItem.iSeed = SDL_SwapLE32(item._iSeed);
packedItem.iCreateInfo = SDL_SwapLE16(item._iCreateInfo);
packedItem.bId = (item._iMagical << 1) | (item._iIdentified ? 1 : 0);
if (item._iMaxDur > 255)
packedItem.bMDur = 254;
else
packedItem.bMDur = item._iMaxDur;
packedItem.bDur = std::min<int32_t>(item._iDurability, packedItem.bMDur);
packedItem.bCh = item._iCharges;
packedItem.bMCh = item._iMaxCharges;
if (item.IDidx == IDI_GOLD)
packedItem.wValue = SDL_SwapLE16(item._ivalue);
packedItem.dwBuff = item.dwBuff;
}
}
}
void PackPlayer(PlayerPack &packed, const Player &player)
{
memset(&packed, 0, sizeof(packed));
packed.destAction = player.destAction;
packed.destParam1 = player.destParam1;
packed.destParam2 = player.destParam2;
packed.plrlevel = player.plrlevel;
packed.px = player.position.tile.x;
packed.py = player.position.tile.y;
if (gbVanilla) {
packed.targx = player.position.tile.x;
packed.targy = player.position.tile.y;
}
CopyUtf8(packed.pName, player._pName, sizeof(packed.pName));
packed.pClass = static_cast<uint8_t>(player._pClass);
packed.pBaseStr = player._pBaseStr;
packed.pBaseMag = player._pBaseMag;
packed.pBaseDex = player._pBaseDex;
packed.pBaseVit = player._pBaseVit;
packed.pLevel = player.getCharacterLevel();
packed.pStatPts = player._pStatPts;
packed.pExperience = SDL_SwapLE32(player._pExperience);
packed.pGold = SDL_SwapLE32(player._pGold);
packed.pHPBase = SDL_SwapLE32(player._pHPBase);
packed.pMaxHPBase = SDL_SwapLE32(player._pMaxHPBase);
packed.pManaBase = SDL_SwapLE32(player._pManaBase);
packed.pMaxManaBase = SDL_SwapLE32(player._pMaxManaBase);
packed.pMemSpells = SDL_SwapLE64(player._pMemSpells);
for (int i = 0; i < 37; i++) // Should be MAX_SPELLS but set to 37 to make save games compatible
packed.pSplLvl[i] = player._pSplLvl[i];
for (int i = 37; i < 47; i++)
packed.pSplLvl2[i - 37] = player._pSplLvl[i];
for (int i = 0; i < NUM_INVLOC; i++)
PackItem(packed.InvBody[i], player.InvBody[i], gbIsHellfire);
packed._pNumInv = player._pNumInv;
for (int i = 0; i < packed._pNumInv; i++)
PackItem(packed.InvList[i], player.InvList[i], gbIsHellfire);
for (int i = 0; i < InventoryGridCells; i++)
packed.InvGrid[i] = player.InvGrid[i];
for (int i = 0; i < MaxBeltItems; i++)
PackItem(packed.SpdList[i], player.SpdList[i], gbIsHellfire);
packed.wReflections = SDL_SwapLE16(player.wReflections);
packed.pDamAcFlags = SDL_SwapLE32(static_cast<uint32_t>(player.pDamAcFlags));
packed.pDiabloKillLevel = SDL_SwapLE32(player.pDiabloKillLevel);
packed.bIsHellfire = gbIsHellfire ? 1 : 0;
}
void PackNetPlayer(PlayerNetPack &packed, const Player &player)
{
packed.plrlevel = player.plrlevel;
packed.px = player.position.tile.x;
packed.py = player.position.tile.y;
CopyUtf8(packed.pName, player._pName, sizeof(packed.pName));
packed.pClass = static_cast<uint8_t>(player._pClass);
packed.pBaseStr = player._pBaseStr;
packed.pBaseMag = player._pBaseMag;
packed.pBaseDex = player._pBaseDex;
packed.pBaseVit = player._pBaseVit;
packed.pLevel = player.getCharacterLevel();
packed.pStatPts = player._pStatPts;
packed.pExperience = SDL_SwapLE32(player._pExperience);
packed.pHPBase = SDL_SwapLE32(player._pHPBase);
packed.pMaxHPBase = SDL_SwapLE32(player._pMaxHPBase);
packed.pManaBase = SDL_SwapLE32(player._pManaBase);
packed.pMaxManaBase = SDL_SwapLE32(player._pMaxManaBase);
packed.pMemSpells = SDL_SwapLE64(player._pMemSpells);
for (int i = 0; i < MAX_SPELLS; i++)
packed.pSplLvl[i] = player._pSplLvl[i];
for (int i = 0; i < NUM_INVLOC; i++)
PackNetItem(player.InvBody[i], packed.InvBody[i]);
packed._pNumInv = player._pNumInv;
for (int i = 0; i < packed._pNumInv; i++)
PackNetItem(player.InvList[i], packed.InvList[i]);
for (int i = 0; i < InventoryGridCells; i++)
packed.InvGrid[i] = player.InvGrid[i];
for (int i = 0; i < MaxBeltItems; i++)
PackNetItem(player.SpdList[i], packed.SpdList[i]);
packed.wReflections = SDL_SwapLE16(player.wReflections);
packed.pDiabloKillLevel = player.pDiabloKillLevel;
packed.pManaShield = player.pManaShield;
packed.friendlyMode = player.friendlyMode ? 1 : 0;
packed.isOnSetLevel = player.plrIsOnSetLevel;
packed.pStrength = SDL_SwapLE32(player._pStrength);
packed.pMagic = SDL_SwapLE32(player._pMagic);
packed.pDexterity = SDL_SwapLE32(player._pDexterity);
packed.pVitality = SDL_SwapLE32(player._pVitality);
packed.pHitPoints = SDL_SwapLE32(player._pHitPoints);
packed.pMaxHP = SDL_SwapLE32(player._pMaxHP);
packed.pMana = SDL_SwapLE32(player._pMana);
packed.pMaxMana = SDL_SwapLE32(player._pMaxMana);
packed.pDamageMod = SDL_SwapLE32(player._pDamageMod);
packed.pBaseToBlk = SDL_SwapLE32(player._pBaseToBlk);
packed.pIMinDam = SDL_SwapLE32(player._pIMinDam);
packed.pIMaxDam = SDL_SwapLE32(player._pIMaxDam);
packed.pIAC = SDL_SwapLE32(player._pIAC);
packed.pIBonusDam = SDL_SwapLE32(player._pIBonusDam);
packed.pIBonusToHit = SDL_SwapLE32(player._pIBonusToHit);
packed.pIBonusAC = SDL_SwapLE32(player._pIBonusAC);
packed.pIBonusDamMod = SDL_SwapLE32(player._pIBonusDamMod);
packed.pIGetHit = SDL_SwapLE32(player._pIGetHit);
packed.pIEnAc = SDL_SwapLE32(player._pIEnAc);
packed.pIFMinDam = SDL_SwapLE32(player._pIFMinDam);
packed.pIFMaxDam = SDL_SwapLE32(player._pIFMaxDam);
packed.pILMinDam = SDL_SwapLE32(player._pILMinDam);
packed.pILMaxDam = SDL_SwapLE32(player._pILMaxDam);
}
void UnPackItem(const ItemPack &packedItem, const Player &player, Item &item, bool isHellfire)
{
auto idx = static_cast<_item_indexes>(SDL_SwapLE16(packedItem.idx));
if (gbIsSpawn) {
idx = RemapItemIdxFromSpawn(idx);
}
if (!isHellfire) {
idx = RemapItemIdxFromDiablo(idx);
}
if (!IsItemAvailable(idx)) {
item.clear();
return;
}
if (idx == IDI_EAR) {
uint16_t ic = SDL_SwapLE16(packedItem.iCreateInfo);
uint32_t iseed = SDL_SwapLE32(packedItem.iSeed);
uint16_t ivalue = SDL_SwapLE16(packedItem.wValue);
int32_t ibuff = SDL_SwapLE32(packedItem.dwBuff);
char heroName[17];
heroName[0] = static_cast<char>((ic >> 8) & 0x7F);
heroName[1] = static_cast<char>(ic & 0x7F);
heroName[2] = static_cast<char>((iseed >> 24) & 0x7F);
heroName[3] = static_cast<char>((iseed >> 16) & 0x7F);
heroName[4] = static_cast<char>((iseed >> 8) & 0x7F);
heroName[5] = static_cast<char>(iseed & 0x7F);
heroName[6] = static_cast<char>(packedItem.bId & 0x7F);
heroName[7] = static_cast<char>(packedItem.bDur & 0x7F);
heroName[8] = static_cast<char>(packedItem.bMDur & 0x7F);
heroName[9] = static_cast<char>(packedItem.bCh & 0x7F);
heroName[10] = static_cast<char>(packedItem.bMCh & 0x7F);
heroName[11] = static_cast<char>((ivalue >> 8) & 0x7F);
heroName[12] = static_cast<char>((ibuff >> 24) & 0x7F);
heroName[13] = static_cast<char>((ibuff >> 16) & 0x7F);
heroName[14] = static_cast<char>((ibuff >> 8) & 0x7F);
heroName[15] = static_cast<char>(ibuff & 0x7F);
heroName[16] = '\0';
RecreateEar(item, ic, iseed, ivalue & 0xFF, heroName);
} else {
item = {};
RecreateItem(player, item, idx, SDL_SwapLE16(packedItem.iCreateInfo), SDL_SwapLE32(packedItem.iSeed), SDL_SwapLE16(packedItem.wValue), isHellfire);
item._iIdentified = (packedItem.bId & 1) != 0;
item._iMaxDur = packedItem.bMDur;
item._iDurability = ClampDurability(item, packedItem.bDur);
item._iMaxCharges = std::clamp<int>(packedItem.bMCh, 0, item._iMaxCharges);
item._iCharges = std::clamp<int>(packedItem.bCh, 0, item._iMaxCharges);
RemoveInvalidItem(item);
if (isHellfire)
item.dwBuff |= CF_HELLFIRE;
else
item.dwBuff &= ~CF_HELLFIRE;
}
}
void UnPackPlayer(const PlayerPack &packed, Player &player)
{
Point position { packed.px, packed.py };
player = {};
player.setCharacterLevel(packed.pLevel);
player._pMaxHPBase = SDL_SwapLE32(packed.pMaxHPBase);
player._pHPBase = SDL_SwapLE32(packed.pHPBase);
player._pHPBase = std::clamp<int32_t>(player._pHPBase, 0, player._pMaxHPBase);
player._pMaxHP = player._pMaxHPBase;
player._pHitPoints = player._pHPBase;
player.position.tile = position;
player.position.future = position;
player.setLevel(std::clamp<int8_t>(packed.plrlevel, 0, NUMLEVELS));
player._pClass = static_cast<HeroClass>(std::clamp<uint8_t>(packed.pClass, 0, enum_size<HeroClass>::value - 1));
ClrPlrPath(player);
player.destAction = ACTION_NONE;
CopyUtf8(player._pName, packed.pName, sizeof(player._pName));
InitPlayer(player, true);
player._pBaseStr = std::min<uint8_t>(packed.pBaseStr, player.GetMaximumAttributeValue(CharacterAttribute::Strength));
player._pStrength = player._pBaseStr;
player._pBaseMag = std::min<uint8_t>(packed.pBaseMag, player.GetMaximumAttributeValue(CharacterAttribute::Magic));
player._pMagic = player._pBaseMag;
player._pBaseDex = std::min<uint8_t>(packed.pBaseDex, player.GetMaximumAttributeValue(CharacterAttribute::Dexterity));
player._pDexterity = player._pBaseDex;
player._pBaseVit = std::min<uint8_t>(packed.pBaseVit, player.GetMaximumAttributeValue(CharacterAttribute::Vitality));
player._pVitality = player._pBaseVit;
player._pStatPts = packed.pStatPts;
player._pExperience = SDL_SwapLE32(packed.pExperience);
player._pGold = SDL_SwapLE32(packed.pGold);
player._pBaseToBlk = PlayersData[static_cast<std::size_t>(player._pClass)].blockBonus;
if ((int)(player._pHPBase & 0xFFFFFFC0) < 64)
player._pHPBase = 64;
player._pMaxManaBase = SDL_SwapLE32(packed.pMaxManaBase);
player._pManaBase = SDL_SwapLE32(packed.pManaBase);
player._pManaBase = std::min<int32_t>(player._pManaBase, player._pMaxManaBase);
player._pMemSpells = SDL_SwapLE64(packed.pMemSpells);
for (int i = 0; i < 37; i++) // Should be MAX_SPELLS but set to 36 to make save games compatible
player._pSplLvl[i] = packed.pSplLvl[i];
for (int i = 37; i < 47; i++)
player._pSplLvl[i] = packed.pSplLvl2[i - 37];
bool isHellfire = packed.bIsHellfire != 0;
for (int i = 0; i < NUM_INVLOC; i++)
UnPackItem(packed.InvBody[i], player, player.InvBody[i], isHellfire);
player._pNumInv = packed._pNumInv;
for (int i = 0; i < player._pNumInv; i++)
UnPackItem(packed.InvList[i], player, player.InvList[i], isHellfire);
for (int i = 0; i < InventoryGridCells; i++)
player.InvGrid[i] = packed.InvGrid[i];
VerifyGoldSeeds(player);
for (int i = 0; i < MaxBeltItems; i++)
UnPackItem(packed.SpdList[i], player, player.SpdList[i], isHellfire);
CalcPlrInv(player, false);
player.wReflections = SDL_SwapLE16(packed.wReflections);
player.pDiabloKillLevel = SDL_SwapLE32(packed.pDiabloKillLevel);
}
bool UnPackNetPlayer(const PlayerNetPack &packed, Player &player)
{
CopyUtf8(player._pName, packed.pName, sizeof(player._pName));
ValidateField(packed.pClass, packed.pClass < enum_size<HeroClass>::value);
player._pClass = static_cast<HeroClass>(packed.pClass);
Point position { packed.px, packed.py };
ValidateFields(position.x, position.y, InDungeonBounds(position));
ValidateField(packed.plrlevel, packed.plrlevel < NUMLEVELS);
ValidateField(packed.pLevel, packed.pLevel >= 1 && packed.pLevel <= player.getMaxCharacterLevel());
int32_t baseHpMax = SDL_SwapLE32(packed.pMaxHPBase);
int32_t baseHp = SDL_SwapLE32(packed.pHPBase);
ValidateFields(baseHp, baseHpMax, baseHp >= 0 && baseHp <= baseHpMax);
int32_t baseManaMax = SDL_SwapLE32(packed.pMaxManaBase);
int32_t baseMana = SDL_SwapLE32(packed.pManaBase);
ValidateFields(baseMana, baseManaMax, baseMana <= baseManaMax);
ValidateFields(packed.pClass, packed.pBaseStr, packed.pBaseStr <= player.GetMaximumAttributeValue(CharacterAttribute::Strength));
ValidateFields(packed.pClass, packed.pBaseMag, packed.pBaseMag <= player.GetMaximumAttributeValue(CharacterAttribute::Magic));
ValidateFields(packed.pClass, packed.pBaseDex, packed.pBaseDex <= player.GetMaximumAttributeValue(CharacterAttribute::Dexterity));
ValidateFields(packed.pClass, packed.pBaseVit, packed.pBaseVit <= player.GetMaximumAttributeValue(CharacterAttribute::Vitality));
ValidateField(packed._pNumInv, packed._pNumInv < InventoryGridCells);
player.setCharacterLevel(packed.pLevel);
player.position.tile = position;
player.position.future = position;
player.plrlevel = packed.plrlevel;
player.plrIsOnSetLevel = packed.isOnSetLevel != 0;
player._pMaxHPBase = baseHpMax;
player._pHPBase = baseHp;
player._pMaxHP = baseHpMax;
player._pHitPoints = baseHp;
ClrPlrPath(player);
player.destAction = ACTION_NONE;
InitPlayer(player, true);
player._pBaseStr = packed.pBaseStr;
player._pStrength = player._pBaseStr;
player._pBaseMag = packed.pBaseMag;
player._pMagic = player._pBaseMag;
player._pBaseDex = packed.pBaseDex;
player._pDexterity = player._pBaseDex;
player._pBaseVit = packed.pBaseVit;
player._pVitality = player._pBaseVit;
player._pStatPts = packed.pStatPts;
player._pExperience = SDL_SwapLE32(packed.pExperience);
player._pBaseToBlk = PlayersData[static_cast<std::size_t>(player._pClass)].blockBonus;
player._pMaxManaBase = baseManaMax;
player._pManaBase = baseMana;
player._pMemSpells = SDL_SwapLE64(packed.pMemSpells);
player.wReflections = SDL_SwapLE16(packed.wReflections);
player.pDiabloKillLevel = packed.pDiabloKillLevel;
player.pManaShield = packed.pManaShield != 0;
player.friendlyMode = packed.friendlyMode != 0;
for (int i = 0; i < MAX_SPELLS; i++)
player._pSplLvl[i] = packed.pSplLvl[i];
for (int i = 0; i < NUM_INVLOC; i++) {
if (!UnPackNetItem(player, packed.InvBody[i], player.InvBody[i]))
return false;
if (player.InvBody[i].isEmpty())
continue;
auto loc = static_cast<int8_t>(player.GetItemLocation(player.InvBody[i]));
switch (i) {
case INVLOC_HEAD:
ValidateField(loc, loc == ILOC_HELM);
break;
case INVLOC_RING_LEFT:
case INVLOC_RING_RIGHT:
ValidateField(loc, loc == ILOC_RING);
break;
case INVLOC_AMULET:
ValidateField(loc, loc == ILOC_AMULET);
break;
case INVLOC_HAND_LEFT:
case INVLOC_HAND_RIGHT:
ValidateField(loc, IsAnyOf(loc, ILOC_ONEHAND, ILOC_TWOHAND));
break;
case INVLOC_CHEST:
ValidateField(loc, loc == ILOC_ARMOR);
break;
}
}
player._pNumInv = packed._pNumInv;
for (int i = 0; i < player._pNumInv; i++) {
if (!UnPackNetItem(player, packed.InvList[i], player.InvList[i]))
return false;
}
for (int i = 0; i < InventoryGridCells; i++)
player.InvGrid[i] = packed.InvGrid[i];
for (int i = 0; i < MaxBeltItems; i++) {
Item &item = player.SpdList[i];
if (!UnPackNetItem(player, packed.SpdList[i], item))
return false;
if (item.isEmpty())
continue;
Size beltItemSize = GetInventorySize(item);
int8_t beltItemType = static_cast<int8_t>(item._itype);
bool beltItemUsable = item.isUsable();
ValidateFields(beltItemSize.width, beltItemSize.height, (beltItemSize == Size { 1, 1 }));
ValidateField(beltItemType, item._itype != ItemType::Gold);
ValidateField(beltItemUsable, beltItemUsable);
}
CalcPlrInv(player, false);
player._pGold = CalculateGold(player);
ValidateFields(player._pStrength, SDL_SwapLE32(packed.pStrength), player._pStrength == SDL_SwapLE32(packed.pStrength));
ValidateFields(player._pMagic, SDL_SwapLE32(packed.pMagic), player._pMagic == SDL_SwapLE32(packed.pMagic));
ValidateFields(player._pDexterity, SDL_SwapLE32(packed.pDexterity), player._pDexterity == SDL_SwapLE32(packed.pDexterity));
ValidateFields(player._pVitality, SDL_SwapLE32(packed.pVitality), player._pVitality == SDL_SwapLE32(packed.pVitality));
ValidateFields(player._pHitPoints, SDL_SwapLE32(packed.pHitPoints), player._pHitPoints == SDL_SwapLE32(packed.pHitPoints));
ValidateFields(player._pMaxHP, SDL_SwapLE32(packed.pMaxHP), player._pMaxHP == SDL_SwapLE32(packed.pMaxHP));
ValidateFields(player._pMana, SDL_SwapLE32(packed.pMana), player._pMana == SDL_SwapLE32(packed.pMana));
ValidateFields(player._pMaxMana, SDL_SwapLE32(packed.pMaxMana), player._pMaxMana == SDL_SwapLE32(packed.pMaxMana));
ValidateFields(player._pDamageMod, SDL_SwapLE32(packed.pDamageMod), player._pDamageMod == SDL_SwapLE32(packed.pDamageMod));
ValidateFields(player._pBaseToBlk, SDL_SwapLE32(packed.pBaseToBlk), player._pBaseToBlk == SDL_SwapLE32(packed.pBaseToBlk));
ValidateFields(player._pIMinDam, SDL_SwapLE32(packed.pIMinDam), player._pIMinDam == SDL_SwapLE32(packed.pIMinDam));
ValidateFields(player._pIMaxDam, SDL_SwapLE32(packed.pIMaxDam), player._pIMaxDam == SDL_SwapLE32(packed.pIMaxDam));
ValidateFields(player._pIAC, SDL_SwapLE32(packed.pIAC), player._pIAC == SDL_SwapLE32(packed.pIAC));
ValidateFields(player._pIBonusDam, SDL_SwapLE32(packed.pIBonusDam), player._pIBonusDam == SDL_SwapLE32(packed.pIBonusDam));
ValidateFields(player._pIBonusToHit, SDL_SwapLE32(packed.pIBonusToHit), player._pIBonusToHit == SDL_SwapLE32(packed.pIBonusToHit));
ValidateFields(player._pIBonusAC, SDL_SwapLE32(packed.pIBonusAC), player._pIBonusAC == SDL_SwapLE32(packed.pIBonusAC));
ValidateFields(player._pIBonusDamMod, SDL_SwapLE32(packed.pIBonusDamMod), player._pIBonusDamMod == SDL_SwapLE32(packed.pIBonusDamMod));
ValidateFields(player._pIGetHit, SDL_SwapLE32(packed.pIGetHit), player._pIGetHit == SDL_SwapLE32(packed.pIGetHit));
ValidateFields(player._pIEnAc, SDL_SwapLE32(packed.pIEnAc), player._pIEnAc == SDL_SwapLE32(packed.pIEnAc));
ValidateFields(player._pIFMinDam, SDL_SwapLE32(packed.pIFMinDam), player._pIFMinDam == SDL_SwapLE32(packed.pIFMinDam));
ValidateFields(player._pIFMaxDam, SDL_SwapLE32(packed.pIFMaxDam), player._pIFMaxDam == SDL_SwapLE32(packed.pIFMaxDam));
ValidateFields(player._pILMinDam, SDL_SwapLE32(packed.pILMinDam), player._pILMinDam == SDL_SwapLE32(packed.pILMinDam));
ValidateFields(player._pILMaxDam, SDL_SwapLE32(packed.pILMaxDam), player._pILMaxDam == SDL_SwapLE32(packed.pILMaxDam));
ValidateFields(player._pMaxHPBase, player.calculateBaseLife(), player._pMaxHPBase <= player.calculateBaseLife());
ValidateFields(player._pMaxManaBase, player.calculateBaseMana(), player._pMaxManaBase <= player.calculateBaseMana());
return true;
}
} // namespace devilution