Skip to content

Commit

Permalink
Issue X2CommunityCore#1233 - ensure order of TAB-cycling through targ…
Browse files Browse the repository at this point in the history
…ets matches the displayed order of target icons
  • Loading branch information
ps2guides committed Sep 8, 2024
1 parent af0ba1e commit 4051dd6
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct CH_CachedTargetHitChanceStruct
var StateObjectReference TargetRef;
var bool bIsDestructibleObject;
var int HitChance;
var int AvailableTargetIndex;
};
// End Issue #1233

Expand Down Expand Up @@ -924,6 +925,10 @@ private function SortEnemies_CH()
local CH_CachedTargetHitChanceStruct CachedTargetHitChance;
local XComGameState_BaseObject TargetObject;
local XComGameStateHistory History;
local X2TargetingMethod TargetingMethod;
local array<AvailableTarget> AvailableTargets;
local bool bTargetingAbility;
local int AvailableTargetIndex;
local int iNumTargets;
local int i;

Expand All @@ -932,6 +937,23 @@ private function SortEnemies_CH()
iNumTargets = m_arrTargets.Length;
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;
bTargetingAbility = true;
}

for (i = 0; i < iNumTargets; i++)
{
CachedTargetHitChance.TargetRef = m_arrTargets[i];
Expand All @@ -941,6 +963,18 @@ private function SortEnemies_CH()

CachedTargetHitChance.bIsDestructibleObject = TargetObject != none && XComGameState_Destructible(TargetObject) != none;

if (bTargetingAbility)
{
for (AvailableTargetIndex = 0; AvailableTargetIndex < AvailableTargets.Length; AvailableTargetIndex++)
{
if (AvailableTargets[AvailableTargetIndex].PrimaryTarget.ObjectID == CachedTargetHitChance.TargetRef.ObjectID)
{
CachedTargetHitChance.AvailableTargetIndex = AvailableTargetIndex;
break;
}
}
}

arrCachedTargetHitChance[i] = CachedTargetHitChance;
}

Expand Down Expand Up @@ -970,6 +1004,13 @@ private function int SortTargetsByHitChance_CH(CH_CachedTargetHitChanceStruct Ob
return -1;
}

// 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;
}

Expand Down

0 comments on commit 4051dd6

Please sign in to comment.