# Shared helpers for the Quake map fixtures (brush.py, map.py, bsp.py, wad2.py): WAD2 / WAD3 writing, running # ericw-tools (qbsp, light, vis from nixpkgs) on a .map, and a reader of the Quake 1 family BSP files they write # (BSP 29, BSP2, 2PSB: layouts from bspfile.h of the Quake source release and ericw-tools; no code taken). # wad(lumps, magic) -> bytes: lumps = [(name, type, data)] # miptex(name, w, h, rng, palette=None) -> bytes (four mip levels of random indices; WAD3 / BSP 30 add the palette) # compile(ctx, text, files, qbsp_args, light_args=None, vis=False) -> { bsp, obj?, log } # read_bsp(data) -> dict of the parsed lumps import math, os, shutil, struct, subprocess, tempfile Q1_LUMPS = ["entities", "planes", "miptex", "vertices", "visibility", "nodes", "texinfo", "faces", "lighting", "clipnodes", "leaves", "marksurfaces", "edges", "surfedges", "models"] def wad(lumps, magic=b"WAD2"): body = b"" dirs = [] for name, typ, data in lumps: dirs.append(struct.pack("> m, h >> m px = pixels[m] if pixels else bytes(rng.randrange(256) for _ in range(lw * lh)) offs.append(o) levels.append(px) o += lw * lh out += struct.pack("<4I", *offs) + b"".join(levels) if palette is not None: out += struct.pack("= 4: n = struct.unpack_from("= 0 else e[1]]) return pts def face_plane(b, f): nx, ny, nz, d, _ = b["planes"][f["plane"]] if f["side"]: return (-nx, -ny, -nz, -d) return (nx, ny, nz, d) def write_bsp(b, lumps): # reassembles a Quake 1 family BSP (b from read_bsp: its variant decides the header) from raw lumps head = {"BSP2": b"BSP2", "2PSB": b"2PSB"}.get(b["variant"]) or struct.pack("