Skip to content

Commit

Permalink
Commit partial fixes even though the algorithm needs overhauling
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliveriver committed Sep 9, 2024
1 parent 2b07081 commit 3125c46
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 10 deletions.
2 changes: 1 addition & 1 deletion server/Adjudication/Evaluation/CycleFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class CycleFinder(AdjacencyValidator adjacencyValidator)

public List<Move> GetMoveCycle(List<Order> orders)
{
var moves = orders.OfType<Move>().Where(m => m.Status is OrderStatus.New or OrderStatus.Success).ToList();
var moves = orders.OfType<Move>().Where(m => m.Status is OrderStatus.New or OrderStatus.Success && !m.IsSzykmanHold).ToList();

var visitedMoves = new List<Move>();
var cycle = new List<Move>();
Expand Down
30 changes: 28 additions & 2 deletions server/Adjudication/Evaluation/MovementEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void EvaluateMovements()

if (unresolvedNonMoves.Count > 0 && unresolvedMoves.Count == 0)
{
IdentifyHeadToHeadBattles();
initialEvaluator.ApplyEvaluationPass();
}

Expand All @@ -39,7 +40,10 @@ public void EvaluateMovements()

foreach (var move in unresolvedMoves)
{
ResolveDependencies(move);
if (move.Dependencies.Count > 0)
{
ResolveDependencies(move);
}
}

unresolvedMoves = activeOrders.OfType<Move>().Where(m => m.Status == OrderStatus.New).ToList();
Expand Down Expand Up @@ -72,6 +76,7 @@ private void IdentifyHeadToHeadBattles()

var opposingMove = moves.FirstOrDefault(m =>
m.Status != OrderStatus.Invalid
&& !m.IsSzykmanHold
&& adjacencyValidator.EqualsOrIsRelated(m.Location, move.Destination)
&& adjacencyValidator.EqualsOrIsRelated(m.Destination, move.Location)
&& m.ConvoyPath.Count == 0);
Expand Down Expand Up @@ -107,24 +112,37 @@ private void ResolveDependencies(Move move)
while (!initialStatuses.SequenceEqual(newStatuses))
{
initialStatuses = newStatuses;
IdentifyHeadToHeadBattles();
treeEvaluator.ApplyEvaluationPass();
newStatuses = move.Dependencies.Select(o => o.Status).ToList();
}

if (move.Status == OrderStatus.New)
{
var initialVirtualHoldStates = move.Dependencies.Select(o => o is Move m && m.IsSzykmanHold).ToList();

var isConsistentSuccess = TryStatusGuess(move, OrderStatus.Success);

foreach (var order in move.Dependencies)
{
order.Status = initialStatuses[move.Dependencies.IndexOf(order)];

if (order is Move otherMove)
{
otherMove.IsSzykmanHold = initialVirtualHoldStates[move.Dependencies.IndexOf(otherMove)];
}
}

var isConsistentFailure = TryStatusGuess(move, OrderStatus.Failure);

foreach (var order in move.Dependencies)
{
order.Status = initialStatuses[move.Dependencies.IndexOf(order)];

if (order is Move otherMove)
{
otherMove.IsSzykmanHold = initialVirtualHoldStates[move.Dependencies.IndexOf(otherMove)];
}
}

if (isConsistentSuccess && !isConsistentFailure)
Expand Down Expand Up @@ -185,10 +203,18 @@ private bool TryStatusGuess(Move move, OrderStatus status)

private void ResolveConvoyParadox(List<Order> orders)
{
var movesViaConvoy = orders.OfType<Move>().Where(m => m.ConvoyPath.Count > 0);
var movesViaConvoy = orders.OfType<Move>().Where(m =>
m.ConvoyPath.Count > 0 || !adjacencyValidator.IsValidDirectMove(m.Unit!, m.Location, m.Destination));
foreach (var move in movesViaConvoy)
{
var destinationOrder = orders.FirstOrDefault(o => adjacencyValidator.EqualsOrIsRelated(o.Location, move.Destination));
if (destinationOrder is not Support)
{
continue;
}

move.Status = OrderStatus.Failure;
move.IsSzykmanHold = true;
}
}

Expand Down
73 changes: 67 additions & 6 deletions server/Adjudication/Evaluation/OrderTreeEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void UpdateSupportsAttackingSelf()

private void EvaluateHold(Hold hold)
{
var attackingMoves = moves.Where(m => adjacencyValidator.EqualsOrIsRelated(m.Destination, hold.Location));
var attackingMoves = moves.Where(m => !m.IsSzykmanHold && adjacencyValidator.EqualsOrIsRelated(m.Destination, hold.Location));
if (attackingMoves.Any(m => m.Status == OrderStatus.Success))
{
hold.Status = OrderStatus.Failure;
Expand All @@ -115,11 +115,40 @@ private void EvaluateHold(Hold hold)

private void EvaluateMove(Move move)
{
var attackingMoves = moves.Where(m => m != move && adjacencyValidator.EqualsOrIsRelated(m.Destination, move.Destination));
var attackingMoves = moves.Where(m => m != move && !m.IsSzykmanHold && adjacencyValidator.EqualsOrIsRelated(m.Destination, move.Destination));

var beatsPreventStrength = move.AttackStrength.Min > (attackingMoves.Any() ? attackingMoves.Max(m => m.PreventStrength.Max) : 0);
var losesToPreventStrength = move.AttackStrength.Max <= (attackingMoves.Any() ? attackingMoves.Min(m => m.PreventStrength.Min) : 0);

var isPreventedByAllDislodgedConvoys = true;

foreach (var attackingMove in attackingMoves)
{
if (attackingMove.Status != OrderStatus.Failure
|| adjacencyValidator.IsValidDirectMove(attackingMove.Unit!, attackingMove.Location, attackingMove.Destination))
{
isPreventedByAllDislodgedConvoys = false;
break;
}

var attackingConvoys = convoys.Where(c => c.Midpoint == attackingMove.Location && c.Destination == attackingMove.Destination);
foreach (var convoy in attackingConvoys)
{
var isDislodged = moves.Any(m => m.Destination == convoy.Location && m.Status == OrderStatus.Success);
if (!isDislodged)
{
isPreventedByAllDislodgedConvoys = false;
break;
}
}
}

if (isPreventedByAllDislodgedConvoys)
{
beatsPreventStrength = true;
losesToPreventStrength = false;
}

if (move.OpposingMove != null)
{
var beatsDefendStrength = move.AttackStrength.Min > move.OpposingMove.DefendStrength.Max;
Expand Down Expand Up @@ -164,9 +193,41 @@ private void EvaluateMove(Move move)

private void EvaluateSupport(Support support)
{
var attackingMoves = moves.Where(m => adjacencyValidator.EqualsOrIsRelated(m.Destination, support.Location));
var attackingMoves = moves.Where(m => !m.IsSzykmanHold && adjacencyValidator.EqualsOrIsRelated(m.Destination, support.Location) && !m.IsSzykmanHold);

if (!attackingMoves.Any(m => !adjacencyValidator.EqualsOrIsRelated(m.Location, support.Destination)))
{
support.Status = OrderStatus.Success;
return;
}

var isAttackedByAllDislodgedConvoys = true;

foreach (var attackingMove in attackingMoves)
{
if (attackingMove.Status != OrderStatus.Failure
|| adjacencyValidator.IsValidDirectMove(attackingMove.Unit!, attackingMove.Location, attackingMove.Destination))
{
isAttackedByAllDislodgedConvoys = false;
}

var attackingConvoys = convoys.Where(c => c.Midpoint == attackingMove.Location && c.Destination == attackingMove.Destination);
foreach (var convoy in attackingConvoys)
{
var isDislodged = moves.Any(m => m.Destination == convoy.Location && m.Status == OrderStatus.Success);
if (!isDislodged)
{
isAttackedByAllDislodgedConvoys = false;
}

if (convoy.Location == support.Destination)
{
return;
}
}
}

if (attackingMoves.All(m => m.Status == OrderStatus.Failure))
if (isAttackedByAllDislodgedConvoys)
{
support.Status = OrderStatus.Success;
return;
Expand All @@ -178,7 +239,7 @@ private void EvaluateSupport(Support support)
return;
}

var opposingMove = attackingMoves.FirstOrDefault(m => adjacencyValidator.EqualsOrIsRelated(m.Location, support.Destination));
var opposingMove = attackingMoves.FirstOrDefault(m => !m.IsSzykmanHold && adjacencyValidator.EqualsOrIsRelated(m.Location, support.Destination));

var isAttackedByUnresolvedConvoy = attackingMoves.Any(m =>
m.Unit!.Owner != support.Unit!.Owner
Expand All @@ -200,7 +261,7 @@ private void EvaluateSupport(Support support)

private void EvaluateConvoy(Convoy convoy)
{
var attackingMoves = moves.Where(m => m.Destination == convoy.Location);
var attackingMoves = moves.Where(m => !m.IsSzykmanHold && m.Destination == convoy.Location);

if (convoy.Status != OrderStatus.Failure && !attackingMoves.Any(m => m.Status != OrderStatus.Failure))
{
Expand Down
2 changes: 1 addition & 1 deletion server/Adjudication/Evaluation/StrengthCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class StrengthCalculator(List<Order> activeOrders, AdjacencyValidator adj

public void UpdateOrderStrength(Order order)
{
if (order is Move move)
if (order is Move move && !move.IsSzykmanHold)
{
CalculateMoveStrength(move);
return;
Expand Down
3 changes: 3 additions & 0 deletions server/Entities/Orders/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ public override OrderStatus Status
[NotMapped]
public OrderStrength PreventStrength { get; set; } = new();

[NotMapped]
public bool IsSzykmanHold { get; set; }

public override string ToString() => $"Move {Location} to {Destination}: {Status}";
}

0 comments on commit 3125c46

Please sign in to comment.