Fix board shape resolution and implement true triangular footprint

This commit is contained in:
Shard
2026-03-26 22:47:10 +00:00
parent e9097214fb
commit c8feeb9d5b
2 changed files with 97 additions and 18 deletions
+3 -2
View File
@@ -83,6 +83,7 @@ public static class GameEngine
GameRules? rules = null)
{
var r = rules ?? GameRules.Default;
var resolvedShape = BoardShapeGenerator.NormalizeShape(boardShape, players.Count);
var state = new GameState
{
@@ -93,8 +94,8 @@ public static class GameEngine
PlayerOneAccountId = players[0].accountId.ToString(),
SeatOrder = players.Select(p => p.accountId.ToString()).ToList(),
Phase = GamePhase.Placement,
Rules = r with { PlayerCount = players.Count, BoardShape = boardShape },
Board = BoardShapeGenerator.Generate(boardShape, r, players.Count),
Rules = r with { PlayerCount = players.Count, BoardShape = resolvedShape },
Board = BoardShapeGenerator.Generate(resolvedShape, r, players.Count),
};
foreach (var (accountId, deck, arch, seatIdx, teamIdx) in players)
+94 -16
View File
@@ -1,5 +1,7 @@
namespace SOR.Game.Grid;
using SOR.Database.Enums;
/// <summary>
/// Generates <see cref="Board"/> instances for different board shapes, populating
/// <see cref="Board.StartingZones"/> for each player seat.
@@ -18,16 +20,40 @@ public static class BoardShapeGenerator
_ => "Rectangle", // 2 and 7–10
};
/// <summary>
/// Normalize inbound shape labels to canonical values.
/// Null/empty/"Default" auto-selects by player count.
/// </summary>
public static string NormalizeShape(string? shape, int playerCount)
{
if (string.IsNullOrWhiteSpace(shape) || shape.Equals("Default", StringComparison.OrdinalIgnoreCase))
return AutoShape(playerCount);
return shape.Trim().ToLowerInvariant() switch
{
"rectangle" => "Rectangle",
"cross" => "Cross",
"triangular" => "Triangular",
"triangle" => "Triangular", // alias accepted from older/alternate clients
"radialstar" => "RadialStar",
"hexring" => "HexRing",
_ => "Rectangle",
};
}
/// <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
public static Board Generate(string? shape, GameRules rules, int playerCount)
{
var normalized = NormalizeShape(shape, playerCount);
return normalized switch
{
"Cross" => MakeCross(rules),
"Triangular" => MakeTriangular(rules),
"RadialStar" => MakeRadialStar(rules),
"HexRing" => MakeHexRing(rules),
_ => MakeRectangle(rules, playerCount),
_ => MakeRectangle(rules, playerCount),
};
}
/// <summary>
/// Rectangle board. Starting zones per seat:
@@ -108,26 +134,41 @@ public static class BoardShapeGenerator
/// <summary>
/// Triangular board for 3 players.
/// Uses a standard rectangle with three well-separated starting zones:
/// Seat 0 = bottom-left corner, Seat 1 = bottom-right corner, Seat 2 = top-center.
/// Generates a true triangular footprint (not a full rectangle) within a bounding box.
/// Starting zones:
/// Seat 0 = bottom-left edge
/// Seat 1 = bottom-right edge
/// Seat 2 = top apex area
/// </summary>
private static Board MakeTriangular(GameRules rules)
{
// Prefer a wider board so all three zones have breathing room.
int w = Math.Max(rules.BoardWidth, 12);
int w = Math.Max(rules.BoardWidth, 13);
int h = Math.Max(rules.BoardHeight, 10);
var board = Board.Create(w, h, rules.StartingRandomTilePct);
if (w % 2 == 0) w += 1; // keep a clear center column for the apex
var tiles = new Dictionary<string, Tile>();
int cx = w / 2;
for (int y = 0; y < h; y++)
{
// Half-span shrinks linearly from base (bottom, y=0) to apex (top, y=h-1).
int halfSpan = (int)Math.Round((double)(h - 1 - y) * (w - 1) / (2 * Math.Max(1, h - 1)));
int x0 = Math.Max(0, cx - halfSpan);
int x1 = Math.Min(w - 1, cx + halfSpan);
for (int x = x0; x <= x1; x++)
AddTile(tiles, x, y, rules.StartingRandomTilePct);
}
int zoneW = Math.Max(2, w / 5); // width of each corner zone
var zones = new Dictionary<int, HashSet<Coord>>
{
[0] = CoordsInRect(0, 0, zoneW - 1, 1, w, h), // bottom-left
[1] = CoordsInRect(w - zoneW, 0, w - 1, 1, w, h), // bottom-right
[2] = CoordsInRect(w / 2 - zoneW / 2, h - 2,
w / 2 + zoneW / 2 - 1, h - 1, w, h), // top-center
// Bottom corners (small bands near each end of the base)
[0] = CollectExistingRect(tiles, 0, 0, Math.Max(1, w / 6), Math.Min(1, h - 1)),
[1] = CollectExistingRect(tiles, w - 1 - Math.Max(1, w / 6), 0, w - 1, Math.Min(1, h - 1)),
// Top apex (2 rows centered)
[2] = CollectExistingRect(tiles, cx - 1, Math.Max(0, h - 2), cx + 1, h - 1),
};
return new Board { Width = board.Width, Height = board.Height, Tiles = board.Tiles, StartingZones = zones };
return new Board { Width = w, Height = h, Tiles = tiles, StartingZones = zones };
}
/// <summary>
@@ -246,9 +287,46 @@ public static class BoardShapeGenerator
return s;
}
private static void AddTile(Dictionary<string, Tile> tiles, int x, int y)
private static HashSet<Coord> CollectExistingRect(
Dictionary<string, Tile> tiles, int x0, int y0, int x1, int y1)
{
var s = new HashSet<Coord>();
for (int x = Math.Min(x0, x1); x <= Math.Max(x0, x1); x++)
for (int y = Math.Min(y0, y1); y <= Math.Max(y0, y1); y++)
{
var c = new Coord(x, y);
if (tiles.ContainsKey(c.ToKey()))
s.Add(c);
}
return s;
}
private static readonly Affinity[] _base1Pool =
[Affinity.Lake, Affinity.Hill, Affinity.Forest, Affinity.Field, Affinity.Aether];
private static readonly Affinity[] _base2Pool =
[Affinity.Lake, Affinity.Hill, Affinity.Forest, Affinity.Field, Affinity.Aether, Affinity.Void];
private static void AddTile(Dictionary<string, Tile> tiles, int x, int y, int startingRandomTilePct = 0)
{
var c = new Coord(x, y);
tiles[c.ToKey()] = new Tile { Coord = c };
var tile = new Tile { Coord = c };
var pct = Math.Clamp(startingRandomTilePct, 0, 100);
if (pct > 0 && Random.Shared.Next(100) < pct)
{
var b1 = _base1Pool[Random.Shared.Next(_base1Pool.Length)];
var b2 = Random.Shared.Next(100) < pct
? _base2Pool[Random.Shared.Next(_base2Pool.Length)]
: Affinity.Void;
if (b2 != Affinity.Void)
{
tile.Base1 = b1;
tile.Base2 = b2;
}
}
tiles[c.ToKey()] = tile;
}
}