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).
This commit is contained in:
4DBug committed 2026-11-01 19:17:00 -06:00
1 parent 67e116a6c6
commit 175ca9e823
23 files changed
+5380 -1143

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
# image/lmp fixtures (tools/fixtures.py): qpic .lmp pictures, a palette.lmp and a colormap.lmp written from wad.h /
# draw.c; reference rgba from Pillow (a "P" image with the palette, index 255 transparent for qpics). The palette is
# random: Quake's own is GPL and never committed.
# tests/gen/lmp.luau { palette, pics = { { name, data, w, h, crc, crcOpaque } }, colormap = { data, crc }, palcrc }
import json, struct, zlib
CODE = r"""
import json, sys, zlib, io
from PIL import Image
job = json.loads(sys.stdin.read())
out = {}
pal = bytes.fromhex(job["pal"])
for k, (w, h, hexpix, key) in job["imgs"].items():
im = Image.frombytes("P", (w, h), bytes.fromhex(hexpix))
im.putpalette(pal)
if key is not None:
im.info["transparency"] = key
out[k] = zlib.crc32(im.convert("RGBA").tobytes())
print(json.dumps(out))
"""
def generate(ctx):
rng = ctx.rng(31)
pal = bytes(rng.randrange(256) for _ in range(768))
imgs, pics = {}, []
for name, w, h in (("disc", 24, 24), ("conback", 64, 40), ("num_0", 24, 24), ("wide", 3, 1)):
pix = bytes(rng.randrange(256) for _ in range(w * h))
imgs[name] = [w, h, pix.hex(), 255]
imgs[name + "_o"] = [w, h, pix.hex(), None]
pics.append({"name": name, "data": struct.pack("<II", w, h) + pix, "w": w, "h": h})
cmap = bytes(rng.randrange(256) for _ in range(64 * 256)) + b"\0"
imgs["colormap"] = [256, 64, cmap[:16384].hex(), None]
imgs["palette"] = [16, 16, bytes(range(256)).hex(), None]
ref = json.loads(ctx.python(["pillow"], CODE, input=json.dumps({"pal": pal.hex(), "imgs": imgs}).encode()))
for p in pics:
p["crc"] = ref[p["name"]]
p["crcOpaque"] = ref[p["name"] + "_o"]
ctx.emit("lmp", {"palette": pal, "pics": pics, "colormap": {"data": cmap, "crc": ref["colormap"]}, "palcrc": ref["palette"]})
+117
View File
@@ -0,0 +1,117 @@
# data/q3shader and data/q3skin fixtures (tools/fixtures.py): shader scripts in the style of the Quake III Arena
# Shader Manual (comments, quoted tokens, braces on their own lines or not, several stages) and .skin files; reference
# structure from an independent tokenizer here (COM_ParseExt rules: tokens per line, // and /* */ comments).
# tests/gen/q3shader.luau { scripts = { { text, shaders = { { name, directives, stages } } } }, skins = { { text, entries } } }
import re
SCRIPTS = [
"""// base walls
textures/base_wall/metalfloor_wall_10
{
qer_editorimage textures/base_wall/metalfloor_wall_10.tga
surfaceparm metalsteps
{
map $lightmap
rgbGen identity
}
{
map textures/base_wall/metalfloor_wall_10.tga
blendFunc GL_DST_COLOR GL_ZERO
rgbGen identity
}
}
textures/sfx/flame1 {
surfaceparm nolightmap
cull none /* both sides */
q3map_surfacelight 2500
{
animMap 10 textures/sfx/flame1.tga textures/sfx/flame2.tga textures/sfx/flame3.tga
blendFunc GL_ONE GL_ONE
tcMod scale 1 1 // per frame
}
}
""",
"""/*
* a sky and a quoted path
*/
textures/skies/hellsky
{
skyparms "env/hell sky" 512 -
surfaceparm noimpact
surfaceparm sky
{ map textures/skies/clouds.tga
tcMod scroll 0.05 0.06 }
}
models/players/sarge/band
{
deformVertexes wave 100 sin 0 2 0 1
{
map models/players/sarge/band.tga
alphaFunc GE128
depthWrite
}
}
""",
]
SKINS = [
"tag_head,\ntag_torso,\nh_head,models/players/sarge/band.tga\nh_cap,models/players/sarge/cigar.tga\n",
"u_torso,models/players/sarge/default.tga\r\nl_legs,models/players/sarge/default.tga\r\n",
]
def tokenize(text):
text = re.sub(r"/\*.*?\*/", lambda m: "\n" * m.group(0).count("\n"), text, flags=re.S)
lines = []
for raw in text.split("\n"):
raw = raw.split("//")[0] if '"' not in raw else raw
cur = []
for m in re.finditer(r'"([^"]*)"|([{}])|([^\s{}"]+)', raw):
if m.group(2):
if cur:
lines.append(cur)
cur = []
lines.append([m.group(2)])
else:
cur.append(m.group(1) if m.group(1) is not None else m.group(3))
if cur:
lines.append(cur)
return lines
def parse(text):
lines = tokenize(text)
out, k = [], 0
while k < len(lines):
sh = {"name": lines[k][0], "directives": [], "stages": []}
k += 2
stage = None
while True:
d = lines[k]
k += 1
if d == ["{"]:
stage = []
elif d == ["}"]:
if stage is None:
break
sh["stages"].append(stage)
stage = None
elif stage is not None:
stage.append(d)
else:
sh["directives"].append(d)
out.append(sh)
return out
def generate(ctx):
skins = []
for s in SKINS:
entries = []
for line in s.replace("\r", "").split("\n"):
if line.strip():
a, b = line.split(",", 1)
entries.append({"surface": a.strip(), "shader": b.strip()})
skins.append({"text": s, "entries": entries})
ctx.emit("q3shader", {"scripts": [{"text": s, "shaders": parse(s)} for s in SCRIPTS], "skins": skins})
+82
View File
@@ -0,0 +1,82 @@
# image/spr fixtures (tools/fixtures.py): sprites written from spritegn.h (Quake IDSP 1: two single frames and a
# group of three; Half-Life IDSP 2 with its palette, alpha test and index alpha; DarkPlaces SPR32); expected frame
# pixels (rgba CRCs), canvas positions from the frame origins, and group delays from the cumulative intervals.
# tests/gen/spr.luau { palette, sprites = { { name, data, version, w, h, origin, frames = { { x, y, w, h, crc, delay } } } } }
import struct, zlib
def frame_bytes(rng, w, h, bpp):
return bytes(rng.randrange(256) for _ in range(w * h * bpp))
def rgba(pix, pal, key=None, mode=None):
out = bytearray()
for i in pix:
if mode == "indexalpha":
out += pal[255 * 3:255 * 3 + 3] + bytes([i])
elif 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 sprite(rng, version, layout, pal, tex=3):
# layout: list of ("single", (ox, oy, w, h)) or ("group", [intervals], [(ox, oy, w, h)])
singles = []
body = b""
frames = []
for item in layout:
if item[0] == "single":
body += struct.pack("<i", 0)
entries = [(item[1], 0.1)]
else:
ivs = item[1]
body += struct.pack("<ii", 1, len(ivs)) + b"".join(struct.pack("<f", v) for v in ivs)
entries = [(f, ivs[k] - (ivs[k - 1] if k else 0)) for k, f in enumerate(item[2])]
for (ox, oy, w, h), delay in entries:
pix = frame_bytes(rng, w, h, 4 if version == 32 else 1)
body += struct.pack("<iiii", ox, oy, w, h) + pix
if version == 32:
crc = zlib.crc32(pix)
elif version == 2 and tex == 2:
crc = rgba(pix, pal, mode="indexalpha")
elif version == 2 and tex != 3:
crc = rgba(pix, pal)
else:
crc = rgba(pix, pal, 255)
frames.append({"ox": ox, "oy": oy, "w": w, "h": h, "crc": crc, "delay": struct.unpack("<f", struct.pack("<f", delay))[0]})
n = len(layout)
mw = max(f["w"] for f in frames)
mh = max(f["h"] for f in frames)
if version == 2:
head = b"IDSP" + struct.pack("<iiifiiifi", 2, 2, tex, 16.0, mw, mh, n, 0.0, 0) + struct.pack("<H", 256) + pal
else:
head = b"IDSP" + struct.pack("<iifiiifi", version, 2, 16.0, mw, mh, n, 0.0, 0)
minx = min(f["ox"] for f in frames)
maxy = max(f["oy"] for f in frames)
for f in frames:
f["x"], f["y"] = f["ox"] - minx, maxy - f["oy"]
w = max(f["ox"] + f["w"] for f in frames) - minx
h = maxy - min(f["oy"] - f["h"] for f in frames)
return {"data": head + body, "version": version, "w": w, "h": h, "origin": [-minx, maxy], "frames": frames}
def generate(ctx):
rng = ctx.rng(64)
pal = bytes(rng.randrange(256) for _ in range(768))
hlpal = bytes(rng.randrange(256) for _ in range(768))
sprites = []
q = sprite(rng, 1, [("single", (-8, 8, 16, 16)), ("single", (-12, 20, 24, 28)),
("group", [0.1, 0.25, 0.5], [(-4, 4, 8, 8), (-6, 6, 12, 12), (-8, 10, 16, 20)])], pal)
q["name"] = "quake"
sprites.append(q)
for tex, name in ((3, "hl_alphatest"), (2, "hl_indexalpha"), (0, "hl_normal")):
s = sprite(rng, 2, [("single", (-16, 16, 32, 32)), ("single", (-10, 12, 20, 24))], hlpal, tex)
s["name"] = name
s["tex"] = tex
sprites.append(s)
s32 = sprite(rng, 32, [("single", (-5, 5, 10, 10)), ("group", [0.2, 0.4], [(-5, 5, 10, 10), (-3, 9, 6, 12)])], pal)
s32["name"] = "spr32"
sprites.append(s32)
ctx.emit("spr", {"palette": pal, "hlpalette": hlpal, "sprites": sprites})
+77
View File
@@ -0,0 +1,77 @@
# 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)