From 5f59be31b0dc8b8b58da06fded6ef91dc170e8b6 Mon Sep 17 00:00:00 2001 From: Treeston Date: Thu, 13 Jul 2017 17:49:48 +0200 Subject: Core/AI: UnitAI::SelectTarget now applies offset BEFORE filtering by predicate (to match expected behavior). (#19975) * Also some major code deduplication (SelectTarget now calls SelectTargetList). (cherry picked from commit 8d0633c842d474a843ec9f4bd45dcec3013bfd5d) --- src/server/game/AI/CoreAI/UnitAI.h | 55 +++----------------------------------- 1 file changed, 4 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/server/game/AI/CoreAI/UnitAI.h b/src/server/game/AI/CoreAI/UnitAI.h index 4dd25592677..1f1a44c429d 100644 --- a/src/server/game/AI/CoreAI/UnitAI.h +++ b/src/server/game/AI/CoreAI/UnitAI.h @@ -172,54 +172,7 @@ class TC_GAME_API UnitAI return nullptr; std::list targetList; - if (targetType == SELECT_TARGET_MAXDISTANCE || targetType == SELECT_TARGET_MINDISTANCE) - { - for (ThreatReference const* ref : mgr.GetUnsortedThreatList()) - { - if (ref->IsOffline()) - continue; - - targetList.push_back(ref->GetVictim()); - } - } - else - { - Unit* currentVictim = mgr.GetCurrentVictim(); - if (currentVictim) - targetList.push_back(currentVictim); - - for (ThreatReference const* ref : mgr.GetSortedThreatList()) - { - if (ref->IsOffline()) - continue; - - Unit* thisTarget = ref->GetVictim(); - if (thisTarget != currentVictim) - targetList.push_back(thisTarget); - } - } - - // filter by predicate - targetList.remove_if([&predicate](Unit* target) { return !predicate(target); }); - - // shortcut: the list certainly isn't gonna get any larger after this point - if (targetList.size() <= offset) - return nullptr; - - // right now, list is unsorted for DISTANCE types - re-sort by MAXDISTANCE - if (targetType == SELECT_TARGET_MAXDISTANCE || targetType == SELECT_TARGET_MINDISTANCE) - SortByDistance(targetList, targetType == SELECT_TARGET_MINDISTANCE); - - // then reverse the sorting for MIN sortings - if (targetType == SELECT_TARGET_MINTHREAT) - targetList.reverse(); - - // now pop the first elements - while (offset) - { - targetList.pop_front(); - --offset; - } + SelectTargetList(targetList, mgr.GetThreatListSize(), targetType, offset, predicate); // maybe nothing fulfills the predicate if (targetList.empty()) @@ -288,9 +241,6 @@ class TC_GAME_API UnitAI } } - // filter by predicate - targetList.remove_if([&predicate](Unit* target) { return !predicate(target); }); - // shortcut: the list isn't gonna get any larger if (targetList.size() <= offset) { @@ -313,6 +263,9 @@ class TC_GAME_API UnitAI --offset; } + // then finally filter by predicate + targetList.remove_if([&predicate](Unit* target) { return !predicate(target); }); + if (targetList.size() <= num) return; -- cgit v1.2.3