Files
4DBug 175ca9e823 archive/wad2, image/lmp, image/spr, data/q3shader, data/q3skin
WAD2 / WAD3 with miptex / qpic / conchars / font / palette images and addImage; Quake .lmp pictures, palette and
colormap (palette never embedded); Quake / Half-Life / SPR32 sprites with frame groups; Quake III shader scripts and
.skin files. Miptex gains qpic decode / encode and palette mapping; Pixels.keyed keeps the key colour (alpha 0).
2026-11-01 19:17:00 -06:00

78 lines
4.2 KiB
Python

# archive/wad2 fixtures (tools/fixtures.py): a Quake gfx.wad-like WAD2 (palette, conchars, a qpic, miptex textures, one
# '{' keyed) and a Half-Life WAD3 (miptex with their palettes, a qpic with its palette, a font), written with
# tools/fixtures/_qbsp.py's WAD writer from wad.h; expected pixels decoded here (index -> palette, 255 or 0 keyed);
# plus the WAD2 compiled into a map by ericw-tools qbsp (it reads the miptex lumps: the BSP's copies must match).
# tests/gen/wad2.luau { wad2 = { data, palette, images = { [name] = { w, h, crc, kind } } }, wad3 = ..., bsp }
import importlib.util, os, struct, zlib
INPUTS = ["tools/fixtures/_qbsp.py"]
def helper(ctx):
spec = importlib.util.spec_from_file_location("_qbsp", os.path.join(ctx.root, "tools", "fixtures", "_qbsp.py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def rgba(pix, pal, key=None):
out = bytearray()
for i in pix:
if key is not None and i == key:
out += pal[i * 3:i * 3 + 3] + b"\0"
else:
out += pal[i * 3:i * 3 + 3] + b"\xff"
return zlib.crc32(bytes(out))
def generate(ctx):
H = helper(ctx)
rng = ctx.rng(22)
pal = bytes(rng.randrange(256) for _ in range(768))
images = {}
lumps = [("palette", 0x40, pal)]
con = bytes(rng.randrange(256) for _ in range(128 * 128))
lumps.append(("conchars", 0x44, con))
images["conchars"] = {"w": 128, "h": 128, "crc": rgba(con, pal, 0), "kind": "conchars"}
qw, qh = 24, 10
qp = bytes(rng.randrange(256) for _ in range(qw * qh))
lumps.append(("disc", 0x42, struct.pack("<II", qw, qh) + qp))
images["disc"] = {"w": qw, "h": qh, "crc": rgba(qp, pal, 255), "kind": "qpic"}
for name, w, h in (("brick", 32, 16), ("{fence", 16, 16)):
mips = [bytes(rng.randrange(256) for _ in range((w >> m) * (h >> m))) for m in range(4)]
lumps.append((name, 0x44, H.miptex(name, w, h, rng, pixels=mips)))
images[name] = {"w": w, "h": h, "crc": rgba(mips[0], pal, 255 if name[0] == "{" else None), "kind": "miptex",
"mip2": rgba(mips[2], pal, 255 if name[0] == "{" else None)}
wad2 = H.wad(lumps)
out = {"wad2": {"data": wad2, "palette": pal, "images": images, "names": [l[0] for l in lumps]}}
# WAD3
images3 = {}
lumps3 = []
for name, w, h in (("CRETE", 32, 32), ("{GRATE", 16, 32)):
p3 = bytes(rng.randrange(256) for _ in range(768))
mips = [bytes(rng.randrange(256) for _ in range((w >> m) * (h >> m))) for m in range(4)]
lumps3.append((name, 0x43, H.miptex(name, w, h, rng, p3, pixels=mips)))
images3[name] = {"w": w, "h": h, "crc": rgba(mips[0], p3, 255 if name[0] == "{" else None), "kind": "miptex"}
p3 = bytes(rng.randrange(256) for _ in range(768))
qp = bytes(rng.randrange(256) for _ in range(20 * 12))
lumps3.append(("LOGO", 0x42, struct.pack("<II", 20, 12) + qp + struct.pack("<H", 256) + p3))
images3["LOGO"] = {"w": 20, "h": 12, "crc": rgba(qp, p3, 255), "kind": "qpic"}
fw, fh = 256, 16
fp = bytes(rng.randrange(256) for _ in range(fw * fh))
chars = b"".join(struct.pack("<HH", (c % 32) * 8, 8) for c in range(256))
lumps3.append(("CONCHARS", 0x46, struct.pack("<IIII", fw, fh, 2, 8) + chars + fp + struct.pack("<H", 256) + p3))
images3["CONCHARS"] = {"w": fw, "h": fh, "crc": rgba(fp, p3, 255), "kind": "font"}
out["wad3"] = {"data": H.wad(lumps3, b"WAD3"), "images": images3, "names": [l[0] for l in lumps3]}
# qbsp copies the miptex it uses into the BSP
text = ('{\n"classname" "worldspawn"\n"wad" "gfx.wad"\n{\n'
"( 0 0 0 ) ( 0 1 0 ) ( 0 0 1 ) brick 0 0 0 1 1\n( 0 0 0 ) ( 0 0 1 ) ( 1 0 0 ) brick 0 0 0 1 1\n"
"( 0 0 0 ) ( 1 0 0 ) ( 0 1 0 ) brick 0 0 0 1 1\n( 64 64 64 ) ( 64 65 64 ) ( 65 64 64 ) brick 0 0 0 1 1\n"
"( 64 64 64 ) ( 65 64 64 ) ( 64 64 65 ) brick 0 0 0 1 1\n( 64 64 64 ) ( 64 64 65 ) ( 64 65 64 ) brick 0 0 0 1 1\n"
'}\n}\n{\n"classname" "info_player_start"\n"origin" "32 32 32"\n}\n')
res = H.compile(ctx, text, {"gfx.wad": wad2}, ["-nofill", "-noclip", "-nopercent"])
b = H.read_bsp(res["bsp"])
L = b["lumps"]["miptex"]
o = struct.unpack_from("<i", L, 4)[0]
out["bsp"] = {"miptex": L[o:o + 40 + 32 * 16 * 85 // 64]}
ctx.emit("wad2", out)