diff --git a/Source/lighting.cpp b/Source/lighting.cpp index 00b614b261d..797a83e81bc 100644 --- a/Source/lighting.cpp +++ b/Source/lighting.cpp @@ -362,7 +362,7 @@ void MakeLightTable() } else if (IsAnyOf(leveltype, DTYPE_NEST, DTYPE_CRYPT)) { // Make the lava fully bright for (auto &lightTable : LightTables) - std::iota(lightTable.begin(), lightTable.begin() + 16, 0); + std::iota(lightTable.begin(), lightTable.begin() + 16, uint8_t { 0 }); LightTables[15][0] = 0; std::fill_n(LightTables[15].begin() + 1, 15, 1); } @@ -438,7 +438,7 @@ void InitLighting() DisableLighting = false; #endif - std::iota(ActiveLights.begin(), ActiveLights.end(), 0); + std::iota(ActiveLights.begin(), ActiveLights.end(), uint8_t { 0 }); VisionActive = {}; TransList = {}; } diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index c3b9eca1df6..8bcfb44b371 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -1002,7 +1002,7 @@ void LoadDroppedItems(LoadHelper &file, size_t savedItemCount) file.Skip(MAXITEMS * 2); // Reset ActiveItems, the Items array will be populated from the start - std::iota(ActiveItems, ActiveItems + MAXITEMS, 0); + std::iota(ActiveItems, ActiveItems + MAXITEMS, uint8_t { 0 }); ActiveItemCount = 0; // Clear dItem so we can populate valid drop locations memset(dItem, 0, sizeof(dItem)); @@ -2171,8 +2171,8 @@ void LoadGame(bool firstflag) // skip ahead for vanilla save compatibility (Related to bugfix where MonsterKillCounts[MaxMonsters] was changed to MonsterKillCounts[NUM_MTYPES] file.Skip(4 * (MaxMonsters - NUM_MTYPES)); if (leveltype != DTYPE_TOWN) { - for (int &monsterId : ActiveMonsters) - monsterId = file.NextBE(); + for (unsigned &monsterId : ActiveMonsters) + monsterId = file.NextBE(); for (size_t i = 0; i < ActiveMonsterCount; i++) LoadMonster(&file, Monsters[ActiveMonsters[i]]); for (size_t i = 0; i < ActiveMonsterCount; i++) @@ -2435,8 +2435,8 @@ void SaveGameData(SaveWriter &saveWriter) file.Skip(4 * (MaxMonsters - NUM_MTYPES)); if (leveltype != DTYPE_TOWN) { - for (int monsterId : ActiveMonsters) - file.WriteBE(monsterId); + for (unsigned monsterId : ActiveMonsters) + file.WriteBE(monsterId); for (size_t i = 0; i < ActiveMonsterCount; i++) SaveMonster(&file, Monsters[ActiveMonsters[i]]); // Write ActiveMissiles @@ -2572,8 +2572,8 @@ void SaveLevel(SaveWriter &saveWriter) file.WriteBE(ActiveObjectCount); if (leveltype != DTYPE_TOWN) { - for (int monsterId : ActiveMonsters) - file.WriteBE(monsterId); + for (unsigned monsterId : ActiveMonsters) + file.WriteBE(monsterId); for (size_t i = 0; i < ActiveMonsterCount; i++) SaveMonster(&file, Monsters[ActiveMonsters[i]]); for (int objectId : ActiveObjects) @@ -2646,8 +2646,8 @@ void LoadLevel() ActiveObjectCount = file.NextBE(); if (leveltype != DTYPE_TOWN) { - for (int &monsterId : ActiveMonsters) - monsterId = file.NextBE(); + for (unsigned &monsterId : ActiveMonsters) + monsterId = file.NextBE(); for (size_t i = 0; i < ActiveMonsterCount; i++) { Monster &monster = Monsters[ActiveMonsters[i]]; LoadMonster(&file, monster); diff --git a/Source/monster.cpp b/Source/monster.cpp index b17d8e2268c..d6a5962e561 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -57,7 +58,7 @@ namespace devilution { CMonster LevelMonsterTypes[MaxLvlMTypes]; size_t LevelMonsterTypeCount; Monster Monsters[MaxMonsters]; -int ActiveMonsters[MaxMonsters]; +unsigned ActiveMonsters[MaxMonsters]; size_t ActiveMonsterCount; /** Tracks the total number of monsters killed per monster_id. */ int MonsterKillCounts[NUM_MTYPES]; @@ -631,7 +632,7 @@ void UpdateEnemy(Monster &monster) } } for (size_t i = 0; i < ActiveMonsterCount; i++) { - const int monsterId = ActiveMonsters[i]; + const unsigned monsterId = ActiveMonsters[i]; Monster &otherMonster = Monsters[monsterId]; if (&otherMonster == &monster) continue; @@ -659,7 +660,7 @@ void UpdateEnemy(Monster &monster) || ((sameroom || !bestsameroom) && dist < bestDist) || (menemy == -1)) { monster.flags |= MFLAG_TARGETS_MONSTER; - menemy = monsterId; + menemy = static_cast(monsterId); target = otherMonster.position.future; bestDist = dist; bestsameroom = sameroom; @@ -3143,8 +3144,8 @@ void EnsureMonsterIndexIsActive(size_t monsterId) continue; if (index < ActiveMonsterCount) return; // monster is already active - int oldId = ActiveMonsters[ActiveMonsterCount]; - ActiveMonsters[ActiveMonsterCount] = static_cast(monsterId); + const unsigned oldId = ActiveMonsters[ActiveMonsterCount]; + ActiveMonsters[ActiveMonsterCount] = static_cast(monsterId); ActiveMonsters[index] = oldId; ActiveMonsterCount += 1; } @@ -3292,10 +3293,7 @@ void InitLevelMonsters() ActiveMonsterCount = 0; totalmonsters = MaxMonsters; - for (size_t i = 0; i < MaxMonsters; i++) { - ActiveMonsters[i] = static_cast(i); - } - + std::iota(std::begin(ActiveMonsters), std::end(ActiveMonsters), 0u); uniquetrans = 0; } @@ -4081,7 +4079,7 @@ void DeleteMonsterList() for (size_t i = MAX_PLRS; i < ActiveMonsterCount;) { if (Monsters[ActiveMonsters[i]].isInvalid) { - if (pcursmonst == ActiveMonsters[i]) // Unselect monster if player highlighted it + if (pcursmonst == static_cast(ActiveMonsters[i])) // Unselect monster if player highlighted it pcursmonst = -1; DeleteMonster(i); } else { diff --git a/Source/monster.h b/Source/monster.h index f857a48ab27..44d46ed31a5 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -474,7 +474,7 @@ struct Monster { // note: missing field _mAFNum extern size_t LevelMonsterTypeCount; extern Monster Monsters[MaxMonsters]; -extern int ActiveMonsters[MaxMonsters]; +extern unsigned ActiveMonsters[MaxMonsters]; extern size_t ActiveMonsterCount; extern int MonsterKillCounts[NUM_MTYPES]; extern bool sgbSaveSoundOn; diff --git a/Source/msg.cpp b/Source/msg.cpp index c8dc860c2de..0ca3a39e97e 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -757,7 +757,7 @@ void DeltaLeaveSync(uint8_t bLevel) DLevel &deltaLevel = GetDeltaLevel(bLevel); for (size_t i = 0; i < ActiveMonsterCount; i++) { - int ma = ActiveMonsters[i]; + const unsigned ma = ActiveMonsters[i]; auto &monster = Monsters[ma]; if (monster.hitPoints == 0) continue; diff --git a/Source/portal.cpp b/Source/portal.cpp index c663ea84c6e..c206bcf41e4 100644 --- a/Source/portal.cpp +++ b/Source/portal.cpp @@ -112,9 +112,9 @@ bool PortalOnLevel(const Player &player) void RemovePortalMissile(const Player &player) { - size_t id = player.getId(); + const size_t id = player.getId(); Missiles.remove_if([id](Missile &missile) { - if (missile._mitype == MissileID::TownPortal && missile._misource == id) { + if (missile._mitype == MissileID::TownPortal && missile._misource == static_cast(id)) { dFlags[missile.position.tile.x][missile.position.tile.y] &= ~DungeonFlag::Missile; if (Portals[id].level != 0) diff --git a/Source/sync.cpp b/Source/sync.cpp index 5ab54e17ce3..4491ec7bcf6 100644 --- a/Source/sync.cpp +++ b/Source/sync.cpp @@ -3,9 +3,10 @@ * * Implementation of functionality for syncing game state with other players. */ -#include #include +#include + #include "levels/gendung.h" #include "lighting.h" #include "monster.h" @@ -24,7 +25,7 @@ int sgnSyncPInv; void SyncOneMonster() { for (size_t i = 0; i < ActiveMonsterCount; i++) { - int m = ActiveMonsters[i]; + const unsigned m = ActiveMonsters[i]; auto &monster = Monsters[m]; sgnMonsterPriority[m] = MyPlayer->position.tile.ManhattanDistance(monster.position.tile); if (monster.activeForTicks == 0) { @@ -52,18 +53,18 @@ void SyncMonsterPos(TSyncMonster &monsterSync, int ndx) bool SyncMonsterActive(TSyncMonster &monsterSync) { - int ndx = -1; + unsigned ndx = std::numeric_limits::max(); uint32_t lru = 0xFFFFFFFF; for (size_t i = 0; i < ActiveMonsterCount; i++) { - int m = ActiveMonsters[i]; + const unsigned m = ActiveMonsters[i]; if (sgnMonsterPriority[m] < lru && sgwLRU[m] < 0xFFFE) { lru = sgnMonsterPriority[m]; ndx = ActiveMonsters[i]; } } - if (ndx == -1) { + if (ndx == std::numeric_limits::max()) { return false; } @@ -73,14 +74,14 @@ bool SyncMonsterActive(TSyncMonster &monsterSync) bool SyncMonsterActive2(TSyncMonster &monsterSync) { - int ndx = -1; + unsigned ndx = std::numeric_limits::max(); uint32_t lru = 0xFFFE; for (size_t i = 0; i < ActiveMonsterCount; i++) { if (sgnMonsters >= ActiveMonsterCount) { sgnMonsters = 0; } - int m = ActiveMonsters[sgnMonsters]; + const unsigned m = ActiveMonsters[sgnMonsters]; if (sgwLRU[m] < lru) { lru = sgwLRU[m]; ndx = ActiveMonsters[sgnMonsters]; @@ -88,7 +89,7 @@ bool SyncMonsterActive2(TSyncMonster &monsterSync) sgnMonsters++; } - if (ndx == -1) { + if (ndx == std::numeric_limits::max()) { return false; } @@ -184,7 +185,7 @@ void SyncMonster(bool isOwner, const TSyncMonster &monsterSync) M_ClearSquares(monster); monster.occupyTile(monster.position.tile, false); Walk(monster, md); - monster.activeForTicks = UINT8_MAX; + monster.activeForTicks = std::numeric_limits::max(); } } } else if (dMonster[position.x][position.y] == 0) { @@ -196,7 +197,7 @@ void SyncMonster(bool isOwner, const TSyncMonster &monsterSync) decode_enemy(monster, enemyId); Direction md = GetDirection(position, monster.enemyPosition); M_StartStand(monster, md); - monster.activeForTicks = UINT8_MAX; + monster.activeForTicks = std::numeric_limits::max(); } decode_enemy(monster, enemyId);