From 673aa72ed791159431a245867f225b923e959665 Mon Sep 17 00:00:00 2001 From: Iridar Date: Mon, 9 Sep 2024 01:25:33 +0300 Subject: [PATCH] Issue #1233 - ensure order of TAB-cycling through targets matches the displayed order of target icons --- .../XComGame/Classes/UITacticalHUD_Enemies.uc | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/X2WOTCCommunityHighlander/Src/XComGame/Classes/UITacticalHUD_Enemies.uc b/X2WOTCCommunityHighlander/Src/XComGame/Classes/UITacticalHUD_Enemies.uc index 133508519..43cc46aa9 100644 --- a/X2WOTCCommunityHighlander/Src/XComGame/Classes/UITacticalHUD_Enemies.uc +++ b/X2WOTCCommunityHighlander/Src/XComGame/Classes/UITacticalHUD_Enemies.uc @@ -16,6 +16,7 @@ struct CH_CachedTargetHitChanceStruct var StateObjectReference TargetRef; var bool bIsDestructibleObject; var int HitChance; + var int AvailableTargetIndex; }; // End Issue #1233 @@ -924,14 +925,43 @@ private function SortEnemies_CH() local CH_CachedTargetHitChanceStruct CachedTargetHitChance; local XComGameState_BaseObject TargetObject; local XComGameStateHistory History; + local X2TargetingMethod TargetingMethod; + local AvailableAction Action; + local array AvailableTargets; + local int AvailableTargetIndex; local int iNumTargets; local int i; - History = `XCOMHISTORY; - iNumTargets = m_arrTargets.Length; + if (iNumTargets == 0) + return; + arrCachedTargetHitChance.Length = iNumTargets; + // Pressing TAB while in tactical invokes UITacticalHUD::GetTargetingMethod().NextTarget(), + // which normally will make the targeting method select the next target + // from the TargetingMethod.Action.AvailableTargets array. + // The array is normally filled by XCGS_Ability::GatherAbilityTargets(), + // which also sorts targets by hit chance. + // This means that when TAB-cycling through multiple targets with the same hit chance, + // the order of selecting targets may not match the displayed order of target icons. + // To make sure these orders match, we additionally sort targets by their index + // in the TargetingMethod.Action.AvailableTargets array. + + TargetingMethod = XComPresentationLayer(screen.Owner).GetTacticalHUD().GetTargetingMethod(); + if (TargetingMethod != none) + { + AvailableTargets = TargetingMethod.Action.AvailableTargets; + } + else + { + if (XComPresentationLayer(Movie.Pres).GetTacticalHUD().m_kAbilityHUD.GetDefaultTargetingAbility(m_arrTargets[0].ObjectID, Action, true)) + { + AvailableTargets = Action.AvailableTargets; + } + } + + History = `XCOMHISTORY; for (i = 0; i < iNumTargets; i++) { CachedTargetHitChance.TargetRef = m_arrTargets[i]; @@ -941,10 +971,20 @@ private function SortEnemies_CH() CachedTargetHitChance.bIsDestructibleObject = TargetObject != none && XComGameState_Destructible(TargetObject) != none; + for (AvailableTargetIndex = 0; AvailableTargetIndex < AvailableTargets.Length; AvailableTargetIndex++) + { + if (AvailableTargets[AvailableTargetIndex].PrimaryTarget.ObjectID == CachedTargetHitChance.TargetRef.ObjectID) + { + CachedTargetHitChance.AvailableTargetIndex = AvailableTargetIndex; + break; + } + } + arrCachedTargetHitChance[i] = CachedTargetHitChance; } arrCachedTargetHitChance.Sort(SortTargetsByHitChance_CH); + arrCachedTargetHitChance.Sort(SortTargetsByTargetIndex_CH); for (i = 0; i < iNumTargets; i++) { @@ -973,6 +1013,17 @@ private function int SortTargetsByHitChance_CH(CH_CachedTargetHitChanceStruct Ob return 1; } +private function int SortTargetsByTargetIndex_CH(CH_CachedTargetHitChanceStruct ObjectA, CH_CachedTargetHitChanceStruct ObjectB) +{ + // Push targets with higher index in the Targeting Method's Available Targets array to the end of the list. + // This should ensure the order of TAB-cycling through targets matches the displayed order of target icons. + if (ObjectA.AvailableTargetIndex > ObjectB.AvailableTargetIndex) + { + return -1; + } + + return 1; +} // End Issue #1233 // ------------------------------------------------------------------------------------------