Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

1.7 KB
Raw Blame
# /// script
# requires-python = ">=3.11"
# dependencies = ["numpy==2.5.3", "pyyaml==6.0.3"]
# ///
"""Settlement -> hex ownership for the simulation.

Usage: uv run tools/settlement_hexes.py   (reads content/map/hexes.json and content/map/settlements_1948.yaml,
writes content/map/settlement_hexes.yaml)
The rule is hexgrid.settlement_hex: the hex containing the settlement if it is land, else the land neighbour with the
nearest centre (Tiberias). The sim reads the result instead of re-implementing the projection, so it needs no
trigonometry (docs/determinism.md: no libm calls in the sim)."""
import json
import sys
from pathlib import Path


sys.path.insert(0, str(Path(__file__).resolve().parent))
import hexgrid as hg  # noqa: E402
import yaml12  # noqa: E402

ROOT = Path(__file__).resolve().parents[1]
MAP = ROOT / "content" / "map"
OUT = MAP / "settlement_hexes.yaml"
HEADER = ("# Generated by tools/settlement_hexes.py from settlements_1948.yaml and hexes.json (hexgrid.settlement_hex).\n"
          "# Do not edit by hand: rerun the tool after changing either input.\n")


def render(hexes: dict, settlements: list[dict]) -> str:
    """The file text: one flow mapping per settlement, in settlements_1948.yaml order."""
    lines = [HEADER, "settlements:\n"]
    for s in settlements:
        q, r = hg.settlement_hex(s["lon"], s["lat"], hexes)
        lines.append(f"  - {{ id: {s['id']}, q: {q}, r: {r} }}\n")
    return "".join(lines)


def main() -> None:
    hexes = json.loads((MAP / "hexes.json").read_text())
    settlements = yaml12.load((MAP / "settlements_1948.yaml").read_text())["settlements"]
    OUT.write_text(render(hexes, settlements))
    print(f"wrote {OUT.relative_to(ROOT)} ({len(settlements)} settlements)")


if __name__ == "__main__":
    main()