fix(abilities): general intra-action continuation for effect-level pauses
Previously, pausing effects (CloneSpiritAsToken/TargetTile,
ChannelCardFromZone/TargetTile+Choice, StackDeck interactive) only stored
the outer action-sequence continuation. Any effects after the pausing one on
the same target, and any remaining targets in the same action, were silently
dropped on resume. This commit makes pause/resume fully general.
Mechanism:
- New IntraActionStep private class: { TargetRef, EffectsJson }
- BuildIntraActionContinuationJson(currentTarget, ctx) captures:
• remaining effects on the paused target (effects[effectIndex+1..])
• all effects applied to each remaining target (targets[targetIndex+1..])
- ApplyIntraActionContinuation applies those steps at resume time before
the outer continuation actions
- FindTargetByRef / GetTargetRef round-trip any resolved target type
(FieldCard, CardInstance, PlayerState, Coord) through a string ref
AbilityContext gains 5 per-iteration tracking fields set by ExecuteSingleAction:
CurrentEffectIndex, CurrentTargetIndex, CurrentActionTargets,
CurrentActionAllEffects, CurrentActionDuration
ExecuteSingleAction target/effect loops converted from foreach to indexed
for-loops so ctx position fields stay in sync with each ExecuteEffect call.
IntraActionContinuationJson added to PendingTilePick, PendingPlayerPick,
and PendingRevealChoicePick. All three pause-emitting cases (CloneSpiritAsToken,
ChannelCardFromZone x2, StackDeck) populate it. All three resume methods
(ResumeTilePick, ResumePlayerPick, ResumeRevealChoicePick) call
ApplyIntraActionContinuation before executing outer continuation.
ChannelCardFromZone player-pick-to-tile-pick chain now forwards
IntraActionContinuationJson so it survives a double-pause sequence.
The "DESIGN CONSTRAINT: pausing effects must be terminal" comment is removed
as it is no longer true.
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -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<FieldCard>()
|
||||
.FirstOrDefault(f => f.InstanceId == pending.SourceInstanceId);
|
||||
var rrcCtx = new AbilityContext { State = state, ActorId = pending.ActorId, Source = rrcSource };
|
||||
|
||||
var rrcContinuation = JsonSerializer.Deserialize<Models.Action[]>(
|
||||
pending.ContinuationActionsJson, _jsonOpts) ?? [];
|
||||
if (rrcContinuation.Length > 0)
|
||||
DurationModel? rrcActionDuration = pending.ForceDurationJson is not null
|
||||
? JsonSerializer.Deserialize<DurationModel>(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<AbilityRange>(pending.RangeJson, _jsonOpts) : null;
|
||||
DurationModel? rrcForceDuration = pending.ForceDurationJson is not null
|
||||
? JsonSerializer.Deserialize<DurationModel>(pending.ForceDurationJson, _jsonOpts) : null;
|
||||
ExecuteActions(rrcContinuation, rrcCtx, clientTargets: null, activationRange: rrcRange,
|
||||
forceDuration: rrcForceDuration);
|
||||
var rrcContinuation = JsonSerializer.Deserialize<Models.Action[]>(
|
||||
pending.ContinuationActionsJson, _jsonOpts) ?? [];
|
||||
if (rrcContinuation.Length > 0)
|
||||
{
|
||||
AbilityRange? rrcRange = pending.RangeJson is not null
|
||||
? JsonSerializer.Deserialize<AbilityRange>(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<Models.Action[]>(
|
||||
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<DurationModel>(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<AbilityRange>(pending.RangeJson, _jsonOpts) : null;
|
||||
DurationModel? forceDuration = pending.ForceDurationJson is not null
|
||||
? JsonSerializer.Deserialize<DurationModel>(pending.ForceDurationJson, _jsonOpts) : null;
|
||||
ExecuteActions(continuation, ctx, clientTargets: null, activationRange: range,
|
||||
forceDuration: forceDuration);
|
||||
var continuation = JsonSerializer.Deserialize<Models.Action[]>(
|
||||
pending.ContinuationActionsJson, _jsonOpts) ?? [];
|
||||
if (continuation.Length > 0)
|
||||
{
|
||||
AbilityRange? range = pending.RangeJson is not null
|
||||
? JsonSerializer.Deserialize<AbilityRange>(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<Models.Action[]>(
|
||||
pending.ContinuationActionsJson, _jsonOpts) ?? [];
|
||||
if (ppContinuation.Length > 0)
|
||||
DurationModel? ppActionDuration = pending.ForceDurationJson is not null
|
||||
? JsonSerializer.Deserialize<DurationModel>(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<AbilityRange>(pending.RangeJson, _jsonOpts) : null;
|
||||
DurationModel? ppForceDuration = pending.ForceDurationJson is not null
|
||||
? JsonSerializer.Deserialize<DurationModel>(pending.ForceDurationJson, _jsonOpts) : null;
|
||||
ExecuteActions(ppContinuation, ppCtx, clientTargets: null, activationRange: ppRange,
|
||||
forceDuration: ppForceDuration);
|
||||
var ppContinuation = JsonSerializer.Deserialize<Models.Action[]>(
|
||||
pending.ContinuationActionsJson, _jsonOpts) ?? [];
|
||||
if (ppContinuation.Length > 0)
|
||||
{
|
||||
AbilityRange? ppRange = pending.RangeJson is not null
|
||||
? JsonSerializer.Deserialize<AbilityRange>(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 ─────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// One step in an intra-action continuation: apply <see cref="EffectsJson"/> to the
|
||||
/// unit/player identified by <see cref="TargetRef"/> (instanceId or accountId).
|
||||
/// </summary>
|
||||
private sealed class IntraActionStep
|
||||
{
|
||||
public string TargetRef { get; set; } = string.Empty;
|
||||
public string EffectsJson { get; set; } = "[]";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a resolved target object to a stable string reference used in
|
||||
/// <see cref="IntraActionStep.TargetRef"/> and <see cref="ActiveEffect.TargetRef"/>.
|
||||
/// </summary>
|
||||
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,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Finds a target in <paramref name="state"/> by the string ref produced by
|
||||
/// <see cref="GetTargetRef"/>. Returns a <see cref="CardInstance"/>, <see cref="PlayerState"/>,
|
||||
/// or <see cref="Coord"/> boxed as object; null if not found.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the <c>IntraActionContinuationJson</c> stored in a pause model when an effect
|
||||
/// pauses mid-action. Captures:
|
||||
/// <list type="bullet">
|
||||
/// <item>remaining effects on the current target (effects after the pausing one)</item>
|
||||
/// <item>all effects applied to each remaining target (targets after the current one)</item>
|
||||
/// </list>
|
||||
/// Returns null when there is nothing left within the current action.
|
||||
/// </summary>
|
||||
private static string? BuildIntraActionContinuationJson(object currentTarget, AbilityContext ctx)
|
||||
{
|
||||
var steps = new List<IntraActionStep>();
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the intra-action continuation captured at pause time.
|
||||
/// Call this in resume methods after completing the effect-specific work and
|
||||
/// <em>before</em> executing the outer <c>ContinuationActionsJson</c>.
|
||||
/// </summary>
|
||||
private static void ApplyIntraActionContinuation(
|
||||
string? intraJson,
|
||||
DurationModel? actionDuration,
|
||||
AbilityContext ctx)
|
||||
{
|
||||
if (intraJson is null) return;
|
||||
IntraActionStep[]? steps;
|
||||
try { steps = JsonSerializer.Deserialize<IntraActionStep[]>(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<Effect[]>(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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Internal metadata envelope stored in ActiveEffect.DurationJson.</summary>
|
||||
private sealed class TrackedDuration
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user