diff options
author | Fedja Beader <fedja@protonmail.ch> | 2025-03-21 23:11:03 +0100 |
---|---|---|
committer | Fedja Beader <fedja@protonmail.ch> | 2025-03-21 23:55:14 +0100 |
commit | 93b70efd79bb92735c7e97c9389000ced374c3d3 (patch) | |
tree | da8f4cc9e4767373d81597953d1d5fa723fe89a8 | |
parent | 1907842b959924a79ddb7246a4ebd863e952c17c (diff) | |
download | manaplus-93b70efd79bb92735c7e97c9389000ced374c3d3.tar.gz manaplus-93b70efd79bb92735c7e97c9389000ced374c3d3.tar.bz2 manaplus-93b70efd79bb92735c7e97c9389000ced374c3d3.tar.xz manaplus-93b70efd79bb92735c7e97c9389000ced374c3d3.zip |
Cleanup findNearestLivingBeing: only update closestBeing if it satisfies
distance criteria.
This should also resolve a theoretical bug if there is a non-ignored
being within range, but something out of range has priority
Cleanup addednum: rename d to distSq
-rw-r--r-- | src/actormanager.cpp | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/src/actormanager.cpp b/src/actormanager.cpp index ffb856b21..4324bf5d7 100644 --- a/src/actormanager.cpp +++ b/src/actormanager.cpp @@ -1288,9 +1288,9 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, return *i; } - int dist = 0; int index = defaultPriorityIndex; Being *closestBeing = nullptr; + int closestDistSq = maxDistSq + 1; FOR_EACH (ActorSprites::iterator, i, mActors) { @@ -1332,15 +1332,15 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, if (!validateBeing(aroundBeing, being, type, excluded, 50)) continue; - int d = being->getDistance(); - d = d*d; + int distSq = being->getDistance(); + distSq = distSq * distSq; if (being->getType() != ActorType::Monster || !mTargetOnlyReachable) { // if distance not calculated, use old distance const int dx = being->getTileX() - x; const int dy = being->getTileY() - y; - d = dx*dx + dy*dy; + distSq = dx*dx + dy*dy; } if (specialDistance && being->getDistance() <= 2 @@ -1353,12 +1353,15 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, // logger->log("index:" + toString(index)); // logger->log("d:" + toString(d)); - if (!filtered && (d <= dist || (closestBeing == nullptr))) + if (!filtered) { - dist = d; - closestBeing = being; + if (distSq < closestDistSq) + { + closestDistSq = distSq; + closestBeing = being; + } } - else if (filtered) + else { int w2 = defaultPriorityIndex; if (closestBeing != nullptr) @@ -1368,26 +1371,20 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, if (it2 != priorityMobsMap.end()) w2 = (*it2).second; - if (w2 < index) - { - dist = d; - closestBeing = being; - index = w2; - continue; - } - if (w2 == index && d <= dist) + if ((w2 < index && distSq <= maxDistSq) || + (w2 == index && distSq < closestDistSq)) { - dist = d; closestBeing = being; + closestDistSq = distSq; index = w2; continue; } } - if (closestBeing == nullptr) + if (closestBeing == nullptr && distSq <= maxDistSq) { - dist = d; closestBeing = being; + closestDistSq = distSq; const StringIntMapCIter it1 = priorityMobsMap.find( being->getName()); if (it1 != priorityMobsMap.end()) @@ -1397,7 +1394,7 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, } } } - return (maxDistSq >= dist) ? closestBeing : nullptr; + return closestBeing; } bool ActorManager::validateBeing(const Being *const aroundBeing, |