diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index 4b215e4..1a87c2c 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -358,10 +358,13 @@ public static class AbilitySystem AbilityContext ctx, IReadOnlyList? clientTargets, bool requireClientTargets = false, - AbilityRange? activationRange = null) + AbilityRange? activationRange = null, + HashSet? restrictTargets = null, + DurationModel? forceDuration = null) { for (int i = 0; i < actions.Length; i++) - ExecuteSingleAction(actions[i], ctx, clientTargets, actionIndex: i, requireClientTargets, activationRange); + ExecuteSingleAction(actions[i], ctx, clientTargets, actionIndex: i, + requireClientTargets, activationRange, restrictTargets, forceDuration); } private static void ExecuteSingleAction( @@ -370,20 +373,38 @@ public static class AbilitySystem IReadOnlyList? clientTargets, int actionIndex, bool requireClientTargets = false, - AbilityRange? activationRange = null) + AbilityRange? activationRange = null, + HashSet? restrictTargets = null, + DurationModel? forceDuration = null) { + // Source-level conditions (Subject != "Target"): gate the whole action. if (action.Conditions is { Length: > 0 }) foreach (var cond in action.Conditions) - if (!EvaluateCondition(cond, ctx, ctx.Source!)) + if (cond.Subject != "Target" && !EvaluateCondition(cond, ctx, ctx.Source!)) return; var resolved = ResolveTargets( - action.Target, ctx, clientTargets, actionIndex, requireClientTargets, activationRange); + action.Target, ctx, clientTargets, actionIndex, requireClientTargets, activationRange, restrictTargets); ctx.ResolvedTargets[actionIndex] = resolved; + // Target-level conditions (Subject == "Target"): filter per target. + var targetConds = action.Conditions?.Where(c => c.Subject == "Target").ToArray(); + var duration = forceDuration ?? action.Duration; + foreach (var target in resolved) + { + if (targetConds is { Length: > 0 }) + { + // Temporarily expose current target so EvaluateCondition resolves "Target" correctly. + ctx.ResolvedTargets[actionIndex] = [target]; + bool passes = targetConds.All(c => EvaluateCondition(c, ctx, target)); + ctx.ResolvedTargets[actionIndex] = resolved; + if (!passes) continue; + } + foreach (var effect in action.Effects) - ExecuteEffect(effect, target, action.Duration, ctx); + ExecuteEffect(effect, target, duration, ctx); + } } // ── Target resolution ───────────────────────────────────────────────────── @@ -394,7 +415,8 @@ public static class AbilitySystem IReadOnlyList? clientTargets, int actionIndex, bool requireClientTargets = false, - AbilityRange? activationRange = null) + AbilityRange? activationRange = null, + HashSet? restrictTargets = null) { var state = ctx.State; @@ -428,6 +450,10 @@ public static class AbilitySystem if (selector.Ownership == TargetOwnership.ExactSelf && ctx.Source is not null) candidates = candidates.Where(c => c.InstanceId == ctx.Source.InstanceId).ToList(); + // Passive apply-to-new-unit: restrict to a specific subset of candidates. + if (restrictTargets is not null) + candidates = candidates.Where(c => restrictTargets.Contains(c.InstanceId)).ToList(); + if (selector.Filters is { Length: > 0 }) candidates = candidates.Where(c => FiltersMatch(selector.Filters, c, ctx)).ToList(); @@ -987,12 +1013,20 @@ public static class AbilitySystem Data = new TriggerEventData { SourceId = fc.InstanceId }, }); EvaluateAurasOnEnter(ctx.State, fc.InstanceId); + ApplyPassivesForSource(ctx.State, fc.InstanceId, ownerPs.AccountId); + ApplyPassivesToNewTarget(ctx.State, fc.InstanceId); break; } case SendToDiscardEffect when target is CardInstance ci: { var owner = FindCardOwner(ctx.State, ci.InstanceId); - if (owner is not null) { RemoveFromAllZones(owner, ci, ctx.State.Board); owner.Discard.Add(ci); } + if (owner is not null) + { + if (owner.Field.Any(f => f.InstanceId == ci.InstanceId)) + RevokePassivesForSource(ctx.State, ci.InstanceId); + RemoveFromAllZones(owner, ci, ctx.State.Board); + owner.Discard.Add(ci); + } break; } case SearchDeckEffect sde: @@ -1140,6 +1174,8 @@ public static class AbilitySystem Data = new TriggerEventData { SourceId = tokenField.InstanceId }, }); EvaluateAurasOnEnter(ctx.State, tokenField.InstanceId); + ApplyPassivesForSource(ctx.State, tokenField.InstanceId, ctx.ActorId); + ApplyPassivesToNewTarget(ctx.State, tokenField.InstanceId); break; } @@ -1254,6 +1290,7 @@ public static class AbilitySystem // Fire leave-aura triggers before removing from board. EvaluateAurasOnLeave(ctx.State, unit.InstanceId); + RevokePassivesForSource(ctx.State, unit.InstanceId); if (unit.CardType == "Ruin") ctx.State.Board?.RemoveRuin(unit.InstanceId); else @@ -1879,6 +1916,75 @@ public static class AbilitySystem }); } + // ── Passive ability application / revocation ───────────────────────────── + + /// + /// Executes all abilities for + /// against all current battlefield units. Effects are tracked with + /// so they are reversed when the source dies + /// or leaves. Call after placing the unit on the board. + /// + public static void ApplyPassivesForSource(GameState state, string sourceUnitId, string ownerId) + { + var source = state.Players.Values + .SelectMany(p => p.Field.OfType()) + .FirstOrDefault(f => f.InstanceId == sourceUnitId); + if (source is null) return; + + var ctx = new AbilityContext { State = state, ActorId = ownerId, Source = source }; + var duration = new DurationModel { Type = DurationType.WhileSourceOnBattlefield }; + foreach (var def in ParseAbilities(source.AbilitiesJson).Where(d => d.Type == AbilityType.Passive)) + ExecuteActions(def.Actions, ctx, clientTargets: null, forceDuration: duration); + } + + /// + /// Applies all existing passive abilities from every battlefield unit to . + /// Restricts targeting to the new unit so already-buffed units are not double-applied. + /// Call after for the new unit. + /// + public static void ApplyPassivesToNewTarget(GameState state, string newUnitId) + { + var restrict = new HashSet { newUnitId }; + var duration = new DurationModel { Type = DurationType.WhileSourceOnBattlefield }; + foreach (var (ownerId, player) in state.Players) + foreach (var sourceUnit in player.Field.OfType() + .Where(f => f.InstanceId != newUnitId).ToList()) + { + var ctx = new AbilityContext { State = state, ActorId = ownerId, Source = sourceUnit }; + foreach (var def in ParseAbilities(sourceUnit.AbilitiesJson).Where(d => d.Type == AbilityType.Passive)) + ExecuteActions(def.Actions, ctx, clientTargets: null, + restrictTargets: restrict, forceDuration: duration); + } + } + + /// + /// Reverses all active effects whose + /// source is . Call before removing a unit from the board. + /// + public static void RevokePassivesForSource(GameState state, string sourceUnitId) + { + var toRevoke = state.ActiveEffects + .Where(ae => ae.SourceInstanceId == sourceUnitId) + .Where(ae => + { + try + { + var td = JsonSerializer.Deserialize(ae.DurationJson, _jsonOpts); + return td?.Duration?.Type == DurationType.WhileSourceOnBattlefield; + } + catch { return false; } + }) + .ToList(); + + foreach (var ae in toRevoke) + { + TrackedDuration? td = null; + try { td = JsonSerializer.Deserialize(ae.DurationJson, _jsonOpts); } catch { } + if (td is not null) ApplyReversal(state, ae, td.OwnerAccountId); + state.ActiveEffects.Remove(ae); + } + } + // ── Aura range evaluation ───────────────────────────────────────────────── /// diff --git a/Server/src/SOR.Game/Abilities/Models/DurationModel.cs b/Server/src/SOR.Game/Abilities/Models/DurationModel.cs index 1c4e765..0d7db9b 100644 --- a/Server/src/SOR.Game/Abilities/Models/DurationModel.cs +++ b/Server/src/SOR.Game/Abilities/Models/DurationModel.cs @@ -15,6 +15,8 @@ public enum DurationType ForNTurns, /// Active while a condition evaluates to true; expires when it becomes false. WhileCondition, + /// Active while the ability's source unit is on the battlefield; revoked on leave/death. + WhileSourceOnBattlefield, /// Persists for the entire match; never expires. PermanentInMatch, } diff --git a/Server/src/SOR.Game/GameEngine.cs b/Server/src/SOR.Game/GameEngine.cs index be6c604..56d7e97 100644 --- a/Server/src/SOR.Game/GameEngine.cs +++ b/Server/src/SOR.Game/GameEngine.cs @@ -281,6 +281,8 @@ public static class GameEngine Data = new TriggerEventData { SourceId = field.InstanceId }, }); AbilitySystem.EvaluateAurasOnEnter(state, field.InstanceId); + AbilitySystem.ApplyPassivesForSource(state, field.InstanceId, actorId); + AbilitySystem.ApplyPassivesToNewTarget(state, field.InstanceId); } else if (card.CardType == "Terramorpha") { @@ -483,6 +485,7 @@ public static class GameEngine // Remove current from board; fire aura leave before removal. AbilitySystem.EvaluateAurasOnLeave(state, current.InstanceId); + AbilitySystem.RevokePassivesForSource(state, current.InstanceId); board.RemoveUnit(current.InstanceId); player.Discard.Add(current); @@ -490,6 +493,8 @@ public static class GameEngine player.ArchSpirit = newArchSpirit; board.PlaceUnit(coord, newArchSpirit.InstanceId, actorId); AbilitySystem.EvaluateAurasOnEnter(state, newArchSpirit.InstanceId); + AbilitySystem.ApplyPassivesForSource(state, newArchSpirit.InstanceId, actorId); + AbilitySystem.ApplyPassivesToNewTarget(state, newArchSpirit.InstanceId); AbilitySystem.FireTrigger(state, new TriggerOccurrence {