125 lines
4.3 KiB
Python
125 lines
4.3 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""engine_assets.py - 1x-Fassung der Assets fuer das Godot-Projekt.
|
||
|
|
|
||
|
|
Das Spiel rendert in einem **640x360-Viewport** (docs/engine.md E1). Darin ist
|
||
|
|
eine Kachel 32 px, eine Kreatur 96 px, die Dialogbox 390 px. `art/final/`
|
||
|
|
liegt dagegen in 2x (Kachel 64, Kreatur 192) — das ist das Bild, das bei 720p
|
||
|
|
auf dem Schirm steht, nicht das, was die Engine rechnet.
|
||
|
|
|
||
|
|
Dieses Werkzeug schreibt deshalb die 1x-Kopie nach `game/assets/`:
|
||
|
|
|
||
|
|
python3 tools/engine_assets.py --write
|
||
|
|
python3 tools/engine_assets.py --check # Exit 1, wenn die Kopie abweicht
|
||
|
|
|
||
|
|
Die Halbierung ist verlustfrei: jede 2x-Datei ist ein exakter
|
||
|
|
nearest-Upscale der 1x-Zeichnung (geprueft mit demselben Rundlauf wie
|
||
|
|
`nachlauf2x.py`). `font/` wird kopiert, nicht halbiert — der 1x-Font ist schon
|
||
|
|
die Engine-Fassung; `_palette_*` und `_versuche` kommen nicht mit.
|
||
|
|
"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import argparse
|
||
|
|
import shutil
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from PIL import Image
|
||
|
|
|
||
|
|
ART = Path(__file__).resolve().parent.parent
|
||
|
|
FINAL = ART / "final"
|
||
|
|
PROJEKT = ART.parent / "game" / "assets"
|
||
|
|
|
||
|
|
# art/final/<Quelle> -> game/assets/<Ziel>
|
||
|
|
ORDNER = {
|
||
|
|
"Terrain": "terrain",
|
||
|
|
"Creatures": "creatures",
|
||
|
|
"Players": "players",
|
||
|
|
"NPCs": "npcs",
|
||
|
|
"Objects": "objects",
|
||
|
|
"UI": "ui",
|
||
|
|
"Pictures": "pictures",
|
||
|
|
}
|
||
|
|
KOPIEREN = {"Font": "font"} # 1:1, keine Halbierung
|
||
|
|
UEBERSPRINGEN = ("_palette_", "_karte_demo", "_vergleich", "_referenz")
|
||
|
|
|
||
|
|
|
||
|
|
def exakt_2x(im: Image.Image) -> bool:
|
||
|
|
"""Ist `im` ein exakter 2x-Nearest-Upscale? (Vergleich ueber Rohbytes --
|
||
|
|
`getbbox()` auf RGBA-Differenzen liefert immer None, siehe nachlauf2x.py.)"""
|
||
|
|
w, h = im.size
|
||
|
|
if w % 2 or h % 2:
|
||
|
|
return False
|
||
|
|
# NEAREST statt BOX: BOX rundet auf halbtransparentem RGBA um 1 ab.
|
||
|
|
klein = im.resize((w // 2, h // 2), Image.NEAREST)
|
||
|
|
return klein.resize(im.size, Image.NEAREST).tobytes() == im.tobytes()
|
||
|
|
|
||
|
|
|
||
|
|
def soll_bild(quelle: Path) -> Image.Image | None:
|
||
|
|
im = Image.open(quelle).convert("RGBA")
|
||
|
|
if not exakt_2x(im):
|
||
|
|
return None
|
||
|
|
return im.resize((im.width // 2, im.height // 2), Image.NEAREST)
|
||
|
|
|
||
|
|
|
||
|
|
def lauf(schreiben: bool) -> list[str]:
|
||
|
|
fehler: list[str] = []
|
||
|
|
anzahl = 0
|
||
|
|
|
||
|
|
for quelle, ziel in ORDNER.items():
|
||
|
|
q = FINAL / quelle
|
||
|
|
if not q.is_dir():
|
||
|
|
continue
|
||
|
|
for p in sorted(q.glob("*.png")):
|
||
|
|
if p.name.startswith(UEBERSPRINGEN):
|
||
|
|
continue
|
||
|
|
bild = soll_bild(p)
|
||
|
|
if bild is None:
|
||
|
|
fehler.append(f"{p}: kein exakter 2x-Upscale")
|
||
|
|
continue
|
||
|
|
ziel_dir = PROJEKT / ziel
|
||
|
|
ziel_pfad = ziel_dir / p.name
|
||
|
|
anzahl += 1
|
||
|
|
if schreiben:
|
||
|
|
ziel_dir.mkdir(parents=True, exist_ok=True)
|
||
|
|
bild.save(ziel_pfad)
|
||
|
|
elif not ziel_pfad.exists():
|
||
|
|
fehler.append(f"{ziel_pfad}: fehlt (--write ausfuehren)")
|
||
|
|
else:
|
||
|
|
ist = Image.open(ziel_pfad).convert("RGBA")
|
||
|
|
if ist.size != bild.size or ist.tobytes() != bild.tobytes():
|
||
|
|
fehler.append(f"{ziel_pfad}: weicht ab")
|
||
|
|
|
||
|
|
for quelle, ziel in KOPIEREN.items():
|
||
|
|
q = FINAL / quelle
|
||
|
|
if not q.is_dir():
|
||
|
|
continue
|
||
|
|
for p in sorted(q.iterdir()):
|
||
|
|
if not p.is_file() or p.suffix not in (".fnt", ".png") or "_2x" in p.name:
|
||
|
|
continue
|
||
|
|
ziel_dir = PROJEKT / ziel
|
||
|
|
anzahl += 1
|
||
|
|
if schreiben:
|
||
|
|
ziel_dir.mkdir(parents=True, exist_ok=True)
|
||
|
|
shutil.copyfile(p, ziel_dir / p.name)
|
||
|
|
elif not (ziel_dir / p.name).exists():
|
||
|
|
fehler.append(f"{ziel_dir / p.name}: fehlt (--write ausfuehren)")
|
||
|
|
|
||
|
|
stand = "geschrieben" if schreiben else "geprueft"
|
||
|
|
print(f"{anzahl} Dateien {stand} -> {PROJEKT.relative_to(ART.parent)}/")
|
||
|
|
for f in fehler:
|
||
|
|
print(" ", f, file=sys.stderr)
|
||
|
|
return fehler
|
||
|
|
|
||
|
|
|
||
|
|
def main(argv: list[str] | None = None) -> int:
|
||
|
|
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||
|
|
g = ap.add_mutually_exclusive_group()
|
||
|
|
g.add_argument("--write", action="store_true", help="game/assets/ schreiben")
|
||
|
|
g.add_argument("--check", action="store_true", help="nur pruefen (Default)")
|
||
|
|
a = ap.parse_args(argv)
|
||
|
|
return 1 if lauf(a.write) else 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|