"""Test fixtures shared with tools built on mapgen (e.g. a viewer's tests): a tiny two-continent world at H3 res 2."""
from __future__ import annotations
import atexit
import functools
import re
import shutil
import tempfile
from pathlib import Path
import numpy as np
from PIL import Image
FIXTURE_TOML = Path(__file__).resolve().parent / "testdata" / "world.toml"
TECT = """
[[plate]]
id = "a"
seed = [10.0, -30.0]
kind = "continental"
motion = [90.0, 4.0]
[[plate]]
id = "b"
seed = [10.0, 30.0]
kind = "continental"
motion = [270.0, 4.0]
[[plate]]
id = "c"
seed = [0.0, 150.0]
kind = "oceanic"
motion = [0.0, 3.0]
"""
def small_world(tmp: Path) -> None:
(tmp / "config").mkdir()
w = FIXTURE_TOML.read_text()
w = w.replace("raster_width = 8192", "raster_width = 256").replace("preview_width = 2048", "preview_width = 128")
w = w.replace("dev_raster_width = 2048", "dev_raster_width = 256")
w = re.sub(r"moves = \[.*?\n\]", "moves = []", w, flags=re.S)
(tmp / "config" / "world.toml").write_text(w)
(tmp / "config" / "tectonics.toml").write_text(TECT)
(tmp / "sketch").mkdir()
lat = 90 - (np.arange(100) + 0.5) * 1.8
lon = (np.arange(200) + 0.5) * 1.8 - 180
LA, LO = np.meshgrid(lat, lon, indexing="ij")
land = ((np.abs(LA - 10) < 35) & (np.abs(LO) < 70)).astype(np.uint8) * 255
for n in ("land", "mountains", "desert", "rainforest", "trench"):
Image.fromarray(land if n == "land" else np.zeros_like(land), "L").save(tmp / "sketch" / f"{n}.png")
@functools.lru_cache(maxsize=1)
def built_world() -> Path:
from mapgen import pipeline as P
tmp = Path(tempfile.mkdtemp(prefix="worldgen-fixture-"))
atexit.register(shutil.rmtree, tmp, True) # 5 MB per build: never leave them in /tmp
small_world(tmp)
P.build(tmp, 2, log=lambda m: None)
return tmp