diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index ac8d7af..d578ac0 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -920,15 +920,21 @@ public static class AbilitySystem forceDuration, continuationActions); if (childActions is null) { - // Child emitted PendingActionGroupPick. Patch it to include: - // - `all` (already-flattened preceding actions) prepended to continuation - // - remaining sibling groups stored so ResumeActionGroupPick can process them + // Child emitted PendingActionGroupPick. Patch it: + // - Store preceding actions (all actions resolved so far in this AND group) + // so resume can execute them BEFORE the chosen branch. + // - Store remaining sibling groups. + // - Leave ContinuationActionsJson untouched (it already carries the outer + // continuation that was passed into this recursive call). if (ctx.State.PendingActionGroupPick is { } pend) { - var existingCont = JsonSerializer.Deserialize( - pend.ContinuationActionsJson, _jsonOpts) ?? []; - pend.ContinuationActionsJson = JsonSerializer.Serialize( - all.Concat(existingCont).ToArray(), _jsonOpts); + // An inner AND group may have already set PrecedingActionsJson. + // Prepend our own `all` to whatever the inner group stored. + var innerPreceding = pend.PrecedingActionsJson is not null + ? JsonSerializer.Deserialize(pend.PrecedingActionsJson, _jsonOpts) ?? [] + : []; + pend.PrecedingActionsJson = JsonSerializer.Serialize( + all.Concat(innerPreceding).ToArray(), _jsonOpts); var remaining = ag.Groups.Skip(gi + 1).ToArray(); if (remaining.Length > 0) @@ -1011,6 +1017,13 @@ public static class AbilitySystem var ctx = new AbilityContext { State = state, ActorId = pending.ActorId, Source = source }; var continuation = JsonSerializer.Deserialize( pending.ContinuationActionsJson, _jsonOpts) ?? []; + + // Actions that ran before the pause in a parent AND group. + // These must execute BEFORE the chosen branch to preserve AND-group order. + var preceding = pending.PrecedingActionsJson is not null + ? JsonSerializer.Deserialize(pending.PrecedingActionsJson, _jsonOpts) ?? [] + : System.Array.Empty(); + AbilityRange? range = pending.RangeJson is not null ? JsonSerializer.Deserialize(pending.RangeJson, _jsonOpts) : null; DurationModel? forceDuration = pending.ForceDurationJson is not null @@ -1027,7 +1040,16 @@ public static class AbilitySystem abilityIndex: 0, range, forceDuration, continuation); if (chosen is null) { - // Nested group triggered another pick — prepend any chosen-leaf prefix. + // Nested group triggered another pick. + // Accumulate our preceding into whatever the inner group set. + if (ctx.State.PendingActionGroupPick is { } innerPend && preceding.Length > 0) + { + var innerInner = innerPend.PrecedingActionsJson is not null + ? JsonSerializer.Deserialize(innerPend.PrecedingActionsJson, _jsonOpts) ?? [] + : []; + innerPend.PrecedingActionsJson = JsonSerializer.Serialize( + preceding.Concat(innerInner).ToArray(), _jsonOpts); + } return state; } } @@ -1046,13 +1068,16 @@ public static class AbilitySystem abilityIndex: 0, range, forceDuration, continuation); if (rgActions is null) { - // This remaining group paused — prepend already-resolved actions. + // This remaining group paused. Carry forward the full preceding context: + // preceding (from parent) + tempList (chosen branch + already-resolved groups) if (ctx.State.PendingActionGroupPick is { } newPend) { - var existingCont = JsonSerializer.Deserialize( - newPend.ContinuationActionsJson, _jsonOpts) ?? []; - newPend.ContinuationActionsJson = JsonSerializer.Serialize( - tempList.Concat(existingCont).ToArray(), _jsonOpts); + var newInnerPreceding = newPend.PrecedingActionsJson is not null + ? JsonSerializer.Deserialize(newPend.PrecedingActionsJson, _jsonOpts) ?? [] + : []; + newPend.PrecedingActionsJson = JsonSerializer.Serialize( + preceding.Concat(tempList).Concat(newInnerPreceding).ToArray(), _jsonOpts); + // ContinuationActionsJson is already set by the new pause. var afterThis = remainingGroups.Skip(rgi + 1).ToArray(); newPend.RemainingGroupsJson = afterThis.Length > 0 ? JsonSerializer.Serialize(afterThis, _jsonOpts) : null; @@ -1064,7 +1089,8 @@ public static class AbilitySystem chosen = tempList.ToArray(); } - var allActions = chosen.Concat(continuation).ToArray(); + // Correct AND-group order: [preceding] + [chosen/remaining] + [outer continuation]. + var allActions = preceding.Concat(chosen).Concat(continuation).ToArray(); ExecuteActions(allActions, ctx, clientTargets: null, requireClientTargets: false, activationRange: range, forceDuration: forceDuration); return state; @@ -1123,6 +1149,24 @@ public static class AbilitySystem ps.Deck.AddRange(top); } + // Execute any continuation actions stored at pause time. + 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) + { + 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); + } + return state; } @@ -1481,6 +1525,9 @@ public static class AbilitySystem ? JsonSerializer.Deserialize(pending.ForceDurationJson, _jsonOpts) : null; // Execute the paused action (now with client targets) then run the continuation. + // CONTRACT: The paused action is placed at index 0 in the replayed slice, so + // the client must submit targets using ActionIndex=0 regardless of the original + // action index in the ability definition. The prompt payload should reflect this. var allActions = new[] { action }.Concat(continuation).ToArray(); ExecuteActions(allActions, ctx, clientTargets, requireClientTargets: true, activationRange: range, forceDuration: forceDuration); @@ -1639,6 +1686,12 @@ public static class AbilitySystem } // 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; @@ -3173,6 +3226,13 @@ 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. + 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; } diff --git a/Server/src/SOR.Game/Abilities/Models/PendingActionGroupPick.cs b/Server/src/SOR.Game/Abilities/Models/PendingActionGroupPick.cs index 54b5b2e..114749a 100644 --- a/Server/src/SOR.Game/Abilities/Models/PendingActionGroupPick.cs +++ b/Server/src/SOR.Game/Abilities/Models/PendingActionGroupPick.cs @@ -38,6 +38,14 @@ public sealed class PendingActionGroupPick /// public string? RemainingGroupsJson { get; set; } + /// + /// JSON-serialized Action[]? — actions from the parent AND group that were already + /// flattened BEFORE the paused child group. These must execute BEFORE the chosen + /// branch, so resume computes: preceding + chosen + remaining-groups + continuation. + /// Null when there are no such preceding actions. + /// + public string? PrecedingActionsJson { get; set; } + /// JSON-serialized AbilityRange? — passed through to ExecuteActions on resume. public string? RangeJson { get; set; }