summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFedja Beader <fedja@protonmail.ch>2025-03-13 00:05:20 +0000
committerFedja Beader <fedja@protonmail.ch>2025-03-13 00:05:20 +0000
commit77b3e4e77d2044b99be790d246ae99b524843faa (patch)
tree0d64b66714eb8815fabd82dd6197d28f0ff43071
parent7bb3daddbfca03366dd51a03d385fdb4f5ec85b7 (diff)
downloadmv-77b3e4e77d2044b99be790d246ae99b524843faa.tar.gz
mv-77b3e4e77d2044b99be790d246ae99b524843faa.tar.bz2
mv-77b3e4e77d2044b99be790d246ae99b524843faa.tar.xz
mv-77b3e4e77d2044b99be790d246ae99b524843faa.zip
Clean-up and optimise ActorManager::findMostDamagedPlayer
Cheaper checks reordered to be checked first, removed squared root. **** mana/plus!144
-rw-r--r--src/actormanager.cpp36
1 files changed, 21 insertions, 15 deletions
diff --git a/src/actormanager.cpp b/src/actormanager.cpp
index 9cedb37b7..80dc6e25d 100644
--- a/src/actormanager.cpp
+++ b/src/actormanager.cpp
@@ -1515,8 +1515,12 @@ Being* ActorManager::findMostDamagedPlayer(const int maxTileDist) const
if (localPlayer == nullptr)
return nullptr;
+ const int maxTileDistSq = maxTileDist * maxTileDist;
int maxDamageTaken = 0;
- Being *target = nullptr;
+ Being *maxDamagePlayer = nullptr;
+
+ const int my_x = localPlayer->getTileX();
+ const int my_y = localPlayer->getTileY();
for_actors
{
@@ -1525,29 +1529,31 @@ Being* ActorManager::findMostDamagedPlayer(const int maxTileDist) const
Being *const being = static_cast<Being*>(*it);
- if ((being == nullptr) || !being->isAlive() || // don't heal dead
- playerRelations.getRelation(being->getName()) ==
- Relation::ENEMY2 || // don't heal enemy
- localPlayer == being) // don't heal self
+ if ((being == nullptr)
+ || (localPlayer == being) // don't heal self
+ || (!being->isAlive()) ) // don't heal the dead
{
continue;
}
- const int dx = being->getTileX() - localPlayer->getTileX();
- const int dy = being->getTileY() - localPlayer->getTileY();
- const int distance = fastSqrtInt(dx * dx + dy * dy);
+ if (being->getDamageTaken() <= maxDamageTaken)
+ continue;
+
+ const int dx = being->getTileX() - my_x;
+ const int dy = being->getTileY() - my_y;
+ const int distSq = dx*dx + dy*dy;
- if (distance > maxTileDist)
+ if (distSq > maxTileDistSq)
continue;
- if (being->getDamageTaken() > maxDamageTaken)
- {
- maxDamageTaken = being->getDamageTaken();
- target = being;
- }
+ if (playerRelations.getRelation(being->getName()) == Relation::ENEMY2)
+ continue; // don't heal enemy
+
+ maxDamageTaken = being->getDamageTaken();
+ maxDamagePlayer = being;
}
- return target;
+ return maxDamagePlayer;
}
#ifdef TMWA_SUPPORT