From 57efa1ec52af28386242767f9a68abb6836a1811 Mon Sep 17 00:00:00 2001 From: Fedja Beader Date: Sat, 22 Mar 2025 01:01:26 +0100 Subject: Cleanup findNearestLivingBeing: search priorityMobsMap at most once per being (down from 3x) Note: the filter used priorityMobs (instead of priorityMobsMap). The former seems to be the std::set variant of the latter, but I can't find where either are filled. It does work in practice, however... --- src/actormanager.cpp | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/actormanager.cpp b/src/actormanager.cpp index d30ddbc3a..674239da0 100644 --- a/src/actormanager.cpp +++ b/src/actormanager.cpp @@ -1305,6 +1305,7 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, } Being *const being = static_cast(*i); + int priority = defaultPriorityIndex; if (filtered) { // Skip ignored @@ -1313,11 +1314,18 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, { continue; } + + const StringIntMapCIter prioIter + = priorityMobsMap.find(being->getName()); + + if (prioIter != priorityMobsMap.end()) + priority = prioIter->second; + // 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()) + && prioIter == priorityMobsMap.end()) { continue; } @@ -1349,9 +1357,8 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, continue; } -// logger->log("being name:" + being->getName()); -// logger->log("index:" + toString(index)); -// logger->log("d:" + toString(d)); + //logger->log("being prio:%3d, dist^2:%3d, name: '%s'", + // priority, distSq, being->getName().c_str()); if (!filtered) { @@ -1363,20 +1370,14 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, } else { - int w2 = defaultPriorityIndex; if (closestBeing != nullptr) { - const StringIntMapCIter it2 = priorityMobsMap.find( - being->getName()); - if (it2 != priorityMobsMap.end()) - w2 = (*it2).second; - - if ((w2 < closestPriority && distSq <= maxDistSq) || - (w2 == closestPriority && distSq < closestDistSq)) + if ((priority < closestPriority && distSq <= maxDistSq) || + (priority == closestPriority && distSq < closestDistSq)) { closestBeing = being; closestDistSq = distSq; - closestPriority = w2; + closestPriority = priority; continue; } } @@ -1385,12 +1386,7 @@ Being *ActorManager::findNearestLivingBeing(const Being *const aroundBeing, { closestBeing = being; closestDistSq = distSq; - const StringIntMapCIter it1 = priorityMobsMap.find( - being->getName()); - if (it1 != priorityMobsMap.end()) - closestPriority = it1->second; - else - closestPriority = defaultPriorityIndex; + closestPriority = priority; } } } -- cgit v1.2.3-70-g09d2