-- model/buildmap tests against an independent Python reading and Ken Silverman's map2stl (tools/fixtures/buildmap.py): -- raw tables, loops, slope heights, floor / wall areas and centroids of the Scene, sprites, v8 / v9, write round trips. local T = require("@tests/T") local BuildMap = require("@rosetta/model/buildmap") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Fx = require("@tests/gen/buildmap") local t = T.suite("buildmap") t:eq(BuildMap.name, "buildmap", "name") t:check(BuildMap.caps.decode and BuildMap.caps.encode, "caps") local function near(a: number, b: number, tol: number): boolean return math.abs(a - b) <= tol * math.max(1, math.abs(b)) end -- area and area-weighted centroid of the triangles of the given primitive kinds, in map2stl's frame (x / 512, y / 512, -- z / 8192, z down) local function stats(scene: Types.Scene, kinds: { [string]: boolean }): (number, { number }) local area, cx, cy, cz = 0, 0, 0, 0 for _, prim in scene.meshes[1].primitives do if kinds[prim.extras.build.kind] then local get = assert(Types.Scene.reader(prim.attributes.POSITION)) for i = 0, prim.attributes.POSITION.count - 1, 3 do local p: { { number } } = {} for k = 0, 2 do p[k + 1] = { get(i + k, 0), -get(i + k, 1), -get(i + k, 2) } end local ux, uy, uz = p[2][1] - p[1][1], p[2][2] - p[1][2], p[2][3] - p[1][3] local vx, vy, vz = p[3][1] - p[1][1], p[3][2] - p[1][2], p[3][3] - p[1][3] local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx local a = 0.5 * math.sqrt(nx * nx + ny * ny + nz * nz) area += a cx += a * (p[1][1] + p[2][1] + p[3][1]) / 3 cy += a * (p[1][2] + p[2][2] + p[3][2]) / 3 cz += a * (p[1][3] + p[2][3] + p[3][3]) / 3 end end end return area, if area > 0 then { cx / area, cy / area, cz / area } else { 0, 0, 0 } end local SECTOR_FIELDS = { "wallptr", "wallnum", "ceilingz", "floorz", "ceilingstat", "floorstat", "ceilingpicnum", "ceilingheinum", "ceilingshade", "ceilingpal", "ceilingxpanning", "ceilingypanning", "floorpicnum", "floorheinum", "floorshade", "floorpal", "floorxpanning", "floorypanning", "visibility", "filler", "lotag", "hitag", "extra" } local WALL_FIELDS = { "x", "y", "point2", "nextwall", "nextsector", "cstat", "picnum", "overpicnum", "shade", "pal", "xrepeat", "yrepeat", "xpanning", "ypanning", "lotag", "hitag", "extra" } local SPRITE_FIELDS = { "x", "y", "z", "cstat", "picnum", "shade", "pal", "clipdist", "filler", "xrepeat", "yrepeat", "xoffset", "yoffset", "sectnum", "statnum", "ang", "owner", "xvel", "yvel", "zvel", "lotag", "hitag", "extra" } for _, m in Fx.maps do local data = buffer.fromstring(m.data) local label = m.name t:check(BuildMap.sniff(Bytes.reader(data)) >= 0.5, label .. ": sniff") local scene, err = BuildMap.decode(Bytes.reader(data), { scale = 1 / 512, skyWalls = true }) t:check(scene, label .. ": " .. tostring(err)) local s = scene :: Types.Scene local map = s.meta.build t:eq(map.version, m.version, label .. ": version") t:eq(#map.sectors, m.counts.sectors, label .. ": sectors") t:eq(#map.walls, m.counts.walls, label .. ": walls") t:eq(#map.sprites, m.counts.sprites, label .. ": sprites") for _, part in { { map.sectors, SECTOR_FIELDS, "sector" }, { map.walls, WALL_FIELDS, "wall" }, { map.sprites, SPRITE_FIELDS, "sprite" } } do for _, f in part[2] :: { string } do local sum = 0 for _, rec in part[1] :: { any } do sum += rec[f] end t:eq(sum, m.sums[part[3]][f], label .. ": " .. part[3] .. "." .. f .. " sum") end end for si, want in m.loops do local got = BuildMap.loops(map, si - 1) t:eq(#got, #want, label .. ": sector " .. (si - 1) .. " loops") for li, l in want do t:eq(table.concat(got[li], ","), table.concat(l, ","), label .. ": sector " .. (si - 1) .. " loop " .. li) end end for _, z in m.z do t:check(near(BuildMap.slopeZ(map, z.sector, true, z.x, z.y), z.floor, 1e-9), label .. ": floor z") t:check(near(BuildMap.slopeZ(map, z.sector, false, z.x, z.y), z.ceiling, 1e-9), label .. ": ceiling z") end if m.stl then local fa, fc = stats(s, { floor = true, ceiling = true }) local wa, wc = stats(s, { wall = true }) t:check(near(fa, m.stl.floorArea, 1e-4), string.format("%s: floor area %.5f vs map2stl %.5f", label, fa, m.stl.floorArea)) t:check(near(wa, m.stl.wallArea, 1e-4), string.format("%s: wall area %.5f vs map2stl %.5f", label, wa, m.stl.wallArea)) for k = 1, 3 do t:check(near(fc[k], m.stl.floorCentroid[k], 1e-4), label .. ": floor centroid " .. k) t:check(near(wc[k], m.stl.wallCentroid[k], 1e-4), label .. ": wall centroid " .. k) end end -- sprites: nodes with their tile, in world units local sprites = 0 for _, node in s.nodes do local b = node.extras and node.extras.build if b and b.sprite then sprites += 1 t:eq(b.tile, b.sprite.picnum, label .. ": sprite tile") local tr = node.translation :: { number } t:check(near(tr[1], b.sprite.x / 512, 1e-9) and near(tr[2], -b.sprite.y / 512, 1e-9) and near(tr[3], -b.sprite.z / 8192, 1e-9), label .. ": sprite position") end end t:eq(sprites, m.counts.sprites, label .. ": sprite nodes") -- write back local again = assert(BuildMap.encode(s)) t:eq(buffer.tostring(again), m.data, label .. ": round trip") t:eq(buffer.tostring(assert(BuildMap.encode(map))), m.data, label .. ": round trip from the map table") end -- details do local rooms = assert(BuildMap.decode(Bytes.reader(buffer.fromstring(Fx.maps[1].data)))) local kinds: { [string]: number } = {} for _, prim in rooms.meshes[1].primitives do local k = prim.extras.build.kind kinds[k] = (kinds[k] or 0) + prim.attributes.POSITION.count t:check(rooms.materials[prim.material :: number].baseColorTexture ~= nil, "material texture") end t:check(kinds.floor and kinds.ceiling and kinds.wall and kinds.masked, "floor, ceiling, wall and masked parts") t:eq(rooms.meta.up, "z", "z up") -- facing: floors up, ceilings down, the hall's north wall (Build y = 0) towards the hall (world -y) local facing = 0 for _, prim in rooms.meshes[1].primitives do local kind = prim.extras.build.kind local pos = assert(Types.Scene.reader(prim.attributes.POSITION)) local nrm = assert(Types.Scene.reader(prim.attributes.NORMAL)) for i = 0, prim.attributes.POSITION.count - 1 do if kind == "floor" and nrm(i, 2) <= 0 or kind == "ceiling" and nrm(i, 2) >= 0 then facing += 1 elseif kind == "wall" and pos(i, 1) == 0 and math.abs(nrm(i, 0)) < 1e-6 and math.abs(nrm(i, 1) + 1) > 1e-6 then facing += 1 end end end t:eq(facing, 0, "faces point into the sectors") t:eq(rooms.nodes[2].name, "start", "start node") -- tile sizes and images from opts.tiles local img = Types.Image.new(32, 16) local withTiles = assert(BuildMap.decode(Bytes.reader(buffer.fromstring(Fx.maps[1].data)), { tiles = { [77] = img } })) local found = false for _, tex in withTiles.textures do if tex.extras and tex.extras.build.tile == 77 then found = tex.image == img end end t:check(found, "tile image attached") -- parallax: walls between two sky ceilings left out unless skyWalls local skyData = buffer.fromstring(Fx.maps[2].data) local a = assert(BuildMap.decode(Bytes.reader(skyData))) local b = assert(BuildMap.decode(Bytes.reader(skyData), { skyWalls = true })) local function wallVerts(sc: Types.Scene): number local n = 0 for _, prim in sc.meshes[1].primitives do if prim.extras.build.kind == "wall" then n += prim.attributes.POSITION.count end end return n end t:check(wallVerts(a) < wallVerts(b), "sky walls dropped") -- v9: room over room fields; linked floors / ceilings not drawn local yax = assert(BuildMap.decode(Bytes.reader(buffer.fromstring(Fx.maps[3].data)))) local ym = yax.meta.build t:eq(ym.sectors[1].ceilingbunch, 3, "yax ceiling bunch") t:eq(ym.sectors[2].floorbunch, 3, "yax floor bunch") t:eq(ym.walls[1].yaxceilingwall, 4, "yax ceiling wall") t:eq(ym.walls[6].yaxfloorwall, 0, "yax floor wall") local flats = 0 for _, prim in yax.meshes[1].primitives do if prim.extras.build.kind == "floor" or prim.extras.build.kind == "ceiling" then flats += prim.attributes.POSITION.count end end t:eq(flats, 12, "yax-linked ceiling / floor skipped (two squares of two triangles left)") t:check(Fx.maps[4].name == "trailing" and buffer.len(assert(BuildMap.parse(Bytes.reader(buffer.fromstring(Fx.maps[4].data)))).trailing :: buffer) == 12, "trailing bytes kept") -- version override local v8 = assert(BuildMap.encode(rooms, { version = 8 })) t:eq(buffer.readu32(v8, 0), 8, "version override") end -- errors do local function code(s: string): string? local _, e = BuildMap.decode(Bytes.reader(buffer.fromstring(s))) return e and e.code end t:eq(code("BLM\26" .. string.rep("\0", 40)), Err.UNSUPPORTED, "Blood map") t:eq(code("\6\0\0\0" .. string.rep("\0", 40)), Err.UNSUPPORTED, "version 6") t:eq(code("// Game: Quake\n{\n\"classname\" \"worldspawn\"\n}\n"), Err.CORRUPT, "Quake map") t:eq(BuildMap.sniff(Bytes.reader(buffer.fromstring("// Game: Quake\n{\n\"classname\" \"worldspawn\"\n}\n"))), 0, "sniff Quake map") t:eq(code(string.sub(Fx.maps[1].data, 1, 100)), Err.TRUNCATED, "truncated") local _, e = BuildMap.encode({ sectors = {}, walls = { { x = 1 } }, sprites = {} }) t:check(e and e.code == Err.ARG, "encode: missing field") local _, e2 = BuildMap.encode(42) t:check(e2 and e2.code == Err.ARG, "encode: not a map") end local samples = {} for _, m in Fx.maps do table.insert(samples, m.data) end t:fuzz("decode", function(b: buffer) return BuildMap.decode(Bytes.reader(b)) end, samples, { mutations = 200 }) t:done()