Add Ascend, Transition, and Terramorpha implementations

- CardInstance: add Tier, PreviousTierId (ascension chain), TerramorphaMode
- GameEngine.Ascend (§6.7.2): any-zone search, tier+lineage validation, full HP/Def/MP on entry
- GameEngine.Transition (§6.7.1): hand-only, Tier 1 guard, full HP/Def/MP on entry
- GameEngine.ChannelCard: Terramorpha path (Mono/Di/Tri tile count + adjacency + affinity push);
  non-Spirit, non-Terramorpha cards now correctly go to Discard instead of Field
- GamePlayCardRequest: add TargetTiles for multi-tile Terramorpha; handler routes accordingly
- CardInstanceDto: expose Tier and TerramorphaMode to clients
- New handlers: game.ascend, game.transition

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-02-26 12:45:23 -05:00
co-authored by Claude Happy
parent 9b6b7191f0
commit b46f4e9157
7 changed files with 412 additions and 58 deletions
+231 -46
View File
@@ -172,14 +172,18 @@ public static class GameEngine
/// <summary>
/// Channel (play) a card from hand.
///
/// Spirit cards require a TargetTile within the ArchSpirit's channeling range
/// and valid affinity on the target tile. They are placed on the board at that tile.
/// Spirit: targetTiles must have exactly 1 entry — a tile within the ArchSpirit's
/// channeling range with valid affinity. The Spirit is placed there.
///
/// Other card types (Repentance, Instinct, Ruin, Terramorpha) do not require a tile
/// in this initial implementation — effect resolution is handled by AbilitySystem (future).
/// Terramorpha: targetTiles must have N entries matching TerramorphaMode (Mono=1, Di=2,
/// 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
/// cost payment — full effect resolution is deferred to AbilitySystem (future).
/// </summary>
public static GameState ChannelCard(
GameState state, string actorId, string cardInstanceId, Coord? targetTile)
GameState state, string actorId, string cardInstanceId, IReadOnlyList<Coord>? targetTiles)
{
ValidateActivePlayer(state, actorId);
ValidatePhase(state, GamePhase.Main);
@@ -195,10 +199,11 @@ public static class GameEngine
if (card.CardType == "Spirit")
{
if (!targetTile.HasValue) throw new GameException("TARGET_TILE_REQUIRED");
if (targetTiles is null || targetTiles.Count == 0)
throw new GameException("TARGET_TILE_REQUIRED");
var tile = board.GetTile(targetTile.Value)
?? throw new GameException("TILE_OUT_OF_BOUNDS");
var coord = targetTiles[0];
var tile = board.GetTile(coord) ?? throw new GameException("TILE_OUT_OF_BOUNDS");
if (!tile.IsEmpty) throw new GameException("TILE_OCCUPIED");
// Validate channeling range from ArchSpirit.
@@ -207,7 +212,7 @@ public static class GameEngine
var minRange = card.MinChannelRange ?? 1;
var maxRange = card.MaxChannelRange ?? 1;
var dist = archSpirit.Position.Value.ManhattanDistance(targetTile.Value);
var dist = archSpirit.Position.Value.ManhattanDistance(coord);
if (dist < minRange || dist > maxRange)
throw new GameException("OUT_OF_CHANNEL_RANGE");
@@ -217,47 +222,167 @@ public static class GameEngine
if (!AffinitySystem.IsAffinityValid(tile, cardAffinity))
throw new GameException("INVALID_AFFINITY");
}
player.Hand.Remove(card);
player.CurrentSp -= card.SpCost;
player.CurrentSt -= card.StCost;
var field = ToFieldCard(card, coord);
player.Field.Add(field);
board.PlaceUnit(coord, field.InstanceId, actorId);
}
else if (card.CardType == "Terramorpha")
{
int expectedCount = card.TerramorphaMode switch
{
TerramorphaMode.Mono => 1,
TerramorphaMode.Di => 2,
TerramorphaMode.Tri => 3,
_ => throw new GameException("UNKNOWN_TERRAMORPHA_MODE"),
};
if (targetTiles is null || targetTiles.Count != expectedCount)
throw new GameException("INVALID_TILE_COUNT");
// Validate all tiles are in bounds.
foreach (var t in targetTiles)
if (board.GetTile(t) is null)
throw new GameException("TILE_OUT_OF_BOUNDS");
// Di/Tri: each subsequent tile must be orthogonally adjacent to the previous.
for (int i = 1; i < targetTiles.Count; i++)
if (!targetTiles[i].IsOrthogonallyAdjacentTo(targetTiles[i - 1]))
throw new GameException("TILES_NOT_ADJACENT");
player.Hand.Remove(card);
player.CurrentSp -= card.SpCost;
player.CurrentSt -= card.StCost;
// Push card affinity to each targeted tile.
if (Enum.TryParse<Affinity>(card.Affinity, out var affinity))
foreach (var t in targetTiles)
AffinitySystem.PushAffinity(board.GetTile(t)!, affinity);
player.Discard.Add(card);
}
else
{
// Repentance, Instinct, Ruin — pay cost and discard.
// Full effect resolution deferred to AbilitySystem (future).
player.Hand.Remove(card);
player.CurrentSp -= card.SpCost;
player.CurrentSt -= card.StCost;
player.Discard.Add(card);
}
player.Hand.Remove(card);
player.CurrentSp -= card.SpCost;
player.CurrentSt -= card.StCost;
return state;
}
var field = new FieldCard
{
InstanceId = card.InstanceId,
CardDefinitionId = card.CardDefinitionId,
Name = card.Name,
CardType = card.CardType,
Affinity = card.Affinity,
Rarity = card.Rarity,
SpCost = card.SpCost,
StCost = card.StCost,
MaxHealth = card.MaxHealth,
CurrentHealth = card.MaxHealth,
MaxDefense = card.MaxDefense,
CurrentDefense = card.MaxDefense,
Damage = card.Damage,
CanAttack = card.CanAttack,
CanDefend = card.CanDefend,
CanMove = card.CanMove,
MovementType = card.MovementType,
MaxMp = card.MaxMp,
CurrentMp = card.MaxMp ?? 0,
MinAttackRange = card.MinAttackRange,
MaxAttackRange = card.MaxAttackRange,
RequiresLos = card.RequiresLos,
MinChannelRange = card.MinChannelRange,
MaxChannelRange = card.MaxChannelRange,
IsLeader = false,
IsExhausted = false,
Position = targetTile,
};
/// <summary>
/// Ascend the active player's ArchSpirit to the next tier (§6.7.2).
///
/// The incoming ArchSpirit must be exactly one tier above the current and must link
/// back to it via PreviousTierId. It may reside in any zone (hand, deck, or discard).
/// Cost is the incoming ArchSpirit's SP/ST cost.
/// The new ArchSpirit enters at the current position with full HP, Defense, and MP.
/// </summary>
public static GameState Ascend(GameState state, string actorId, string incomingCardInstanceId)
{
ValidateActivePlayer(state, actorId);
ValidatePhase(state, GamePhase.Main);
player.Field.Add(field);
var board = state.Board ?? throw new GameException("BOARD_NOT_INITIALIZED");
var player = state.Players[actorId];
var current = player.ArchSpirit ?? throw new GameException("NO_ARCH_SPIRIT");
if (current.Position is null) throw new GameException("ARCH_SPIRIT_NOT_PLACED");
if (targetTile.HasValue)
board.PlaceUnit(targetTile.Value, field.InstanceId, actorId);
// Find the incoming ArchSpirit in any zone (hand → deck → discard).
var incoming = FindInAnyZone(player, incomingCardInstanceId)
?? throw new GameException("CARD_NOT_FOUND");
if (incoming.CardType != "ArchSpirit")
throw new GameException("NOT_AN_ARCH_SPIRIT");
// Tier chain: incoming must be exactly current.Tier + 1.
int currentTier = current.Tier ?? 1;
if (incoming.Tier != currentTier + 1)
throw new GameException("INVALID_ASCEND_TIER");
// Lineage: incoming.PreviousTierId must reference current's CardDefinitionId.
if (incoming.PreviousTierId != current.CardDefinitionId)
throw new GameException("INVALID_ASCEND_LINEAGE");
// 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");
player.CurrentSp -= incoming.SpCost;
player.CurrentSt -= incoming.StCost;
// Remove incoming from its zone.
RemoveFromAnyZone(player, incoming);
// Build the new FieldCard at the current position.
var coord = current.Position.Value;
var newArchSpirit = ToFieldCard(incoming, coord, isLeader: true);
// Remove current from board; move to discard.
board.RemoveUnit(current.InstanceId);
player.Discard.Add(current);
// Seat the new ArchSpirit.
player.ArchSpirit = newArchSpirit;
board.PlaceUnit(coord, newArchSpirit.InstanceId, actorId);
return state;
}
/// <summary>
/// Transition the active player's ArchSpirit with a Tier 1 ArchSpirit from hand (§6.7.1).
///
/// The incoming must be Tier 1 and must come from hand.
/// Cost is the incoming ArchSpirit's SP/ST cost.
/// The new ArchSpirit enters at the current position with full HP, Defense, and MP.
/// </summary>
public static GameState Transition(GameState state, string actorId, string incomingCardInstanceId)
{
ValidateActivePlayer(state, actorId);
ValidatePhase(state, GamePhase.Main);
var board = state.Board ?? throw new GameException("BOARD_NOT_INITIALIZED");
var player = state.Players[actorId];
var current = player.ArchSpirit ?? throw new GameException("NO_ARCH_SPIRIT");
if (current.Position is null) throw new GameException("ARCH_SPIRIT_NOT_PLACED");
// Incoming must be in hand (§6.7.1).
var incoming = player.Hand.FirstOrDefault(c => c.InstanceId == incomingCardInstanceId)
?? throw new GameException("CARD_NOT_IN_HAND");
if (incoming.CardType != "ArchSpirit")
throw new GameException("NOT_AN_ARCH_SPIRIT");
// Must be Tier 1.
if ((incoming.Tier ?? 1) != 1)
throw new GameException("NOT_TIER_1");
// 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");
player.CurrentSp -= incoming.SpCost;
player.CurrentSt -= incoming.StCost;
player.Hand.Remove(incoming);
// Build the new FieldCard at the current position.
var coord = current.Position.Value;
var newArchSpirit = ToFieldCard(incoming, coord, isLeader: true);
// Remove current from board; move to discard.
board.RemoveUnit(current.InstanceId);
player.Discard.Add(current);
// Seat the new ArchSpirit.
player.ArchSpirit = newArchSpirit;
board.PlaceUnit(coord, newArchSpirit.InstanceId, actorId);
return state;
}
@@ -596,6 +721,8 @@ public static class GameEngine
IsLeader = true,
IsExhausted = false,
Position = null,
Tier = archSpiritDef.Tier,
PreviousTierId = archSpiritDef.PreviousTierId,
};
ps.CurrentSp = 0;
@@ -630,6 +757,9 @@ public static class GameEngine
RequiresLos = def.RequiresLos ?? false,
MinChannelRange = def.MinChannelRange,
MaxChannelRange = def.MaxChannelRange,
Tier = def.Tier,
PreviousTierId = def.PreviousTierId,
TerramorphaMode = def.TerramorphaMode,
};
private static void DrawCards(PlayerState player, int count)
@@ -702,6 +832,59 @@ public static class GameEngine
return blocked;
}
/// <summary>
/// Create a FieldCard from an existing CardInstance, placing it at the given position
/// with full HP, Defense, and MP restored. isLeader=true for ArchSpirits.
/// </summary>
private static FieldCard ToFieldCard(CardInstance source, Coord position, bool isLeader = false)
=> new()
{
InstanceId = source.InstanceId,
CardDefinitionId = source.CardDefinitionId,
Name = source.Name,
CardType = source.CardType,
Affinity = source.Affinity,
Rarity = source.Rarity,
SpCost = source.SpCost,
StCost = source.StCost,
MaxHealth = source.MaxHealth,
CurrentHealth = source.MaxHealth,
MaxDefense = source.MaxDefense,
CurrentDefense = source.MaxDefense,
Damage = source.Damage,
CanAttack = source.CanAttack,
CanDefend = source.CanDefend,
CanMove = source.CanMove,
MovementType = source.MovementType,
MaxMp = source.MaxMp,
CurrentMp = source.MaxMp ?? 0,
MinAttackRange = source.MinAttackRange,
MaxAttackRange = source.MaxAttackRange,
RequiresLos = source.RequiresLos,
MinChannelRange = source.MinChannelRange,
MaxChannelRange = source.MaxChannelRange,
IsLeader = isLeader,
IsExhausted = false,
Tier = source.Tier,
PreviousTierId = source.PreviousTierId,
TerramorphaMode = source.TerramorphaMode,
Position = position,
};
/// <summary>Search hand → deck → discard for a card by instance ID.</summary>
private static CardInstance? FindInAnyZone(PlayerState player, string instanceId)
=> player.Hand.FirstOrDefault(c => c.InstanceId == instanceId)
?? player.Deck.FirstOrDefault(c => c.InstanceId == instanceId)
?? (CardInstance?)player.Discard.FirstOrDefault(c => c.InstanceId == instanceId);
/// <summary>Remove a card from whichever zone holds it (hand, deck, or discard).</summary>
private static void RemoveFromAnyZone(PlayerState player, CardInstance card)
{
if (!player.Hand.Remove(card))
if (!player.Deck.Remove(card))
player.Discard.Remove(card);
}
private static void ValidateActivePlayer(GameState state, string actorId)
{
if (state.ActivePlayerAccountId != actorId)
@@ -735,7 +918,9 @@ public static class GameEngine
? new CoordDto(fc.Position.Value.X, fc.Position.Value.Y) : null,
CurrentMp : fc?.CurrentMp,
MaxMp : c.MaxMp,
MovementType : c.MovementType?.ToString());
MovementType : c.MovementType?.ToString(),
Tier : c.Tier,
TerramorphaMode : c.TerramorphaMode?.ToString());
}
private static BoardDto ToBoardDto(Board board)