118 lines
5.2 KiB
Python
118 lines
5.2 KiB
Python
# Build ART fixtures (tools/fixtures.py). No Duke 3D data (proprietary): a random PALETTE.DAT (256 distinct 6-bit
|
|
# colours, 32 shade tables, a translucency table) and LOOKUP.DAT (palookups 1 and 5, five alternate palettes) are
|
|
# made here. References, from the nix eduke32 source (tools/fixtures/_build_ref.py):
|
|
# - arttool create / addtile builds TILES000.ART (tiles 10..15, two of them empty) from 8-bit PCX files written
|
|
# here, with centre offsets and animation fields; `arttool info` gives the per-tile fields;
|
|
# - arttool exporttile writes each tile back as PCX through palette.dat (<< 2 scaling): its RGB is the reference;
|
|
# - an independent Python reading of the ART bytes (BUILDINF.TXT layout) gives each tile's column-major indices.
|
|
# tests/gen/art.luau { art, palette, lookup, start, last, tiles = { { tile, w, h, idx, rgb, xofs, yofs, animType,
|
|
# animFrames, animSpeed, picanm } }, shade = { n, table }, remap = { pal, table } }
|
|
import os, shutil, struct, subprocess, tempfile, importlib.util
|
|
|
|
_spec = importlib.util.spec_from_file_location("_build_ref", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_build_ref.py"))
|
|
B = importlib.util.module_from_spec(_spec)
|
|
_spec.loader.exec_module(B)
|
|
|
|
INPUTS = ["tools/fixtures/_build_ref.py"]
|
|
|
|
|
|
def palette_dat(rng):
|
|
cols, seen = [], set()
|
|
while len(cols) < 256:
|
|
c = (rng.randrange(64), rng.randrange(64), rng.randrange(64))
|
|
if c not in seen:
|
|
seen.add(c)
|
|
cols.append(c)
|
|
pal = bytes(v for c in cols for v in c)
|
|
shades = rng.randbytes(32 * 256)
|
|
return pal + struct.pack("<H", 32) + shades + rng.randbytes(65536)
|
|
|
|
|
|
def lookup_dat(rng):
|
|
out = bytearray([2])
|
|
for n in (1, 5):
|
|
out.append(n)
|
|
out += rng.randbytes(256)
|
|
for _ in range(5):
|
|
out += bytes(rng.randrange(64) for _ in range(768))
|
|
return bytes(out)
|
|
|
|
|
|
def parse_art(data):
|
|
"""independent ART reading: (first, last, [(w, h, picanm, column-major bytes)])"""
|
|
ver, _, first, last = struct.unpack_from("<4I", data, 0)
|
|
assert ver == 1
|
|
n = last - first + 1
|
|
xs = struct.unpack_from("<%dh" % n, data, 16)
|
|
ys = struct.unpack_from("<%dh" % n, data, 16 + 2 * n)
|
|
anm = struct.unpack_from("<%dI" % n, data, 16 + 4 * n)
|
|
p, tiles = 16 + 8 * n, []
|
|
for i in range(n):
|
|
size = xs[i] * ys[i]
|
|
tiles.append((xs[i], ys[i], anm[i], data[p:p + size]))
|
|
p += size
|
|
return first, last, tiles
|
|
|
|
|
|
SPECS = [ # tile, w, h, addtile options
|
|
(10, 16, 8, ["-x", "3", "-y", "-2"]),
|
|
(11, 1, 5, ["-ann", "3", "-ant", "2", "-ans", "4"]),
|
|
(13, 33, 17, ["-x", "-7", "-ann", "63", "-ant", "1", "-ans", "15"]),
|
|
(15, 64, 64, ["-y", "100", "-ant", "3"]),
|
|
]
|
|
|
|
|
|
def generate(ctx):
|
|
rng = ctx.rng(1997)
|
|
tools = B.tools(ctx)
|
|
pal_dat = palette_dat(rng)
|
|
pal8 = bytes(v << 2 for v in pal_dat[:768])
|
|
lookup = lookup_dat(rng)
|
|
tmp = tempfile.mkdtemp(prefix="rosetta-art-")
|
|
try:
|
|
run = lambda *a: subprocess.run([tools["arttool"]] + list(a), cwd=tmp, capture_output=True, check=True).stdout.decode()
|
|
run("create", "-o", "10", "-n", "6")
|
|
made = {}
|
|
for tile, w, h, extra in SPECS:
|
|
idx = bytes(rng.randrange(256) if rng.random() < 0.85 else 255 for _ in range(w * h))
|
|
made[tile] = idx
|
|
with open(os.path.join(tmp, "t%d.pcx" % tile), "wb") as f:
|
|
f.write(B.pcx_write(w, h, idx, pal8))
|
|
run("addtile", *extra, str(tile), "t%d.pcx" % tile)
|
|
art = open(os.path.join(tmp, "tiles000.art"), "rb").read()
|
|
with open(os.path.join(tmp, "palette.dat"), "wb") as f:
|
|
f.write(pal_dat)
|
|
info = {}
|
|
for line in run("info").split("\n"):
|
|
line = line.strip()
|
|
if line.startswith("Tile "):
|
|
head, rest = line.split(": ", 1)
|
|
t = int(head.split()[1])
|
|
size, rest = rest.split(" ", 1)
|
|
fields = dict(kv.split(": ") for kv in rest.split(", "))
|
|
info[t] = (size, {k: int(v) for k, v in fields.items()})
|
|
first, last, parsed = parse_art(art)
|
|
tiles = []
|
|
for i, (w, h, anm, cols) in enumerate(parsed):
|
|
t = first + i
|
|
size, f = info[t]
|
|
assert size == "%dx%d" % (w, h)
|
|
e = {"tile": t, "w": w, "h": h, "picanm": anm, "xofs": f["Xofs"], "yofs": f["Yofs"], "animType": f["AnimType"],
|
|
"animFrames": f["AnimFrames"], "animSpeed": f["AnimSpeed"]}
|
|
if w * h > 0:
|
|
idx = bytes(cols[x * h + y] for y in range(h) for x in range(w))
|
|
assert idx == made[t]
|
|
run("exporttile", str(t))
|
|
pw, ph, pidx, ppal = B.pcx_read(open(os.path.join(tmp, "tile%04d.pcx" % t), "rb").read())
|
|
assert (pw, ph, pidx) == (w, h, idx)
|
|
e["idx"] = idx
|
|
e["rgb"] = bytes(ppal[v * 3 + k] for v in pidx for k in range(3))
|
|
tiles.append(e)
|
|
finally:
|
|
shutil.rmtree(tmp)
|
|
ctx.emit("art", {
|
|
"art": art, "palette": pal_dat, "lookup": lookup, "start": first, "last": last, "tiles": tiles,
|
|
"shade": {"n": 7, "table": pal_dat[770 + 7 * 256:770 + 8 * 256]},
|
|
"remap": {"pal": 5, "table": lookup[1 + 257 + 1:1 + 257 + 257]},
|
|
})
|