Add square grid system, positional combat, sacrifice, and Placement phase

Grid infrastructure (SOR.Game/Grid/):
- Coord — immutable (X,Y) struct with Manhattan distance, adjacency, BFS helpers
- Tile — mutable affinity slots (Base1/Base2) + occupancy tracking
- Board — 8×8 (configurable) grid, PlaceUnit/RemoveUnit/MoveUnit/BuildOccupancyMap
- Pathfinding — Walk/Flight BFS (GetReachableWithCost), Jump/Teleport range (GetJumpRange)
- LineOfSight — supercover DDA; occupied intermediate tiles block LOS
- AffinitySystem — full Resolve table for all 20+ affinities per affinity.md,
  PushAffinity, ApplyPlacementAffinity, IsAffinityValid (basic vs non-basic rules)

Combat (SOR.Game/Combat/):
- AttackSystem — Manhattan range + optional LOS validation (§6.10)
- SacrificeSystem — exact ST return formula from gameplay.md §6.15

Abilities (SOR.Game/Abilities/):
- AbilitySystem stub — future pass per abilities.md

Game engine / state changes:
- GamePhase: added Placement phase (before Draw)
- GameRuleSet + GameRules: added BoardWidth/BoardHeight (default 8×8)
- GameState: added Board, PlayerOneAccountId; CardInstance gains MovementType,
  MaxMp, MinAttackRange, MaxAttackRange, RequiresLos, MinChannelRange,
  MaxChannelRange, IsLeader (moved from FieldCard); FieldCard adds Position + CurrentMp
- [JsonPolymorphic] on CardInstance so FieldCard survives JSONB round-trip
- JsonStringEnumConverter added to GameEngine serializer options
- GameEngine.Initialize: creates Board + sets Placement phase; no opening draw yet
- GameEngine.PlaceArchSpirit: validates row, pushes affinity, triggers opening draw
  and SP gain when both players placed
- GameEngine.ChannelCard (was PlayCard): validates channeling range + affinity for Spirits
- GameEngine.MoveUnit: Walk/Flight BFS, Jump (1 MP/landing), Teleport (LOS + 1 MP)
- GameEngine.Attack: adds range+LOS validation via AttackSystem
- GameEngine.Sacrifice: adjacency + SacrificeSystem ST formula
- GameEngine.EndTurn: now also restores MP (§6.0 step 1) for next player
- ToDto: includes Board tiles with resolved affinities; CardInstanceDto gets Position,
  CurrentMp, MaxMp, MovementType

