diff --git a/Source/diablo.cpp b/Source/diablo.cpp index ef95b8153..075960a9c 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -510,7 +510,7 @@ void PressKey(int vkey) } else if (AutomapActive) { AutomapUp(); } else if (IsStashOpen) { - Stash.SetPage(Stash.GetPage() - 1); + Stash.PreviousPage(); } } else if (vkey == DVL_VK_DOWN) { if (stextflag != STORE_NONE) { @@ -524,7 +524,7 @@ void PressKey(int vkey) } else if (AutomapActive) { AutomapDown(); } else if (IsStashOpen) { - Stash.SetPage(Stash.GetPage() + 1); + Stash.NextPage(); } } else if (vkey == DVL_VK_PRIOR) { if (stextflag != STORE_NONE) { diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index bb2ec1fbc..f744ca357 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -1809,6 +1809,8 @@ void LoadStash() for (unsigned i = 0; i < itemCount; i++) { LoadItemData(file, Stash.stashList[i]); } + + Stash.SetPage(file.NextLE()); } void RemoveEmptyInventory(Player &player) @@ -2055,7 +2057,8 @@ void SaveStash() + sizeof(uint32_t) + (sizeof(uint32_t) + 10 * 10 * sizeof(uint16_t)) * Stash.stashGrids.size() + sizeof(uint32_t) - + itemSize * Stash.stashList.size()); + + itemSize * Stash.stashList.size() + + sizeof(uint32_t)); file.WriteLE(StashVersion); @@ -2077,6 +2080,8 @@ void SaveStash() for (const Item &item : Stash.stashList) { SaveItem(file, item); } + + file.WriteLE(static_cast(Stash.GetPage())); } void SaveGameData() diff --git a/Source/miniwin/misc_msg.cpp b/Source/miniwin/misc_msg.cpp index 96d3a56dc..ddf0ec081 100644 --- a/Source/miniwin/misc_msg.cpp +++ b/Source/miniwin/misc_msg.cpp @@ -320,13 +320,13 @@ void ProcessGamepadEvents(GameAction &action) break; case GameActionType_USE_HEALTH_POTION: if (IsStashOpen) - Stash.SetPage(Stash.GetPage() - 1); + Stash.PreviousPage(); else UseBeltItem(BLT_HEALING); break; case GameActionType_USE_MANA_POTION: if (IsStashOpen) - Stash.SetPage(Stash.GetPage() + 1); + Stash.NextPage(); else UseBeltItem(BLT_MANA); break; diff --git a/Source/qol/stash.cpp b/Source/qol/stash.cpp index 96ad99e81..16d7985fa 100644 --- a/Source/qol/stash.cpp +++ b/Source/qol/stash.cpp @@ -29,6 +29,7 @@ int WithdrawGoldValue; namespace { constexpr unsigned CountStashPages = 50; +constexpr unsigned LastStashPage = CountStashPages - 1; int InitialWithdrawGoldValue; @@ -293,19 +294,19 @@ void CheckStashButtonRelease(Point mousePosition) if (stashButton.Contains(mousePosition)) { switch (StashButtonPressed) { case 0: - Stash.SetPage(Stash.GetPage() - 10); + Stash.PreviousPage(10); break; case 1: - Stash.SetPage(Stash.GetPage() - 1); + Stash.PreviousPage(); break; case 2: StartGoldWithdraw(); break; case 3: - Stash.SetPage(Stash.GetPage() + 1); + Stash.NextPage(); break; case 4: - Stash.SetPage(Stash.GetPage() + 10); + Stash.NextPage(10); break; } } @@ -534,7 +535,28 @@ void StashStruct::RemoveStashItem(uint16_t iv) void StashStruct::SetPage(unsigned newPage) { - page = std::min(newPage, CountStashPages - 1); + page = std::min(newPage, LastStashPage); + dirty = true; +} + +void StashStruct::NextPage(unsigned offset) +{ + if (page <= LastStashPage) { + page += std::min(offset, LastStashPage - page); + } else { + page = LastStashPage; + } + dirty = true; +} + +void StashStruct::PreviousPage(unsigned offset) +{ + if (page <= LastStashPage) { + page -= std::min(offset, page); + } else { + page = LastStashPage; + } + dirty = true; } void StashStruct::RefreshItemStatFlags() diff --git a/Source/qol/stash.h b/Source/qol/stash.h index e4f81cc50..42b04f548 100644 --- a/Source/qol/stash.h +++ b/Source/qol/stash.h @@ -28,6 +28,9 @@ public: } void SetPage(unsigned newPage); + void NextPage(unsigned offset = 1); + void PreviousPage(unsigned offset = 1); + /** @brief Updates _iStatFlag for all stash items. */ void RefreshItemStatFlags();