# model/vbsp fixtures: a small Source map (a room of brush faces, a non-quad ceiling, a nodraw face, a power-3 # displacement floor, a func_brush model, entities with repeated outputs, a pak lump) written here from the SDK's # bspfile.h layouts, then loaded and saved by srctools (BSP.save: static props in game lump versions 4 .. 11, LZMA # compressed lumps, the L4D2 lump header, a v19 header, a srctools-written entity lump). What srctools reads back from # each saved file is the reference: face polygons + texinfo projections, static props, entities, pak file names; the # displacement grid is tessellated here as CCoreDispInfo::GenerateDispSurf does. # tests/gen/vbsp.luau { cases = { { name, data, version, l4d2?, compressed?, props, entities, pak, groups } } } # groups: one per (model, material): { model, material, tris, verts = sorted "x y z u v" lines (%.2f) }. import json CODE = r''' import io, json, math, os, struct, sys, tempfile, zipfile import srctools.bsp as B from srctools import Vec, Angle _out, sys.stdout = sys.stdout, sys.stderr # BSP.save prints SURF_NODRAW = 0x80 def build(): texdata = [("BRICK/BRICKWALL001A", 512, 512), ("TOOLS/TOOLSNODRAW", 64, 64), ("NATURE/BLENDGRASSDIRT", 256, 256)] texinfo = [ # s xyz shift, t xyz shift, flags, texdata ((0.25, 0, 0, 16), (0, 0, -0.25, 8), 0, 0), ((0, 0.25, 0, 0), (0, 0, -0.25, 0), 0, 0), ((0.25, 0, 0, 0), (0, -0.25, 0, 0), 0, 0), ((0.25, 0, 0, 0), (0, 0.25, 0, 0), SURF_NODRAW, 1), ((0.125, 0, 0, 0), (0, -0.125, 0, 32), 0, 2), ] verts, vindex = [], {} def v(p): p = tuple(float(x) for x in p) if p not in vindex: vindex[p] = len(verts) verts.append(p) return vindex[p] edges, eindex, surfedges = [(0, 0)], {}, [] def poly_edges(poly): first = len(surfedges) ids = [v(p) for p in poly] for k in range(len(ids)): a, b = ids[k], ids[(k + 1) % len(ids)] if (b, a) in eindex: surfedges.append(-eindex[(b, a)]) else: if (a, b) not in eindex: eindex[(a, b)] = len(edges) edges.append((a, b)) surfedges.append(eindex[(a, b)]) return first, len(ids) planes = [] def plane(n, d): planes.append((n, d)) return len(planes) - 1 def newell(poly): nx = ny = nz = 0.0 for k in range(len(poly)): x1, y1, z1 = poly[k]; x2, y2, z2 = poly[(k + 1) % len(poly)] nx += (y1 - y2) * (z1 + z2); ny += (z1 - z2) * (x1 + x2); nz += (x1 - x2) * (y1 + y2) l = math.sqrt(nx * nx + ny * ny + nz * nz) return (nx / l, ny / l, nz / l) faces = [] def face(poly, ti, disp=-1, flip_side=False): # Source winding: clockwise seen from the front, so the front normal is minus Newell's n = tuple(-c for c in newell(poly)) d = sum(a * b for a, b in zip(n, poly[0])) if flip_side: pn, side = plane(tuple(-c for c in n), -d), 1 else: pn, side = plane(n, d), 0 fe, ne = poly_edges(poly) faces.append(dict(plane=pn, side=side, firstedge=fe, numedges=ne, texinfo=ti, disp=disp)) return len(faces) - 1 S, H = 256.0, 256.0 # inward-facing room walls: clockwise seen from inside face([(-S, -S, 0), (-S, -S, H), (S, -S, H), (S, -S, 0)], 0) # south wall, faces +y face([(S, S, 0), (S, S, H), (-S, S, H), (-S, S, 0)], 0, flip_side=True) # north wall, faces -y (plane side 1) face([(-S, S, 0), (-S, S, H), (-S, -S, H), (-S, -S, 0)], 1) # west, faces +x face([(S, -S, 0), (S, -S, H), (S, S, H), (S, S, 0)], 1) # east, faces -x # an octagonal ceiling facing down oc = [] for k in range(8): a = -2 * math.pi * k / 8 oc.append((round(S * math.cos(a), 3), round(S * math.sin(a), 3), H)) face(list(reversed(oc)), 2) face([(-S, -S, H + 8), (S, -S, H + 8), (S, S, H + 8), (-S, S, H + 8)], 3) # nodraw # displacement floor facing up (clockwise seen from above), start position at its second corner floor = [(-S, -S, 0), (-S, S, 0), (S, S, 0), (S, -S, 0)] fdisp = face(floor, 4, disp=0) world_faces = len(faces) # func_brush: a 32-unit cube around its own origin, outward faces c = 16.0 cube = [ [(-c, -c, c), (-c, c, c), (c, c, c), (c, -c, c)], [(-c, -c, -c), (c, -c, -c), (c, c, -c), (-c, c, -c)], [(-c, -c, -c), (-c, -c, c), (c, -c, c), (c, -c, -c)], [(c, c, -c), (c, c, c), (-c, c, c), (-c, c, -c)], [(-c, c, -c), (-c, c, c), (-c, -c, c), (-c, -c, -c)], [(c, -c, -c), (c, -c, c), (c, c, c), (c, c, -c)], ] for p in cube: face(p, 0) models = [((-S, -S, 0), (S, S, H + 8), (0, 0, 0), 0, world_faces), ((-c, -c, -c), (c, c, c), (0, 0, 0), world_faces, len(faces) - world_faces)] power = 3 n = 2 ** power + 1 dispverts = [] for i in range(n): for j in range(n): dist = 24 * math.sin(i * 0.7) * math.cos(j * 0.5) + 8 dispverts.append(((0.0, 0.0, 1.0) if dist >= 0 else (0.0, 0.0, -1.0), abs(dist), (i * 31 + j * 7) % 256)) dispinfo = dict(start=floor[1], power=power, face=fdisp) ents = ( '{\n"world_maxs" "256 256 264"\n"world_mins" "-256 -256 0"\n"skyname" "sky_day01_01"\n"mapversion" "7"\n"classname" "worldspawn"\n}\n' '{\n"origin" "0 0 64"\n"model" "*1"\n"targetname" "door"\n"classname" "func_brush"\n}\n' '{\n"origin" "-128 0 32"\n"angles" "0 90 0"\n"classname" "info_player_start"\n}\n' '{\n"origin" "0 0 200"\n"_light" "255 240 200 300"\n"classname" "light"\n}\n' '{\n"targetname" "relay"\n"OnTrigger" "door,Enable,,0,-1"\n"OnTrigger" "door,Disable,,5,-1"\n"classname" "logic_relay"\n}\n' ) pak = io.BytesIO() with zipfile.ZipFile(pak, "w", zipfile.ZIP_STORED) as z: z.writestr("materials/maps/test/cubemapdefault.vtf", b"VTF\0" + bytes(60)) z.writestr("materials/custom/note.txt", b"hello from the pak lump\n") return dict(texdata=texdata, texinfo=texinfo, verts=verts, edges=edges, surfedges=surfedges, planes=planes, faces=faces, models=models, dispverts=dispverts, dispinfo=dispinfo, ents=ents, pak=pak.getvalue()) def write(m, version=20): lumps = {} lumps[0] = m["ents"].encode() + b"\0" lumps[1] = b"".join(struct.pack("<3ffi", *n, d, 0) for n, d in m["planes"]) names = b"" table = [] for name, w, h in m["texdata"]: table.append(len(names)) names += name.encode() + b"\0" lumps[43] = names lumps[44] = b"".join(struct.pack("= 0 else b]) start = min(range(4), key=lambda k: sum((corners[k][i] - di["start"][i]) ** 2 for i in range(3))) p = corners[start:] + corners[:start] n = 2 ** di["power"] + 1 out = [] lerp = lambda a, b, t: tuple(a[i] + (b[i] - a[i]) * t for i in range(3)) for i in range(n): e0 = lerp(p[0], p[1], i / (n - 1)) e1 = lerp(p[3], p[2], i / (n - 1)) for j in range(n): base = lerp(e0, e1, j / (n - 1)) vec, dist, alpha = m["dispverts"][i * n + j] dist = struct.unpack("= 0: pw = 2 ** m["dispinfo"]["power"] g["tris"] += 2 * pw * pw for base, pos in disp: g["verts"].append("%.2f %.2f %.2f %.2f %.2f" % (*pos, *uv(base))) else: poly = [e.a for e in f.edges] g["tris"] += len(poly) - 2 for q in poly: g["verts"].append("%.2f %.2f %.2f %.2f %.2f" % (q.x, q.y, q.z, *uv((q.x, q.y, q.z)))) for g in groups.values(): g["verts"] = "\n".join(sorted(g["verts"])) props = [{"model": sp.model, "origin": list(sp.origin), "angles": [sp.angles.pitch, sp.angles.yaw, sp.angles.roll], "skin": sp.skin, "scale": sp.scaling if isinstance(sp.scaling, float) else sp.scaling.x} for sp in bsp.props] ents = [bsp.ents.spawn] + list(bsp.ents.entities) outputs = sum(len(e.outputs) for e in ents) pak = sorted(bsp.pakfile.namelist()) return {"groups": sorted(groups.values(), key=lambda g: (g["model"], g["material"])), "props": props, "entities": len(ents), "outputs": outputs, "classnames": sorted(e["classname"] for e in ents), "pak": pak, "spversion": bsp.static_prop_version.version} m = build() base = write(m) cases = [] V = B.StaticPropVersion for name, kw in [ ("v20", dict(sp=V.V6)), ("v19", dict(version=19, sp=V.V4)), ("v20_v5", dict(sp=V.V5)), ("v20_lzma", dict(sp=V.V10, compress=True)), ("tf2_v7", dict(sp=V.V7)), ("l4d2", dict(version=B.VERSIONS.L4D2, sp=V.V9, l4d2=True)), ("csgo", dict(version=21, sp=V.V11)), ("csgo_lzma", dict(version=21, sp=V.V11, compress=True)), ("l4d_v8", dict(version=20, sp=V.V8)), ("srctools_ents", dict(sp=V.V5, rewrite_ents=True)), ]: data = variant(base, name, **kw) ref = reference(data, m) ref.update({"name": name, "data": data.hex(), "version": struct.unpack_from("