diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index b603bf7..ee3ef28 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -4533,6 +4533,14 @@ public static class AbilitySystem /// Call this in resume methods after completing the effect-specific work and /// before executing the outer ContinuationActionsJson. /// + /// + /// Each can have a different effect list (e.g. the first step + /// is only remaining effects on the originally-paused target, subsequent steps are the full + /// effect list for each remaining target). If a nested pause occurs mid-replay we directly + /// rebuild the next IntraActionContinuationJson from the remaining slice rather than + /// relying on , which assumes a homogeneous + /// per-target effect list and would produce wrong results here. + /// private static void ApplyIntraActionContinuation( string? intraJson, DurationModel? actionDuration, @@ -4544,23 +4552,77 @@ public static class AbilitySystem catch { return; } if (steps is null) return; - foreach (var step in steps) + for (int si = 0; si < steps.Length; si++) { - var tgt = FindTargetByRef(ctx.State, step.TargetRef); + var step = steps[si]; + 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) + + for (int ei = 0; ei < effects.Length; ei++) { - ExecuteEffect(eff, tgt, actionDuration, ctx); - if (ctx.State.HasPendingInteraction()) return; + // Set position fields so ExecuteEffect has consistent context, but note that + // we will override any IntraActionContinuationJson it writes (see below). + ctx.CurrentEffectIndex = ei; + ctx.CurrentActionAllEffects = effects; + ctx.CurrentActionTargets = [tgt]; + ctx.CurrentTargetIndex = 0; + ctx.CurrentActionDuration = actionDuration; + + ExecuteEffect(effects[ei], tgt, actionDuration, ctx); + + if (ctx.State.HasPendingInteraction()) + { + // A nested pause was set. BuildIntraActionContinuationJson may have + // already written an IntraActionContinuationJson based on the single-target + // ctx snapshot above, which is incomplete. Override it with the precise + // remaining slice: remaining effects on this step's target + remaining steps. + var remEffects = effects[(ei + 1)..]; + var newSteps = new List(); + if (remEffects.Length > 0) + newSteps.Add(new IntraActionStep + { + TargetRef = step.TargetRef, + EffectsJson = JsonSerializer.Serialize(remEffects, _jsonOpts), + }); + for (int rs = si + 1; rs < steps.Length; rs++) + newSteps.Add(steps[rs]); + + SetPendingIntraActionContinuation(ctx.State, + newSteps.Count > 0 ? JsonSerializer.Serialize(newSteps, _jsonOpts) : null); + return; + } + } + + if (ctx.State.HasPendingInteraction()) + { + // Pause between effects (edge case). Remaining = steps after si. + var remSteps = new List(); + for (int rs = si + 1; rs < steps.Length; rs++) + remSteps.Add(steps[rs]); + SetPendingIntraActionContinuation(ctx.State, + remSteps.Count > 0 ? JsonSerializer.Serialize(remSteps, _jsonOpts) : null); + return; } - if (ctx.State.HasPendingInteraction()) return; } } + /// + /// Overwrites the IntraActionContinuationJson on whichever pause model is currently + /// set in . Used by to + /// replace any stale value written by . + /// + private static void SetPendingIntraActionContinuation(GameState state, string? intraJson) + { + if (state.PendingTilePick is { } tp) { tp.IntraActionContinuationJson = intraJson; return; } + if (state.PendingPlayerPick is { } pp) { pp.IntraActionContinuationJson = intraJson; return; } + if (state.PendingRevealChoicePick is { } rcp) { rcp.IntraActionContinuationJson = intraJson; } + } + /// Internal metadata envelope stored in ActiveEffect.DurationJson. private sealed class TrackedDuration {