Add AbilitySystem — core effect execution, trigger hooks, activate_ability handler
- AbilitySystem.cs: full rewrite — ActivateAbility, FireTrigger, FireCardEffects, ExecuteActions, ResolveTargets, ExecuteEffect (§5.1–5.10), condition evaluation, filter evaluation, trigger matching, value/affinity resolution - GameState.cs: CardInstance gains AbilitiesJson, EffectsJson, AbilityUsesThisTurn, Genus; PlayerState gains SpAccumulationBonus, DrawPerTurnBonus and 12 action- restriction flags; GameState gains ActiveEffects list; new ActiveEffect class - GameMessages.cs: GameActivateAbilityRequest/Response + AbilityTarget DTO - GameEngine.cs: ActivateAbility public method; trigger hooks in ChannelCard, MoveUnit, Attack (UnitAttack + UnitDeath), Sacrifice, EndTurn (TurnEnd/TurnStart), Ascend, Transition; AbilitiesJson/EffectsJson copied in DefinitionToInstance, BuildPlayerState, ToFieldCard; Repentance/Instinct/Ruin now fire EffectsJson inline - GameActivateAbilityHandler: game.activate_ability message handler - ServiceRegistration: register GameActivateAbilityHandler 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:
@@ -4,6 +4,7 @@ using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using SOR.Database.Entities.Catalog;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Game.Abilities;
|
||||
using SOR.Game.Combat;
|
||||
using SOR.Game.Grid;
|
||||
|
||||
@@ -230,6 +231,21 @@ public static class GameEngine
|
||||
var field = ToFieldCard(card, coord);
|
||||
player.Field.Add(field);
|
||||
board.PlaceUnit(coord, field.InstanceId, actorId);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Channeled,
|
||||
ActorId = actorId,
|
||||
CardType = "Spirit",
|
||||
Data = new TriggerEventData { SourceId = field.InstanceId },
|
||||
});
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitEnterBattlefield,
|
||||
ActorId = actorId,
|
||||
UnitType = "Spirit",
|
||||
Data = new TriggerEventData { SourceId = field.InstanceId },
|
||||
});
|
||||
}
|
||||
else if (card.CardType == "Terramorpha")
|
||||
{
|
||||
@@ -264,15 +280,32 @@ public static class GameEngine
|
||||
AffinitySystem.PushAffinity(board.GetTile(t)!, affinity);
|
||||
|
||||
player.Discard.Add(card);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Channeled,
|
||||
ActorId = actorId,
|
||||
CardType = "Terramorpha",
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
// Repentance, Instinct, Ruin — pay cost and discard.
|
||||
// Full effect resolution deferred to AbilitySystem (future).
|
||||
// Repentance, Instinct, Ruin — pay cost, fire EffectsJson actions, then discard.
|
||||
player.Hand.Remove(card);
|
||||
player.CurrentSp -= card.SpCost;
|
||||
player.CurrentSt -= card.StCost;
|
||||
|
||||
// Fire inline effects if the card has an EffectsJson action list.
|
||||
AbilitySystem.FireCardEffects(state, actorId, card);
|
||||
|
||||
player.Discard.Add(card);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Channeled,
|
||||
ActorId = actorId,
|
||||
CardType = card.CardType,
|
||||
});
|
||||
}
|
||||
|
||||
return state;
|
||||
@@ -333,6 +366,13 @@ public static class GameEngine
|
||||
player.ArchSpirit = newArchSpirit;
|
||||
board.PlaceUnit(coord, newArchSpirit.InstanceId, actorId);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Ascend,
|
||||
ActorId = actorId,
|
||||
Data = new TriggerEventData { SourceId = newArchSpirit.InstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -384,6 +424,13 @@ public static class GameEngine
|
||||
player.ArchSpirit = newArchSpirit;
|
||||
board.PlaceUnit(coord, newArchSpirit.InstanceId, actorId);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Transition,
|
||||
ActorId = actorId,
|
||||
Data = new TriggerEventData { SourceId = newArchSpirit.InstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -449,6 +496,14 @@ public static class GameEngine
|
||||
unit.Position = targetPos;
|
||||
board.MoveUnit(unitInstanceId, targetPos);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitMove,
|
||||
ActorId = actorId,
|
||||
UnitType = unit.CardType,
|
||||
Data = new TriggerEventData { SourceId = unitInstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -504,10 +559,30 @@ public static class GameEngine
|
||||
var activePlayer = state.Players[actorId];
|
||||
var opponentPlayer = state.Players[opponentId];
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitAttack,
|
||||
ActorId = actorId,
|
||||
UnitType = attacker.CardType,
|
||||
Data = new TriggerEventData
|
||||
{
|
||||
SourceId = attackerInstanceId,
|
||||
TargetId = targetInstanceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (attacker.CurrentHealth <= 0)
|
||||
{
|
||||
if (state.Board is not null) state.Board.RemoveUnit(attacker.InstanceId);
|
||||
MoveToDiscard(activePlayer, attacker);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitDeath,
|
||||
ActorId = actorId,
|
||||
UnitType = attacker.CardType,
|
||||
Data = new TriggerEventData { TargetId = attacker.InstanceId },
|
||||
});
|
||||
}
|
||||
|
||||
if (target.CurrentHealth <= 0)
|
||||
@@ -521,6 +596,14 @@ public static class GameEngine
|
||||
{
|
||||
if (state.Board is not null) state.Board.RemoveUnit(target.InstanceId);
|
||||
MoveToDiscard(opponentPlayer, target);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitDeath,
|
||||
ActorId = opponentId,
|
||||
UnitType = target.CardType,
|
||||
Data = new TriggerEventData { TargetId = target.InstanceId },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -559,6 +642,14 @@ public static class GameEngine
|
||||
player.Field.Remove(unit);
|
||||
player.Discard.Add(unit);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.SpiritSacrificed,
|
||||
ActorId = actorId,
|
||||
UnitType = unit.CardType,
|
||||
Data = new TriggerEventData { SourceId = actorId, TargetId = unitInstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -579,12 +670,24 @@ public static class GameEngine
|
||||
ValidateActivePlayer(state, actorId);
|
||||
ValidatePhase(state, GamePhase.Main);
|
||||
|
||||
// Reset exhausted for the player ending their turn.
|
||||
// Reset exhausted and ability uses for the player ending their turn.
|
||||
var active = state.Players[actorId];
|
||||
foreach (var card in active.Field.OfType<FieldCard>())
|
||||
card.IsExhausted = false;
|
||||
{
|
||||
card.IsExhausted = false;
|
||||
card.AbilityUsesThisTurn = null;
|
||||
}
|
||||
if (active.ArchSpirit is not null)
|
||||
active.ArchSpirit.IsExhausted = false;
|
||||
{
|
||||
active.ArchSpirit.IsExhausted = false;
|
||||
active.ArchSpirit.AbilityUsesThisTurn = null;
|
||||
}
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.TurnEnd,
|
||||
ActorId = actorId,
|
||||
});
|
||||
|
||||
// Switch active player.
|
||||
state.ActivePlayerAccountId = GetOpponentId(state, actorId);
|
||||
@@ -605,12 +708,18 @@ public static class GameEngine
|
||||
if (next.ArchSpirit.MaxDefense.HasValue) next.ArchSpirit.CurrentDefense = next.ArchSpirit.MaxDefense;
|
||||
}
|
||||
|
||||
next.CurrentSp += rules.SpAccumulationPerTurn;
|
||||
next.CurrentSp += rules.SpAccumulationPerTurn + next.SpAccumulationBonus;
|
||||
if (rules.SpHasCap && next.CurrentSp > rules.SpCap)
|
||||
next.CurrentSp = rules.SpCap;
|
||||
|
||||
state.Phase = GamePhase.Draw;
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.TurnStart,
|
||||
ActorId = state.ActivePlayerAccountId,
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -621,6 +730,32 @@ public static class GameEngine
|
||||
return state;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Activate an Active ability on a field unit owned by the active player.
|
||||
/// Validates turn / phase, delegates cost + effect resolution to AbilitySystem.
|
||||
/// </summary>
|
||||
public static GameState ActivateAbility(
|
||||
GameState state,
|
||||
string actorId,
|
||||
string sourceInstanceId,
|
||||
int abilityIndex,
|
||||
IReadOnlyList<AbilityTarget>? targets)
|
||||
{
|
||||
ValidateActivePlayer(state, actorId);
|
||||
ValidatePhase(state, GamePhase.Main);
|
||||
|
||||
state = AbilitySystem.ActivateAbility(state, actorId, sourceInstanceId, abilityIndex, targets);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitAbilityActivated,
|
||||
ActorId = actorId,
|
||||
Data = new TriggerEventData { SourceId = sourceInstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
// ── DTO projection ────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
@@ -723,6 +858,8 @@ public static class GameEngine
|
||||
Position = null,
|
||||
Tier = archSpiritDef.Tier,
|
||||
PreviousTierId = archSpiritDef.PreviousTierId,
|
||||
AbilitiesJson = archSpiritDef.AbilitiesJson,
|
||||
EffectsJson = archSpiritDef.EffectsJson,
|
||||
};
|
||||
|
||||
ps.CurrentSp = 0;
|
||||
@@ -760,6 +897,8 @@ public static class GameEngine
|
||||
Tier = def.Tier,
|
||||
PreviousTierId = def.PreviousTierId,
|
||||
TerramorphaMode = def.TerramorphaMode,
|
||||
AbilitiesJson = def.AbilitiesJson,
|
||||
EffectsJson = def.EffectsJson,
|
||||
};
|
||||
|
||||
private static void DrawCards(PlayerState player, int count)
|
||||
@@ -868,6 +1007,8 @@ public static class GameEngine
|
||||
Tier = source.Tier,
|
||||
PreviousTierId = source.PreviousTierId,
|
||||
TerramorphaMode = source.TerramorphaMode,
|
||||
AbilitiesJson = source.AbilitiesJson,
|
||||
EffectsJson = source.EffectsJson,
|
||||
Position = position,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user