diff --git a/Source/control.cpp b/Source/control.cpp index 6a3569b38..5e0179ec3 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -1229,9 +1229,9 @@ void DrawInfoBox(const Surface &out) InfoString = std::string_view(Towners[pcursmonst].name); } } - if (pcursplr != -1) { + if (PlayerUnderCursor != nullptr) { InfoColor = UiFlags::ColorWhitegold; - auto &target = Players[pcursplr]; + auto &target = *PlayerUnderCursor; InfoString = std::string_view(target._pName); AddPanelString(fmt::format(fmt::runtime(_("{:s}, Level: {:d}")), target.getClassName(), target.getCharacterLevel())); AddPanelString(fmt::format(fmt::runtime(_("Hit Points {:d} of {:d}")), target._pHitPoints >> 6, target._pMaxHP >> 6)); diff --git a/Source/controls/plrctrls.cpp b/Source/controls/plrctrls.cpp index 482bae81e..8f045cf55 100644 --- a/Source/controls/plrctrls.cpp +++ b/Source/controls/plrctrls.cpp @@ -382,8 +382,7 @@ void CheckPlayerNearby() if (myPlayer.friendlyMode && spl != SpellID::Resurrect && spl != SpellID::HealOther) return; - for (size_t i = 0; i < Players.size(); i++) { - const Player &player = Players[i]; + for (const Player &player : Players) { if (&player == MyPlayer) continue; const int mx = player.position.future.x; @@ -401,15 +400,15 @@ void CheckPlayerNearby() continue; } - if (pcursplr != -1 && distance < newDdistance) + if (PlayerUnderCursor != nullptr && distance < newDdistance) continue; const int newRotations = GetRotaryDistance(player.position.future); - if (pcursplr != -1 && distance == newDdistance && rotations < newRotations) + if (PlayerUnderCursor != nullptr && distance == newDdistance && rotations < newRotations) continue; distance = newDdistance; rotations = newRotations; - pcursplr = i; + PlayerUnderCursor = &player; } } @@ -475,7 +474,7 @@ void FindTrigger() } } - if (pcursmonst != -1 || pcursplr != -1 || cursPosition.x == -1 || cursPosition.y == -1) + if (pcursmonst != -1 || PlayerUnderCursor != nullptr || cursPosition.x == -1 || cursPosition.y == -1) return; // Prefer monster/player info text CheckTrigForce(); @@ -534,8 +533,8 @@ void Interact() return; } - if (leveltype != DTYPE_TOWN && pcursplr != -1 && !myPlayer.friendlyMode) { - NetSendCmdParam1(true, myPlayer.UsesRangedWeapon() ? CMD_RATTACKPID : CMD_ATTACKPID, pcursplr); + if (leveltype != DTYPE_TOWN && PlayerUnderCursor != nullptr && !myPlayer.friendlyMode) { + NetSendCmdParam1(true, myPlayer.UsesRangedWeapon() ? CMD_RATTACKPID : CMD_ATTACKPID, PlayerUnderCursor->getId()); LastMouseButtonAction = MouseActionType::AttackPlayerTarget; return; } @@ -1742,14 +1741,14 @@ void plrctrls_after_check_curs_move() if (ControllerActionHeld != GameActionType_NONE && IsNoneOf(LastMouseButtonAction, MouseActionType::None, MouseActionType::Attack, MouseActionType::Spell)) { InvalidateTargets(); - if (pcursmonst == -1 && ObjectUnderCursor == nullptr && pcursitem == -1 && pcursinvitem == -1 && pcursstashitem == StashStruct::EmptyCell && pcursplr == -1) { + if (pcursmonst == -1 && ObjectUnderCursor == nullptr && pcursitem == -1 && pcursinvitem == -1 && pcursstashitem == StashStruct::EmptyCell && PlayerUnderCursor == nullptr) { FindTrigger(); } return; } // Clear focuse set by cursor - pcursplr = -1; + PlayerUnderCursor = nullptr; pcursmonst = -1; pcursitem = -1; ObjectUnderCursor = nullptr; @@ -1914,7 +1913,7 @@ bool SpellHasActorTarget() cursPosition = Monsters[pcursmonst].position.tile; } - return pcursplr != -1 || pcursmonst != -1; + return PlayerUnderCursor != nullptr || pcursmonst != -1; } void UpdateSpellTarget(SpellID spell) @@ -1922,7 +1921,7 @@ void UpdateSpellTarget(SpellID spell) if (SpellHasActorTarget()) return; - pcursplr = -1; + PlayerUnderCursor = nullptr; pcursmonst = -1; Player &myPlayer = *MyPlayer; @@ -2002,7 +2001,7 @@ void PerformSpellAction() const Player &myPlayer = *MyPlayer; SpellID spl = myPlayer._pRSpell; - if ((pcursplr == -1 && (spl == SpellID::Resurrect || spl == SpellID::HealOther)) + if ((PlayerUnderCursor == nullptr && (spl == SpellID::Resurrect || spl == SpellID::HealOther)) || (ObjectUnderCursor == nullptr && spl == SpellID::TrapDisarm)) { myPlayer.Say(HeroSpeech::ICantCastThatHere); return; @@ -2010,7 +2009,7 @@ void PerformSpellAction() UpdateSpellTarget(myPlayer._pRSpell); CheckPlrSpell(false); - if (pcursplr != -1) + if (PlayerUnderCursor != nullptr) LastMouseButtonAction = MouseActionType::SpellPlayerTarget; else if (pcursmonst != -1) LastMouseButtonAction = MouseActionType::SpellMonsterTarget; diff --git a/Source/cursor.cpp b/Source/cursor.cpp index 262db1135..31e39cf30 100644 --- a/Source/cursor.cpp +++ b/Source/cursor.cpp @@ -183,7 +183,7 @@ bool TrySelectPlayer(bool flipflag, int mx, int my) Player &player = Players[playerId]; if (&player != MyPlayer && player._pHitPoints != 0) { cursPosition = Point { mx, my } + Displacement { 1, 0 }; - pcursplr = static_cast(playerId); + PlayerUnderCursor = &player; } } if (flipflag && my + 1 < MAXDUNY && dPlayer[mx][my + 1] != 0) { @@ -191,21 +191,22 @@ bool TrySelectPlayer(bool flipflag, int mx, int my) Player &player = Players[playerId]; if (&player != MyPlayer && player._pHitPoints != 0) { cursPosition = Point { mx, my } + Displacement { 0, 1 }; - pcursplr = static_cast(playerId); + PlayerUnderCursor = &player; } } if (dPlayer[mx][my] != 0) { const uint8_t playerId = std::abs(dPlayer[mx][my]) - 1; - if (playerId != MyPlayerId) { + Player &player = Players[playerId]; + if (&player != MyPlayer) { cursPosition = { mx, my }; - pcursplr = static_cast(playerId); + PlayerUnderCursor = &player; } } if (TileContainsDeadPlayer({ mx, my })) { for (const Player &player : Players) { if (player.position.tile == Point { mx, my } && &player != MyPlayer) { cursPosition = { mx, my }; - pcursplr = static_cast(player.getId()); + PlayerUnderCursor = &player; } } } @@ -216,7 +217,7 @@ bool TrySelectPlayer(bool flipflag, int mx, int my) for (const Player &player : Players) { if (player.position.tile.x == mx + xx && player.position.tile.y == my + yy && &player != MyPlayer) { cursPosition = Point { mx, my } + Displacement { xx, yy }; - pcursplr = static_cast(player.getId()); + PlayerUnderCursor = &player; } } } @@ -228,11 +229,11 @@ bool TrySelectPlayer(bool flipflag, int mx, int my) const Player &player = Players[playerId]; if (&player != MyPlayer && player._pHitPoints != 0) { cursPosition = Point { mx, my } + Displacement { 1, 1 }; - pcursplr = static_cast(playerId); + PlayerUnderCursor = &player; } } - return pcursplr != -1; + return PlayerUnderCursor != nullptr; } bool TrySelectObject(bool flipflag, Point tile) @@ -382,7 +383,7 @@ bool TrySelectPixelBased(Point tile) Displacement renderingOffset = player.getRenderingOffset(sprite); if (checkSprite(adjacentTile, sprite, renderingOffset)) { cursPosition = adjacentTile; - pcursplr = playerId; + PlayerUnderCursor = &player; return true; } } @@ -394,7 +395,7 @@ bool TrySelectPixelBased(Point tile) Displacement renderingOffset = player.getRenderingOffset(sprite); if (checkSprite(adjacentTile, sprite, renderingOffset)) { cursPosition = adjacentTile; - pcursplr = static_cast(player.getId()); + PlayerUnderCursor = &player; return true; } } @@ -443,7 +444,7 @@ int8_t pcursitem; /** Current highlighted object */ Object *ObjectUnderCursor; /** Current highlighted player */ -int8_t pcursplr; +const Player *PlayerUnderCursor; /** Current highlighted tile position */ Point cursPosition; /** Previously highlighted monster */ @@ -627,7 +628,7 @@ void InitLevelCursor() ObjectUnderCursor = nullptr; pcursitem = -1; pcursstashitem = StashStruct::EmptyCell; - pcursplr = -1; + PlayerUnderCursor = nullptr; ClearCursor(); } @@ -762,7 +763,7 @@ void CheckCursMove() if ((sgbMouseDown != CLICK_NONE || ControllerActionHeld != GameActionType_NONE) && IsNoneOf(LastMouseButtonAction, MouseActionType::None, MouseActionType::Attack, MouseActionType::Spell)) { InvalidateTargets(); - if (pcursmonst == -1 && ObjectUnderCursor == nullptr && pcursitem == -1 && pcursinvitem == -1 && pcursstashitem == StashStruct::EmptyCell && pcursplr == -1) { + if (pcursmonst == -1 && ObjectUnderCursor == nullptr && pcursitem == -1 && pcursinvitem == -1 && pcursstashitem == StashStruct::EmptyCell && PlayerUnderCursor == nullptr) { cursPosition = { mx, my }; CheckTrigForce(); CheckTown(); @@ -782,7 +783,7 @@ void CheckCursMove() } pcursinvitem = -1; pcursstashitem = StashStruct::EmptyCell; - pcursplr = -1; + PlayerUnderCursor = nullptr; ShowUniqueItemInfoBox = false; panelflag = false; trigflag = false; diff --git a/Source/cursor.h b/Source/cursor.h index 10a4bef60..57e0c9f9a 100644 --- a/Source/cursor.h +++ b/Source/cursor.h @@ -39,7 +39,8 @@ extern int8_t pcursitem; struct Object; // Defined in objects.h extern Object *ObjectUnderCursor; -extern int8_t pcursplr; +struct Player; // Defined in player.h +extern const Player *PlayerUnderCursor; extern Point cursPosition; extern DVL_API_FOR_TEST int pcurs; diff --git a/Source/diablo.cpp b/Source/diablo.cpp index f1bdca0f5..468a32981 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -245,7 +245,7 @@ void LeftMouseCmd(bool bShift) NetSendCmdLocParam1(true, invflag ? CMD_GOTOGETITEM : CMD_GOTOAGETITEM, cursPosition, pcursitem); if (pcursmonst != -1) NetSendCmdLocParam1(true, CMD_TALKXY, cursPosition, pcursmonst); - if (pcursitem == -1 && pcursmonst == -1 && pcursplr == -1) { + if (pcursitem == -1 && pcursmonst == -1 && PlayerUnderCursor == nullptr) { LastMouseButtonAction = MouseActionType::Walk; NetSendCmdLoc(MyPlayerId, true, CMD_WALKXY, cursPosition); } @@ -270,9 +270,9 @@ void LeftMouseCmd(bool bShift) LastMouseButtonAction = MouseActionType::AttackMonsterTarget; NetSendCmdParam1(true, CMD_RATTACKID, pcursmonst); } - } else if (pcursplr != -1 && !myPlayer.friendlyMode) { + } else if (PlayerUnderCursor != nullptr && !myPlayer.friendlyMode) { LastMouseButtonAction = MouseActionType::AttackPlayerTarget; - NetSendCmdParam1(true, CMD_RATTACKPID, pcursplr); + NetSendCmdParam1(true, CMD_RATTACKPID, PlayerUnderCursor->getId()); } } else { if (bShift) { @@ -290,12 +290,12 @@ void LeftMouseCmd(bool bShift) } else if (pcursmonst != -1) { LastMouseButtonAction = MouseActionType::AttackMonsterTarget; NetSendCmdParam1(true, CMD_ATTACKID, pcursmonst); - } else if (pcursplr != -1 && !myPlayer.friendlyMode) { + } else if (PlayerUnderCursor != nullptr && !myPlayer.friendlyMode) { LastMouseButtonAction = MouseActionType::AttackPlayerTarget; - NetSendCmdParam1(true, CMD_ATTACKPID, pcursplr); + NetSendCmdParam1(true, CMD_ATTACKPID, PlayerUnderCursor->getId()); } } - if (!bShift && pcursitem == -1 && ObjectUnderCursor == nullptr && pcursmonst == -1 && pcursplr == -1) { + if (!bShift && pcursitem == -1 && ObjectUnderCursor == nullptr && pcursmonst == -1 && PlayerUnderCursor == nullptr) { LastMouseButtonAction = MouseActionType::Walk; NetSendCmdLoc(MyPlayerId, true, CMD_WALKXY, cursPosition); } @@ -2490,8 +2490,8 @@ int DiabloMain(int argc, char **argv) bool TryIconCurs() { if (pcurs == CURSOR_RESURRECT) { - if (pcursplr != -1) { - NetSendCmdParam1(true, CMD_RESURRECT, pcursplr); + if (PlayerUnderCursor != nullptr) { + NetSendCmdParam1(true, CMD_RESURRECT, PlayerUnderCursor->getId()); NewCursor(CURSOR_HAND); return true; } @@ -2500,8 +2500,8 @@ bool TryIconCurs() } if (pcurs == CURSOR_HEALOTHER) { - if (pcursplr != -1) { - NetSendCmdParam1(true, CMD_HEALOTHER, pcursplr); + if (PlayerUnderCursor != nullptr) { + NetSendCmdParam1(true, CMD_HEALOTHER, PlayerUnderCursor->getId()); NewCursor(CURSOR_HAND); return true; } @@ -2572,8 +2572,8 @@ bool TryIconCurs() NetSendCmdLocParam5(true, CMD_SPELLXYD, cursPosition, static_cast(spellID), static_cast(spellType), static_cast(sd), spellLevel, spellFrom); } else if (pcursmonst != -1) { NetSendCmdParam5(true, CMD_SPELLID, pcursmonst, static_cast(spellID), static_cast(spellType), spellLevel, spellFrom); - } else if (pcursplr != -1 && !myPlayer.friendlyMode) { - NetSendCmdParam5(true, CMD_SPELLPID, pcursplr, static_cast(spellID), static_cast(spellType), spellLevel, spellFrom); + } else if (PlayerUnderCursor != nullptr && !myPlayer.friendlyMode) { + NetSendCmdParam5(true, CMD_SPELLPID, PlayerUnderCursor->getId(), static_cast(spellID), static_cast(spellType), spellLevel, spellFrom); } else { NetSendCmdLocParam4(true, CMD_SPELLXY, cursPosition, static_cast(spellID), static_cast(spellType), spellLevel, spellFrom); } diff --git a/Source/engine/render/scrollrt.cpp b/Source/engine/render/scrollrt.cpp index 703ba0f5c..b474a9230 100644 --- a/Source/engine/render/scrollrt.cpp +++ b/Source/engine/render/scrollrt.cpp @@ -384,7 +384,7 @@ void DrawPlayer(const Surface &out, const Player &player, Point tilePosition, Po const ClxSprite sprite = player.currentSprite(); Point spriteBufferPosition = targetBufferPosition + player.getRenderingOffset(sprite); - if (static_cast(pcursplr) < Players.size() && &player == &Players[pcursplr]) + if (&player == PlayerUnderCursor) ClxDrawOutlineSkipColorZero(out, 165, spriteBufferPosition, sprite); if (&player == MyPlayer && IsNoneOf(leveltype, DTYPE_NEST, DTYPE_CRYPT)) { diff --git a/Source/player.cpp b/Source/player.cpp index 24497170e..c404ac42c 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -3206,9 +3206,9 @@ void CheckPlrSpell(bool isShiftHeld, SpellID spellID, SpellType spellType) } else if (pcursmonst != -1 && !isShiftHeld) { LastMouseButtonAction = MouseActionType::SpellMonsterTarget; NetSendCmdParam5(true, CMD_SPELLID, pcursmonst, static_cast(spellID), static_cast(spellType), spellLevel, spellFrom); - } else if (pcursplr != -1 && !isShiftHeld && !myPlayer.friendlyMode) { + } else if (PlayerUnderCursor != nullptr && !isShiftHeld && !myPlayer.friendlyMode) { LastMouseButtonAction = MouseActionType::SpellPlayerTarget; - NetSendCmdParam5(true, CMD_SPELLPID, pcursplr, static_cast(spellID), static_cast(spellType), spellLevel, spellFrom); + NetSendCmdParam5(true, CMD_SPELLPID, PlayerUnderCursor->getId(), static_cast(spellID), static_cast(spellType), spellLevel, spellFrom); } else { LastMouseButtonAction = MouseActionType::Spell; NetSendCmdLocParam4(true, CMD_SPELLXY, cursPosition, static_cast(spellID), static_cast(spellType), spellLevel, spellFrom); diff --git a/Source/track.cpp b/Source/track.cpp index c7198ce9b..f139d577f 100644 --- a/Source/track.cpp +++ b/Source/track.cpp @@ -49,12 +49,12 @@ void InvalidateTargets() if (ObjectUnderCursor != nullptr && ObjectUnderCursor->_oSelFlag < 1) ObjectUnderCursor = nullptr; - if (pcursplr != -1) { - Player &targetPlayer = Players[pcursplr]; + if (PlayerUnderCursor != nullptr) { + const Player &targetPlayer = *PlayerUnderCursor; if (targetPlayer._pmode == PM_DEATH || targetPlayer._pmode == PM_QUIT || !targetPlayer.plractive || !targetPlayer.isOnActiveLevel() || targetPlayer._pHitPoints >> 6 <= 0 || !IsTileLit(targetPlayer.position.tile)) - pcursplr = -1; + PlayerUnderCursor = nullptr; } } @@ -91,8 +91,8 @@ void RepeatMouseAction() NetSendCmdParam1(true, rangedAttack ? CMD_RATTACKID : CMD_ATTACKID, pcursmonst); break; case MouseActionType::AttackPlayerTarget: - if (pcursplr != -1 && !myPlayer.friendlyMode) - NetSendCmdParam1(true, rangedAttack ? CMD_RATTACKPID : CMD_ATTACKPID, pcursplr); + if (PlayerUnderCursor != nullptr && !myPlayer.friendlyMode) + NetSendCmdParam1(true, rangedAttack ? CMD_RATTACKPID : CMD_ATTACKPID, PlayerUnderCursor->getId()); break; case MouseActionType::Spell: if (ControlMode != ControlTypes::KeyboardAndMouse) { @@ -105,7 +105,7 @@ void RepeatMouseAction() CheckPlrSpell(false); break; case MouseActionType::SpellPlayerTarget: - if (pcursplr != -1 && !myPlayer.friendlyMode) + if (PlayerUnderCursor != nullptr && !myPlayer.friendlyMode) CheckPlrSpell(false); break; case MouseActionType::OperateObject: