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:
@@ -15,10 +15,10 @@ internal static class AiDifficultyProfiles
|
||||
DisplayName = "Novice",
|
||||
EngineType = "Random",
|
||||
MistakeRate = 1.0f,
|
||||
MinThinkTimeMs = 600,
|
||||
MaxThinkTimeMs = 1400,
|
||||
MinActionDelayMs = 300,
|
||||
MaxActionDelayMs = 700,
|
||||
MinThinkTimeMs = 300,
|
||||
MaxThinkTimeMs = 700,
|
||||
MinActionDelayMs = 150,
|
||||
MaxActionDelayMs = 400,
|
||||
},
|
||||
|
||||
// ── Tier 2: Apprentice (Heuristic, 60% mistakes) ─────────────────────
|
||||
@@ -28,10 +28,10 @@ internal static class AiDifficultyProfiles
|
||||
DisplayName = "Apprentice",
|
||||
EngineType = "Heuristic",
|
||||
MistakeRate = 0.60f,
|
||||
MinThinkTimeMs = 700,
|
||||
MaxThinkTimeMs = 1600,
|
||||
MinActionDelayMs = 400,
|
||||
MaxActionDelayMs = 900,
|
||||
MinThinkTimeMs = 400,
|
||||
MaxThinkTimeMs = 900,
|
||||
MinActionDelayMs = 200,
|
||||
MaxActionDelayMs = 500,
|
||||
MaterialWeight = 1.0f,
|
||||
AffinityControlWeight = 0.5f,
|
||||
ThreatWeight = 0.5f,
|
||||
@@ -46,10 +46,10 @@ internal static class AiDifficultyProfiles
|
||||
DisplayName = "Journeyman",
|
||||
EngineType = "Heuristic",
|
||||
MistakeRate = 0.40f,
|
||||
MinThinkTimeMs = 800,
|
||||
MaxThinkTimeMs = 1800,
|
||||
MinActionDelayMs = 400,
|
||||
MaxActionDelayMs = 900,
|
||||
MinThinkTimeMs = 400,
|
||||
MaxThinkTimeMs = 1000,
|
||||
MinActionDelayMs = 200,
|
||||
MaxActionDelayMs = 500,
|
||||
MaterialWeight = 1.0f,
|
||||
AffinityControlWeight = 0.8f,
|
||||
ThreatWeight = 0.8f,
|
||||
@@ -64,10 +64,10 @@ internal static class AiDifficultyProfiles
|
||||
DisplayName = "Adept",
|
||||
EngineType = "Heuristic",
|
||||
MistakeRate = 0.25f,
|
||||
MinThinkTimeMs = 900,
|
||||
MaxThinkTimeMs = 2000,
|
||||
MinActionDelayMs = 500,
|
||||
MaxActionDelayMs = 1100,
|
||||
MinThinkTimeMs = 500,
|
||||
MaxThinkTimeMs = 1100,
|
||||
MinActionDelayMs = 250,
|
||||
MaxActionDelayMs = 600,
|
||||
MaterialWeight = 1.0f,
|
||||
AffinityControlWeight = 1.0f,
|
||||
ThreatWeight = 1.0f,
|
||||
@@ -82,10 +82,10 @@ internal static class AiDifficultyProfiles
|
||||
DisplayName = "Expert",
|
||||
EngineType = "Heuristic",
|
||||
MistakeRate = 0.10f,
|
||||
MinThinkTimeMs = 1000,
|
||||
MaxThinkTimeMs = 2200,
|
||||
MinActionDelayMs = 600,
|
||||
MaxActionDelayMs = 1200,
|
||||
MinThinkTimeMs = 600,
|
||||
MaxThinkTimeMs = 1300,
|
||||
MinActionDelayMs = 300,
|
||||
MaxActionDelayMs = 700,
|
||||
MaterialWeight = 1.0f,
|
||||
AffinityControlWeight = 1.2f,
|
||||
ThreatWeight = 1.2f,
|
||||
@@ -102,10 +102,10 @@ internal static class AiDifficultyProfiles
|
||||
MistakeRate = 0.05f,
|
||||
MctsTimeBudgetMs = 500,
|
||||
MctsMaxDepth = 3,
|
||||
MinThinkTimeMs = 1200,
|
||||
MaxThinkTimeMs = 2500,
|
||||
MinActionDelayMs = 700,
|
||||
MaxActionDelayMs = 1400,
|
||||
MinThinkTimeMs = 700,
|
||||
MaxThinkTimeMs = 1500,
|
||||
MinActionDelayMs = 400,
|
||||
MaxActionDelayMs = 800,
|
||||
MaterialWeight = 1.2f,
|
||||
AffinityControlWeight = 1.4f,
|
||||
ThreatWeight = 1.4f,
|
||||
@@ -120,10 +120,10 @@ internal static class AiDifficultyProfiles
|
||||
MistakeRate = 0.02f,
|
||||
MctsTimeBudgetMs = 1500,
|
||||
MctsMaxDepth = 5,
|
||||
MinThinkTimeMs = 1400,
|
||||
MaxThinkTimeMs = 3000,
|
||||
MinActionDelayMs = 800,
|
||||
MaxActionDelayMs = 1500,
|
||||
MinThinkTimeMs = 800,
|
||||
MaxThinkTimeMs = 1700,
|
||||
MinActionDelayMs = 450,
|
||||
MaxActionDelayMs = 900,
|
||||
MaterialWeight = 1.3f,
|
||||
AffinityControlWeight = 1.5f,
|
||||
ThreatWeight = 1.5f,
|
||||
@@ -138,10 +138,10 @@ internal static class AiDifficultyProfiles
|
||||
MistakeRate = 0.0f,
|
||||
MctsTimeBudgetMs = 4000,
|
||||
MctsMaxDepth = 7,
|
||||
MinThinkTimeMs = 1500,
|
||||
MaxThinkTimeMs = 3200,
|
||||
MinActionDelayMs = 900,
|
||||
MaxActionDelayMs = 1600,
|
||||
MinThinkTimeMs = 900,
|
||||
MaxThinkTimeMs = 1800,
|
||||
MinActionDelayMs = 500,
|
||||
MaxActionDelayMs = 1000,
|
||||
MaterialWeight = 1.4f,
|
||||
AffinityControlWeight = 1.6f,
|
||||
ThreatWeight = 1.6f,
|
||||
@@ -157,10 +157,10 @@ internal static class AiDifficultyProfiles
|
||||
MistakeRate = 0.0f,
|
||||
MctsTimeBudgetMs = 6000,
|
||||
MctsMaxDepth = 10,
|
||||
MinThinkTimeMs = 1500,
|
||||
MaxThinkTimeMs = 3000,
|
||||
MinActionDelayMs = 900,
|
||||
MaxActionDelayMs = 1600,
|
||||
MinThinkTimeMs = 900,
|
||||
MaxThinkTimeMs = 1800,
|
||||
MinActionDelayMs = 500,
|
||||
MaxActionDelayMs = 1000,
|
||||
MaterialWeight = 1.5f,
|
||||
AffinityControlWeight = 1.8f,
|
||||
ThreatWeight = 1.8f,
|
||||
@@ -175,10 +175,10 @@ internal static class AiDifficultyProfiles
|
||||
MistakeRate = 0.0f,
|
||||
MctsTimeBudgetMs = 10000,
|
||||
MctsMaxDepth = 15,
|
||||
MinThinkTimeMs = 1500,
|
||||
MaxThinkTimeMs = 3000,
|
||||
MinActionDelayMs = 1000,
|
||||
MaxActionDelayMs = 1800,
|
||||
MinThinkTimeMs = 1000,
|
||||
MaxThinkTimeMs = 2000,
|
||||
MinActionDelayMs = 600,
|
||||
MaxActionDelayMs = 1100,
|
||||
MaterialWeight = 1.6f,
|
||||
AffinityControlWeight = 2.0f,
|
||||
ThreatWeight = 2.0f,
|
||||
|
||||
@@ -9,45 +9,40 @@ using SOR.Game.Grid;
|
||||
/// <summary>
|
||||
/// Board evaluation function for the heuristic decision engine.
|
||||
///
|
||||
/// Score = Σ (weight × feature) using the formula from the architecture plan §5.2.
|
||||
/// Higher = better for the AI. Weights come from AiDifficultyProfile so that
|
||||
/// different difficulty/strategy combinations produce different behaviour without
|
||||
/// any code changes.
|
||||
/// Score = Σ (weight × feature). Higher = better for the AI.
|
||||
/// Weights come from AiDifficultyProfile so different difficulties/strategies
|
||||
/// produce different behaviour without code changes.
|
||||
/// </summary>
|
||||
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(
|
||||
GameState state,
|
||||
string aiAccountId,
|
||||
GameState state,
|
||||
string aiAccountId,
|
||||
AiDifficultyProfile profile)
|
||||
{
|
||||
if (!state.Players.TryGetValue(aiAccountId, out var aiPlayer))
|
||||
return 0f;
|
||||
|
||||
float material = MaterialScore(state, aiAccountId);
|
||||
float affinityControl = AffinityControlScore(state, aiAccountId, aiPlayer);
|
||||
float threat = ThreatScore(state, aiAccountId, aiPlayer);
|
||||
float positional = PositionalScore(state, aiAccountId, aiPlayer);
|
||||
float resource = ResourceScore(state, aiPlayer);
|
||||
float material = MaterialScore(state, aiAccountId);
|
||||
float affinityControl = AffinityControlScore(state, aiAccountId, aiPlayer);
|
||||
float threat = ThreatScore(state, aiAccountId, aiPlayer);
|
||||
float positional = PositionalScore(state, aiAccountId, aiPlayer);
|
||||
float resource = ResourceScore(state, aiPlayer);
|
||||
float sacrificeOpportunity = SacrificeOpportunityScore(state, aiPlayer);
|
||||
|
||||
return material * profile.MaterialWeight
|
||||
+ affinityControl * profile.AffinityControlWeight
|
||||
+ threat * profile.ThreatWeight
|
||||
+ positional * profile.PositionalWeight
|
||||
+ resource * profile.ResourceWeight;
|
||||
return material * profile.MaterialWeight
|
||||
+ affinityControl * profile.AffinityControlWeight
|
||||
+ threat * profile.ThreatWeight
|
||||
+ positional * profile.PositionalWeight
|
||||
+ resource * profile.ResourceWeight
|
||||
+ sacrificeOpportunity * profile.ResourceWeight; // same weight as resource
|
||||
}
|
||||
|
||||
// ── Feature functions ──────────────────────────────────────────────────────
|
||||
// ── Material ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Material = own unit value − opponent unit value + ArchSpirit HP ratio bonus.
|
||||
///
|
||||
/// Unit value = Damage × 2 + MaxHealth (roughly captures combat power).
|
||||
/// ArchSpirit HP ratio × 500 heavily rewards keeping your ArchSpirit healthy.
|
||||
/// own unit power − opponent unit power + ArchSpirit HP ratio × 500.
|
||||
/// ArchSpirit HP dominates: keeping yours alive and threatening theirs are top priorities.
|
||||
/// </summary>
|
||||
private static float MaterialScore(GameState state, string aiAccountId)
|
||||
{
|
||||
@@ -57,38 +52,37 @@ internal static class ScoringFunctions
|
||||
{
|
||||
if (player.IsEliminated) continue;
|
||||
|
||||
bool isOwn = playerId == aiAccountId;
|
||||
float sign = isOwn ? 1f : -1f;
|
||||
bool isOwn = playerId == aiAccountId;
|
||||
float sign = isOwn ? 1f : -1f;
|
||||
|
||||
// Field units.
|
||||
foreach (var fc in player.Field.OfType<FieldCard>())
|
||||
{
|
||||
if (fc.IsLeader) continue; // ArchSpirit counted separately.
|
||||
float value = (fc.Damage ?? 0) * 2f + (fc.MaxHealth ?? 0);
|
||||
score += sign * value;
|
||||
if (fc.IsLeader) continue;
|
||||
score += sign * ((fc.Damage ?? 0) * 2f + (fc.MaxHealth ?? 0));
|
||||
}
|
||||
|
||||
// ArchSpirit HP ratio.
|
||||
var arch = player.ArchSpirit;
|
||||
if (arch is not null)
|
||||
{
|
||||
float maxHp = arch.MaxHealth ?? 1;
|
||||
float curHp = arch.CurrentHealth ?? 0;
|
||||
float ratio = maxHp > 0 ? curHp / maxHp : 0f;
|
||||
|
||||
if (isOwn)
|
||||
score += ratio * 500f;
|
||||
else
|
||||
score -= ratio * 500f; // destroying opponent ArchSpirit is very good
|
||||
score += sign * (ratio * 500f);
|
||||
}
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
// ── Affinity control + Terramorpha chaining ───────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Affinity control = tiles with own ArchSpirit's affinity within channeling range × 3.
|
||||
/// Rewards building a "pool" of valid channeling tiles.
|
||||
/// Coverage = number of affinity-matching tiles within ArchSpirit channeling range × 3.
|
||||
/// 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>
|
||||
private static float AffinityControlScore(
|
||||
GameState state, string aiAccountId, PlayerState aiPlayer)
|
||||
@@ -97,14 +91,13 @@ internal static class ScoringFunctions
|
||||
var archSpirit = aiPlayer.ArchSpirit;
|
||||
|
||||
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 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)
|
||||
{
|
||||
var dist = board.HexSides > 0
|
||||
@@ -113,28 +106,72 @@ internal static class ScoringFunctions
|
||||
if (dist > maxRange) continue;
|
||||
|
||||
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>
|
||||
/// Threat = −30 per opponent unit that can reach the AI's ArchSpirit.
|
||||
/// A strong deterrent against leaving the ArchSpirit undefended.
|
||||
/// Base penalty of −30 per enemy unit that can reach the AI's ArchSpirit.
|
||||
/// Scaled up sharply when the ArchSpirit is damaged:
|
||||
/// HP < 50% → additional −40 per threat
|
||||
/// HP < 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>
|
||||
private static float ThreatScore(
|
||||
GameState state, string aiAccountId, PlayerState aiPlayer)
|
||||
{
|
||||
var ctx = new AiTurnContext(state, aiAccountId);
|
||||
int threats = ThreatDetector.CountThreatsToArchSpirit(ctx);
|
||||
return -threats * 30f;
|
||||
var ctx = new AiTurnContext(state, aiAccountId);
|
||||
int threats = ThreatDetector.CountThreatsToArchSpirit(ctx);
|
||||
|
||||
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>
|
||||
/// Positional = +15 per own unit adjacent to any enemy ArchSpirit
|
||||
/// + +2 per own unit in the center 3×3 zone of the board.
|
||||
/// Rewards pressure and board control.
|
||||
/// +15 per own unit adjacent to any enemy ArchSpirit (pressure).
|
||||
/// +2 per own unit in the board centre (control).
|
||||
/// </summary>
|
||||
private static float PositionalScore(
|
||||
GameState state, string aiAccountId, PlayerState aiPlayer)
|
||||
@@ -143,13 +180,10 @@ internal static class ScoringFunctions
|
||||
if (board is null) return 0f;
|
||||
|
||||
float score = 0f;
|
||||
|
||||
// Center zone: tiles within ⌊board/3⌋ of center.
|
||||
int cx = board.Width / 2;
|
||||
int cy = board.Height / 2;
|
||||
int centerRadius = Math.Max(1, Math.Min(board.Width, board.Height) / 3);
|
||||
|
||||
// Collect enemy ArchSpirit positions.
|
||||
var enemyArchPositions = new List<Coord>();
|
||||
foreach (var (id, p) in state.Players)
|
||||
{
|
||||
@@ -162,7 +196,6 @@ internal static class ScoringFunctions
|
||||
{
|
||||
if (unit.Position is null) continue;
|
||||
|
||||
// Adjacent to enemy ArchSpirit.
|
||||
foreach (var eap in enemyArchPositions)
|
||||
{
|
||||
bool adjacent = board.HexSides > 0
|
||||
@@ -171,7 +204,6 @@ internal static class ScoringFunctions
|
||||
if (adjacent) { score += 15f; break; }
|
||||
}
|
||||
|
||||
// Center control (square grid only for simplicity).
|
||||
if (board.HexSides == 0)
|
||||
{
|
||||
int dx = Math.Abs(unit.Position.Value.X - cx);
|
||||
@@ -184,21 +216,71 @@ internal static class ScoringFunctions
|
||||
return score;
|
||||
}
|
||||
|
||||
// ── Resource ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Resource = −10 if SP well above typical per-turn gain (penalise hoarding)
|
||||
/// + +5 per Sacrifice Token.
|
||||
/// −10 for hoarding SP beyond 3× per-turn gain.
|
||||
/// +8 per sacrifice token (raised: tokens are immediately fungible for strong plays).
|
||||
/// </summary>
|
||||
private static float ResourceScore(GameState state, PlayerState aiPlayer)
|
||||
{
|
||||
float score = 0f;
|
||||
|
||||
// Penalise excessive unspent SP.
|
||||
int spPerTurn = state.Rules.SpAccumulationPerTurn;
|
||||
if (spPerTurn > 0 && aiPlayer.CurrentSp > spPerTurn * 3)
|
||||
score -= 10f;
|
||||
|
||||
// Sacrifice tokens are immediately valuable.
|
||||
score += aiPlayer.CurrentSt * 5f;
|
||||
score += aiPlayer.CurrentSt * 8f;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user