Browse Source

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.
pull/4262/head^2
ephphatha 4 years ago committed by Anders Jenbo
parent
commit
2b47f3abcc
  1. 18
      Source/loadsave.cpp
  2. 12
      Source/qol/stash.cpp
  3. 8
      Source/qol/stash.h

18
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<T>(SDL_SwapBE64(in));
default:
return in;
}
@ -1322,10 +1322,10 @@ void SaveMonster(SaveHelper *file, Monster &monster)
file->Skip(1); // Alignment
file->WriteLE<uint16_t>(monster.mExp);
file->WriteLE<uint8_t>(std::min<uint16_t>(monster.mHit, std::numeric_limits<uint8_t>::max())); // For backwards compatibility
file->WriteLE<uint8_t>(static_cast<uint8_t>(std::min<uint16_t>(monster.mHit, std::numeric_limits<uint8_t>::max()))); // For backwards compatibility
file->WriteLE<uint8_t>(monster.mMinDamage);
file->WriteLE<uint8_t>(monster.mMaxDamage);
file->WriteLE<uint8_t>(std::min<uint16_t>(monster.mHit2, std::numeric_limits<uint8_t>::max())); // For backwards compatibility
file->WriteLE<uint8_t>(static_cast<uint8_t>(std::min<uint16_t>(monster.mHit2, std::numeric_limits<uint8_t>::max()))); // For backwards compatibility
file->WriteLE<uint8_t>(monster.mMinDamage2);
file->WriteLE<uint8_t>(monster.mMaxDamage2);
file->WriteLE<uint8_t>(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<uint32_t>(Missiles.size() - MaxMissilesForSaveGame) : 0;
SaveHelper file(CurrentSaveArchive(), "additionalMissiles", sizeof(uint32_t) + sizeof(uint32_t) + (missileCountAdditional * BytesWrittenBySaveMissile));
file.WriteLE<uint32_t>(VersionAdditionalMissiles);
@ -2061,17 +2061,19 @@ void SaveStash()
file.WriteLE<uint32_t>(Stash.gold);
file.WriteLE<uint32_t>(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<uint32_t>(static_cast<uint32_t>(Stash.stashGrids.size()));
for (const auto &stashPage : Stash.stashGrids) {
file.WriteLE<uint32_t>(stashPage.first);
for (auto row : stashPage.second) {
for (const auto &row : stashPage.second) {
for (uint16_t cell : row) {
file.WriteLE<uint16_t>(cell);
}
}
}
file.WriteLE<uint32_t>(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<uint32_t>(static_cast<uint32_t>(Stash.stashList.size()));
for (const Item &item : Stash.stashList) {
SaveItem(file, item);
}

12
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;

8
Source/qol/stash.h

@ -17,23 +17,23 @@ namespace devilution {
class StashStruct {
public:
void RemoveStashItem(uint16_t iv);
std::map<int, std::array<std::array<uint16_t, 10>, 10>> stashGrids;
std::map<unsigned, std::array<std::array<uint16_t, 10>, 10>> stashGrids;
std::vector<Item> 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 };

Loading…
Cancel
Save