diff options
author | Fedja Beader <fedja@protonmail.ch> | 2025-03-21 20:16:35 +0100 |
---|---|---|
committer | Fedja Beader <fedja@protonmail.ch> | 2025-03-21 22:20:31 +0100 |
commit | 1907842b959924a79ddb7246a4ebd863e952c17c (patch) | |
tree | 4b8a0a7af6ba25181b2b0f89d5829db1f67ad679 | |
parent | 77b3e4e77d2044b99be790d246ae99b524843faa (diff) | |
download | manaplus-1907842b959924a79ddb7246a4ebd863e952c17c.tar.gz manaplus-1907842b959924a79ddb7246a4ebd863e952c17c.tar.bz2 manaplus-1907842b959924a79ddb7246a4ebd863e952c17c.tar.xz manaplus-1907842b959924a79ddb7246a4ebd863e952c17c.zip |
Clean-up ActorManager::findNearestLivingBeing
This resolves at least three instances where squared distance was
compared with non-squared one, two cases of maxDist and no squaring of
getDistance result.
-rw-r--r-- | src/actormanager.cpp | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/src/actormanager.cpp b/src/actormanager.cpp index 80dc6e25d..ffb856b21 100644 --- a/src/actormanager.cpp +++ b/src/actormanager.cpp @@ -1130,7 +1130,7 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, } Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, - int maxDist, + const int maxDist, const ActorTypeT &type, const int x, const int y, const Being *const excluded, @@ -1155,7 +1155,7 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, specialDistance = true; } - maxDist = maxDist * maxDist; + const int maxDistSq = maxDist * maxDist; const bool cycleSelect = allowSort == AllowSort_true && ((mCyclePlayers && type == ActorType::Player) @@ -1307,14 +1307,17 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, if (filtered) { - if (ignoreAttackMobs.find(being->getName()) - != ignoreAttackMobs.end()) + // Skip ignored + if (ignoreAttackMobs.find(being->getName()) != + ignoreAttackMobs.end()) { continue; } - if (ignoreDefault && attackMobs.find(being->getName()) - == attackMobs.end() && priorityMobs.find(being->getName()) - == priorityMobs.end()) + // if (default) is in ignore list, then only attack + // those in priority or general attack list. + if (ignoreDefault + && attackMobs.find(being->getName()) == attackMobs.end() + && priorityMobs.find(being->getName()) == priorityMobs.end()) { continue; } @@ -1326,9 +1329,12 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, continue; } - const bool valid = validateBeing(aroundBeing, being, - type, excluded, 50); + if (!validateBeing(aroundBeing, being, type, excluded, 50)) + continue; + int d = being->getDistance(); + d = d*d; + if (being->getType() != ActorType::Monster || !mTargetOnlyReachable) { // if distance not calculated, use old distance @@ -1337,9 +1343,6 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, d = dx*dx + dy*dy; } - if (!valid) - continue; - if (specialDistance && being->getDistance() <= 2 && being->getType() == type) { @@ -1394,7 +1397,7 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, } } } - return (maxDist >= dist) ? closestBeing : nullptr; + return (maxDistSq >= dist) ? closestBeing : nullptr; } bool ActorManager::validateBeing(const Being *const aroundBeing, |