From a814d2bdb9321ceb3a8b238470f21a117e6f0a8d Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 14 Apr 2026 20:32:03 -0400 Subject: [PATCH] fix(ai): respect TeamIndex for ally/enemy distinction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AiTurnContext.OpponentIds was everyone-except-self, so in team games the AI would attack its own allies and score them as enemy material. The game model already has TeamIndex on PlayerState and GameState.GetEnemyIds() / AreAllies() helpers — the AI just never used them. - AiTurnContext: use state.GetEnemyIds() so OpponentIds excludes allies (fixes attack targeting, threat detection, positional advance) - MaterialScore: use state.AreAllies() so allied units/ArchSpirit score positively instead of as enemy material - PositionalScore: skip allied players when collecting enemy positions Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Server/src/SOR.Ai/Core/AiTurnContext.cs | 4 +--- Server/src/SOR.Ai/Heuristics/ScoringFunctions.cs | 7 ++++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Server/src/SOR.Ai/Core/AiTurnContext.cs b/Server/src/SOR.Ai/Core/AiTurnContext.cs index 269dff0..c8bdbd8 100644 --- a/Server/src/SOR.Ai/Core/AiTurnContext.cs +++ b/Server/src/SOR.Ai/Core/AiTurnContext.cs @@ -14,9 +14,7 @@ public sealed class AiTurnContext State = state; AiAccountId = aiAccountId; AiPlayer = state.Players[aiAccountId]; - OpponentIds = state.Players.Keys - .Where(k => k != aiAccountId && !state.Players[k].IsEliminated) - .ToList(); + OpponentIds = state.GetEnemyIds(aiAccountId).ToList(); Rules = state.Rules; } diff --git a/Server/src/SOR.Ai/Heuristics/ScoringFunctions.cs b/Server/src/SOR.Ai/Heuristics/ScoringFunctions.cs index 316ced2..d39ec85 100644 --- a/Server/src/SOR.Ai/Heuristics/ScoringFunctions.cs +++ b/Server/src/SOR.Ai/Heuristics/ScoringFunctions.cs @@ -52,8 +52,9 @@ internal static class ScoringFunctions { if (player.IsEliminated) continue; - bool isOwn = playerId == aiAccountId; - float sign = isOwn ? 1f : -1f; + // Allies (same TeamIndex) score positively; enemies score negatively. + bool isAlly = state.AreAllies(playerId, aiAccountId); + float sign = isAlly ? 1f : -1f; foreach (var fc in player.Field.OfType()) { @@ -214,7 +215,7 @@ internal static class ScoringFunctions var enemyUnitPositions = new List(); foreach (var (id, p) in state.Players) { - if (id == aiAccountId || p.IsEliminated) continue; + if (p.IsEliminated || state.AreAllies(id, aiAccountId)) continue; if (p.ArchSpirit?.Position.HasValue == true) enemyArchPositions.Add(p.ArchSpirit.Position.Value); foreach (var fc in p.Field.OfType())