merge ws/idtech: UDMF (codecs/model/DoomMap), Doom 3 / Quake 4 .map + .proc, Wolf3D VSWAP + GAMEMAPS, codecs/text/Lexer

This commit is contained in:
4DBug committed 2026-11-01 19:48:30 -06:00
commit 0e0b213a80
38 files changed
+11791 -2566

No files matched your search

+161
View File
@@ -0,0 +1,161 @@
-- model/d3map tests: an editor-layout Doom 3 / Quake 4 map written by the fixture: brushDef3 side areas against qhull
-- (scipy HalfspaceIntersection), texture coordinates against Doom 3's ComputeAxisBase + matrix, the patchDef3 grid
-- against a direct Bezier evaluation, entities. Fixtures: tools/fixtures/d3map.py.
local T = require("@tests/T")
local F = require("@rosetta")
local D3map = require("@rosetta/model/d3map")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/d3map")
local t = T.suite("d3map")
t:eq(D3map.name, "d3map", "name")
t:eq(F.detect(Fx.doom3, { name = "test.map" }), "d3map", "F.detect: Version 2 is not a Quake map")
t:eq(F.detect(Fx.quake4, { name = "test.map" }), "d3map", "F.detect: Version 3")
local s, err = D3map.decode(Bytes.reader(buffer.fromstring(Fx.doom3)))
t:check(s, "doom3: " .. tostring(err))
local sc = s :: any
t:eq(sc.meta.source.variant, "doom3", "variant")
t:eq((F.load(Fx.quake4, { format = "d3map" }) :: any).meta.source.variant, "quake4", "Quake 4")
t:eq(sc.meta.counts.brushes, 3, "brushes")
t:eq(sc.meta.counts.patches, 2, "patches")
t:eq(sc.meta.counts.skippedPrimitives, 1, "brushDef skipped")
local byMaterial: { [string]: any } = {}
for _, mesh in sc.meshes do
for _, p in mesh.primitives do
byMaterial[sc.materials[p.material].name] = p
end
end
local function tris(p: any, fn: (ax: number, ay: number, az: number, bx: number, by: number, bz: number, cx: number, cy: number, cz: number, a: number, b: number, c: number) -> ())
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local idx = Types.Scene.reader(p.indices) :: any
for k = 0, p.indices.count - 1, 3 do
local a, b, c = idx(k, 0), idx(k + 1, 0), idx(k + 2, 0)
fn(pos(a, 0), pos(a, 1), pos(a, 2), pos(b, 0), pos(b, 1), pos(b, 2), pos(c, 0), pos(c, 1), pos(c, 2), a, b, c)
end
end
do
local bad, uvBad, windBad, faces = {}, 0, 0, 0
for _, w in Fx.sides do
local p = byMaterial[w.material]
if w.area < 1e-6 then
t:check(p == nil, w.material .. ": a redundant side has no polygon")
elseif not p then
table.insert(bad, w.material .. " missing")
else
faces += 1
local area = 0
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
tris(p, function(ax, ay, az, bx, by, bz, cx, cy, cz, a)
local ux, uy, uz, vx, vy, vz = bx - ax, by - ay, bz - az, cx - ax, cy - ay, cz - az
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
area += math.sqrt(nx * nx + ny * ny + nz * nz) / 2
if nx * nrm(a, 0) + ny * nrm(a, 1) + nz * nrm(a, 2) <= 0 then
windBad += 1
end
end)
if math.abs(area - w.area) > 1e-3 * math.max(1, w.area) then
table.insert(bad, string.format("%s: %.4f vs %.4f", w.material, area, w.area))
end
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local uv = Types.Scene.reader(p.attributes.TEXCOORD_0) :: any
local v = w.vecs
for i = 0, p.attributes.POSITION.count - 1 do
local x, y, z = pos(i, 0), pos(i, 1), pos(i, 2)
local su = x * v[1] + y * v[2] + z * v[3] + v[4]
local tv = x * v[5] + y * v[6] + z * v[7] + v[8]
if math.abs(uv(i, 0) - su) > 1e-3 or math.abs(uv(i, 1) - tv) > 1e-3 then
uvBad += 1
end
end
end
end
t:eq(#bad, 0, "side areas against qhull: " .. table.concat(bad, "; ", 1, math.min(#bad, 4)))
t:eq(uvBad, 0, "texture coordinates: ComputeAxisBase + the texture matrix")
t:eq(windBad, 0, "brush triangles counter-clockwise about the side normal")
t:note("%d brush sides with polygons match qhull; volumes %s", faces, table.concat(Fx.volumes :: any, ", "))
end
do
local p = byMaterial["textures/test/patchDef3"]
t:check(p, "patchDef3 primitive")
local w = Fx.patch3
t:eq(p.attributes.POSITION.count, w.width * w.height, "patchDef3: explicit subdivisions")
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local worst = 0
for i = 0, w.width * w.height - 1 do
for c = 0, 2 do
worst = math.max(worst, math.abs(pos(i, c) - w.points[i * 3 + c + 1]))
end
end
t:check(worst < 1e-3, string.format("patchDef3 grid against the Bezier evaluation (worst %.2g)", worst))
local p2 = byMaterial["textures/test/patchDef2"]
t:check(p2, "patchDef2 primitive")
local pos2 = Types.Scene.reader(p2.attributes.POSITION) :: any
local c = Fx.patch2.corners
t:check(pos2(0, 0) == c[1] and pos2(0, 1) == c[2] and math.abs(pos2(0, 2) - c[3]) < 1e-4, "patchDef2 first corner")
local n2 = p2.attributes.POSITION.count - 1
t:check(pos2(n2, 0) == c[4] and pos2(n2, 1) == c[5] and math.abs(pos2(n2, 2) - c[6]) < 1e-4, "patchDef2 last corner")
local nrm = Types.Scene.reader(p2.attributes.NORMAL) :: any
local agree = 0
local total = 0
tris(p2, function(ax, ay, az, bx, by, bz, cx, cy, cz, a)
local ux, uy, uz, vx, vy, vz = bx - ax, by - ay, bz - az, cx - ax, cy - ay, cz - az
local d = (uy * vz - uz * vy) * nrm(a, 0) + (uz * vx - ux * vz) * nrm(a, 1) + (ux * vy - uy * vx) * nrm(a, 2)
total += 1
if d > 0 then
agree += 1
end
end)
t:eq(agree, total, "patch normals face the triangle winding")
end
do
local named: { [string]: any } = {}
for _, n in sc.nodes do
named[n.name] = n
end
t:eq(#sc.nodes, 1 + 4, "root + 4 entities")
t:check(named.worldspawn and named.worldspawn.mesh, "worldspawn mesh")
t:check(named.crate and named.crate.mesh and named.crate.extras.keys.model == "crate", "func_static: mesh and keys")
t:check(named.crate.translation == nil, "brush entities stay in world coordinates")
local l = named.light_1
t:check(l.translation[1] == 10 and l.translation[3] == 30, "light origin")
local q = l.rotation
t:check(math.abs(q[3] - math.sqrt(0.5)) < 1e-6 and math.abs(q[4] - math.sqrt(0.5)) < 1e-6, "rotation matrix rows: yaw 90")
local ps = named.info_player_start_1
t:check(math.abs(ps.rotation[3] - math.sqrt(0.5)) < 1e-6, "angle 90")
t:eq(ps.extras.classname, "info_player_start", "classname")
t:eq(sc.meta.entities[1].classname, "worldspawn", "meta.entities")
local lean = D3map.decode(Bytes.reader(buffer.fromstring(Fx.doom3)), { skip = { "textures/test/box_0", "TEXTURES/TEST/PATCHDEF2" } }) :: any
local seen = {}
for _, mesh in lean.meshes do
for _, p in mesh.primitives do
seen[lean.materials[p.material].name] = true
end
end
t:check(not seen["textures/test/box_0"] and not seen["textures/test/patchDef2"] and seen["textures/test/box_1"], "skip")
end
do
local function code(src: string, opts: any?): any
local _, e = D3map.decode(Bytes.reader(buffer.fromstring(src)), opts)
return e and e.code
end
t:eq(code("{ }"), Err.CORRUPT, "no Version")
t:eq(code("Version 2\n{ \"classname\" \"worldspawn\"\n{ brushDef3 { ( 0 0 1 0 ) ( ( 1 0 0 ) ( 0 1 0 ) ) \"x\" 0 0 0 }"), Err.CORRUPT, "truncated")
t:eq(code("Version 2\n{ \"classname\" \"worldspawn\" { patchDef2 { \"x\" ( 4 3 0 0 0 ) ( ) } } }"), Err.CORRUPT, "short patch")
t:eq(code(Fx.doom3, { maxPrimitives = 2 }), Err.LIMIT, "maxPrimitives")
t:eq(code("Version 2\n"), Err.CORRUPT, "no entities")
t:check(F.detect("Version 2\n{\n\"classname\" \"worldspawn\"\n}\n") == "d3map", "an empty map sniffs")
end
t:fuzz("decode", function(b: buffer)
return D3map.decode(Bytes.reader(b))
end, { buffer.fromstring(Fx.doom3) }, { mutations = 200 })
t:done()
+87
View File
@@ -0,0 +1,87 @@
-- model/proc tests: dmap-layout Doom 3 and Quake 4 files written by the fixture (models, surfaces, portals, nodes,
-- shadow models) against the writer's geometry. Fixtures: tools/fixtures/proc.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Proc = require("@rosetta/model/proc")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/proc")
local t = T.suite("proc")
t:eq(Proc.name, "proc", "name")
for key, c in Fx do
t:eq(F.detect(c.text), "proc", key .. ": F.detect")
local s, e = Proc.decode(Bytes.reader(buffer.fromstring(c.text)))
t:check(s, key .. ": " .. tostring(e))
local sc = s :: any
t:eq(#sc.meshes, #c.models, key .. ": models")
for mi, m in c.models do
local mesh = sc.meshes[mi]
t:eq(mesh.name, m.name, key .. ": model name")
t:eq(#mesh.primitives, #m.surfaces, key .. ": surfaces of " .. m.name)
for si, w in m.surfaces do
local p = mesh.primitives[si]
local label = key .. " " .. m.name .. " surface " .. si
t:eq(p.attributes.POSITION.count, w.verts, label .. ": vertices")
t:eq(Crc32.of(p.attributes.POSITION.data), w.pos, label .. ": positions")
t:eq(Crc32.of(p.attributes.TEXCOORD_0.data), w.uv, label .. ": texcoords")
t:eq(Crc32.of(p.indices.data), w.idx, label .. ": indices, counter-clockwise")
t:eq(sc.materials[p.material].name, w.material, label .. ": material")
-- every triangle faces its vertex normal after the turn
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
local idx = Types.Scene.reader(p.indices) :: any
local ok = true
for k = 0, p.indices.count - 1, 3 do
local a, b, cc = idx(k, 0), idx(k + 1, 0), idx(k + 2, 0)
local ux, uy, uz = pos(b, 0) - pos(a, 0), pos(b, 1) - pos(a, 1), pos(b, 2) - pos(a, 2)
local vx, vy, vz = pos(cc, 0) - pos(a, 0), pos(cc, 1) - pos(a, 1), pos(cc, 2) - pos(a, 2)
local d = (uy * vz - uz * vy) * nrm(a, 0) + (uz * vx - ux * vz) * nrm(a, 1) + (ux * vy - uy * vx) * nrm(a, 2)
ok = ok and d > 0
end
t:check(ok, label .. ": winding faces the normals")
if key == "quake4" then
t:check(p.attributes.COLOR_0 ~= nil, label .. ": vertex colours")
end
end
end
local info = sc.meta.proc
t:eq(info.areas, c.areas, key .. ": areas")
t:eq(#info.portals, #c.portals, key .. ": portals")
t:eq(info.portals[1].positive, c.portals[1].positive, key .. ": portal areas")
t:eq(#info.portals[1].points, #c.portals[1].points, key .. ": portal points")
t:eq(info.portals[1].points[12], c.portals[1].points[12], key .. ": portal point")
t:eq(#info.nodes, #c.nodes, key .. ": nodes")
t:eq(info.nodes[1].positive, 2, key .. ": node children 1-based")
t:eq(info.nodes[1].negative, -1, key .. ": area children kept")
t:eq(info.nodes[1].plane[4], c.nodes[1].plane[4], key .. ": node plane")
t:eq(info.shadowModels[1].name, c.shadow.name, key .. ": shadow model")
t:eq(#info.shadowModels[1].vertices, c.shadow.verts * 3, key .. ": shadow vertices")
t:eq(#info.shadowModels[1].indices, c.shadow.indices, key .. ": shadow indices")
t:eq(info.models[1].area, 0, key .. ": area number")
t:eq(#sc.nodes, 1 + #c.models, key .. ": a node per model")
t:eq(sc.meta.up, "z", key .. ": z up")
end
t:eq((F.load(Fx.quake4.text) :: any).meta.source.version, "PROC 4", "Quake 4 version")
do
local function code(s: string, opts: any?): any
local _, e = Proc.decode(Bytes.reader(buffer.fromstring(s)), opts)
return e and e.code
end
t:eq(code("hello"), Err.CORRUPT, "not a proc")
t:eq(code('mapProcFile003 model { "m" 1 { "x" 1 3 ( 0 0 0 0 0 0 0 1 ) 0 0 5 } }'), Err.CORRUPT, "index out of range")
t:eq(code('mapProcFile003 model { "m" 1 { "x" 1 3 ( 0 0 0 ) 0 0 0 } }'), Err.CORRUPT, "short vertex")
t:eq(code('mapProcFile003 model { "m" 1 { "x" 1 3 ( 0 0 0 0 0 0 0 1 ) 0 0'), Err.CORRUPT, "truncated")
t:eq(code(Fx.doom3.text, { maxVertices = 10 }), Err.LIMIT, "maxVertices")
local s = Proc.decode(Bytes.reader(buffer.fromstring("mapProcFile003 unknownSection { a { b } c } model { \"m\" 0 }"))) :: any
t:check(s and #s.meshes == 1, "unknown sections skipped")
end
t:fuzz("decode", function(b: buffer)
return Proc.decode(Bytes.reader(b))
end, { buffer.fromstring(Fx.doom3.text), buffer.fromstring(Fx.quake4.text) }, { mutations = 300 })
t:done()
+173
View File
@@ -0,0 +1,173 @@
-- model/udmf + archive/wad UDMF tests: Freedoom MAP01 converted to TEXTMAP against its binary lumps (same tables, same
-- Scene geometry), and a hand-written ZDoom room with exact areas. Fixtures: tools/fixtures/udmf.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Udmf = require("@rosetta/model/udmf")
local Wad = require("@rosetta/archive/wad")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/udmf")
local t = T.suite("udmf")
t:eq(Udmf.name, "udmf", "name")
t:eq(F.detect(Fx.room), "udmf", "F.detect")
do
local bin = Wad.decode(Bytes.reader(buffer.fromstring(Fx.binary))) :: any
local udmf = Wad.decode(Bytes.reader(buffer.fromstring(Fx.textmap))) :: any
t:eq(Wad.maps(udmf)[1], "MAP01", "UDMF map listed")
local a = Wad.mapData(bin, "MAP01") :: any
local b, e = Wad.mapData(udmf, "MAP01")
t:check(b, "TEXTMAP: " .. tostring(e))
local m = b :: any
t:eq(m.format, "udmf", "format")
t:eq(m.namespace, "Doom", "namespace")
t:eq(#m.vertices, #a.vertices, "vertices")
local same = true
for i, v in a.vertices do
same = same and m.vertices[i] == v
end
t:check(same, "vertex coordinates")
for _, k in { "linedefs", "sidedefs", "sectors", "things" } do
t:eq(#m[k], #a[k], k)
end
local bad = {}
for i, l in a.linedefs do
local u = m.linedefs[i]
if u.v1 ~= l.v1 or u.v2 ~= l.v2 or u.flags ~= l.flags or u.special ~= l.special or (l.special ~= 0 and u.tag ~= l.tag) or u.front ~= l.front or u.back ~= l.back then
table.insert(bad, "linedef " .. i)
end
end
for i, s in a.sidedefs do
local u = m.sidedefs[i]
if u.xoff ~= s.xoff or u.yoff ~= s.yoff or u.upper ~= s.upper or u.lower ~= s.lower or u.middle ~= s.middle or u.sector ~= s.sector then
table.insert(bad, "sidedef " .. i)
end
end
for i, s in a.sectors do
local u = m.sectors[i]
if u.floor ~= s.floor or u.ceiling ~= s.ceiling or u.floorFlat ~= s.floorFlat or u.ceilingFlat ~= s.ceilingFlat or u.light ~= s.light or u.special ~= s.special or u.tag ~= s.tag then
table.insert(bad, "sector " .. i)
end
end
for i, th in a.things do
local u = m.things[i]
if u.x ~= th.x or u.y ~= th.y or u.type ~= th.type or u.angle ~= th.angle or u.flags ~= th.flags then
table.insert(bad, "thing " .. i)
end
end
t:eq(#bad, 0, "TEXTMAP tables equal the binary ones: " .. table.concat(bad, ", ", 1, math.min(#bad, 5)))
local t0 = T.clock()
local s1 = Wad.map(bin, "MAP01") :: any
local s2, se = Wad.map(udmf, "MAP01")
t:check(s2, "TEXTMAP scene: " .. tostring(se))
local dt = T.clock() - t0
local p1, p2 = s1.meshes[1].primitives, (s2 :: any).meshes[1].primitives
t:eq(#p2, #p1, "primitives")
local geo = true
for i, p in p1 do
local q = p2[i]
geo = geo and q ~= nil and p.extras.wad.texture == q.extras.wad.texture
and buffer.tostring(p.attributes.POSITION.data) == buffer.tostring(q.attributes.POSITION.data)
and buffer.tostring(p.attributes.TEXCOORD_0.data) == buffer.tostring(q.attributes.TEXCOORD_0.data)
end
t:check(geo, "the TEXTMAP Scene is the binary map's geometry, byte for byte")
local withImage = 0
for _, tex in (s2 :: any).textures do
if tex.image then
withImage += 1
end
end
t:check(withImage > 0, "textures from the WAD")
t:eq((s2 :: any).meta.source.namespace, "Doom", "meta namespace")
t:eq(#(s2 :: any).nodes, 1 + #a.things, "a node per thing")
t:note("MAP01 as UDMF: %d bytes of TEXTMAP, %d primitives, both scenes in %.2f s", #Fx.text, #p2, dt)
t:check(F.detect(Fx.text) == "udmf", "TEXTMAP text detected")
local alone = F.load(Fx.text) :: any
t:check(alone and #alone.meshes[1].primitives == #p1, "standalone TEXTMAP: same batches")
end
do
local s, e = Udmf.decode(Bytes.reader(buffer.fromstring(Fx.room)), { textureSize = function(kind: string, name: string)
if kind == "texture" then
return 64, 128
end
return 64, 64
end })
t:check(s, "room: " .. tostring(e))
local area = { floor = 0, ceil = 0, wall = 0 }
for _, p in (s :: any).meshes[1].primitives do
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
for k = 0, p.attributes.POSITION.count - 1, 3 do
local ux, uy, uz = pos(k + 1, 0) - pos(k, 0), pos(k + 1, 1) - pos(k, 1), pos(k + 1, 2) - pos(k, 2)
local vx, vy, vz = pos(k + 2, 0) - pos(k, 0), pos(k + 2, 1) - pos(k, 1), pos(k + 2, 2) - pos(k, 2)
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
local ar = math.sqrt(cx * cx + cy * cy + cz * cz) / 2
if nrm(k, 2) > 0.5 then
area.floor += ar
elseif nrm(k, 2) < -0.5 then
area.ceil += ar
else
area.wall += ar
end
end
end
t:eq(area.floor, 256 * 256, "room floors")
t:eq(area.ceil, 256 * 256, "room ceilings")
t:eq(area.wall, 4 * 256 * 128 + 4 * 64 * 32, "room walls")
local sc = s :: any
t:eq(sc.nodes[3].translation[3], 40, "thing height above the pillar floor")
t:eq(sc.nodes[3].translation[1], 128.5, "float coordinates")
t:eq(sc.meta.source.namespace, "ZDoom", "namespace (case-insensitive key)")
local m = Udmf.data(Bytes.reader(buffer.fromstring(Fx.room))) :: any
t:eq(m.globals.user_note, 'a "quoted" \\ note', "string escapes")
t:eq(m.vertices[3], 256, "hex")
t:eq(m.vertices[8], 256, "octal")
t:eq(m.sectors[1].fields.user_gravity, 0.5, "user_ field kept")
t:eq(m.sidedefs[5].fields.offsetx_bottom, 4, "namespace field kept")
t:eq(m.sidedefs[1].middle, "WALL", "texture names upper-cased")
t:eq(m.sidedefs[1].fields.texturemiddle, "Wall", "as written in fields")
t:eq(m.linedefs[2].flags, 1, "keys and keywords are case-insensitive")
t:eq(m.linedefs[4].flags, 1 + 512 + 1024, "Hexen bits: blocking, repeat, use activation")
t:eq(m.linedefs[4].tag, 7, "id is the tag outside Doom namespaces")
t:eq(m.linedefs[4].args[1], 3, "args")
t:eq(m.sectors[2].light, 200, "light")
t:eq(m.sectors[2].tag, 3, "sector id")
t:eq(m.things[2].flags, 16, "Hexen thing bits: dormant")
t:eq(m.things[2].tid, 5, "thing id")
t:eq(m.blocks[1].type, "vavoombox", "unknown block kept")
t:eq(m.blocks[1].fields.foo, 1, "unknown block fields")
end
do
local function bad(s: string): any
local _, e = Udmf.decode(Bytes.reader(buffer.fromstring(s)))
return e and e.code
end
t:eq(bad('namespace = "x"; vertex { x = 1 y = 2; }'), Err.CORRUPT, "missing ;")
t:eq(bad('namespace = "x"; /* open'), Err.CORRUPT, "unterminated comment")
t:eq(bad('namespace = "x'), Err.CORRUPT, "unterminated string")
t:eq(bad('namespace = "x"; linedef { v2 = 1; sidefront = 0; }'), Err.CORRUPT, "linedef without v1")
t:eq(bad('namespace = "x"; vertex { x = 1; y = 2; } vertex { x = 1; y = 2; } } '), Err.CORRUPT, "stray }")
local _, le = Udmf.decode(Bytes.reader(buffer.fromstring('namespace = "x"; a {} b {} c {}')), { maxBlocks = 2 })
t:check(le and le.code == Err.LIMIT, "maxBlocks")
t:check(F.detect("namespace = 1;") ~= "udmf", "a quoted namespace is required to sniff")
end
t:fuzz("decode", function(b: buffer)
return Udmf.decode(Bytes.reader(b))
end, { buffer.fromstring(Fx.room) }, { mutations = 400 })
local small = Wad.new()
small:add("E1M1", "")
small:add("TEXTMAP", Fx.room)
small:add("ENDMAP", "")
t:fuzz("wad", function(b: buffer)
local w = Wad.decode(Bytes.reader(b))
if w then
Wad.map(w, "MAP01")
end
return w
end, { small:write() :: buffer }, { mutations = 300 })
t:done()