fix: add proximity gradient to positional score to prevent AI stalemate

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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-04-13 17:07:34 -04:00
co-authored by Claude Happy
parent f37828e809
commit 16521b47b9
@@ -170,8 +170,17 @@ internal static class ScoringFunctions
// ── Positional ────────────────────────────────────────────────────────────
/// <summary>
/// +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.
/// </summary>
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<Coord>();
var enemyUnitPositions = new List<Coord>();
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<FieldCard>())
if (fc.Position is not null) enemyUnitPositions.Add(fc.Position.Value);
}
foreach (var unit in aiPlayer.Field.OfType<FieldCard>())
{
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;
}