Implement phase-2 prevention/replacement triggers and sequence duration
This commit is contained in:
@@ -36,6 +36,13 @@ public static class AbilitySystem
|
||||
// Runtime limiter for Triggered abilities: (source instance, ability key, turn) -> count
|
||||
private static readonly Dictionary<(string SourceInstanceId, string AbilityKey, int Turn), int> _triggerCountsPerTurn = new();
|
||||
|
||||
// Transient prevention markers.
|
||||
private static readonly Dictionary<string, int?> _preventNextDamageByUnit = new();
|
||||
private static readonly HashSet<string> _preventDeathUnits = new();
|
||||
|
||||
// Top-level ability/action sequence depth for UntilEndOfSequence cleanup.
|
||||
private static int _sequenceDepth = 0;
|
||||
|
||||
// ── Parsing ────────────────────────────────────────────────────────────────
|
||||
|
||||
public static AbilityDefinition[] ParseAbilities(string? json)
|
||||
@@ -431,9 +438,22 @@ public static class AbilitySystem
|
||||
HashSet<string>? restrictTargets = null,
|
||||
DurationModel? forceDuration = null)
|
||||
{
|
||||
for (int i = 0; i < actions.Length; i++)
|
||||
ExecuteSingleAction(actions[i], ctx, clientTargets, actionIndex: i,
|
||||
requireClientTargets, activationRange, restrictTargets, forceDuration);
|
||||
_sequenceDepth++;
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < actions.Length; i++)
|
||||
ExecuteSingleAction(actions[i], ctx, clientTargets, actionIndex: i,
|
||||
requireClientTargets, activationRange, restrictTargets, forceDuration);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_sequenceDepth--;
|
||||
if (_sequenceDepth <= 0)
|
||||
{
|
||||
_sequenceDepth = 0;
|
||||
ExpireEndOfSequenceEffects(ctx.State);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ExecuteSingleAction(
|
||||
@@ -1253,6 +1273,41 @@ public static class AbilitySystem
|
||||
TrackTick(new HealEffect { Amount = V(ResolveValue(hot.Amount, ctx)) },
|
||||
ci.InstanceId, hot.Duration, ctx);
|
||||
break;
|
||||
case PreventDeathEffect when target is CardInstance ci:
|
||||
{
|
||||
_preventDeathUnits.Add(ci.InstanceId);
|
||||
var d = duration is null || duration.Type == DurationType.Instant
|
||||
? new DurationModel { Type = DurationType.UntilEndOfSequence }
|
||||
: duration;
|
||||
TrackEffect(new ClearPreventDeathEffect(), ci.InstanceId, d, ctx);
|
||||
break;
|
||||
}
|
||||
case PreventNextDamageEffect pnd when target is CardInstance ci:
|
||||
{
|
||||
var cap = pnd.MaxAmount is null ? (int?)null : ResolveValue(pnd.MaxAmount, ctx);
|
||||
_preventNextDamageByUnit[ci.InstanceId] = cap;
|
||||
var d = duration is null || duration.Type == DurationType.Instant
|
||||
? new DurationModel { Type = DurationType.UntilEndOfSequence }
|
||||
: duration;
|
||||
TrackEffect(new ClearPreventNextDamageEffect(), ci.InstanceId, d, ctx);
|
||||
break;
|
||||
}
|
||||
case ClearPreventDeathEffect when target is CardInstance ci:
|
||||
{
|
||||
_preventDeathUnits.Remove(ci.InstanceId);
|
||||
break;
|
||||
}
|
||||
case ClearPreventNextDamageEffect when target is CardInstance ci:
|
||||
{
|
||||
_preventNextDamageByUnit.Remove(ci.InstanceId);
|
||||
break;
|
||||
}
|
||||
case SetHealthMinimumEffect shm when target is CardInstance ci:
|
||||
{
|
||||
var cur = ci.CurrentHealth ?? 0;
|
||||
if (cur < shm.Minimum) ci.CurrentHealth = shm.Minimum;
|
||||
break;
|
||||
}
|
||||
|
||||
// ── §5.9 Displacement ─────────────────────────────────────────────
|
||||
case PushUnitEffect pu when target is FieldCard fc2:
|
||||
@@ -1475,9 +1530,46 @@ public static class AbilitySystem
|
||||
private static void ApplyDamageToUnit(
|
||||
CardInstance unit, int rawAmount, bool bypassDefense, AbilityContext ctx)
|
||||
{
|
||||
// Pre-damage replacement/prevention window.
|
||||
FireTrigger(ctx.State, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.BeforeDamageApplied,
|
||||
ActorId = ctx.ActorId,
|
||||
Data = new TriggerEventData { SourceId = ctx.Source?.InstanceId, TargetId = unit.InstanceId, Amount = rawAmount },
|
||||
UnitType = unit.CardType,
|
||||
});
|
||||
|
||||
var amount = rawAmount;
|
||||
if (_preventNextDamageByUnit.TryGetValue(unit.InstanceId, out var cap))
|
||||
{
|
||||
var prevented = cap is null ? amount : Math.Min(amount, Math.Max(0, cap.Value));
|
||||
amount -= prevented;
|
||||
if (cap is not null)
|
||||
{
|
||||
var remain = cap.Value - prevented;
|
||||
if (remain > 0) _preventNextDamageByUnit[unit.InstanceId] = remain;
|
||||
else _preventNextDamageByUnit.Remove(unit.InstanceId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_preventNextDamageByUnit.Remove(unit.InstanceId);
|
||||
}
|
||||
|
||||
if (prevented > 0)
|
||||
{
|
||||
FireTrigger(ctx.State, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.DamagePrevented,
|
||||
ActorId = ctx.ActorId,
|
||||
Data = new TriggerEventData { SourceId = ctx.Source?.InstanceId, TargetId = unit.InstanceId, Amount = prevented },
|
||||
UnitType = unit.CardType,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
int net = bypassDefense
|
||||
? rawAmount
|
||||
: Math.Max(0, rawAmount - (unit.CurrentDefense ?? 0));
|
||||
? amount
|
||||
: Math.Max(0, amount - (unit.CurrentDefense ?? 0));
|
||||
unit.CurrentHealth = (unit.CurrentHealth ?? 0) - net;
|
||||
if (unit.CurrentHealth <= 0) KillUnit(unit, ctx, ctx.Source?.InstanceId);
|
||||
}
|
||||
@@ -1487,6 +1579,21 @@ public static class AbilitySystem
|
||||
var owner = FindCardOwner(ctx.State, unit.InstanceId);
|
||||
if (owner is null) return;
|
||||
|
||||
// Pre-death replacement/prevention window.
|
||||
FireTrigger(ctx.State, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.BeforeUnitDeath,
|
||||
ActorId = owner.AccountId,
|
||||
Data = new TriggerEventData { SourceId = killerInstanceId, TargetId = unit.InstanceId },
|
||||
UnitType = unit.CardType,
|
||||
});
|
||||
|
||||
if (_preventDeathUnits.Remove(unit.InstanceId))
|
||||
{
|
||||
unit.CurrentHealth = Math.Max(unit.CurrentHealth ?? 0, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
// Cancel deferred effects whose source is this unit.
|
||||
ctx.State.PendingDeferredEffects.RemoveAll(p => p.SourceInstanceId == unit.InstanceId);
|
||||
|
||||
@@ -1871,6 +1978,17 @@ public static class AbilitySystem
|
||||
&& (t.CausedBy is null || Own(t.CausedBy, occurrence, ownerId, state, uid)),
|
||||
OnDamageDealtTrigger =>
|
||||
occurrence.Kind == TriggerEventKind.DamageDealt,
|
||||
OnDamagePreventedTrigger t =>
|
||||
occurrence.Kind == TriggerEventKind.DamagePrevented
|
||||
&& UnitOwnerMatches(t.TargetOwnership, occurrence.Data?.TargetId, state, ownerId),
|
||||
BeforeDamageAppliedTrigger t =>
|
||||
occurrence.Kind == TriggerEventKind.BeforeDamageApplied
|
||||
&& Own(t.SourceOwnership, occurrence, ownerId, state, uid)
|
||||
&& UnitOwnerMatches(t.TargetOwnership, occurrence.Data?.TargetId, state, ownerId),
|
||||
BeforeUnitDeathTrigger t =>
|
||||
occurrence.Kind == TriggerEventKind.BeforeUnitDeath
|
||||
&& UnitType(t.UnitType, occurrence.UnitType)
|
||||
&& UnitOwnerMatches(t.Ownership, occurrence.Data?.TargetId, state, ownerId),
|
||||
OnUnitEnterBattlefieldTrigger t =>
|
||||
occurrence.Kind == TriggerEventKind.UnitEnterBattlefield
|
||||
&& UnitType(t.UnitType, occurrence.UnitType)
|
||||
@@ -2206,6 +2324,10 @@ public static class AbilitySystem
|
||||
}
|
||||
break;
|
||||
|
||||
case DurationType.UntilEndOfSequence:
|
||||
// handled by ExpireEndOfSequenceEffects after top-level action resolution
|
||||
break;
|
||||
|
||||
case DurationType.WhileCondition:
|
||||
if (dur.Condition is not null)
|
||||
{
|
||||
@@ -2231,6 +2353,27 @@ public static class AbilitySystem
|
||||
return state;
|
||||
}
|
||||
|
||||
private static GameState ExpireEndOfSequenceEffects(GameState state)
|
||||
{
|
||||
var toRemove = new List<ActiveEffect>();
|
||||
foreach (var ae in state.ActiveEffects)
|
||||
{
|
||||
TrackedDuration? td;
|
||||
try { td = JsonSerializer.Deserialize<TrackedDuration>(ae.DurationJson, _jsonOpts); }
|
||||
catch { continue; }
|
||||
if (td?.Duration?.Type != DurationType.UntilEndOfSequence) continue;
|
||||
|
||||
if (!td.IsTick)
|
||||
ApplyReversal(state, ae, td.OwnerAccountId);
|
||||
toRemove.Add(ae);
|
||||
}
|
||||
|
||||
foreach (var ae in toRemove)
|
||||
state.ActiveEffects.Remove(ae);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private static bool MatchPlayerScope(
|
||||
string scope, string currentPlayerId, string ownerAccountId)
|
||||
=> scope.ToLowerInvariant() switch
|
||||
|
||||
Reference in New Issue
Block a user