From 8c5b65192fe5a77256615c2e219e6fb0293f1e54 Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 26 Feb 2026 16:48:50 -0500 Subject: [PATCH] =?UTF-8?q?Add=20aura=20range=20evaluation=20=E2=80=94=20E?= =?UTF-8?q?nterAuraRange/LeaveAuraRange=20triggers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add EnterAuraRange and LeaveAuraRange to TriggerEventKind enum - Add TriggerMatches cases for OnEnterAuraRangeTrigger / OnLeaveAuraRangeTrigger with AuraOwnership + UnitOwnerMatches (ally/enemy/any) scope filtering - Add EvaluateAuraTransitions (on move), EvaluateAurasOnEnter (on channel), EvaluateAurasOnLeave (on sacrifice/kill) public methods to AbilitySystem - Bidirectional evaluation: moving unit vs. stationary auras + moving unit's own auras vs. stationary units - Hook into GameEngine: MoveUnit, ChannelCard (Spirit), Sacrifice - KillUnit now calls EvaluateAurasOnLeave before board removal Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../src/SOR.Game/Abilities/AbilityContext.cs | 2 + .../src/SOR.Game/Abilities/AbilitySystem.cs | 228 ++++++++++++++++++ Server/src/SOR.Game/GameEngine.cs | 4 + 3 files changed, 234 insertions(+) diff --git a/Server/src/SOR.Game/Abilities/AbilityContext.cs b/Server/src/SOR.Game/Abilities/AbilityContext.cs index 397f99c..e3bf264 100644 --- a/Server/src/SOR.Game/Abilities/AbilityContext.cs +++ b/Server/src/SOR.Game/Abilities/AbilityContext.cs @@ -67,6 +67,8 @@ public enum TriggerEventKind STSpent, CardDrawn, TileAffinityChanged, + EnterAuraRange, + LeaveAuraRange, } /// diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index bba6774..513c6aa 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -697,6 +697,8 @@ public static class AbilitySystem var owner = FindCardOwner(ctx.State, unit.InstanceId); if (owner is null) return; + // Fire leave-aura triggers before removing from board. + EvaluateAurasOnLeave(ctx.State, unit.InstanceId); ctx.State.Board?.RemoveUnit(unit.InstanceId); RemoveFromAllZones(owner, unit, board: null); // board already handled above unit.CurrentHealth = 0; @@ -979,6 +981,14 @@ public static class AbilitySystem occurrence.Kind == TriggerEventKind.Channeled && CardType(t.CardType, occurrence.CardType) && Own(t.Ownership, occurrence, ownerId, state), + OnEnterAuraRangeTrigger eat => + occurrence.Kind == TriggerEventKind.EnterAuraRange + && Own(eat.AuraOwnership, occurrence, ownerId, state) + && UnitOwnerMatches(eat.UnitOwnership, occurrence.Data?.TargetId, state, ownerId), + OnLeaveAuraRangeTrigger lat => + occurrence.Kind == TriggerEventKind.LeaveAuraRange + && Own(lat.AuraOwnership, occurrence, ownerId, state) + && UnitOwnerMatches(lat.UnitOwnership, occurrence.Data?.TargetId, state, ownerId), _ => false, }; @@ -992,6 +1002,21 @@ public static class AbilitySystem _ => true, }; + private static bool UnitOwnerMatches( + string scope, string? unitInstanceId, GameState state, string referenceOwnerId) + { + if (scope.ToLowerInvariant() == "any" || unitInstanceId is null) return true; + foreach (var (ownerId, player) in state.Players) + if (player.Field.Any(c => c.InstanceId == unitInstanceId)) + return scope.ToLowerInvariant() switch + { + "ally" or "owner" => ownerId == referenceOwnerId, + "enemy" => ownerId != referenceOwnerId, + _ => true, + }; + return true; + } + private static bool UnitType(string? required, string? actual) => required is null or "Unit" or "Any" || required == actual; @@ -1253,4 +1278,207 @@ public static class AbilitySystem Source = src, }); } + + // ── Aura range evaluation ───────────────────────────────────────────────── + + /// + /// Fires OnEnterAuraRange / OnLeaveAuraRange triggers when a unit moves from + /// to . + /// Handles both the moving unit crossing into/out of stationary auras, and the + /// moving unit's own auras sweeping over stationary units. + /// Call after updating the unit's position on the board. + /// + public static GameState EvaluateAuraTransitions( + GameState state, + string movingUnitId, + Coord from, + Coord to) + { + // Part 1: stationary aura-bearing units vs. moving unit's position change. + foreach (var (auraOwnerId, player) in state.Players) + foreach (var auraUnit in player.Field.OfType() + .Where(f => f.Position.HasValue && f.InstanceId != movingUnitId).ToList()) + { + foreach (var def in ParseAbilities(auraUnit.AbilitiesJson) + .Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null)) + { + var auraPos = auraUnit.Position!.Value; + int min = def.AuraRange!.Min, max = def.AuraRange.Max; + FireAuraTransition(state, auraOwnerId, auraUnit.InstanceId, movingUnitId, + wasIn: from.ManhattanDistance(auraPos) >= min && from.ManhattanDistance(auraPos) <= max, + isIn: to.ManhattanDistance(auraPos) >= min && to.ManhattanDistance(auraPos) <= max); + } + } + + // Part 2: moving unit's own auras vs. stationary units. + var movingUnit = state.Players.Values + .SelectMany(p => p.Field.OfType()) + .FirstOrDefault(f => f.InstanceId == movingUnitId); + if (movingUnit is null) return state; + + string? movingOwnerId = null; + foreach (var (oid, pl) in state.Players) + if (pl.Field.Any(c => c.InstanceId == movingUnitId)) { movingOwnerId = oid; break; } + if (movingOwnerId is null) return state; + + foreach (var def in ParseAbilities(movingUnit.AbilitiesJson) + .Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null)) + { + int min = def.AuraRange!.Min, max = def.AuraRange.Max; + foreach (var (_, pl) in state.Players) + foreach (var other in pl.Field.OfType() + .Where(f => f.Position.HasValue && f.InstanceId != movingUnitId).ToList()) + { + var otherPos = other.Position!.Value; + FireAuraTransition(state, movingOwnerId, movingUnitId, other.InstanceId, + wasIn: from.ManhattanDistance(otherPos) >= min && from.ManhattanDistance(otherPos) <= max, + isIn: to.ManhattanDistance(otherPos) >= min && to.ManhattanDistance(otherPos) <= max); + } + } + + return state; + } + + /// + /// Fires OnEnterAuraRange triggers for a unit newly placed on the battlefield. + /// Handles both existing auras covering the new unit and the new unit's own auras + /// covering existing units. + /// Call after placing the unit at its initial position. + /// + public static GameState EvaluateAurasOnEnter(GameState state, string newUnitId) + { + var newUnit = state.Players.Values + .SelectMany(p => p.Field.OfType()) + .FirstOrDefault(f => f.InstanceId == newUnitId); + if (newUnit?.Position is null) return state; + var newPos = newUnit.Position.Value; + + // Part 1: existing aura units — check if new unit entered their range. + foreach (var (auraOwnerId, player) in state.Players) + foreach (var auraUnit in player.Field.OfType() + .Where(f => f.Position.HasValue && f.InstanceId != newUnitId).ToList()) + { + foreach (var def in ParseAbilities(auraUnit.AbilitiesJson) + .Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null)) + { + int dist = newPos.ManhattanDistance(auraUnit.Position!.Value); + if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max) + FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.EnterAuraRange, + ActorId = auraOwnerId, + Data = new TriggerEventData { SourceId = auraUnit.InstanceId, TargetId = newUnitId }, + }); + } + } + + // Part 2: new unit's own auras — check all existing units. + string? newUnitOwnerId = null; + foreach (var (oid, pl) in state.Players) + if (pl.Field.Any(c => c.InstanceId == newUnitId)) { newUnitOwnerId = oid; break; } + if (newUnitOwnerId is null) return state; + + foreach (var def in ParseAbilities(newUnit.AbilitiesJson) + .Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null)) + { + foreach (var (_, pl) in state.Players) + foreach (var other in pl.Field.OfType() + .Where(f => f.Position.HasValue && f.InstanceId != newUnitId).ToList()) + { + int dist = newPos.ManhattanDistance(other.Position!.Value); + if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max) + FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.EnterAuraRange, + ActorId = newUnitOwnerId, + Data = new TriggerEventData { SourceId = newUnitId, TargetId = other.InstanceId }, + }); + } + } + + return state; + } + + /// + /// Fires OnLeaveAuraRange triggers for a unit about to leave the battlefield. + /// Handles both existing auras and the leaving unit's own auras. + /// Call before removing the unit from the board. + /// + public static GameState EvaluateAurasOnLeave(GameState state, string leavingUnitId) + { + var leavingUnit = state.Players.Values + .SelectMany(p => p.Field.OfType()) + .FirstOrDefault(f => f.InstanceId == leavingUnitId); + if (leavingUnit?.Position is null) return state; + var leavingPos = leavingUnit.Position.Value; + + // Part 1: existing aura units — check if leaving unit was in their range. + foreach (var (auraOwnerId, player) in state.Players) + foreach (var auraUnit in player.Field.OfType() + .Where(f => f.Position.HasValue && f.InstanceId != leavingUnitId).ToList()) + { + foreach (var def in ParseAbilities(auraUnit.AbilitiesJson) + .Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null)) + { + int dist = leavingPos.ManhattanDistance(auraUnit.Position!.Value); + if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max) + FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.LeaveAuraRange, + ActorId = auraOwnerId, + Data = new TriggerEventData { SourceId = auraUnit.InstanceId, TargetId = leavingUnitId }, + }); + } + } + + // Part 2: leaving unit's own auras — fire LeaveAuraRange for each in-range unit. + string? leavingOwnerId = null; + foreach (var (oid, pl) in state.Players) + if (pl.Field.Any(c => c.InstanceId == leavingUnitId)) { leavingOwnerId = oid; break; } + if (leavingOwnerId is null) return state; + + foreach (var def in ParseAbilities(leavingUnit.AbilitiesJson) + .Where(d => d.Type == AbilityType.Aura && d.AuraRange is not null)) + { + foreach (var (_, pl) in state.Players) + foreach (var other in pl.Field.OfType() + .Where(f => f.Position.HasValue && f.InstanceId != leavingUnitId).ToList()) + { + int dist = leavingPos.ManhattanDistance(other.Position!.Value); + if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max) + FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.LeaveAuraRange, + ActorId = leavingOwnerId, + Data = new TriggerEventData { SourceId = leavingUnitId, TargetId = other.InstanceId }, + }); + } + } + + return state; + } + + private static void FireAuraTransition( + GameState state, + string auraOwnerId, + string auraUnitId, + string targetUnitId, + bool wasIn, + bool isIn) + { + if (!wasIn && isIn) + FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.EnterAuraRange, + ActorId = auraOwnerId, + Data = new TriggerEventData { SourceId = auraUnitId, TargetId = targetUnitId }, + }); + else if (wasIn && !isIn) + FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.LeaveAuraRange, + ActorId = auraOwnerId, + Data = new TriggerEventData { SourceId = auraUnitId, TargetId = targetUnitId }, + }); + } } diff --git a/Server/src/SOR.Game/GameEngine.cs b/Server/src/SOR.Game/GameEngine.cs index 250c0f2..30120fd 100644 --- a/Server/src/SOR.Game/GameEngine.cs +++ b/Server/src/SOR.Game/GameEngine.cs @@ -246,6 +246,7 @@ public static class GameEngine UnitType = "Spirit", Data = new TriggerEventData { SourceId = field.InstanceId }, }); + AbilitySystem.EvaluateAurasOnEnter(state, field.InstanceId); } else if (card.CardType == "Terramorpha") { @@ -496,6 +497,8 @@ public static class GameEngine unit.Position = targetPos; board.MoveUnit(unitInstanceId, targetPos); + AbilitySystem.EvaluateAuraTransitions(state, unitInstanceId, from, targetPos); + AbilitySystem.FireTrigger(state, new TriggerOccurrence { Kind = TriggerEventKind.UnitMove, @@ -638,6 +641,7 @@ public static class GameEngine var stReturn = SacrificeSystem.GetSTReturn(player.CurrentSt, sacValue); player.CurrentSt += stReturn; + AbilitySystem.EvaluateAurasOnLeave(state, unitInstanceId); if (state.Board is not null) state.Board.RemoveUnit(unitInstanceId); player.Field.Remove(unit); player.Discard.Add(unit);