Skip to content

Commit

Permalink
Remove invalid items after loading heroitem data
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCWills authored and AJenbo committed Nov 19, 2023
1 parent 293ad81 commit c4792be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
18 changes: 11 additions & 7 deletions Source/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ void LoadItemData(LoadHelper &file, Item &item)
else
item._iDamAcFlags = ItemSpecialEffectHf::None;
UpdateHellfireFlag(item, item._iIName);
}

void LoadAndValidateItemData(LoadHelper &file, Item &item)
{
LoadItemData(file, item);
RemoveInvalidItem(item);
}

Expand Down Expand Up @@ -487,20 +491,20 @@ void LoadPlayer(LoadHelper &file, Player &player)
file.Skip<uint32_t>(); // skip _pBWidth

for (Item &item : player.InvBody)
LoadItemData(file, item);
LoadAndValidateItemData(file, item);

for (Item &item : player.InvList)
LoadItemData(file, item);
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)
LoadItemData(file, item);
LoadAndValidateItemData(file, item);

LoadItemData(file, player.HoldItem);
LoadAndValidateItemData(file, player.HoldItem);

player._pIMinDam = file.NextLE<int32_t>();
player._pIMaxDam = file.NextLE<int32_t>();
Expand Down Expand Up @@ -822,13 +826,13 @@ void LoadObject(LoadHelper &file, Object &object)

void LoadItem(LoadHelper &file, Item &item)
{
LoadItemData(file, item);
LoadAndValidateItemData(file, item);
GetItemFrm(item);
}

void LoadPremium(LoadHelper &file, int i)
{
LoadItemData(file, premiumitems[i]);
LoadAndValidateItemData(file, premiumitems[i]);
}

void LoadQuest(LoadHelper *file, int i)
Expand Down Expand Up @@ -2073,7 +2077,7 @@ void LoadStash()
auto itemCount = file.NextLE<uint32_t>();
Stash.stashList.resize(itemCount);
for (unsigned i = 0; i < itemCount; i++) {
LoadItemData(file, Stash.stashList[i]);
LoadAndValidateItemData(file, Stash.stashList[i]);
}

Stash.SetPage(file.NextLE<uint32_t>());
Expand Down
1 change: 0 additions & 1 deletion Source/pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ void UnPackItem(const ItemPack &packedItem, const Player &player, Item &item, bo
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);
}
}

Expand Down
15 changes: 13 additions & 2 deletions Source/pfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,17 @@ void pfile_write_hero(SaveWriter &saveWriter, bool writeGameData)
}
}

void RemoveAllInvalidItems(Player &player)
{
for (int i = 0; i < NUM_INVLOC; i++)
RemoveInvalidItem(player.InvBody[i]);
for (int i = 0; i < player._pNumInv; i++)
RemoveInvalidItem(player.InvList[i]);
for (int i = 0; i < MaxBeltItems; i++)
RemoveInvalidItem(player.SpdList[i]);
RemoveEmptyInventory(player);
}

} // namespace

#ifdef UNPACKED_SAVES
Expand Down Expand Up @@ -666,7 +677,7 @@ bool pfile_ui_set_hero_infos(bool (*uiAddHeroInfo)(_uiheroinfo *))

UnPackPlayer(pkplr, player);
LoadHeroItems(player);
RemoveEmptyInventory(player);
RemoveAllInvalidItems(player);
CalcPlrInv(player, false);

Game2UiPlayer(player, &uihero, hasSaveGame);
Expand Down Expand Up @@ -753,7 +764,7 @@ void pfile_read_player_from_save(uint32_t saveNum, Player &player)

UnPackPlayer(pkplr, player);
LoadHeroItems(player);
RemoveEmptyInventory(player);
RemoveAllInvalidItems(player);
CalcPlrInv(player, false);
}

Expand Down

0 comments on commit c4792be

Please sign in to comment.