Fix: conditional passive re-evaluation + INSUFFICIENT_TARGETS crash

Issue 1 — ActivationConditions on passives were never checked:
- ApplyPassivesForSource and ApplyPassivesToNewTarget now evaluate
  ActivationConditions against the source unit before executing each
  passive ability (e.g. IsOnTileWithAffinity: Lake only grants the buff
  when the unit is actually on a Lake tile).

Issue 1 — Passives not re-evaluated on movement / tile changes:
- Added ReEvaluateConditionalPassives(state): for every unit with at
  least one conditional passive, revoke all WhileSourceOnBattlefield
  effects then reapply with conditions re-checked. Units without
  conditional passives are skipped with zero overhead.
- Called after MoveUnit (unit changes tile affinity),
  ActivateAbility (ability may change board state),
  and ChannelCard Terramorpha path (tile affinities change).

Issue 2 — INSUFFICIENT_TARGETS thrown from ApplyPassivesToNewTarget:
- ResolveTargets: when restrictTargets is set (passive-to-new-target
  mode only) and filtering produces zero candidates, return [] silently
  instead of throwing. An ExactSelf source-buff simply has no valid
  target among the new unit — that is expected, not an error.

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-03-18 11:04:03 -04:00
co-authored by Claude Happy
parent 6c02c17a9d
commit 66452313d5
2 changed files with 54 additions and 0 deletions
@@ -496,7 +496,14 @@ public static class AbilitySystem
{
int needed = selector.Count.N;
if (!selector.Count.UpTo && candidates.Count < needed)
{
// In passive-to-new-target mode (restrictTargets set) there is no
// player-driven target selection; zero candidates simply means this
// passive does not apply to the new unit — skip silently.
if (restrictTargets is not null)
return [];
throw new GameException("INSUFFICIENT_TARGETS");
}
candidates = candidates.Take(needed).ToList();
}
@@ -1935,8 +1942,15 @@ public static class AbilitySystem
var ctx = new AbilityContext { State = state, ActorId = ownerId, Source = source };
var fallback = new DurationModel { Type = DurationType.WhileSourceOnBattlefield };
foreach (var def in ParseAbilities(source.AbilitiesJson).Where(d => d.Type == AbilityType.Passive))
{
// Honour ability-level activation conditions (e.g. IsOnTileWithAffinity).
if (def.ActivationConditions is { Length: > 0 } &&
!def.ActivationConditions.All(c => EvaluateCondition(c, ctx, source)))
continue;
ExecuteActions(def.Actions, ctx, clientTargets: null,
forceDuration: def.Duration ?? fallback);
}
}
/// <summary>
@@ -1954,8 +1968,15 @@ public static class AbilitySystem
{
var ctx = new AbilityContext { State = state, ActorId = ownerId, Source = sourceUnit };
foreach (var def in ParseAbilities(sourceUnit.AbilitiesJson).Where(d => d.Type == AbilityType.Passive))
{
// Respect activation conditions evaluated against the source unit.
if (def.ActivationConditions is { Length: > 0 } &&
!def.ActivationConditions.All(c => EvaluateCondition(c, ctx, sourceUnit)))
continue;
ExecuteActions(def.Actions, ctx, clientTargets: null,
restrictTargets: restrict, forceDuration: def.Duration ?? fallback);
}
}
}
@@ -1987,6 +2008,31 @@ public static class AbilitySystem
}
}
/// <summary>
/// Revokes and reapplies every passive that carries <see cref="AbilityDefinition.ActivationConditions"/>,
/// keeping condition-gated buffs in sync with the current game state.
/// Call after any action that could change condition outcomes (unit movement,
/// tile affinity change, ability activation, etc.).
/// Units without conditional passives are skipped at zero cost.
/// </summary>
public static void ReEvaluateConditionalPassives(GameState state)
{
foreach (var (ownerId, player) in state.Players)
{
foreach (var unit in player.Field.OfType<FieldCard>().ToList())
{
bool hasConditional = ParseAbilities(unit.AbilitiesJson)
.Any(d => d.Type == AbilityType.Passive && d.ActivationConditions is { Length: > 0 });
if (!hasConditional) continue;
// Full revoke-then-reapply so stat modifiers stay correct regardless
// of which side of a condition boundary the unit currently sits on.
RevokePassivesForSource(state, unit.InstanceId);
ApplyPassivesForSource(state, unit.InstanceId, ownerId);
}
}
}
// ── Aura range evaluation ─────────────────────────────────────────────────
/// <summary>