Replace geometric NPolyInside test with walk-and-flood N-gon algorithm
ApplyHexConfig now: 1. Picks the closest hex direction (axial, pointy-top) to each of the N evenly-spaced target angles (360°/N · k). 2. Walks sideLength steps per side from pre-computed centred corners. 3. Bridges any gap between the end of one side and the start of the next by greedily stepping toward the target corner. 4. Flood-fills the interior from (0,0) stopping at the border ring. 5. Marks only the out-of-shape in-bounds tiles as absent; present tiles stay as default (no _tiles entry needed). N=3 (triangle) and N=6 (hexagon) produce exact shapes with no bridging; other N values approximate with minimal bridging tiles. Zone generation updated to use the actual axial corner positions instead of Cartesian vertex approximations. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
d28d02428d
commit
d00d7150bb
@@ -540,82 +540,172 @@ else
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a regular N-gon of hex tiles and N corner starting zones.
|
||||
/// HexSides is stored as radius+1 (for HexGrid coordinate math).
|
||||
/// HexPolygonSides is stored as N (the corner count / polygon shape).
|
||||
/// Generate a regular N-gon board of hex tiles.
|
||||
/// Each of the N sides is a straight run of sideLength tiles along the nearest hex-grid
|
||||
/// direction to 360°/N·k. Corners are connected with bridging tiles when the walk doesn't
|
||||
/// land exactly on the next corner. The interior is flood-filled from the centre.
|
||||
/// HexSides = bounding radius + 1; HexPolygonSides = N.
|
||||
/// </summary>
|
||||
private void ApplyHexConfig()
|
||||
{
|
||||
int N = Math.Clamp(_pendingHexPolySides, 3, 10);
|
||||
int R = Math.Clamp(_pendingHexRadius, 2, 15);
|
||||
int sides = R + 1; // HexGrid "sides" parameter = radius + 1
|
||||
int dim = 2 * R + 1;
|
||||
int N = Math.Clamp(_pendingHexPolySides, 3, 10);
|
||||
int sideLength = Math.Clamp(_pendingHexRadius, 2, 15);
|
||||
|
||||
_pendingHexPolySides = N;
|
||||
_pendingHexRadius = R;
|
||||
_template.HexSides = sides;
|
||||
_template.HexPolygonSides = N;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
// Six axial directions for a pointy-top hex grid, with their angles (degrees).
|
||||
// Angle convention: x_cart = q + r·½, y_cart = −r·√3/2 (y-up / screen-inverted).
|
||||
(int dq, int dr)[] hexDirs = { (1,0), (1,-1), (0,-1), (-1,0), (-1,1), (0,1) };
|
||||
double[] hexAngles = { 0, 60, 120, 180, 240, 300 };
|
||||
|
||||
// ── 1. Choose one hex direction per side ─────────────────────────────────
|
||||
var sideDirs = new (int dq, int dr)[N];
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
double target = 360.0 * k / N;
|
||||
int best = 0;
|
||||
double bestDiff = double.MaxValue;
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
double diff = (hexAngles[i] - target + 360) % 360;
|
||||
if (diff > 180) diff = 360 - diff;
|
||||
if (diff < bestDiff) { bestDiff = diff; best = i; }
|
||||
}
|
||||
sideDirs[k] = hexDirs[best];
|
||||
}
|
||||
|
||||
// ── 2. Compute corner positions in axial coords, centred at origin ───────
|
||||
var rawCorners = new (int q, int r)[N];
|
||||
int cqAcc = 0, crAcc = 0;
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
rawCorners[k] = (cqAcc, crAcc);
|
||||
cqAcc += sideLength * sideDirs[k].dq;
|
||||
crAcc += sideLength * sideDirs[k].dr;
|
||||
}
|
||||
double avgQ = rawCorners.Average(c => (double)c.q);
|
||||
double avgR = rawCorners.Average(c => (double)c.r);
|
||||
int shiftQ = -(int)Math.Round(avgQ);
|
||||
int shiftR = -(int)Math.Round(avgR);
|
||||
var corners = new (int q, int r)[N];
|
||||
for (int k = 0; k < N; k++)
|
||||
corners[k] = (rawCorners[k].q + shiftQ, rawCorners[k].r + shiftR);
|
||||
|
||||
// ── 3. Bounding hex radius ────────────────────────────────────────────────
|
||||
int R = sideLength;
|
||||
foreach (var (q, r) in corners)
|
||||
R = Math.Max(R, Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))));
|
||||
R += 1; // one-cell padding so no board tile touches the canvas edge
|
||||
|
||||
// ── 4. Walk perimeter, bridging any gap at each corner ───────────────────
|
||||
var borderSet = new HashSet<(int q, int r)>();
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
var (sq, sr) = corners[k];
|
||||
var (eq, er) = corners[(k + 1) % N];
|
||||
var (dq, dr) = sideDirs[k];
|
||||
|
||||
int pq = sq, pr = sr;
|
||||
for (int step = 0; step < sideLength; step++)
|
||||
{
|
||||
borderSet.Add((pq, pr));
|
||||
pq += dq;
|
||||
pr += dr;
|
||||
}
|
||||
|
||||
// Bridge from (pq,pr) → (eq,er) if the walk didn't land exactly there.
|
||||
int safety = 0;
|
||||
while ((pq != eq || pr != er) && safety++ < sideLength * 3)
|
||||
{
|
||||
borderSet.Add((pq, pr));
|
||||
int bestBdq = 0, bestBdr = 0;
|
||||
double bestDist = double.MaxValue;
|
||||
foreach (var (hdq, hdr) in hexDirs)
|
||||
{
|
||||
int nq = pq + hdq, nr = pr + hdr;
|
||||
double d = Math.Max(Math.Abs(eq - nq),
|
||||
Math.Max(Math.Abs(er - nr),
|
||||
Math.Abs((eq - nq) + (er - nr))));
|
||||
if (d < bestDist) { bestDist = d; bestBdq = hdq; bestBdr = hdr; }
|
||||
}
|
||||
pq += bestBdq;
|
||||
pr += bestBdr;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 5. Flood-fill the interior from the centre tile (0,0) ────────────────
|
||||
var present = new HashSet<(int q, int r)>(borderSet);
|
||||
var queue = new Queue<(int q, int r)>();
|
||||
if (!borderSet.Contains((0, 0)))
|
||||
{
|
||||
present.Add((0, 0));
|
||||
queue.Enqueue((0, 0));
|
||||
}
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var (q, r) = queue.Dequeue();
|
||||
foreach (var (hdq, hdr) in hexDirs)
|
||||
{
|
||||
int nq = q + hdq, nr = r + hdr;
|
||||
if (present.Contains((nq, nr))) continue;
|
||||
if (Math.Max(Math.Abs(nq), Math.Max(Math.Abs(nr), Math.Abs(nq + nr))) > R) continue;
|
||||
present.Add((nq, nr));
|
||||
if (!borderSet.Contains((nq, nr)))
|
||||
queue.Enqueue((nq, nr));
|
||||
}
|
||||
}
|
||||
|
||||
// ── 6. Commit to template ─────────────────────────────────────────────────
|
||||
int dim = 2 * R + 1;
|
||||
int hexSides = R + 1;
|
||||
|
||||
_pendingHexPolySides = N;
|
||||
_pendingHexRadius = sideLength;
|
||||
_template.HexSides = hexSides;
|
||||
_template.HexPolygonSides = N;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
|
||||
_tiles.Clear();
|
||||
_zones.Clear();
|
||||
|
||||
// Mark every tile that is inside the hexagonal arrangement but outside the N-gon as absent.
|
||||
// Tiles inside the N-gon are left as default (no entry = void tile = present).
|
||||
// Tiles in-bounds but outside the N-gon are marked absent; present tiles are default.
|
||||
for (int x = 0; x < dim; x++)
|
||||
for (int y = 0; y < dim; y++)
|
||||
{
|
||||
if (!IsHexInBounds(x, y, hexSides)) continue;
|
||||
int q = x - R, r = y - R;
|
||||
if (Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))) > R)
|
||||
continue; // outside hex bounding circle — not rendered
|
||||
|
||||
double cx = q + r * 0.5;
|
||||
double cy = r * Math.Sqrt(3.0) / 2.0;
|
||||
if (!NPolyInside(cx, cy, N, R))
|
||||
{
|
||||
var tile = GetOrCreateTile(x, y);
|
||||
tile.IsAbsent = true;
|
||||
}
|
||||
if (!present.Contains((q, r)))
|
||||
GetOrCreateTile(x, y).IsAbsent = true;
|
||||
}
|
||||
|
||||
// N vertex positions (scaled hex Cartesian)
|
||||
var verts = new (double vx, double vy)[N];
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
double angle = Math.PI / 2.0 + 2.0 * Math.PI * k / N;
|
||||
verts[k] = (R * Math.Cos(angle), R * Math.Sin(angle));
|
||||
}
|
||||
|
||||
// For each player count 2..N, pick evenly-spaced corners.
|
||||
int half = Math.Max(1, R / 4);
|
||||
// ── 7. Generate starting zones at the N polygon corners ──────────────────
|
||||
int half = Math.Max(1, sideLength / 4);
|
||||
for (int playerCount = 2; playerCount <= N; playerCount++)
|
||||
{
|
||||
var seatMap = new Dictionary<int, HashSet<string>>();
|
||||
for (int seat = 0; seat < playerCount; seat++)
|
||||
{
|
||||
int ci = (int)Math.Round((double)seat * N / playerCount) % N;
|
||||
var (vx, vy) = verts[ci];
|
||||
int ci = (int)Math.Round((double)seat * N / playerCount) % N;
|
||||
int nextCi = (ci + 1) % N;
|
||||
int prevCi = (ci - 1 + N) % N;
|
||||
var (cqz, crz) = corners[ci];
|
||||
|
||||
var zone = new HashSet<string>();
|
||||
zone.Add(TileKey(cqz + R, crz + R));
|
||||
|
||||
// Corner tile
|
||||
var corner = FindNearestNgonTile(vx, vy, R);
|
||||
zone.Add(TileKey(corner.x, corner.y));
|
||||
|
||||
// Edge tiles toward adjacent vertices
|
||||
// Spread slightly along the two edges meeting at this corner.
|
||||
for (int step = 1; step <= half; step++)
|
||||
{
|
||||
double t = (double)step / (half + 1);
|
||||
int nextCi = (ci + 1) % N;
|
||||
int prevCi = (ci - 1 + N) % N;
|
||||
var n1 = FindNearestNgonTile(vx + (verts[nextCi].vx - vx) * t,
|
||||
vy + (verts[nextCi].vy - vy) * t, R);
|
||||
var n2 = FindNearestNgonTile(vx + (verts[prevCi].vx - vx) * t,
|
||||
vy + (verts[prevCi].vy - vy) * t, R);
|
||||
zone.Add(TileKey(n1.x, n1.y));
|
||||
zone.Add(TileKey(n2.x, n2.y));
|
||||
var (ncq, ncr) = corners[nextCi];
|
||||
var (pcq, pcr) = corners[prevCi];
|
||||
zone.Add(TileKey((int)Math.Round(cqz + (ncq - cqz) * t) + R,
|
||||
(int)Math.Round(crz + (ncr - crz) * t) + R));
|
||||
zone.Add(TileKey((int)Math.Round(cqz + (pcq - cqz) * t) + R,
|
||||
(int)Math.Round(crz + (pcr - crz) * t) + R));
|
||||
}
|
||||
|
||||
// Drop any zone tile that ended up on an absent cell.
|
||||
zone.RemoveWhere(k => _tiles.TryGetValue(k, out var td) && td.IsAbsent);
|
||||
seatMap[seat] = zone;
|
||||
}
|
||||
_zones[playerCount] = seatMap;
|
||||
|
||||
Reference in New Issue
Block a user