Implement multiplayer extension: 2-10 players FFA/Team/Multi-Team
Core engine: - Add SeatIndex, TeamIndex, IsEliminated to PlayerState - Add SeatOrder, GameMode to GameState with helpers: ActiveSeats(), GetNextActiveSeat(), GetEnemyIds(), AreAllies() - Add StartingZones to Board for per-seat placement validation - Add BoardShape, PlayerCount to GameRules - New BoardShapeGenerator: Rectangle (up to 4 seats) + Cross shapes - N-player Initialize() overload; old 2-player overload delegates to it - EndTurn uses GetNextActiveSeat() instead of hardcoded GetOpponentId() - Attack finds target across all enemy players; EliminatePlayer() helper allows multi-player elimination without immediate game over - PlaceArchSpirit validates via StartingZones (legacy row fallback preserved) - AbilitySystem.Own() uses AreAllies() for team-aware ownership checks Database: - GameSession: +player_count, +game_mode, +additional_players_json - CustomLobby: +game_mode, +team_count, +additional_players_json - EF migration: AddMultiplayerSlots (applied) Handlers: - GameCreateCustomLobbyHandler: honours MaxPlayers/GameMode/TeamCount from request - GameJoinCustomLobbyHandler: multi-slot join up to MaxPlayers - GameLobbyReadyHandler: N-player all-ready check; seats/team assignment by mode - GameReadyHandler: builds N-player list from AdditionalPlayersJson - GameHandlerBase.LoadActiveSessionAsync: recognises additional players Client: - Game.gd: get_opponents() returns all non-local players; get_opponent() kept as backward-compat alias returning first opponent 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:
@@ -0,0 +1,109 @@
|
||||
namespace SOR.Game.Grid;
|
||||
|
||||
/// <summary>
|
||||
/// Generates <see cref="Board"/> instances for different board shapes, populating
|
||||
/// <see cref="Board.StartingZones"/> for each player seat.
|
||||
/// </summary>
|
||||
public static class BoardShapeGenerator
|
||||
{
|
||||
/// <summary>Generate a board appropriate for the given shape, rules, and player count.</summary>
|
||||
public static Board Generate(string shape, GameRules rules, int playerCount) =>
|
||||
shape switch
|
||||
{
|
||||
"Cross" => MakeCross(rules),
|
||||
_ => MakeRectangle(rules, playerCount),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Rectangle board. Starting zones per seat:
|
||||
/// Seat 0 = rows 0–1 (bottom edge)
|
||||
/// Seat 1 = rows Height-2 .. Height-1 (top edge)
|
||||
/// Seat 2 = cols 0–1 (left edge)
|
||||
/// Seat 3 = cols Width-2 .. Width-1 (right edge)
|
||||
/// </summary>
|
||||
private static Board MakeRectangle(GameRules rules, int playerCount)
|
||||
{
|
||||
var board = Board.Create(rules.BoardWidth, rules.BoardHeight, rules.StartingRandomTilePct);
|
||||
int w = rules.BoardWidth, h = rules.BoardHeight;
|
||||
|
||||
var zones = new Dictionary<int, HashSet<Coord>>();
|
||||
|
||||
// Seat 0 = bottom 2 rows
|
||||
zones[0] = CoordsInRows(0, 1, w);
|
||||
if (playerCount >= 2)
|
||||
zones[1] = CoordsInRows(h - 2, h - 1, w);
|
||||
if (playerCount >= 3)
|
||||
zones[2] = CoordsInCols(0, 1, h);
|
||||
if (playerCount >= 4)
|
||||
zones[3] = CoordsInCols(w - 2, w - 1, h);
|
||||
|
||||
// Return a new Board with the zones attached (Board is a reference type with mutable Tiles
|
||||
// so we create a wrapper that shares the same Tiles dictionary).
|
||||
return new Board { Width = board.Width, Height = board.Height, Tiles = board.Tiles, StartingZones = zones };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cross (plus-sign) board for 4 players.
|
||||
/// A square center arena with 4 arms; the tip of each arm is one player's starting zone.
|
||||
/// </summary>
|
||||
private static Board MakeCross(GameRules rules)
|
||||
{
|
||||
const int arm = 4;
|
||||
const int center = 5;
|
||||
int total = center + arm * 2; // 13 in each dimension
|
||||
|
||||
var tiles = new Dictionary<string, Tile>();
|
||||
|
||||
// Center square
|
||||
for (int x = arm; x < arm + center; x++)
|
||||
for (int y = arm; y < arm + center; y++)
|
||||
AddTile(tiles, x, y);
|
||||
|
||||
int mid = arm + center / 2; // midpoint column/row of the center section
|
||||
|
||||
// Bottom arm
|
||||
for (int i = 0; i < arm; i++) AddTile(tiles, mid, i);
|
||||
// Top arm
|
||||
for (int i = 0; i < arm; i++) AddTile(tiles, mid, arm + center + i);
|
||||
// Left arm
|
||||
for (int i = 0; i < arm; i++) AddTile(tiles, i, mid);
|
||||
// Right arm
|
||||
for (int i = 0; i < arm; i++) AddTile(tiles, arm + center + i, mid);
|
||||
|
||||
var zones = new Dictionary<int, HashSet<Coord>>
|
||||
{
|
||||
[0] = new() { new Coord(mid, 0) }, // bottom tip
|
||||
[1] = new() { new Coord(mid, arm + center - 1 + arm) }, // top tip
|
||||
[2] = new() { new Coord(0, mid) }, // left tip
|
||||
[3] = new() { new Coord(arm + center - 1 + arm, mid) }, // right tip
|
||||
};
|
||||
|
||||
return new Board { Width = total, Height = total, Tiles = tiles, StartingZones = zones };
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private static HashSet<Coord> CoordsInRows(int r0, int r1, int width)
|
||||
{
|
||||
var s = new HashSet<Coord>();
|
||||
for (int x = 0; x < width; x++)
|
||||
for (int y = r0; y <= r1; y++)
|
||||
s.Add(new Coord(x, y));
|
||||
return s;
|
||||
}
|
||||
|
||||
private static HashSet<Coord> CoordsInCols(int c0, int c1, int height)
|
||||
{
|
||||
var s = new HashSet<Coord>();
|
||||
for (int x = c0; x <= c1; x++)
|
||||
for (int y = 0; y < height; y++)
|
||||
s.Add(new Coord(x, y));
|
||||
return s;
|
||||
}
|
||||
|
||||
private static void AddTile(Dictionary<string, Tile> tiles, int x, int y)
|
||||
{
|
||||
var c = new Coord(x, y);
|
||||
tiles[c.ToKey()] = new Tile { Coord = c };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user