Fix: ValueExpr now resolves Target.* and ActionN.Target.* references
Extends ResolveValue to support dynamic references to the resolved target's
properties (SpCost, StCost, Damage, Health, Defense, MP). Also adds Self.SpCost
and Self.StCost.
Usage in ability JSON:
{"Ref": "Target.SpCost"} — current action's target's SP cost
{"Ref": "Action0.Target.SpCost"} — cross-action: action 0's target's SP cost
Enables effects like "return target to hand and refund its channel cost":
Action 0: Target spirit → ReturnToHand
Action 1: Target Owner (Player) → ModifySP {"Ref": "Action0.Target.SpCost"}
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:
@@ -1696,14 +1696,57 @@ public static class AbilitySystem
|
||||
if (expr.Fixed.HasValue) return expr.Fixed.Value;
|
||||
if (expr.Ref is null) return 0;
|
||||
var parts = expr.Ref.Split('.');
|
||||
return (parts[0], parts.Length > 1 ? parts[1] : "") switch
|
||||
var p0 = parts[0];
|
||||
var p1 = parts.Length > 1 ? parts[1] : "";
|
||||
var p2 = parts.Length > 2 ? parts[2] : "";
|
||||
|
||||
// "Self.*" — source unit's properties.
|
||||
if (p0 == "Self")
|
||||
return p1 switch
|
||||
{
|
||||
"Damage" => ctx.Source?.Damage ?? 0,
|
||||
"Health" => ctx.Source?.CurrentHealth ?? 0,
|
||||
"Defense" => ctx.Source?.CurrentDefense ?? 0,
|
||||
"MP" => (ctx.Source as FieldCard)?.CurrentMp ?? 0,
|
||||
"SpCost" => ctx.Source?.SpCost ?? 0,
|
||||
"StCost" => ctx.Source?.StCost ?? 0,
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
// "EventData.*" — data from the triggering event.
|
||||
if (p0 == "EventData")
|
||||
return p1 == "Amount" ? ctx.EventData?.Amount ?? 0 : 0;
|
||||
|
||||
// "Target.*" — first resolved target of the most-recently-resolved action.
|
||||
if (p0 == "Target")
|
||||
{
|
||||
("EventData", "Amount") => ctx.EventData?.Amount ?? 0,
|
||||
("Self", "Damage") => ctx.Source?.Damage ?? 0,
|
||||
("Self", "Health") => ctx.Source?.CurrentHealth ?? 0,
|
||||
("Self", "Defense") => ctx.Source?.CurrentDefense ?? 0,
|
||||
("Self", "MP") => (ctx.Source as FieldCard)?.CurrentMp ?? 0,
|
||||
_ => 0,
|
||||
var target = ctx.ResolvedTargets.Count > 0
|
||||
? ctx.ResolvedTargets.Values.Last().FirstOrDefault()
|
||||
: null;
|
||||
return ResolveTargetProp(target, p1);
|
||||
}
|
||||
|
||||
// "ActionN.Target.*" — first resolved target of action N (cross-action reference).
|
||||
if (p0.StartsWith("Action") && p1 == "Target"
|
||||
&& int.TryParse(p0[6..], out int actionIdx)
|
||||
&& ctx.ResolvedTargets.TryGetValue(actionIdx, out var tgts))
|
||||
return ResolveTargetProp(tgts.FirstOrDefault(), p2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static int ResolveTargetProp(object? target, string prop)
|
||||
{
|
||||
if (target is not CardInstance ci) return 0;
|
||||
return prop switch
|
||||
{
|
||||
"Damage" => ci.Damage ?? 0,
|
||||
"Health" => ci.CurrentHealth ?? 0,
|
||||
"Defense" => ci.CurrentDefense ?? 0,
|
||||
"MP" => (ci as FieldCard)?.CurrentMp ?? 0,
|
||||
"SpCost" => ci.SpCost,
|
||||
"StCost" => ci.StCost,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user