# BSP fixtures (tools/fixtures.py): maps written here from the bspfile.h layouts of the Quake / Quake II source # releases and the Unofficial Quake 3 Map Specs: two boxes (the world and a brush entity "*1"), faces stored # clockwise as the games do (some with side = 1 on a flipped plane), texture vectors, entities. # q1 / hl: miptex lump with one embedded texture (Half-Life: with its palette) and one kept in a WAD (offsets 0); # q2: texture names (.wal sizes through opts.files); # q3: polygon, mesh and patch faces, two lightmaps, vertex colours; plus a .pk3 (zip) of it for assimp. # References: the expected triangles per model and texture (positions, normals, texture coordinates, computed here # with the documented rules), texture CRCs, entities; assimp's soup of the Quake III map. # tests/gen/bsp.luau { q1 = { data, palette, models = { [model] = { [texture] = { pos, uv, nrm } } }, textures, # entities }, hl = ..., q2 = { ..., wal }, q3 = { ..., lightmaps, patchArea, assimp } } import importlib.util, io, math, os, struct, zipfile, zlib INPUTS = ["tools/fixtures/_quake_ref.py"] def ref(ctx): spec = importlib.util.spec_from_file_location("_quake_ref", os.path.join(ctx.root, "tools", "fixtures", "_quake_ref.py")) m = importlib.util.module_from_spec(spec) spec.loader.exec_module(m) return m def f32(x): return struct.unpack("= 0 else [0] + [k - i for i in range(1, k)] out = [] for i in range(1, k - 1): out += [order[0], order[i], order[i + 1]] return out def brush_map(rng, variant, pal): q2 = variant == "q2" faces = [(f, 0) for f in box((-64, -64, 0), (64, 64, 128))] + [(f, 1) for f in box((-8, -8, 8), (8, 8, 40))] verts, edges, surfedges, planes, texinfo, flist = [], [(0, 0)], [], [], [], [] vindex = {} def vid(p): p = tuple(f32(c) for c in p) if p not in vindex: vindex[p] = len(verts) verts.append(p) return vindex[p] texnames = ["brick", "door"] if q2 else ["brick", "+0door"] sizes = [(16, 8), (8, 8)] tis = [] for t in range(2): s = [f32(rng.uniform(-1, 1)) for _ in range(3)] + [f32(rng.uniform(-8, 8))] tt = [f32(rng.uniform(-1, 1)) for _ in range(3)] + [f32(rng.uniform(-8, 8))] tis.append((s, tt)) texinfo.append((s, tt, t)) models = {} for i, ((poly, n), model) in enumerate(faces): tex = model side = i % 3 == 2 pn = tuple(-c for c in n) if side else n planes.append((pn, 0.0)) first = len(surfedges) ids = [vid(p) for p in poly] for k in range(len(ids)): a, b = ids[k], ids[(k + 1) % len(ids)] if k % 2 == 0: edges.append((a, b)) surfedges.append(len(edges) - 1) else: edges.append((b, a)) surfedges.append(-(len(edges) - 1)) flist.append((len(planes) - 1, 1 if side else 0, first, len(ids), tex)) # expected triangles fp = [tuple(f32(c) for c in p) for p in poly] s, tt = tis[tex] w, h = (sizes[tex] if variant != "q2" else (sizes[tex] if tex == 0 else (1, 1))) e = models.setdefault(model, {}).setdefault(texnames[tex], {"pos": [], "uv": [], "nrm": []}) for c in ccw_fan(fp, n): x, y, z = fp[c] e["pos"] += [x, y, z] e["nrm"] += list(n) e["uv"] += [(x * s[0] + y * s[1] + z * s[2] + s[3]) / w, (x * tt[0] + y * tt[1] + z * tt[2] + tt[3]) / h] # lumps L = {} L["entities"] = ENTS L["planes"] = b"".join(struct.pack("<4fi", *n, d, 0) for n, d in planes) L["vertices"] = b"".join(struct.pack("<3f", *v) for v in verts) L["edges"] = b"".join(struct.pack("<2H", *e) for e in edges) L["surfedges"] = b"".join(struct.pack(" 7 else (0, 0) fb += struct.pack("<8i2i2i3f6f3f2i", sh, -1, kind, first, n, mv, nmv, lm, 0, 0, 0, 0, *([0.0] * 12), w, h) L["faces"] = fb lms = [bytes(rng.randrange(256) for _ in range(128 * 128 * 3)) for _ in range(2)] L["lightmaps"] = b"".join(lms) names = ["entities", "textures", "planes", "nodes", "leafs", "leaffaces", "leafbrushes", "models", "brushes", "brushsides", "vertexes", "meshverts", "effects", "faces", "lightmaps", "lightvols", "visdata"] off = 8 + 8 * len(names) body, dirs = b"", [] for n in names: d = L.get(n, b"") dirs.append((off + len(body), len(d))) body += d + b"\0" * ((4 - len(d) % 4) % 4) data = b"IBSP" + struct.pack("