From 69121bb79ec2a4440bed686d2c22e73cc0f12497 Mon Sep 17 00:00:00 2001 From: Fedja Beader Date: Mon, 10 Mar 2025 17:56:45 +0000 Subject: Speedup ActorManager::findNearestByName by testing cheap bounding box distance before (potentially) asking pathfinder. Pathfinder may get called in the validateBeing() call, can't tell yet. Squashed with: * Fix linter This test and the below static_cast looks like dynamic_cast should've been used. **** mana/plus!143 --- src/actormanager.cpp | 17 ++++++++--------- 1 file 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; } } -- cgit v1.2.3-70-g09d2