105 lines
3.6 KiB
Python
105 lines
3.6 KiB
Python
"""Orchestrator: maps.json -> tiles + web/data/world.json.
|
|
|
|
python tools/build.py # base + every map
|
|
python tools/build.py customs # just one map
|
|
python tools/build.py --tiles-only # skip the slow PSD render
|
|
python tools/build.py shoreline --tiles-only --trim-only
|
|
python tools/build.py --force-export # re-render even if base.png exists
|
|
|
|
Exporting a 1 GB PSD takes minutes, so base.png is reused unless it is older
|
|
than the PSD or --force-export is given. The base layer (`overall`) is a plain
|
|
image and is always cheap to retile.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
|
|
import export_base
|
|
import tile
|
|
from common import BUILD, DATA, ROOT, WORLD_ID, load_config
|
|
|
|
|
|
def needs_export(m: dict, force: bool) -> bool:
|
|
base = BUILD / m["id"] / "base.png"
|
|
if force or not base.exists():
|
|
return True
|
|
psd = ROOT / m["psd"]
|
|
return psd.exists() and psd.stat().st_mtime > base.stat().st_mtime
|
|
|
|
|
|
def main(argv: list[str]) -> None:
|
|
trim_only = "--trim-only" in argv
|
|
tiles_only = "--tiles-only" in argv or trim_only
|
|
force = "--force-export" in argv
|
|
ids = [a for a in argv if not a.startswith("--")]
|
|
|
|
cfg = load_config()
|
|
world_m = tile.world_entry(cfg)
|
|
|
|
if ids:
|
|
targets = [world_m] if WORLD_ID in ids else []
|
|
targets += [m for m in cfg.maps if m["id"] in ids]
|
|
known = {m["id"] for m in cfg.maps} | {WORLD_ID}
|
|
unknown = set(ids) - known
|
|
if unknown:
|
|
raise SystemExit(f"unknown map id(s): {', '.join(sorted(unknown))}")
|
|
else:
|
|
targets = [world_m] + cfg.maps
|
|
|
|
if not tiles_only:
|
|
failed = []
|
|
for m in targets:
|
|
if m["id"] == WORLD_ID:
|
|
continue
|
|
if not needs_export(m, force):
|
|
print(f"[{m['id']}] base.png up to date, skipping export")
|
|
continue
|
|
try:
|
|
export_base.export(m)
|
|
except Exception as exc: # noqa: BLE001
|
|
failed.append(m["id"])
|
|
print(f"[{m['id']}] EXPORT FAILED -- {type(exc).__name__}: {exc}\n")
|
|
if failed:
|
|
print(f"skipping tiling for failed export(s): {', '.join(failed)}\n")
|
|
targets = [m for m in targets if m["id"] not in failed]
|
|
|
|
entries = []
|
|
for m in targets:
|
|
if m["id"] != WORLD_ID and not (BUILD / m["id"] / "base.png").exists():
|
|
print(f"[{m['id']}] no base.png, skipping tiling")
|
|
continue
|
|
entries.append(tile.tile_map(cfg, m, trim_only=trim_only))
|
|
|
|
# Keep maps that weren't part of this run so partial builds don't drop them.
|
|
by_id = {e["id"]: e for e in entries}
|
|
world_path = DATA / "world.json"
|
|
if world_path.exists():
|
|
for e in json.loads(world_path.read_text(encoding="utf-8")).get("maps", []):
|
|
by_id.setdefault(e["id"], e)
|
|
|
|
known = {m["id"] for m in cfg.maps} | {WORLD_ID}
|
|
maps = [e for e in by_id.values() if e["id"] in known]
|
|
# Base first so it paints underneath everything else.
|
|
maps.sort(key=lambda e: (not e.get("base"), -e["size"][0]))
|
|
|
|
ww, wh = cfg.world_size()
|
|
world = {
|
|
"tileSize": cfg.tile_size,
|
|
"maxZoom": cfg.max_zoom,
|
|
"worldSize": [ww, wh],
|
|
"cellSize": cfg.cell_size,
|
|
"rotate": cfg.rotate,
|
|
"overallScale": cfg.overall_scale,
|
|
"attribution": cfg.attribution,
|
|
"maps": maps,
|
|
}
|
|
DATA.mkdir(parents=True, exist_ok=True)
|
|
world_path.write_text(json.dumps(world, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
print(f"\nwrote {world_path.relative_to(ROOT)}: world {ww}x{wh}, {len(maps)} layer(s)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1:])
|