-- archive/wad tests: a cut-down Freedoom IWAD against DeuTex, map floor areas by Green's theorem. -- Fixtures: tools/fixtures/wad.py. local T = require("@tests/T") local F = require("@rosetta") local Wad = require("@rosetta/archive/wad") 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/wad") local t = T.suite("wad") t:eq(Wad.name, "wad", "name") local fd = Fx.freedoom local a, err = Wad.decode(Bytes.reader(buffer.fromstring(fd.data))) t:check(a, "freedoom: " .. tostring(err)) t:eq(F.detect(fd.data), "wad", "F.detect") t:eq(a.meta.source.type, "IWAD", "IWAD") do local lumps = Wad.lumps(a) :: { Wad.Lump } t:eq(#lumps, #fd.lumps, "lump count") local list = a:list() for i, w in fd.lumps do t:eq(lumps[i].name, w.name, "lump " .. i) t:eq(lumps[i].size, w.size, "size " .. w.name) t:eq(Crc32.of((a:read(list[i].name))), w.crc, "data " .. w.name) end t:eq(list[2].name, "MAP01/THINGS", "map lumps are named after their map") t:eq(lumps[2].map, "MAP01", "map membership") t:check(lumps[1].marker, "map marker") local byName = {} for _, l in lumps do byName[l.name] = l end t:eq(byName.BLOOD1.namespace, "flats", "flats namespace") t:eq(byName.BOS2A1.namespace, "sprites", "sprites namespace") t:eq(byName.AG128_1.namespace, "patches", "patches namespace") t:eq(byName.PLAYPAL.namespace, nil, "no namespace") t:eq(#Wad.maps(a), 1, "one map") end do local n = 0 for _, w in fd.images do local img, e = Wad.image(a, w.name) t:check(img, w.name .. ": " .. tostring(e)) t:eq(img.width, w.width, w.name .. ": width") t:eq(img.height, w.height, w.name .. ": height") if w.kind == "flats" then t:eq(Crc32.of(img.pixels), w.crc, w.name .. ": flat pixels against DeuTex") else t:eq(Crc32.of(img.pixels), w.crc, w.name .. ": picture pixels against DeuTex") if w.left then t:eq(img.meta.offset[1], w.left, w.name .. ": left offset") t:eq(img.meta.offset[2], w.top, w.name .. ": top offset") end end n += 1 end t:note("%d graphics match DeuTex", n) for _, tx in fd.textures do local img, e = Wad.texture(a, tx.name) t:check(img, tx.name .. ": " .. tostring(e)) t:check(img.width == tx.width and img.height == tx.height, tx.name .. ": texture size") t:eq(img.meta.source.patches, #tx.patches, tx.name .. ": patch count") end end do local m = Wad.mapData(a, "MAP01") :: any local r = fd.map t:eq(#m.things, r.things, "things") t:eq(#m.linedefs, r.linedefs, "linedefs") t:eq(#m.sidedefs, r.sidedefs, "sidedefs") t:eq(#m.vertices // 2, r.vertices, "vertices") t:eq(#m.sectors, r.sectors, "sectors") for i, th in r.thingList do local g = m.things[i] t:check(g.x == th.x and g.y == th.y and g.type == th.type and g.angle == th.angle, "thing " .. i) end local t0 = T.clock() local s, e = Wad.map(a, "MAP01") t:check(s, "map scene: " .. tostring(e)) local floor, nprim = 0, 0 for _, p in s.meshes[1].primitives do nprim += 1 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 if nrm(k, 2) > 0.5 then local ax, ay, bx, by, cx, cy = pos(k, 0), pos(k, 1), pos(k + 1, 0), pos(k + 1, 1), pos(k + 2, 0), pos(k + 2, 1) floor += ((bx - ax) * (cy - ay) - (by - ay) * (cx - ax)) / 2 end end end t:check(math.abs(floor - r.floorArea) <= r.floorArea * 0.01, string.format("floor area %.0f vs %.0f", floor, r.floorArea)) t:check(#s.nodes == 1 + r.things, "a node per thing") t:eq(s.meta.up, "z", "z up") local withImage = 0 for _, tex in s.textures do if tex.image then withImage += 1 end end t:note("MAP01: %d primitives, %d textures (%d with images in the cut-down WAD), floor area %.0f of %.0f, %.2f s", nprim, #s.textures, withImage, floor, r.floorArea, T.clock() - t0) local lean = Wad.map(a, "MAP01", { textures = false }) :: any t:check(lean.textures[1].image == nil and lean.textures[1].uri ~= nil, "textures = false") end do local rm = Wad.decode(Bytes.reader(buffer.fromstring(Fx.room.data))) t:check(rm, "room decodes") local names = {} for _, e in rm:list() do table.insert(names, e.name) end t:eq(names[2], "E1M1/THINGS", "E1M1 lumps") t:eq(names[16], "E1M2/TEXTMAP", "UDMF lumps belong to their map") t:eq(names[17], "E1M2/ENDMAP", "up to ENDMAP") t:eq(names[18], "DEMO1", "after the map") t:eq(names[19], "DEMO1~2", "duplicate names get ~n") local _, ue = Wad.map(rm, "E1M2") t:check(ue and ue.code == Err.UNSUPPORTED, "UDMF unsupported") local s = Wad.map(rm, "E1M1") :: any local area = { floor = 0, ceil = 0, wall = 0 } for _, p in s.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 -- the winding agrees with the normal t:check(cx * nrm(k, 0) + cy * nrm(k, 1) + cz * nrm(k, 2) > 0, "winding faces the normal") 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, "floors: the room around the pillar + the pillar top") t:eq(area.ceil, 256 * 256, "ceilings") t:eq(area.wall, 4 * 256 * 128 + 4 * 64 * 32, "outer walls + the pillar's lower walls") t:eq(s.nodes[2].translation[3], 0, "thing on the room floor") t:eq(s.nodes[3].translation[3], 32, "thing on the pillar") t:eq(s.nodes[2].extras.thing.type, 1, "thing type") local tall = Wad.image(rm, "TALL") :: any t:eq(tall.height, 300, "tall patch height") t:eq(buffer.readu32(tall.pixels, 0), 0, "tall patch: transparent above the first post") t:eq(buffer.readu32(tall.pixels, 100 * 4), buffer.readu32((Wad.palette(rm) :: buffer), 7 * 4), "tall patch: first post") t:eq(buffer.readu32(tall.pixels, 249 * 4), buffer.readu32((Wad.palette(rm) :: buffer), 9 * 4), "tall patch: relative topdelta 100 + 50") t:eq(buffer.readu32(tall.pixels, 260 * 4), 0, "tall patch: transparent below") local flat = Wad.image(rm, "FLOOR") :: any t:check(flat.width == 64 and flat.height == 64, "flat 64 x 64") -- writer: lossless for decoded WADs (map lumps and ~n names stripped back) t:check(buffer.tostring(F.save(rm) :: buffer) == Fx.room.data, "room: decode + save is byte-identical") end do t:check(buffer.tostring(F.save(a) :: buffer) == fd.data, "freedoom: decode + save is byte-identical") local p = Wad.new() p:add("dir/demo1", "abc") p:add("MAP02/THINGS", "") local back = Wad.decode(Bytes.reader(p:write() :: buffer)) t:eq(Wad.lumps(back)[1].name, "DEMO1", "names upper-cased, paths stripped") t:eq(back.meta.source.type, "PWAD", "PWAD by default") local long = Wad.new() long:add("NINECHARS", "x") local _, e = long:write() t:check(e and e.code == Err.ARG, "9-byte lump name") local zip = F.format("zip").create() zip:add("x.lmp", "1") t:eq(Wad.lumps(F.load(F.save(zip, nil, { format = "wad" }) :: buffer))[1].name, "X.LMP", "zip -> wad") end do local _, e1 = Wad.decode(Bytes.reader(buffer.fromstring(string.sub(fd.data, 1, 100)))) t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1)) local _, e2 = Wad.image(a, "NOPE") t:check(e2 and e2.code == Err.ARG, "missing lump") local _, e3 = Wad.image({}, "X") t:check(e3 and e3.code == Err.ARG, "not a WAD") local _, e4 = Wad.map(a, "MAP99") t:check(e4 and e4.code == Err.ARG, "missing map") end t:fuzz("decode", function(b: buffer) local w = Wad.decode(Bytes.reader(b)) if w then for _, l in Wad.lumps(w) :: any do if not l.map and l.size > 0 then Wad.image(w, l.name) end end for _, m in Wad.maps(w) do Wad.map(w, m) end Wad.texture(w, "X") end return w end, { Fx.room.data }, { mutations = 300 }) do local rand = T.rng(9) local threw = 0 for i = 1, 60 do local b = buffer.fromstring(fd.data) for _ = 1, 1 + rand(20) do local at = 12 + rand(#fd.data - 12 - (#fd.lumps * 16)) buffer.writeu8(b, at, rand(256)) end local ok = pcall(function() local w = Wad.decode(Bytes.reader(b)) if w then if i % 6 == 0 then Wad.map(w, "MAP01") end for _, im in fd.images do Wad.image(w, im.name) end end end) if not ok then threw += 1 end end t:eq(threw, 0, "mutated lumps never throw") end t:done()