From 2b47f3abccfbebfeea50f9f532aed5abee3a1978 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Wed, 30 Mar 2022 21:36:23 +1100 Subject: [PATCH] Address type conversion warnings in loadsave.cpp Most of this was centred around stash pages being saved/loaded as unsigned values but stored as signed values. Consistently used unsigned since it matches the intended usage. --- Source/loadsave.cpp | 18 ++++++++++-------- Source/qol/stash.cpp | 12 ++++++------ Source/qol/stash.h | 8 ++++---- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 020fb6f8a..bb2ec1fbc 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -69,7 +69,7 @@ T SwapBE(T in) case 4: return SDL_SwapBE32(in); case 8: - return SDL_SwapBE64(in); + return static_cast(SDL_SwapBE64(in)); default: return in; } @@ -1322,10 +1322,10 @@ void SaveMonster(SaveHelper *file, Monster &monster) file->Skip(1); // Alignment file->WriteLE(monster.mExp); - file->WriteLE(std::min(monster.mHit, std::numeric_limits::max())); // For backwards compatibility + file->WriteLE(static_cast(std::min(monster.mHit, std::numeric_limits::max()))); // For backwards compatibility file->WriteLE(monster.mMinDamage); file->WriteLE(monster.mMaxDamage); - file->WriteLE(std::min(monster.mHit2, std::numeric_limits::max())); // For backwards compatibility + file->WriteLE(static_cast(std::min(monster.mHit2, std::numeric_limits::max()))); // For backwards compatibility file->WriteLE(monster.mMinDamage2); file->WriteLE(monster.mMaxDamage2); file->WriteLE(monster.mArmorClass); @@ -1535,7 +1535,7 @@ constexpr uint32_t VersionAdditionalMissiles = 0; void SaveAdditionalMissiles() { constexpr size_t BytesWrittenBySaveMissile = 180; - size_t missileCountAdditional = (Missiles.size() > MaxMissilesForSaveGame) ? Missiles.size() - MaxMissilesForSaveGame : 0; + uint32_t missileCountAdditional = (Missiles.size() > MaxMissilesForSaveGame) ? static_cast(Missiles.size() - MaxMissilesForSaveGame) : 0; SaveHelper file(CurrentSaveArchive(), "additionalMissiles", sizeof(uint32_t) + sizeof(uint32_t) + (missileCountAdditional * BytesWrittenBySaveMissile)); file.WriteLE(VersionAdditionalMissiles); @@ -2061,17 +2061,19 @@ void SaveStash() file.WriteLE(Stash.gold); - file.WriteLE(Stash.stashGrids.size()); - for (auto stashPage : Stash.stashGrids) { + // 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())); + for (const auto &stashPage : Stash.stashGrids) { file.WriteLE(stashPage.first); - for (auto row : stashPage.second) { + for (const auto &row : stashPage.second) { for (uint16_t cell : row) { file.WriteLE(cell); } } } - file.WriteLE(Stash.stashList.size()); + // 100 pages of 100 items is still only 10 000, as with the page count will definitely fit in 32 bits even in the worst case. + file.WriteLE(static_cast(Stash.stashList.size())); for (const Item &item : Stash.stashList) { SaveItem(file, item); } diff --git a/Source/qol/stash.cpp b/Source/qol/stash.cpp index e971578d0..dfb8c9a33 100644 --- a/Source/qol/stash.cpp +++ b/Source/qol/stash.cpp @@ -28,7 +28,7 @@ int WithdrawGoldValue; namespace { -constexpr int CountStashPages = 50; +constexpr unsigned CountStashPages = 50; int InitialWithdrawGoldValue; @@ -50,7 +50,7 @@ Art StashNavButtonArt; * @param stashListIndex The item's StashList index * @param itemSize Size of item */ -void AddItemToStashGrid(int page, Point position, uint16_t stashListIndex, Size itemSize) +void AddItemToStashGrid(unsigned page, Point position, uint16_t stashListIndex, Size itemSize) { for (auto point : PointsInRectangleRange({ { 0, 0 }, itemSize })) { Stash.stashGrids[page][position.x + point.x][position.y + point.y] = stashListIndex + 1; @@ -539,9 +539,9 @@ void StashStruct::RemoveStashItem(uint16_t iv) Stash.dirty = true; } -void StashStruct::SetPage(int newPage) +void StashStruct::SetPage(unsigned newPage) { - page = clamp(newPage, 0, CountStashPages - 1); + page = std::max(newPage, CountStashPages - 1); } void StashStruct::RefreshItemStatFlags() @@ -635,8 +635,8 @@ bool AutoPlaceItemInStash(Player &player, const Item &item, bool persistItem) Size itemSize = GetInventorySize(item); // Try to add the item to the current active page and if it's not possible move forward - for (int pageCounter = 0; pageCounter < CountStashPages; pageCounter++) { - int pageIndex = Stash.GetPage() + pageCounter; + for (unsigned pageCounter = 0; pageCounter < CountStashPages; pageCounter++) { + unsigned pageIndex = Stash.GetPage() + pageCounter; // Wrap around if needed if (pageIndex >= CountStashPages) pageIndex -= CountStashPages; diff --git a/Source/qol/stash.h b/Source/qol/stash.h index eef233d23..e2a72dc25 100644 --- a/Source/qol/stash.h +++ b/Source/qol/stash.h @@ -17,23 +17,23 @@ namespace devilution { class StashStruct { public: void RemoveStashItem(uint16_t iv); - std::map, 10>> stashGrids; + std::map, 10>> stashGrids; std::vector stashList; int gold; bool dirty = false; - int GetPage() const + unsigned GetPage() const { return page; } - void SetPage(int newPage); + void SetPage(unsigned newPage); /** @brief Updates _iStatFlag for all stash items. */ void RefreshItemStatFlags(); private: /** Current Page */ - int page; + unsigned page; }; constexpr Point InvalidStashPoint { -1, -1 };