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 673aa72
Showing 1 changed file with 53 additions and 2 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,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<AvailableTarget> 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];
Expand All @@ -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++)
{
Expand Down Expand Up @@ -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

// ------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 673aa72

Please sign in to comment.