Fix: abilitiesJson null crash, battlefield popup, ExactSelf ability filter

Client (game_screen.gd):
- Fix crash when clicking a unit with null abilitiesJson: server sends the key
  with a null value, so dict.get(key,default) returns null — guard both uses
  (_show_ability_panel and _on_hold_timer_fired) with explicit null check.
- Add double-click on battlefield units to open a full CardDetailPopup showing
  name, affinity, stats, and ability descriptions. Popup is instantiated once
  in _ready() and reused; aliases movementPoints/abilities keys as expected.

Server (Filters.cs / AbilitySystem.cs):
- IsSelf filter now matches by CardDefinitionId (definition-level: any copy of
  the same card). This is the appropriate semantic for "self" as a type scope.
- IsExactSelf filter added: matches by InstanceId (instance-level: only this
  specific unit on the battlefield). Use this to prevent duplicate-instance
  trigger bleed where two copies of a card both fire when only one acted.
- TriggerMatches now accepts the evaluating FieldCard and forwards its
  InstanceId to Own() for all ownership checks.
- Own() gains "ExactSelf" scope: fires only when occ.Data.SourceId matches the
  evaluating unit's InstanceId — eliminating the definition-level bleed bug.

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-08 16:28:06 -04:00
co-authored by Claude Happy
parent 94f39be8a7
commit 11ebdc40d7
3 changed files with 93 additions and 33 deletions
+50 -29
View File
@@ -128,7 +128,7 @@ public static class AbilitySystem
var def = abilities[i];
if (def.Type != AbilityType.Triggered) continue;
if (def.TriggerEvent is null) continue;
if (!TriggerMatches(def.TriggerEvent, occurrence, state, ownerId)) continue;
if (!TriggerMatches(def.TriggerEvent, occurrence, state, ownerId, unit)) continue;
var ctx = new AbilityContext
{
@@ -1387,7 +1387,10 @@ public static class AbilitySystem
_ => false,
},
TierIsFilter tif => card.Tier == tif.Tier,
IsSelfFilter => ctx.Source is not null && card.InstanceId == ctx.Source.InstanceId,
// IsSelf: definition-level match (any instance of the same card definition).
IsSelfFilter => ctx.Source is not null && card.CardDefinitionId == ctx.Source.CardDefinitionId,
// IsExactSelf: instance-level match (only this specific unit on the battlefield).
IsExactSelfFilter => ctx.Source is not null && card.InstanceId == ctx.Source.InstanceId,
HealthBelowFilter hbf => (card.CurrentHealth ?? 0) < hbf.N,
HealthAboveFilter haf => (card.CurrentHealth ?? 0) > haf.N,
_ => true,
@@ -1395,91 +1398,109 @@ public static class AbilitySystem
// ── Trigger matching ──────────────────────────────────────────────────────
/// <param name="evaluatingUnit">
/// The specific battlefield unit whose trigger is being evaluated.
/// Required for the "ExactSelf" ownership scope — fire only when this unit
/// is the source of the event (occ.Data.SourceId == evaluatingUnit.InstanceId).
/// </param>
private static bool TriggerMatches(
TriggerEvent trigger, TriggerOccurrence occurrence, GameState state, string ownerId)
=> trigger switch
TriggerEvent trigger, TriggerOccurrence occurrence, GameState state, string ownerId,
FieldCard? evaluatingUnit = null)
{
var uid = evaluatingUnit?.InstanceId;
return trigger switch
{
OnTurnStartTrigger t =>
occurrence.Kind == TriggerEventKind.TurnStart
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnTurnEndTrigger t =>
occurrence.Kind == TriggerEventKind.TurnEnd
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnUnitDeathTrigger t =>
occurrence.Kind == TriggerEventKind.UnitDeath
&& UnitType(t.UnitType, occurrence.UnitType)
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnDamageDealtTrigger =>
occurrence.Kind == TriggerEventKind.DamageDealt,
OnUnitEnterBattlefieldTrigger t =>
occurrence.Kind == TriggerEventKind.UnitEnterBattlefield
&& UnitType(t.UnitType, occurrence.UnitType)
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnUnitLeaveBattlefieldTrigger t =>
occurrence.Kind == TriggerEventKind.UnitLeaveBattlefield
&& UnitType(t.UnitType, occurrence.UnitType)
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnSPGainedTrigger t =>
occurrence.Kind == TriggerEventKind.SPGained
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnSPSpentTrigger t =>
occurrence.Kind == TriggerEventKind.SPSpent
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnSTGainedTrigger t =>
occurrence.Kind == TriggerEventKind.STGained
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnSTSpentTrigger t =>
occurrence.Kind == TriggerEventKind.STSpent
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnCardDrawnTrigger t =>
occurrence.Kind == TriggerEventKind.CardDrawn
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnUnitMoveTrigger t =>
occurrence.Kind == TriggerEventKind.UnitMove
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnUnitAttackTrigger t =>
occurrence.Kind == TriggerEventKind.UnitAttack
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnSpiritSacrificedTrigger t =>
occurrence.Kind == TriggerEventKind.SpiritSacrificed
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnAscendTrigger t =>
occurrence.Kind == TriggerEventKind.Ascend
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnTransitionTrigger t =>
occurrence.Kind == TriggerEventKind.Transition
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnChanneledTrigger t =>
occurrence.Kind == TriggerEventKind.Channeled
&& CardType(t.CardType, occurrence.CardType)
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnEnterAuraRangeTrigger eat =>
occurrence.Kind == TriggerEventKind.EnterAuraRange
&& Own(eat.AuraOwnership, occurrence, ownerId, state)
&& Own(eat.AuraOwnership, occurrence, ownerId, state, uid)
&& UnitOwnerMatches(eat.UnitOwnership, occurrence.Data?.TargetId, state, ownerId),
OnLeaveAuraRangeTrigger lat =>
occurrence.Kind == TriggerEventKind.LeaveAuraRange
&& Own(lat.AuraOwnership, occurrence, ownerId, state)
&& Own(lat.AuraOwnership, occurrence, ownerId, state, uid)
&& UnitOwnerMatches(lat.UnitOwnership, occurrence.Data?.TargetId, state, ownerId),
OnRuinActivationTrigger rat =>
occurrence.Kind == TriggerEventKind.RuinActivation
&& Own(rat.RuinOwnership, occurrence, ownerId, state)
&& Own(rat.RuinOwnership, occurrence, ownerId, state, uid)
&& (rat.OccupantOwnership is null
|| UnitOwnerMatches(rat.OccupantOwnership, occurrence.Data?.TargetId, state, ownerId)),
OnUnitOccupyRuinTrigger uor =>
occurrence.Kind == TriggerEventKind.UnitOccupyRuin
&& Own(uor.UnitOwnership, occurrence, ownerId, state),
&& Own(uor.UnitOwnership, occurrence, ownerId, state, uid),
_ => false,
};
}
private static bool Own(string? scope, TriggerOccurrence occ, string unitOwnerId, GameState _)
/// <param name="evalUnitId">
/// Instance ID of the unit whose trigger is being evaluated.
/// Only used for the "ExactSelf" scope: the trigger fires only when this unit
/// is the specific unit that caused the event (occ.Data.SourceId == evalUnitId).
/// </param>
private static bool Own(string? scope, TriggerOccurrence occ, string unitOwnerId, GameState _,
string? evalUnitId = null)
=> (scope?.ToLowerInvariant()) switch
{
null or "any" => true,
null or "any" => true,
"own" or "self" or "owner" => occ.ActorId == unitOwnerId,
"enemy" => occ.ActorId != unitOwnerId,
"ally" => occ.ActorId == unitOwnerId,
_ => true,
"enemy" => occ.ActorId != unitOwnerId,
"ally" => occ.ActorId == unitOwnerId,
// ExactSelf: fires only for the specific unit that performed the action.
// Requires occ.Data.SourceId to carry the acting unit's instance ID.
"exactself" => evalUnitId is not null && occ.Data?.SourceId == evalUnitId,
_ => true,
};
private static bool UnitOwnerMatches(
@@ -26,6 +26,7 @@ using SOR.Database.Enums;
[JsonDerivedType(typeof(CardSTCostRangeFilter), "CardSTCostRange")]
[JsonDerivedType(typeof(WithinRangeFilter), "WithinRange")]
[JsonDerivedType(typeof(IsSelfFilter), "IsSelf")]
[JsonDerivedType(typeof(IsExactSelfFilter), "IsExactSelf")]
// ── Tile filters ─────────────────────────────────────────────────────────────
[JsonDerivedType(typeof(TileAffinityMatchesFilter), "TileAffinityMatches")]
[JsonDerivedType(typeof(TileIsOccupiedFilter), "TileIsOccupied")]
@@ -86,9 +87,18 @@ public sealed class CardSTCostRangeFilter : Filter { public int Min { get; set;
/// <summary>Unit must be within Min..Max Manhattan distance of the ability's source unit.</summary>
public sealed class WithinRangeFilter : Filter { public int Min { get; set; } public int Max { get; set; } }
/// <summary>Matches only the exact unit that owns this ability (not all friendly units).</summary>
/// <summary>
/// Matches any unit that shares the same card definition as the ability source.
/// Use for abilities that affect "all copies of this card type".
/// </summary>
public sealed class IsSelfFilter : Filter { }
/// <summary>
/// Matches only the exact unit instance that owns this ability (by InstanceId).
/// Use this instead of IsSelf when only the specific unit on the battlefield should be affected.
/// </summary>
public sealed class IsExactSelfFilter : Filter { }
// ── Tile filter implementations ───────────────────────────────────────────────
public sealed class TileAffinityMatchesFilter : Filter