diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 96ae43c4c..d3cae16d3 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -2102,11 +2102,23 @@ void SaveStash() file.WriteLE(Stash.gold); - // Current stash size is 50 pages, expanding to 100 in the near future. Will definitely fit in a 32 bit value. - file.WriteLE(static_cast(Stash.stashGrids.size())); + std::vector pagesToSave; for (const auto &stashPage : Stash.stashGrids) { - file.WriteLE(stashPage.first); - for (const auto &row : stashPage.second) { + if (std::any_of(stashPage.second.cbegin(), stashPage.second.cend(), [](const auto &row) { + return std::any_of(row.cbegin(), row.cend(), [](auto cell) { + return cell > 0; + }); + })) { + // found a page that contains at least one item + pagesToSave.push_back(stashPage.first); + } + }; + + // Current stash size is 100 pages. Will definitely fit in a 32 bit value. + file.WriteLE(static_cast(pagesToSave.size())); + for (const auto &page : pagesToSave) { + file.WriteLE(page); + for (const auto &row : Stash.stashGrids[page]) { for (uint16_t cell : row) { file.WriteLE(cell); }