New handlers: game.place_arch_spirit, game.move_unit, game.sacrifice
Updated handlers: game.play_card (target tile), GameReadyHandler (new Initialize sig)
Migration: AddBoardDimensions (board_width/board_height on game_rule_sets)

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 11:22:30 -05:00
co-authored by Claude Happy
parent 7ab11e6f46
commit 9b6b7191f0
27 changed files with 1739 additions and 127 deletions
+54 -15
View File
@@ -1,6 +1,8 @@
namespace SOR.Game;
using System.Text.Json.Serialization;
using SOR.Database.Enums;
using SOR.Game.Grid;
/// <summary>
/// Full live game state — serialized as JSONB in GameSession.StateJson.
@@ -15,14 +17,23 @@ public sealed class GameState
/// <summary>AccountId (as string) of the player whose turn it currently is.</summary>
public string ActivePlayerAccountId { get; set; } = string.Empty;
/// <summary>AccountId (as string) of the first player (P1). Determines placement row.</summary>
public string PlayerOneAccountId { get; set; } = string.Empty;
/// <summary>Per-player state, keyed by AccountId.ToString().</summary>
public Dictionary<string, PlayerState> Players { get; set; } = new();
/// <summary>Running sequence number for action log entries.</summary>
/// <summary>Running sequence number for action log / StateVersion.</summary>
public int NextActionSequence { get; set; } = 1;
/// <summary>Rules snapshot applied to this game session.</summary>
public GameRules Rules { get; set; } = GameRules.Default;
/// <summary>
/// The battlefield grid. Null only before the game session is initialized.
/// Created during GameEngine.Initialize and populated during the Placement phase.
/// </summary>
public Board? Board { get; set; }
}
public sealed class PlayerState
@@ -45,29 +56,35 @@ public sealed class PlayerState
public int CurrentSp { get; set; }
/// <summary>
/// Sacrifice Tokens — earned only by sacrificing Spirits (never granted per-turn).
/// Starts at 0.
/// Sacrifice Tokens — earned by sacrificing Spirits (§6.15).
/// Starts at 0; never granted per turn.
/// </summary>
public int CurrentSt { get; set; }
// ── Leader ───────────────────────────────────────────────────────────────
/// <summary>
/// The player's ArchSpirit — starts on the field at game start.
/// The player's ArchSpirit — placed during the Placement phase.
/// If destroyed the game ends (this player loses).
/// </summary>
public FieldCard? ArchSpirit { get; set; }
}
/// <summary>
/// A card instance in play. Carries mutable combat state.
/// A card instance in any zone. Carries mutable combat and positional state.
///
/// [JsonPolymorphic] is required so that FieldCard sub-type properties
/// survive the GameState JSONB serialize/deserialize round-trip.
/// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$t")]
[JsonDerivedType(typeof(CardInstance), "c")]
[JsonDerivedType(typeof(FieldCard), "f")]
public class CardInstance
{
/// <summary>Unique instance ID within the game session (not a DB id).</summary>
public string InstanceId { get; set; } = Guid.NewGuid().ToString("N")[..8];
// ── Definition mirror (denormalized for performance) ─────────────────────
// ── Definition mirror ─────────────────────────────────────────────────────
public Guid CardDefinitionId { get; set; }
public string Name { get; set; } = string.Empty;
@@ -77,7 +94,7 @@ public class CardInstance
public int SpCost { get; set; }
public int StCost { get; set; }
// ── Combat stats ─────────────────────────────────────────────────────────
// ── Combat stats ──────────────────────────────────────────────────────────
public int? MaxHealth { get; set; }
public int? CurrentHealth { get; set; }
@@ -85,22 +102,44 @@ public class CardInstance
public int? CurrentDefense { get; set; }
public int? Damage { get; set; }
// ── State flags ──────────────────────────────────────────────────────────
// ── Movement ──────────────────────────────────────────────────────────────
/// <summary>True if the card has already attacked or used an ability this turn.</summary>
public MovementType? MovementType { get; set; }
public int? MaxMp { get; set; }
public bool CanMove { get; set; }
// ── Attack ────────────────────────────────────────────────────────────────
public int? MinAttackRange { get; set; }
public int? MaxAttackRange { get; set; }
public bool RequiresLos { get; set; }
// ── Channeling ────────────────────────────────────────────────────────────
public int? MinChannelRange { get; set; }
public int? MaxChannelRange { get; set; }
// ── State flags ───────────────────────────────────────────────────────────
/// <summary>True if this unit has already attacked or used an ability this turn.</summary>
public bool IsExhausted { get; set; }
// ── Field flags ──────────────────────────────────────────────────────────
/// <summary>True for the player's ArchSpirit (the win-condition target).</summary>
public bool IsLeader { get; set; }
public bool CanAttack { get; set; }
public bool CanDefend { get; set; }
public bool CanAttack { get; set; }
public bool CanDefend { get; set; }
}
/// <summary>
/// A CardInstance that is on the field (includes the ArchSpirit leader).
/// A CardInstance that is on the field (placed by ChannelCard or PlaceArchSpirit).
/// Carries positional state (tile coordinate) and remaining movement points.
/// </summary>
public sealed class FieldCard : CardInstance
{
/// <summary>True for the player's starting ArchSpirit (the win-condition target).</summary>
public bool IsLeader { get; set; }
/// <summary>Current tile position on the board. Null until placed.</summary>
public Coord? Position { get; set; }
/// <summary>Remaining movement points this turn.</summary>
public int CurrentMp { get; set; }
}