"""What the map of Missouri shows, for any view of it (the 2D window, the 3D war room). Pure: no drawing. The Census outlines are missouri.json; the colours come from the campaign's state.""" from __future__ import annotations import json from pathlib import Path from . import data as D MAP_FILE = Path(__file__).with_name("missouri.json") SEA, LAND, EDGE, RIVER, HOME = "#9cc3d5", "#e8dfc4", "#a59a7c", "#4a86b0", "#556b2f" GROUP_COLOURS = ("#c0392b", "#2c6fbb") # each region's first / second group UNPICKED = "#e8e2cc" # an incursion's flag before you've picked a side _MAP = None def blend(a, b, t): """Colour a..b at t in 0..1.""" ca = [int(a[i:i + 2], 16) for i in (1, 3, 5)] cb = [int(b[i:i + 2], 16) for i in (1, 3, 5)] return "#" + "".join(f"{round(x + (y - x) * t):02x}" for x, y in zip(ca, cb)) def the_map(): global _MAP if _MAP is None: _MAP = json.loads(MAP_FILE.read_text()) return _MAP def by_name(): """County name (no " County") -> its FIPS key in the map.""" return {c["name"].removesuffix(" County"): f for f, c in the_map()["counties"].items()} def region_of(): """County FIPS -> the region it's in (counties outside every region are absent).""" names = by_name() return {names[n]: rid for rid, r in D.REGIONS.items() for n in r["counties"]} def region_centre(rid): """The middle of a region: the average of its counties' outline centres.""" m, names = the_map(), by_name() pts = [] for n in D.REGIONS[rid]["counties"]: ring = max(m["counties"][names[n]]["outline"], key=len) pts.append((sum(x for x, _ in ring) / len(ring), sum(y for _, y in ring) / len(ring))) return (sum(x for x, _ in pts) / len(pts), sum(y for _, y in pts) / len(pts)) def extent(): """(min x, min y, max x, max y) of every county outline.""" m = the_map() xs = [x for c in m["counties"].values() for r in c["outline"] for x, _ in r] ys = [y for c in m["counties"].values() for r in c["outline"] for _, y in r] return min(xs), min(ys), max(xs), max(ys) def county_colour(c, fips, regions=None): """-> (fill colour, hostile?) for a county, from the campaign's state: the fort's own county; a region a group holds (deep if allied, pale and hatched if hostile); a region still in play (tinted toward the group it leans to); or plain land.""" county = the_map()["counties"][fips] rid = (regions if regions is not None else region_of()).get(fips) if county["name"] == f"{D.HOME} County": return HOME, False if not rid: return LAND, False reg = c.regions[rid] if reg.owner is not None: return blend(LAND, GROUP_COLOURS[reg.owner], 0.85 if reg.ally else 0.4), not reg.ally g = 0 if reg.lean >= 0 else 1 return blend("#d7cfb5", GROUP_COLOURS[g], 0.15 + 0.5 * abs(reg.lean) / 100), False