Skip to content

Commit

Permalink
Fix a few -Wsign-compare warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Feb 4, 2024
1 parent 41c08e0 commit 6f40b34
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Source/lighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 = {};
}
Expand Down
18 changes: 9 additions & 9 deletions Source/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ void LoadDroppedItems(LoadHelper &file, size_t savedItemCount)
file.Skip<uint8_t>(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));
Expand Down Expand Up @@ -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<int32_t>();
for (unsigned &monsterId : ActiveMonsters)
monsterId = file.NextBE<uint32_t>();
for (size_t i = 0; i < ActiveMonsterCount; i++)
LoadMonster(&file, Monsters[ActiveMonsters[i]]);
for (size_t i = 0; i < ActiveMonsterCount; i++)
Expand Down Expand Up @@ -2435,8 +2435,8 @@ void SaveGameData(SaveWriter &saveWriter)
file.Skip(4 * (MaxMonsters - NUM_MTYPES));

if (leveltype != DTYPE_TOWN) {
for (int monsterId : ActiveMonsters)
file.WriteBE<int32_t>(monsterId);
for (unsigned monsterId : ActiveMonsters)
file.WriteBE<uint32_t>(monsterId);
for (size_t i = 0; i < ActiveMonsterCount; i++)
SaveMonster(&file, Monsters[ActiveMonsters[i]]);
// Write ActiveMissiles
Expand Down Expand Up @@ -2572,8 +2572,8 @@ void SaveLevel(SaveWriter &saveWriter)
file.WriteBE<int32_t>(ActiveObjectCount);

if (leveltype != DTYPE_TOWN) {
for (int monsterId : ActiveMonsters)
file.WriteBE<int32_t>(monsterId);
for (unsigned monsterId : ActiveMonsters)
file.WriteBE<uint32_t>(monsterId);
for (size_t i = 0; i < ActiveMonsterCount; i++)
SaveMonster(&file, Monsters[ActiveMonsters[i]]);
for (int objectId : ActiveObjects)
Expand Down Expand Up @@ -2646,8 +2646,8 @@ void LoadLevel()
ActiveObjectCount = file.NextBE<int32_t>();

if (leveltype != DTYPE_TOWN) {
for (int &monsterId : ActiveMonsters)
monsterId = file.NextBE<int32_t>();
for (unsigned &monsterId : ActiveMonsters)
monsterId = file.NextBE<uint32_t>();
for (size_t i = 0; i < ActiveMonsterCount; i++) {
Monster &monster = Monsters[ActiveMonsters[i]];
LoadMonster(&file, monster);
Expand Down
18 changes: 8 additions & 10 deletions Source/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <algorithm>
#include <array>
#include <numeric>
#include <string_view>

#include <fmt/core.h>
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -659,7 +660,7 @@ void UpdateEnemy(Monster &monster)
|| ((sameroom || !bestsameroom) && dist < bestDist)
|| (menemy == -1)) {
monster.flags |= MFLAG_TARGETS_MONSTER;
menemy = monsterId;
menemy = static_cast<int>(monsterId);
target = otherMonster.position.future;
bestDist = dist;
bestsameroom = sameroom;
Expand Down Expand Up @@ -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<int>(monsterId);
const unsigned oldId = ActiveMonsters[ActiveMonsterCount];
ActiveMonsters[ActiveMonsterCount] = static_cast<unsigned>(monsterId);
ActiveMonsters[index] = oldId;
ActiveMonsterCount += 1;
}
Expand Down Expand Up @@ -3292,10 +3293,7 @@ void InitLevelMonsters()
ActiveMonsterCount = 0;
totalmonsters = MaxMonsters;

for (size_t i = 0; i < MaxMonsters; i++) {
ActiveMonsters[i] = static_cast<int>(i);
}

std::iota(std::begin(ActiveMonsters), std::end(ActiveMonsters), 0u);
uniquetrans = 0;
}

Expand Down Expand Up @@ -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<int>(ActiveMonsters[i])) // Unselect monster if player highlighted it
pcursmonst = -1;
DeleteMonster(i);
} else {
Expand Down
2 changes: 1 addition & 1 deletion Source/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Source/msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Source/portal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(id)) {
dFlags[missile.position.tile.x][missile.position.tile.y] &= ~DungeonFlag::Missile;

if (Portals[id].level != 0)
Expand Down
21 changes: 11 additions & 10 deletions Source/sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
*
* Implementation of functionality for syncing game state with other players.
*/
#include <climits>
#include <cstdint>

#include <limits>

#include "levels/gendung.h"
#include "lighting.h"
#include "monster.h"
Expand All @@ -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) {
Expand Down Expand Up @@ -52,18 +53,18 @@ void SyncMonsterPos(TSyncMonster &monsterSync, int ndx)

bool SyncMonsterActive(TSyncMonster &monsterSync)
{
int ndx = -1;
unsigned ndx = std::numeric_limits<unsigned>::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<unsigned>::max()) {
return false;
}

Expand All @@ -73,22 +74,22 @@ bool SyncMonsterActive(TSyncMonster &monsterSync)

bool SyncMonsterActive2(TSyncMonster &monsterSync)
{
int ndx = -1;
unsigned ndx = std::numeric_limits<unsigned>::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];
}
sgnMonsters++;
}

if (ndx == -1) {
if (ndx == std::numeric_limits<unsigned>::max()) {
return false;
}

Expand Down Expand Up @@ -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<uint8_t>::max();
}
}
} else if (dMonster[position.x][position.y] == 0) {
Expand All @@ -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<uint8_t>::max();
}

decode_enemy(monster, enemyId);
Expand Down

0 comments on commit 6f40b34

Please sign in to comment.