A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
tools: coast_measure, the sea-round measuring tool, finds the shore where the sea ends (refs #388) (#689)
Eyal Golan585f34eda7aa
diff --git a/tools/README.md b/tools/README.mdindex 2a98d380f..bababb955 100644--- a/tools/README.md+++ b/tools/README.md@@ -60,6 +60,9 @@ Offline data tools (uv inline-script Python). Run from the repo root.- `art_compare.py` puts screenshots next to the promo image of their pose, writes deuteranopia and protanopia copies andlogs rough colour numbers; `art_checkpoint.sh [--no-build] <name>` builds WebGL, takes the screenshots(`web_check.py --shots`) and runs it (docs/visual-v1.md).+- `coast_measure.py <shots...> --window south|north [--promo] [--check]` measures the beach width, foam band, lace lines+ and sand lightness across the coast of a gameplay03 screenshot (or promo 03's same coast), the median over several+ shots, against bands set from promo 03 (#388).- `machine_lock.py` serialises frame-time measurements against every other Unity job on the machine (all worktrees,batch runs and open editors).- `headed.py` drives a WebGL build in a headed Chrome for Testing over CDP (real GPU, real mouse events, a temporarydiff --git a/tools/coast_measure.py b/tools/coast_measure.pynew file mode 100644index 000000000..8754a801b--- /dev/null+++ b/tools/coast_measure.py@@ -0,0 +1,199 @@+# /// script+# requires-python = ">=3.11"+# dependencies = ["pillow==12.3.0"]+# ///+"""Beach width, sand lightness and foam lace across a west-facing coast, per image row, in 1920-wide frame pixels+(issue #388, sea round 3: the plan's section 1 measured promo 03 and the gameplay03 shot with this).++Usage: uv run tools/coast_measure.py IMAGE [IMAGE ...] (--window south|north | --box Y0 Y1 X0 X1) [--promo] [--step N]+ [--check] [--rows]+Each image is scaled to 1920 wide first (box filter); Y/X are in that 1920 frame. --window takes the gameplay03 pose's+window (WINDOWS), or with --promo the same stretch of coast in promo 03 (PROMO_WINDOWS). With several images (three ?shot+captures: the surf moves and ?shot does not pin the water's clock) each metric is the median over them. --check compares+those medians with BANDS (set from promo 03 before any change) and exits 1 when one is outside.++Classes (sRGB 0-255): water: b > r + 15 (docs/visual-v1.md promo-sea's sea pixel); foam: luma >= 0.72 and max-min <= 30;+sand: luma >= 0.60 and r - b >= 25; crown: any other pixel under luma 0.40 (a tree crown or its shadow). Per row: the+shore is where the sea ends, the first run of 3 sand or crown pixels after >= 6 water/foam pixels: a crown stands on land,+so one over the waterline (main's citrus south of Jaffa) marks the shore as sand would, instead of moving it to the sand+past the crown. The beach is the sand run from the first 3 sand pixels at or after the shore, bridging gaps <= 12 px (a+border band, a pebble); the foam band is the seaward stretch [shore-80, shore) from the farthest foam pixel in it; lace lines are foam runs >= 2 px in that band. Widths are corrected+to across the coast with a least-squares fit of the shore's slope over the rows."""+import argparse+import statistics as st+import sys+from pathlib import Path++from PIL import Image++FRAME_W = 1920+SEA_RUN, SAND_RUN, GAP_PX, FOAM_REACH_PX = 6, 3, 12, 80+# The gameplay03 pose's windows (y0, y1, x0, x1) and the same coast in promo 03 at 1920 wide.+WINDOWS = {"south": (690, 880, 380, 760), "north": (110, 330, 600, 900)}+PROMO_WINDOWS = {"south": (748, 862, 300, 700), "north": (80, 300, 560, 900)}+# The bands, from promo 03 (plan C1 section 1): (low, high), either None for open. Promo 03 itself measures south: beach+# 84, foam 42, 2 lines, share 0.56, sand p50 0.74 and p90 0.86; north: beach 24.3, foam 12.5. The north window is a guard:+# promo 03's northern frontage is narrow.+BANDS = {+ "south": {"beach_px": (40, 95), "foam_px": (25, 55), "lace_lines": (2, None), "foam_share": (0.35, 0.75),+ "sand_luma_p50": (0.72, 0.80), "sand_luma_p90": (0.82, None)},+ "north": {"beach_px": (15, 40), "foam_px": (6, 30)},+}+++def load(path: Path) -> Image.Image:+ with Image.open(path) as im:+ im = im.convert("RGB")+ if im.width != FRAME_W:+ im = im.resize((FRAME_W, max(1, round(im.height * FRAME_W / im.width / 2) * 2)), Image.Resampling.BOX)+ return im+++def luma(r: int, g: int, b: int) -> float:+ return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255+++def classify(r: int, g: int, b: int) -> str:+ lum = luma(r, g, b)+ if lum >= 0.72 and max(r, g, b) - min(r, g, b) <= 30:+ return "F"+ if b > r + 15:+ return "W"+ if lum >= 0.60 and r - b >= 25:+ return "S"+ return "C" if lum < 0.40 else "L"+++def quantile(v: list[float], p: float) -> float:+ s = sorted(v)+ return s[min(len(s) - 1, int(p * len(s)))]+++def measure_row(px: list[tuple[int, int, int]]) -> tuple[int, int, list[float], int, int, float] | None:+ """(shore index, beach px, sand lumas, foam px, lace lines, foam share) of one row, or None with no shore."""+ c = [classify(*p) for p in px]+ seen, shore = 0, None+ for i in range(len(c) - SAND_RUN):+ if c[i] in "WF":+ seen += 1+ if seen >= SEA_RUN and all(v in "SC" for v in c[i:i + SAND_RUN]):+ shore = i+ break+ if shore is None:+ return None+ sand = next((i for i in range(shore, len(c) - SAND_RUN + 1) if c[i:i + SAND_RUN] == ["S"] * SAND_RUN), None)+ if sand is None:+ return None+ j, end, gap = sand, sand, 0+ while j < len(c):+ if c[j] == "S":+ end, gap = j, 0+ else:+ gap += 1+ if gap > GAP_PX:+ break+ j += 1+ lums = [luma(*px[k]) for k in range(sand, end + 1) if c[k] == "S"]+ band = c[max(0, shore - FOAM_REACH_PX):shore]+ far = next((k for k, v in enumerate(band) if v == "F"), None)+ seg = band[far:] if far is not None else []+ lines, run = 0, 0+ for v in seg + ["W"]:+ if v == "F":+ run += 1+ else:+ lines += run >= 2+ run = 0+ return shore, end - sand + 1, lums, len(seg), lines, (seg.count("F") / len(seg)) if seg else 0.0+++def measure(im: Image.Image, y0: int, y1: int, x0: int, x1: int, step: int = 2) -> dict:+ """The window's per-row numbers and their summary (widths across the coast)."""+ data = im.load()+ rows, pool = [], []+ for y in range(y0, y1, step):+ r = measure_row([data[x, y] for x in range(x0, x1)])+ if r is None:+ continue+ shore, beach, lums, foam, lines, share = r+ pool.extend(lums)+ rows.append({"y": y, "shore_x": x0 + shore, "beach_px": beach, "sand_luma": st.median(lums) if lums else 0.0,+ "foam_px": foam, "lace_lines": lines, "foam_share": share})+ if not rows:+ return {"rows": []}+ ys, xs = [r["y"] for r in rows], [r["shore_x"] for r in rows]+ my, mx = st.mean(ys), st.mean(xs)+ k = sum((y - my) * (x - mx) for y, x in zip(ys, xs)) / max(sum((y - my) ** 2 for y in ys), 1e-9)+ f = 1 / (1 + k * k) ** 0.5+ return {"rows": rows, "slope_k": k, "perp_factor": f,+ "beach_px": st.median(r["beach_px"] * f for r in rows),+ "beach_p25": quantile([r["beach_px"] * f for r in rows], 0.25),+ "beach_p75": quantile([r["beach_px"] * f for r in rows], 0.75),+ "foam_px": st.median(r["foam_px"] * f for r in rows),+ "lace_lines": st.median(r["lace_lines"] for r in rows),+ "foam_share": st.median(r["foam_share"] for r in rows),+ "sand_luma_p50": quantile(pool, 0.5) if pool else 0.0,+ "sand_luma_p90": quantile(pool, 0.9) if pool else 0.0}+++METRICS = ("beach_px", "foam_px", "lace_lines", "foam_share", "sand_luma_p50", "sand_luma_p90")+++def combine(summaries: list[dict]) -> dict:+ """Each metric's median over the shots that found a shore."""+ found = [s for s in summaries if s["rows"]]+ return {m: st.median(s[m] for s in found) for m in METRICS} if found else {}+++def check(summary: dict, bands: dict) -> list[str]:+ """The metrics outside their band (all of them when nothing was measured)."""+ out = []+ for m, (lo, hi) in bands.items():+ v = summary.get(m)+ if v is None or (lo is not None and v < lo) or (hi is not None and v > hi):+ out.append(f"{m} {v if v is None else round(v, 3)} outside [{lo}, {hi}]")+ return out+++def main(argv: list[str] | None = None) -> int:+ ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])+ ap.add_argument("images", nargs="+", type=Path)+ g = ap.add_mutually_exclusive_group(required=True)+ g.add_argument("--window", choices=sorted(WINDOWS))+ g.add_argument("--box", nargs=4, type=int, metavar=("Y0", "Y1", "X0", "X1"))+ ap.add_argument("--promo", action="store_true", help="the window's stretch of coast in promo 03, not gameplay03")+ ap.add_argument("--step", type=int, default=2)+ ap.add_argument("--check", action="store_true", help="exit 1 when a median is outside the window's band")+ ap.add_argument("--rows", action="store_true", help="print every row")+ a = ap.parse_args(argv)+ box = tuple(a.box) if a.box else (PROMO_WINDOWS if a.promo else WINDOWS)[a.window]+ sums = []+ for path in a.images:+ s = measure(load(path), *box, step=a.step)+ sums.append(s)+ if not s["rows"]:+ print(f"{path}: no shore in {box}")+ continue+ if a.rows:+ for r in s["rows"]:+ print(" y=%(y)d shore_x=%(shore_x)d beach_px=%(beach_px)d sand_luma=%(sand_luma).2f foam_px=%(foam_px)d "+ "lace_lines=%(lace_lines)d foam_share=%(foam_share).2f" % r)+ print(f"{path}: rows={len(s['rows'])} perp_factor={s['perp_factor']:.2f} beach_px={s['beach_px']:.1f} "+ f"(p25 {s['beach_p25']:.1f}, p75 {s['beach_p75']:.1f}) foam_px={s['foam_px']:.1f} "+ f"lace_lines={s['lace_lines']} foam_share={s['foam_share']:.2f} "+ f"sand_luma p50={s['sand_luma_p50']:.2f} p90={s['sand_luma_p90']:.2f}")+ med = combine(sums)+ print("MEDIAN " + " ".join(f"{m}={v:.2f}" for m, v in med.items()) if med else "MEDIAN no shore")+ if not a.check:+ return 0 if med else 1+ if not a.window:+ ap.error("--check needs --window")+ bad = check(med, BANDS[a.window])+ for b in bad:+ print("OUTSIDE " + b)+ print("BANDS " + ("PASS" if not bad else "FAIL"))+ return 1 if bad else 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tools/tests/test_coast_measure.py b/tools/tests/test_coast_measure.pynew file mode 100644index 000000000..1419cc776--- /dev/null+++ b/tools/tests/test_coast_measure.py@@ -0,0 +1,113 @@+"""tools/coast_measure.py (issue #388, sea round 3): the beach, foam and lace numbers across a west-facing coast."""+from pathlib import Path++import pytest+from PIL import Image++import coast_measure as cm++ROOT = Path(__file__).resolve().parents[2]+SEA, FOAM, SAND, LAND = (60, 120, 150), (235, 238, 236), (225, 200, 150), (120, 130, 70)+CROWN = (46, 34, 16) # a citrus crown in the gameplay03 shot+++def coast(beach: int, foam: list[tuple[int, int]], shore: int = 200, h: int = 40, slope: float = 0.0) -> Image.Image:+ """Sea west of the shore (x = shore + slope * y), foam runs (start, width) counted seaward from it, then beach px of+ sand and land beyond."""+ im = Image.new("RGB", (cm.FRAME_W, h), LAND)+ px = im.load()+ for y in range(h):+ s = shore + round(slope * y)+ for x in range(cm.FRAME_W):+ px[x, y] = SEA if x < s else SAND if x < s + beach else LAND+ for start, width in foam:+ for x in range(s - start - width, s - start):+ px[x, y] = FOAM+ return im+++def test_the_classes_follow_the_promo_sea_rules():+ assert [cm.classify(*c) for c in (SEA, FOAM, SAND, LAND)] == ["W", "F", "S", "L"]+++def test_a_straight_coast_measures_its_beach_foam_and_lace_lines():+ im = coast(beach=60, foam=[(0, 4), (10, 3), (20, 3)]) # swash at the shore and two breaker lines out to 23 px+ s = cm.measure(im, 0, 40, 100, 400)+ assert len(s["rows"]) == 20 # every other row+ assert s["beach_px"] == 60 and s["perp_factor"] == 1+ assert s["foam_px"] == 23 and s["lace_lines"] == 3+ assert s["foam_share"] == pytest.approx(10 / 23)+ assert s["sand_luma_p50"] == pytest.approx(cm.luma(*SAND))+++def test_a_gap_of_twelve_px_is_bridged_and_a_wider_one_ends_the_beach():+ im = coast(beach=80, foam=[])+ px = im.load()+ for y in range(im.height):+ for x in range(230, 242): # 12 px of land 30 px into the beach: bridged+ px[x, y] = LAND+ assert cm.measure(im, 0, 40, 100, 400)["beach_px"] == 80+ for y in range(im.height):+ px[242, y] = LAND # 13 px: the beach ends at 30+ assert cm.measure(im, 0, 40, 100, 400)["beach_px"] == 30+++def test_a_crown_over_the_waterline_does_not_move_the_shore_or_the_foam():+ """Main's citrus crowns stand at the waterline in the gameplay03 south window; the shore must stay where the sea+ ends, not jump past the crown to the next sand."""+ plain = coast(beach=60, foam=[(0, 4), (10, 3), (20, 3)])+ crowned = plain.copy()+ px = crowned.load()+ for y in range(crowned.height):+ for x in range(200, 214): # a 14 px crown on the sand, from the waterline in+ px[x, y] = CROWN+ a, b = cm.measure(plain, 0, 40, 100, 400), cm.measure(crowned, 0, 40, 100, 400)+ assert [r["shore_x"] for r in b["rows"]] == [r["shore_x"] for r in a["rows"]] == [200] * 20+ assert (b["foam_px"], b["lace_lines"], b["foam_share"]) == (a["foam_px"], a["lace_lines"], a["foam_share"]) == (+ 23, 3, pytest.approx(10 / 23))+ assert b["beach_px"] == 46 # the sand still reads from where it shows+++def test_a_slanted_coast_is_measured_across_it():+ im = coast(beach=50, foam=[(0, 10)], h=200, slope=1.0) # 45 degrees: rows cross the strip at sqrt(2) its width+ s = cm.measure(im, 0, 200, 100, 600)+ assert s["slope_k"] == pytest.approx(1.0, abs=0.01)+ assert s["beach_px"] == pytest.approx(50 / 2 ** 0.5, abs=0.5)+ assert s["foam_px"] == pytest.approx(10 / 2 ** 0.5, abs=0.5)+++def test_no_shore_gives_no_rows_and_fails_every_band():+ s = cm.measure(Image.new("RGB", (cm.FRAME_W, 20), LAND), 0, 20, 0, 400)+ assert s["rows"] == [] and cm.combine([s]) == {}+ assert len(cm.check({}, cm.BANDS["north"])) == len(cm.BANDS["north"])+++def test_several_shots_combine_by_median_and_the_check_names_each_miss():+ shots = [cm.measure(coast(b, [(0, 30)]), 0, 40, 100, 400) for b in (30, 50, 70)]+ med = cm.combine(shots)+ assert med["beach_px"] == 50 and med["foam_px"] == 30+ assert cm.check(med, {"beach_px": (40, 95)}) == []+ assert cm.check(med, {"beach_px": (60, None), "foam_px": (None, 20)}) == [+ "beach_px 50.0 outside [60, None]", "foam_px 30.0 outside [None, 20]"]+++def test_a_frame_of_another_width_is_scaled_to_1920(tmp_path):+ p = tmp_path / "wide.png"+ Image.new("RGB", (3840, 2160), SEA).save(p)+ assert cm.load(p).size == (1920, 1080)++[email protected]("window", sorted(cm.WINDOWS))+def test_promo_03_measures_inside_its_own_bands(window):+ """The bands were set from promo 03 (plan C1, section 1); the promo itself must pass them."""+ s = cm.measure(cm.load(ROOT / "promo" / "final" / "03-gameplay-1936.png"), *cm.PROMO_WINDOWS[window])+ assert cm.check(cm.combine([s]), cm.BANDS[window]) == []+++def test_the_cli_checks_the_band_and_exits_1_outside_it(tmp_path, capsys):+ p = tmp_path / "shot.png"+ im = Image.new("RGB", (1920, 1080), LAND)+ im.paste(coast(beach=10, foam=[(0, 3)], shore=700, h=220), (0, 110)) # a 10 px beach in the north window+ im.save(p)+ assert cm.main([str(p), "--window", "north", "--check"]) == 1+ assert "OUTSIDE beach_px" in capsys.readouterr().out