fix(ai): prevent greedy engine from freezing after a high-value ability activation

When an ability activation raised the score baseline significantly (e.g.
bouncing a unit, dealing damage, healing), no subsequent move or attack
could beat that new baseline, so the AI ended its turn after just one
action leaving all other units idle.

Two-part fix:
1. Relax the selection threshold by NoRegressTolerance (1.0 pts) so that
   moves/attacks that are near-neutral still qualify as candidates.
2. Track serialised-state hashes for every state seen during the turn;
   candidates that would revisit a prior state are skipped, preventing
   the tolerance from enabling intra-turn cycles.

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-14 06:22:59 -04:00
co-authored by Claude Happy
parent e3df26ae07
commit babf5f9821
@@ -52,6 +52,18 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine
const int maxActions = 50;
int iterations = 0;
// Track serialised-state hashes seen so far this turn to prevent intra-turn
// cycles. Using full JSON hash is cheap enough given we already serialise for
// every candidate evaluation.
var seenStateHashes = new HashSet<int> { HashState(workingState) };
// Allow actions that don't strictly beat the current baseline but are no
// worse than this tolerance. This prevents the AI from freezing after a
// single high-value action (e.g. an ability activation) raises the baseline
// above what any subsequent move/attack can achieve, leaving other units idle.
// Cycle detection (seenStateHashes) ensures the tolerance cannot cause loops.
const float NoRegressTolerance = 1.0f;
while (iterations++ < maxActions)
{
var workingCtx = new AiTurnContext(workingState, ctx.AiAccountId);
@@ -61,17 +73,20 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine
if (candidates.Count == 0) break;
AiAction? bestAction = null;
float bestScore = baseScore;
AiAction? bestAction = null;
float bestScore = baseScore - NoRegressTolerance; // threshold: must not regress beyond tolerance
int bestNewHash = 0;
foreach (var candidate in candidates)
{
var simState = CloneState(workingState);
float score;
int newHash;
try
{
ApplyAction(simState, ctx.AiAccountId, candidate);
score = ScoringFunctions.Evaluate(simState, ctx.AiAccountId, _profile);
newHash = HashState(simState);
score = ScoringFunctions.Evaluate(simState, ctx.AiAccountId, _profile);
}
catch
{
@@ -79,19 +94,26 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine
continue;
}
// Skip actions that would revisit a state already seen this turn.
if (seenStateHashes.Contains(newHash)) continue;
if (score > bestScore)
{
bestScore = score;
bestAction = candidate;
bestScore = score;
bestAction = candidate;
bestNewHash = newHash;
}
}
if (bestAction is null)
break; // no improvement available
break; // no improvement or non-regressive action available
// ── Mistake injection ──────────────────────────────────────────────
if (_profile.MistakeRate > 0 && Random.Shared.NextSingle() < _profile.MistakeRate)
bestAction = candidates[Random.Shared.Next(candidates.Count)];
{
bestAction = candidates[Random.Shared.Next(candidates.Count)];
bestNewHash = 0; // hash unknown for random pick; recomputed below
}
plan.Add(bestAction);
@@ -99,6 +121,8 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine
try { ApplyAction(workingState, ctx.AiAccountId, bestAction); }
catch { break; /* action became invalid — stop the loop */ }
seenStateHashes.Add(bestNewHash != 0 ? bestNewHash : HashState(workingState));
baseScore = ScoringFunctions.Evaluate(workingState, ctx.AiAccountId, _profile);
}
@@ -111,6 +135,9 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine
private static GameState CloneState(GameState state)
=> GameEngine.Deserialize(GameEngine.Serialize(state));
private static int HashState(GameState state)
=> GameEngine.Serialize(state).GetHashCode();
/// <summary>
/// Apply one AiAction directly to a GameState (for simulation purposes).
/// Throws GameException if the action is invalid — callers must handle this.