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 8d0633c842)
This commit is contained in:
Treeston
2017-07-13 17:49:48 +02:00
committed by Shauren
parent 6e0be8b469
commit 5f59be31b0

View File

@@ -172,54 +172,7 @@ class TC_GAME_API UnitAI
return nullptr;
std::list<Unit*> 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 <offset> 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;