From 385f22f4f398cc401f41d599e39e53468e95c4a1 Mon Sep 17 00:00:00 2001 From: staphen Date: Sun, 3 Sep 2023 20:18:35 -0400 Subject: [PATCH 1/2] Restore packed item state when regenerating an item using heroitem data --- Source/loadsave.cpp | 5 +++++ Source/pack.cpp | 6 ------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 9a959b92642..14b64c5734e 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -969,6 +969,11 @@ void LoadMatchingItems(LoadHelper &file, const Player &player, const int n, Item if ((heroItem.dwBuff & CF_HELLFIRE) != (unpackedItem.dwBuff & CF_HELLFIRE)) { unpackedItem = {}; RecreateItem(player, unpackedItem, heroItem.IDidx, heroItem._iCreateInfo, heroItem._iSeed, heroItem._ivalue, (heroItem.dwBuff & CF_HELLFIRE) != 0); + unpackedItem._iIdentified = heroItem._iIdentified; + unpackedItem._iMaxDur = heroItem._iMaxDur; + unpackedItem._iDurability = ClampDurability(unpackedItem, heroItem._iDurability); + unpackedItem._iMaxCharges = std::clamp(heroItem._iMaxCharges, 0, unpackedItem._iMaxCharges); + unpackedItem._iCharges = std::clamp(heroItem._iCharges, 0, unpackedItem._iMaxCharges); } if (!IsShopPriceValid(unpackedItem)) { unpackedItem.clear(); diff --git a/Source/pack.cpp b/Source/pack.cpp index 04168b78e13..4c1d7c2bbed 100644 --- a/Source/pack.cpp +++ b/Source/pack.cpp @@ -407,13 +407,7 @@ 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); - - if (isHellfire) - item.dwBuff |= CF_HELLFIRE; - else - item.dwBuff &= ~CF_HELLFIRE; } } From 26636553f02eb38641173cdef4fa8c99984e3065 Mon Sep 17 00:00:00 2001 From: staphen Date: Mon, 4 Sep 2023 21:14:30 -0400 Subject: [PATCH 2/2] Remove invalid items after loading heroitem data --- Source/loadsave.cpp | 18 +++++++++++------- Source/pack.cpp | 1 - Source/pfile.cpp | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 14b64c5734e..88e5e5af18f 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); } @@ -489,10 +493,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(); @@ -500,9 +504,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(); @@ -824,13 +828,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) @@ -2075,7 +2079,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 4c1d7c2bbed..38ae52474ce 100644 --- a/Source/pack.cpp +++ b/Source/pack.cpp @@ -407,7 +407,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 02f6ccd8d0f..a358e839242 100644 --- a/Source/pfile.cpp +++ b/Source/pfile.cpp @@ -509,6 +509,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 @@ -664,7 +675,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); @@ -750,7 +761,7 @@ void pfile_read_player_from_save(uint32_t saveNum, Player &player) UnPackPlayer(pkplr, player); LoadHeroItems(player); - RemoveEmptyInventory(player); + RemoveAllInvalidItems(player); CalcPlrInv(player, false); }