Fix: implement Passive ability execution with per-target conditions
Passive abilities (e.g. 'All Merfolk +1 attack') were never executed
because AbilityType.Passive had no runtime processing.
Three root causes fixed:
1. Passive abilities are now applied when units enter the battlefield
and reversed when they leave (new DurationType.WhileSourceOnBattlefield).
- ApplyPassivesForSource: runs a unit's passives against all current targets
- ApplyPassivesToNewTarget: applies existing passives to a newly-entered unit
- RevokePassivesForSource: reverses all passive effects on source death/leave
- Hooked into GameEngine.Channel, Transition and AbilitySystem
ReturnToBattlefield, SummonToken, KillUnit, SendToDiscard
2. Action conditions with Subject="Target" now filter per-target instead of
aborting the entire action (which evaluated against ctx.Source).
Source-level conditions still gate the whole action; target-level ones
are evaluated individually and skip non-matching targets.
3. ExecuteActions/ResolveTargets gain a restrictTargets parameter used by
ApplyPassivesToNewTarget to avoid double-applying existing passive buffs
to units that already received them when the passive source entered.
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:
@@ -358,10 +358,13 @@ public static class AbilitySystem
|
||||
AbilityContext ctx,
|
||||
IReadOnlyList<AbilityTarget>? clientTargets,
|
||||
bool requireClientTargets = false,
|
||||
AbilityRange? activationRange = null)
|
||||
AbilityRange? activationRange = null,
|
||||
HashSet<string>? restrictTargets = null,
|
||||
DurationModel? forceDuration = null)
|
||||
{
|
||||
for (int i = 0; i < actions.Length; i++)
|
||||
ExecuteSingleAction(actions[i], ctx, clientTargets, actionIndex: i, requireClientTargets, activationRange);
|
||||
ExecuteSingleAction(actions[i], ctx, clientTargets, actionIndex: i,
|
||||
requireClientTargets, activationRange, restrictTargets, forceDuration);
|
||||
}
|
||||
|
||||
private static void ExecuteSingleAction(
|
||||
@@ -370,20 +373,38 @@ public static class AbilitySystem
|
||||
IReadOnlyList<AbilityTarget>? clientTargets,
|
||||
int actionIndex,
|
||||
bool requireClientTargets = false,
|
||||
AbilityRange? activationRange = null)
|
||||
AbilityRange? activationRange = null,
|
||||
HashSet<string>? restrictTargets = null,
|
||||
DurationModel? forceDuration = null)
|
||||
{
|
||||
// Source-level conditions (Subject != "Target"): gate the whole action.
|
||||
if (action.Conditions is { Length: > 0 })
|
||||
foreach (var cond in action.Conditions)
|
||||
if (!EvaluateCondition(cond, ctx, ctx.Source!))
|
||||
if (cond.Subject != "Target" && !EvaluateCondition(cond, ctx, ctx.Source!))
|
||||
return;
|
||||
|
||||
var resolved = ResolveTargets(
|
||||
action.Target, ctx, clientTargets, actionIndex, requireClientTargets, activationRange);
|
||||
action.Target, ctx, clientTargets, actionIndex, requireClientTargets, activationRange, restrictTargets);
|
||||
ctx.ResolvedTargets[actionIndex] = resolved;
|
||||
|
||||
// Target-level conditions (Subject == "Target"): filter per target.
|
||||
var targetConds = action.Conditions?.Where(c => c.Subject == "Target").ToArray();
|
||||
var duration = forceDuration ?? action.Duration;
|
||||
|
||||
foreach (var target in resolved)
|
||||
{
|
||||
if (targetConds is { Length: > 0 })
|
||||
{
|
||||
// Temporarily expose current target so EvaluateCondition resolves "Target" correctly.
|
||||
ctx.ResolvedTargets[actionIndex] = [target];
|
||||
bool passes = targetConds.All(c => EvaluateCondition(c, ctx, target));
|
||||
ctx.ResolvedTargets[actionIndex] = resolved;
|
||||
if (!passes) continue;
|
||||
}
|
||||
|
||||
foreach (var effect in action.Effects)
|
||||
ExecuteEffect(effect, target, action.Duration, ctx);
|
||||
ExecuteEffect(effect, target, duration, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Target resolution ─────────────────────────────────────────────────────
|
||||
@@ -394,7 +415,8 @@ public static class AbilitySystem
|
||||
IReadOnlyList<AbilityTarget>? clientTargets,
|
||||
int actionIndex,
|
||||
bool requireClientTargets = false,
|
||||
AbilityRange? activationRange = null)
|
||||
AbilityRange? activationRange = null,
|
||||
HashSet<string>? restrictTargets = null)
|
||||
{
|
||||
var state = ctx.State;
|
||||
|
||||
@@ -428,6 +450,10 @@ public static class AbilitySystem
|
||||
if (selector.Ownership == TargetOwnership.ExactSelf && ctx.Source is not null)
|
||||
candidates = candidates.Where(c => c.InstanceId == ctx.Source.InstanceId).ToList();
|
||||
|
||||
// Passive apply-to-new-unit: restrict to a specific subset of candidates.
|
||||
if (restrictTargets is not null)
|
||||
candidates = candidates.Where(c => restrictTargets.Contains(c.InstanceId)).ToList();
|
||||
|
||||
if (selector.Filters is { Length: > 0 })
|
||||
candidates = candidates.Where(c => FiltersMatch(selector.Filters, c, ctx)).ToList();
|
||||
|
||||
@@ -987,12 +1013,20 @@ public static class AbilitySystem
|
||||
Data = new TriggerEventData { SourceId = fc.InstanceId },
|
||||
});
|
||||
EvaluateAurasOnEnter(ctx.State, fc.InstanceId);
|
||||
ApplyPassivesForSource(ctx.State, fc.InstanceId, ownerPs.AccountId);
|
||||
ApplyPassivesToNewTarget(ctx.State, fc.InstanceId);
|
||||
break;
|
||||
}
|
||||
case SendToDiscardEffect when target is CardInstance ci:
|
||||
{
|
||||
var owner = FindCardOwner(ctx.State, ci.InstanceId);
|
||||
if (owner is not null) { RemoveFromAllZones(owner, ci, ctx.State.Board); owner.Discard.Add(ci); }
|
||||
if (owner is not null)
|
||||
{
|
||||
if (owner.Field.Any(f => f.InstanceId == ci.InstanceId))
|
||||
RevokePassivesForSource(ctx.State, ci.InstanceId);
|
||||
RemoveFromAllZones(owner, ci, ctx.State.Board);
|
||||
owner.Discard.Add(ci);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SearchDeckEffect sde:
|
||||
@@ -1140,6 +1174,8 @@ public static class AbilitySystem
|
||||
Data = new TriggerEventData { SourceId = tokenField.InstanceId },
|
||||
});
|
||||
EvaluateAurasOnEnter(ctx.State, tokenField.InstanceId);
|
||||
ApplyPassivesForSource(ctx.State, tokenField.InstanceId, ctx.ActorId);
|
||||
ApplyPassivesToNewTarget(ctx.State, tokenField.InstanceId);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1254,6 +1290,7 @@ public static class AbilitySystem
|
||||
|
||||
// Fire leave-aura triggers before removing from board.
|
||||
EvaluateAurasOnLeave(ctx.State, unit.InstanceId);
|
||||
RevokePassivesForSource(ctx.State, unit.InstanceId);
|
||||
if (unit.CardType == "Ruin")
|
||||
ctx.State.Board?.RemoveRuin(unit.InstanceId);
|
||||
else
|
||||
@@ -1879,6 +1916,75 @@ public static class AbilitySystem
|
||||
});
|
||||
}
|
||||
|
||||
// ── Passive ability application / revocation ─────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Executes all <see cref="AbilityType.Passive"/> abilities for <paramref name="sourceUnitId"/>
|
||||
/// against all current battlefield units. Effects are tracked with
|
||||
/// <see cref="DurationType.WhileSourceOnBattlefield"/> so they are reversed when the source dies
|
||||
/// or leaves. Call after placing the unit on the board.
|
||||
/// </summary>
|
||||
public static void ApplyPassivesForSource(GameState state, string sourceUnitId, string ownerId)
|
||||
{
|
||||
var source = state.Players.Values
|
||||
.SelectMany(p => p.Field.OfType<FieldCard>())
|
||||
.FirstOrDefault(f => f.InstanceId == sourceUnitId);
|
||||
if (source is null) return;
|
||||
|
||||
var ctx = new AbilityContext { State = state, ActorId = ownerId, Source = source };
|
||||
var duration = new DurationModel { Type = DurationType.WhileSourceOnBattlefield };
|
||||
foreach (var def in ParseAbilities(source.AbilitiesJson).Where(d => d.Type == AbilityType.Passive))
|
||||
ExecuteActions(def.Actions, ctx, clientTargets: null, forceDuration: duration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies all existing passive abilities from every battlefield unit to <paramref name="newUnitId"/>.
|
||||
/// Restricts targeting to the new unit so already-buffed units are not double-applied.
|
||||
/// Call after <see cref="ApplyPassivesForSource"/> for the new unit.
|
||||
/// </summary>
|
||||
public static void ApplyPassivesToNewTarget(GameState state, string newUnitId)
|
||||
{
|
||||
var restrict = new HashSet<string> { newUnitId };
|
||||
var duration = new DurationModel { Type = DurationType.WhileSourceOnBattlefield };
|
||||
foreach (var (ownerId, player) in state.Players)
|
||||
foreach (var sourceUnit in player.Field.OfType<FieldCard>()
|
||||
.Where(f => f.InstanceId != newUnitId).ToList())
|
||||
{
|
||||
var ctx = new AbilityContext { State = state, ActorId = ownerId, Source = sourceUnit };
|
||||
foreach (var def in ParseAbilities(sourceUnit.AbilitiesJson).Where(d => d.Type == AbilityType.Passive))
|
||||
ExecuteActions(def.Actions, ctx, clientTargets: null,
|
||||
restrictTargets: restrict, forceDuration: duration);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverses all <see cref="DurationType.WhileSourceOnBattlefield"/> active effects whose
|
||||
/// source is <paramref name="sourceUnitId"/>. Call before removing a unit from the board.
|
||||
/// </summary>
|
||||
public static void RevokePassivesForSource(GameState state, string sourceUnitId)
|
||||
{
|
||||
var toRevoke = state.ActiveEffects
|
||||
.Where(ae => ae.SourceInstanceId == sourceUnitId)
|
||||
.Where(ae =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var td = JsonSerializer.Deserialize<TrackedDuration>(ae.DurationJson, _jsonOpts);
|
||||
return td?.Duration?.Type == DurationType.WhileSourceOnBattlefield;
|
||||
}
|
||||
catch { return false; }
|
||||
})
|
||||
.ToList();
|
||||
|
||||
foreach (var ae in toRevoke)
|
||||
{
|
||||
TrackedDuration? td = null;
|
||||
try { td = JsonSerializer.Deserialize<TrackedDuration>(ae.DurationJson, _jsonOpts); } catch { }
|
||||
if (td is not null) ApplyReversal(state, ae, td.OwnerAccountId);
|
||||
state.ActiveEffects.Remove(ae);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Aura range evaluation ─────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -15,6 +15,8 @@ public enum DurationType
|
||||
ForNTurns,
|
||||
/// <summary>Active while a condition evaluates to true; expires when it becomes false.</summary>
|
||||
WhileCondition,
|
||||
/// <summary>Active while the ability's source unit is on the battlefield; revoked on leave/death.</summary>
|
||||
WhileSourceOnBattlefield,
|
||||
/// <summary>Persists for the entire match; never expires.</summary>
|
||||
PermanentInMatch,
|
||||
}
|
||||
|
||||
@@ -281,6 +281,8 @@ public static class GameEngine
|
||||
Data = new TriggerEventData { SourceId = field.InstanceId },
|
||||
});
|
||||
AbilitySystem.EvaluateAurasOnEnter(state, field.InstanceId);
|
||||
AbilitySystem.ApplyPassivesForSource(state, field.InstanceId, actorId);
|
||||
AbilitySystem.ApplyPassivesToNewTarget(state, field.InstanceId);
|
||||
}
|
||||
else if (card.CardType == "Terramorpha")
|
||||
{
|
||||
@@ -483,6 +485,7 @@ public static class GameEngine
|
||||
|
||||
// Remove current from board; fire aura leave before removal.
|
||||
AbilitySystem.EvaluateAurasOnLeave(state, current.InstanceId);
|
||||
AbilitySystem.RevokePassivesForSource(state, current.InstanceId);
|
||||
board.RemoveUnit(current.InstanceId);
|
||||
player.Discard.Add(current);
|
||||
|
||||
@@ -490,6 +493,8 @@ public static class GameEngine
|
||||
player.ArchSpirit = newArchSpirit;
|
||||
board.PlaceUnit(coord, newArchSpirit.InstanceId, actorId);
|
||||
AbilitySystem.EvaluateAurasOnEnter(state, newArchSpirit.InstanceId);
|
||||
AbilitySystem.ApplyPassivesForSource(state, newArchSpirit.InstanceId, actorId);
|
||||
AbilitySystem.ApplyPassivesToNewTarget(state, newArchSpirit.InstanceId);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user