merge ws/source: VBSP v19-21, VMF, KeyValues / data/vdf, codecs/model/Displacement (with ws/quake's Brush / Patch / Miptex)

This commit is contained in:
4DBug committed 2026-11-01 19:46:56 -06:00
commit e100366cb8
44 files changed
+18855 -1508

No files matched your search

+325
View File
@@ -0,0 +1,325 @@
-- model/map tests: Quake / Valve 220 / Quake II / Quake III .map files against ericw-tools qbsp (CSG'd face area per
-- plane and texture coordinates at every compiled corner, per entity), texture sizes from WAD2 / WAD3 / .wal through
-- files, the patchDef2 against a Bezier evaluation, entities as nodes, write round trips, errors, the fuzz pass.
-- Fixtures: tools/fixtures/map.py.
local T = require("@tests/T")
local F = require("@rosetta")
local MapFmt = require("@rosetta/model/map")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/map")
local t = T.suite("map")
t:eq(MapFmt.name, "map", "name")
local function r(x: number, k: number): number
return math.floor(x * k + 0.5)
end
local function key(nx: number, ny: number, nz: number, d: number): string
return string.format("%d %d %d %d", r(nx, 1000), r(ny, 1000), r(nz, 1000), r(d, 100))
end
type Tri = { p: { number }, uv: { number }, n: { number }, tex: string }
local function triangles(s: any, mesh: any): { Tri }
local out: { Tri } = {}
for _, p in mesh.primitives do
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local uv = Types.Scene.reader(p.attributes.TEXCOORD_0) :: any
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
local tex = s.materials[p.material].name
for k = 0, p.indices.count - 1, 3 do
local tri: Tri = { p = {}, uv = {}, n = {}, tex = tex }
for c = 0, 2 do
local v = buffer.readu32(p.indices.data, (k + c) * 4)
for a = 0, 2 do
table.insert(tri.p, pos(v, a))
end
table.insert(tri.uv, uv(v, 0))
table.insert(tri.uv, uv(v, 1))
if c == 0 then
tri.n = { nrm(v, 0), nrm(v, 1), nrm(v, 2) }
end
end
table.insert(out, tri)
end
end
return out
end
local function area(p: { number }): number
local ux, uy, uz = p[4] - p[1], p[5] - p[2], p[6] - p[3]
local vx, vy, vz = p[7] - p[1], p[8] - p[2], p[9] - p[3]
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
return math.sqrt(cx * cx + cy * cy + cz * cz) / 2
end
local function polyArea(p: { number }): number
local a = 0
for k = 1, #p // 3 - 2 do
a += area({ p[1], p[2], p[3], p[k * 3 + 1], p[k * 3 + 2], p[k * 3 + 3], p[k * 3 + 4], p[k * 3 + 5], p[k * 3 + 6] })
end
return a
end
-- barycentric coordinates of x on the triangle's plane (nil when outside)
local function bary(tri: Tri, x: number, y: number, z: number): (number?, number?)
local p = tri.p
local ux, uy, uz = p[4] - p[1], p[5] - p[2], p[6] - p[3]
local vx, vy, vz = p[7] - p[1], p[8] - p[2], p[9] - p[3]
local wx, wy, wz = x - p[1], y - p[2], z - p[3]
local uu, uv, vv = ux * ux + uy * uy + uz * uz, ux * vx + uy * vy + uz * vz, vx * vx + vy * vy + vz * vz
local wu, wv = wx * ux + wy * uy + wz * uz, wx * vx + wy * vy + wz * vz
local den = uu * vv - uv * uv
if den == 0 then
return nil, nil
end
local b = (vv * wu - uv * wv) / den
local c = (uu * wv - uv * wu) / den
local tol = 1e-4
if b < -tol or c < -tol or b + c > 1 + tol then
return nil, nil
end
return b, c
end
local function compare(label: string, s: any, model: string, faces: any)
local node
for _, n in s.nodes do
if n.extras and n.extras.entity == tonumber(model) then
node = n
end
end
t:check(node and node.mesh, label .. ": entity " .. model .. " has a mesh")
local tris = {}
for _, tri in triangles(s, s.meshes[node.mesh]) do
if tri.tex ~= "base/curve" then -- the patch: qbsp has no patches
table.insert(tris, tri)
end
end
local ours, theirs = {}, {}
for _, tri in tris do
local n = tri.n
local k = key(n[1], n[2], n[3], n[1] * tri.p[1] + n[2] * tri.p[2] + n[3] * tri.p[3])
ours[k] = (ours[k] or 0) + area(tri.p)
end
for _, f in faces do
local k = key(f.plane[1], f.plane[2], f.plane[3], f.plane[4])
theirs[k] = (theirs[k] or 0) + polyArea(f.pts)
end
local bad = 0
for k, a in theirs do
if math.abs((ours[k] or 0) - a) > 0.05 then
bad += 1
t:note("%s model %s plane %s: %s vs qbsp %s", label, model, k, tostring(ours[k]), tostring(a))
end
end
for k in ours do
if not theirs[k] then
bad += 1
end
end
t:eq(bad, 0, label .. ": model " .. model .. " face area per plane")
local miss, off = 0, 0
for _, f in faces do
local fk = key(f.plane[1], f.plane[2], f.plane[3], f.plane[4])
for c = 0, #f.pts // 3 - 1 do
local x, y, z = f.pts[c * 3 + 1], f.pts[c * 3 + 2], f.pts[c * 3 + 3]
local s0 = (f.vecs[1] * x + f.vecs[2] * y + f.vecs[3] * z + f.vecs[4]) / f.size[1]
local t0 = (f.vecs[5] * x + f.vecs[6] * y + f.vecs[7] * z + f.vecs[8]) / f.size[2]
local found, best = false, math.huge
for _, tri in tris do
local n = tri.n
if tri.tex == f.tex and key(n[1], n[2], n[3], n[1] * tri.p[1] + n[2] * tri.p[2] + n[3] * tri.p[3]) == fk then
local b, cc = bary(tri, x, y, z)
if b and cc then
found = true
local u = tri.uv[1] + b * (tri.uv[3] - tri.uv[1]) + cc * (tri.uv[5] - tri.uv[1])
local v = tri.uv[2] + b * (tri.uv[4] - tri.uv[2]) + cc * (tri.uv[6] - tri.uv[2])
best = math.min(best, math.max(math.abs(u - s0) * f.size[1], math.abs(v - t0) * f.size[2]))
end
end
end
if not found then
miss += 1
if miss <= 3 then
local on = {}
for _, tri in tris do
local n = tri.n
if key(n[1], n[2], n[3], n[1] * tri.p[1] + n[2] * tri.p[2] + n[3] * tri.p[3]) == fk then
table.insert(on, tri.tex)
end
end
t:note("%s model %s: corner %s %s %s (%s) on %s, ours there: %s", label, model, x, y, z, f.tex, fk, table.concat(on, ","))
end
elseif best > 0.05 then
off += 1
end
end
end
t:eq(miss, 0, label .. ": model " .. model .. " every qbsp corner on one of our triangles")
t:eq(off, 0, label .. ": model " .. model .. " texture coordinates match qbsp")
end
for _, label in { "quake", "valve", "quake2", "quake3" } do
local c = Fx[label]
t:eq(F.detect(c.text), "map", label .. ": F.detect")
local s, err = MapFmt.decode(Bytes.reader(buffer.fromstring(c.text)), { files = c.files, hull = true })
t:check(s, label .. ": " .. tostring(err))
local scene = s :: any
t:eq(scene.meta.source.variant, ({ quake = "quake", valve = "valve220", quake2 = "quake2", quake3 = "quake3" } :: any)[label], label .. ": variant")
t:eq(scene.meta.up, "z", label .. ": z up")
for model, faces in c.models do
compare(label, scene, model, faces)
end
-- texture sizes found in the WAD / .wal files (Quake III needs images: 64 x 64 without decodeImage)
for _, m in scene.materials do
local want = c.sizes[m.name]
if want and label ~= "quake3" then
t:check(m.extras.map.width == want[1] and m.extras.map.height == want[2] and m.extras.map.sizeKnown, label .. ": size of " .. m.name)
end
end
-- entities: nodes with their keys in order
t:eq(#scene.meta.entities, 4, label .. ": entities")
local door = scene.nodes[3]
t:eq(door.name, "door1", label .. ": brush entity named by targetname")
t:eq(door.extras.classname, "func_door", label .. ": classname")
t:eq(table.concat(Types.keys(door.extras.keys), ","), "classname,targetname,angle", label .. ": key order")
local start = scene.nodes[4]
t:eq(start.translation and table.concat(start.translation, " "), "-128 -128 24", label .. ": point entity at its origin")
t:check(start.mesh == nil, label .. ": point entity without a mesh")
-- round trip: write, read again, same entities and geometry
local out, eerr = MapFmt.encode(scene)
t:check(out, label .. ": encode " .. tostring(eerr))
local again = MapFmt.decode(Bytes.reader(out :: buffer), { files = c.files, hull = true }) :: any
t:check(again ~= nil, label .. ": reload")
local a1, a2 = MapFmt.parse(c.text) :: any, MapFmt.parse(out :: buffer) :: any
local same = #a1.entities == #a2.entities
for i, e in a1.entities do
local e2 = a2.entities[i]
same = same and e2 ~= nil and #e.brushes == #e2.brushes
for k, v in e.keys do
same = same and e2.keys[k] == v
end
for bi, b in e.brushes do
local b2 = e2.brushes[bi]
same = same and b.type == b2.type and #(b.faces or {}) == #(b2.faces or {})
for fi, f in b.faces or {} do
local f2 = b2.faces[fi]
same = same and f.texture == f2.texture and table.concat(f.points, " ") == table.concat(f2.points, " ")
for _, fld in { "offset", "scale", "u", "v", "flags" } do
same = same and table.concat((f :: any)[fld] or {}, " ") == table.concat((f2 :: any)[fld] or {}, " ")
end
same = same and f.rotation == f2.rotation
if f.matrix then
same = same and table.concat(f.matrix[1], " ") == table.concat(f2.matrix[1], " ") and table.concat(f.matrix[2], " ") == table.concat(f2.matrix[2], " ")
end
end
if b.points then
same = same and table.concat(b.points, " ") == table.concat(b2.points, " ")
end
end
end
t:check(same, label .. ": write -> parse gives the same entities, brushes and faces")
t:eq(again.meta.source.variant, scene.meta.source.variant, label .. ": variant survives the round trip")
t:eq(#again.meshes, #scene.meshes, label .. ": meshes survive the round trip")
end
-- the Quake III patch: our grid = the Bezier evaluation
do
local c = Fx.quake3
local s = MapFmt.decode(Bytes.reader(buffer.fromstring(c.text)), { files = c.files }) :: any
t:eq(s.meta.source.patches, 1, "quake3: one patch")
local prim
for _, p in s.meshes[1].primitives do
if s.materials[p.material].name == "base/curve" then
prim = p
end
end
t:check(prim, "quake3: patch primitive")
local pos = Types.Scene.reader(prim.attributes.POSITION) :: any
local uv = Types.Scene.reader(prim.attributes.TEXCOORD_0) :: any
t:eq(prim.attributes.POSITION.count, c.patch.w * c.patch.h, "quake3: grid size")
local worst = 0
for k = 0, c.patch.w * c.patch.h - 1 do
for a = 0, 2 do
worst = math.max(worst, math.abs(pos(k, a) - c.patch.grid[k * 5 + a + 1]))
end
worst = math.max(worst, math.abs(uv(k, 0) - c.patch.grid[k * 5 + 4]) * 64, math.abs(uv(k, 1) - c.patch.grid[k * 5 + 5]) * 64)
end
t:check(worst < 1e-4, "quake3: patch points match the Bezier evaluation (" .. worst .. ")")
t:eq(s.textures[s.materials[prim.material].baseColorTexture.texture].uri, "textures/base/curve", "quake3: texture URI")
local lean = MapFmt.decode(Bytes.reader(buffer.fromstring(c.text)), { patchLevel = 1, skip = { "brick" } }) :: any
for _, p in lean.meshes[1].primitives do
t:check(lean.materials[p.material].name ~= "brick", "skip: brick left out")
end
end
-- options and conversions
do
local c = Fx.quake
local merged = MapFmt.decode(Bytes.reader(buffer.fromstring(c.text)), { files = c.files, hull = true, merge = true }) :: any
local plain = MapFmt.decode(Bytes.reader(buffer.fromstring(c.text)), { files = c.files }) :: any
local function count(sc: any): number
local n = 0
for _, p in sc.meshes[1].primitives do
n += p.indices.count
end
return n
end
t:check(count(merged) < count(plain), "hull + merge: fewer triangles than every face")
t:eq(plain.materials[1].extras.map.sizeKnown, true, "sizes from the worldspawn WAD")
local nowad = MapFmt.decode(Bytes.reader(buffer.fromstring(c.text))) :: any
t:eq(nowad.materials[1].extras.map.width, 64, "no WAD: 64 x 64")
local sized = MapFmt.decode(Bytes.reader(buffer.fromstring(c.text)), { textureSizes = { brick = { 8, 8 } } }) :: any
for _, m in sized.materials do
if m.name == "brick" then
t:eq(m.extras.map.width, 8, "textureSizes")
end
end
-- Quake standard -> Valve 220: same texture coordinates
local valve = assert(MapFmt.encode(plain, { variant = "valve220" }))
local v = MapFmt.decode(Bytes.reader(valve), { files = c.files }) :: any
t:eq(v.meta.source.variant, "valve220", "converted to Valve 220")
local u1 = Types.Scene.reader(plain.meshes[1].primitives[1].attributes.TEXCOORD_0) :: any
local u2 = Types.Scene.reader(v.meshes[1].primitives[1].attributes.TEXCOORD_0) :: any
local worst = 0
for k = 0, plain.meshes[1].primitives[1].attributes.TEXCOORD_0.count - 1 do
worst = math.max(worst, math.abs(u1(k, 0) - u2(k, 0)), math.abs(u1(k, 1) - u2(k, 1)))
end
t:check(worst < 1e-5, "Valve 220 conversion keeps the texture coordinates (" .. worst .. ")")
-- edits: a moved point entity writes its new origin
plain.nodes[4].translation = { 1, 2, 3 }
local moved = MapFmt.parse(assert(MapFmt.encode(plain))) :: any
t:eq(moved.entities[3].keys.origin, "1 2 3", "moved entity origin")
-- through the api
local viaApi = assert(F.model.load(c.text, { files = c.files }))
t:eq(viaApi.meta.source.format, "map", "F.model.load")
local saved = assert(F.save(viaApi, nil, { format = "map" }))
t:check(buffer.len(saved) > 100, "F.save as map")
end
-- sniffing and errors
do
local build = buffer.create(64)
buffer.writeu32(build, 0, 7)
t:eq(MapFmt.sniff(Bytes.reader(build)), 0, "Build engine (binary) .map: not ours")
t:eq(MapFmt.sniff(Bytes.reader(buffer.fromstring('{"classname": "x"}'))), 0, "JSON: not a map")
t:check(MapFmt.sniff(Bytes.reader(buffer.fromstring('{\n"classname" "worldspawn"\n}'))) > 0.5, "minimal map")
local _, e1 = MapFmt.decode(Bytes.reader(buffer.fromstring('{ "classname" "worldspawn" { ( 0 0 0 ) ( 1 0 0 ) brick')))
t:check(e1 and e1.code == Err.CORRUPT, "truncated face: " .. tostring(e1))
local _, e2 = MapFmt.decode(Bytes.reader(buffer.fromstring('{ "classname" "worldspawn" { brushDef3 { } } }')))
t:check(e2 and e2.code == Err.UNSUPPORTED, "Doom 3 brushDef3: unsupported")
local _, e3 = MapFmt.encode(Types.Scene.new())
t:check(e3 and e3.code == Err.ARG, "encode without entities")
local bad = assert(MapFmt.decode(Bytes.reader(buffer.fromstring(Fx.quake.text)))) :: any
bad.nodes[2].extras.keys.message = 'a "quoted" value'
local _, e4 = MapFmt.encode(bad)
t:check(e4 and e4.code == Err.ARG, "quotes in a value")
end
t:fuzz("decode", function(b: buffer)
return MapFmt.decode(Bytes.reader(b), { hull = true, merge = true })
end, { buffer.fromstring(Fx.quake.text), buffer.fromstring(Fx.quake3.text), buffer.fromstring(Fx.valve.text) }, { mutations = 60, garbage = 20 })
t:done()
+221
View File
@@ -0,0 +1,221 @@
-- model/vbsp tests: maps written by tools/fixtures/vbsp.py and saved by srctools (v19 / v20 / v21, L4D2 header, LZMA
-- lumps, static props v4 .. v11) against what srctools reads back: face polygons and texture projections per
-- (model, material), the displacement grid, static props, entities, the pak lump; errors and the fuzz pass.
local T = require("@tests/T")
local Vbsp = require("@rosetta/model/vbsp")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Mat4 = require("@rosetta/codecs/model/Mat4")
local Fx = require("@tests/gen/vbsp")
local t = T.suite("vbsp")
t:eq(Vbsp.name, "vbsp", "name")
t:check(Vbsp.caps.decode and not Vbsp.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 lines(prim: Types.Primitive): (string, number)
local P, U = prim.attributes.POSITION, prim.attributes.TEXCOORD_0
local out = table.create(P.count)
for i = 0, P.count - 1 do
out[i + 1] = table.concat({
f2(buffer.readf32(P.data, i * 12)), f2(buffer.readf32(P.data, i * 12 + 4)), f2(buffer.readf32(P.data, i * 12 + 8)),
f2(buffer.readf32(U.data, i * 8)), f2(buffer.readf32(U.data, i * 8 + 4)),
}, " ")
end
table.sort(out)
return table.concat(out, "\n"), (prim.indices :: Types.Accessor).count // 3
end
local function fixLines(s: string): string
local out = {}
for l in string.gmatch(s, "[^\n]+") do
table.insert(out, (string.gsub(l, "%-0%.00", "0.00")))
end
table.sort(out)
return table.concat(out, "\n")
end
-- every triangle faces its vertices' normals (front faces are counter-clockwise)
local function facing(prim: Types.Primitive): boolean
local P, N, I = prim.attributes.POSITION, prim.attributes.NORMAL, prim.indices :: Types.Accessor
for k = 0, I.count // 3 - 1 do
local a, b, c = buffer.readu32(I.data, k * 12), buffer.readu32(I.data, k * 12 + 4), buffer.readu32(I.data, k * 12 + 8)
local function p(i: number, j: number): number
return buffer.readf32(P.data, i * 12 + j * 4)
end
local ux, uy, uz = p(b, 0) - p(a, 0), p(b, 1) - p(a, 1), p(b, 2) - p(a, 2)
local vx, vy, vz = p(c, 0) - p(a, 0), p(c, 1) - p(a, 1), p(c, 2) - p(a, 2)
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
local d = nx * buffer.readf32(N.data, a * 12) + ny * buffer.readf32(N.data, a * 12 + 4) + nz * buffer.readf32(N.data, a * 12 + 8)
if d <= 0 then
return false
end
end
return true
end
-- Source AngleVectors(pitch, yaw, roll): forward, left (= -right), up
local function basis(p: number, y: number, r: number): { number }
local sp, cp = math.sin(math.rad(p)), math.cos(math.rad(p))
local sy, cy = math.sin(math.rad(y)), math.cos(math.rad(y))
local sr, cr = math.sin(math.rad(r)), math.cos(math.rad(r))
return {
cp * cy, cp * sy, -sp,
sr * sp * cy - cr * sy, sr * sp * sy + cr * cy, sr * cp,
cr * sp * cy + sr * sy, cr * sp * sy - sr * cy, cr * cp,
}
end
local first: Types.Scene? = nil
for _, c in Fx.cases do
local scene, err = Vbsp.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(scene ~= nil, c.name .. ": " .. tostring(err))
if not scene then
continue
end
first = first or scene
local src = scene.meta.source :: any
t:eq(src.version, c.version, c.name .. ": version")
t:eq(src.variant == "l4d2", c.l4d2, c.name .. ": L4D2 header")
t:eq(src.compressed, c.compressed, c.name .. ": LZMA lumps")
t:eq(src.staticPropVersion, c.spversion, c.name .. ": static prop version")
-- faces
local byKey: { [string]: Types.Primitive } = {}
for _, n in scene.nodes do
if n.mesh then
local model = (n.extras :: any).vbsp.model
for _, prim in scene.meshes[n.mesh].primitives do
byKey[model .. "|" .. (scene.materials[prim.material :: number].name :: string)] = prim
end
end
end
local ngroups = 0
for _ in byKey do
ngroups += 1
end
t:eq(ngroups, #c.groups, c.name .. ": one primitive per (model, material), nodraw skipped")
for _, g in c.groups do
local prim = byKey[g.model .. "|" .. g.material]
t:check(prim ~= nil, c.name .. ": group " .. g.model .. " " .. g.material)
if prim then
local got, tris = lines(prim)
t:eq(tris, g.tris, c.name .. ": " .. g.material .. " triangles")
t:eq(got, fixLines(g.verts), c.name .. ": " .. g.material .. " positions and UVs as srctools")
t:check(facing(prim), c.name .. ": " .. g.material .. " counter-clockwise fronts")
if string.find(g.material, "BLEND", 1, true) then
t:check(prim.attributes._BLEND ~= nil, c.name .. ": displacement alpha")
end
end
end
-- static props
local props = {}
for _, n in scene.nodes do
if n.extras and (n.extras :: any).model then
table.insert(props, n)
end
end
t:eq(#props, #c.props, c.name .. ": static props")
for i, want in c.props do
local n = props[i]
if n then
t:eq((n.extras :: any).model, want.model, c.name .. ": prop " .. i .. " model")
t:eq((n.extras :: any).vbsp.skin, want.skin, c.name .. ": prop " .. i .. " skin")
local tr = n.translation :: { number }
t:check(math.abs(tr[1] - want.origin[1]) + math.abs(tr[2] - want.origin[2]) + math.abs(tr[3] - want.origin[3]) < 1e-4, c.name .. ": prop " .. i .. " origin")
local m = Mat4.fromQuat(n.rotation :: { number })
local bv = basis(want.angles[1], want.angles[2], want.angles[3])
local d = 0
for col = 0, 2 do
for row = 0, 2 do
d = math.max(d, math.abs(m[col * 4 + row + 1] - bv[col * 3 + row + 1]))
end
end
t:check(d < 1e-5, c.name .. ": prop " .. i .. " rotation is AngleVectors")
end
end
-- entities
local ents = scene.meta.entities :: { any }
t:eq(#ents, c.entities, c.name .. ": entities")
local outputs, classes = 0, {}
for _, e in ents do
table.insert(classes, e.classname)
if type(e.OnTrigger) == "table" then
outputs += #e.OnTrigger
elseif e.OnTrigger then
outputs += 1
end
end
table.sort(classes)
t:eq(table.concat(classes, ","), table.concat(c.classnames, ","), c.name .. ": classnames")
t:eq(outputs, c.outputs, c.name .. ": repeated outputs")
-- pak
local pak = scene.meta.pak
t:check(pak ~= nil and pak.kind == "archive", c.name .. ": pak archive")
if pak then
local names = {}
for _, e in pak:list() do
table.insert(names, e.name)
end
table.sort(names)
t:eq(table.concat(names, ","), table.concat(c.pak, ","), c.name .. ": pak names")
t:eq(buffer.tostring(pak:read("materials/custom/note.txt") :: buffer), "hello from the pak lump\n", c.name .. ": pak read")
end
end
if first then
local s = first :: Types.Scene
t:eq(s.meta.up, "z", "z up")
local door, light
for _, n in s.nodes do
if n.name == "door" then
door = n
end
if n.light then
light = n
end
end
t:check(door and door.mesh and door.translation and door.translation[3] == 64, "func_brush placed at its origin")
t:check(light and s.lights[light.light].type == "point" and math.abs(s.lights[light.light].intensity - 300) < 1e-9, "light entity")
local disp = nil
for _, m in s.meshes do
for _, p in m.primitives do
if p.attributes._BLEND then
disp = p
end
end
end
t:check(disp ~= nil and disp.attributes.POSITION.count == 81, "power 3: 9 x 9 grid")
local all = Vbsp.decode(Bytes.reader(buffer.fromstring(Fx.cases[1].data)), { tools = true }) :: Types.Scene
t:check(#all.materials == #s.materials + 1, "tools = true keeps the nodraw face")
end
do
local plain = buffer.fromstring(Fx.plain)
t:eq(Vbsp.sniff(Bytes.reader(plain)), 1, "sniff")
t:eq(Vbsp.sniff(Bytes.reader(buffer.fromstring("IBSP" .. string.rep("\0", 2000)))), 0, "not Quake")
local bad = buffer.fromstring(Fx.plain)
buffer.writei32(bad, 4, 42)
local _, e1 = Vbsp.decode(Bytes.reader(bad))
t:eq(e1 and e1.code, Err.UNSUPPORTED, "version 42")
local _, e2 = Vbsp.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.plain, 1, 600))))
t:eq(e2 and e2.code, Err.TRUNCATED, "short header")
local cut = buffer.fromstring(string.sub(Fx.plain, 1, 3000))
local _, e3 = Vbsp.decode(Bytes.reader(cut))
t:eq(e3 and e3.code, Err.TRUNCATED, "lumps past the end")
end
local samples = {}
for i, c in Fx.cases do
if i <= 4 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return Vbsp.decode(Bytes.reader(b))
end, samples, { keepHeader = 8 })
t:done()
+223
View File
@@ -0,0 +1,223 @@
-- 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()