merge ws/quake (codecs/model/Brush for model/t3d)

This commit is contained in:
4DBug committed 2026-11-01 19:26:29 -06:00
commit 37aa29d03e
49 files changed
+15426 -1411

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
-- archive/wad2 tests: WAD2 / WAD3 lumps, miptex / qpic / conchars / font / palette images against pixels decoded in
-- the fixture, ericw-tools qbsp's copy of a miptex, write round trips, adding images, errors, the fuzz pass.
-- Fixtures: tools/fixtures/wad2.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Wad2 = require("@rosetta/archive/wad2")
local Miptex = require("@rosetta/codecs/image/Miptex")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/wad2")
local t = T.suite("wad2")
t:eq(Wad2.name, "wad2", "name")
for _, v in { "wad2", "wad3" } do
local c = Fx[v]
t:eq(F.detect(c.data), "wad2", v .. ": F.detect")
local a, err = Wad2.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(a, v .. ": " .. tostring(err))
t:eq(a.meta.source.variant, v, v .. ": variant")
local names = {}
for _, e in a:list() do
table.insert(names, e.name)
end
t:eq(table.concat(names, ","), table.concat(c.names, ","), v .. ": lump names in order")
for name, want in c.images do
local img, ierr = Wad2.image(a, name)
t:check(img, v .. ": image " .. name .. ": " .. tostring(ierr))
t:eq(img.width .. "x" .. img.height, want.w .. "x" .. want.h, v .. ": " .. name .. " size")
t:eq(Crc32.of(img.pixels), want.crc, v .. ": " .. name .. " pixels")
t:eq(img.meta.source.kind, want.kind, v .. ": " .. name .. " kind")
if want.mip2 then
local m2 = assert(Wad2.image(a, name, { mip = 2 }))
t:eq(Crc32.of(m2.pixels), want.mip2, v .. ": " .. name .. " mip 2")
end
end
-- write: the same lumps back
local out = assert(F.save(a))
local b = assert(Wad2.decode(Bytes.reader(out)))
t:eq(b.meta.source.variant, v, v .. ": variant kept")
for _, e in a:list() do
t:check(buffer.tostring(assert(a:read(e.name))) == buffer.tostring(assert(b:read(e.name))), v .. ": round trip " .. e.name)
t:eq((b:list()[1] :: any).type, (a:list()[1] :: any).type, v .. ": lump types kept")
end
end
do
local a = assert(Wad2.decode(Bytes.reader(buffer.fromstring(Fx.wad2.data))))
t:eq(Wad2.image(a, "palette").width, 16, "palette lump as a 16 x 16 image")
t:check(Wad2.palette(a) ~= nil, "the WAD's palette")
-- an explicit palette wins over the WAD's
local grey = string.rep("\0", 768)
local g = assert(Wad2.image(a, "brick", { palette = grey }))
t:eq(buffer.readu32(g.pixels, 0), 0xFF000000, "explicit palette")
-- qbsp copied brick into the BSP: same pixels
local m = assert(Miptex.decode(buffer.fromstring(Fx.bsp.miptex), 0, #Fx.bsp.miptex, { palette = Wad2.palette(a) }))
t:eq(Crc32.of(m.pixels), Fx.wad2.images.brick.crc, "qbsp's copy of the miptex")
-- adding images: palette colours map back exactly
local brick = assert(Wad2.image(a, "brick"))
local n = Wad2.new("wad2")
t:check(Wad2.addImage(n, "copy", brick, { palette = Fx.wad2.palette }), "addImage miptex")
t:check(Wad2.addImage(n, "pic", brick, { palette = Fx.wad2.palette, kind = "qpic" }), "addImage qpic")
local _, e1 = Wad2.addImage(n, "odd", F.ops.image.new(10, 10), { palette = Fx.wad2.palette })
t:check(e1 and e1.code == Err.ARG, "miptex sizes: multiples of 16")
local _, e2 = Wad2.addImage(n, "nopal", brick)
t:check(e2 and e2.code == Err.ARG, "WAD2 needs a palette")
local nb = assert(Wad2.decode(Bytes.reader(assert(F.save(n)))))
t:eq(Crc32.of(assert(Wad2.image(nb, "copy", { palette = Fx.wad2.palette })).pixels), Fx.wad2.images.brick.crc, "added miptex decodes the same")
t:eq(Crc32.of(assert(Wad2.image(nb, "pic", { palette = Fx.wad2.palette, keyed = false })).pixels), Fx.wad2.images.brick.crc, "added qpic decodes the same")
-- WAD3: its own palette from median cut (at most 256 colours: exact)
local w3 = assert(Wad2.decode(Bytes.reader(buffer.fromstring(Fx.wad3.data))))
local crete = assert(Wad2.image(w3, "crete"))
local n3 = Wad2.new("wad3")
t:check(Wad2.addImage(n3, "CRETE2", crete), "addImage into WAD3")
local d3 = assert(Wad2.decode(Bytes.reader(assert(F.save(n3)))))
t:eq(d3.meta.source.variant, "wad3", "WAD3 written")
t:eq(Crc32.of(assert(Wad2.image(d3, "CRETE2")).pixels), Fx.wad3.images.CRETE.crc, "WAD3 miptex with a made palette")
t:check(assert(Wad2.image(w3, "CONCHARS")).meta.font.glyphs[66].width == 8, "font glyph table")
local _, e3 = Wad2.image(w3, "missing")
t:check(e3 and e3.code == Err.ARG, "missing lump")
local _, e4 = Wad2.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.wad2.data, 1, 20))))
t:check(e4 and e4.code == Err.TRUNCATED, "truncated directory: " .. tostring(e4))
end
t:fuzz("decode", function(b: buffer)
local a = Wad2.decode(Bytes.reader(b))
if a then
for _, e in a:list() do
Wad2.image(a, e.name)
end
end
return a
end, { Fx.wad2.data, Fx.wad3.data }, { mutations = 80 })
t:done()
+224
View File
@@ -0,0 +1,224 @@
-- codecs/model/Brush tests: random overlapping brushes in Quake standard / Valve 220 / brush-primitive projections
-- against ericw-tools qbsp -nofill (face area per plane after CSG, texture coordinates at every compiled corner);
-- merging, the primitive builder, the fuzz pass. Fixtures: tools/fixtures/brush.py.
local T = require("@tests/T")
local Brush = require("@rosetta/codecs/model/Brush")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/brush")
local t = T.suite("brush")
local function faceOf(rec: any): Brush.Face?
local p = rec.pts
local plane = Brush.planeFromPoints({ p[1], p[2], p[3] }, { p[4], p[5], p[6] }, { p[7], p[8], p[9] })
if not plane then
return nil
end
local a = rec.args
local f: Brush.Face = { plane = plane, texture = rec.tex }
if rec.kind == "standard" then
f.vecs = Brush.quakeVecs(plane, a[1], a[2], a[3], a[4], a[5])
elseif rec.kind == "valve" then
f.vecs = Brush.valveVecs({ a[1], a[2], a[3], a[4] }, { a[5], a[6], a[7], a[8] }, a[10], a[11])
else
f.bp = { { a[1], a[2], a[3] }, { a[4], a[5], a[6] } }
end
return f
end
local function key(n: { number }): string
local function r(x: number, k: number): number
return math.floor(x * k + 0.5)
end
return string.format("%d %d %d %d", r(n[1], 1000), r(n[2], 1000), r(n[3], 1000), r(n[4], 100))
end
local function inside(pts: { number }, n: { number }, x: number, y: number, z: number, tol: number): boolean
local k = #pts // 3
for i = 0, k - 1 do
local j = (i + 1) % k
local ex, ey, ez = pts[j * 3 + 1] - pts[i * 3 + 1], pts[j * 3 + 2] - pts[i * 3 + 2], pts[j * 3 + 3] - pts[i * 3 + 3]
-- inward edge normal = n x e (counter-clockwise polygons)
local ix, iy, iz = n[2] * ez - n[3] * ey, n[3] * ex - n[1] * ez, n[1] * ey - n[2] * ex
local l = math.sqrt(ix * ix + iy * iy + iz * iz)
if l > 0 and ((x - pts[i * 3 + 1]) * ix + (y - pts[i * 3 + 2]) * iy + (z - pts[i * 3 + 3]) * iz) / l < -tol then
return false
end
end
return true
end
for kind, m in Fx do
local brushes: { { Brush.Face } } = {}
for _, b in m.brushes do
local faces = {}
for _, rec in b do
table.insert(faces, assert(faceOf(rec)))
end
table.insert(brushes, faces)
end
local polys = assert(Brush.polygons(brushes, { hull = true }))
-- area per plane: ours after CSG = qbsp's faces (split by its BSP, merged, subdivided)
local ours, theirs = {}, {}
for _, p in polys do
local k = key(brushes[p.brush][p.face].plane)
ours[k] = (ours[k] or 0) + Brush.area(p.points)
end
for _, f in m.faces do
local k = key(f.plane)
theirs[k] = (theirs[k] or 0) + Brush.area(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: plane %s area %s, qbsp %s", kind, k, tostring(ours[k]), tostring(a))
end
end
for k in ours do
if not theirs[k] then
bad += 1
t:note("%s: plane %s only in ours", kind, k)
end
end
t:eq(bad, 0, kind .. ": face area per plane matches qbsp")
-- every compiled corner lies on one of our polygons with the same texture, and maps to the same texels
local miss, off, worst = 0, 0, 0
local corners = 0
for _, f in m.faces do
local pk = key(f.plane)
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]
corners += 1
local s = f.vecs[1] * x + f.vecs[2] * y + f.vecs[3] * z + f.vecs[4]
local tt = f.vecs[5] * x + f.vecs[6] * y + f.vecs[7] * z + f.vecs[8]
local found = false
local best = math.huge
for _, p in polys do
local face = brushes[p.brush][p.face]
if face.texture == f.tex and key(face.plane) == pk and inside(p.points, p.normal, x, y, z, 0.02) then
found = true
local size = m.sizes[f.tex]
local u, v = Brush.uv(face, x, y, z, size[1], size[2])
best = math.min(best, math.max(math.abs(u * size[1] - s), math.abs(v * size[2] - tt)))
end
end
if not found then
miss += 1
local any = {}
for _, p in polys do
local face = brushes[p.brush][p.face]
if key(face.plane) == pk and inside(p.points, p.normal, x, y, z, 0.02) then
table.insert(any, face.texture .. "#" .. p.brush)
end
end
t:note("%s miss %s %s %s on %s tex %s: ours %s", kind, x, y, z, pk, f.tex, table.concat(any, ","))
elseif best > 0.02 then
off += 1
t:note("%s off %s %s %s on %s tex %s best %s vecs %s", kind, x, y, z, pk, f.tex, best, table.concat(f.vecs, " "))
worst = math.max(worst, best)
end
end
end
t:eq(miss, 0, kind .. ": every qbsp corner is on one of our faces (" .. corners .. " corners)")
t:eq(off, 0, kind .. ": texture coordinates match qbsp's texinfo (worst " .. worst .. " texels)")
-- primitives: counter-clockwise about the face normals, one per texture
local built = Brush.primitives(polys, brushes, function(name: string)
local s = m.sizes[name]
return s[1], s[2]
end)
local seen, wrong = {}, 0
for _, b in built do
t:check(not seen[b.texture], kind .. ": one primitive per texture " .. b.texture)
seen[b.texture] = true
local prim = b.primitive
local pos = Types.Scene.reader(prim.attributes.POSITION) :: any
local nrm = Types.Scene.reader(prim.attributes.NORMAL) :: any
local idx = prim.indices :: Types.Accessor
for k = 0, idx.count - 1, 3 do
local i0, i1, i2 = buffer.readu32(idx.data, k * 4), buffer.readu32(idx.data, k * 4 + 4), buffer.readu32(idx.data, k * 4 + 8)
local ux, uy, uz = pos(i1, 0) - pos(i0, 0), pos(i1, 1) - pos(i0, 1), pos(i1, 2) - pos(i0, 2)
local vx, vy, vz = pos(i2, 0) - pos(i0, 0), pos(i2, 1) - pos(i0, 1), pos(i2, 2) - pos(i0, 2)
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
if cx * nrm(i0, 0) + cy * nrm(i0, 1) + cz * nrm(i0, 2) <= 0 then
wrong += 1
end
end
end
t:eq(wrong, 0, kind .. ": triangles counter-clockwise about their normals")
-- merging keeps the area per plane
local merged = assert(Brush.polygons(brushes, { hull = true, merge = true }))
t:check(#merged <= #polys, kind .. ": merge does not add polygons")
local after = {}
for _, p in merged do
local k = key(brushes[p.brush][p.face].plane)
after[k] = (after[k] or 0) + Brush.area(p.points)
end
local drift = 0
for k, a in ours do
drift = math.max(drift, math.abs((after[k] or 0) - a))
end
t:check(drift < 1e-6, kind .. ": merged area per plane unchanged")
end
-- two touching cubes merge into one box: 6 faces, no inner faces
do
local function cube(x0: number, x1: number): { Brush.Face }
local f = {}
for _, p in { { 1, 0, 0, x1 }, { -1, 0, 0, -x0 }, { 0, 1, 0, 64 }, { 0, -1, 0, 0 }, { 0, 0, 1, 64 }, { 0, 0, -1, 0 } } do
table.insert(f, { plane = p, texture = "t", vecs = Brush.quakeVecs(p, 0, 0, 0, 1, 1) })
end
return f
end
local polys = assert(Brush.polygons({ cube(0, 64), cube(64, 128) }, { hull = true, merge = true }))
t:eq(#polys, 6, "touching cubes: 6 merged faces")
local area = 0
for _, p in polys do
area += Brush.area(p.points)
end
t:eq(area, 2 * 128 * 64 * 2 + 2 * 64 * 64, "touching cubes: surface area")
local plain = assert(Brush.polygons({ cube(0, 64), cube(64, 128) }))
t:eq(#plain, 12, "without hull / merge: every face")
t:check(Brush.planeFromPoints({ 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 }) == nil, "colinear points: no plane")
-- Quake standard with exact rotations, against TextureAxisFromPlane by hand: a floor, rotated 90 degrees
local v = Brush.quakeVecs({ 0, 0, 1, 0 }, 8, 4, 90, 2, 1)
t:eq(v[1][1] .. " " .. v[1][2] .. " " .. v[1][4], "0 0.5 8", "rotated floor s axis")
t:eq(v[2][1] .. " " .. v[2][2] .. " " .. v[2][4], "1 0 4", "rotated floor t axis")
-- brush primitives <-> texel vectors
local n = { 0.6, 0, 0.8, 10 }
local bp = { { 0.01, 0.02, 0.5 }, { -0.03, 0.004, 0.25 } }
local back = Brush.vecsToBp(n, Brush.bpVecs(n, bp, 64, 32), 64, 32)
local err = 0
for i = 1, 2 do
for j = 1, 3 do
err = math.max(err, math.abs(back[i][j] - bp[i][j]))
end
end
t:check(err < 1e-12, "bpVecs / vecsToBp round trip")
end
-- fuzz: random planes never throw
do
local rand = T.rng(5)
local threw = 0
for _ = 1, 200 do
local faces = {}
for _ = 1, 1 + rand(10) do
local p = { rand(2001) / 1000 - 1, rand(2001) / 1000 - 1, rand(2001) / 1000 - 1, rand(200) - 100 }
table.insert(faces, { plane = p, texture = "x", vecs = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 } } })
end
local ok = pcall(function()
local polys = Brush.polygons({ faces, faces }, { hull = true, merge = true })
if polys then
Brush.primitives(polys, { faces, faces }, function()
return 64, 64
end)
end
end)
if not ok then
threw += 1
end
end
t:eq(threw, 0, "random planes never throw")
end
t:done()
+85
View File
@@ -0,0 +1,85 @@
-- codecs/model/Patch tests: biquadratic Bezier evaluation against the closed form, shared sub-patch edges, triangle
-- winding and normals, explicit (patchDef3-style) subdivisions per axis, the fuzz pass.
local T = require("@tests/T")
local Patch = require("@rosetta/codecs/model/Patch")
local t = T.suite("patch")
-- a 5 x 3 grid (two sub-patches along u): x = column * 8, y = row * 8, z = a bump on the middle control row
local ctrl = {}
for j = 0, 2 do
for i = 0, 4 do
table.insert(ctrl, i * 8)
table.insert(ctrl, j * 8)
table.insert(ctrl, if j == 1 then 16 else 0)
end
end
local pts, gw, gh = Patch.grid(ctrl, 5, 3, 3, 4, 2)
t:check(pts ~= nil, "grid")
t:eq(gw, 9, "width: 2 sub-patches x 4 segments + 1")
t:eq(gh, 3, "height: 2 segments + 1")
local p = pts :: { number }
-- z(v) = 2 v (1 - v) * 16 on every column (the bump is constant along u); x is linear in u
local worst = 0
for gy = 0, gh - 1 do
local v = gy / 2
for gx = 0, gw - 1 do
local o = (gy * gw + gx) * 3
worst = math.max(worst, math.abs(p[o + 1] - gx * 4), math.abs(p[o + 2] - v * 16), math.abs(p[o + 3] - 32 * v * (1 - v)))
end
end
t:check(worst < 1e-12, "closed form (worst " .. worst .. ")")
-- corners interpolate the control points
t:eq(p[1] .. " " .. p[2] .. " " .. p[3], "0 0 0", "first corner")
local last = (gw * gh - 1) * 3
t:eq(p[last + 1] .. " " .. p[last + 2] .. " " .. p[last + 3], "32 16 0", "last corner")
local tris = Patch.triangles(gw, gh)
t:eq(#tris, (gw - 1) * (gh - 1) * 6, "two triangles per cell")
local nrm = Patch.normals(p, 3, gw, gh)
local bad = 0
for k = 1, #tris, 3 do
local a, b, c = tris[k] * 3, tris[k + 1] * 3, tris[k + 2] * 3
local ux, uy, uz = p[b + 1] - p[a + 1], p[b + 2] - p[a + 2], p[b + 3] - p[a + 3]
local vx, vy, vz = p[c + 1] - p[a + 1], p[c + 2] - p[a + 2], p[c + 3] - p[a + 3]
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
if cx * nrm[a + 1] + cy * nrm[a + 2] + cz * nrm[a + 3] <= 0 then
bad += 1
end
end
t:eq(bad, 0, "normals face the front of the triangles")
-- extra components (texture coordinates) interpolate too; normalize renormalizes a vec3
local flat = {}
for j = 0, 2 do
for i = 0, 2 do
for _, v in { i, j, 0, i / 2, j / 2, 0, 0, 2 } do
table.insert(flat, v)
end
end
end
local q, qw, qh = Patch.grid(flat, 3, 3, 8, 3, 5)
t:eq(qw * qh, 4 * 6, "explicit subdivisions per axis")
Patch.normalize(q :: { number }, 8, 5)
t:eq((q :: { number })[8], 1, "renormalized normal")
t:eq((q :: { number })[(qw * qh - 1) * 8 + 4], 1, "texture coordinate at the far corner")
t:check(Patch.grid({ 1, 2, 3 }, 2, 2, 3, 4, 4) == nil, "even grid: nil")
t:check(Patch.grid(ctrl, 5, 3, 3, 1000, 1000, 1000) == nil, "point limit: nil")
t:fuzz("grid", function(b: buffer)
local n = buffer.len(b)
local w = 1 + 2 * (if n > 0 then buffer.readu8(b, 0) % 4 else 1)
local h = 1 + 2 * (if n > 1 then buffer.readu8(b, 1) % 4 else 1)
local c = {}
for k = 0, n - 1 do
table.insert(c, buffer.readu8(b, k) - 128)
end
local g, gw2, gh2 = Patch.grid(c, w, h, 3, 1 + n % 5, 1 + n % 3)
if g then
Patch.normals(g, 3, gw2, gh2)
Patch.triangles(gw2, gh2)
end
return g
end, { buffer.create(27), buffer.create(45), buffer.create(75) })
t:done()
+57
View File
@@ -0,0 +1,57 @@
-- data/q3shader tests: shader scripts against an independent tokenizer (tools/fixtures/q3shader.py), write round
-- trips, lookups, errors, the fuzz pass.
local T = require("@tests/T")
local F = require("@rosetta")
local Q3Shader = require("@rosetta/data/q3shader")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/q3shader")
local t = T.suite("q3shader")
t:eq(Q3Shader.name, "q3shader", "name")
local function dump(v: any): string
if type(v) ~= "table" then
return tostring(v)
end
local parts = {}
if v.name then
table.insert(parts, "name=" .. v.name)
table.insert(parts, "d=" .. dump(v.directives))
table.insert(parts, "s=" .. dump(v.stages))
elseif v.surface then
table.insert(parts, v.surface .. "=" .. v.shader)
else
for _, x in v do
table.insert(parts, dump(x))
end
end
return "{" .. table.concat(parts, "|") .. "}"
end
local samples = {}
for i, c in Fx.scripts do
t:eq(F.detect(c.text), "q3shader", "script " .. i .. ": F.detect")
local list, err = Q3Shader.decode(Bytes.reader(buffer.fromstring(c.text)))
t:check(list, "script " .. i .. ": " .. tostring(err))
t:eq(dump(list), dump(c.shaders), "script " .. i .. ": structure")
local out = assert(Q3Shader.encode(list :: any))
t:eq(dump(Q3Shader.decode(Bytes.reader(out))), dump(c.shaders), "script " .. i .. ": round trip")
table.insert(samples, c.text)
end
do
local list = assert(Q3Shader.decode(Bytes.reader(buffer.fromstring(Fx.scripts[2].text))))
local sky = assert(Q3Shader.find(list, "TEXTURES/SKIES/HELLSKY"))
t:eq(assert(Q3Shader.directive(sky.directives, "skyparms"))[2], "env/hell sky", "quoted token")
t:eq(#sky.stages, 1, "stage on brace lines")
local again = assert(F.data.load(Fx.scripts[1].text))
t:eq(#again, 2, "F.data.load")
local _, e1 = Q3Shader.decode(Bytes.reader(buffer.fromstring("textures/x\n{\n{\nmap a\n")))
t:check(e1 and e1.code == Err.TRUNCATED, "unclosed shader")
local _, e2 = Q3Shader.encode({ { name = "has space", directives = {}, stages = {} } } :: any)
t:check(e2 and e2.code == Err.ARG, "bad name")
end
t:fuzz("decode", function(b: buffer)
return Q3Shader.decode(Bytes.reader(b))
end, samples)
t:done()
+36
View File
@@ -0,0 +1,36 @@
-- data/q3skin tests: Quake III .skin files against tools/fixtures/q3shader.py's parse, write round trips, lookup,
-- errors, the fuzz pass.
local T = require("@tests/T")
local F = require("@rosetta")
local Q3Skin = require("@rosetta/data/q3skin")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/q3shader")
local t = T.suite("q3skin")
t:eq(Q3Skin.name, "q3skin", "name")
local function dump(list: any): string
local parts = {}
for _, e in list do
table.insert(parts, e.surface .. "=" .. e.shader)
end
return table.concat(parts, "|")
end
for i, c in Fx.skins do
local list, err = Q3Skin.decode(Bytes.reader(buffer.fromstring(c.text)))
t:check(list, "skin " .. i .. ": " .. tostring(err))
t:eq(dump(list), dump(c.entries), "skin " .. i .. ": entries")
t:eq(dump(Q3Skin.decode(Bytes.reader(assert(Q3Skin.encode(list :: any))))), dump(c.entries), "skin " .. i .. ": round trip")
t:eq(F.detect(c.text, { name = "x.skin" }), "q3skin", "skin " .. i .. ": F.detect")
end
t:eq(Q3Skin.shader(assert(Q3Skin.decode(Bytes.reader(buffer.fromstring(Fx.skins[1].text)))), "H_HEAD"), "models/players/sarge/band.tga", "skin lookup")
local _, e1 = Q3Skin.encode({ { surface = "a,b", shader = "x" } })
t:check(e1 and e1.code == Err.ARG, "comma in a surface name")
local _, e2 = Q3Skin.decode(Bytes.reader(buffer.fromstring("no comma here\n")))
t:check(e2 and e2.code == Err.CORRUPT, "line without a comma")
t:fuzz("decode", function(b: buffer)
return Q3Skin.decode(Bytes.reader(b))
end, { Fx.skins[1].text, Fx.skins[2].text })
t:done()
+48
View File
@@ -0,0 +1,48 @@
-- image/lmp tests: qpic pictures, palette.lmp and colormap.lmp against Pillow (tools/fixtures/lmp.py), the palette
-- through files, the grey fallback, the qpic writer round trip, errors, the fuzz pass.
local T = require("@tests/T")
local F = require("@rosetta")
local Lmp = require("@rosetta/image/lmp")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/lmp")
local t = T.suite("lmp")
t:eq(Lmp.name, "lmp", "name")
local samples = {}
for _, p in Fx.pics do
local img, err = Lmp.decode(Bytes.reader(buffer.fromstring(p.data)), { palette = Fx.palette })
t:check(img, p.name .. ": " .. tostring(err))
t:eq(img.width .. "x" .. img.height, p.w .. "x" .. p.h, p.name .. ": size")
t:eq(Crc32.of(img.pixels), p.crc, p.name .. ": pixels = Pillow (index 255 transparent)")
local opaque = assert(Lmp.decode(Bytes.reader(buffer.fromstring(p.data)), { files = { ["gfx/palette.lmp"] = Fx.palette }, keyed = false }))
t:eq(Crc32.of(opaque.pixels), p.crcOpaque, p.name .. ": opaque, palette through files")
t:eq(opaque.meta.source.palette, true, p.name .. ": palette found")
-- write back: the palette colours map to the same indices
local out = assert(Lmp.encode(img, { palette = Fx.palette }))
t:check(buffer.tostring(out) == p.data, p.name .. ": writer gives the same bytes")
table.insert(samples, p.data)
end
do
local p = Fx.pics[2]
t:eq(F.detect(p.data, { name = "conback.lmp" }), "lmp", "F.detect by size + extension")
local grey = assert(Lmp.decode(Bytes.reader(buffer.fromstring(p.data))))
t:eq(grey.meta.source.palette, false, "no palette: grey")
local cm = assert(Lmp.decode(Bytes.reader(buffer.fromstring(Fx.colormap.data)), { palette = Fx.palette }))
t:eq(cm.width .. "x" .. cm.height, "256x64", "colormap size")
t:eq(Crc32.of(cm.pixels), Fx.colormap.crc, "colormap = Pillow")
local pal = assert(Lmp.decode(Bytes.reader(buffer.fromstring(Fx.palette))))
t:eq(Crc32.of(pal.pixels), Fx.palcrc, "palette.lmp swatch = Pillow")
t:eq(#pal.meta.palette, 256, "palette entries")
t:check(buffer.tostring(assert(Lmp.encode(pal, { kind = "palette" }))) == Fx.palette, "palette round trip")
local _, e1 = Lmp.encode(grey)
t:check(e1 and e1.code == Err.ARG, "qpic writer needs the palette")
local _, e2 = Lmp.decode(Bytes.reader(buffer.fromstring(string.sub(p.data, 1, 30))), { kind = "qpic" })
t:check(e2 and e2.code == Err.CORRUPT or e2 and e2.code == Err.TRUNCATED, "truncated qpic: " .. tostring(e2))
end
t:fuzz("decode", function(b: buffer)
return Lmp.decode(Bytes.reader(b), { palette = Fx.palette, kind = "qpic" })
end, samples)
t:done()
+67
View File
@@ -0,0 +1,67 @@
-- image/spr tests: Quake / Half-Life (alpha test, index alpha, normal) / SPR32 sprites against the frames written by
-- tools/fixtures/spr.py (pixels, canvas positions from the origins, group delays), the writer round trips (versions 1,
-- 2 and 32), errors, the fuzz pass.
local T = require("@tests/T")
local F = require("@rosetta")
local Spr = require("@rosetta/image/spr")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/spr")
local t = T.suite("spr")
t:eq(Spr.name, "spr", "name")
local samples = {}
for _, s in Fx.sprites do
t:eq(F.detect(s.data), "spr", s.name .. ": F.detect")
local img, err = Spr.decode(Bytes.reader(buffer.fromstring(s.data)), { palette = Fx.palette })
t:check(img, s.name .. ": " .. tostring(err))
t:eq(img.width .. "x" .. img.height, s.w .. "x" .. s.h, s.name .. ": canvas")
t:eq(img.meta.origin[1] .. " " .. img.meta.origin[2], s.origin[1] .. " " .. s.origin[2], s.name .. ": origin")
local frames = img.frames or { { image = img, x = 0, y = 0, delay = 0.1 } }
t:eq(#frames, #s.frames, s.name .. ": frames")
for i, want in s.frames do
local f = frames[i]
t:eq(f.x .. "," .. f.y .. " " .. f.image.width .. "x" .. f.image.height, want.x .. "," .. want.y .. " " .. want.w .. "x" .. want.h, s.name .. ": frame " .. i .. " placement")
t:eq(Crc32.of(f.image.pixels), want.crc, s.name .. ": frame " .. i .. " pixels")
t:check(math.abs(f.delay - want.delay) < 1e-6, s.name .. ": frame " .. i .. " delay")
end
t:eq(img.meta.source.version, s.version, s.name .. ": version")
-- write in the same version and read back the same frames
local out, eerr = Spr.encode(img, { version = s.version, palette = if s.version == 2 then Fx.hlpalette else Fx.palette, texFormat = s.tex })
t:check(out, s.name .. ": encode " .. tostring(eerr))
local back = assert(Spr.decode(Bytes.reader(out :: buffer), { palette = Fx.palette }))
local bf = back.frames or { { image = back, x = 0, y = 0 } }
local same = #bf == #frames
for i, f in frames do
local g = bf[i]
same = same and g and g.x == f.x and g.y == f.y
if s.version == 32 or s.version == 1 or s.tex == 0 or s.tex == 3 then
same = same and Crc32.of(g.image.pixels) == Crc32.of(f.image.pixels)
end
end
t:check(same, s.name .. ": round trip keeps frames, placement and pixels")
table.insert(samples, s.data)
end
do
local q = Fx.sprites[1]
local img = assert(Spr.decode(Bytes.reader(buffer.fromstring(q.data)), { palette = Fx.palette }))
-- Half-Life from a Quake sprite: a made palette
local hl = assert(Spr.encode(img))
local back = assert(Spr.decode(Bytes.reader(hl)))
t:eq(back.meta.source.version, 2, "default version 2")
t:eq(back.meta.source.texFormat, "alphatest", "transparent pixels: alpha test")
local grey = assert(Spr.decode(Bytes.reader(buffer.fromstring(q.data))))
t:eq(grey.meta.source.palette, false, "Quake sprite without a palette: grey")
local _, e1 = Spr.encode(img, { version = 1 })
t:check(e1 and e1.code == Err.ARG, "version 1 needs a palette")
local _, e2 = Spr.decode(Bytes.reader(buffer.fromstring("IDS2" .. string.rep("\0", 40))))
t:check(e2 and e2.code == Err.CORRUPT, "Quake II sprite: not IDSP")
local _, e3 = Spr.decode(Bytes.reader(buffer.fromstring(string.sub(q.data, 1, 60))), { palette = Fx.palette })
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
end
t:fuzz("decode", function(b: buffer)
return Spr.decode(Bytes.reader(b), { palette = Fx.palette })
end, samples)
t:done()
+159
View File
@@ -90,6 +90,40 @@ do
t:eq(grey.meta.source.palette, false, "q1 without a palette: grey")
end
-- Half-Life / Quake II RGB lightmaps
for _, name in { "hl", "q2" } do
local c = Fx[name]
local s = assert(Bsp.decode(Bytes.reader(buffer.fromstring(c.data)))) :: any
local data = buffer.fromstring(c.data)
local la = buffer.readi32(data, (if name == "q2" then 8 + 7 * 8 else 4 + 8 * 8))
local atlas
for _, tx in s.textures do
if tx.name == "lightmap" then
atlas = tx.image
end
end
local placed = {}
for _, r in s.meta.lightmaps do
placed[r.face] = r
end
local bad = 0
for _, want in c.lightmaps do
local r = placed[want.face]
if not r or r.w ~= want.w or r.h ~= want.h then
bad += 1
else
for k = 0, want.w * want.h - 1 do
local px = buffer.readu32(atlas.pixels, ((r.y + k // want.w) * atlas.width + r.x + k % want.w) * 4)
local o = la + want.ofs + k * 3
if px ~= 0xFF000000 + buffer.readu8(data, o + 2) * 65536 + buffer.readu8(data, o + 1) * 256 + buffer.readu8(data, o) then
bad += 1
end
end
end
end
t:check(#c.lightmaps == 12 and bad == 0, name .. ": RGB lightmaps (" .. bad .. " wrong)")
end
do
local q2 = brush("q2", Fx.q2, { files = { ["textures/brick.wal"] = Fx.q2.wal } })
t:eq(q2.textures[1].uri, "textures/brick.wal", "q2: .wal URI")
@@ -214,4 +248,129 @@ do
end
t:eq(threw, 0, "q3: mutated lumps never throw")
end
-- ericw-tools maps: BSP 29 / BSP2 / 2PSB, lightmap atlas, .lit, visibility and hulls
do
local base: any = nil
for _, name in { "bsp29", "bsp2", "2psb" } do
local c = Fx.ericw[name]
local s, err = Bsp.decode(Bytes.reader(buffer.fromstring(c.data)), { lit = c.lit })
t:check(s, name .. ": " .. tostring(err))
local sc = s :: any
t:eq(F.detect(c.data), "bsp", name .. ": F.detect")
t:eq(sc.meta.source.variant, "quake", name .. ": variant")
t:eq(sc.meta.source.layout, ({ bsp29 = nil, bsp2 = "BSP2", ["2psb"] = "2PSB" } :: any)[name], name .. ": layout")
t:eq(sc.meta.skippedFaces, 0, name .. ": every face read")
-- the same compile in three layouts: the same triangles
local all = {}
for _, mesh in sc.meshes do
for _, p in mesh.primitives do
for _, x in Soup.list(Soup.positions(p)) do
table.insert(all, x)
end
end
end
if base then
t:check(near(all, base, 0), name .. ": geometry equals BSP 29's")
else
base = all
end
-- lightmaps: sizes as CalcSurfaceExtents, luxels copied from the lump (coloured from the .lit)
local placed = {}
for _, r in sc.meta.lightmaps do
placed[r.face] = r
end
local atlas
for _, tx in sc.textures do
if tx.name == "lightmap" then
atlas = tx.image
end
end
t:check(atlas ~= nil, name .. ": lightmap atlas")
local data = buffer.fromstring(c.data)
local litBuf = buffer.fromstring(c.lit)
local sizeBad, pixBad = 0, 0
for _, want in c.lightmaps do
local r = placed[want.face]
if not r or r.w ~= want.w or r.h ~= want.h then
sizeBad += 1
else
for k = 0, want.w * want.h - 1, 7 do
local x, y = r.x + k % want.w, r.y + k // want.w
local px = buffer.readu32(atlas.pixels, (y * atlas.width + x) * 4)
local o = 8 + (want.ofs + k) * 3
local exp = 0xFF000000 + buffer.readu8(litBuf, o + 2) * 65536 + buffer.readu8(litBuf, o + 1) * 256 + buffer.readu8(litBuf, o)
if px ~= exp then
pixBad += 1
end
end
end
end
t:eq(sizeBad, 0, name .. ": lightmap sizes (" .. #c.lightmaps .. " faces)")
t:eq(pixBad, 0, name .. ": lightmap luxels from the .lit")
local _ = data
local prim = sc.meshes[1].primitives[1]
t:check(prim.attributes.TEXCOORD_1 ~= nil, name .. ": TEXCOORD_1")
local mat = sc.materials[prim.material]
t:eq(sc.textures[mat.extras.bsp.lightmap].name, "lightmap", name .. ": material names the atlas")
local lu = Types.Scene.reader(prim.attributes.TEXCOORD_1) :: any
local inRange = true
for k = 0, prim.attributes.TEXCOORD_1.count - 1 do
if lu(k, 0) < 0 or lu(k, 0) > 1 or lu(k, 1) < 0 or lu(k, 1) > 1 then
inRange = false
end
end
t:check(inRange, name .. ": lightmap coordinates inside the atlas")
local grey = assert(Bsp.decode(Bytes.reader(buffer.fromstring(c.data)))) :: any
local gr
for _, r in grey.meta.lightmaps do
if r.face == c.lightmaps[1].face then
gr = r
end
end
local gimg
for _, tx in grey.textures do
if tx.name == "lightmap" then
gimg = tx.image
end
end
local v = buffer.readu8(data, 0)
local _ = v
local px = buffer.readu32(gimg.pixels, (gr.y * gimg.width + gr.x) * 4)
t:eq(bit32.band(px, 0xFF), bit32.band(bit32.rshift(px, 8), 0xFF), name .. ": without .lit: grey luxels")
-- visibility as vis wrote it
t:eq(#sc.meta.bsp.leaves, c.leaves, name .. ": leaves")
local pvsBad = 0
for leaf, want in c.pvs do
local got = Bsp.pvs(sc, leaf - 1) or {}
if table.concat(got, ",") ~= table.concat(want, ",") then
pvsBad += 1
end
end
t:eq(pvsBad, 0, name .. ": PVS of every leaf")
t:check(c.visible > 0, name .. ": vis ran")
-- contents in the point hull and the clipping hulls
local cBad = 0
for _, pr in c.probes do
for h = 0, 2 do
local got = Bsp.contents(sc, pr.p[1], pr.p[2], pr.p[3], h)
if got ~= pr.c[h + 1] then
cBad += 1
t:note("%s: contents at %s hull %d: %s, want %s", name, table.concat(pr.p, " "), h, tostring(got), tostring(pr.c[h + 1]))
end
end
end
t:eq(cBad, 0, name .. ": contents at the probes")
t:eq(Bsp.contents(sc, -128, -128, 100, 0), -1, name .. ": the room is empty")
t:eq(Bsp.contents(sc, 0, 0, 100, 0), -2, name .. ": the pillar is solid")
t:eq(Bsp.contents(sc, -250, 0, 100, 1), -2, name .. ": hull 1 is solid near a wall")
t:eq(Bsp.contents(sc, 0, 0, -8, 0), -2, name .. ": the floor is solid")
local leaf = Bsp.leafAt(sc, -128, -128, 24)
t:check(leaf and leaf > 0, name .. ": player start leaf")
local seen = Bsp.pvs(sc, leaf :: number) or {}
t:check(table.find(seen, Bsp.leafAt(sc, 0, 0, 220) :: number) ~= nil, name .. ": the light's leaf is visible from the start")
end
t:fuzz("bsp2", function(b: buffer)
return Bsp.decode(Bytes.reader(b), { lit = Fx.ericw.bsp2.lit })
end, { Fx.ericw.bsp2.data, Fx.ericw["2psb"].data, Fx.ericw.bsp29.data }, { mutations = 60 })
end
t:done()
+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()