Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore packed item state when regenerating an item using heroitem data #6567

Merged
merged 2 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 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 @@ -489,20 +493,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 @@ -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)
Expand Down Expand Up @@ -969,6 +973,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<int>(heroItem._iMaxCharges, 0, unpackedItem._iMaxCharges);
unpackedItem._iCharges = std::clamp<int>(heroItem._iCharges, 0, unpackedItem._iMaxCharges);
}
if (!IsShopPriceValid(unpackedItem)) {
unpackedItem.clear();
Expand Down Expand Up @@ -2070,7 +2079,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
7 changes: 0 additions & 7 deletions Source/pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,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);

if (isHellfire)
item.dwBuff |= CF_HELLFIRE;
else
item.dwBuff &= ~CF_HELLFIRE;
}
}

Expand Down
15 changes: 13 additions & 2 deletions Source/pfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
Loading