diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 90253d3d6..c3b9eca1d 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -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); } @@ -487,10 +491,10 @@ void LoadPlayer(LoadHelper &file, Player &player) file.Skip(); // 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(); @@ -498,9 +502,9 @@ void LoadPlayer(LoadHelper &file, Player &player) cell = file.NextLE(); for (Item &item : player.SpdList) - LoadItemData(file, item); + LoadAndValidateItemData(file, item); - LoadItemData(file, player.HoldItem); + LoadAndValidateItemData(file, player.HoldItem); player._pIMinDam = file.NextLE(); player._pIMaxDam = file.NextLE(); @@ -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) @@ -2073,7 +2077,7 @@ void LoadStash() auto itemCount = file.NextLE(); 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()); diff --git a/Source/pack.cpp b/Source/pack.cpp index 617346688..7fe7c8ab9 100644 --- a/Source/pack.cpp +++ b/Source/pack.cpp @@ -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(packedItem.bMCh, 0, item._iMaxCharges); item._iCharges = std::clamp(packedItem.bCh, 0, item._iMaxCharges); - RemoveInvalidItem(item); } } diff --git a/Source/pfile.cpp b/Source/pfile.cpp index ebb0f814d..333e921cf 100644 --- a/Source/pfile.cpp +++ b/Source/pfile.cpp @@ -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 @@ -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); @@ -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); }