diff options
| author | godosa <godosa@godosa.eu> | 2026-10-06 23:52:03 +0200 |
|---|---|---|
| committer | godosa <godosa@godosa.eu> | 2026-10-06 23:52:03 +0200 |
| commit | 346b1c5195bffc71ceaa9262453e3c189656400b (patch) | |
| tree | 01ac0d31e2724cd6abcc689a5a228e2cbea2f6cf /mapgen/testing.py | |
| download | worldgen-346b1c5195bffc71ceaa9262453e3c189656400b.tar.gz worldgen-346b1c5195bffc71ceaa9262453e3c189656400b.zip | |
worldgen: initial public history
Diffstat (limited to 'mapgen/testing.py')
| -rw-r--r-- | mapgen/testing.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/mapgen/testing.py b/mapgen/testing.py new file mode 100644 index 0000000..b70e5fe --- /dev/null +++ b/mapgen/testing.py @@ -0,0 +1,59 @@ +"""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 |
