Resolve merge conflict: wire HexSideLength into main's hex algorithm
Main already had the HexLine+flood-fill implementation. Resolution keeps main's CartToHex/CubeRound local functions and zone logic, but switches the sideLength source from _pendingHexRadius to _pendingHexSideLength and adds _template.HexSideLength = L to persist the new field. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -108,10 +108,10 @@ else
|
||||
<div class="form-text">Number of corners / player slots. 3 = triangle, 6 = hexagon, 10 = decagon.</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label fw-semibold">Radius (tiles)</label>
|
||||
<input type="number" class="form-control" min="2" max="15"
|
||||
@bind="_pendingHexRadius" />
|
||||
<div class="form-text">Board radius from center to corner. Diameter = 2R+1.</div>
|
||||
<label class="form-label fw-semibold">Side Length (tiles)</label>
|
||||
<input type="number" class="form-control" min="1" max="12"
|
||||
@bind="_pendingHexSideLength" />
|
||||
<div class="form-text">Number of hex tiles per polygon edge.</div>
|
||||
</div>
|
||||
<button class="btn btn-outline-secondary btn-sm w-100" @onclick="ApplyHexConfig">
|
||||
Apply
|
||||
@@ -266,9 +266,9 @@ else
|
||||
<div class="card-header d-flex justify-content-between align-items-center">
|
||||
@if (_template.GridType == "Hexagon")
|
||||
{
|
||||
int _hpoly = _template.HexPolygonSides > 0 ? _template.HexPolygonSides : _pendingHexPolySides;
|
||||
int _hradius = _template.HexSides > 1 ? _template.HexSides - 1 : _pendingHexRadius;
|
||||
<span>Canvas <span class="text-muted small">@_hpoly-gon, R=@_hradius (@(2*_hradius+1)×@(2*_hradius+1))</span></span>
|
||||
int _hpoly = _template.HexPolygonSides > 0 ? _template.HexPolygonSides : _pendingHexPolySides;
|
||||
int _hside = _template.HexSideLength > 0 ? _template.HexSideLength : _pendingHexSideLength;
|
||||
<span>Canvas <span class="text-muted small">@_hpoly-gon, L=@_hside tiles/edge</span></span>
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -284,7 +284,7 @@ else
|
||||
const double HexR = 16.0;
|
||||
const double HexPad = 12.0;
|
||||
// hexN = radius + 1 (for HexGrid coordinate math)
|
||||
int hexN = _template.HexSides > 0 ? _template.HexSides : (_pendingHexRadius + 1);
|
||||
int hexN = _template.HexSides > 0 ? _template.HexSides : (_pendingHexSideLength * 2 + 1);
|
||||
int hexDim = 2 * hexN - 1;
|
||||
double svgW = HexR * Math.Sqrt(3) * (hexDim - 1) + 2 * HexR + 2 * HexPad;
|
||||
double svgH = HexR * 1.5 * (hexDim - 1) + 2 * HexR + 2 * HexPad;
|
||||
@@ -406,8 +406,8 @@ else
|
||||
private BoardTemplate _template = new();
|
||||
private int _pendingWidth = 8;
|
||||
private int _pendingHeight = 8;
|
||||
private int _pendingHexPolySides = 6; // polygon corner count (3–10)
|
||||
private int _pendingHexRadius = 5; // board radius in tiles (2–15)
|
||||
private int _pendingHexPolySides = 6; // polygon corner count (3–10)
|
||||
private int _pendingHexSideLength = 5; // tiles per polygon edge (1–12)
|
||||
|
||||
// Only non-default tiles stored; key = "x,y"
|
||||
private Dictionary<string, TileDefinition> _tiles = new();
|
||||
@@ -454,8 +454,8 @@ else
|
||||
_template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow };
|
||||
_pendingWidth = _template.Width;
|
||||
_pendingHeight = _template.Height;
|
||||
_pendingHexPolySides = 6;
|
||||
_pendingHexRadius = 5;
|
||||
_pendingHexPolySides = 6;
|
||||
_pendingHexSideLength = 5;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -469,8 +469,8 @@ else
|
||||
_template = t;
|
||||
_pendingWidth = t.Width;
|
||||
_pendingHeight = t.Height;
|
||||
_pendingHexPolySides = t.HexPolygonSides > 0 ? t.HexPolygonSides : 6;
|
||||
_pendingHexRadius = t.HexSides > 1 ? t.HexSides - 1 : 5;
|
||||
_pendingHexPolySides = t.HexPolygonSides > 0 ? t.HexPolygonSides : 6;
|
||||
_pendingHexSideLength = t.HexSideLength > 0 ? t.HexSideLength : 5;
|
||||
DeserializeTiles(t.TilesJson);
|
||||
DeserializeZones(t.StartingZonesJson);
|
||||
}
|
||||
@@ -542,17 +542,17 @@ else
|
||||
/// <summary>
|
||||
/// Generate a regular N-gon board of hex tiles for any N in [3,10].
|
||||
/// Algorithm:
|
||||
/// 1. Place N corners as a regular N-gon in Cartesian space (scaled by sideLength),
|
||||
/// 1. Place N corners as a regular N-gon in Cartesian space (scaled by HexSideLength),
|
||||
/// then convert each to the nearest hex using cube-coordinate rounding.
|
||||
/// 2. Draw each edge via cube-space lerp so the line follows the hex lattice naturally.
|
||||
/// 3. Flood-fill the interior from (0,0), using the border as walls.
|
||||
/// 4. Mark absent all in-bounds hex cells that are outside the filled region.
|
||||
/// HexSides = bounding radius + 1; HexPolygonSides = N.
|
||||
/// HexSides = bounding radius + 1; HexPolygonSides = N; HexSideLength = tiles per edge.
|
||||
/// </summary>
|
||||
private void ApplyHexConfig()
|
||||
{
|
||||
int N = Math.Clamp(_pendingHexPolySides, 3, 10);
|
||||
int sideLength = Math.Clamp(_pendingHexRadius, 2, 15);
|
||||
int N = Math.Clamp(_pendingHexPolySides, 3, 10);
|
||||
int L = Math.Clamp(_pendingHexSideLength, 1, 12);
|
||||
|
||||
// ── Helpers: cube ↔ axial, rounding, hex-line ────────────────────────────
|
||||
|
||||
@@ -607,17 +607,16 @@ else
|
||||
}
|
||||
|
||||
// ── 1. Compute N corners in axial hex coords ──────────────────────────────
|
||||
// Regular N-gon in Cartesian, radius = sideLength, top-pointing (−π/2 offset).
|
||||
// Regular N-gon in Cartesian, radius = L, top-pointing (−π/2 offset).
|
||||
var corners = new (int q, int r)[N];
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
double angle = -Math.PI / 2.0 + 2.0 * Math.PI * k / N;
|
||||
corners[k] = CartToHex(sideLength * Math.Cos(angle),
|
||||
sideLength * Math.Sin(angle));
|
||||
corners[k] = CartToHex(L * Math.Cos(angle), L * Math.Sin(angle));
|
||||
}
|
||||
|
||||
// ── 2. Bounding hex radius ────────────────────────────────────────────────
|
||||
int R = sideLength;
|
||||
int R = L;
|
||||
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;
|
||||
@@ -661,9 +660,10 @@ else
|
||||
int hexSides = R + 1;
|
||||
|
||||
_pendingHexPolySides = N;
|
||||
_pendingHexRadius = sideLength;
|
||||
_pendingHexSideLength = L;
|
||||
_template.HexSides = hexSides;
|
||||
_template.HexPolygonSides = N;
|
||||
_template.HexSideLength = L;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
|
||||
@@ -681,7 +681,7 @@ else
|
||||
}
|
||||
|
||||
// ── 6. Generate starting zones at the N polygon corners ──────────────────
|
||||
int half = Math.Max(1, sideLength / 4);
|
||||
int half = Math.Max(1, L / 4);
|
||||
for (int playerCount = 2; playerCount <= N; playerCount++)
|
||||
{
|
||||
var seatMap = new Dictionary<int, HashSet<string>>();
|
||||
@@ -721,9 +721,13 @@ else
|
||||
_zones.Clear();
|
||||
if (_template.GridType == "Hexagon")
|
||||
{
|
||||
_template.HexSides = _pendingHexRadius + 1;
|
||||
double circumR = _pendingHexSideLength <= 1 ? 0.0
|
||||
: (_pendingHexSideLength - 1.0) / (2.0 * Math.Sin(Math.PI / _pendingHexPolySides));
|
||||
int R = Math.Max(_pendingHexSideLength * 2, (int)Math.Ceiling(circumR) + 2);
|
||||
R = Math.Clamp(R, 2, 30);
|
||||
_template.HexSides = R + 1;
|
||||
_template.HexPolygonSides = _pendingHexPolySides;
|
||||
int dim = 2 * _pendingHexRadius + 1;
|
||||
int dim = 2 * R + 1;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
}
|
||||
@@ -749,24 +753,28 @@ else
|
||||
return Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))) <= R;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when scaled hex-Cartesian point (cx,cy) is inside a regular N-gon with
|
||||
/// circumradius R. Uses cross-product test on each CCW edge (slightly expanded to
|
||||
/// include boundary tiles).
|
||||
/// </summary>
|
||||
private static bool NPolyInside(double cx, double cy, int N, double R)
|
||||
/// <summary>Round continuous axial (fq,fr) to the nearest integer hex cell (preserves cube constraint).</summary>
|
||||
private static (int q, int r) HexRound(double fq, double fr)
|
||||
{
|
||||
double Rexp = R * 1.02; // slight expansion to include perimeter tiles
|
||||
for (int k = 0; k < N; k++)
|
||||
double fs = -fq - fr;
|
||||
int rq = (int)Math.Round(fq), rr = (int)Math.Round(fr), rs = (int)Math.Round(fs);
|
||||
double dq = Math.Abs(rq - fq), dr = Math.Abs(rr - fr), ds = Math.Abs(rs - fs);
|
||||
if (dq > dr && dq > ds) rq = -rr - rs;
|
||||
else if (dr > ds) rr = -rq - rs;
|
||||
return (rq, rr);
|
||||
}
|
||||
|
||||
/// <summary>Yield all hex cells on the straight line from (q0,r0) to (q1,r1) via cube lerp.</summary>
|
||||
private static IEnumerable<(int q, int r)> HexLinePath(int q0, int r0, int q1, int r1)
|
||||
{
|
||||
int s0 = -q0 - r0, s1 = -q1 - r1;
|
||||
int steps = Math.Max(Math.Abs(q1 - q0), Math.Max(Math.Abs(r1 - r0), Math.Abs(s1 - s0)));
|
||||
for (int i = 0; i <= steps; i++)
|
||||
{
|
||||
double a1 = Math.PI / 2.0 + 2.0 * Math.PI * k / N;
|
||||
double a2 = Math.PI / 2.0 + 2.0 * Math.PI * (k + 1) / N;
|
||||
double v1x = Rexp * Math.Cos(a1), v1y = Rexp * Math.Sin(a1);
|
||||
double v2x = Rexp * Math.Cos(a2), v2y = Rexp * Math.Sin(a2);
|
||||
double cross = (v2x - v1x) * (cy - v1y) - (v2y - v1y) * (cx - v1x);
|
||||
if (cross < -1e-9) return false;
|
||||
double t = steps == 0 ? 0.0 : (double)i / steps;
|
||||
var pt = HexRound(q0 + (q1 - q0) * t, r0 + (r1 - r0) * t);
|
||||
yield return pt;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Find the nearest non-absent hex tile (in offset coords) to scaled-Cartesian (vx,vy).</summary>
|
||||
|
||||
Reference in New Issue
Block a user