Aura: self-apply Actions + ClearOnLeave / LingerDuration on leave

Previously, Aura abilities only fired OnEnterAuraRange / OnLeaveAuraRange
trigger events — their own Actions were never executed. Auras now directly
apply their Actions to any unit that enters range (using WhileSourceOnBattlefield
by default) and handle departure via three modes:

- Persist (default): effects expire at their configured Duration unchanged
- ClearOnLeave: effects are reverted immediately when the unit leaves range
- LingerDuration: effects switch to the specified duration when leaving range,
  allowing them to outlive the aura's reach (and the source if it leaves)

Server:
- AbilityDefinition: add ClearOnLeave (bool) and LingerDuration (DurationModel?)
- AbilitySystem: add ApplyAuraEffects + HandleAuraLeave helpers;
  refactor FireAuraTransition to take FieldCard + AbilityDefinition and call
  both helpers; update all six call sites in EvaluateAuraTransitions /
  EvaluateAurasOnEnter / EvaluateAurasOnLeave to use the unified path

Admin:
- AbilityBuilder: add AuraClearOnLeave / AuraLingerDurationStr / N draft fields;
  parse + compile round-trip; 3-radio On Leave UI + optional Linger duration select;
  update Aura help row to describe the new behaviour

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-24 21:11:56 -04:00
co-authored by Claude Happy
parent afb083093a
commit 9c42e5c1c8
3 changed files with 176 additions and 40 deletions
@@ -139,6 +139,53 @@
@bind="ab.AuraRangeMax" @bind:after="Compile" />
</div>
</div>
<div class="row g-2 mb-3 align-items-end">
<div class="col-auto">
<label class="form-label small mb-1">On Leave Range</label>
<div class="d-flex align-items-center gap-2">
<div class="form-check mb-0">
<input class="form-check-input" type="radio"
name="auraLeave_@ab.GetHashCode()" id="auraLeaveNone_@ab.GetHashCode()"
checked="@(string.IsNullOrEmpty(ab.AuraLingerDurationStr) && !ab.AuraClearOnLeave)"
@onchange="() => SetAuraLeaveMode(ab, 0)" />
<label class="form-check-label small" for="auraLeaveNone_@ab.GetHashCode()">Persist (effect duration)</label>
</div>
<div class="form-check mb-0">
<input class="form-check-input" type="radio"
name="auraLeave_@ab.GetHashCode()" id="auraLeaveClear_@ab.GetHashCode()"
checked="@(ab.AuraClearOnLeave && string.IsNullOrEmpty(ab.AuraLingerDurationStr))"
@onchange="() => SetAuraLeaveMode(ab, 1)" />
<label class="form-check-label small" for="auraLeaveClear_@ab.GetHashCode()">Clear immediately</label>
</div>
<div class="form-check mb-0">
<input class="form-check-input" type="radio"
name="auraLeave_@ab.GetHashCode()" id="auraLeaveLinger_@ab.GetHashCode()"
checked="@(!string.IsNullOrEmpty(ab.AuraLingerDurationStr))"
@onchange="() => SetAuraLeaveMode(ab, 2)" />
<label class="form-check-label small" for="auraLeaveLinger_@ab.GetHashCode()">Linger</label>
</div>
</div>
</div>
@if (!string.IsNullOrEmpty(ab.AuraLingerDurationStr))
{
<div class="col-auto">
<label class="form-label small mb-0">Linger Duration</label>
<select class="form-select form-select-sm" style="width:230px"
@bind="ab.AuraLingerDurationStr" @bind:after="Compile">
@foreach (var d in DurationTypeValues)
{ <option value="@d">@d</option> }
</select>
</div>
@if (ab.AuraLingerDurationStr == "ForNTurns")
{
<div class="col-auto">
<label class="form-label small mb-0">N</label>
<input type="number" min="1" class="form-control form-control-sm" style="width:80px"
@bind="ab.AuraLingerDurationN" @bind:after="Compile" />
</div>
}
}
</div>
}
else if (ab.TypeStr == "Triggered")
{
@@ -759,7 +806,7 @@
<tbody>
<tr><td><strong>Active</strong></td><td>Player explicitly activates on their turn</td><td>Range, Cost, UsesPerTurn</td></tr>
<tr><td><strong>Passive</strong></td><td>Always in effect while unit is on battlefield</td><td>Actions (target=Self)</td></tr>
<tr><td><strong>Aura</strong></td><td>Continuously affects all units in range</td><td>AuraRange, Actions</td></tr>
<tr><td><strong>Aura</strong></td><td>Applies Actions to units entering range; fires OnEnterAuraRange / OnLeaveAuraRange triggers. On Leave: <em>Persist</em> = keep effect duration, <em>Clear</em> = revert immediately, <em>Linger</em> = switch to linger duration</td><td>AuraRange, Actions, ClearOnLeave / LingerDuration</td></tr>
<tr><td><strong>Triggered</strong></td><td>Fires automatically when a specific game event occurs</td><td>TriggerEvent, Actions</td></tr>
</tbody>
</table>
@@ -1186,8 +1233,11 @@
RangeMax = node["Range"]?["Max"]?.GetValue<int>(),
RangeRequiresLos = node["Range"]?["RequiresLos"]?.GetValue<bool>() ?? false,
RangeLinearOnly = node["Range"]?["LinearOnly"]?.GetValue<bool>() ?? false,
AuraRangeMin = node["AuraRange"]?["Min"]?.GetValue<int>(),
AuraRangeMax = node["AuraRange"]?["Max"]?.GetValue<int>(),
AuraRangeMin = node["AuraRange"]?["Min"]?.GetValue<int>(),
AuraRangeMax = node["AuraRange"]?["Max"]?.GetValue<int>(),
AuraClearOnLeave = node["ClearOnLeave"]?.GetValue<bool>() ?? false,
AuraLingerDurationStr = node["LingerDuration"]?["Type"]?.GetValue<string>() ?? "",
AuraLingerDurationN = node["LingerDuration"]?["N"]?.GetValue<int>() ?? 1,
};
if (node["TriggerEvent"] is JsonNode trig)
@@ -1513,6 +1563,14 @@
["Min"] = ab.AuraRangeMin ?? 1,
["Max"] = ab.AuraRangeMax ?? 1,
};
if (!string.IsNullOrEmpty(ab.AuraLingerDurationStr))
{
var ld = new JsonObject { ["Type"] = ab.AuraLingerDurationStr };
if (ab.AuraLingerDurationStr == "ForNTurns") ld["N"] = ab.AuraLingerDurationN;
node["LingerDuration"] = ld;
}
else if (ab.AuraClearOnLeave)
node["ClearOnLeave"] = true;
}
else if (ab.TypeStr == "Triggered" && !string.IsNullOrEmpty(ab.TriggerType))
{
@@ -2502,6 +2560,14 @@
// ── Event helpers ─────────────────────────────────────────────────────────────
/// <param name="mode">0=Persist, 1=ClearOnLeave, 2=Linger(ForNTurns)</param>
private void SetAuraLeaveMode(AbilityDraft ab, int mode)
{
ab.AuraClearOnLeave = mode == 1;
ab.AuraLingerDurationStr = mode == 2 ? "ForNTurns" : string.Empty;
Compile();
}
private void SelectTrigger(AbilityDraft ab, string triggerType)
{
ab.TriggerType = triggerType;
@@ -2585,8 +2651,11 @@
public bool RangeRequiresLos { get; set; } = false;
public bool RangeLinearOnly { get; set; } = false;
public int UsesPerTurn { get; set; } = 1;
public int? AuraRangeMin { get; set; }
public int? AuraRangeMax { get; set; }
public int? AuraRangeMin { get; set; }
public int? AuraRangeMax { get; set; }
public bool AuraClearOnLeave { get; set; } = false;
public string AuraLingerDurationStr { get; set; } = "";
public int AuraLingerDurationN { get; set; } = 1;
public string TriggerType { get; set; } = "";
public string TriggerPlayer { get; set; } = "Owner";
public string TriggerOwnership { get; set; } = "Owner";
+89 -34
View File
@@ -2246,7 +2246,7 @@ public static class AbilitySystem
{
var auraPos = auraUnit.Position!.Value;
int min = def.AuraRange!.Min, max = def.AuraRange.Max;
FireAuraTransition(state, auraOwnerId, auraUnit.InstanceId, movingUnitId,
FireAuraTransition(state, auraOwnerId, auraUnit, def, movingUnitId,
wasIn: from.ManhattanDistance(auraPos) >= min && from.ManhattanDistance(auraPos) <= max,
isIn: to.ManhattanDistance(auraPos) >= min && to.ManhattanDistance(auraPos) <= max);
}
@@ -2272,7 +2272,7 @@ public static class AbilitySystem
.Where(f => f.Position.HasValue && f.InstanceId != movingUnitId).ToList())
{
var otherPos = other.Position!.Value;
FireAuraTransition(state, movingOwnerId, movingUnitId, other.InstanceId,
FireAuraTransition(state, movingOwnerId, movingUnit, def, other.InstanceId,
wasIn: from.ManhattanDistance(otherPos) >= min && from.ManhattanDistance(otherPos) <= max,
isIn: to.ManhattanDistance(otherPos) >= min && to.ManhattanDistance(otherPos) <= max);
}
@@ -2305,12 +2305,7 @@ public static class AbilitySystem
{
int dist = newPos.ManhattanDistance(auraUnit.Position!.Value);
if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max)
FireTrigger(state, new TriggerOccurrence
{
Kind = TriggerEventKind.EnterAuraRange,
ActorId = auraOwnerId,
Data = new TriggerEventData { SourceId = auraUnit.InstanceId, TargetId = newUnitId },
});
FireAuraTransition(state, auraOwnerId, auraUnit, def, newUnitId, wasIn: false, isIn: true);
}
}
@@ -2329,12 +2324,7 @@ public static class AbilitySystem
{
int dist = newPos.ManhattanDistance(other.Position!.Value);
if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max)
FireTrigger(state, new TriggerOccurrence
{
Kind = TriggerEventKind.EnterAuraRange,
ActorId = newUnitOwnerId,
Data = new TriggerEventData { SourceId = newUnitId, TargetId = other.InstanceId },
});
FireAuraTransition(state, newUnitOwnerId, newUnit, def, other.InstanceId, wasIn: false, isIn: true);
}
}
@@ -2364,12 +2354,7 @@ public static class AbilitySystem
{
int dist = leavingPos.ManhattanDistance(auraUnit.Position!.Value);
if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max)
FireTrigger(state, new TriggerOccurrence
{
Kind = TriggerEventKind.LeaveAuraRange,
ActorId = auraOwnerId,
Data = new TriggerEventData { SourceId = auraUnit.InstanceId, TargetId = leavingUnitId },
});
FireAuraTransition(state, auraOwnerId, auraUnit, def, leavingUnitId, wasIn: true, isIn: false);
}
}
@@ -2388,39 +2373,109 @@ public static class AbilitySystem
{
int dist = leavingPos.ManhattanDistance(other.Position!.Value);
if (dist >= def.AuraRange!.Min && dist <= def.AuraRange.Max)
FireTrigger(state, new TriggerOccurrence
{
Kind = TriggerEventKind.LeaveAuraRange,
ActorId = leavingOwnerId,
Data = new TriggerEventData { SourceId = leavingUnitId, TargetId = other.InstanceId },
});
FireAuraTransition(state, leavingOwnerId, leavingUnit, def, other.InstanceId, wasIn: true, isIn: false);
}
}
return state;
}
/// <summary>
/// Executes the aura ability's own Actions on <paramref name="targetUnitId"/> when it enters
/// range. Uses <paramref name="def"/>.Duration if set, otherwise WhileSourceOnBattlefield.
/// </summary>
private static void ApplyAuraEffects(
GameState state,
string auraOwnerId,
FieldCard auraUnit,
AbilityDefinition def,
string targetUnitId)
{
if (def.Actions is not { Length: > 0 }) return;
// Honour aura-level activation conditions (e.g. IsOnTileWithAffinity of the aura source).
var ctx = new AbilityContext { State = state, ActorId = auraOwnerId, Source = auraUnit };
if (def.ActivationConditions is { Length: > 0 } &&
!def.ActivationConditions.All(c => EvaluateCondition(c, ctx, auraUnit)))
return;
var fallback = new DurationModel { Type = DurationType.WhileSourceOnBattlefield };
ExecuteActions(def.Actions, ctx, clientTargets: null,
restrictTargets: new HashSet<string> { targetUnitId },
forceDuration: def.Duration ?? fallback);
}
/// <summary>
/// Handles the leave-range side-effects of an aura.
/// If <see cref="AbilityDefinition.LingerDuration"/> is set, switches tracked effects to that
/// duration instead of revoking them. If <see cref="AbilityDefinition.ClearOnLeave"/> is set,
/// immediately reverts and removes the effects. Otherwise does nothing (effects expire normally).
/// </summary>
private static void HandleAuraLeave(
GameState state,
FieldCard auraUnit,
AbilityDefinition def,
string targetUnitId,
string auraOwnerId)
{
if (def.Actions is not { Length: > 0 }) return;
var matching = state.ActiveEffects
.Where(ae => ae.SourceInstanceId == auraUnit.InstanceId && ae.TargetRef == targetUnitId)
.ToList();
if (matching.Count == 0) return;
if (def.LingerDuration is not null)
{
// Switch the tracked effects to the linger duration so they outlive the aura range.
foreach (var ae in matching)
{
ae.DurationJson = JsonSerializer.Serialize(
new TrackedDuration { Duration = def.LingerDuration, OwnerAccountId = auraOwnerId },
_jsonOpts);
ae.TurnsRemaining = def.LingerDuration.N ?? 0;
}
}
else if (def.ClearOnLeave)
{
foreach (var ae in matching)
{
TrackedDuration? td = null;
try { td = JsonSerializer.Deserialize<TrackedDuration>(ae.DurationJson, _jsonOpts); } catch { }
if (td is not null && !td.IsTick) ApplyReversal(state, ae, td.OwnerAccountId);
state.ActiveEffects.Remove(ae);
}
}
}
private static void FireAuraTransition(
GameState state,
string auraOwnerId,
string auraUnitId,
string targetUnitId,
bool wasIn,
bool isIn)
GameState state,
string auraOwnerId,
FieldCard auraUnit,
AbilityDefinition def,
string targetUnitId,
bool wasIn,
bool isIn)
{
if (!wasIn && isIn)
{
FireTrigger(state, new TriggerOccurrence
{
Kind = TriggerEventKind.EnterAuraRange,
ActorId = auraOwnerId,
Data = new TriggerEventData { SourceId = auraUnitId, TargetId = targetUnitId },
Data = new TriggerEventData { SourceId = auraUnit.InstanceId, TargetId = targetUnitId },
});
ApplyAuraEffects(state, auraOwnerId, auraUnit, def, targetUnitId);
}
else if (wasIn && !isIn)
{
FireTrigger(state, new TriggerOccurrence
{
Kind = TriggerEventKind.LeaveAuraRange,
ActorId = auraOwnerId,
Data = new TriggerEventData { SourceId = auraUnitId, TargetId = targetUnitId },
Data = new TriggerEventData { SourceId = auraUnit.InstanceId, TargetId = targetUnitId },
});
HandleAuraLeave(state, auraUnit, def, targetUnitId, auraOwnerId);
}
}
}
@@ -33,7 +33,19 @@ public sealed class AbilityDefinition
public int UsesPerTurn { get; set; } = 1;
// ── Aura only ────────────────────────────────────────────────────────────
public AbilityRange? AuraRange { get; set; }
public AbilityRange? AuraRange { get; set; }
/// <summary>
/// When true, effects applied by this aura to a unit are reverted immediately when that
/// unit leaves range. Default false: effects expire at their configured Duration.
/// Ignored when <see cref="LingerDuration"/> is set.
/// </summary>
public bool ClearOnLeave { get; set; }
/// <summary>
/// If set, effects switch to this duration when a unit leaves range instead of
/// being reverted immediately. Takes precedence over <see cref="ClearOnLeave"/>.
/// Null means effects expire at their own configured Duration.
/// </summary>
public DurationModel? LingerDuration { get; set; }
// ── Triggered only ───────────────────────────────────────────────────────
/// <summary>The event that fires this ability. Must be set for Triggered type.</summary>