From 16521b47b9b530907d014edd58f0c84d7df2b6e3 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 13 Apr 2026 17:07:34 -0400 Subject: [PATCH] fix: add proximity gradient to positional score to prevent AI stalemate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The old scoring only gave +15 when a unit was already adjacent to the enemy ArchSpirit — no reward for closing distance from further away. This caused both AIs to settle into a static local optimum far from each other and never advance (observed at turn 171 with armies on opposite sides of the board). Adds a continuous gradient: +20×(1-dist/maxDist) per unit toward enemy ArchSpirit and +8×(1-dist/maxDist) toward nearest enemy unit. Each step forward now gives a small positive score improvement, so the greedy engine always has a reason to advance when no better action exists. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../src/SOR.Ai/Heuristics/ScoringFunctions.cs | 71 ++++++++++++++++--- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/Server/src/SOR.Ai/Heuristics/ScoringFunctions.cs b/Server/src/SOR.Ai/Heuristics/ScoringFunctions.cs index 809d67e..b22d20d 100644 --- a/Server/src/SOR.Ai/Heuristics/ScoringFunctions.cs +++ b/Server/src/SOR.Ai/Heuristics/ScoringFunctions.cs @@ -170,8 +170,17 @@ internal static class ScoringFunctions // ── Positional ──────────────────────────────────────────────────────────── /// - /// +15 per own unit adjacent to any enemy ArchSpirit (pressure). - /// +2 per own unit in the board centre (control). + /// Rewards units for advancing toward the enemy rather than sitting idle. + /// + /// Per non-leader unit: + /// +15 adjacent to enemy ArchSpirit (attack pressure) + /// +20×(1 − dist/maxDist) proximity gradient toward nearest enemy ArchSpirit + /// +8×(1 − dist/maxDist) proximity gradient toward nearest enemy unit + /// +2 in board centre (control) + /// + /// The gradients give a small but non-zero reward for every step toward the enemy, + /// so the greedy engine always has a reason to advance when no better action exists. + /// Without them the engine settles into a static local optimum far from the opponent. /// private static float PositionalScore( GameState state, string aiAccountId, PlayerState aiPlayer) @@ -184,30 +193,72 @@ internal static class ScoringFunctions int cy = board.Height / 2; int centerRadius = Math.Max(1, Math.Min(board.Width, board.Height) / 3); + // Upper-bound on board distance (used to normalise gradients to [0,1]). + float maxDist = board.HexSides > 0 + ? board.Width + board.Height // hex rough bound + : board.Width + board.Height - 2f; // Manhattan max on W×H grid + if (maxDist < 1f) maxDist = 1f; + + // Collect enemy positions. var enemyArchPositions = new List(); + var enemyUnitPositions = new List(); foreach (var (id, p) in state.Players) { if (id == aiAccountId || p.IsEliminated) continue; if (p.ArchSpirit?.Position.HasValue == true) enemyArchPositions.Add(p.ArchSpirit.Position.Value); + foreach (var fc in p.Field.OfType()) + if (fc.Position is not null) enemyUnitPositions.Add(fc.Position.Value); } foreach (var unit in aiPlayer.Field.OfType()) { - if (unit.Position is null) continue; + if (unit.IsLeader || unit.Position is null) continue; + var pos = unit.Position.Value; - foreach (var eap in enemyArchPositions) + // ── Proximity to enemy ArchSpirit ────────────────────────────────── + if (enemyArchPositions.Count > 0) { - bool adjacent = board.HexSides > 0 - ? HexGrid.Distance(unit.Position.Value, eap, board.HexSides) == 1 - : unit.Position.Value.IsOrthogonallyAdjacentTo(eap); - if (adjacent) { score += 15f; break; } + float minDist = float.MaxValue; + foreach (var eap in enemyArchPositions) + { + float d = board.HexSides > 0 + ? HexGrid.Distance(pos, eap, board.HexSides) + : pos.ManhattanDistance(eap); + if (d < minDist) minDist = d; + } + + if (minDist <= 1f) + { + // Adjacent: strong bonus (imminent attack opportunity). + score += 15f; + } + else + { + // Gradient: continuously reward closing distance. + score += 20f * (1f - minDist / maxDist); + } } + // ── Proximity to nearest enemy unit ──────────────────────────────── + if (enemyUnitPositions.Count > 0) + { + float minEnemyDist = float.MaxValue; + foreach (var eup in enemyUnitPositions) + { + float d = board.HexSides > 0 + ? HexGrid.Distance(pos, eup, board.HexSides) + : pos.ManhattanDistance(eup); + if (d < minEnemyDist) minEnemyDist = d; + } + score += 8f * (1f - minEnemyDist / maxDist); + } + + // ── Centre control ───────────────────────────────────────────────── if (board.HexSides == 0) { - int dx = Math.Abs(unit.Position.Value.X - cx); - int dy = Math.Abs(unit.Position.Value.Y - cy); + int dx = Math.Abs(pos.X - cx); + int dy = Math.Abs(pos.Y - cy); if (dx <= centerRadius && dy <= centerRadius) score += 2f; }