codecs/model/DoomMap: the Doom map-to-Scene builder moved out of archive/wad, plus a UDMF 1.1 TEXTMAP parser (fields kept, namespace flag bits); Wad.map loads UDMF maps; model/udmf for a standalone TEXTMAP; codecs/text/Lexer (C-style tokens for id Tech / UDMF text)

STYLE.md: layout block and the 2a table list DoomMap and Lexer. tests/archive/wad: the UDMF stub map now loads (empty) instead of UNSUPPORTED.
This commit is contained in:
4DBug committed 2026-11-01 18:33:24 -06:00
1 parent 01c044805f
commit 7d4e0381fe
20 files changed
+3857 -1979

No files matched your search

-428
View File
@@ -1,428 +0,0 @@
--!strict
-- archive/wad/Map: Doom / Heretic / Hexen binary map lumps (Hexen when BEHAVIOR is present) to tables and a Scene, with
-- vanilla texture alignment (offsets, unpegged) and the sky hack; z up, map units. From the Unofficial Doom Specs and
-- the Hexen specs; no code taken. Not supported: UDMF (TEXTMAP).
local Types = require("@rosetta/Types")
local Polygon = require("@rosetta/codecs/model/Polygon")
local Map = {}
local Scene = Types.Scene
local readu8, readi16, readu16 = buffer.readu8, buffer.readi16, buffer.readu16
local writef32, writeu32 = buffer.writef32, buffer.writeu32
export type Lump = { buf: buffer, at: number, size: number }
export type Thing = { x: number, y: number, z: number?, angle: number, type: number, flags: number, tid: number?, special: number?, args: { number }? }
export type Linedef = { v1: number, v2: number, flags: number, special: number, tag: number?, args: { number }?, front: number?, back: number? }
export type Sidedef = { xoff: number, yoff: number, upper: string, lower: string, middle: string, sector: number }
export type Sector = { floor: number, ceiling: number, floorFlat: string, ceilingFlat: string, light: number, special: number, tag: number }
export type MapData = {
format: "doom" | "hexen",
things: { Thing },
linedefs: { Linedef },
sidedefs: { Sidedef },
vertices: { number }, -- x1, y1, x2, y2, ...
sectors: { Sector },
}
export type Textures = {
size: (kind: string, name: string) -> (number?, number?),
image: ((kind: string, name: string) -> Types.Image?)?,
}
-- ------------------------------------------------------------------------------------------------ parse
local function name8(b: buffer, at: number): string
local s = buffer.readstring(b, at, 8)
local z = string.find(s, "\0", 1, true)
return string.upper(if z then string.sub(s, 1, z - 1) else s)
end
local function side(v: number): number?
return if v == 0xFFFF then nil else v + 1
end
function Map.parse(lumps: { [string]: Lump }, hexen: boolean): (MapData?, string?)
for _, k in { "LINEDEFS", "SIDEDEFS", "VERTEXES", "SECTORS" } do
if not lumps[k] then
return nil, "map has no " .. k
end
end
local m: MapData = { format = if hexen then "hexen" else "doom", things = {}, linedefs = {}, sidedefs = {}, vertices = {}, sectors = {} }
local v = lumps.VERTEXES
for i = 0, v.size // 4 - 1 do
table.insert(m.vertices, readi16(v.buf, v.at + i * 4))
table.insert(m.vertices, readi16(v.buf, v.at + i * 4 + 2))
end
local s = lumps.SECTORS
for i = 0, s.size // 26 - 1 do
local o = s.at + i * 26
table.insert(m.sectors, {
floor = readi16(s.buf, o),
ceiling = readi16(s.buf, o + 2),
floorFlat = name8(s.buf, o + 4),
ceilingFlat = name8(s.buf, o + 12),
light = readi16(s.buf, o + 20),
special = readu16(s.buf, o + 22),
tag = readu16(s.buf, o + 24),
})
end
local sd = lumps.SIDEDEFS
for i = 0, sd.size // 30 - 1 do
local o = sd.at + i * 30
table.insert(m.sidedefs, {
xoff = readi16(sd.buf, o),
yoff = readi16(sd.buf, o + 2),
upper = name8(sd.buf, o + 4),
lower = name8(sd.buf, o + 12),
middle = name8(sd.buf, o + 20),
sector = readu16(sd.buf, o + 28) + 1,
})
end
local l = lumps.LINEDEFS
local ls = if hexen then 16 else 14
for i = 0, l.size // ls - 1 do
local o = l.at + i * ls
local b = l.buf
if hexen then
table.insert(m.linedefs, {
v1 = readu16(b, o) + 1,
v2 = readu16(b, o + 2) + 1,
flags = readu16(b, o + 4),
special = readu8(b, o + 6),
args = { readu8(b, o + 7), readu8(b, o + 8), readu8(b, o + 9), readu8(b, o + 10), readu8(b, o + 11) },
front = side(readu16(b, o + 12)),
back = side(readu16(b, o + 14)),
})
else
table.insert(m.linedefs, {
v1 = readu16(b, o) + 1,
v2 = readu16(b, o + 2) + 1,
flags = readu16(b, o + 4),
special = readu16(b, o + 6),
tag = readu16(b, o + 8),
front = side(readu16(b, o + 10)),
back = side(readu16(b, o + 12)),
})
end
end
local t = lumps.THINGS
if t then
local ts = if hexen then 20 else 10
for i = 0, t.size // ts - 1 do
local o = t.at + i * ts
local b = t.buf
if hexen then
table.insert(m.things, {
tid = readi16(b, o),
x = readi16(b, o + 2),
y = readi16(b, o + 4),
z = readi16(b, o + 6),
angle = readi16(b, o + 8),
type = readu16(b, o + 10),
flags = readu16(b, o + 12),
special = readu8(b, o + 14),
args = { readu8(b, o + 15), readu8(b, o + 16), readu8(b, o + 17), readu8(b, o + 18), readu8(b, o + 19) },
})
else
table.insert(m.things, {
x = readi16(b, o),
y = readi16(b, o + 2),
angle = readi16(b, o + 4),
type = readu16(b, o + 6),
flags = readu16(b, o + 8),
})
end
end
end
return m, nil
end
-- ------------------------------------------------------------------------------------------------ sector outlines
-- sector loops: front sides v1 -> v2, back sides v2 -> v1, lines with the sector on both sides left out; the sharpest
-- right turn at forks; unclosed chains dropped
local function loops(m: MapData): { [number]: { { number } } }
local edges: { [number]: { { number } } } = {}
local nv = #m.vertices // 2
local function add(sec: number, a: number, b: number)
local e = edges[sec] or {}
edges[sec] = e
table.insert(e, { a, b })
end
for _, ld in m.linedefs do
if ld.v1 >= 1 and ld.v1 <= nv and ld.v2 >= 1 and ld.v2 <= nv and ld.v1 ~= ld.v2 then
local fs = ld.front and m.sidedefs[ld.front]
local bs = ld.back and m.sidedefs[ld.back]
local f = fs and fs.sector
local b = bs and bs.sector
if f ~= b then
if f and m.sectors[f] then
add(f, ld.v1, ld.v2)
end
if b and m.sectors[b] then
add(b, ld.v2, ld.v1)
end
end
end
end
local V = m.vertices
local out: { [number]: { { number } } } = {}
for sec, list in edges do
local from: { [number]: { number } } = {}
for i, e in list do
local l = from[e[1]] or {}
from[e[1]] = l
table.insert(l, i)
end
local used: { [number]: boolean } = {}
local res: { { number } } = {}
for start = 1, #list do
if not used[start] then
used[start] = true
local loop = { list[start][1] }
local cur = start
local closed = false
for _ = 1, #list do
local e = list[cur]
local at = e[2]
if at == loop[1] then
closed = true
break
end
table.insert(loop, at)
local cands = from[at]
local best, bestT = nil :: number?, math.huge
if cands then
local ix, iy = V[at * 2 - 1] - V[e[1] * 2 - 1], V[at * 2] - V[e[1] * 2]
for _, c in cands do
if not used[c] then
local n2 = list[c][2]
local ox, oy = V[n2 * 2 - 1] - V[at * 2 - 1], V[n2 * 2] - V[at * 2]
local t = math.atan2(ix * oy - iy * ox, ix * ox + iy * oy)
if t < bestT then
best, bestT = c, t
end
end
end
end
if not best then
break
end
used[best] = true
cur = best
end
if closed and #loop >= 3 then
table.insert(res, loop)
end
end
end
out[sec] = res
end
return out
end
local function insideLoop(V: { number }, loop: { number }, x: number, y: number): boolean
local c = false
local n = #loop
local j = n
for i = 1, n do
local ax, ay = V[loop[i] * 2 - 1], V[loop[i] * 2]
local bx, by = V[loop[j] * 2 - 1], V[loop[j] * 2]
if (ay > y) ~= (by > y) and x < (bx - ax) * (y - ay) / (by - ay) + ax then
c = not c
end
j = i
end
return c
end
-- ------------------------------------------------------------------------------------------------ scene
type Batch = { kind: string, name: string, pos: { number }, nrm: { number }, uv: { number } }
local LOWER_UNPEGGED, UPPER_UNPEGGED, TWO_SIDED = 16, 8, 4
function Map.scene(m: MapData, tex: Textures, name: string?): Types.Scene
local batches: { [string]: Batch } = {}
local order: { string } = {}
local function batch(kind: string, tname: string): Batch
local key = kind .. ":" .. tname
local b = batches[key]
if not b then
b = { kind = kind, name = tname, pos = {}, nrm = {}, uv = {} }
batches[key] = b
table.insert(order, key)
end
return b
end
local function vert(b: Batch, x: number, y: number, z: number, nx: number, ny: number, nz: number, u: number, v: number)
local p, n, t = b.pos, b.nrm, b.uv
p[#p + 1], p[#p + 2], p[#p + 3] = x, y, z
n[#n + 1], n[#n + 2], n[#n + 3] = nx, ny, nz
t[#t + 1], t[#t + 2] = u, v
end
local V = m.vertices
local nv = #V // 2
local secLoops = loops(m)
for sec, ls in secLoops do
local s = m.sectors[sec]
local flat: { { number } } = {}
for li, loop in ls do
local f: { number } = {}
for _, vi in loop do
table.insert(f, V[vi * 2 - 1])
table.insert(f, V[vi * 2])
end
flat[li] = f
end
local tris = Polygon.triangulate(flat)
for pass = 1, 2 do
local tname = if pass == 1 then s.floorFlat else s.ceilingFlat
local fw, fh = tex.size("flat", tname)
local w, h = fw or 64, fh or 64
local z = if pass == 1 then s.floor else s.ceiling
local b = batch("flat", tname)
local nz = if pass == 1 then 1 else -1
for k = 1, #tris, 6 do
for c = 0, 2 do
local cc = if pass == 1 then c else 2 - c
local loop = ls[tris[k + cc * 2]]
local vi = loop[tris[k + cc * 2 + 1]]
local x, y = V[vi * 2 - 1], V[vi * 2]
vert(b, x, y, z, 0, 0, nz, x / w, -y / h)
end
end
end
end
local function quad(tname: string, x1: number, y1: number, x2: number, y2: number, zb: number, zt: number, xoff: number, len: number, texTop: number, yoff: number)
if tname == "-" or tname == "" or zt <= zb then
return
end
local tw, th = tex.size("texture", tname)
local w, h = tw or 64, th or 128
local b = batch("texture", tname)
local dx, dy = x2 - x1, y2 - y1
local l = math.sqrt(dx * dx + dy * dy)
local nx, ny = dy / l, -dx / l
local u0, u1 = xoff / w, (xoff + len) / w
local vb, vt = (texTop - zb + yoff) / h, (texTop - zt + yoff) / h
vert(b, x1, y1, zb, nx, ny, 0, u0, vb)
vert(b, x2, y2, zb, nx, ny, 0, u1, vb)
vert(b, x2, y2, zt, nx, ny, 0, u1, vt)
vert(b, x1, y1, zb, nx, ny, 0, u0, vb)
vert(b, x2, y2, zt, nx, ny, 0, u1, vt)
vert(b, x1, y1, zt, nx, ny, 0, u0, vt)
end
for _, ld in m.linedefs do
if ld.v1 >= 1 and ld.v1 <= nv and ld.v2 >= 1 and ld.v2 <= nv and ld.v1 ~= ld.v2 then
local ax, ay, bx, by = V[ld.v1 * 2 - 1], V[ld.v1 * 2], V[ld.v2 * 2 - 1], V[ld.v2 * 2]
local len = math.sqrt((bx - ax) ^ 2 + (by - ay) ^ 2)
for sideN = 1, 2 do
local sdi = if sideN == 1 then ld.front else ld.back
local odi = if sideN == 1 then ld.back else ld.front
local sd = sdi and m.sidedefs[sdi]
local me = sd and m.sectors[sd.sector]
if sd and me then
local x1, y1, x2, y2 = ax, ay, bx, by
if sideN == 2 then
x1, y1, x2, y2 = bx, by, ax, ay
end
local od = odi and m.sidedefs[odi]
local other = od and m.sectors[od.sector]
if not other then
local th = select(2, tex.size("texture", sd.middle)) or 128
local top = if bit32.band(ld.flags, LOWER_UNPEGGED) ~= 0 then me.floor + th else me.ceiling
quad(sd.middle, x1, y1, x2, y2, me.floor, me.ceiling, sd.xoff, len, top, sd.yoff)
else
if other.floor > me.floor then
local top = if bit32.band(ld.flags, LOWER_UNPEGGED) ~= 0 then me.ceiling else other.floor
quad(sd.lower, x1, y1, x2, y2, me.floor, other.floor, sd.xoff, len, top, sd.yoff)
end
local sky = string.sub(me.ceilingFlat, 1, 5) == "F_SKY" and string.sub(other.ceilingFlat, 1, 5) == "F_SKY"
if other.ceiling < me.ceiling and not sky then
local th = select(2, tex.size("texture", sd.upper)) or 128
local top = if bit32.band(ld.flags, UPPER_UNPEGGED) ~= 0 then me.ceiling else other.ceiling + th
quad(sd.upper, x1, y1, x2, y2, other.ceiling, me.ceiling, sd.xoff, len, top, sd.yoff)
end
if sd.middle ~= "-" and sd.middle ~= "" and bit32.band(ld.flags, TWO_SIDED) ~= 0 then
local th = select(2, tex.size("texture", sd.middle)) or 128
local lo, hi = math.max(me.floor, other.floor), math.min(me.ceiling, other.ceiling)
local top = if bit32.band(ld.flags, LOWER_UNPEGGED) ~= 0 then lo + th else hi
quad(sd.middle, x1, y1, x2, y2, math.max(lo, top + sd.yoff - th), math.min(hi, top + sd.yoff), sd.xoff, len, top, sd.yoff)
end
end
end
end
end
end
local scene = Scene.new()
scene.meta.up = "z"
local mesh: Types.Mesh = { name = name, primitives = {} }
local texIndex: { [string]: number } = {}
for _, key in order do
local b = batches[key]
local n = #b.pos // 3
if n > 0 then
local pb, nb, ub, ib = buffer.create(n * 12), buffer.create(n * 12), buffer.create(n * 8), buffer.create(n * 4)
for i = 1, n * 3 do
writef32(pb, (i - 1) * 4, b.pos[i])
writef32(nb, (i - 1) * 4, b.nrm[i])
end
for i = 1, n * 2 do
writef32(ub, (i - 1) * 4, b.uv[i])
end
for i = 0, n - 1 do
writeu32(ib, i * 4, i)
end
local ti = texIndex[key]
if not ti then
table.insert(scene.textures, { name = b.name, uri = b.name, image = if tex.image then tex.image(b.kind, b.name) else nil, extras = { wad = { kind = b.kind } } })
ti = #scene.textures
texIndex[key] = ti
end
local mat = Scene.material(b.name)
mat.metallic = 0
mat.baseColorTexture = { texture = ti }
mat.extras = { wad = { kind = b.kind } }
table.insert(scene.materials, mat)
table.insert(mesh.primitives, {
attributes = {
POSITION = Scene.accessor(pb, "f32", 3, n),
NORMAL = Scene.accessor(nb, "f32", 3, n),
TEXCOORD_0 = Scene.accessor(ub, "f32", 2, n),
},
indices = Scene.accessor(ib, "u32", 1, n),
mode = "triangles" :: any,
material = #scene.materials,
extras = { wad = { kind = b.kind, texture = b.name } },
})
end
end
table.insert(scene.meshes, mesh)
scene.nodes[1] = { name = name, children = {}, mesh = 1 }
scene.roots[1] = 1
for i, th in m.things do
local z = th.z or 0
for sec, ls in secLoops do
local inside = false
for _, loop in ls do
if insideLoop(V, loop, th.x, th.y) then
inside = not inside
end
end
if inside then
z += m.sectors[sec].floor
break
end
end
local a = math.rad(th.angle) / 2
table.insert(scene.nodes, {
name = "thing " .. i,
children = {},
translation = { th.x, th.y, z },
rotation = { 0, 0, math.sin(a), math.cos(a) },
extras = { thing = { type = th.type, angle = th.angle, flags = th.flags, tid = th.tid, special = th.special, args = th.args } },
})
table.insert(scene.nodes[1].children, #scene.nodes)
end
return scene
end
return Map
+14 -10
View File
@@ -1,7 +1,7 @@
--!strict
-- archive/wad: Doom-engine WADs (IWAD / PWAD; Unofficial Doom Specs, w_wad.c / r_data.c; no code taken), plus lumps,
-- palettes, pictures, wall textures and maps. Entry names: "<map>/<lump>", repeats "<name>~<n>". Not supported: UDMF
-- geometry (lumps listed), Strife's TEXTURE variant, big-endian (Jaguar) WADs, compressed Jaguar lumps.
-- palettes, pictures, wall textures and maps (binary and UDMF TEXTMAP). Entry names: "<map>/<lump>", repeats
-- "<name>~<n>". Not supported: Strife's TEXTURE variant, big-endian (Jaguar) WADs, compressed Jaguar lumps.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -10,7 +10,7 @@ local Types = require("@rosetta/Types")
local Archive = require("@rosetta/containers/Archive")
local Pixels = require("@rosetta/codecs/image/Pixels")
local Picture = require("@rosetta/archive/wad/Picture")
local Map = require("@rosetta/archive/wad/Map")
local Map = require("@rosetta/codecs/model/DoomMap")
-- ------------------------------------------------------------------------------------------------ contract
local Wad = {
@@ -18,7 +18,7 @@ local Wad = {
category = "archive",
extensions = { "wad" },
mime = { "application/x-doom" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Archive", "codecs/image/Pixels", "codecs/model/Polygon" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Archive", "codecs/image/Pixels", "codecs/model/DoomMap" },
caps = { decode = true, encode = true, images = true, maps = true, variants = { "iwad", "pwad" } },
}
@@ -38,6 +38,7 @@ export type Lump = {
export type MapOptions = {
textures: boolean?, -- default true
palette: number?, -- default 0
maxBlocks: number?, -- UDMF, default 2^22
}
-- ------------------------------------------------------------------------------------------------ constants
@@ -377,7 +378,7 @@ function Wad.maps(a: any): { string }
return out
end
function Wad.mapData(a: any, name: string): (Map.MapData?, Err.Err?)
function Wad.mapData(a: any, name: string, opts: MapOptions?): (Map.MapData?, Err.Err?)
local w = info(a)
if not w then
return nil, Err.new(Err.ARG, "expected a decoded WAD archive")
@@ -392,10 +393,13 @@ function Wad.mapData(a: any, name: string): (Map.MapData?, Err.Err?)
if not next(lumps) then
return nil, Err.new(Err.ARG, "no map named " .. tostring(name))
end
if lumps.TEXTMAP then
return nil, Err.new(Err.UNSUPPORTED, "UDMF (TEXTMAP) maps")
local m, msg
local tm = lumps.TEXTMAP
if tm then
m, msg = Map.udmf(buffer.readstring(tm.buf, tm.at, tm.size), opts and opts.maxBlocks)
else
m, msg = Map.parse(lumps, lumps.BEHAVIOR ~= nil)
end
local m, msg = Map.parse(lumps, lumps.BEHAVIOR ~= nil)
if not m then
return nil, Err.from(msg)
end
@@ -403,7 +407,7 @@ function Wad.mapData(a: any, name: string): (Map.MapData?, Err.Err?)
end
function Wad.map(a: any, name: string, opts: MapOptions?): (Types.Scene?, Err.Err?)
local m, err = Wad.mapData(a, name)
local m, err = Wad.mapData(a, name, opts)
if not m then
return nil, err
end
@@ -441,7 +445,7 @@ function Wad.map(a: any, name: string, opts: MapOptions?): (Types.Scene?, Err.Er
image = if images then image else nil,
}
local scene = Map.scene(m, tex, string.upper(name))
scene.meta.source = { format = "wad", map = string.upper(name), mapFormat = m.format, things = #m.things, linedefs = #m.linedefs, sectors = #m.sectors, vertices = #m.vertices // 2 }
scene.meta.source = { format = "wad", map = string.upper(name), mapFormat = m.format, namespace = m.namespace, things = #m.things, linedefs = #m.linedefs, sectors = #m.sectors, vertices = #m.vertices // 2 }
return scene, nil
end