Files

174 lines
7.2 KiB
Luau

-- 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()