# model/vmf fixtures: the Hammer-written VMFs of srctools' test suite (MIT; read from the nix srctools source, not # committed) with every solid's polygons as srctools.geometry clips them, and a VMF written here in Hammer's layout # (a room whose floor is the power-3 displacement of tools/fixtures/vbsp.py, a func_detail, a prop_static, outputs, # nested visgroups, a group, cameras, hidden objects) with what srctools.vmf reads from it. # tests/gen/vmf.luau { files = { { name, data, solids = { canon } } }, room = { data, entities, outputs, visgroups, # cameras, solids, disp } } # canon: a solid's polygons, each its corners "%.2f %.2f %.2f" sorted and joined by ";", sorted and joined by "|". import json, math, os, subprocess, tarfile CODE = r''' import json, sys from srctools.vmf import VMF from srctools.keyvalues import Keyvalues from srctools.geometry import Geometry _out, sys.stdout = sys.stdout, sys.stderr inp = json.loads(sys.stdin.read()) def f2(x): s = "%.2f" % x return "0.00" if s == "-0.00" else s def canon(solid): polys = [] for p in Geometry.from_brush(solid).polys: polys.append(";".join(sorted("%s %s %s" % (f2(v.x), f2(v.y), f2(v.z)) for v in p.vertices))) return "|".join(sorted(polys)) res = {"files": [], "room": None} for name, text in inp["files"]: vmf = VMF.parse(Keyvalues.parse(text)) solids = [canon(s) for s in vmf.brushes] + [canon(s) for e in vmf.entities for s in e.solids] res["files"].append({"name": name, "solids": solids}) vmf = VMF.parse(Keyvalues.parse(inp["room"])) ents = [vmf.spawn] + list(vmf.entities) def visnames(tree): out = [] for v in tree: out.append(v.name) out += visnames(v.child_groups) return out res["room"] = { "entities": len(ents), "outputs": sum(len(e.outputs) for e in ents), "visgroups": sorted(visnames(vmf.vis_tree)), "cameras": len(vmf.cameras), "solids": sorted(canon(s) for s in list(vmf.brushes) + [s for e in vmf.entities for s in e.solids]), } _out.write(json.dumps(res)) ''' S, H = 256, 256 def side(i, a, b, c, mat, u, v, extra=""): return ('\t\tside\n\t\t{\n\t\t\t"id" "%d"\n\t\t\t"plane" "(%g %g %g) (%g %g %g) (%g %g %g)"\n\t\t\t"material" "%s"\n' '\t\t\t"uaxis" "[%g %g %g 0] 0.25"\n\t\t\t"vaxis" "[%g %g %g 0] 0.25"\n\t\t\t"rotation" "0"\n' '\t\t\t"lightmapscale" "16"\n\t\t\t"smoothing_groups" "0"\n%s\t\t}\n') % (i, *a, *b, *c, mat, *u, *v, extra) def box(sid, lo, hi, mat, top=None, extra_top=""): (x0, y0, z0), (x1, y1, z1) = lo, hi m = lambda k: top if (k == 0 and top) else mat sides = [ side(sid + 1, (x0, y1, z1), (x1, y1, z1), (x1, y0, z1), m(0), (1, 0, 0), (0, -1, 0), extra_top), side(sid + 2, (x0, y0, z0), (x1, y0, z0), (x1, y1, z0), mat, (1, 0, 0), (0, -1, 0)), side(sid + 3, (x0, y1, z0), (x0, y1, z1), (x0, y0, z1), mat, (0, 1, 0), (0, 0, -1)), side(sid + 4, (x1, y0, z0), (x1, y0, z1), (x1, y1, z1), mat, (0, 1, 0), (0, 0, -1)), side(sid + 5, (x1, y1, z0), (x1, y1, z1), (x0, y1, z1), mat, (1, 0, 0), (0, 0, -1)), side(sid + 6, (x0, y0, z0), (x0, y0, z1), (x1, y0, z1), mat, (1, 0, 0), (0, 0, -1)), ] return '\tsolid\n\t{\n\t\t"id" "%d"\n%s\t\teditor\n\t\t{\n\t\t\t"color" "0 180 0"\n\t\t\t"visgroupid" "2"\n\t\t\t"groupid" "40"\n\t\t\t"visgroupshown" "1"\n\t\t\t"visgroupautoshown" "1"\n\t\t}\n\t}\n' % (sid, "".join(sides)) def dispinfo(): power = 3 n = 2 ** power + 1 rows = lambda f, ind="\t\t\t\t": "".join('%s"row%d" "%s"\n' % (ind, i, " ".join(f(i, j) for j in range(n))) for i in range(n)) dist = lambda i, j: 24 * math.sin(i * 0.7) * math.cos(j * 0.5) + 8 out = ('\t\t\tdispinfo\n\t\t\t{\n\t\t\t\t"power" "3"\n\t\t\t\t"startposition" "[%g %g 0]"\n\t\t\t\t"flags" "0"\n' '\t\t\t\t"elevation" "0"\n\t\t\t\t"subdiv" "0"\n') % (-S, S) out += "\t\t\t\tnormals\n\t\t\t\t{\n" + rows(lambda i, j: "0 0 1" if dist(i, j) >= 0 else "0 0 -1", "\t\t\t\t") + "\t\t\t\t}\n" out += "\t\t\t\tdistances\n\t\t\t\t{\n" + rows(lambda i, j: "%.9g" % abs(dist(i, j)), "\t\t\t\t") + "\t\t\t\t}\n" out += "\t\t\t\toffsets\n\t\t\t\t{\n" + rows(lambda i, j: "0 0 0", "\t\t\t\t") + "\t\t\t\t}\n" out += "\t\t\t\toffset_normals\n\t\t\t\t{\n" + rows(lambda i, j: "0 0 1", "\t\t\t\t") + "\t\t\t\t}\n" out += "\t\t\t\talphas\n\t\t\t\t{\n" + rows(lambda i, j: "%d" % ((i * 31 + j * 7) % 256), "\t\t\t\t") + "\t\t\t\t}\n" out += '\t\t\t\tallowed_verts\n\t\t\t\t{\n\t\t\t\t"10" "-1 -1 -1 -1 -1 -1 -1 -1 -1 -1"\n\t\t\t\t}\n\t\t\t}\n' return out def room(): world = "" world += box(10, (-S, -S, -16), (S, S, 0), "TOOLS/TOOLSNODRAW", top="NATURE/BLENDGRASSDIRT", extra_top=dispinfo()) world += box(20, (-S, -S - 16, 0), (S, -S, H), "BRICK/BRICKWALL001A") world += box(30, (-S, S, 0), (S, S + 16, H), "BRICK/BRICKWALL001A") hidden = box(40, (S, -S, 0), (S + 16, S, H), "BRICK/BRICKWALL001A") hidden = "\thidden\n\t{\n" + "\n".join("\t" + l if l else l for l in hidden.split("\n")) + "\t}\n" detail = box(50, (-16, -16, 0), (16, 16, 32), "BRICK/BRICKWALL001A").replace("\n\t", "\n").replace("\tsolid", "solid", 1) detail = "\n".join("\t" + l if l else l for l in detail.split("\n")) text = ('versioninfo\n{\n\t"editorversion" "400"\n\t"editorbuild" "8864"\n\t"mapversion" "3"\n\t"formatversion" "100"\n\t"prefab" "0"\n}\n' 'visgroups\n{\n\tvisgroup\n\t{\n\t\t"name" "Walls"\n\t\t"visgroupid" "1"\n\t\t"color" "200 100 0"\n\t\tvisgroup\n\t\t{\n' '\t\t\t"name" "North"\n\t\t\t"visgroupid" "2"\n\t\t\t"color" "0 100 200"\n\t\t}\n\t}\n}\n' 'viewsettings\n{\n\t"bSnapToGrid" "1"\n\t"bShowGrid" "1"\n\t"nGridSpacing" "16"\n}\n' 'world\n{\n\t"id" "1"\n\t"mapversion" "3"\n\t"classname" "worldspawn"\n\t"skyname" "sky_day01_01"\n' + world + hidden + '\tgroup\n\t{\n\t\t"id" "40"\n\t\teditor\n\t\t{\n\t\t\t"color" "0 180 0"\n\t\t\t"visgroupshown" "1"\n\t\t\t"visgroupautoshown" "1"\n\t\t}\n\t}\n}\n' 'entity\n{\n\t"id" "60"\n\t"classname" "func_detail"\n' + detail + '}\n' 'entity\n{\n\t"id" "70"\n\t"classname" "prop_static"\n\t"angles" "0 45 0"\n\t"model" "models/props_junk/wood_crate001a.mdl"\n' '\t"skin" "0"\n\t"origin" "64 32 0"\n\teditor\n\t{\n\t\t"color" "255 255 0"\n\t\t"visgroupshown" "1"\n\t\t"visgroupautoshown" "1"\n' '\t\t"logicalpos" "[0 0]"\n\t}\n}\n' 'entity\n{\n\t"id" "80"\n\t"classname" "logic_relay"\n\t"targetname" "relay"\n\tconnections\n\t{\n' '\t\t"OnTrigger" "door,Open,,0,-1"\n\t\t"OnTrigger" "door,Close,,5,-1"\n\t\t"OnSpawn" "relay,Trigger,,1,1"\n\t}\n' '\t"origin" "0 0 128"\n}\n' 'entity\n{\n\t"id" "90"\n\t"classname" "info_player_start"\n\t"angles" "0 90 0"\n\t"origin" "-128 0 8"\n}\n' 'cameras\n{\n\t"activecamera" "0"\n\tcamera\n\t{\n\t\t"position" "[-200 -200 128]"\n\t\t"look" "[0 0 64]"\n\t}\n' '\tcamera\n\t{\n\t\t"position" "[200 0 64]"\n\t\t"look" "[0 0 64]"\n\t}\n}\n' 'cordons\n{\n\t"active" "0"\n}\n') return text def disp_expected(): # tools/fixtures/vbsp.py: the floor corners clockwise from above, corner 0 nearest the start power, n = 3, 9 p = [(-S, S, 0), (S, S, 0), (S, -S, 0), (-S, -S, 0)] out = [] for i in range(n): e0 = [p[0][k] + (p[1][k] - p[0][k]) * i / (n - 1) for k in range(3)] e1 = [p[3][k] + (p[2][k] - p[3][k]) * i / (n - 1) for k in range(3)] for j in range(n): b = [e0[k] + (e1[k] - e0[k]) * j / (n - 1) for k in range(3)] d = float("%.9g" % (24 * math.sin(i * 0.7) * math.cos(j * 0.5) + 8)) out.append("%.2f %.2f %.2f" % (b[0], b[1], b[2] + d)) return "\n".join(sorted(out)) def generate(ctx): src = subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "--impure", "--expr", "with import {}; python3Packages.srctools.src"], capture_output=True, check=True).stdout.decode().strip() files = [] with tarfile.open(src) as tf: for m in tf.getmembers(): base = os.path.basename(m.name) if m.isfile() and base.endswith(".vmf") and ("/test_geometry/" in m.name or "/test_vmf/" in m.name) and base != "test_merge.vmf": files.append((base, tf.extractfile(m).read().decode("utf-8"))) files.sort() text = room() res = json.loads(ctx.python(["srctools"], CODE, input=json.dumps({"files": files, "room": text}).encode())) out = [{"name": n, "data": d.encode(), "solids": r["solids"]} for (n, d), r in zip(files, res["files"])] r = res["room"] r.update({"data": text.encode(), "disp": disp_expected()}) ctx.emit("vmf", {"files": out, "room": r})