mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-21 01:37:37 +01:00
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).
This commit is contained in:
@@ -171,54 +171,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())
|
||||
@@ -287,9 +240,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)
|
||||
{
|
||||
@@ -312,6 +262,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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user