diff options
-rw-r--r-- | src/actormanager.cpp | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/src/actormanager.cpp b/src/actormanager.cpp index 7835d3289..9cedb37b7 100644 --- a/src/actormanager.cpp +++ b/src/actormanager.cpp @@ -909,8 +909,8 @@ Being *ActorManager::findNearestByName(const std::string &name, if (localPlayer == nullptr) return nullptr; - int dist = 0; Being* closestBeing = nullptr; + int closestDistSq = INT_MAX; const int x = localPlayer->getTileX(); const int y = localPlayer->getTileY(); @@ -920,8 +920,8 @@ Being *ActorManager::findNearestByName(const std::string &name, // if (reportTrue(*it == nullptr)) // continue; - if ((*it)->getType() == ActorType::FloorItem - || (*it)->getType() == ActorType::Portal) + if ((*it)->getType() == ActorType::FloorItem || + (*it)->getType() == ActorType::Portal) { continue; } @@ -932,17 +932,16 @@ Being *ActorManager::findNearestByName(const std::string &name, (type == ActorType::Unknown || type == being->getType())) { if (being->getType() == ActorType::Player) - { return being; - } + const int dx = being->getTileX() - x; const int dy = being->getTileY() - y; - const int d = dx*dx + dy*dy; + const int distSq = dx*dx + dy*dy; - if (validateBeing(nullptr, being, type, nullptr, 50) - && (d < dist || closestBeing == nullptr)) + if (distSq < closestDistSq + && validateBeing(nullptr, being, type, nullptr, 50)) { - dist = d; + closestDistSq = distSq; closestBeing = being; } } |