diff --git a/Server/src/SOR.Game/Abilities/AbilityContext.cs b/Server/src/SOR.Game/Abilities/AbilityContext.cs index e3bf264..230467c 100644 --- a/Server/src/SOR.Game/Abilities/AbilityContext.cs +++ b/Server/src/SOR.Game/Abilities/AbilityContext.cs @@ -69,6 +69,8 @@ public enum TriggerEventKind TileAffinityChanged, EnterAuraRange, LeaveAuraRange, + RuinActivation, + UnitOccupyRuin, } /// diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index 513c6aa..8204abe 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -141,6 +141,72 @@ public static class AbilitySystem return state; } + /// + /// At the start of each turn: for every Ruin on the battlefield whose tile is occupied + /// by a Spirit or ArchSpirit, pick ONE of the Ruin's OnRuinActivationTrigger abilities + /// at random and execute it. Then fire RuinActivation for any observer cards. + /// + public static GameState FireRuinAbilities(GameState state) + { + foreach (var (ownerId, player) in state.Players) + { + var ruins = player.Field + .OfType() + .Where(c => c.CardType == "Ruin" && c.Position.HasValue) + .ToList(); + + foreach (var ruin in ruins) + { + var tile = state.Board?.GetTile(ruin.Position!.Value); + if (tile?.OccupantId is null) continue; + + // Only fires if a Spirit or ArchSpirit is on the tile. + var occupantType = FindUnitType(state, tile.OccupantId); + if (occupantType is not ("Spirit" or "ArchSpirit" or "SpiritToken")) continue; + + var abilities = ParseAbilities(ruin.AbilitiesJson); + var ctx = new AbilityContext { State = state, ActorId = ownerId, Source = ruin }; + + var eligible = abilities + .Where(a => a.Type == AbilityType.Triggered + && a.TriggerEvent is OnRuinActivationTrigger) + .Where(a => a.ActivationConditions is not { Length: > 0 } + || a.ActivationConditions.All(c => EvaluateCondition(c, ctx, ruin))) + .ToList(); + + if (eligible.Count == 0) continue; + + // Select exactly one ability at random. + var chosen = eligible[Random.Shared.Next(eligible.Count)]; + ExecuteActions(chosen.Actions, ctx, clientTargets: null); + + // Notify observer cards that the ruin activated. + FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.RuinActivation, + ActorId = ownerId, + Data = new TriggerEventData + { + SourceId = ruin.InstanceId, + TargetId = tile.OccupantId, + }, + }); + } + } + return state; + } + + private static string? FindUnitType(GameState state, string instanceId) + { + foreach (var player in state.Players.Values) + { + var c = player.Field.FirstOrDefault(x => x.InstanceId == instanceId) + ?? player.ArchSpirit as CardInstance; + if (c?.InstanceId == instanceId) return c.CardType; + } + return null; + } + // ── Cost payment ────────────────────────────────────────────────────────── private static void PayCost(CostEntry[] costs, AbilityContext ctx) @@ -699,7 +765,10 @@ public static class AbilitySystem // Fire leave-aura triggers before removing from board. EvaluateAurasOnLeave(ctx.State, unit.InstanceId); - ctx.State.Board?.RemoveUnit(unit.InstanceId); + if (unit.CardType == "Ruin") + ctx.State.Board?.RemoveRuin(unit.InstanceId); + else + ctx.State.Board?.RemoveUnit(unit.InstanceId); RemoveFromAllZones(owner, unit, board: null); // board already handled above unit.CurrentHealth = 0; owner.Discard.Add(unit); @@ -781,7 +850,11 @@ public static class AbilitySystem private static void RemoveFromAllZones(PlayerState player, CardInstance card, Board? board) { - if (board is not null) board.RemoveUnit(card.InstanceId); + if (board is not null) + { + board.RemoveUnit(card.InstanceId); + if (card.CardType == "Ruin") board.RemoveRuin(card.InstanceId); + } player.Hand.Remove(card); player.Deck.Remove(card); player.Field.Remove(card); @@ -989,6 +1062,14 @@ public static class AbilitySystem occurrence.Kind == TriggerEventKind.LeaveAuraRange && Own(lat.AuraOwnership, occurrence, ownerId, state) && UnitOwnerMatches(lat.UnitOwnership, occurrence.Data?.TargetId, state, ownerId), + OnRuinActivationTrigger rat => + occurrence.Kind == TriggerEventKind.RuinActivation + && Own(rat.RuinOwnership, occurrence, ownerId, state) + && (rat.OccupantOwnership is null + || UnitOwnerMatches(rat.OccupantOwnership, occurrence.Data?.TargetId, state, ownerId)), + OnUnitOccupyRuinTrigger uor => + occurrence.Kind == TriggerEventKind.UnitOccupyRuin + && Own(uor.UnitOwnership, occurrence, ownerId, state), _ => false, }; diff --git a/Server/src/SOR.Game/GameEngine.cs b/Server/src/SOR.Game/GameEngine.cs index 30120fd..a2b4800 100644 --- a/Server/src/SOR.Game/GameEngine.cs +++ b/Server/src/SOR.Game/GameEngine.cs @@ -180,7 +180,10 @@ public static class GameEngine /// Tri=3). Each subsequent tile must be orthogonally adjacent to the previous. /// The card's affinity is pushed to each tile; the card then goes to Discard. /// - /// Repentance / Instinct / Ruin: targetTiles may be null. The card goes to Discard after + /// Ruin: placed on a random tile matching the card's affinity (ignores occupancy). + /// Abilities fire at TurnStart if a Spirit/ArchSpirit occupies the tile. + /// + /// Repentance / Instinct: targetTiles may be null. The card goes to Discard after /// cost payment — full effect resolution is deferred to AbilitySystem (future). /// public static GameState ChannelCard( @@ -196,6 +199,27 @@ public static class GameEngine if (player.CurrentSp < card.SpCost) throw new GameException("INSUFFICIENT_SP"); if (player.CurrentSt < card.StCost) throw new GameException("INSUFFICIENT_ST"); + // Ability lock-out checks. + if (card.CardType == "Spirit" && !player.CanChannelSpirit) + throw new GameException("CANNOT_CHANNEL_SPIRIT"); + if (card.CardType == "Repentance" && !player.CanChannelRepentance) + throw new GameException("CANNOT_CHANNEL_REPENTANCE"); + if (card.CardType == "Instinct" && !player.CanChannelInstinct) + throw new GameException("CANNOT_CHANNEL_INSTINCT"); + if (card.CardType == "Ruin" && !player.CanChannelRuin) + throw new GameException("CANNOT_CHANNEL_RUIN"); + if (card.CardType == "Terramorpha") + { + var allowed = card.TerramorphaMode switch + { + TerramorphaMode.Mono => player.CanChannelTerramorphaMono, + TerramorphaMode.Di => player.CanChannelTerramorphaDi, + TerramorphaMode.Tri => player.CanChannelTerramorphaTri, + _ => true, + }; + if (!allowed) throw new GameException("CANNOT_CHANNEL_TERRAMORPHA"); + } + var board = state.Board ?? throw new GameException("BOARD_NOT_INITIALIZED"); if (card.CardType == "Spirit") @@ -289,9 +313,36 @@ public static class GameEngine CardType = "Terramorpha", }); } + else if (card.CardType == "Ruin") + { + // Place on a random tile matching the Ruin's affinity (may already be occupied). + var hasAffinity = Enum.TryParse(card.Affinity, out var ruinAffinity); + var validTiles = board.Tiles.Values + .Where(t => !hasAffinity || AffinitySystem.IsAffinityValid(t, ruinAffinity)) + .ToList(); + if (validTiles.Count == 0) throw new GameException("NO_VALID_TILE"); + + var ruinCoord = validTiles[Random.Shared.Next(validTiles.Count)].Coord; + + player.Hand.Remove(card); + player.CurrentSp -= card.SpCost; + player.CurrentSt -= card.StCost; + + var ruinField = ToFieldCard(card, ruinCoord); + player.Field.Add(ruinField); + board.PlaceRuin(ruinCoord, ruinField.InstanceId, actorId); + + AbilitySystem.FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.Channeled, + ActorId = actorId, + CardType = "Ruin", + Data = new TriggerEventData { SourceId = ruinField.InstanceId }, + }); + } else { - // Repentance, Instinct, Ruin — pay cost, fire EffectsJson actions, then discard. + // Repentance, Instinct — pay cost, fire EffectsJson actions, then discard. player.Hand.Remove(card); player.CurrentSp -= card.SpCost; player.CurrentSt -= card.StCost; @@ -405,6 +456,9 @@ public static class GameEngine if ((incoming.Tier ?? 1) != 1) throw new GameException("NOT_TIER_1"); + if (!player.CanTransitionArchSpirit) + throw new GameException("CANNOT_TRANSITION"); + // Pay the incoming ArchSpirit's cost. if (player.CurrentSp < incoming.SpCost) throw new GameException("INSUFFICIENT_SP"); if (player.CurrentSt < incoming.StCost) throw new GameException("INSUFFICIENT_ST"); @@ -417,13 +471,15 @@ public static class GameEngine var coord = current.Position.Value; var newArchSpirit = ToFieldCard(incoming, coord, isLeader: true); - // Remove current from board; move to discard. + // Remove current from board; fire aura leave before removal. + AbilitySystem.EvaluateAurasOnLeave(state, current.InstanceId); board.RemoveUnit(current.InstanceId); player.Discard.Add(current); - // Seat the new ArchSpirit. + // Seat the new ArchSpirit; fire aura enter after placement. player.ArchSpirit = newArchSpirit; board.PlaceUnit(coord, newArchSpirit.InstanceId, actorId); + AbilitySystem.EvaluateAurasOnEnter(state, newArchSpirit.InstanceId); AbilitySystem.FireTrigger(state, new TriggerOccurrence { @@ -507,6 +563,18 @@ public static class GameEngine Data = new TriggerEventData { SourceId = unitInstanceId }, }); + // Fire UnitOccupyRuin if the destination tile has a Ruin. + var destTileAfterMove = board.GetTile(targetPos); + if (destTileAfterMove?.RuinId is not null) + { + AbilitySystem.FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.UnitOccupyRuin, + ActorId = actorId, + Data = new TriggerEventData { SourceId = unitInstanceId, TargetId = destTileAfterMove.RuinId }, + }); + } + return state; } @@ -731,6 +799,8 @@ public static class GameEngine ActorId = state.ActivePlayerAccountId, }); + AbilitySystem.FireRuinAbilities(state); + return state; } @@ -1083,7 +1153,9 @@ public static class GameEngine Base2 : t.Base2.ToString(), ResolvedAffinity : AffinitySystem.Resolve(t.Base1, t.Base2).ToString(), OccupantId : t.OccupantId, - OccupantOwner : t.OccupantOwner)); + OccupantOwner : t.OccupantOwner, + RuinId : t.RuinId, + RuinOwner : t.RuinOwner)); return new BoardDto(board.Width, board.Height, tiles); } diff --git a/Server/src/SOR.Game/GameMessages.cs b/Server/src/SOR.Game/GameMessages.cs index 86db6d0..5fd7be3 100644 --- a/Server/src/SOR.Game/GameMessages.cs +++ b/Server/src/SOR.Game/GameMessages.cs @@ -246,7 +246,9 @@ public sealed record TileDto( string Base2, string ResolvedAffinity, string? OccupantId, - string? OccupantOwner); + string? OccupantOwner, + string? RuinId, + string? RuinOwner); /// Client-facing 2D coordinate. public sealed record CoordDto(int X, int Y); diff --git a/Server/src/SOR.Game/Grid/Board.cs b/Server/src/SOR.Game/Grid/Board.cs index 0255c91..c195cec 100644 --- a/Server/src/SOR.Game/Grid/Board.cs +++ b/Server/src/SOR.Game/Grid/Board.cs @@ -75,6 +75,22 @@ public sealed class Board src.OccupantOwner = null; } + /// Place a Ruin on a tile. Does not check occupancy — Ruins share tiles with units. + public void PlaceRuin(Coord c, string instanceId, string ownerAccountId) + { + var tile = GetTile(c) ?? throw new GameException($"TILE_OUT_OF_BOUNDS:{c}"); + tile.RuinId = instanceId; + tile.RuinOwner = ownerAccountId; + } + + public void RemoveRuin(string instanceId) + { + var tile = Tiles.Values.FirstOrDefault(t => t.RuinId == instanceId); + if (tile is null) return; + tile.RuinId = null; + tile.RuinOwner = null; + } + /// /// Build a coord→instanceId lookup for all occupied tiles. /// Used by pathfinding and LOS checks. diff --git a/Server/src/SOR.Game/Grid/Tile.cs b/Server/src/SOR.Game/Grid/Tile.cs index 32fc7a7..17c8e9f 100644 --- a/Server/src/SOR.Game/Grid/Tile.cs +++ b/Server/src/SOR.Game/Grid/Tile.cs @@ -30,4 +30,10 @@ public sealed class Tile public string? OccupantOwner { get; set; } public bool IsEmpty => OccupantId is null; + + // ── Ruin ────────────────────────────────────────────────────────────────── + + /// InstanceId of a Ruin on this tile, or null if none. Independent of OccupantId. + public string? RuinId { get; set; } + public string? RuinOwner { get; set; } }