Fix turn choice ordering not functioning properly

This commit is contained in:
2025-11-02 22:57:46 +01:00
parent 7e537c4003
commit f00453448f
3 changed files with 76 additions and 19 deletions

View File

@@ -25,6 +25,7 @@ public class BattleChoiceQueue : IDeepCloneable
/// <inheritdoc cref="BattleChoiceQueue"/>
public BattleChoiceQueue(ITurnChoice[] choices)
{
Array.Sort(choices, TurnChoiceComparer.Instance!);
_choices = choices;
}

View File

@@ -18,12 +18,12 @@ public class TurnChoiceComparer : IComparer<ITurnChoice>
private static CompareValues CompareForSameType(ITurnChoice x, ITurnChoice y)
{
// Higher speed goes first
var speedComparison = x.Speed.CompareTo(y.Speed);
var speedComparison = y.Speed.CompareTo(x.Speed);
if (speedComparison != 0)
return (CompareValues)speedComparison;
// If speed is equal, we use the random values we've given to each choice to tiebreak.
// This is to ensure that the order of choices is deterministic.
return (CompareValues)x.RandomValue.CompareTo(y.RandomValue);
return (CompareValues)y.RandomValue.CompareTo(x.RandomValue);
}
private static CompareValues CompareImpl(ITurnChoice? x, ITurnChoice? y)
@@ -46,7 +46,7 @@ public class TurnChoiceComparer : IComparer<ITurnChoice>
if (y is IMoveChoice moveY)
{
// Higher priority goes first
var priorityComparison = moveX.Priority.CompareTo(moveY.Priority);
var priorityComparison = moveY.Priority.CompareTo(moveX.Priority);
if (priorityComparison != 0)
return (CompareValues)priorityComparison;
return CompareForSameType(moveX, moveY);