diff options
author | Fedja Beader <fedja@protonmail.ch> | 2025-03-21 23:11:03 +0100 |
---|---|---|
committer | Fedja Beader <fedja@protonmail.ch> | 2025-04-06 01:44:21 +0200 |
commit | 2125df248d60f0ce3f0f3d43239131beb3a2f3d6 (patch) | |
tree | 030da3eba810bb45591c5468974d75a2d71c52a6 /src | |
parent | 48a62cbf4ce6930f4a3d86d0af620d4c21cbc191 (diff) | |
download | mv-2125df248d60f0ce3f0f3d43239131beb3a2f3d6.tar.gz mv-2125df248d60f0ce3f0f3d43239131beb3a2f3d6.tar.bz2 mv-2125df248d60f0ce3f0f3d43239131beb3a2f3d6.tar.xz mv-2125df248d60f0ce3f0f3d43239131beb3a2f3d6.zip |
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
**** mana/plus!145
Diffstat (limited to 'src')
-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, |