diff --git a/Source/automap.cpp b/Source/automap.cpp index 601f788f6..bade67f20 100644 --- a/Source/automap.cpp +++ b/Source/automap.cpp @@ -735,7 +735,7 @@ void DrawAutomap(const Surface &out) screen.y += AmLine32; } - for (int playerId = 0; playerId < MAX_PLRS; playerId++) { + for (size_t playerId = 0; playerId < Players.size(); playerId++) { Player &player = Players[playerId]; if (player.isOnActiveLevel() && player.plractive && !player._pLvlChanging && (&player == &myPlayer || player.friendlyMode)) { DrawAutomapPlr(out, myPlayerOffset, playerId); diff --git a/Source/control.cpp b/Source/control.cpp index 6f98167c0..1a7c57250 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -327,7 +327,7 @@ void ResetTalkMsg() uint32_t pmask = 0; - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (WhisperList[i]) pmask |= 1 << i; } @@ -1139,7 +1139,7 @@ void DrawTalkPan(const Surface &out) x += 46; int talkBtn = 0; - for (int i = 0; i < 4; i++) { + for (size_t i = 0; i < 4; i++) { if (i == MyPlayerId) continue; @@ -1207,7 +1207,7 @@ void control_release_talk_btn() int off = (MousePosition.y - (69 + mainPanelPosition.y)) / 18; - int p = 0; + size_t p = 0; for (; p < MAX_PLRS && off != -1; p++) { if (p != MyPlayerId) off--; diff --git a/Source/controls/game_controls.cpp b/Source/controls/game_controls.cpp index 29a3d89f3..6d7a46eb5 100644 --- a/Source/controls/game_controls.cpp +++ b/Source/controls/game_controls.cpp @@ -102,6 +102,10 @@ bool HandleStartAndSelect(const ControllerButtonEvent &ctrlEvent, GameAction *ac bool GetGameAction(const SDL_Event &event, ControllerButtonEvent ctrlEvent, GameAction *action) { + if (MyPlayer == nullptr) { + return false; + } + const bool inGameMenu = InGameMenu(); #ifndef USE_SDL1 diff --git a/Source/controls/plrctrls.cpp b/Source/controls/plrctrls.cpp index 2714f6cc1..61b1fa775 100644 --- a/Source/controls/plrctrls.cpp +++ b/Source/controls/plrctrls.cpp @@ -368,7 +368,7 @@ void CheckPlayerNearby() if (myPlayer.friendlyMode && spl != SPL_RESURRECT && spl != SPL_HEALOTHER) return; - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (i == MyPlayerId) continue; const Player &player = Players[i]; diff --git a/Source/cursor.cpp b/Source/cursor.cpp index b5fdb17ed..7dbb51c08 100644 --- a/Source/cursor.cpp +++ b/Source/cursor.cpp @@ -403,52 +403,52 @@ void CheckCursMove() if (leveltype != DTYPE_TOWN) { if (pcurstemp != -1) { if (!flipflag && mx + 2 < MAXDUNX && my + 1 < MAXDUNY && dMonster[mx + 2][my + 1] != 0 && IsTileLit({ mx + 2, my + 1 })) { - int mi = abs(dMonster[mx + 2][my + 1]) - 1; - if (mi == pcurstemp && Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 4) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 2][my + 1]) - 1; + if (monsterId == pcurstemp && Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 4) != 0) { cursPosition = Point { mx, my } + Displacement { 2, 1 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (flipflag && mx + 1 < MAXDUNX && my + 2 < MAXDUNY && dMonster[mx + 1][my + 2] != 0 && IsTileLit({ mx + 1, my + 2 })) { - int mi = abs(dMonster[mx + 1][my + 2]) - 1; - if (mi == pcurstemp && Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 4) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 1][my + 2]) - 1; + if (monsterId == pcurstemp && Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 4) != 0) { cursPosition = Point { mx, my } + Displacement { 1, 2 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (mx + 2 < MAXDUNX && my + 2 < MAXDUNY && dMonster[mx + 2][my + 2] != 0 && IsTileLit({ mx + 2, my + 2 })) { - int mi = abs(dMonster[mx + 2][my + 2]) - 1; - if (mi == pcurstemp && Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 4) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 2][my + 2]) - 1; + if (monsterId == pcurstemp && Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 4) != 0) { cursPosition = Point { mx, my } + Displacement { 2, 2 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (mx + 1 < MAXDUNX && !flipflag && dMonster[mx + 1][my] != 0 && IsTileLit({ mx + 1, my })) { - int mi = abs(dMonster[mx + 1][my]) - 1; - if (mi == pcurstemp && Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 2) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 1][my]) - 1; + if (monsterId == pcurstemp && Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 2) != 0) { cursPosition = Point { mx, my } + Displacement { 1, 0 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (my + 1 < MAXDUNY && flipflag && dMonster[mx][my + 1] != 0 && IsTileLit({ mx, my + 1 })) { - int mi = abs(dMonster[mx][my + 1]) - 1; - if (mi == pcurstemp && Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 2) != 0) { + const uint16_t monsterId = abs(dMonster[mx][my + 1]) - 1; + if (monsterId == pcurstemp && Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 2) != 0) { cursPosition = Point { mx, my } + Displacement { 0, 1 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (dMonster[mx][my] != 0 && IsTileLit({ mx, my })) { - int mi = abs(dMonster[mx][my]) - 1; - if (mi == pcurstemp && Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 1) != 0) { + const uint16_t monsterId = abs(dMonster[mx][my]) - 1; + if (monsterId == pcurstemp && Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 1) != 0) { cursPosition = { mx, my }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (mx + 1 < MAXDUNX && my + 1 < MAXDUNY && dMonster[mx + 1][my + 1] != 0 && IsTileLit({ mx + 1, my + 1 })) { - int mi = abs(dMonster[mx + 1][my + 1]) - 1; - if (mi == pcurstemp && Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 2) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 1][my + 1]) - 1; + if (monsterId == pcurstemp && Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 2) != 0) { cursPosition = Point { mx, my } + Displacement { 1, 1 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (pcursmonst != -1 && (Monsters[pcursmonst]._mFlags & MFLAG_HIDDEN) != 0) { @@ -463,52 +463,52 @@ void CheckCursMove() } } if (!flipflag && mx + 2 < MAXDUNX && my + 1 < MAXDUNY && dMonster[mx + 2][my + 1] != 0 && IsTileLit({ mx + 2, my + 1 })) { - int mi = abs(dMonster[mx + 2][my + 1]) - 1; - if (Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 4) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 2][my + 1]) - 1; + if (Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 4) != 0) { cursPosition = Point { mx, my } + Displacement { 2, 1 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (flipflag && mx + 1 < MAXDUNX && my + 2 < MAXDUNY && dMonster[mx + 1][my + 2] != 0 && IsTileLit({ mx + 1, my + 2 })) { - int mi = abs(dMonster[mx + 1][my + 2]) - 1; - if (Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 4) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 1][my + 2]) - 1; + if (Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 4) != 0) { cursPosition = Point { mx, my } + Displacement { 1, 2 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (mx + 2 < MAXDUNX && my + 2 < MAXDUNY && dMonster[mx + 2][my + 2] != 0 && IsTileLit({ mx + 2, my + 2 })) { - int mi = abs(dMonster[mx + 2][my + 2]) - 1; - if (Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 4) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 2][my + 2]) - 1; + if (Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 4) != 0) { cursPosition = Point { mx, my } + Displacement { 2, 2 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (!flipflag && mx + 1 < MAXDUNX && dMonster[mx + 1][my] != 0 && IsTileLit({ mx + 1, my })) { - int mi = abs(dMonster[mx + 1][my]) - 1; - if (Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 2) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 1][my]) - 1; + if (Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 2) != 0) { cursPosition = Point { mx, my } + Displacement { 1, 0 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (flipflag && my + 1 < MAXDUNY && dMonster[mx][my + 1] != 0 && IsTileLit({ mx, my + 1 })) { - int mi = abs(dMonster[mx][my + 1]) - 1; - if (Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 2) != 0) { + const uint16_t monsterId = abs(dMonster[mx][my + 1]) - 1; + if (Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 2) != 0) { cursPosition = Point { mx, my } + Displacement { 0, 1 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (dMonster[mx][my] != 0 && IsTileLit({ mx, my })) { - int mi = abs(dMonster[mx][my]) - 1; - if (Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 1) != 0) { + const uint16_t monsterId = abs(dMonster[mx][my]) - 1; + if (Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 1) != 0) { cursPosition = { mx, my }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (mx + 1 < MAXDUNX && my + 1 < MAXDUNY && dMonster[mx + 1][my + 1] != 0 && IsTileLit({ mx + 1, my + 1 })) { - int mi = abs(dMonster[mx + 1][my + 1]) - 1; - if (Monsters[mi]._mhitpoints >> 6 > 0 && (Monsters[mi].MData->mSelFlag & 2) != 0) { + const uint16_t monsterId = abs(dMonster[mx + 1][my + 1]) - 1; + if (Monsters[monsterId]._mhitpoints >> 6 > 0 && (Monsters[monsterId].MData->mSelFlag & 2) != 0) { cursPosition = Point { mx, my } + Displacement { 1, 1 }; - pcursmonst = mi; + pcursmonst = monsterId; } } if (pcursmonst != -1 && (Monsters[pcursmonst]._mFlags & MFLAG_HIDDEN) != 0) { @@ -539,31 +539,31 @@ void CheckCursMove() if (pcursmonst == -1) { if (!flipflag && mx + 1 < MAXDUNX && dPlayer[mx + 1][my] != 0) { - int8_t bv = abs(dPlayer[mx + 1][my]) - 1; - if (bv != MyPlayerId && Players[bv]._pHitPoints != 0) { + const uint8_t playerId = abs(dPlayer[mx + 1][my]) - 1; + if (playerId != MyPlayerId && Players[playerId]._pHitPoints != 0) { cursPosition = Point { mx, my } + Displacement { 1, 0 }; - pcursplr = bv; + pcursplr = static_cast(playerId); } } if (flipflag && my + 1 < MAXDUNY && dPlayer[mx][my + 1] != 0) { - int8_t bv = abs(dPlayer[mx][my + 1]) - 1; - if (bv != MyPlayerId && Players[bv]._pHitPoints != 0) { + const uint8_t playerId = abs(dPlayer[mx][my + 1]) - 1; + if (playerId != MyPlayerId && Players[playerId]._pHitPoints != 0) { cursPosition = Point { mx, my } + Displacement { 0, 1 }; - pcursplr = bv; + pcursplr = static_cast(playerId); } } if (dPlayer[mx][my] != 0) { - int8_t bv = abs(dPlayer[mx][my]) - 1; - if (bv != MyPlayerId) { + const uint8_t playerId = abs(dPlayer[mx][my]) - 1; + if (playerId != MyPlayerId) { cursPosition = { mx, my }; - pcursplr = bv; + pcursplr = static_cast(playerId); } } if (TileContainsDeadPlayer({ mx, my })) { - for (int i = 0; i < MAX_PLRS; i++) { - if (Players[i].position.tile == Point { mx, my } && i != MyPlayerId) { + for (size_t playerId = 0; playerId < Players.size(); ++playerId) { + if (Players[playerId].position.tile == Point { mx, my } && playerId != MyPlayerId) { cursPosition = { mx, my }; - pcursplr = i; + pcursplr = static_cast(playerId); } } } @@ -571,10 +571,10 @@ void CheckCursMove() for (int xx = -1; xx < 2; xx++) { for (int yy = -1; yy < 2; yy++) { if (TileContainsDeadPlayer({ mx + xx, my + yy })) { - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (Players[i].position.tile.x == mx + xx && Players[i].position.tile.y == my + yy && i != MyPlayerId) { cursPosition = Point { mx, my } + Displacement { xx, yy }; - pcursplr = i; + pcursplr = static_cast(i); } } } @@ -582,10 +582,10 @@ void CheckCursMove() } } if (mx + 1 < MAXDUNX && my + 1 < MAXDUNY && dPlayer[mx + 1][my + 1] != 0) { - int8_t bv = abs(dPlayer[mx + 1][my + 1]) - 1; - if (bv != MyPlayerId && Players[bv]._pHitPoints != 0) { + const uint8_t playerId = abs(dPlayer[mx + 1][my + 1]) - 1; + if (playerId != MyPlayerId && Players[playerId]._pHitPoints != 0) { cursPosition = Point { mx, my } + Displacement { 1, 1 }; - pcursplr = bv; + pcursplr = static_cast(playerId); } } } @@ -615,36 +615,36 @@ void CheckCursMove() if (object != nullptr) { // found object that can be activated with the given cursor position cursPosition = testPosition; - pcursobj = object->GetId(); + pcursobj = static_cast(object->GetId()); } } if (pcursplr == -1 && pcursobj == -1 && pcursmonst == -1) { if (!flipflag && mx + 1 < MAXDUNX && dItem[mx + 1][my] > 0) { - int8_t bv = dItem[mx + 1][my] - 1; - if (Items[bv]._iSelFlag >= 2) { + const uint8_t itemId = dItem[mx + 1][my] - 1; + if (Items[itemId]._iSelFlag >= 2) { cursPosition = Point { mx, my } + Displacement { 1, 0 }; - pcursitem = bv; + pcursitem = static_cast(itemId); } } if (flipflag && my + 1 < MAXDUNY && dItem[mx][my + 1] > 0) { - int8_t bv = dItem[mx][my + 1] - 1; - if (Items[bv]._iSelFlag >= 2) { + const uint8_t itemId = dItem[mx][my + 1] - 1; + if (Items[itemId]._iSelFlag >= 2) { cursPosition = Point { mx, my } + Displacement { 0, 1 }; - pcursitem = bv; + pcursitem = static_cast(itemId); } } if (dItem[mx][my] > 0) { - int8_t bv = dItem[mx][my] - 1; - if (Items[bv]._iSelFlag == 1 || Items[bv]._iSelFlag == 3) { + const uint8_t itemId = dItem[mx][my] - 1; + if (Items[itemId]._iSelFlag == 1 || Items[itemId]._iSelFlag == 3) { cursPosition = { mx, my }; - pcursitem = bv; + pcursitem = static_cast(itemId); } } if (mx + 1 < MAXDUNX && my + 1 < MAXDUNY && dItem[mx + 1][my + 1] > 0) { - int8_t bv = dItem[mx + 1][my + 1] - 1; - if (Items[bv]._iSelFlag >= 2) { + const uint8_t itemId = dItem[mx + 1][my + 1] - 1; + if (Items[itemId]._iSelFlag >= 2) { cursPosition = Point { mx, my } + Displacement { 1, 1 }; - pcursitem = bv; + pcursitem = static_cast(itemId); } } if (pcursitem == -1) { diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 3f24af6f7..4e760fe2d 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -2280,7 +2280,7 @@ void LoadGameLevel(bool firstflag, lvl_entry lvldir) SyncPortals(); - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { Player &player = Players[i]; if (player.plractive && player.isOnActiveLevel() && (!player._pLvlChanging || i == MyPlayerId)) { if (player._pHitPoints > 0) { diff --git a/Source/dvlnet/base.cpp b/Source/dvlnet/base.cpp index e24046c34..39c261c61 100644 --- a/Source/dvlnet/base.cpp +++ b/Source/dvlnet/base.cpp @@ -4,6 +4,8 @@ #include #include +#include "player.h" + namespace devilution { namespace net { @@ -218,7 +220,7 @@ bool base::SNetSendMessage(int playerId, void *data, unsigned int size) bool base::AllTurnsArrived() { - for (auto i = 0; i < MAX_PLRS; ++i) { + for (size_t i = 0; i < Players.size(); ++i) { PlayerState &playerState = playerStateTable_[i]; if (!playerState.isConnected) continue; @@ -237,7 +239,7 @@ bool base::SNetReceiveTurns(char **data, size_t *size, uint32_t *status) { poll(); - for (auto i = 0; i < MAX_PLRS; ++i) { + for (size_t i = 0; i < Players.size(); ++i) { status[i] = 0; PlayerState &playerState = playerStateTable_[i]; @@ -257,7 +259,7 @@ bool base::SNetReceiveTurns(char **data, size_t *size, uint32_t *status) } if (AllTurnsArrived()) { - for (auto i = 0; i < MAX_PLRS; ++i) { + for (size_t i = 0; i < Players.size(); ++i) { PlayerState &playerState = playerStateTable_[i]; if (!playerState.isConnected) continue; @@ -284,7 +286,7 @@ bool base::SNetReceiveTurns(char **data, size_t *size, uint32_t *status) return true; } - for (auto i = 0; i < MAX_PLRS; ++i) { + for (size_t i = 0; i < Players.size(); ++i) { PlayerState &playerState = playerStateTable_[i]; if (!playerState.isConnected) continue; @@ -414,7 +416,7 @@ bool base::SNetDropPlayer(int playerid, uint32_t flags) plr_t base::GetOwner() { - for (auto i = 0; i < MAX_PLRS; ++i) { + for (size_t i = 0; i < Players.size(); ++i) { if (IsConnected(i)) { return i; } diff --git a/Source/dvlnet/base_protocol.h b/Source/dvlnet/base_protocol.h index 9308a8386..b5fd2d977 100644 --- a/Source/dvlnet/base_protocol.h +++ b/Source/dvlnet/base_protocol.h @@ -68,7 +68,7 @@ template plr_t base_protocol

::get_master() { plr_t ret = plr_self; - for (plr_t i = 0; i < MAX_PLRS; ++i) + for (plr_t i = 0; i < Players.size(); ++i) if (peers[i].endpoint) ret = std::min(ret, i); return ret; @@ -195,7 +195,7 @@ void base_protocol

::send(packet &pkt) return; SendTo(destination, pkt); } else if (destination == PLR_BROADCAST) { - for (plr_t player = 0; player < MAX_PLRS; player++) + for (plr_t player = 0; player < Players.size(); player++) SendTo(player, pkt); } else if (destination == PLR_MASTER) { throw dvlnet_exception(); @@ -236,7 +236,7 @@ void base_protocol

::recv() } } while (proto.get_disconnected(sender)) { - for (plr_t i = 0; i < MAX_PLRS; ++i) { + for (plr_t i = 0; i < Players.size(); ++i) { if (peers[i].endpoint == sender) { DisconnectNet(i); break; @@ -253,7 +253,7 @@ template void base_protocol

::handle_join_request(packet &pkt, endpoint_t sender) { plr_t i; - for (i = 0; i < MAX_PLRS; ++i) { + for (i = 0; i < Players.size(); ++i) { Peer &peer = peers[i]; if (i != plr_self && !peer.endpoint) { peer.endpoint = sender; @@ -268,7 +268,7 @@ void base_protocol

::handle_join_request(packet &pkt, endpoint_t sender) } auto senderinfo = sender.serialize(); - for (plr_t j = 0; j < MAX_PLRS; ++j) { + for (plr_t j = 0; j < Players.size(); ++j) { endpoint_t peer = peers[j].endpoint; if ((j != plr_self) && (j != i) && peer) { auto peerpkt = pktfty->make_packet(PLR_MASTER, PLR_BROADCAST, i, senderinfo); @@ -298,7 +298,7 @@ void base_protocol

::recv_decrypted(packet &pkt, endpoint_t sender) return; const GameData *gameData = (const GameData *)pkt.Info().data(); std::vector playerNames; - for (size_t i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { std::string playerName; const char *playerNamePointer = (const char *)(pkt.Info().data() + sizeof(GameData) + (i * sizePlayerName)); playerName.append(playerNamePointer, strnlen(playerNamePointer, PLR_NAME_LEN)); @@ -327,7 +327,7 @@ void base_protocol

::recv_ingame(packet &pkt, endpoint_t sender) constexpr size_t sizePlayerName = (sizeof(char) * PLR_NAME_LEN); buf.resize(game_init_info.size() + (sizePlayerName * MAX_PLRS) + gamename.size()); std::memcpy(buf.data(), &game_init_info[0], game_init_info.size()); - for (size_t i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (Players[i].plractive) { std::memcpy(buf.data() + game_init_info.size() + (i * sizePlayerName), &Players[i]._pName, sizePlayerName); } else { @@ -381,7 +381,7 @@ void base_protocol

::recv_ingame(packet &pkt, endpoint_t sender) if (plr_self != PLR_BROADCAST) { if (wasBroadcast) { // Send a handshake to everyone just after PT_JOIN_ACCEPT - for (plr_t player = 0; player < MAX_PLRS; player++) + for (plr_t player = 0; player < Players.size(); player++) InitiateHandshake(player); } diff --git a/Source/dvlnet/loopback.cpp b/Source/dvlnet/loopback.cpp index df8b32518..9c4138138 100644 --- a/Source/dvlnet/loopback.cpp +++ b/Source/dvlnet/loopback.cpp @@ -1,6 +1,7 @@ #include "dvlnet/loopback.h" #include "multi.h" +#include "player.h" #include "utils/language.h" #include "utils/stubs.h" @@ -41,7 +42,7 @@ bool loopback::SNetSendMessage(int dest, void *data, unsigned int size) bool loopback::SNetReceiveTurns(char **data, size_t *size, uint32_t * /*status*/) { - for (auto i = 0; i < MAX_PLRS; ++i) { + for (size_t i = 0; i < Players.size(); ++i) { size[i] = 0; data[i] = nullptr; } diff --git a/Source/dvlnet/tcp_server.cpp b/Source/dvlnet/tcp_server.cpp index 635f26c2b..6cdb8b1ce 100644 --- a/Source/dvlnet/tcp_server.cpp +++ b/Source/dvlnet/tcp_server.cpp @@ -6,6 +6,7 @@ #include #include "dvlnet/base.h" +#include "player.h" #include "utils/log.hpp" namespace devilution { @@ -44,7 +45,7 @@ tcp_server::scc tcp_server::MakeConnection() plr_t tcp_server::NextFree() { - for (plr_t i = 0; i < MAX_PLRS; ++i) + for (plr_t i = 0; i < Players.size(); ++i) if (!connections[i]) return i; return PLR_BROADCAST; @@ -52,7 +53,7 @@ plr_t tcp_server::NextFree() bool tcp_server::Empty() { - for (plr_t i = 0; i < MAX_PLRS; ++i) + for (plr_t i = 0; i < Players.size(); ++i) if (connections[i]) return false; return true; @@ -108,7 +109,7 @@ void tcp_server::HandleReceiveNewPlayer(const scc &con, packet &pkt) if (Empty()) game_init_info = pkt.Info(); - for (plr_t player = 0; player < MAX_PLRS; player++) { + for (plr_t player = 0; player < Players.size(); player++) { if (connections[player]) { auto playerPacket = pktfty.make_packet(PLR_MASTER, PLR_BROADCAST, newplr); StartSend(connections[player], *playerPacket); @@ -135,7 +136,7 @@ void tcp_server::HandleReceivePacket(packet &pkt) void tcp_server::SendPacket(packet &pkt) { if (pkt.Destination() == PLR_BROADCAST) { - for (auto i = 0; i < MAX_PLRS; ++i) + for (size_t i = 0; i < Players.size(); ++i) if (i != pkt.Source() && connections[i]) StartSend(connections[i], pkt); } else { diff --git a/Source/engine/render/scrollrt.cpp b/Source/engine/render/scrollrt.cpp index a42a70b38..5aed6ecc8 100644 --- a/Source/engine/render/scrollrt.cpp +++ b/Source/engine/render/scrollrt.cpp @@ -548,7 +548,7 @@ void DrawPlayer(const Surface &out, int pnum, Point tilePosition, Point targetBu if (pnum == pcursplr) Cl2DrawOutline(out, 165, spriteBufferPosition.x, spriteBufferPosition.y, *sprite, nCel); - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { Cl2Draw(out, spriteBufferPosition.x, spriteBufferPosition.y, *sprite, nCel); DrawPlayerIcons(out, player, targetBufferPosition, false); return; @@ -582,7 +582,7 @@ void DrawDeadPlayer(const Surface &out, Point tilePosition, Point targetBufferPo { dFlags[tilePosition.x][tilePosition.y] &= ~DungeonFlag::DeadPlayer; - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { Player &player = Players[i]; if (player.plractive && player._pHitPoints == 0 && player.isOnActiveLevel() && player.position.tile == tilePosition) { dFlags[tilePosition.x][tilePosition.y] |= DungeonFlag::DeadPlayer; diff --git a/Source/inv.cpp b/Source/inv.cpp index 999c9d40c..bc80a4700 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -1951,7 +1951,7 @@ bool UseInvItem(int pnum, int cii) Player &player = Players[pnum]; - if (player._pInvincible && player._pHitPoints == 0 && pnum == MyPlayerId) + if (player._pInvincible && player._pHitPoints == 0 && pnum == static_cast(MyPlayerId)) return true; if (pcurs != CURSOR_HAND) return true; @@ -2050,7 +2050,7 @@ bool UseInvItem(int pnum, int cii) int idata = ItemCAnimTbl[item->_iCurs]; if (item->_iMiscId == IMISC_BOOK) PlaySFX(IS_RBOOK); - else if (pnum == MyPlayerId) + else if (pnum == static_cast(MyPlayerId)) PlaySFX(ItemInvSnds[idata]); UseItem(pnum, item->_iMiscId, item->_iSpell); diff --git a/Source/items.cpp b/Source/items.cpp index bbb666496..0cbfdcc9f 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -3002,7 +3002,7 @@ void GetItemAttrs(Item &item, int itemData, int lvl) void SetupItem(Item &item) { - item.setNewAnimation(MyPlayer->pLvlLoad == 0); + item.setNewAnimation(MyPlayer != nullptr && MyPlayer->pLvlLoad == 0); item._iIdentified = false; } @@ -3910,7 +3910,7 @@ void UseItem(int pnum, item_misc_id mid, spell_id spl) case IMISC_SCROLL: if (ControlMode == ControlTypes::KeyboardAndMouse && spelldata[spl].sTargeted) { player._pTSpell = spl; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NewCursor(CURSOR_TELEPORT); } else { ClrPlrPath(player); @@ -3920,14 +3920,14 @@ void UseItem(int pnum, item_misc_id mid, spell_id spl) player.destAction = ACTION_SPELL; player.destParam1 = cursPosition.x; player.destParam2 = cursPosition.y; - if (pnum == MyPlayerId && spl == SPL_NOVA) + if (pnum == static_cast(MyPlayerId) && spl == SPL_NOVA) NetSendCmdLoc(pnum, true, CMD_NOVA, cursPosition); } break; case IMISC_SCROLLT: if (ControlMode == ControlTypes::KeyboardAndMouse && spelldata[spl].sTargeted) { player._pTSpell = spl; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NewCursor(CURSOR_TELEPORT); } else { ClrPlrPath(player); @@ -3973,7 +3973,7 @@ void UseItem(int pnum, item_misc_id mid, spell_id spl) case IMISC_OILHARD: case IMISC_OILIMP: player._pOilType = mid; - if (pnum != MyPlayerId) { + if (pnum != static_cast(MyPlayerId)) { return; } if (sbookflag) { @@ -3992,27 +3992,27 @@ void UseItem(int pnum, item_misc_id mid, spell_id spl) break; case IMISC_RUNEF: player._pTSpell = SPL_RUNEFIRE; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NewCursor(CURSOR_TELEPORT); break; case IMISC_RUNEL: player._pTSpell = SPL_RUNELIGHT; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NewCursor(CURSOR_TELEPORT); break; case IMISC_GR_RUNEL: player._pTSpell = SPL_RUNENOVA; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NewCursor(CURSOR_TELEPORT); break; case IMISC_GR_RUNEF: player._pTSpell = SPL_RUNEIMMOLAT; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NewCursor(CURSOR_TELEPORT); break; case IMISC_RUNES: player._pTSpell = SPL_RUNESTONE; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NewCursor(CURSOR_TELEPORT); break; default: diff --git a/Source/missiles.cpp b/Source/missiles.cpp index aadc8f9dd..74a1f2761 100644 --- a/Source/missiles.cpp +++ b/Source/missiles.cpp @@ -237,7 +237,7 @@ bool MonsterMHit(int pnum, int m, int mindam, int maxdam, int dist, missile_id t if (resist) dam >>= 2; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) monster._mhitpoints -= dam; if ((gbIsHellfire && HasAnyOf(player._pIFlags, ItemSpecialEffect::NoHealOnMonsters)) || (!gbIsHellfire && HasAnyOf(player._pIFlags, ItemSpecialEffect::FireArrows))) @@ -342,7 +342,7 @@ bool Plr2PlrMHit(int pnum, int p, int mindam, int maxdam, int dist, missile_id m dam /= 2; if (resper > 0) { dam -= (dam * resper) / 100; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdDamage(true, p, dam); target.Say(HeroSpeech::ArghClang); return true; @@ -352,7 +352,7 @@ bool Plr2PlrMHit(int pnum, int p, int mindam, int maxdam, int dist, missile_id m StartPlrBlock(p, GetDirection(target.position.tile, player.position.tile)); *blocked = true; } else { - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdDamage(true, p, dam); StartPlrHit(p, dam, false); } @@ -680,7 +680,7 @@ bool IsMissileBlockedByTile(Point tile) void GetDamageAmt(int i, int *mind, int *maxd) { - assert(MyPlayerId >= 0 && MyPlayerId < MAX_PLRS); + assert(MyPlayerId < Players.size()); assert(i >= 0 && i < 64); Player &myPlayer = *MyPlayer; @@ -802,7 +802,7 @@ void GetDamageAmt(int i, int *mind, int *maxd) int GetSpellLevel(int playerId, spell_id sn) { - if (playerId != MyPlayerId) + if (playerId != static_cast(MyPlayerId)) return 1; // BUGFIX spell level will be wrong in multiplayer Player &player = Players[playerId]; @@ -1004,7 +1004,7 @@ bool PlayerMHit(int pnum, Monster *monster, int dist, int mind, int maxd, missil if (resper > 0) { dam -= dam * resper / 100; - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { ApplyPlrDamage(pnum, 0, 0, dam, earflag); } @@ -1014,7 +1014,7 @@ bool PlayerMHit(int pnum, Monster *monster, int dist, int mind, int maxd, missil return true; } - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { ApplyPlrDamage(pnum, 0, 0, dam, earflag); } @@ -1041,7 +1041,7 @@ void InitMissiles() for (auto &missile : Missiles) { if (missile._mitype == MIS_INFRA) { int src = missile._misource; - if (src == MyPlayerId) + if (src == static_cast(MyPlayerId)) CalcPlrItemVals(myPlayer, true); } } @@ -1052,7 +1052,7 @@ void InitMissiles() myPlayer._pSpellFlags &= ~SpellFlag::RageCooldown; for (auto &missile : Missiles) { if (missile._mitype == MIS_BLODBOIL) { - if (missile._misource == MyPlayerId) { + if (missile._misource == static_cast(MyPlayerId)) { int missingHP = myPlayer._pMaxHP - myPlayer._pHitPoints; CalcPlrItemVals(myPlayer, true); ApplyPlrDamage(MyPlayerId, 0, 1, missingHP + missile.var2); @@ -1121,7 +1121,7 @@ void AddReflection(Missile &missile, const AddMissileParameter & /*parameter*/) if (player.wReflections + add >= std::numeric_limits::max()) add = 0; player.wReflections += add; - if (missile._misource == MyPlayerId) + if (missile._misource == static_cast(MyPlayerId)) NetSendCmdParam1(true, CMD_SETREFLECT, player.wReflections); UseMana(missile._misource, SPL_REFLECT); @@ -1487,7 +1487,7 @@ void AddRing(Missile &missile, const AddMissileParameter & /*parameter*/) void AddSearch(Missile &missile, const AddMissileParameter & /*parameter*/) { - if (missile._misource == MyPlayerId) + if (missile._misource == static_cast(MyPlayerId)) AutoMapShowItems = true; int lvl = 2; if (missile._misource >= 0) @@ -1876,7 +1876,7 @@ void AddTown(Missile &missile, const AddMissileParameter ¶meter) other._mirange = 0; } PutMissile(missile); - if (missile._misource == MyPlayerId && !missile._miDelFlag && leveltype != DTYPE_TOWN) { + if (missile._misource == static_cast(MyPlayerId) && !missile._miDelFlag && leveltype != DTYPE_TOWN) { if (!setlevel) { NetSendCmdLocParam3(true, CMD_ACTIVATEPORTAL, missile.position.tile, currlevel, leveltype, 0); } else { @@ -1931,7 +1931,7 @@ void AddManashield(Missile &missile, const AddMissileParameter & /*parameter*/) return; player.pManaShield = true; - if (missile._misource == MyPlayerId) + if (missile._misource == static_cast(MyPlayerId)) NetSendCmd(true, CMD_SETSHIELD); if (missile._micaster == TARGET_MONSTERS) @@ -2164,7 +2164,7 @@ void AddGolem(Missile &missile, const AddMissileParameter ¶meter) return; } } - if (Monsters[playerId].position.tile != GolemHoldingCell && playerId == MyPlayerId) + if (Monsters[playerId].position.tile != GolemHoldingCell && playerId == static_cast(MyPlayerId)) M_StartKill(playerId, playerId); UseMana(playerId, SPL_GOLEM); @@ -2216,7 +2216,7 @@ void AddHealOther(Missile &missile, const AddMissileParameter & /*parameter*/) { missile._miDelFlag = true; UseMana(missile._misource, SPL_HEALOTHER); - if (missile._misource == MyPlayerId) { + if (missile._misource == static_cast(MyPlayerId)) { NewCursor(CURSOR_HEALOTHER); if (ControlMode != ControlTypes::KeyboardAndMouse) TryIconCurs(); @@ -2250,7 +2250,7 @@ void AddIdentify(Missile &missile, const AddMissileParameter & /*parameter*/) { missile._miDelFlag = true; UseMana(missile._misource, SPL_IDENTIFY); - if (missile._misource == MyPlayerId) { + if (missile._misource == static_cast(MyPlayerId)) { if (sbookflag) sbookflag = false; if (!invflag) { @@ -2347,7 +2347,7 @@ void AddRepair(Missile &missile, const AddMissileParameter & /*parameter*/) { missile._miDelFlag = true; UseMana(missile._misource, SPL_REPAIR); - if (missile._misource == MyPlayerId) { + if (missile._misource == static_cast(MyPlayerId)) { if (sbookflag) sbookflag = false; if (!invflag) { @@ -2363,7 +2363,7 @@ void AddRecharge(Missile &missile, const AddMissileParameter & /*parameter*/) { missile._miDelFlag = true; UseMana(missile._misource, SPL_RECHARGE); - if (missile._misource == MyPlayerId) { + if (missile._misource == static_cast(MyPlayerId)) { if (sbookflag) sbookflag = false; if (!invflag) { @@ -2379,7 +2379,7 @@ void AddDisarm(Missile &missile, const AddMissileParameter & /*parameter*/) { missile._miDelFlag = true; UseMana(missile._misource, SPL_DISARM); - if (missile._misource == MyPlayerId) { + if (missile._misource == static_cast(MyPlayerId)) { NewCursor(CURSOR_DISARM); if (ControlMode != ControlTypes::KeyboardAndMouse) { if (pcursobj != -1) @@ -2477,7 +2477,7 @@ void AddHbolt(Missile &missile, const AddMissileParameter ¶meter) void AddResurrect(Missile &missile, const AddMissileParameter & /*parameter*/) { UseMana(missile._misource, SPL_RESURRECT); - if (missile._misource == MyPlayerId) { + if (missile._misource == static_cast(MyPlayerId)) { NewCursor(CURSOR_RESURRECT); if (ControlMode != ControlTypes::KeyboardAndMouse) TryIconCurs(); @@ -2496,7 +2496,7 @@ void AddTelekinesis(Missile &missile, const AddMissileParameter & /*parameter*/) { missile._miDelFlag = true; UseMana(missile._misource, SPL_TELEKINESIS); - if (missile._misource == MyPlayerId) + if (missile._misource == static_cast(MyPlayerId)) NewCursor(CURSOR_TELEKINESIS); } @@ -2529,8 +2529,7 @@ void AddRportal(Missile &missile, const AddMissileParameter & /*parameter*/) void AddDiabApoca(Missile &missile, const AddMissileParameter & /*parameter*/) { - int players = gbIsMultiplayer ? MAX_PLRS : 1; - for (int pnum = 0; pnum < players; pnum++) { + for (size_t pnum = 0; pnum < Players.size(); pnum++) { Player &player = Players[pnum]; if (!player.plractive) continue; @@ -3029,7 +3028,7 @@ void MI_Search(Missile &missile) missile._miDelFlag = true; PlaySfxLoc(IS_CAST7, Players[missile._misource].position.tile); - if (missile._misource == MyPlayerId) + if (missile._misource == static_cast(MyPlayerId)) AutoMapShowItems = false; } @@ -3184,7 +3183,7 @@ void MI_Town(Missile &missile) missile.var2++; } - for (int p = 0; p < MAX_PLRS; p++) { + for (size_t p = 0; p < Players.size(); p++) { Player &player = Players[p]; if (player.plractive && player.isOnActiveLevel() && !player._pLvlChanging && player._pmode == PM_STAND && player.position.tile == missile.position.tile) { ClrPlrPath(player); @@ -3456,7 +3455,7 @@ void MI_Teleport(Missile &missile) ChangeLightXY(player._plid, player.position.tile); ChangeVisionXY(player._pvid, player.position.tile); } - if (id == MyPlayerId) { + if (id == static_cast(MyPlayerId)) { ViewPosition = Point { 0, 0 } + (player.position.tile - ScrollInfo.tile); } } diff --git a/Source/monster.cpp b/Source/monster.cpp index bef03547f..f36718751 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -710,7 +710,7 @@ void UpdateEnemy(Monster &monster) bool bestsameroom = false; const auto &position = monster.position.tile; if ((monster._mFlags & MFLAG_BERSERK) != 0 || (monster._mFlags & MFLAG_GOLEM) == 0) { - for (int pnum = 0; pnum < MAX_PLRS; pnum++) { + for (size_t pnum = 0; pnum < Players.size(); pnum++) { Player &player = Players[pnum]; if (!player.plractive || !player.isOnActiveLevel() || player._pLvlChanging || (((player._pHitPoints >> 6) == 0) && gbIsMultiplayer)) @@ -721,7 +721,7 @@ void UpdateEnemy(Monster &monster) || ((sameroom || !bestsameroom) && dist < bestDist) || (menemy == -1)) { monster._mFlags &= ~MFLAG_TARGETS_MONSTER; - menemy = pnum; + menemy = static_cast(pnum); target = player.position.future; bestDist = dist; bestsameroom = sameroom; @@ -1361,14 +1361,14 @@ void MonsterAttackPlayer(int i, int pnum, int hit, int minDam, int maxDam) if (blkper < blk) { Direction dir = GetDirection(player.position.tile, monster.position.tile); StartPlrBlock(pnum, dir); - if (pnum == MyPlayerId && player.wReflections > 0) { + if (pnum == static_cast(MyPlayerId) && player.wReflections > 0) { int dam = GenerateRnd(((maxDam - minDam) << 6) + 1) + (minDam << 6); dam = std::max(dam + (player._pIGetHit << 6), 64); CheckReflect(i, pnum, dam); } return; } - if (monster.MType->mtype == MT_YZOMBIE && pnum == MyPlayerId) { + if (monster.MType->mtype == MT_YZOMBIE && pnum == static_cast(MyPlayerId)) { if (player._pMaxHP > 64) { if (player._pMaxHPBase > 64) { player._pMaxHP -= 64; @@ -1384,7 +1384,7 @@ void MonsterAttackPlayer(int i, int pnum, int hit, int minDam, int maxDam) } int dam = (minDam << 6) + GenerateRnd(((maxDam - minDam) << 6) + 1); dam = std::max(dam + (player._pIGetHit << 6), 64); - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { if (player.wReflections > 0) CheckReflect(i, pnum, dam); ApplyPlrDamage(pnum, 0, 0, dam); @@ -3738,7 +3738,7 @@ void monster_some_crypt() void InitGolems() { if (!setlevel) { - for (int i = 0; i < MAX_PLRS; i++) + for (size_t i = 0; i < Players.size(); i++) AddMonster(GolemHoldingCell, Direction::South, 0, false); } } @@ -3806,7 +3806,7 @@ void SetMapMonsters(const uint16_t *dunData, Point startPosition) { AddMonsterType(MT_GOLEM, PLACE_SPECIAL); if (setlevel) - for (int i = 0; i < MAX_PLRS; i++) + for (size_t i = 0; i < MAX_PLRS; i++) AddMonster(GolemHoldingCell, Direction::South, 0, false); if (setlevel && setlvlnum == SL_VILEBETRAYER) { @@ -3954,7 +3954,7 @@ void M_StartHit(int i, int pnum, int dam) Monster &monster = Monsters[i]; monster.mWhoHit |= 1 << pnum; - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { delta_monster_hp(i, monster._mhitpoints, *MyPlayer); NetSendCmdMonDmg(false, i, dam); } @@ -3982,7 +3982,7 @@ void M_StartKill(int i, int pnum) assert(i >= 0 && i < MAXMONSTERS); auto &monster = Monsters[i]; - if (MyPlayerId == pnum) { + if (static_cast(MyPlayerId) == pnum) { delta_kill_monster(i, monster.position.tile, *MyPlayer); if (i != pnum) { NetSendCmdLocParam1(false, CMD_MONSTDEATH, monster.position.tile, i); @@ -4194,7 +4194,7 @@ void GolumAi(int i) void DeleteMonsterList() { - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { auto &golem = Monsters[i]; if (!golem._mDelFlag) continue; @@ -4831,7 +4831,7 @@ void TalktoMonster(Monster &monster) void SpawnGolem(int i, Point position, Missile &missile) { - assert(i >= 0 && i < MAX_PLRS); + assert(i >= 0 && static_cast(i) < Players.size()); Player &player = Players[i]; auto &golem = Monsters[i]; @@ -4849,7 +4849,7 @@ void SpawnGolem(int i, Point position, Missile &missile) golem._mFlags |= MFLAG_GOLEM; StartSpecialStand(golem, Direction::South); UpdateEnemy(golem); - if (i == MyPlayerId) { + if (i == static_cast(MyPlayerId)) { NetSendCmdGolem( golem.position.tile.x, golem.position.tile.y, diff --git a/Source/msg.cpp b/Source/msg.cpp index 84100ccb1..1f2ebfa07 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -639,9 +639,9 @@ void DeltaPutItem(const TCmdPItem &message, Point position, const Player &player bool IOwnLevel(const Player &player) { - int i; + size_t i; - for (i = 0; i < MAX_PLRS; i++) { + for (i = 0; i < Players.size(); i++) { if (!Players[i].plractive) continue; if (Players[i]._pLvlChanging) @@ -669,7 +669,7 @@ void DeltaOpenPortal(int pnum, Point position, uint8_t bLevel, dungeon_type bLTy void CheckUpdatePlayer(int pnum) { - if (gbIsMultiplayer && pnum == MyPlayerId) + if (gbIsMultiplayer && pnum == static_cast(MyPlayerId)) pfile_update(true); } @@ -999,7 +999,7 @@ DWORD OnPutItem(const TCmd *pCmd, int pnum) Player &player = Players[pnum]; if (player.isOnActiveLevel()) { int ii; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) ii = InvPutItem(player, position, ItemLimbo); else ii = SyncPutItem(player, position, message.wIndx, message.wCI, message.dwSeed, message.bId, message.bDur, message.bMDur, message.bCh, message.bMCh, message.wValue, message.dwBuff, message.wToHit, message.wMaxDam, message.bMinStr, message.bMinMag, message.bMinDex, message.bAC); @@ -1055,7 +1055,7 @@ DWORD OnRespawnItem(const TCmd *pCmd, int pnum) } else if (IsPItemValid(message)) { const Point position { message.x, message.y }; Player &player = Players[pnum]; - if (player.isOnActiveLevel() && pnum != MyPlayerId) { + if (player.isOnActiveLevel() && pnum != static_cast(MyPlayerId)) { SyncPutItem(player, position, message.wIndx, message.wCI, message.dwSeed, message.bId, message.bDur, message.bMDur, message.bCh, message.bMCh, message.wValue, message.dwBuff, message.wToHit, message.wMaxDam, message.bMinStr, message.bMinMag, message.bMinDex, message.bAC); } PutItemRecord(message.dwSeed, message.wCI, message.wIndx); @@ -1489,7 +1489,7 @@ DWORD OnNewLevel(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (pnum != MyPlayerId) { + } else if (pnum != static_cast(MyPlayerId)) { if (message.wParam1 < WM_FIRST || message.wParam1 > WM_LAST) return sizeof(message); @@ -1526,7 +1526,7 @@ DWORD OnMonstDeath(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); - else if (pnum != MyPlayerId && InDungeonBounds(position) && message.wParam1 < MAXMONSTERS) { + else if (pnum != static_cast(MyPlayerId) && InDungeonBounds(position) && message.wParam1 < MAXMONSTERS) { Player &player = Players[pnum]; if (player.isOnActiveLevel()) M_SyncStartKill(message.wParam1, position, pnum); @@ -1543,7 +1543,7 @@ DWORD OnKillGolem(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); - else if (pnum != MyPlayerId && InDungeonBounds(position)) { + else if (pnum != static_cast(MyPlayerId) && InDungeonBounds(position)) { Player &player = Players[pnum]; if (player.isOnActiveLevel()) M_SyncStartKill(pnum, position, pnum); @@ -1563,7 +1563,7 @@ DWORD OnAwakeGolem(const TCmd *pCmd, int pnum) } else if (InDungeonBounds(position)) { if (!Players[pnum].isOnActiveLevel()) { DeltaSyncGolem(message, pnum, message._currlevel); - } else if (pnum != MyPlayerId) { + } else if (pnum != static_cast(MyPlayerId)) { // Check if this player already has an active golem for (auto &missile : Missiles) { if (missile._mitype == MIS_GOLEM && missile._misource == pnum) { @@ -1584,7 +1584,7 @@ DWORD OnMonstDamage(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (pnum != MyPlayerId) { + } else if (pnum != static_cast(MyPlayerId)) { Player &player = Players[pnum]; if (player.isOnActiveLevel() && message.wMon < MAXMONSTERS) { auto &monster = Monsters[message.wMon]; @@ -1607,7 +1607,7 @@ DWORD OnPlayerDeath(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); - else if (pnum != MyPlayerId) + else if (pnum != static_cast(MyPlayerId)) StartPlayerKill(pnum, message.wParam1); else CheckUpdatePlayer(pnum); @@ -1721,7 +1721,7 @@ DWORD OnChangePlayerItems(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); - } else if (pnum != MyPlayerId && message.wIndx <= IDI_LAST) { + } else if (pnum != static_cast(MyPlayerId) && message.wIndx <= IDI_LAST) { CheckInvSwap(player, bodyLocation, message.wIndx, message.wCI, message.dwSeed, message.bId != 0, message.dwBuff); } @@ -1736,7 +1736,7 @@ DWORD OnDeletePlayerItems(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); - else if (pnum != MyPlayerId && message.bLoc < NUM_INVLOC) + else if (pnum != static_cast(MyPlayerId) && message.bLoc < NUM_INVLOC) inv_update_rem_item(Players[pnum], static_cast(message.bLoc)); return sizeof(message); @@ -1748,7 +1748,7 @@ DWORD OnPlayerLevel(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); - else if (message.wParam1 <= MAXCHARLEVEL && pnum != MyPlayerId) + else if (message.wParam1 <= MAXCHARLEVEL && pnum != static_cast(MyPlayerId)) Players[pnum]._pLevel = static_cast(message.wParam1); return sizeof(message); @@ -1776,7 +1776,7 @@ DWORD OnSpawnItem(const TCmd *pCmd, int pnum) } else if (IsPItemValid(message)) { Player &player = Players[pnum]; Point position = { message.x, message.y }; - if (player.isOnActiveLevel() && pnum != MyPlayerId) { + if (player.isOnActiveLevel() && pnum != static_cast(MyPlayerId)) { SyncDropItem(position, message.wIndx, message.wCI, message.dwSeed, message.bId, message.bDur, message.bMDur, message.bCh, message.bMCh, message.wValue, message.dwBuff, message.wToHit, message.wMaxDam, message.bMinStr, message.bMinMag, message.bMinDex, message.bAC); } PutItemRecord(message.dwSeed, message.wCI, message.wIndx); @@ -1824,7 +1824,7 @@ DWORD OnPlayerJoinLevel(const TCmd *pCmd, int pnum) EventPlrMsg(fmt::format(fmt::runtime(_("Player '{:s}' (level {:d}) just joined the game")), player._pName, player._pLevel)); } - if (player.plractive && MyPlayerId != pnum) { + if (player.plractive && static_cast(MyPlayerId) != pnum) { player.position.tile = position; if (isSetLevel) player.setLevel(static_cast<_setlevels>(playerLevel)); @@ -1843,7 +1843,7 @@ DWORD OnPlayerJoinLevel(const TCmd *pCmd, int pnum) dFlags[player.position.tile.x][player.position.tile.y] |= DungeonFlag::DeadPlayer; } - player._pvid = AddVision(player.position.tile, player._pLightRad, pnum == MyPlayerId); + player._pvid = AddVision(player.position.tile, player._pLightRad, pnum == static_cast(MyPlayerId)); } } @@ -1863,7 +1863,7 @@ DWORD OnActivatePortal(const TCmd *pCmd, int pnum) auto dungeonType = static_cast(message.wParam2); ActivatePortal(pnum, position, level, dungeonType, isSetLevel); - if (pnum != MyPlayerId) { + if (pnum != static_cast(MyPlayerId)) { if (leveltype == DTYPE_TOWN) { AddInTownPortal(pnum); } else if (Players[pnum].isOnActiveLevel()) { @@ -1906,7 +1906,7 @@ DWORD OnRestartTown(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) { SendPacket(pnum, pCmd, sizeof(*pCmd)); } else { - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { MyPlayerIsDead = false; gamemenu_off(); } @@ -1922,7 +1922,7 @@ DWORD OnSetStrength(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); - else if (message.wParam1 <= 750 && pnum != MyPlayerId) + else if (message.wParam1 <= 750 && pnum != static_cast(MyPlayerId)) SetPlrStr(Players[pnum], message.wParam1); return sizeof(message); @@ -1934,7 +1934,7 @@ DWORD OnSetDexterity(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); - else if (message.wParam1 <= 750 && pnum != MyPlayerId) + else if (message.wParam1 <= 750 && pnum != static_cast(MyPlayerId)) SetPlrDex(Players[pnum], message.wParam1); return sizeof(message); @@ -1946,7 +1946,7 @@ DWORD OnSetMagic(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); - else if (message.wParam1 <= 750 && pnum != MyPlayerId) + else if (message.wParam1 <= 750 && pnum != static_cast(MyPlayerId)) SetPlrMag(Players[pnum], message.wParam1); return sizeof(message); @@ -1958,7 +1958,7 @@ DWORD OnSetVitality(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) SendPacket(pnum, &message, sizeof(message)); - else if (message.wParam1 <= 750 && pnum != MyPlayerId) + else if (message.wParam1 <= 750 && pnum != static_cast(MyPlayerId)) SetPlrVit(Players[pnum], message.wParam1); return sizeof(message); @@ -1989,7 +1989,7 @@ DWORD OnSyncQuest(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) { SendPacket(pnum, &message, sizeof(message)); } else { - if (pnum != MyPlayerId && message.q < MAXQUESTS && message.qstate <= QUEST_HIVE_DONE) + if (pnum != static_cast(MyPlayerId) && message.q < MAXQUESTS && message.qstate <= QUEST_HIVE_DONE) SetMultiQuest(message.q, message.qstate, message.qlog != 0, message.qvar1); sgbDeltaChanged = true; } @@ -2038,7 +2038,7 @@ DWORD OnNova(const TCmd *pCmd, int pnum) if (gbBufferMsgs != 1) { Player &player = Players[pnum]; - if (player.isOnActiveLevel() && pnum != MyPlayerId && InDungeonBounds(position)) { + if (player.isOnActiveLevel() && pnum != static_cast(MyPlayerId) && InDungeonBounds(position)) { ClrPlrPath(player); player._pSpell = SPL_NOVA; player._pSplType = RSPLTYPE_INVALID; @@ -2336,7 +2336,7 @@ void DeltaSaveLevel() if (!gbIsMultiplayer) return; - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (i != MyPlayerId) ResetPlayerGFX(Players[i]); } @@ -2562,7 +2562,7 @@ void NetSendCmdGolem(uint8_t mx, uint8_t my, Direction dir, uint8_t menemy, int void NetSendCmdLoc(int playerId, bool bHiPri, _cmd_id bCmd, Point position) { - if (playerId == MyPlayerId && WasPlayerCmdAlreadyRequested(bCmd, position)) + if (playerId == static_cast(MyPlayerId) && WasPlayerCmdAlreadyRequested(bCmd, position)) return; TCmdLoc cmd; diff --git a/Source/multi.cpp b/Source/multi.cpp index 124c81d13..82893c8f1 100644 --- a/Source/multi.cpp +++ b/Source/multi.cpp @@ -148,7 +148,7 @@ bool IsNetPlayerValid(const Player &player) void CheckPlayerInfoTimeouts() { - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (i == MyPlayerId) { continue; } @@ -197,16 +197,16 @@ void MonsterSeeds() void HandleTurnUpperBit(int pnum) { - int i; + size_t i; - for (i = 0; i < MAX_PLRS; i++) { - if ((player_state[i] & PS_CONNECTED) != 0 && i != pnum) + for (i = 0; i < Players.size(); i++) { + if ((player_state[i] & PS_CONNECTED) != 0 && i != static_cast(pnum)) break; } if (MyPlayerId == i) { sgbSendDeltaTbl[pnum] = true; - } else if (MyPlayerId == pnum) { + } else if (static_cast(MyPlayerId) == pnum) { gbDeltaSender = i; } } @@ -226,7 +226,7 @@ void ParseTurn(int pnum, uint32_t turn) void PlayerLeftMsg(int pnum, bool left) { - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { return; } @@ -262,7 +262,7 @@ void PlayerLeftMsg(int pnum, bool left) void ClearPlayerLeftState() { - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (sgbPlayerLeftGameTbl[i]) { if (gbBufferMsgs == 1) msg_send_drop_pkt(i, sgdwPlayerLeftReasonTbl[i]); @@ -277,7 +277,7 @@ void ClearPlayerLeftState() void CheckDropPlayer() { - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if ((player_state[i] & PS_ACTIVE) == 0 && (player_state[i] & PS_CONNECTED) != 0) { SNetDropPlayer(i, LEAVE_DROP); } @@ -417,6 +417,8 @@ void EventHandler(bool add) bool InitSingle(GameData *gameData) { + Players.emplace_back(); + if (!SNetInitializeProvider(SELCONN_LOOPBACK, gameData)) { return false; } @@ -437,6 +439,8 @@ bool InitSingle(GameData *gameData) bool InitMulti(GameData *gameData) { + Players.resize(MAX_PLRS); + int playerId; while (true) { @@ -517,7 +521,7 @@ void multi_send_msg_packet(uint32_t pmask, const byte *data, size_t size) pkt.hdr.wLen = static_cast(len); memcpy(pkt.body, data, size); size_t playerID = 0; - for (size_t v = 1; playerID < MAX_PLRS; playerID++, v <<= 1) { + for (size_t v = 1; playerID < Players.size(); playerID++, v <<= 1) { if ((v & pmask) != 0) { if (!SNetSendMessage(playerID, &pkt.hdr, len) && SErrGetLastError() != STORM_ERROR_INVALID_PLAYER) { nthread_terminate_game("SNetSendMessage"); @@ -529,10 +533,10 @@ void multi_send_msg_packet(uint32_t pmask, const byte *data, size_t size) void multi_msg_countdown() { - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if ((player_state[i] & PS_TURN_ARRIVED) != 0) { if (gdwMsgLenTbl[i] == 4) - ParseTurn(i, *(DWORD *)glpMsgTbl[i]); + ParseTurn(static_cast(i), *(DWORD *)glpMsgTbl[i]); } } } @@ -557,7 +561,7 @@ bool multi_handle_delta() return false; } - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (sgbSendDeltaTbl[i]) { sgbSendDeltaTbl[i] = false; DeltaExportData(i); @@ -617,7 +621,7 @@ void multi_process_network_packets() } Point syncPosition = { pkt->px, pkt->py }; player.position.last = syncPosition; - if (dwID != MyPlayerId) { + if (dwID != static_cast(MyPlayerId)) { assert(gbBufferMsgs != 2); player._pHitPoints = pkt->php; player._pMaxHP = pkt->pmhp; @@ -662,7 +666,7 @@ void multi_process_network_packets() void multi_send_zero_packet(int pnum, _cmd_id bCmd, const byte *data, size_t size) { - assert(pnum != MyPlayerId); + assert(pnum != static_cast(MyPlayerId)); assert(data != nullptr); assert(size <= 0x0ffff); @@ -708,6 +712,8 @@ void NetClose() SNetLeaveGame(3); if (gbIsMultiplayer) SDL_Delay(2000); + Players.clear(); + MyPlayer = nullptr; } bool NetInit(bool bSinglePlayer) @@ -720,9 +726,8 @@ bool NetInit(bool bSinglePlayer) memset(sgbPlayerLeftGameTbl, 0, sizeof(sgbPlayerLeftGameTbl)); memset(sgdwPlayerLeftReasonTbl, 0, sizeof(sgdwPlayerLeftReasonTbl)); memset(sgbSendDeltaTbl, 0, sizeof(sgbSendDeltaTbl)); - for (Player &player : Players) { - player.Reset(); - } + Players.clear(); + MyPlayer = nullptr; memset(sgwPackPlrOffsetTbl, 0, sizeof(sgwPackPlrOffsetTbl)); SNetSetBasePlayer(0); if (bSinglePlayer) { @@ -785,7 +790,7 @@ void recv_plrinfo(int pnum, const TCmdPlrInfoHdr &header, bool recv) { static PlayerPack PackedPlayerBuffer[MAX_PLRS]; - if (MyPlayerId == pnum) { + if (static_cast(MyPlayerId) == pnum) { return; } assert(pnum >= 0 && pnum < MAX_PLRS); diff --git a/Source/objects.cpp b/Source/objects.cpp index b9c10c3be..497a6e4c0 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -2371,7 +2371,7 @@ void OperateChest(int pnum, int i, bool sendmsg) AddMissile(Objects[i].position, player.position.tile, mdir, mtype, TARGET_PLAYERS, -1, 0, 0); Objects[i]._oTrapFlag = false; } - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam2(false, CMD_PLROPOBJ, pnum, i); } @@ -2382,7 +2382,7 @@ void OperateMushroomPatch(int pnum, Object &questContainer) } if (Quests[Q_MUSHROOM]._qactive != QUEST_ACTIVE) { - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { Players[pnum].Say(HeroSpeech::ICantUseThisYet); } return; @@ -2408,7 +2408,7 @@ void OperateInnSignChest(int pnum, Object &questContainer) } if (Quests[Q_LTBANNER]._qvar1 != 2) { - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { Players[pnum].Say(HeroSpeech::ICantOpenThisYet); } return; @@ -2449,7 +2449,7 @@ void OperateSlainHero(int pnum, int i) CreateMagicWeapon(Objects[i].position, ItemType::Axe, ICURS_BATTLE_AXE, true, false); } MyPlayer->Say(HeroSpeech::RestInPeaceMyFriend); - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam1(false, CMD_OPERATEOBJ, i); } @@ -2496,7 +2496,7 @@ void OperateSarc(int pnum, int i, bool sendmsg) CreateRndItem(Objects[i].position, false, sendmsg, false); if (Objects[i]._oVar1 >= 8) SpawnSkeleton(Objects[i]._oVar2, Objects[i].position); - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam1(false, CMD_OPERATEOBJ, i); } @@ -3408,7 +3408,7 @@ void OperateSkelBook(int pnum, int i, bool sendmsg) CreateTypeItem(Objects[i].position, false, ItemType::Misc, IMISC_SCROLL, sendmsg, false); else CreateTypeItem(Objects[i].position, false, ItemType::Misc, IMISC_BOOK, sendmsg, false); - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam1(false, CMD_OPERATEOBJ, i); } @@ -3436,7 +3436,7 @@ void OperateBookCase(int pnum, int i, bool sendmsg) zhar._mmode = MonsterMode::Talk; } } - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam1(false, CMD_OPERATEOBJ, i); } @@ -3448,7 +3448,7 @@ void OperateDecap(int pnum, int i, bool sendmsg) Objects[i]._oSelFlag = 0; SetRndSeed(Objects[i]._oRndSeed); CreateRndItem(Objects[i].position, false, sendmsg, false); - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam1(false, CMD_OPERATEOBJ, i); } @@ -3470,7 +3470,7 @@ void OperateArmorStand(int pnum, int i, bool sendmsg) } else if (currlevel >= 13) { CreateTypeItem(Objects[i].position, true, ItemType::HeavyArmor, IMISC_NONE, sendmsg, false); } - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam1(false, CMD_OPERATEOBJ, i); } @@ -3513,7 +3513,7 @@ bool OperateFountains(int pnum, int i) bool applied = false; switch (Objects[i]._otype) { case OBJ_BLOODFTN: - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) return false; if (player._pHitPoints < player._pMaxHP) { @@ -3529,7 +3529,7 @@ bool OperateFountains(int pnum, int i) PlaySfxLoc(LS_FOUNTAIN, Objects[i].position); break; case OBJ_PURIFYINGFTN: - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) return false; if (player._pMana < player._pMaxMana) { @@ -3561,7 +3561,7 @@ bool OperateFountains(int pnum, int i) 0, 2 * leveltype); applied = true; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam1(false, CMD_OPERATEOBJ, i); break; case OBJ_TEARFTN: { @@ -3569,7 +3569,7 @@ bool OperateFountains(int pnum, int i) break; PlaySfxLoc(LS_FOUNTAIN, Objects[i].position); Objects[i]._oSelFlag = 0; - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) return false; unsigned randomValue = (Objects[i]._oRndSeed >> 16) % 12; @@ -3598,7 +3598,7 @@ bool OperateFountains(int pnum, int i) CheckStats(player); applied = true; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam1(false, CMD_OPERATEOBJ, i); } break; default: @@ -3621,7 +3621,7 @@ void OperateWeaponRack(int pnum, int i, bool sendmsg) CreateTypeItem(Objects[i].position, leveltype != DTYPE_CATHEDRAL, weaponType, IMISC_NONE, sendmsg, false); - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmdParam1(false, CMD_OPERATEOBJ, i); } @@ -3660,7 +3660,7 @@ bool OperateNakrulBook(int s) void OperateStoryBook(int pnum, int i) { - if (Objects[i]._oSelFlag == 0 || qtextflag || pnum != MyPlayerId) { + if (Objects[i]._oSelFlag == 0 || qtextflag || pnum != static_cast(MyPlayerId)) { return; } Objects[i]._oAnimFrame = Objects[i]._oVar4; @@ -3686,7 +3686,7 @@ void OperateLazStand(int pnum, int i) return; } - if (Objects[i]._oSelFlag == 0 || qtextflag || pnum != MyPlayerId) { + if (Objects[i]._oSelFlag == 0 || qtextflag || pnum != static_cast(MyPlayerId)) { return; } @@ -3805,7 +3805,7 @@ void BreakBarrel(int pnum, Object &barrel, bool forcebreak, bool sendmsg) { if (barrel._oSelFlag == 0) return; - if (!forcebreak && pnum != MyPlayerId) { + if (!forcebreak && pnum != static_cast(MyPlayerId)) { return; } @@ -3858,7 +3858,7 @@ void BreakBarrel(int pnum, Object &barrel, bool forcebreak, bool sendmsg) if (barrel._oVar2 >= 8) SpawnSkeleton(barrel._oVar4, barrel.position); } - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { NetSendCmdParam2(false, CMD_BREAKOBJ, pnum, static_cast(barrel.GetId())); } } @@ -4791,7 +4791,7 @@ void ObjChangeMapResync(int x1, int y1, int x2, int y2) void TryDisarm(int pnum, int i) { - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NewCursor(CURSOR_HAND); if (!Objects[i]._oTrapFlag) { return; @@ -4824,7 +4824,7 @@ int ItemMiscIdIdx(item_misc_id imiscid) void OperateObject(int pnum, int i, bool teleFlag) { - bool sendmsg = pnum == MyPlayerId; + bool sendmsg = pnum == static_cast(MyPlayerId); switch (Objects[i]._otype) { case OBJ_L1LDOOR: case OBJ_L1RDOOR: @@ -5049,28 +5049,28 @@ void SyncOpObject(int pnum, int cmd, int i) switch (Objects[i]._otype) { case OBJ_L1LDOOR: case OBJ_L1RDOOR: - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) SyncOpL1Door(cmd, i); break; case OBJ_L2LDOOR: case OBJ_L2RDOOR: - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) SyncOpL2Door(cmd, i); break; case OBJ_L3LDOOR: case OBJ_L3RDOOR: - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) SyncOpL3Door(cmd, i); break; case OBJ_L5LDOOR: case OBJ_L5RDOOR: - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) SyncOpL5Door(cmd, i); break; case OBJ_LEVER: case OBJ_L5LEVER: case OBJ_SWITCHSKL: - OperateLever(i, pnum == MyPlayerId); + OperateLever(i, pnum == static_cast(MyPlayerId)); break; case OBJ_CHEST1: case OBJ_CHEST2: @@ -5087,7 +5087,7 @@ void SyncOpObject(int pnum, int cmd, int i) case OBJ_BLINDBOOK: case OBJ_BLOODBOOK: case OBJ_STEELTOME: - OperateBookLever(i, pnum == MyPlayerId); + OperateBookLever(i, pnum == static_cast(MyPlayerId)); break; case OBJ_SHRINEL: case OBJ_SHRINER: diff --git a/Source/player.cpp b/Source/player.cpp index ded0a7091..3392625d1 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -37,9 +37,9 @@ namespace devilution { -int MyPlayerId; -Player *MyPlayer = &Players[MyPlayerId]; -Player Players[MAX_PLRS]; +size_t MyPlayerId; +Player *MyPlayer; +std::vector Players; bool MyPlayerIsDead; /** Specifies the X-coordinate delta from the player start location in Tristram. */ @@ -324,7 +324,7 @@ void HandleWalkMode(int pnum, Displacement vel, Direction dir) // The player's tile position after finishing this movement action player.position.future = player.position.tile + dirModeParams.tileAdd; - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { ScrollViewPort(player, dirModeParams.scrollDir); } @@ -355,7 +355,7 @@ void StartWalk(int pnum, Displacement vel, Direction dir, bool pmWillBeCalled) { Player &player = Players[pnum]; - if (player._pInvincible && player._pHitPoints == 0 && pnum == MyPlayerId) { + if (player._pInvincible && player._pHitPoints == 0 && pnum == static_cast(MyPlayerId)) { SyncPlrKill(pnum, -1); return; } @@ -397,7 +397,7 @@ void StartWalkStand(int pnum) player.position.future = player.position.tile; player.position.offset = { 0, 0 }; - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { ScrollInfo.offset = { 0, 0 }; ScrollInfo._sdir = ScrollDirection::None; ViewPosition = player.position.tile; @@ -434,7 +434,7 @@ void StartAttack(int pnum, Direction d) } Player &player = Players[pnum]; - if (player._pInvincible && player._pHitPoints == 0 && pnum == MyPlayerId) { + if (player._pInvincible && player._pHitPoints == 0 && pnum == static_cast(MyPlayerId)) { SyncPlrKill(pnum, -1); return; } @@ -466,7 +466,7 @@ void StartRangeAttack(int pnum, Direction d, int cx, int cy) } Player &player = Players[pnum]; - if (player._pInvincible && player._pHitPoints == 0 && pnum == MyPlayerId) { + if (player._pInvincible && player._pHitPoints == 0 && pnum == static_cast(MyPlayerId)) { SyncPlrKill(pnum, -1); return; } @@ -507,7 +507,7 @@ void StartSpell(int pnum, Direction d, int cx, int cy) app_fatal("StartSpell: illegal player %i", pnum); Player &player = Players[pnum]; - if (player._pInvincible && player._pHitPoints == 0 && pnum == MyPlayerId) { + if (player._pInvincible && player._pHitPoints == 0 && pnum == static_cast(MyPlayerId)) { SyncPlrKill(pnum, -1); return; } @@ -613,19 +613,19 @@ void InitLevelChange(int pnum) player.pManaShield = false; player.wReflections = 0; // share info about your manashield when another player joins the level - if (pnum != MyPlayerId && myPlayer.pManaShield) + if (pnum != static_cast(MyPlayerId) && myPlayer.pManaShield) NetSendCmd(true, CMD_SETSHIELD); // share info about your reflect charges when another player joins the level - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) NetSendCmdParam1(true, CMD_SETREFLECT, myPlayer.wReflections); - if (pnum == MyPlayerId && qtextflag) { + if (pnum == static_cast(MyPlayerId) && qtextflag) { qtextflag = false; stream_stop(); } RemovePlrFromMap(pnum); SetPlayerOld(player); - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { dPlayer[player.position.tile.x][player.position.tile.y] = pnum + 1; } else { player._pLvlVisited[player.plrlevel] = true; @@ -635,7 +635,7 @@ void InitLevelChange(int pnum) player.destAction = ACTION_NONE; player._pLvlChanging = true; - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { player.pLvlLoad = 10; } } @@ -686,7 +686,7 @@ bool DoWalk(int pnum, int variant) } // Update the "camera" tile position - if (pnum == MyPlayerId && ScrollInfo._sdir != ScrollDirection::None) { + if (pnum == static_cast(MyPlayerId) && ScrollInfo._sdir != ScrollDirection::None) { ViewPosition = Point { 0, 0 } + (player.position.tile - ScrollInfo.tile); } @@ -902,7 +902,7 @@ bool PlrHitMonst(int pnum, int m, bool adjacentDamage = false) if (adjacentDamage) dam >>= 2; - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { if (HasAnyOf(player.pDamAcFlags, ItemSpecialEffectHf::Peril)) { dam2 += player._pIGetHit << 6; if (dam2 >= 0) { @@ -1466,7 +1466,7 @@ void CheckNewPath(int pnum, bool pmWillBeCalled) Direction d; if (player.walkpath[0] != WALK_NONE) { if (player._pmode == PM_STAND) { - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { if (player.destAction == ACTION_ATTACKMON || player.destAction == ACTION_ATTACKPLR) { if (player.destAction == ACTION_ATTACKMON) { x = abs(player.position.future.x - monster->position.future.x); @@ -1633,7 +1633,7 @@ void CheckNewPath(int pnum, bool pmWillBeCalled) } break; case ACTION_PICKUPITEM: - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { x = abs(player.position.tile.x - item->position.x); y = abs(player.position.tile.y - item->position.y); if (x <= 1 && y <= 1 && pcurs == CURSOR_HAND && !item->_iRequest) { @@ -1643,7 +1643,7 @@ void CheckNewPath(int pnum, bool pmWillBeCalled) } break; case ACTION_PICKUPAITEM: - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { x = abs(player.position.tile.x - item->position.x); y = abs(player.position.tile.y - item->position.y); if (x <= 1 && y <= 1 && pcurs == CURSOR_HAND) { @@ -1652,7 +1652,7 @@ void CheckNewPath(int pnum, bool pmWillBeCalled) } break; case ACTION_TALK: - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { TalkToTowner(player, player.destParam1); } break; @@ -1750,8 +1750,8 @@ bool PlrDeathModeOK(Player &player) void ValidatePlayer() { - if ((DWORD)MyPlayerId >= MAX_PLRS) { - app_fatal("ValidatePlayer: illegal player %i", MyPlayerId); + if (MyPlayerId >= MAX_PLRS) { + app_fatal("Invalid player %u", static_cast(MyPlayerId)); } Player &myPlayer = *MyPlayer; @@ -1985,13 +1985,6 @@ bool Player::IsWalking() const return IsAnyOf(_pmode, PM_WALK, PM_WALK2, PM_WALK3); } -void Player::Reset() -{ - // Create empty default initialized Player on heap to avoid excessive stack usage - auto emptyPlayer = std::make_unique(); - *this = std::move(*emptyPlayer); -} - int Player::GetManaShieldDamageReduction() { constexpr int8_t Max = 7; @@ -2449,7 +2442,7 @@ void CreatePlayer(int playerId, HeroClass c) } Player &player = Players[playerId]; - player.Reset(); + player = {}; SetRndSeed(SDL_GetTicks()); player._pClass = c; @@ -2702,7 +2695,7 @@ void AddPlrExperience(Player &player, int lvl, int exp) void AddPlrMonstExper(int lvl, int exp, char pmask) { int totplrs = 0; - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (((1 << i) & pmask) != 0) { totplrs++; } @@ -2717,12 +2710,10 @@ void AddPlrMonstExper(int lvl, int exp, char pmask) void InitPlayer(Player &player, bool firstTime) { - Player &myPlayer = *MyPlayer; - if (firstTime) { player._pRSplType = RSPLTYPE_INVALID; player._pRSpell = SPL_INVALID; - if (&player == &myPlayer) + if (&player == MyPlayer) LoadHotkeys(); player._pSBkSpell = SPL_INVALID; player._pSpell = player._pRSpell; @@ -2753,7 +2744,7 @@ void InitPlayer(Player &player, bool firstTime) player._pdir = Direction::South; - if (&player == &myPlayer) { + if (&player == MyPlayer) { if (!firstTime || leveltype != DTYPE_TOWN) { player.position.tile = ViewPosition; } @@ -2769,13 +2760,13 @@ void InitPlayer(Player &player, bool firstTime) player.walkpath[0] = WALK_NONE; player.destAction = ACTION_NONE; - if (&player == &myPlayer) { + if (&player == MyPlayer) { player._plid = AddLight(player.position.tile, player._pLightRad); ChangeLightXY(player._plid, player.position.tile); // fix for a bug where old light is still visible at the entrance after reentering level } else { player._plid = NO_LIGHT; } - player._pvid = AddVision(player.position.tile, player._pLightRad, &player == &myPlayer); + player._pvid = AddVision(player.position.tile, player._pLightRad, &player == MyPlayer); } if (player._pClass == HeroClass::Warrior) { @@ -2795,7 +2786,7 @@ void InitPlayer(Player &player, bool firstTime) player._pNextExper = ExpLvlsTbl[player._pLevel]; player._pInvincible = false; - if (&player == &myPlayer) { + if (&player == MyPlayer) { MyPlayerIsDead = false; ScrollInfo.offset = { 0, 0 }; ScrollInfo._sdir = ScrollDirection::None; @@ -2804,8 +2795,8 @@ void InitPlayer(Player &player, bool firstTime) void InitMultiView() { - if ((DWORD)MyPlayerId >= MAX_PLRS) { - app_fatal("InitPlayer: illegal player %i", MyPlayerId); + if (MyPlayerId >= MAX_PLRS) { + app_fatal("Invalid player %u", static_cast(MyPlayerId)); } Player &myPlayer = *MyPlayer; @@ -2863,7 +2854,7 @@ void StartStand(int pnum, Direction dir) } Player &player = Players[pnum]; - if (player._pInvincible && player._pHitPoints == 0 && pnum == MyPlayerId) { + if (player._pInvincible && player._pHitPoints == 0 && pnum == static_cast(MyPlayerId)) { SyncPlrKill(pnum, -1); return; } @@ -2883,7 +2874,7 @@ void StartPlrBlock(int pnum, Direction dir) } Player &player = Players[pnum]; - if (player._pInvincible && player._pHitPoints == 0 && pnum == MyPlayerId) { + if (player._pInvincible && player._pHitPoints == 0 && pnum == static_cast(MyPlayerId)) { SyncPlrKill(pnum, -1); return; } @@ -2941,7 +2932,7 @@ void StartPlrHit(int pnum, int dam, bool forcehit) } Player &player = Players[pnum]; - if (player._pInvincible && player._pHitPoints == 0 && pnum == MyPlayerId) { + if (player._pInvincible && player._pHitPoints == 0 && pnum == static_cast(MyPlayerId)) { SyncPlrKill(pnum, -1); return; } @@ -2997,7 +2988,7 @@ StartPlayerKill(int pnum, int earflag) return; } - if (MyPlayerId == pnum) { + if (static_cast(MyPlayerId) == pnum) { NetSendCmdParam1(true, CMD_PLRDEAD, earflag); } @@ -3021,7 +3012,7 @@ StartPlayerKill(int pnum, int earflag) player._pInvincible = true; SetPlayerHitPoints(player, 0); - if (pnum != MyPlayerId && earflag == 0 && !diablolevel) { + if (pnum != static_cast(MyPlayerId) && earflag == 0 && !diablolevel) { for (auto &item : player.InvBody) { item.clear(); } @@ -3034,7 +3025,7 @@ StartPlayerKill(int pnum, int earflag) dFlags[player.position.tile.x][player.position.tile.y] |= DungeonFlag::DeadPlayer; SetPlayerOld(player); - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { drawhpflag = true; if (!player.HoldItem.isEmpty()) { @@ -3116,7 +3107,7 @@ void ApplyPlrDamage(int pnum, int dam, int minHP /*= 0*/, int frac /*= 0*/, int if (manaShieldLevel > 0) { totalDamage += totalDamage / -player.GetManaShieldDamageReduction(); } - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) drawmanaflag = true; if (player._pMana >= totalDamage) { player._pMana -= totalDamage; @@ -3129,7 +3120,7 @@ void ApplyPlrDamage(int pnum, int dam, int minHP /*= 0*/, int frac /*= 0*/, int } player._pMana = 0; player._pManaBase = player._pMaxManaBase - player._pMaxMana; - if (pnum == MyPlayerId) + if (pnum == static_cast(MyPlayerId)) NetSendCmd(true, CMD_REMSHIELD); } } @@ -3168,7 +3159,7 @@ void SyncPlrKill(int pnum, int earflag) void RemovePlrMissiles(int pnum) { - if (leveltype != DTYPE_TOWN && pnum == MyPlayerId) { + if (leveltype != DTYPE_TOWN && pnum == static_cast(MyPlayerId)) { auto &golem = Monsters[MyPlayerId]; if (golem.position.tile.x != 1 || golem.position.tile.y != 0) { M_StartKill(MyPlayerId, MyPlayerId); @@ -3223,7 +3214,7 @@ StartNewLvl(int pnum, interface_mode fom, int lvl) app_fatal("StartNewLvl"); } - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { player._pmode = PM_NEWLVL; player._pInvincible = true; PostMessage(fom, 0, 0); @@ -3251,7 +3242,7 @@ void RestartTownLvl(int pnum) CalcPlrInv(player, false); - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { player._pmode = PM_NEWLVL; player._pInvincible = true; PostMessage(WM_DIABRETOWN, 0, 0); @@ -3275,7 +3266,7 @@ void StartWarpLvl(int pnum, int pidx) } } - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { SetCurrentPortal(pidx); player._pmode = PM_NEWLVL; player._pInvincible = true; @@ -3285,8 +3276,8 @@ void StartWarpLvl(int pnum, int pidx) void ProcessPlayers() { - if ((DWORD)MyPlayerId >= MAX_PLRS) { - app_fatal("ProcessPlayers: illegal player %i", MyPlayerId); + if (MyPlayerId >= MAX_PLRS) { + app_fatal("Illegal player %u", static_cast(MyPlayerId)); } Player &myPlayer = *MyPlayer; @@ -3318,7 +3309,7 @@ void ProcessPlayers() ValidatePlayer(); - for (int pnum = 0; pnum < MAX_PLRS; pnum++) { + for (size_t pnum = 0; pnum < Players.size(); pnum++) { Player &player = Players[pnum]; if (player.plractive && player.isOnActiveLevel() && (pnum == MyPlayerId || !player._pLvlChanging)) { CheckCheatStats(player); @@ -3453,8 +3444,8 @@ void CheckPlrSpell(bool isShiftHeld, spell_id spellID, spell_type spellType) bool addflag = false; int sl; - if ((DWORD)MyPlayerId >= MAX_PLRS) { - app_fatal("CheckPlrSpell: illegal player %i", MyPlayerId); + if (MyPlayerId >= MAX_PLRS) { + app_fatal("Invalid player %u", static_cast(MyPlayerId)); } Player &myPlayer = *MyPlayer; @@ -3624,7 +3615,7 @@ void SyncInitPlrPos(int pnum) player.position.tile = position; dPlayer[position.x][position.y] = pnum + 1; - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { player.position.future = position; ViewPosition = position; } @@ -3639,7 +3630,7 @@ void SyncInitPlr(int pnum) SetPlrAnims(player); SyncInitPlrPos(pnum); - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) player._plid = NO_LIGHT; } @@ -3813,8 +3804,8 @@ enum { void PlayDungMsgs() { - if ((DWORD)MyPlayerId >= MAX_PLRS) { - app_fatal("PlayDungMsgs: illegal player %i", MyPlayerId); + if (MyPlayerId >= MAX_PLRS) { + app_fatal("Invalid player %u", static_cast(MyPlayerId)); } Player &myPlayer = *MyPlayer; diff --git a/Source/player.h b/Source/player.h index 7b3d03a67..afe427ebd 100644 --- a/Source/player.h +++ b/Source/player.h @@ -7,6 +7,7 @@ #include #include +#include #include "diablo.h" #include "engine.h" @@ -452,11 +453,6 @@ struct Player { */ bool IsWalking() const; - /** - * @brief Resets all Data of the current Player - */ - void Reset(); - /** * @brief Returns item location taking into consideration barbarian's ability to hold two-handed maces and clubs in one hand. */ @@ -738,9 +734,9 @@ struct Player { } }; -extern DVL_API_FOR_TEST int MyPlayerId; +extern DVL_API_FOR_TEST size_t MyPlayerId; extern DVL_API_FOR_TEST Player *MyPlayer; -extern DVL_API_FOR_TEST Player Players[MAX_PLRS]; +extern DVL_API_FOR_TEST std::vector Players; extern bool MyPlayerIsDead; extern int BlockBonuses[enum_size::value]; diff --git a/Source/portal.cpp b/Source/portal.cpp index 18ef3dda7..aac1ff4c6 100644 --- a/Source/portal.cpp +++ b/Source/portal.cpp @@ -152,7 +152,7 @@ void GetPortalLevel() leveltype = Portals[portalindex].ltype; } - if (portalindex == MyPlayerId) { + if (portalindex == static_cast(MyPlayerId)) { NetSendCmd(true, CMD_DEACTIVATEPORTAL); DeactivatePortal(portalindex); } @@ -165,7 +165,7 @@ void GetPortalLvlPos() } else { ViewPosition = Portals[portalindex].position; - if (portalindex != MyPlayerId) { + if (portalindex != static_cast(MyPlayerId)) { ViewPosition.x++; ViewPosition.y++; } diff --git a/Source/qol/autopickup.cpp b/Source/qol/autopickup.cpp index d6e69af1b..3c810af34 100644 --- a/Source/qol/autopickup.cpp +++ b/Source/qol/autopickup.cpp @@ -75,7 +75,7 @@ bool DoPickup(Item item) void AutoPickup(int pnum) { - if (pnum != MyPlayerId) + if (pnum != static_cast(MyPlayerId)) return; if (leveltype == DTYPE_TOWN && !*sgOptions.Gameplay.autoPickupInTown) return; diff --git a/Source/qol/monhealthbar.cpp b/Source/qol/monhealthbar.cpp index 7e2687a5c..f282ab04e 100644 --- a/Source/qol/monhealthbar.cpp +++ b/Source/qol/monhealthbar.cpp @@ -160,7 +160,7 @@ void DrawMonsterHealthBar(const Surface &out) } int tagOffset = 5; - for (int i = 0; i < MAX_PLRS; i++) { + for (size_t i = 0; i < Players.size(); i++) { if (1 << i & monster.mWhoHit) { DrawArt(out, position + Displacement { tagOffset, height - 31 }, &playerExpTags, i + 1); } else if (Players[i].plractive) { diff --git a/Source/spells.cpp b/Source/spells.cpp index 6a0d531c0..b3279082f 100644 --- a/Source/spells.cpp +++ b/Source/spells.cpp @@ -100,7 +100,7 @@ void PlacePlayer(int pnum) dPlayer[newPosition.x][newPosition.y] = pnum + 1; - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { ViewPosition = newPosition; } } @@ -170,7 +170,7 @@ void UseMana(int id, spell_id sn) { int ma; // mana cost - if (id != MyPlayerId) + if (id != static_cast(MyPlayerId)) return; Player &myPlayer = *MyPlayer; diff --git a/Source/sync.cpp b/Source/sync.cpp index a2c6a2160..3f34a5286 100644 --- a/Source/sync.cpp +++ b/Source/sync.cpp @@ -295,7 +295,7 @@ uint32_t OnSyncData(const TCmd *pCmd, int pnum) if (gbBufferMsgs == 1) { return header.wLen + sizeof(header); } - if (pnum == MyPlayerId) { + if (pnum == static_cast(MyPlayerId)) { return header.wLen + sizeof(header); } @@ -312,7 +312,7 @@ uint32_t OnSyncData(const TCmd *pCmd, int pnum) continue; if (GetLevelForMultiplayer(*MyPlayer) == level) { - SyncMonster(pnum > MyPlayerId, monsterSyncs[i]); + SyncMonster(pnum > static_cast(MyPlayerId), monsterSyncs[i]); } delta_sync_monster(monsterSyncs[i], level); diff --git a/Source/towners.cpp b/Source/towners.cpp index 00ad41e4b..c5a9d86c1 100644 --- a/Source/towners.cpp +++ b/Source/towners.cpp @@ -808,7 +808,7 @@ bool IsTownerPresent(_talker_id npc) case TOWN_COWFARM: return gbIsHellfire && sgGameInitInfo.bCowQuest != 0; case TOWN_GIRL: - return gbIsHellfire && sgGameInitInfo.bTheoQuest != 0 && Players->_pLvlVisited[17] && Quests[Q_GIRL]._qactive != QUEST_DONE; + return gbIsHellfire && sgGameInitInfo.bTheoQuest != 0 && MyPlayer->_pLvlVisited[17] && Quests[Q_GIRL]._qactive != QUEST_DONE; default: return true; } diff --git a/test/drlg_l1_test.cpp b/test/drlg_l1_test.cpp index 93bf8b6cf..1e4f1ee7f 100644 --- a/test/drlg_l1_test.cpp +++ b/test/drlg_l1_test.cpp @@ -12,6 +12,8 @@ TEST(Drlg_l1, CreateL5Dungeon_diablo_1_2588) { LoadExpectedLevelData("diablo/1-2588.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = true; TestCreateDungeon(1, 2588, ENTRY_MAIN); @@ -24,6 +26,8 @@ TEST(Drlg_l1, CreateL5Dungeon_diablo_1_743271966) { LoadExpectedLevelData("diablo/1-743271966.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = true; TestCreateDungeon(1, 743271966, ENTRY_MAIN); @@ -36,6 +40,8 @@ TEST(Drlg_l1, CreateL5Dungeon_diablo_2_1383137027) { LoadExpectedLevelData("diablo/2-1383137027.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = true; InitQuests(); @@ -54,6 +60,8 @@ TEST(Drlg_l1, CreateL5Dungeon_diablo_3_844660068) { LoadExpectedLevelData("diablo/3-844660068.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = true; Quests[Q_SKELKING]._qactive = QUEST_NOTAVAIL; @@ -67,6 +75,8 @@ TEST(Drlg_l1, CreateL5Dungeon_diablo_4_609325643) { LoadExpectedLevelData("diablo/4-609325643.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = true; Quests[Q_LTBANNER]._qactive = QUEST_NOTAVAIL; @@ -80,6 +90,8 @@ TEST(Drlg_l1, CreateL5Dungeon_hellfire_1_401921334) { LoadExpectedLevelData("hellfire/1-401921334.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = false; TestCreateDungeon(1, 401921334, ENTRY_MAIN); @@ -92,6 +104,8 @@ TEST(Drlg_l1, CreateL5Dungeon_hellfire_2_128964898) { LoadExpectedLevelData("hellfire/2-128964898.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = false; InitQuests(); Quests[Q_PWATER]._qactive = QUEST_NOTAVAIL; @@ -107,6 +121,8 @@ TEST(Drlg_l1, CreateL5Dungeon_hellfire_2_1180526547) { LoadExpectedLevelData("hellfire/2-1180526547.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = false; InitQuests(); Quests[Q_PWATER]._qactive = QUEST_NOTAVAIL; @@ -122,6 +138,8 @@ TEST(Drlg_l1, CreateL5Dungeon_hellfire_3_1799396623) { LoadExpectedLevelData("hellfire/3-1799396623.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = false; InitQuests(); Quests[Q_SKELKING]._qactive = QUEST_NOTAVAIL; @@ -136,6 +154,8 @@ TEST(Drlg_l1, CreateL5Dungeon_hellfire_3_1512491184) { LoadExpectedLevelData("hellfire/3-1512491184.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = false; InitQuests(); Quests[Q_SKELKING]._qactive = QUEST_INIT; @@ -150,6 +170,8 @@ TEST(Drlg_l1, CreateL5Dungeon_hellfire_4_1190318991) { LoadExpectedLevelData("hellfire/4-1190318991.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = false; InitQuests(); Quests[Q_LTBANNER]._qactive = QUEST_NOTAVAIL; @@ -164,6 +186,8 @@ TEST(Drlg_l1, CreateL5Dungeon_hellfire_4_1924296259) { LoadExpectedLevelData("hellfire/4-1924296259.dun"); + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->pOriginalCathedral = false; InitQuests(); Quests[Q_LTBANNER]._qactive = QUEST_INIT; diff --git a/test/effects_test.cpp b/test/effects_test.cpp index 4b3e0bf31..36a576e62 100644 --- a/test/effects_test.cpp +++ b/test/effects_test.cpp @@ -7,6 +7,8 @@ using namespace devilution; TEST(Effects, CalculateSoundPosition_center) { + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->position.tile = { 50, 50 }; int plVolume = 0; int plPan = 0; @@ -17,6 +19,8 @@ TEST(Effects, CalculateSoundPosition_center) TEST(Effects, CalculateSoundPosition_near) { + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->position.tile = { 50, 50 }; int plVolume = 0; int plPan = 0; @@ -27,6 +31,8 @@ TEST(Effects, CalculateSoundPosition_near) TEST(Effects, CalculateSoundPosition_out_of_range) { + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->position.tile = { 12, 12 }; int plVolume = 1234; int plPan = 0; @@ -37,6 +43,8 @@ TEST(Effects, CalculateSoundPosition_out_of_range) TEST(Effects, CalculateSoundPosition_extreme_right) { + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->position.tile = { 50, 50 }; int plVolume = 0; int plPan = 0; @@ -47,6 +55,8 @@ TEST(Effects, CalculateSoundPosition_extreme_right) TEST(Effects, CalculateSoundPosition_extreme_left) { + Players.resize(1); + MyPlayer = &Players[0]; MyPlayer->position.tile = { 50, 50 }; int plVolume = 0; int plPan = 0; diff --git a/test/inv_test.cpp b/test/inv_test.cpp index b0f718b91..31bb3d295 100644 --- a/test/inv_test.cpp +++ b/test/inv_test.cpp @@ -4,7 +4,17 @@ #include "inv.h" #include "player.h" -using namespace devilution; +namespace devilution { +namespace { + +class InvTest : public ::testing::Test { +public: + void SetUp() override + { + Players.resize(1); + MyPlayer = &Players[0]; + } +}; /* Set up a given item as a spell scroll, allowing for its usage. */ void set_up_scroll(Item &item, spell_id spell) @@ -28,7 +38,7 @@ void clear_inventory() } // Test that the scroll is used in the inventory in correct conditions -TEST(Inv, UseScroll_from_inventory) +TEST_F(InvTest, UseScroll_from_inventory) { set_up_scroll(MyPlayer->InvList[2], SPL_FIREBOLT); MyPlayer->_pNumInv = 5; @@ -36,14 +46,14 @@ TEST(Inv, UseScroll_from_inventory) } // Test that the scroll is used in the belt in correct conditions -TEST(Inv, UseScroll_from_belt) +TEST_F(InvTest, UseScroll_from_belt) { set_up_scroll(MyPlayer->SpdList[2], SPL_FIREBOLT); EXPECT_TRUE(UseScroll(MyPlayer->_pRSpell)); } // Test that the scroll is not used in the inventory for each invalid condition -TEST(Inv, UseScroll_from_inventory_invalid_conditions) +TEST_F(InvTest, UseScroll_from_inventory_invalid_conditions) { // Empty the belt to prevent using a scroll from the belt for (int i = 0; i < MAXBELTITEMS; i++) { @@ -72,7 +82,7 @@ TEST(Inv, UseScroll_from_inventory_invalid_conditions) } // Test that the scroll is not used in the belt for each invalid condition -TEST(Inv, UseScroll_from_belt_invalid_conditions) +TEST_F(InvTest, UseScroll_from_belt_invalid_conditions) { // Disable the inventory to prevent using a scroll from the inventory MyPlayer->_pNumInv = 0; @@ -99,7 +109,7 @@ TEST(Inv, UseScroll_from_belt_invalid_conditions) } // Test gold calculation -TEST(Inv, CalculateGold) +TEST_F(InvTest, CalculateGold) { MyPlayer->_pNumInv = 10; // Set up 4 slots of gold in the inventory @@ -117,7 +127,7 @@ TEST(Inv, CalculateGold) } // Test automatic gold placing -TEST(Inv, GoldAutoPlace) +TEST_F(InvTest, GoldAutoPlace) { // Empty the inventory clear_inventory(); @@ -139,7 +149,7 @@ TEST(Inv, GoldAutoPlace) } // Test removing an item from inventory with no other items. -TEST(Inv, RemoveInvItem) +TEST_F(InvTest, RemoveInvItem) { clear_inventory(); // Put a two-slot misc item into the inventory: @@ -156,7 +166,7 @@ TEST(Inv, RemoveInvItem) } // Test removing an item from inventory with other items in it. -TEST(Inv, RemoveInvItem_other_item) +TEST_F(InvTest, RemoveInvItem_other_item) { clear_inventory(); // Put a two-slot misc item and a ring into the inventory: @@ -178,7 +188,7 @@ TEST(Inv, RemoveInvItem_other_item) } // Test removing an item from the belt -TEST(Inv, RemoveSpdBarItem) +TEST_F(InvTest, RemoveSpdBarItem) { // Clear the belt for (int i = 0; i < MAXBELTITEMS; i++) { @@ -192,7 +202,7 @@ TEST(Inv, RemoveSpdBarItem) } // Test removing a scroll from the inventory -TEST(Inv, RemoveCurrentSpellScroll_inventory) +TEST_F(InvTest, RemoveCurrentSpellScroll_inventory) { clear_inventory(); @@ -209,7 +219,7 @@ TEST(Inv, RemoveCurrentSpellScroll_inventory) } // Test removing a scroll from the belt -TEST(Inv, RemoveCurrentSpellScroll_belt) +TEST_F(InvTest, RemoveCurrentSpellScroll_belt) { // Clear the belt for (int i = 0; i < MAXBELTITEMS; i++) { @@ -225,7 +235,7 @@ TEST(Inv, RemoveCurrentSpellScroll_belt) EXPECT_TRUE(MyPlayer->SpdList[3].isEmpty()); } -TEST(Inv, ItemSize) +TEST_F(InvTest, ItemSize) { Item testItem {}; @@ -246,3 +256,6 @@ TEST(Inv, ItemSize) InitializeItem(testItem, IDI_GOLD); EXPECT_EQ(GetInventorySize(testItem), Size(1, 1)); } + +} // namespace +} // namespace devilution diff --git a/test/pack_test.cpp b/test/pack_test.cpp index 72834d4f5..4c5ba7ca0 100644 --- a/test/pack_test.cpp +++ b/test/pack_test.cpp @@ -332,7 +332,16 @@ const TestItemStruct DiabloItems[] = { // clang-format on }; -TEST(PackTest, UnPackItem_diablo) +class PackTest : public ::testing::Test { +public: + void SetUp() override + { + Players.resize(1); + MyPlayer = &Players[0]; + } +}; + +TEST_F(PackTest, UnPackItem_diablo) { Item id; ItemPack is; @@ -353,7 +362,7 @@ TEST(PackTest, UnPackItem_diablo) } } -TEST(PackTest, UnPackItem_diablo_unique_bug) +TEST_F(PackTest, UnPackItem_diablo_unique_bug) { ItemPack pkItemBug = { 6, 911, 14, 5, 60, 60, 0, 0, 0, 0 }; // Veil of Steel - with morph bug ItemPack pkItem = { 6, 655, 14, 5, 60, 60, 0, 0, 0, 0 }; // Veil of Steel - fixed @@ -403,7 +412,7 @@ const TestItemStruct SpawnItems[] = { // clang-format on }; -TEST(PackTest, UnPackItem_spawn) +TEST_F(PackTest, UnPackItem_spawn) { Item id; ItemPack is; @@ -447,7 +456,7 @@ const TestItemStruct DiabloMPItems[] = { // clang-format on }; -TEST(PackTest, UnPackItem_diablo_multiplayer) +TEST_F(PackTest, UnPackItem_diablo_multiplayer) { Item id; ItemPack is; @@ -660,7 +669,7 @@ const TestItemStruct HellfireItems[] = { // clang-format on }; -TEST(PackTest, UnPackItem_hellfire) +TEST_F(PackTest, UnPackItem_hellfire) { Item id; ItemPack is; @@ -682,7 +691,7 @@ TEST(PackTest, UnPackItem_hellfire) } } -TEST(PackTest, UnPackItem_diablo_strip_hellfire_items) +TEST_F(PackTest, UnPackItem_diablo_strip_hellfire_items) { ItemPack is = { 1478792102, 259, 92, 0, 0, 0, 0, 0, 0, 0 }; // Scroll of Search Item id; @@ -696,7 +705,7 @@ TEST(PackTest, UnPackItem_diablo_strip_hellfire_items) ASSERT_EQ(id._itype, ItemType::None); } -TEST(PackTest, UnPackItem_empty) +TEST_F(PackTest, UnPackItem_empty) { ItemPack is = { 0, 0, 0xFFFF, 0, 0, 0, 0, 0, 0, 0 }; Item id; @@ -706,7 +715,7 @@ TEST(PackTest, UnPackItem_empty) ASSERT_EQ(id._itype, ItemType::None); } -TEST(PackTest, PackItem_empty) +TEST_F(PackTest, PackItem_empty) { ItemPack is; Item id = {}; @@ -737,25 +746,25 @@ static void compareGold(const ItemPack &is, int iCurs) ComparePackedItems(is, is2); } -TEST(PackTest, UnPackItem_gold_small) +TEST_F(PackTest, UnPackItem_gold_small) { ItemPack is = { 0, 0, IDI_GOLD, 0, 0, 0, 0, 0, 1000, 0 }; compareGold(is, ICURS_GOLD_SMALL); } -TEST(PackTest, UnPackItem_gold_medium) +TEST_F(PackTest, UnPackItem_gold_medium) { ItemPack is = { 0, 0, IDI_GOLD, 0, 0, 0, 0, 0, 1001, 0 }; compareGold(is, ICURS_GOLD_MEDIUM); } -TEST(PackTest, UnPackItem_gold_large) +TEST_F(PackTest, UnPackItem_gold_large) { ItemPack is = { 0, 0, IDI_GOLD, 0, 0, 0, 0, 0, 2500, 0 }; compareGold(is, ICURS_GOLD_LARGE); } -TEST(PackTest, UnPackItem_ear) +TEST_F(PackTest, UnPackItem_ear) { ItemPack is = { 1633955154, 17509, 23, 111, 103, 117, 101, 68, 19843, 0 }; Item id; diff --git a/test/path_test.cpp b/test/path_test.cpp index 43eb37f7e..3ddffdd4d 100644 --- a/test/path_test.cpp +++ b/test/path_test.cpp @@ -110,7 +110,7 @@ void CheckPath(Point startPosition, Point destinationPosition, std::vector