From 1bdee4523b848cf0da0dddb64879e70605c23ac5 Mon Sep 17 00:00:00 2001 From: Cesar Canassa Date: Mon, 1 Aug 2022 01:20:47 +0200 Subject: [PATCH] :sparkles: Adds ignoreMovingMonsters to MonsterAtPosition (#5118) --- Source/monster.cpp | 28 ++++++++++++++-------------- Source/monster.h | 2 +- Source/player.cpp | 6 +++--- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Source/monster.cpp b/Source/monster.cpp index 321407999..5b55f80a4 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -3721,7 +3721,7 @@ void M_StartStand(Monster &monster, Direction md) void M_ClearSquares(const Monster &monster) { for (Point searchTile : PointsInRectangleRange { Rectangle { monster.position.old, 1 } }) { - if (MonsterAtPosition(searchTile) == &monster) + if (FindMonsterAtPosition(searchTile) == &monster) dMonster[searchTile.x][searchTile.y] = 0; } } @@ -4141,12 +4141,11 @@ bool DirOK(const Monster &monster, Direction mdir) for (int y = futurePosition.y - 3; y <= futurePosition.y + 3; y++) { if (!InDungeonBounds({ x, y })) continue; - int mi = dMonster[x][y]; - if (mi <= 0) + Monster *minion = FindMonsterAtPosition({ x, y }, true); + if (minion == nullptr) continue; - auto &minion = Monsters[mi - 1]; - if (minion.leaderRelation == LeaderRelation::Leashed && minion.getLeader() == &monster) { + if (minion->leaderRelation == LeaderRelation::Leashed && minion->getLeader() == &monster) { mcount++; } } @@ -4474,17 +4473,18 @@ void MissToMonst(Missile &missile, Point position) return; } - if (dMonster[oldPosition.x][oldPosition.y] <= 0) + Monster *target = FindMonsterAtPosition(oldPosition, true); + + if (target == nullptr) return; - Monster &target = *MonsterAtPosition(oldPosition); - MonsterAttackMonster(monster, target, 500, monster.minDamageSpecial, monster.maxDamageSpecial); + MonsterAttackMonster(monster, *target, 500, monster.minDamageSpecial, monster.maxDamageSpecial); if (IsAnyOf(monster.type().type, MT_NSNAKE, MT_RSNAKE, MT_BSNAKE, MT_GSNAKE)) return; Point newPosition = oldPosition + monster.direction; - if (IsTileAvailable(target, newPosition)) { + if (IsTileAvailable(*target, newPosition)) { monsterId = dMonster[oldPosition.x][oldPosition.y]; dMonster[newPosition.x][newPosition.y] = monsterId; dMonster[oldPosition.x][oldPosition.y] = 0; @@ -4494,7 +4494,7 @@ void MissToMonst(Missile &missile, Point position) } } -Monster *MonsterAtPosition(Point position) +Monster *FindMonsterAtPosition(Point position, bool ignoreMovingMonsters) { if (!InDungeonBounds(position)) { return nullptr; @@ -4502,12 +4502,12 @@ Monster *MonsterAtPosition(Point position) auto monsterId = dMonster[position.x][position.y]; - if (monsterId != 0) { - return &Monsters[abs(monsterId) - 1]; + if (monsterId == 0 || (ignoreMovingMonsters && monsterId < 0)) { + // nothing at this position, return a nullptr + return nullptr; } - // nothing at this position, return a nullptr - return nullptr; + return &Monsters[abs(monsterId) - 1]; } bool IsTileAvailable(const Monster &monster, Point position) diff --git a/Source/monster.h b/Source/monster.h index fcc523e8f..856584499 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -409,7 +409,7 @@ void PrintUniqueHistory(); void PlayEffect(Monster &monster, MonsterSound mode); void MissToMonst(Missile &missile, Point position); -Monster *MonsterAtPosition(Point position); +Monster *FindMonsterAtPosition(Point position, bool ignoreMovingMonsters = false); /** * @brief Check that the given tile is available to the monster diff --git a/Source/player.cpp b/Source/player.cpp index 9d390c547..bf920da70 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -1033,7 +1033,7 @@ bool DoAttack(Player &player) if (player.AnimInfo.currentFrame == player._pAFNum - 1) { Point position = player.position.tile + player._pdir; - Monster *monster = MonsterAtPosition(position); + Monster *monster = FindMonsterAtPosition(position); if (monster != nullptr) { if (CanTalkToMonst(*monster)) { @@ -1075,7 +1075,7 @@ bool DoAttack(Player &player) && !(player.InvBody[INVLOC_HAND_LEFT]._itype == ItemType::Shield || player.InvBody[INVLOC_HAND_RIGHT]._itype == ItemType::Shield))))) { // playing as a class/weapon with cleave position = player.position.tile + Right(player._pdir); - monster = MonsterAtPosition(position); + monster = FindMonsterAtPosition(position); if (monster != nullptr) { if (!CanTalkToMonst(*monster) && monster->position.old == position) { if (PlrHitMonst(player, *monster, true)) @@ -1083,7 +1083,7 @@ bool DoAttack(Player &player) } } position = player.position.tile + Left(player._pdir); - monster = MonsterAtPosition(position); + monster = FindMonsterAtPosition(position); if (monster != nullptr) { if (!CanTalkToMonst(*monster) && monster->position.old == position) { if (PlrHitMonst(player, *monster, true))