From 7985a9211d1eba17bdb4167c1b7d134c252d7543 Mon Sep 17 00:00:00 2001 From: obligaron Date: Sat, 22 Apr 2023 14:30:59 +0200 Subject: [PATCH] Change Player::_pSplLvl to uint8_t --- Source/debug.cpp | 4 ++-- Source/loadsave.cpp | 8 ++++---- Source/pack.h | 8 ++++---- Source/player.cpp | 6 +++--- Source/player.h | 6 +++--- Source/stores.cpp | 2 +- test/player_test.cpp | 2 +- test/player_test.h | 24 +++++++----------------- test/writehero_test.cpp | 2 +- 9 files changed, 26 insertions(+), 36 deletions(-) diff --git a/Source/debug.cpp b/Source/debug.cpp index cc326d8c6..9ee4b1f28 100644 --- a/Source/debug.cpp +++ b/Source/debug.cpp @@ -92,7 +92,7 @@ uint32_t glMid2Seed[NUMLEVELS]; uint32_t glMid3Seed[NUMLEVELS]; uint32_t glEndSeed[NUMLEVELS]; -void SetSpellLevelCheat(SpellID spl, int spllvl) +void SetSpellLevelCheat(SpellID spl, uint8_t spllvl) { Player &myPlayer = *MyPlayer; @@ -529,7 +529,7 @@ std::string DebugCmdMinStats(const string_view parameter) std::string DebugCmdSetSpellsLevel(const string_view parameter) { - int level = std::max(0, atoi(parameter.data())); + uint8_t level = static_cast(std::max(0, atoi(parameter.data()))); for (int i = static_cast(SpellID::Firebolt); i < MAX_SPELLS; i++) { if (GetSpellBookLevel(static_cast(i)) != -1) { SetSpellLevelCheat(static_cast(i), level); diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 538032de7..8b58b2d37 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -374,8 +374,8 @@ void LoadPlayer(LoadHelper &file, Player &player) file.Skip(3); // Alignment player._pSBkSpell = static_cast(file.NextLE()); file.Skip(); // Skip _pSBkSplType - for (int8_t &spellLevel : player._pSplLvl) - spellLevel = file.NextLE(); + for (uint8_t &spellLevel : player._pSplLvl) + spellLevel = file.NextLE(); file.Skip(7); // Alignment player._pMemSpells = file.NextLE(); player._pAblSpells = file.NextLE(); @@ -1149,8 +1149,8 @@ void SavePlayer(SaveHelper &file, const Player &player) file.WriteLE(static_cast(player._pSBkSpell)); file.Skip(); // Skip _pSBkSplType - for (int8_t spellLevel : player._pSplLvl) - file.WriteLE(spellLevel); + for (uint8_t spellLevel : player._pSplLvl) + file.WriteLE(spellLevel); file.Skip(7); // Alignment file.WriteLE(player._pMemSpells); diff --git a/Source/pack.h b/Source/pack.h index 89b8f2de5..d515a0d16 100644 --- a/Source/pack.h +++ b/Source/pack.h @@ -52,7 +52,7 @@ struct PlayerPack { int32_t pMaxHPBase; int32_t pManaBase; int32_t pMaxManaBase; - int8_t pSplLvl[37]; // Should be MAX_SPELLS but set to 37 to make save games compatible + uint8_t pSplLvl[37]; // Should be MAX_SPELLS but set to 37 to make save games compatible uint64_t pMemSpells; ItemPack InvBody[NUM_INVLOC]; ItemPack InvList[InventoryGridCells]; @@ -69,9 +69,9 @@ struct PlayerPack { int8_t bIsHellfire; int8_t bReserved; // For future use uint16_t wReflections; - int16_t wReserved2; // For future use - int8_t pSplLvl2[10]; // Hellfire spells - int16_t wReserved8; // For future use + int16_t wReserved2; // For future use + uint8_t pSplLvl2[10]; // Hellfire spells + int16_t wReserved8; // For future use uint32_t pDiabloKillLevel; uint32_t pDifficulty; uint32_t pDamAcFlags; // `ItemSpecialEffectHf` is 1 byte but this is 4 bytes. diff --git a/Source/player.cpp b/Source/player.cpp index c99735033..295c84cdb 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -1825,7 +1825,7 @@ bool Player::isWalking() const int Player::GetManaShieldDamageReduction() { - constexpr int8_t Max = 7; + constexpr uint8_t Max = 7; return 24 - std::min(_pSplLvl[static_cast(SpellID::ManaShield)], Max) * 3; } @@ -2357,7 +2357,7 @@ void CreatePlayer(Player &player, HeroClass c) player._pMemSpells = 0; } - for (int8_t &spellLevel : player._pSplLvl) { + for (uint8_t &spellLevel : player._pSplLvl) { spellLevel = 0; } @@ -2871,7 +2871,7 @@ void ApplyPlrDamage(DamageType damageType, Player &player, int dam, int minHP /* AddFloatingNumber(damageType, player, totalDamage); } if (totalDamage > 0 && player.pManaShield) { - int8_t manaShieldLevel = player._pSplLvl[static_cast(SpellID::ManaShield)]; + uint8_t manaShieldLevel = player._pSplLvl[static_cast(SpellID::ManaShield)]; if (manaShieldLevel > 0) { totalDamage += totalDamage / -player.GetManaShieldDamageReduction(); } diff --git a/Source/player.h b/Source/player.h index a6cdd48bf..d6ae4deee 100644 --- a/Source/player.h +++ b/Source/player.h @@ -33,7 +33,7 @@ constexpr int InventoryGridCells = 40; constexpr int MaxBeltItems = 8; constexpr int MaxResistance = 75; constexpr int MaxCharacterLevel = 50; -constexpr int MaxSpellLevel = 15; +constexpr uint8_t MaxSpellLevel = 15; constexpr int PlayerNameLength = 32; constexpr size_t NumHotkeys = 12; @@ -338,7 +338,7 @@ struct Player { SpellID _pRSpell; SpellType _pRSplType; SpellID _pSBkSpell; - int8_t _pSplLvl[64]; + uint8_t _pSplLvl[64]; /** @brief Bitmask of staff spell */ uint64_t _pISpells; /** @brief Bitmask of learned spells */ @@ -593,7 +593,7 @@ struct Player { return 0; } - return std::max(_pISplLvlAdd + _pSplLvl[static_cast(spell)], 0); + return std::max(_pISplLvlAdd + _pSplLvl[static_cast(spell)], 0); } /** diff --git a/Source/stores.cpp b/Source/stores.cpp index 5f005d18f..8de6d311a 100644 --- a/Source/stores.cpp +++ b/Source/stores.cpp @@ -741,7 +741,7 @@ void WitchBookLevel(Item &bookItem) if (bookItem._iMiscId != IMISC_BOOK) return; bookItem._iMinMag = GetSpellData(bookItem._iSpell).minInt; - int8_t spellLevel = MyPlayer->_pSplLvl[static_cast(bookItem._iSpell)]; + uint8_t spellLevel = MyPlayer->_pSplLvl[static_cast(bookItem._iSpell)]; while (spellLevel > 0) { bookItem._iMinMag += 20 * bookItem._iMinMag / 100; spellLevel--; diff --git a/test/player_test.cpp b/test/player_test.cpp index 747590e57..749279e3b 100644 --- a/test/player_test.cpp +++ b/test/player_test.cpp @@ -86,7 +86,7 @@ TEST(Player, PM_DoGotHit) static void AssertPlayer(Player &player) { - ASSERT_EQ(Count8(player._pSplLvl, 64), 0); + ASSERT_EQ(CountU8(player._pSplLvl, 64), 0); ASSERT_EQ(Count8(player.InvGrid, InventoryGridCells), 1); ASSERT_EQ(CountItems(player.InvBody, NUM_INVLOC), 1); ASSERT_EQ(CountItems(player.InvList, InventoryGridCells), 1); diff --git a/test/player_test.h b/test/player_test.h index 03240fbe9..955d8b25b 100644 --- a/test/player_test.h +++ b/test/player_test.h @@ -12,30 +12,20 @@ using namespace devilution; static int CountItems(Item *items, int n) { - int count = n; - for (int i = 0; i < n; i++) - if (items[i].isEmpty()) - count--; - - return count; + return std::count_if(items, items + n, [](Item x) { return !x.isEmpty(); }); } static int Count8(int8_t *ints, int n) { - int count = n; - for (int i = 0; i < n; i++) - if (ints[i] == 0) - count--; + return std::count_if(ints, ints + n, [](int8_t x) { return x != 0; }); +} - return count; +static int CountU8(uint8_t *ints, int n) +{ + return std::count_if(ints, ints + n, [](uint8_t x) { return x != 0; }); } static int CountBool(bool *bools, int n) { - int count = n; - for (int i = 0; i < n; i++) - if (!bools[i]) - count--; - - return count; + return std::count_if(bools, bools + n, [](bool x) { return x; }); } diff --git a/test/writehero_test.cpp b/test/writehero_test.cpp index 8c85da624..bf45b267c 100644 --- a/test/writehero_test.cpp +++ b/test/writehero_test.cpp @@ -257,7 +257,7 @@ void PackPlayerTest(PlayerPack *pPack) void AssertPlayer(Player &player) { - ASSERT_EQ(Count8(player._pSplLvl, 64), 23); + ASSERT_EQ(CountU8(player._pSplLvl, 64), 23); ASSERT_EQ(Count8(player.InvGrid, InventoryGridCells), 9); ASSERT_EQ(CountItems(player.InvBody, NUM_INVLOC), 6); ASSERT_EQ(CountItems(player.InvList, InventoryGridCells), 2);