improve(ai): smarter heuristics + faster turn timing

ScoringFunctions improvements:
- AffinityControlScore: add cluster bonus (+1.5 per tile adjacent to
  another matching-affinity tile) so Terramorpha chaining near existing
  converted tiles scores higher than isolated placements
- ThreatScore: scale threat penalty with ArchSpirit HP — below 50% HP
  adds -40 per threat, below 25% adds -90 (AI retreats/defends when hurt
  instead of ignoring danger until death)
- ResourceScore: raise ST token value 5→8 (tokens are immediately fungible)
- SacrificeOpportunityScore (new): one-step lookahead that detects when
  sacrificing adjacent units would unlock an unaffordable card in hand;
  adds that card's estimated value so the greedy engine connects
  sacrifice→gain ST→play card chains rather than seeing sacrifice as a
  net loss

AiDifficultyProfiles: halved all timing delays across tiers 1–10
(think time and between-action delay) for faster gameplay during development

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 12:05:09 -04:00
co-authored by Claude Happy
parent 4a1eb9472b
commit d3db778c7f
2 changed files with 185 additions and 103 deletions
@@ -15,10 +15,10 @@ internal static class AiDifficultyProfiles
DisplayName = "Novice", DisplayName = "Novice",
EngineType = "Random", EngineType = "Random",
MistakeRate = 1.0f, MistakeRate = 1.0f,
MinThinkTimeMs = 600, MinThinkTimeMs = 300,
MaxThinkTimeMs = 1400, MaxThinkTimeMs = 700,
MinActionDelayMs = 300, MinActionDelayMs = 150,
MaxActionDelayMs = 700, MaxActionDelayMs = 400,
}, },
// ── Tier 2: Apprentice (Heuristic, 60% mistakes) ───────────────────── // ── Tier 2: Apprentice (Heuristic, 60% mistakes) ─────────────────────
@@ -28,10 +28,10 @@ internal static class AiDifficultyProfiles
DisplayName = "Apprentice", DisplayName = "Apprentice",
EngineType = "Heuristic", EngineType = "Heuristic",
MistakeRate = 0.60f, MistakeRate = 0.60f,
MinThinkTimeMs = 700, MinThinkTimeMs = 400,
MaxThinkTimeMs = 1600, MaxThinkTimeMs = 900,
MinActionDelayMs = 400, MinActionDelayMs = 200,
MaxActionDelayMs = 900, MaxActionDelayMs = 500,
MaterialWeight = 1.0f, MaterialWeight = 1.0f,
AffinityControlWeight = 0.5f, AffinityControlWeight = 0.5f,
ThreatWeight = 0.5f, ThreatWeight = 0.5f,
@@ -46,10 +46,10 @@ internal static class AiDifficultyProfiles
DisplayName = "Journeyman", DisplayName = "Journeyman",
EngineType = "Heuristic", EngineType = "Heuristic",
MistakeRate = 0.40f, MistakeRate = 0.40f,
MinThinkTimeMs = 800, MinThinkTimeMs = 400,
MaxThinkTimeMs = 1800, MaxThinkTimeMs = 1000,
MinActionDelayMs = 400, MinActionDelayMs = 200,
MaxActionDelayMs = 900, MaxActionDelayMs = 500,
MaterialWeight = 1.0f, MaterialWeight = 1.0f,
AffinityControlWeight = 0.8f, AffinityControlWeight = 0.8f,
ThreatWeight = 0.8f, ThreatWeight = 0.8f,
@@ -64,10 +64,10 @@ internal static class AiDifficultyProfiles
DisplayName = "Adept", DisplayName = "Adept",
EngineType = "Heuristic", EngineType = "Heuristic",
MistakeRate = 0.25f, MistakeRate = 0.25f,
MinThinkTimeMs = 900, MinThinkTimeMs = 500,
MaxThinkTimeMs = 2000, MaxThinkTimeMs = 1100,
MinActionDelayMs = 500, MinActionDelayMs = 250,
MaxActionDelayMs = 1100, MaxActionDelayMs = 600,
MaterialWeight = 1.0f, MaterialWeight = 1.0f,
AffinityControlWeight = 1.0f, AffinityControlWeight = 1.0f,
ThreatWeight = 1.0f, ThreatWeight = 1.0f,
@@ -82,10 +82,10 @@ internal static class AiDifficultyProfiles
DisplayName = "Expert", DisplayName = "Expert",
EngineType = "Heuristic", EngineType = "Heuristic",
MistakeRate = 0.10f, MistakeRate = 0.10f,
MinThinkTimeMs = 1000, MinThinkTimeMs = 600,
MaxThinkTimeMs = 2200, MaxThinkTimeMs = 1300,
MinActionDelayMs = 600, MinActionDelayMs = 300,
MaxActionDelayMs = 1200, MaxActionDelayMs = 700,
MaterialWeight = 1.0f, MaterialWeight = 1.0f,
AffinityControlWeight = 1.2f, AffinityControlWeight = 1.2f,
ThreatWeight = 1.2f, ThreatWeight = 1.2f,
@@ -102,10 +102,10 @@ internal static class AiDifficultyProfiles
MistakeRate = 0.05f, MistakeRate = 0.05f,
MctsTimeBudgetMs = 500, MctsTimeBudgetMs = 500,
MctsMaxDepth = 3, MctsMaxDepth = 3,
MinThinkTimeMs = 1200, MinThinkTimeMs = 700,
MaxThinkTimeMs = 2500, MaxThinkTimeMs = 1500,
MinActionDelayMs = 700, MinActionDelayMs = 400,
MaxActionDelayMs = 1400, MaxActionDelayMs = 800,
MaterialWeight = 1.2f, MaterialWeight = 1.2f,
AffinityControlWeight = 1.4f, AffinityControlWeight = 1.4f,
ThreatWeight = 1.4f, ThreatWeight = 1.4f,
@@ -120,10 +120,10 @@ internal static class AiDifficultyProfiles
MistakeRate = 0.02f, MistakeRate = 0.02f,
MctsTimeBudgetMs = 1500, MctsTimeBudgetMs = 1500,
MctsMaxDepth = 5, MctsMaxDepth = 5,
MinThinkTimeMs = 1400, MinThinkTimeMs = 800,
MaxThinkTimeMs = 3000, MaxThinkTimeMs = 1700,
MinActionDelayMs = 800, MinActionDelayMs = 450,
MaxActionDelayMs = 1500, MaxActionDelayMs = 900,
MaterialWeight = 1.3f, MaterialWeight = 1.3f,
AffinityControlWeight = 1.5f, AffinityControlWeight = 1.5f,
ThreatWeight = 1.5f, ThreatWeight = 1.5f,
@@ -138,10 +138,10 @@ internal static class AiDifficultyProfiles
MistakeRate = 0.0f, MistakeRate = 0.0f,
MctsTimeBudgetMs = 4000, MctsTimeBudgetMs = 4000,
MctsMaxDepth = 7, MctsMaxDepth = 7,
MinThinkTimeMs = 1500, MinThinkTimeMs = 900,
MaxThinkTimeMs = 3200, MaxThinkTimeMs = 1800,
MinActionDelayMs = 900, MinActionDelayMs = 500,
MaxActionDelayMs = 1600, MaxActionDelayMs = 1000,
MaterialWeight = 1.4f, MaterialWeight = 1.4f,
AffinityControlWeight = 1.6f, AffinityControlWeight = 1.6f,
ThreatWeight = 1.6f, ThreatWeight = 1.6f,
@@ -157,10 +157,10 @@ internal static class AiDifficultyProfiles
MistakeRate = 0.0f, MistakeRate = 0.0f,
MctsTimeBudgetMs = 6000, MctsTimeBudgetMs = 6000,
MctsMaxDepth = 10, MctsMaxDepth = 10,
MinThinkTimeMs = 1500, MinThinkTimeMs = 900,
MaxThinkTimeMs = 3000, MaxThinkTimeMs = 1800,
MinActionDelayMs = 900, MinActionDelayMs = 500,
MaxActionDelayMs = 1600, MaxActionDelayMs = 1000,
MaterialWeight = 1.5f, MaterialWeight = 1.5f,
AffinityControlWeight = 1.8f, AffinityControlWeight = 1.8f,
ThreatWeight = 1.8f, ThreatWeight = 1.8f,
@@ -175,10 +175,10 @@ internal static class AiDifficultyProfiles
MistakeRate = 0.0f, MistakeRate = 0.0f,
MctsTimeBudgetMs = 10000, MctsTimeBudgetMs = 10000,
MctsMaxDepth = 15, MctsMaxDepth = 15,
MinThinkTimeMs = 1500, MinThinkTimeMs = 1000,
MaxThinkTimeMs = 3000, MaxThinkTimeMs = 2000,
MinActionDelayMs = 1000, MinActionDelayMs = 600,
MaxActionDelayMs = 1800, MaxActionDelayMs = 1100,
MaterialWeight = 1.6f, MaterialWeight = 1.6f,
AffinityControlWeight = 2.0f, AffinityControlWeight = 2.0f,
ThreatWeight = 2.0f, ThreatWeight = 2.0f,
+145 -63
View File
@@ -9,45 +9,40 @@ using SOR.Game.Grid;
/// <summary> /// <summary>
/// Board evaluation function for the heuristic decision engine. /// Board evaluation function for the heuristic decision engine.
/// ///
/// Score = Σ (weight × feature) using the formula from the architecture plan §5.2. /// Score = Σ (weight × feature). Higher = better for the AI.
/// Higher = better for the AI. Weights come from AiDifficultyProfile so that /// Weights come from AiDifficultyProfile so different difficulties/strategies
/// different difficulty/strategy combinations produce different behaviour without /// produce different behaviour without code changes.
/// any code changes.
/// </summary> /// </summary>
internal static class ScoringFunctions internal static class ScoringFunctions
{ {
/// <summary>
/// Evaluate the board state from the AI's perspective.
/// Returns a dimensionless float — only the relative ordering of scores matters.
/// </summary>
public static float Evaluate( public static float Evaluate(
GameState state, GameState state,
string aiAccountId, string aiAccountId,
AiDifficultyProfile profile) AiDifficultyProfile profile)
{ {
if (!state.Players.TryGetValue(aiAccountId, out var aiPlayer)) if (!state.Players.TryGetValue(aiAccountId, out var aiPlayer))
return 0f; return 0f;
float material = MaterialScore(state, aiAccountId); float material = MaterialScore(state, aiAccountId);
float affinityControl = AffinityControlScore(state, aiAccountId, aiPlayer); float affinityControl = AffinityControlScore(state, aiAccountId, aiPlayer);
float threat = ThreatScore(state, aiAccountId, aiPlayer); float threat = ThreatScore(state, aiAccountId, aiPlayer);
float positional = PositionalScore(state, aiAccountId, aiPlayer); float positional = PositionalScore(state, aiAccountId, aiPlayer);
float resource = ResourceScore(state, aiPlayer); float resource = ResourceScore(state, aiPlayer);
float sacrificeOpportunity = SacrificeOpportunityScore(state, aiPlayer);
return material * profile.MaterialWeight return material * profile.MaterialWeight
+ affinityControl * profile.AffinityControlWeight + affinityControl * profile.AffinityControlWeight
+ threat * profile.ThreatWeight + threat * profile.ThreatWeight
+ positional * profile.PositionalWeight + positional * profile.PositionalWeight
+ resource * profile.ResourceWeight; + resource * profile.ResourceWeight
+ sacrificeOpportunity * profile.ResourceWeight; // same weight as resource
} }
// ── Feature functions ────────────────────────────────────────────────────── // ── Material ──────────────────────────────────────────────────────────────
/// <summary> /// <summary>
/// Material = own unit value − opponent unit value + ArchSpirit HP ratio bonus. /// own unit power − opponent unit power + ArchSpirit HP ratio × 500.
/// /// ArchSpirit HP dominates: keeping yours alive and threatening theirs are top priorities.
/// Unit value = Damage × 2 + MaxHealth (roughly captures combat power).
/// ArchSpirit HP ratio × 500 heavily rewards keeping your ArchSpirit healthy.
/// </summary> /// </summary>
private static float MaterialScore(GameState state, string aiAccountId) private static float MaterialScore(GameState state, string aiAccountId)
{ {
@@ -57,38 +52,37 @@ internal static class ScoringFunctions
{ {
if (player.IsEliminated) continue; if (player.IsEliminated) continue;
bool isOwn = playerId == aiAccountId; bool isOwn = playerId == aiAccountId;
float sign = isOwn ? 1f : -1f; float sign = isOwn ? 1f : -1f;
// Field units.
foreach (var fc in player.Field.OfType<FieldCard>()) foreach (var fc in player.Field.OfType<FieldCard>())
{ {
if (fc.IsLeader) continue; // ArchSpirit counted separately. if (fc.IsLeader) continue;
float value = (fc.Damage ?? 0) * 2f + (fc.MaxHealth ?? 0); score += sign * ((fc.Damage ?? 0) * 2f + (fc.MaxHealth ?? 0));
score += sign * value;
} }
// ArchSpirit HP ratio.
var arch = player.ArchSpirit; var arch = player.ArchSpirit;
if (arch is not null) if (arch is not null)
{ {
float maxHp = arch.MaxHealth ?? 1; float maxHp = arch.MaxHealth ?? 1;
float curHp = arch.CurrentHealth ?? 0; float curHp = arch.CurrentHealth ?? 0;
float ratio = maxHp > 0 ? curHp / maxHp : 0f; float ratio = maxHp > 0 ? curHp / maxHp : 0f;
score += sign * (ratio * 500f);
if (isOwn)
score += ratio * 500f;
else
score -= ratio * 500f; // destroying opponent ArchSpirit is very good
} }
} }
return score; return score;
} }
// ── Affinity control + Terramorpha chaining ───────────────────────────────
/// <summary> /// <summary>
/// Affinity control = tiles with own ArchSpirit's affinity within channeling range × 3. /// Coverage = number of affinity-matching tiles within ArchSpirit channeling range × 3.
/// Rewards building a "pool" of valid channeling tiles. /// Cluster bonus = +1.5 per tile that is adjacent to at least one other matching tile.
///
/// The cluster bonus drives Terramorpha placements that extend an existing chain:
/// placing a Terramorpha adjacent to another already-converted tile scores higher
/// than an isolated placement, even when coverage gain is the same.
/// </summary> /// </summary>
private static float AffinityControlScore( private static float AffinityControlScore(
GameState state, string aiAccountId, PlayerState aiPlayer) GameState state, string aiAccountId, PlayerState aiPlayer)
@@ -97,14 +91,13 @@ internal static class ScoringFunctions
var archSpirit = aiPlayer.ArchSpirit; var archSpirit = aiPlayer.ArchSpirit;
if (board is null || archSpirit?.Position is null) return 0f; if (board is null || archSpirit?.Position is null) return 0f;
if (!Enum.TryParse<Affinity>(archSpirit.Affinity, out var aiAffinity)) return 0f;
if (!Enum.TryParse<Affinity>(archSpirit.Affinity, out var aiAffinity))
return 0f;
var origin = archSpirit.Position.Value; var origin = archSpirit.Position.Value;
var maxRange = archSpirit.MaxChannelRange ?? 1; var maxRange = archSpirit.MaxChannelRange ?? 1;
float count = 0f; // Pass 1: collect all matching tiles within channeling range.
var matchingInRange = new HashSet<Coord>();
foreach (var tile in board.Tiles.Values) foreach (var tile in board.Tiles.Values)
{ {
var dist = board.HexSides > 0 var dist = board.HexSides > 0
@@ -113,28 +106,72 @@ internal static class ScoringFunctions
if (dist > maxRange) continue; if (dist > maxRange) continue;
if (AffinitySystem.IsAffinityValid(tile, aiAffinity)) if (AffinitySystem.IsAffinityValid(tile, aiAffinity))
count++; matchingInRange.Add(tile.Coord);
} }
return count * 3f; float coverage = matchingInRange.Count * 3f;
// Pass 2: cluster bonus — each matching tile adjacent to another matching tile.
// Counted at most once per tile (break after first matching neighbour found)
// to avoid over-weighting large clusters.
float clusterBonus = 0f;
foreach (var coord in matchingInRange)
{
foreach (var neighbor in board.GetNeighbors(coord))
{
if (matchingInRange.Contains(neighbor))
{
clusterBonus += 1.5f;
break;
}
}
}
return coverage + clusterBonus;
} }
// ── Threat ────────────────────────────────────────────────────────────────
/// <summary> /// <summary>
/// Threat = −30 per opponent unit that can reach the AI's ArchSpirit. /// Base penalty of −30 per enemy unit that can reach the AI's ArchSpirit.
/// A strong deterrent against leaving the ArchSpirit undefended. /// Scaled up sharply when the ArchSpirit is damaged:
/// HP &lt; 50% → additional −40 per threat
/// HP &lt; 25% → additional −90 per threat (critical — flee or defend)
///
/// This makes the AI increasingly aggressive about blocking/retreating as
/// its ArchSpirit loses health, rather than ignoring danger until it's too late.
/// </summary> /// </summary>
private static float ThreatScore( private static float ThreatScore(
GameState state, string aiAccountId, PlayerState aiPlayer) GameState state, string aiAccountId, PlayerState aiPlayer)
{ {
var ctx = new AiTurnContext(state, aiAccountId); var ctx = new AiTurnContext(state, aiAccountId);
int threats = ThreatDetector.CountThreatsToArchSpirit(ctx); int threats = ThreatDetector.CountThreatsToArchSpirit(ctx);
return -threats * 30f;
if (threats == 0) return 0f;
float penalty = threats * 30f;
var arch = aiPlayer.ArchSpirit;
if (arch is not null)
{
float maxHp = arch.MaxHealth ?? 1f;
float curHp = arch.CurrentHealth ?? 0f;
float hpRatio = maxHp > 0 ? curHp / maxHp : 0f;
if (hpRatio < 0.25f)
penalty += threats * 90f; // critical HP — survival overrides everything
else if (hpRatio < 0.5f)
penalty += threats * 40f; // damaged — strongly prefer to defend
}
return -penalty;
} }
// ── Positional ────────────────────────────────────────────────────────────
/// <summary> /// <summary>
/// Positional = +15 per own unit adjacent to any enemy ArchSpirit /// +15 per own unit adjacent to any enemy ArchSpirit (pressure).
/// + +2 per own unit in the center 3×3 zone of the board. /// +2 per own unit in the board centre (control).
/// Rewards pressure and board control.
/// </summary> /// </summary>
private static float PositionalScore( private static float PositionalScore(
GameState state, string aiAccountId, PlayerState aiPlayer) GameState state, string aiAccountId, PlayerState aiPlayer)
@@ -143,13 +180,10 @@ internal static class ScoringFunctions
if (board is null) return 0f; if (board is null) return 0f;
float score = 0f; float score = 0f;
// Center zone: tiles within ⌊board/3⌋ of center.
int cx = board.Width / 2; int cx = board.Width / 2;
int cy = board.Height / 2; int cy = board.Height / 2;
int centerRadius = Math.Max(1, Math.Min(board.Width, board.Height) / 3); int centerRadius = Math.Max(1, Math.Min(board.Width, board.Height) / 3);
// Collect enemy ArchSpirit positions.
var enemyArchPositions = new List<Coord>(); var enemyArchPositions = new List<Coord>();
foreach (var (id, p) in state.Players) foreach (var (id, p) in state.Players)
{ {
@@ -162,7 +196,6 @@ internal static class ScoringFunctions
{ {
if (unit.Position is null) continue; if (unit.Position is null) continue;
// Adjacent to enemy ArchSpirit.
foreach (var eap in enemyArchPositions) foreach (var eap in enemyArchPositions)
{ {
bool adjacent = board.HexSides > 0 bool adjacent = board.HexSides > 0
@@ -171,7 +204,6 @@ internal static class ScoringFunctions
if (adjacent) { score += 15f; break; } if (adjacent) { score += 15f; break; }
} }
// Center control (square grid only for simplicity).
if (board.HexSides == 0) if (board.HexSides == 0)
{ {
int dx = Math.Abs(unit.Position.Value.X - cx); int dx = Math.Abs(unit.Position.Value.X - cx);
@@ -184,21 +216,71 @@ internal static class ScoringFunctions
return score; return score;
} }
// ── Resource ──────────────────────────────────────────────────────────────
/// <summary> /// <summary>
/// Resource = −10 if SP well above typical per-turn gain (penalise hoarding) /// −10 for hoarding SP beyond 3× per-turn gain.
/// + +5 per Sacrifice Token. /// +8 per sacrifice token (raised: tokens are immediately fungible for strong plays).
/// </summary> /// </summary>
private static float ResourceScore(GameState state, PlayerState aiPlayer) private static float ResourceScore(GameState state, PlayerState aiPlayer)
{ {
float score = 0f; float score = 0f;
// Penalise excessive unspent SP.
int spPerTurn = state.Rules.SpAccumulationPerTurn; int spPerTurn = state.Rules.SpAccumulationPerTurn;
if (spPerTurn > 0 && aiPlayer.CurrentSp > spPerTurn * 3) if (spPerTurn > 0 && aiPlayer.CurrentSp > spPerTurn * 3)
score -= 10f; score -= 10f;
// Sacrifice tokens are immediately valuable. score += aiPlayer.CurrentSt * 8f;
score += aiPlayer.CurrentSt * 5f;
return score;
}
// ── Sacrifice opportunity ─────────────────────────────────────────────────
/// <summary>
/// One-step lookahead: if there are sacrificeable units adjacent to the ArchSpirit,
/// check whether the ST gained from sacrificing them would unlock any card in hand
/// that is currently unaffordable. Add that card's estimated value to the score.
///
/// Without this, the greedy engine sees sacrifice as a net negative (lose a unit)
/// and skips it — even though sacrifice + play on the same turn is strongly positive.
/// </summary>
private static float SacrificeOpportunityScore(GameState state, PlayerState aiPlayer)
{
if (!aiPlayer.CanSacrificeSpirit) return 0f;
var archSpirit = aiPlayer.ArchSpirit;
if (archSpirit?.Position is null) return 0f;
// Count units the AI could sacrifice right now.
int sacrificeableCount = 0;
foreach (var fc in aiPlayer.Field.OfType<FieldCard>())
{
if (fc.IsLeader || !fc.CanBeSacrificed || fc.Position is null) continue;
if (fc.Position.Value.IsOrthogonallyAdjacentTo(archSpirit.Position.Value))
sacrificeableCount++;
}
if (sacrificeableCount == 0) return 0f;
float score = 0f;
int projectedSt = aiPlayer.CurrentSt + sacrificeableCount;
foreach (var card in aiPlayer.Hand)
{
// Only cards that are unaffordable now but reachable with sacrifice ST.
if (card.StCost <= aiPlayer.CurrentSt) continue;
if (card.StCost > projectedSt) continue;
if (card.SpCost > aiPlayer.CurrentSp) continue;
float value = card.CardType switch
{
"Spirit" => (card.Damage ?? 0) * 2f + (card.MaxHealth ?? 0) + 10f,
"Terramorpha" => 20f,
_ => 8f,
};
score += value;
}
return score; return score;
} }