diff --git a/Server/src/SOR.Game/Abilities/AbilityContext.cs b/Server/src/SOR.Game/Abilities/AbilityContext.cs index ebdac81..d0d5867 100644 --- a/Server/src/SOR.Game/Abilities/AbilityContext.cs +++ b/Server/src/SOR.Game/Abilities/AbilityContext.cs @@ -39,6 +39,23 @@ public sealed class AbilityContext /// Available to ExecuteEffect when it needs to store a continuation pause. /// public AbilityRange? ActivationRange { get; set; } + + // ── Intra-action position tracking (set per-loop-iteration by ExecuteSingleAction) ── + + /// Index of the currently-executing effect within . + public int CurrentEffectIndex { get; set; } + + /// Index of the currently-executing target within the resolved target list. + public int CurrentTargetIndex { get; set; } + + /// Snapshot of all resolved targets for the current action. + public object[]? CurrentActionTargets { get; set; } + + /// All effects in the current action (the full Effects array, not just remaining). + public Effect[]? CurrentActionAllEffects { get; set; } + + /// Resolved duration for the current action (forceDuration ?? action.Duration). + public DurationModel? CurrentActionDuration { get; set; } } /// diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index d578ac0..b603bf7 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -1149,22 +1149,30 @@ public static class AbilitySystem ps.Deck.AddRange(top); } - // Execute any continuation actions stored at pause time. + // Continue execution after the chooser submits their ordering. var rrcSource = state.Players.Values .SelectMany(p => p.Field).OfType() .FirstOrDefault(f => f.InstanceId == pending.SourceInstanceId); var rrcCtx = new AbilityContext { State = state, ActorId = pending.ActorId, Source = rrcSource }; - var rrcContinuation = JsonSerializer.Deserialize( - pending.ContinuationActionsJson, _jsonOpts) ?? []; - if (rrcContinuation.Length > 0) + DurationModel? rrcActionDuration = pending.ForceDurationJson is not null + ? JsonSerializer.Deserialize(pending.ForceDurationJson, _jsonOpts) : null; + + // Apply remaining effects within the same action. + ApplyIntraActionContinuation(pending.IntraActionContinuationJson, rrcActionDuration, rrcCtx); + + // Execute outer continuation actions. + if (!rrcCtx.State.HasPendingInteraction()) { - AbilityRange? rrcRange = pending.RangeJson is not null - ? JsonSerializer.Deserialize(pending.RangeJson, _jsonOpts) : null; - DurationModel? rrcForceDuration = pending.ForceDurationJson is not null - ? JsonSerializer.Deserialize(pending.ForceDurationJson, _jsonOpts) : null; - ExecuteActions(rrcContinuation, rrcCtx, clientTargets: null, activationRange: rrcRange, - forceDuration: rrcForceDuration); + var rrcContinuation = JsonSerializer.Deserialize( + pending.ContinuationActionsJson, _jsonOpts) ?? []; + if (rrcContinuation.Length > 0) + { + AbilityRange? rrcRange = pending.RangeJson is not null + ? JsonSerializer.Deserialize(pending.RangeJson, _jsonOpts) : null; + ExecuteActions(rrcContinuation, rrcCtx, clientTargets: null, activationRange: rrcRange, + forceDuration: rrcActionDuration); + } } return state; @@ -1302,17 +1310,23 @@ public static class AbilitySystem } } - // Execute any continuation actions stored at pause time. - var continuation = JsonSerializer.Deserialize( - pending.ContinuationActionsJson, _jsonOpts) ?? []; - if (continuation.Length > 0) + // Apply remaining effects within the same action before the outer continuation. + DurationModel? tpActionDuration = pending.ForceDurationJson is not null + ? JsonSerializer.Deserialize(pending.ForceDurationJson, _jsonOpts) : null; + ApplyIntraActionContinuation(pending.IntraActionContinuationJson, tpActionDuration, ctx); + + // Execute outer continuation actions. + if (!ctx.State.HasPendingInteraction()) { - AbilityRange? range = pending.RangeJson is not null - ? JsonSerializer.Deserialize(pending.RangeJson, _jsonOpts) : null; - DurationModel? forceDuration = pending.ForceDurationJson is not null - ? JsonSerializer.Deserialize(pending.ForceDurationJson, _jsonOpts) : null; - ExecuteActions(continuation, ctx, clientTargets: null, activationRange: range, - forceDuration: forceDuration); + var continuation = JsonSerializer.Deserialize( + pending.ContinuationActionsJson, _jsonOpts) ?? []; + if (continuation.Length > 0) + { + AbilityRange? range = pending.RangeJson is not null + ? JsonSerializer.Deserialize(pending.RangeJson, _jsonOpts) : null; + ExecuteActions(continuation, ctx, clientTargets: null, activationRange: range, + forceDuration: tpActionDuration); + } } return state; } @@ -1374,6 +1388,8 @@ public static class AbilitySystem if (ppCcze?.TargetTile == true) { // Chain into a tile pick for the chosen dest player. + // Forward IntraActionContinuationJson so the tile-pick resume + // continues with the same intra-action remainder. state.PendingTilePick = new PendingTilePick { ActorId = pending.ActorId, @@ -1384,6 +1400,7 @@ public static class AbilitySystem DestPlayerAccountId = pickedPlayerId, RequireEmpty = true, EffectJson = pending.EffectJson, + IntraActionContinuationJson = pending.IntraActionContinuationJson, ContinuationActionsJson = pending.ContinuationActionsJson, RangeJson = pending.RangeJson, ForceDurationJson = pending.ForceDurationJson, @@ -1469,16 +1486,23 @@ public static class AbilitySystem } } - var ppContinuation = JsonSerializer.Deserialize( - pending.ContinuationActionsJson, _jsonOpts) ?? []; - if (ppContinuation.Length > 0) + DurationModel? ppActionDuration = pending.ForceDurationJson is not null + ? JsonSerializer.Deserialize(pending.ForceDurationJson, _jsonOpts) : null; + + // Apply remaining effects within the same action before the outer continuation. + ApplyIntraActionContinuation(pending.IntraActionContinuationJson, ppActionDuration, ppCtx); + + if (!ppCtx.State.HasPendingInteraction()) { - AbilityRange? ppRange = pending.RangeJson is not null - ? JsonSerializer.Deserialize(pending.RangeJson, _jsonOpts) : null; - DurationModel? ppForceDuration = pending.ForceDurationJson is not null - ? JsonSerializer.Deserialize(pending.ForceDurationJson, _jsonOpts) : null; - ExecuteActions(ppContinuation, ppCtx, clientTargets: null, activationRange: ppRange, - forceDuration: ppForceDuration); + var ppContinuation = JsonSerializer.Deserialize( + pending.ContinuationActionsJson, _jsonOpts) ?? []; + if (ppContinuation.Length > 0) + { + AbilityRange? ppRange = pending.RangeJson is not null + ? JsonSerializer.Deserialize(pending.RangeJson, _jsonOpts) : null; + ExecuteActions(ppContinuation, ppCtx, clientTargets: null, activationRange: ppRange, + forceDuration: ppActionDuration); + } } return state; } @@ -1674,8 +1698,18 @@ public static class AbilitySystem var targetConds = action.Conditions?.Where(c => c.Subject == "Target").ToArray(); var duration = forceDuration ?? action.Duration; - foreach (var target in resolved) + // Snapshot all targets and effects for intra-action continuation capture. + ctx.CurrentActionAllEffects = action.Effects; + ctx.CurrentActionTargets = [.. resolved]; + ctx.CurrentActionDuration = duration; + ctx.ContinuationActions = continuationActions; + ctx.ActivationRange = activationRange; + + for (int ti = 0; ti < resolved.Count; ti++) { + var target = resolved[ti]; + ctx.CurrentTargetIndex = ti; + if (targetConds is { Length: > 0 }) { // Temporarily expose current target so EvaluateCondition resolves "Target" correctly. @@ -1685,19 +1719,10 @@ public static class AbilitySystem if (!passes) continue; } - // Expose continuation context so effect-level pauses can store it. - // DESIGN CONSTRAINT: A pausing effect (e.g. CloneSpiritAsToken with TargetTile, - // ChannelCardFromZone with TargetTile/Choice) must be the LAST effect in its - // action's Effects list. Any effects after a pausing one will be silently skipped - // because the outer loop returns on the first HasPendingInteraction() hit. - // Similarly, remaining targets in the same action are also dropped. - // This is an intentional trade-off: pausing effects are semantically terminal. - ctx.ContinuationActions = continuationActions; - ctx.ActivationRange = activationRange; - - foreach (var effect in action.Effects) + for (int ei = 0; ei < action.Effects.Length; ei++) { - ExecuteEffect(effect, target, duration, ctx); + ctx.CurrentEffectIndex = ei; + ExecuteEffect(action.Effects[ei], target, duration, ctx); // Stop as soon as an effect sets an interactive pause. if (ctx.State.HasPendingInteraction()) return; } @@ -3001,6 +3026,7 @@ public static class AbilitySystem TargetInstanceId = ciCcze.InstanceId, ValidPlayerIds = ctx.State.Players.Keys.ToList(), EffectJson = JsonSerializer.Serialize((Effect)ccze, _jsonOpts), + IntraActionContinuationJson = BuildIntraActionContinuationJson(ciCcze, ctx), ContinuationActionsJson = JsonSerializer.Serialize(ctx.ContinuationActions, _jsonOpts), RangeJson = ctx.ActivationRange is not null ? JsonSerializer.Serialize(ctx.ActivationRange, _jsonOpts) : null, @@ -3025,6 +3051,7 @@ public static class AbilitySystem DestPlayerAccountId = ccDestPlayer.AccountId, RequireEmpty = true, EffectJson = JsonSerializer.Serialize((Effect)ccze, _jsonOpts), + IntraActionContinuationJson = BuildIntraActionContinuationJson(ciCcze, ctx), ContinuationActionsJson = JsonSerializer.Serialize(ctx.ContinuationActions, _jsonOpts), RangeJson = ctx.ActivationRange is not null ? JsonSerializer.Serialize(ctx.ActivationRange, _jsonOpts) : null, @@ -3145,6 +3172,12 @@ public static class AbilitySystem EffectType = "CloneSpiritAsToken", TargetInstanceId = spiritToClone.InstanceId, RequireEmpty = true, + IntraActionContinuationJson = BuildIntraActionContinuationJson(spiritToClone, ctx), + ContinuationActionsJson = JsonSerializer.Serialize(ctx.ContinuationActions, _jsonOpts), + RangeJson = ctx.ActivationRange is not null + ? JsonSerializer.Serialize(ctx.ActivationRange, _jsonOpts) : null, + ForceDurationJson = duration is not null + ? JsonSerializer.Serialize(duration, _jsonOpts) : null, }; } break; @@ -3226,8 +3259,7 @@ public static class AbilitySystem RevealedCardsJson = JsonSerializer.Serialize(revealed, _jsonOpts), Count = n, TargetPlayerAccountId = stackPs.AccountId, - // Preserve the rest of the ability sequence so ResumeRevealChoicePick can - // continue execution after the chooser submits their ordering. + IntraActionContinuationJson = BuildIntraActionContinuationJson(stackPs, ctx), ContinuationActionsJson = JsonSerializer.Serialize(ctx.ContinuationActions, _jsonOpts), RangeJson = ctx.ActivationRange is not null ? JsonSerializer.Serialize(ctx.ActivationRange, _jsonOpts) : null, @@ -4401,6 +4433,134 @@ public static class AbilitySystem private static ValueExpr V(int n) => new() { Fixed = n }; + // ── Intra-action continuation helpers ───────────────────────────────────── + + /// + /// One step in an intra-action continuation: apply to the + /// unit/player identified by (instanceId or accountId). + /// + private sealed class IntraActionStep + { + public string TargetRef { get; set; } = string.Empty; + public string EffectsJson { get; set; } = "[]"; + } + + /// + /// Converts a resolved target object to a stable string reference used in + /// and . + /// + private static string GetTargetRef(object target) => target switch + { + FieldCard fc => fc.InstanceId, + CardInstance ci => ci.InstanceId, + PlayerState ps => ps.AccountId, + Coord c => $"tile:{c.X},{c.Y}", + _ => string.Empty, + }; + + /// + /// Finds a target in by the string ref produced by + /// . Returns a , , + /// or boxed as object; null if not found. + /// + private static object? FindTargetByRef(GameState state, string targetRef) + { + if (string.IsNullOrEmpty(targetRef)) return null; + if (state.Players.TryGetValue(targetRef, out var ps)) return ps; + foreach (var p in state.Players.Values) + { + var c = (CardInstance?)( + p.Field.FirstOrDefault(c => c.InstanceId == targetRef) + ?? p.Hand.FirstOrDefault(c => c.InstanceId == targetRef) + ?? p.Deck.FirstOrDefault(c => c.InstanceId == targetRef) + ?? (CardInstance?)p.Discard.FirstOrDefault(c => c.InstanceId == targetRef)); + if (c is not null) return c; + if (p.ArchSpirit?.InstanceId == targetRef) return p.ArchSpirit; + } + if (targetRef.StartsWith("tile:")) + { + var parts = targetRef[5..].Split(','); + if (parts.Length == 2 + && int.TryParse(parts[0], out var tx) + && int.TryParse(parts[1], out var ty)) + return new Coord(tx, ty); + } + return null; + } + + /// + /// Builds the IntraActionContinuationJson stored in a pause model when an effect + /// pauses mid-action. Captures: + /// + /// remaining effects on the current target (effects after the pausing one) + /// all effects applied to each remaining target (targets after the current one) + /// + /// Returns null when there is nothing left within the current action. + /// + private static string? BuildIntraActionContinuationJson(object currentTarget, AbilityContext ctx) + { + var steps = new List(); + + // Remaining effects on the current target. + if (ctx.CurrentActionAllEffects is { } allEffects && ctx.CurrentEffectIndex + 1 < allEffects.Length) + { + var remEffects = allEffects[(ctx.CurrentEffectIndex + 1)..]; + steps.Add(new IntraActionStep + { + TargetRef = GetTargetRef(currentTarget), + EffectsJson = JsonSerializer.Serialize(remEffects, _jsonOpts), + }); + } + + // All effects on every remaining target. + if (ctx.CurrentActionTargets is { } allTargets && ctx.CurrentActionAllEffects is { } allEff2) + { + for (int rt = ctx.CurrentTargetIndex + 1; rt < allTargets.Length; rt++) + { + steps.Add(new IntraActionStep + { + TargetRef = GetTargetRef(allTargets[rt]), + EffectsJson = JsonSerializer.Serialize(allEff2, _jsonOpts), + }); + } + } + + return steps.Count > 0 ? JsonSerializer.Serialize(steps, _jsonOpts) : null; + } + + /// + /// Executes the intra-action continuation captured at pause time. + /// Call this in resume methods after completing the effect-specific work and + /// before executing the outer ContinuationActionsJson. + /// + private static void ApplyIntraActionContinuation( + string? intraJson, + DurationModel? actionDuration, + AbilityContext ctx) + { + if (intraJson is null) return; + IntraActionStep[]? steps; + try { steps = JsonSerializer.Deserialize(intraJson, _jsonOpts); } + catch { return; } + if (steps is null) return; + + foreach (var step in steps) + { + var tgt = FindTargetByRef(ctx.State, step.TargetRef); + if (tgt is null) continue; + Effect[]? effects; + try { effects = JsonSerializer.Deserialize(step.EffectsJson, _jsonOpts); } + catch { continue; } + if (effects is null) continue; + foreach (var eff in effects) + { + ExecuteEffect(eff, tgt, actionDuration, ctx); + if (ctx.State.HasPendingInteraction()) return; + } + if (ctx.State.HasPendingInteraction()) return; + } + } + /// Internal metadata envelope stored in ActiveEffect.DurationJson. private sealed class TrackedDuration { diff --git a/Server/src/SOR.Game/Abilities/Models/PendingPlayerPick.cs b/Server/src/SOR.Game/Abilities/Models/PendingPlayerPick.cs index 9729fd7..2363fa8 100644 --- a/Server/src/SOR.Game/Abilities/Models/PendingPlayerPick.cs +++ b/Server/src/SOR.Game/Abilities/Models/PendingPlayerPick.cs @@ -36,6 +36,12 @@ public sealed class PendingPlayerPick // ── Continuation ───────────────────────────────────────────────────────── + /// + /// Serialized IntraActionStep[] — remaining work within the paused action. + /// Applied by the resume handler before executing ContinuationActionsJson. + /// + public string? IntraActionContinuationJson { get; set; } + public string ContinuationActionsJson { get; set; } = "[]"; public string? RangeJson { get; set; } public string? ForceDurationJson { get; set; } diff --git a/Server/src/SOR.Game/Abilities/Models/PendingRevealChoicePick.cs b/Server/src/SOR.Game/Abilities/Models/PendingRevealChoicePick.cs index 225b3e4..7e3f4f6 100644 --- a/Server/src/SOR.Game/Abilities/Models/PendingRevealChoicePick.cs +++ b/Server/src/SOR.Game/Abilities/Models/PendingRevealChoicePick.cs @@ -33,6 +33,12 @@ public sealed class PendingRevealChoicePick // ── Continuation ───────────────────────────────────────────────────────── + /// + /// Serialized IntraActionStep[] — remaining work within the paused action. + /// Applied by the resume handler before executing ContinuationActionsJson. + /// + public string? IntraActionContinuationJson { get; set; } + public string ContinuationActionsJson { get; set; } = "[]"; public string? RangeJson { get; set; } public string? ForceDurationJson { get; set; } diff --git a/Server/src/SOR.Game/Abilities/Models/PendingTilePick.cs b/Server/src/SOR.Game/Abilities/Models/PendingTilePick.cs index 561c9a1..619fe4c 100644 --- a/Server/src/SOR.Game/Abilities/Models/PendingTilePick.cs +++ b/Server/src/SOR.Game/Abilities/Models/PendingTilePick.cs @@ -47,6 +47,13 @@ public sealed class PendingTilePick // ── Continuation ───────────────────────────────────────────────────────── + /// + /// Serialized IntraActionStep[] — remaining work within the paused action + /// (remaining effects on the current target, then all effects on remaining targets). + /// Applied by the resume handler before executing ContinuationActionsJson. + /// + public string? IntraActionContinuationJson { get; set; } + public string ContinuationActionsJson { get; set; } = "[]"; public string? RangeJson { get; set; } public string? ForceDurationJson { get; set; }