From 10a73d8002d3487af8cbc73564969ffead898549 Mon Sep 17 00:00:00 2001 From: hondacrx Date: Sat, 22 Aug 2020 14:47:58 -0400 Subject: [PATCH] Core/AI: UnitAI::SelectTarget now applies offset BEFORE filtering by predicate (to match expected behavior). Port From (https://github.com/TrinityCore/TrinityCore/commit/5f59be31b0dc8b8b58da06fded6ef91dc170e8b6) --- Source/Game/AI/CoreAI/UnitAI.cs | 55 +++------------------------------ 1 file changed, 4 insertions(+), 51 deletions(-) diff --git a/Source/Game/AI/CoreAI/UnitAI.cs b/Source/Game/AI/CoreAI/UnitAI.cs index 10f636181..546f5078f 100644 --- a/Source/Game/AI/CoreAI/UnitAI.cs +++ b/Source/Game/AI/CoreAI/UnitAI.cs @@ -139,54 +139,7 @@ namespace Game.AI if (mgr.GetThreatListSize() <= offset) return null; - List targetList = new List(); - if (targetType == SelectAggroTarget.MaxDistance || targetType == SelectAggroTarget.MinDistance) - { - foreach (HostileReference refe in mgr.GetThreatList()) - { - if (!refe.IsOnline()) - continue; - - targetList.Add(refe.GetTarget()); - } - } - else - { - Unit currentVictim = mgr.GetCurrentVictim(); - if (currentVictim) - targetList.Add(currentVictim); - - foreach (HostileReference refe in mgr.GetThreatList()) - { - if (!refe.IsOnline()) - continue; - - Unit thisTarget = refe.GetTarget(); - if (thisTarget != currentVictim) - targetList.Add(thisTarget); - } - } - - // filter by predicate - targetList.RemoveAll(target => { return !selector.Check(target); }); - // shortcut: the list certainly isn't gonna get any larger after this point - if (targetList.Count <= offset) - return null; - - // right now, list is unsorted for DISTANCE types - re-sort by MAXDISTANCE - if (targetType == SelectAggroTarget.MaxDistance || targetType == SelectAggroTarget.MinDistance) - SortByDistance(targetList, targetType == SelectAggroTarget.MinDistance); - - // then reverse the sorting for MIN sortings - if (targetType == SelectAggroTarget.MinThreat) - targetList.Reverse(); - - // now pop the first elements - while (offset != 0) - { - targetList.RemoveAt(0); - --offset; - } + List targetList = SelectTargetList((uint)mgr.GetThreatListSize(), targetType, offset, selector); // maybe nothing fulfills the predicate if (targetList.Empty()) @@ -262,9 +215,6 @@ namespace Game.AI } } - // filter by predicate - targetList.RemoveAll(target => { return !selector.Check(target); }); - // shortcut: the list isn't gonna get any larger if (targetList.Count <= offset) { @@ -287,6 +237,9 @@ namespace Game.AI --offset; } + // then finally filter by predicate + targetList.RemoveAll(target => { return !selector.Check(target); }); + if (targetList.Count <= num) return targetList;