-- model/vmf tests: srctools' Hammer-written test VMFs (solid polygons against srctools.geometry, byte-exact write -- back), a generated room (displacement floor equal to tools/fixtures/vbsp.py's, outputs, visgroups, cameras, hidden -- solids, a prop_static), the document builders, errors and the fuzz pass. Fixtures: tools/fixtures/vmf.py. local T = require("@tests/T") local Vmf = require("@rosetta/model/vmf") local Vbsp = require("@rosetta/model/vbsp") local Brush = require("@rosetta/codecs/model/Brush") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Fx = require("@tests/gen/vmf") local VbspFx = require("@tests/gen/vbsp") local t = T.suite("vmf") t:eq(Vmf.name, "vmf", "name") t:check(Vmf.caps.decode and Vmf.caps.encode, "caps") local function f2(x: number): string local s = string.format("%.2f", x) return if s == "-0.00" then "0.00" else s end local function canonOf(faces: { any }): string local polys = Brush.polygons({ faces }) :: { Brush.Polygon } local out = {} for _, p in polys do local pts = {} for k = 0, #p.points // 3 - 1 do table.insert(pts, f2(p.points[k * 3 + 1]) .. " " .. f2(p.points[k * 3 + 2]) .. " " .. f2(p.points[k * 3 + 3])) end table.sort(pts) table.insert(out, table.concat(pts, ";")) end table.sort(out) return table.concat(out, "|") end local function solidCanons(doc: any): { string } local list = {} for _, s in Vmf.brushes(doc) :: { any } do table.insert(list, canonOf(s.faces)) end table.sort(list) return list end local function dec(data: string, opts: any?): (Types.Scene?, Err.Err?) return Vmf.decode(Bytes.reader(buffer.fromstring(data)), opts) end for _, c in Fx.files do local scene, err = dec(c.data, { tools = true }) t:check(scene ~= nil, c.name .. ": " .. tostring(err)) if scene then local want = table.clone(c.solids) table.sort(want) t:eq(table.concat(solidCanons(scene.meta.vmf), "\n"), table.concat(want, "\n"), c.name .. ": solids as srctools.geometry") t:eq(buffer.tostring(Vmf.encode(scene) :: buffer), c.data, c.name .. ": written back byte for byte") local tris, polys = 0, 0 for _, m in scene.meshes do for _, p in m.primitives do tris += (p.indices :: Types.Accessor).count // 3 end end for _, s in c.solids do for poly in string.gmatch(s, "[^|]+") do local _, corners = string.gsub(poly, ";", "") polys += corners - 1 end end if not string.find(c.data, "dispinfo", 1, true) then t:eq(tris, polys, c.name .. ": triangles = corners - 2 per polygon") else t:eq(tris, 128, c.name .. ": a power-3 displacement replaces its brush") end end end do local R = Fx.room local scene = dec(R.data) :: Types.Scene t:check(scene ~= nil, "room decodes") t:eq(#scene.meta.entities, R.entities, "room: entities as srctools") local outputs = 0 for _, e in scene.meta.entities do if e.connections then for _, k in Types.keys(e.connections) do local v = e.connections[k] outputs += if type(v) == "table" then #v else 1 end end end t:eq(outputs, R.outputs, "room: outputs as srctools") local names = {} local function walk(list: { any }) for _, v in list do table.insert(names, v.name) walk(v.children) end end walk(scene.meta.visgroups) table.sort(names) t:eq(table.concat(names, ","), table.concat(R.visgroups, ","), "room: nested visgroups") t:eq(#scene.cameras, R.cameras, "room: cameras") t:eq(scene.meta.cameras.active, 0, "room: active camera") t:eq(#scene.meta.groups, 1, "room: group") local want = table.clone(R.solids) table.sort(want) t:eq(table.concat(solidCanons(scene.meta.vmf), "\n"), table.concat(want, "\n"), "room: solids (hidden one included) as srctools") t:eq(buffer.tostring(Vmf.encode(scene) :: buffer), R.data, "room: written back byte for byte") -- the displacement floor local disp: Types.Primitive? = nil local nodraw = false for _, m in scene.meshes do for _, p in m.primitives do if p.attributes._BLEND then disp = p end if string.find(scene.materials[p.material :: number].name :: string, "TOOLS", 1, true) then nodraw = true end end end t:check(not nodraw, "room: tool materials left out") t:check(disp ~= nil, "room: displacement") local function positions(p: Types.Primitive): string local P = p.attributes.POSITION local out = {} for i = 0, P.count - 1 do table.insert(out, f2(buffer.readf32(P.data, i * 12)) .. " " .. f2(buffer.readf32(P.data, i * 12 + 4)) .. " " .. f2(buffer.readf32(P.data, i * 12 + 8))) end table.sort(out) return table.concat(out, "\n") end if disp then t:eq(positions(disp), R.disp, "room: displacement grid") local b = Vbsp.decode(Bytes.reader(buffer.fromstring(VbspFx.cases[1].data))) :: Types.Scene for _, m in b.meshes do for _, p in m.primitives do if p.attributes._BLEND then t:eq(positions(p), positions(disp), "room: the same surface as the compiled map's displacement") end end end end local prop, cam for _, n in scene.nodes do if n.extras and (n.extras :: any).model then prop = n end if n.camera == 1 then cam = n end end t:check(prop and prop.translation and prop.translation[1] == 64 and (prop.extras :: any).vmf.classname == "prop_static", "room: prop_static node") t:check(cam ~= nil and cam.rotation ~= nil, "room: camera node") if cam then -- the camera's -z looks at [0 0 64] local q = cam.rotation :: { number } local x, y, z, w = q[1], q[2], q[3], q[4] local fx = -(2 * (x * z + w * y)) local fy = -(2 * (y * z - w * x)) local fz = -(1 - 2 * (x * x + y * y)) local dx, dy, dz = 200, 200, -64 local l = math.sqrt(dx * dx + dy * dy + dz * dz) t:check(math.abs(fx - dx / l) + math.abs(fy - dy / l) + math.abs(fz - dz / l) < 1e-6, "room: camera looks at its target") end t:eq(scene.meta.source.editorBuild, 8864, "versioninfo") end do -- builders local doc = Vmf.document({ skyname = "sky_day01_01" }) table.insert(Vmf.world(doc) :: any, Vmf.box({ -64, -64, 0 }, { 64, 64, 16 }, "DEV/DEV_MEASUREGENERIC01B")) Vmf.entity(doc, { { "classname", "func_detail" } }, { Vmf.box({ 0, 0, 16 }, { 32, 32, 48 }, "BRICK/BRICKWALL001A") }) Vmf.entity(doc, { { "classname", "info_player_start" }, { "origin", "0 0 17" } }) local text = buffer.tostring(Vmf.encode(doc) :: buffer) local scene = dec(text, { textureSizes = { ["DEV/DEV_MEASUREGENERIC01B"] = { 128, 128 } } }) :: Types.Scene t:check(scene ~= nil, "built document decodes") t:eq(#scene.meta.entities, 3, "world + 2 entities") t:eq(string.sub(text, 1, 12), "versioninfo\n", "Hammer layout") local ok = true for _, s in Vmf.brushes(scene.meta.vmf) :: { any } do local polys = Brush.polygons({ s.faces }) :: { Brush.Polygon } ok = ok and #polys == 6 for _, p in polys do -- outward: the normal points away from the brush centre local cx, cy, cz = 0, 0, 0 for _, q in polys do cx += q.points[1] cy += q.points[2] cz += q.points[3] end cx, cy, cz = cx / 6, cy / 6, cz / 6 ok = ok and (p.points[1] - cx) * p.normal[1] + (p.points[2] - cy) * p.normal[2] + (p.points[3] - cz) * p.normal[3] > -1e-9 end end t:check(ok, "boxes: 6 outward faces") local mat = scene.materials[1] t:eq((mat.extras :: any).vmf.width, 128, "textureSizes") local P = scene.meshes[1].primitives[1].attributes.TEXCOORD_0 local maxU = 0 for i = 0, P.count - 1 do maxU = math.max(maxU, math.abs(buffer.readf32(P.data, i * 8))) end t:check(math.abs(maxU - 2) < 1e-6, "Valve 220 UVs: 64 units / 0.25 scale / 128 texels") local _, e1 = dec('world\n{\n\tsolid\n\t{\n\t\tside\n\t\t{\n\t\t\t"plane" "(0 0 0) (1 1)"\n\t\t}\n\t}\n}\n') t:eq(e1 and e1.code, Err.CORRUPT, "bad plane") local _, e2 = dec("world\n{\n") t:eq(e2 and e2.code, Err.TRUNCATED, "truncated") local _, e3 = Vmf.encode({ kind = "scene", meta = {} }) t:eq(e3 and e3.code, Err.ARG, "a Scene without meta.vmf") t:check(Vmf.sniff(Bytes.reader(buffer.fromstring(Fx.room.data))) > 0.5, "sniff") end local samples = { Fx.room.data } for i = 1, 3 do table.insert(samples, Fx.files[i].data) end t:fuzz("decode", function(b: buffer) return Vmf.decode(Bytes.reader(b)) end, samples) t:done()