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