From d9d1385f6cec7175c8268ab24fe7470902f7b15d Mon Sep 17 00:00:00 2001 From: Andrew James Date: Wed, 13 Jul 2022 20:56:52 +1000 Subject: [PATCH] Take const reference in DirOK (#4985) --- Source/monster.cpp | 40 ++++++++++++++++++++-------------------- Source/monster.h | 2 +- Source/sync.cpp | 2 +- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Source/monster.cpp b/Source/monster.cpp index 450a84d8c..d27795277 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -1831,19 +1831,19 @@ bool RandomWalk(int monsterId, Direction md) Direction mdtemp = md; Monster &monster = Monsters[monsterId]; - bool ok = DirOK(monsterId, md); + bool ok = DirOK(monster, md); if (FlipCoin()) - ok = ok || (md = Right(mdtemp), DirOK(monsterId, md)) || (md = Left(mdtemp), DirOK(monsterId, md)); + ok = ok || (md = Right(mdtemp), DirOK(monster, md)) || (md = Left(mdtemp), DirOK(monster, md)); else - ok = ok || (md = Left(mdtemp), DirOK(monsterId, md)) || (md = Right(mdtemp), DirOK(monsterId, md)); + ok = ok || (md = Left(mdtemp), DirOK(monster, md)) || (md = Right(mdtemp), DirOK(monster, md)); if (FlipCoin()) { ok = ok - || (md = Left(Left(mdtemp)), DirOK(monsterId, md)) - || (md = Right(Right(mdtemp)), DirOK(monsterId, md)); + || (md = Left(Left(mdtemp)), DirOK(monster, md)) + || (md = Right(Right(mdtemp)), DirOK(monster, md)); } else { ok = ok - || (md = Right(Right(mdtemp)), DirOK(monsterId, md)) - || (md = Left(Left(mdtemp)), DirOK(monsterId, md)); + || (md = Right(Right(mdtemp)), DirOK(monster, md)) + || (md = Left(Left(mdtemp)), DirOK(monster, md)); } if (ok) Walk(monster, md); @@ -1855,11 +1855,13 @@ bool RandomWalk2(int monsterId, Direction md) auto &monster = Monsters[monsterId]; Direction mdtemp = md; - bool ok = DirOK(monsterId, md); // Can we continue in the same direction - if (FlipCoin()) { // Randomly go left or right - ok = ok || (mdtemp = Right(md), DirOK(monsterId, Right(md))) || (mdtemp = Left(md), DirOK(monsterId, Left(md))); + bool ok = DirOK(monster, md); // Can we continue in the same direction + + // Randomly go left or right + if (FlipCoin()) { + ok = ok || (mdtemp = Right(md), DirOK(monster, Right(md))) || (mdtemp = Left(md), DirOK(monster, Left(md))); } else { - ok = ok || (mdtemp = Left(md), DirOK(monsterId, Left(md))) || (mdtemp = Right(md), DirOK(monsterId, Right(md))); + ok = ok || (mdtemp = Left(md), DirOK(monster, Left(md))) || (mdtemp = Right(md), DirOK(monster, Right(md))); } if (ok) @@ -2033,7 +2035,7 @@ void AiAvoidance(int monsterId) } monster.goal = MonsterGoal::Move; int dist = std::max(abs(mx), abs(my)); - if ((monster.goalVar1++ >= 2 * dist && DirOK(monsterId, md)) || dTransVal[monster.position.tile.x][monster.position.tile.y] != dTransVal[fx][fy]) { + if ((monster.goalVar1++ >= 2 * dist && DirOK(monster, md)) || dTransVal[monster.position.tile.x][monster.position.tile.y] != dTransVal[fx][fy]) { monster.goal = MonsterGoal::Normal; } else if (!RoundWalk(monsterId, md, &monster.goalVar2)) { AiDelay(monster, GenerateRnd(10) + 10); @@ -2172,7 +2174,7 @@ void AiRangedAvoidance(int monsterId) monster.goalVar2 = GenerateRnd(2); } monster.goal = MonsterGoal::Move; - if (monster.goalVar1++ >= 2 * dist && DirOK(monsterId, md)) { + if (monster.goalVar1++ >= 2 * dist && DirOK(monster, md)) { monster.goal = MonsterGoal::Normal; } else if (v < (500 * (monster.intelligence + 1) >> lessmissiles) && (LineClearMissile(monster.position.tile, { fx, fy }))) { @@ -2575,7 +2577,7 @@ void LeoricAi(int monsterId) monster.goalVar2 = GenerateRnd(2); } monster.goal = MonsterGoal::Move; - if ((monster.goalVar1++ >= 2 * dist && DirOK(monsterId, md)) || dTransVal[monster.position.tile.x][monster.position.tile.y] != dTransVal[fx][fy]) { + if ((monster.goalVar1++ >= 2 * dist && DirOK(monster, md)) || dTransVal[monster.position.tile.x][monster.position.tile.y] != dTransVal[fx][fy]) { monster.goal = MonsterGoal::Normal; } else if (!RoundWalk(monsterId, md, &monster.goalVar2)) { AiDelay(monster, GenerateRnd(10) + 10); @@ -2959,7 +2961,7 @@ void CounselorAi(int monsterId) } else if (monster.goal == MonsterGoal::Move) { int dist = std::max(abs(mx), abs(my)); if (dist >= 2 && monster.activeForTicks == UINT8_MAX && dTransVal[monster.position.tile.x][monster.position.tile.y] == dTransVal[fx][fy]) { - if (monster.goalVar1++ < 2 * dist || !DirOK(monsterId, md)) { + if (monster.goalVar1++ < 2 * dist || !DirOK(monster, md)) { RoundWalk(monsterId, md, &monster.goalVar2); } else { monster.goal = MonsterGoal::Normal; @@ -3064,7 +3066,7 @@ void MegaAi(int monsterId) } monster.goal = MonsterGoal::Move; monster.goalVar3 = 4; - if (monster.goalVar1++ < 2 * dist || !DirOK(monsterId, md)) { + if (monster.goalVar1++ < 2 * dist || !DirOK(monster, md)) { if (v < 5 * (monster.intelligence + 16)) RoundWalk(monsterId, md, &monster.goalVar2); } else @@ -4099,7 +4101,7 @@ void PrepDoEnding() bool Walk(Monster &monster, Direction md) { - if (!DirOK(monster.getId(), md)) { + if (!DirOK(monster, md)) { return false; } @@ -4299,10 +4301,8 @@ void FreeMonsters() } } -bool DirOK(int monsterId, Direction mdir) +bool DirOK(const Monster &monster, Direction mdir) { - assert(static_cast(monsterId) < MaxMonsters); - auto &monster = Monsters[monsterId]; Point position = monster.position.tile; Point futurePosition = position + mdir; if (!IsRelativeMoveOK(monster, position, mdir)) diff --git a/Source/monster.h b/Source/monster.h index e8cd75423..5182274bd 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -350,7 +350,7 @@ void GolumAi(int monsterId); void DeleteMonsterList(); void ProcessMonsters(); void FreeMonsters(); -bool DirOK(int monsterId, Direction mdir); +bool DirOK(const Monster &monster, Direction mdir); bool PosOkMissile(Point position); bool LineClearMissile(Point startPoint, Point endPoint); bool LineClear(const std::function &clear, Point startPoint, Point endPoint); diff --git a/Source/sync.cpp b/Source/sync.cpp index 1e581210d..37b551e29 100644 --- a/Source/sync.cpp +++ b/Source/sync.cpp @@ -179,7 +179,7 @@ void SyncMonster(bool isOwner, const TSyncMonster &monsterSync) if (monster.position.tile.WalkingDistance(position) <= 2) { if (!monster.isWalking()) { Direction md = GetDirection(monster.position.tile, position); - if (DirOK(monsterId, md)) { + if (DirOK(monster, md)) { M_ClearSquares(monster); dMonster[monster.position.tile.x][monster.position.tile.y] = monsterId + 1; Walk(monster, md);