fix(abilities): nested pause during intra-action replay captures correct remainder

When ApplyIntraActionContinuation replays IntraActionStep items and a step's
effect itself pauses (emitting PendingTilePick / PendingPlayerPick /
PendingRevealChoicePick), the new pause model may already have an
IntraActionContinuationJson written by BuildIntraActionContinuationJson.
That value is wrong in the replay context because BuildIntraActionContinuationJson
assumes all remaining targets get the same homogeneous effect list, whereas
IntraActionStep items each carry their own per-step effect list.

Fix: convert ApplyIntraActionContinuation loops to indexed, set ctx position
fields (CurrentEffectIndex, CurrentActionAllEffects, CurrentActionTargets,
CurrentTargetIndex, CurrentActionDuration) per iteration for consistency, then
after any nested pause override the IntraActionContinuationJson directly via
new SetPendingIntraActionContinuation helper.  The override is built from the
precise remaining slice:
  - remaining effects on the current step's target  (effects[(ei+1)..])
  - all remaining steps as-is                       (steps[(si+1)..])
This is correct regardless of whether different steps have different effect lists.

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:
dingo
2026-04-21 11:30:01 -04:00
co-authored by Claude Happy
parent 0b7742f724
commit 473e5b075e
+68 -6
View File
@@ -4533,6 +4533,14 @@ public static class AbilitySystem
/// Call this in resume methods after completing the effect-specific work and
/// <em>before</em> executing the outer <c>ContinuationActionsJson</c>.
/// </summary>
/// <remarks>
/// Each <see cref="IntraActionStep"/> 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 <c>IntraActionContinuationJson</c> from the remaining slice rather than
/// relying on <see cref="BuildIntraActionContinuationJson"/>, which assumes a homogeneous
/// per-target effect list and would produce wrong results here.
/// </remarks>
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<Effect[]>(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<IntraActionStep>();
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<IntraActionStep>();
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;
}
}
/// <summary>
/// Overwrites the <c>IntraActionContinuationJson</c> on whichever pause model is currently
/// set in <paramref name="state"/>. Used by <see cref="ApplyIntraActionContinuation"/> to
/// replace any stale value written by <see cref="BuildIntraActionContinuationJson"/>.
/// </summary>
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; }
}
/// <summary>Internal metadata envelope stored in ActiveEffect.DurationJson.</summary>
private sealed class TrackedDuration
{