From 2da2ea7b0388c450a87f022e3fdf7b03b1579fd4 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Wed, 29 Jun 2022 08:22:39 +1000 Subject: [PATCH] Take monster reference in M_ClearSquares --- Source/monster.cpp | 45 ++++++++++++++++++++++++++------------------- Source/monster.h | 4 +++- Source/msg.cpp | 5 +++-- Source/sync.cpp | 4 ++-- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/Source/monster.cpp b/Source/monster.cpp index f969295c1..1f8e405d4 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -693,7 +693,7 @@ void StartMonsterGotHit(int monsterId) monster.position.offset = { 0, 0 }; monster.position.tile = monster.position.old; monster.position.future = monster.position.old; - M_ClearSquares(monsterId); + M_ClearSquares(monster); dMonster[monster.position.tile.x][monster.position.tile.y] = monsterId + 1; } @@ -950,7 +950,7 @@ void DiabloDeath(Monster &diablo, bool sendmsg) monster._mVar1 = 0; monster.position.tile = monster.position.old; monster.position.future = monster.position.tile; - M_ClearSquares(k); + M_ClearSquares(monster); dMonster[monster.position.tile.x][monster.position.tile.y] = k + 1; } AddLight(diablo.position.tile, 8); @@ -1034,7 +1034,7 @@ void Teleport(int monsterId) if (!position) return; - M_ClearSquares(monsterId); + M_ClearSquares(monster); dMonster[monster.position.tile.x][monster.position.tile.y] = 0; dMonster[position->x][position->y] = monsterId + 1; monster.position.old = *position; @@ -1118,7 +1118,7 @@ void MonsterDeath(int monsterId, int pnum, Direction md, bool sendmsg) monster.position.offset = { 0, 0 }; monster.position.tile = monster.position.old; monster.position.future = monster.position.old; - M_ClearSquares(monsterId); + M_ClearSquares(monster); dMonster[monster.position.tile.x][monster.position.tile.y] = monsterId + 1; CheckQuestKill(monster, sendmsg); M_FallenFear(monster.position.tile); @@ -3880,20 +3880,11 @@ void M_StartStand(Monster &monster, Direction md) UpdateEnemy(monster); } -void M_ClearSquares(int monsterId) +void M_ClearSquares(const Monster &monster) { - auto &monster = Monsters[monsterId]; - - int mx = monster.position.old.x; - int my = monster.position.old.y; - int m1 = -(monsterId + 1); - int m2 = monsterId + 1; - - for (int y = my - 1; y <= my + 1; y++) { - for (int x = mx - 1; x <= mx + 1; x++) { - if (InDungeonBounds({ x, y }) && (dMonster[x][y] == m1 || dMonster[x][y] == m2)) - dMonster[x][y] = 0; - } + for (Point searchTile : PointsInRectangleRange { Rectangle { monster.position.old, 1 } }) { + if (MonsterAtPosition(searchTile) == &monster) + dMonster[searchTile.x][searchTile.y] = 0; } } @@ -3906,7 +3897,7 @@ void M_GetKnockback(int monsterId) return; } - M_ClearSquares(monsterId); + M_ClearSquares(monster); monster.position.old += dir; StartMonsterGotHit(monsterId); } @@ -3987,7 +3978,7 @@ void M_SyncStartKill(int monsterId, Point position, int pnum) } if (dMonster[position.x][position.y] == 0) { - M_ClearSquares(monsterId); + M_ClearSquares(monster); monster.position.tile = position; monster.position.old = position; } @@ -4704,6 +4695,22 @@ void MissToMonst(Missile &missile, Point position) } } +Monster *MonsterAtPosition(Point position) +{ + if (!InDungeonBounds(position)) { + return nullptr; + } + + auto monsterId = dMonster[position.x][position.y]; + + if (monsterId != 0) { + return &Monsters[abs(monsterId) - 1]; + } + + // nothing at this position, return a nullptr + return nullptr; +} + bool IsTileAvailable(const Monster &monster, Point position) { if (!IsTileAvailable(position)) diff --git a/Source/monster.h b/Source/monster.h index 69c36a2e5..0a148c9ff 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -305,7 +305,7 @@ int AddMonster(Point position, Direction dir, int mtype, bool inMap); void AddDoppelganger(Monster &monster); bool M_Talker(const Monster &monster); void M_StartStand(Monster &monster, Direction md); -void M_ClearSquares(int monsterId); +void M_ClearSquares(const Monster &monster); void M_GetKnockback(int monsterId); void M_StartHit(int monsterId, int dam); void M_StartHit(int monsterId, int pnum, int dam); @@ -331,6 +331,8 @@ void PrintUniqueHistory(); void PlayEffect(Monster &monster, int mode); void MissToMonst(Missile &missile, Point position); +Monster *MonsterAtPosition(Point position); + /** * @brief Check that the given tile is available to the monster */ diff --git a/Source/msg.cpp b/Source/msg.cpp index e9f77aed3..6a0609523 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -2401,8 +2401,9 @@ void DeltaLoadLevel() if (deltaLevel.monster[i].position.x == 0xFF) continue; - M_ClearSquares(i); + // Should this be Monsters[ActiveMonsters[i]]? this loop uses ActiveMonsterCount, not MAXMONSTERS auto &monster = Monsters[i]; + M_ClearSquares(monster); { const WorldTilePosition position = deltaLevel.monster[i].position; monster.position.tile = position; @@ -2414,7 +2415,7 @@ void DeltaLoadLevel() monster.mWhoHit = deltaLevel.monster[i].mWhoHit; } if (deltaLevel.monster[i]._mhitpoints == 0) { - M_ClearSquares(i); + M_ClearSquares(monster); if (monster._mAi != AI_DIABLO) { if (monster._uniqtype == 0) { AddCorpse(monster.position.tile, monster.type().corpseId, monster._mdir); diff --git a/Source/sync.cpp b/Source/sync.cpp index 7997ca6cc..10d954f9c 100644 --- a/Source/sync.cpp +++ b/Source/sync.cpp @@ -180,14 +180,14 @@ void SyncMonster(bool isOwner, const TSyncMonster &monsterSync) if (!monster.IsWalking()) { Direction md = GetDirection(monster.position.tile, position); if (DirOK(monsterId, md)) { - M_ClearSquares(monsterId); + M_ClearSquares(monster); dMonster[monster.position.tile.x][monster.position.tile.y] = monsterId + 1; M_WalkDir(monster, md); monster._msquelch = UINT8_MAX; } } } else if (dMonster[position.x][position.y] == 0) { - M_ClearSquares(monsterId); + M_ClearSquares(monster); dMonster[position.x][position.y] = monsterId + 1; monster.position.tile = position; decode_enemy(monster, enemyId);