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

+248
View File
@@ -0,0 +1,248 @@
--!strict
-- codecs/model/DoomMap/Udmf: UDMF 1.1 TEXTMAP text (zdoom.org specs/udmf.txt; no code taken) to the DoomMap tables.
-- Every block keeps its fields (lower-case keys, namespace and user_ keys included); unknown blocks and global
-- assignments are kept as data. Flags are rebuilt in the binary layout of the namespace (Doom or Hexen bits).
local Lexer = require("@rosetta/codecs/text/Lexer")
local Udmf = {}
export type Block = { type: string, fields: { [string]: any } }
export type Document = { namespace: string?, globals: { [string]: any }, blocks: { Block } }
-- ------------------------------------------------------------------------------------------------ parse
local function value(L: Lexer.Lexer): (any, string?)
local k, v = L:next()
if k == "number" then
local raw = L.raw :: string
-- integer := ... | 0[0-9]+ (octal) | 0x[0-9A-Fa-f]+
if string.match(raw, "^[+-]?0[0-7]+$") then
local neg = string.sub(raw, 1, 1) == "-"
local o = tonumber((string.gsub(raw, "^[+-]?0", "")), 8) :: number
return if neg then -o else o, nil
end
return v, nil
elseif k == "string" then
return v, nil
elseif k == "word" then
local w = string.lower(v)
if w == "true" then
return true, nil
elseif w == "false" then
return false, nil
end
return v, nil
end
return nil, L.err or "expected a value"
end
function Udmf.parse(text: string, maxBlocks: number?): (Document?, string?)
local L = Lexer.new(text)
local doc: Document = { namespace = nil, globals = {}, blocks = {} }
local limit = maxBlocks or 2 ^ 22
while true do
local k, id = L:next()
if k == nil then
if L.err then
return nil, L.err .. " at line " .. L:line()
end
break
end
if k ~= "word" then
return nil, "expected an identifier at line " .. L:line()
end
local name = string.lower(id)
local pk, pv = L:next()
if pk == "punct" and pv == "=" then
local v, err = value(L)
if err then
return nil, err .. " at line " .. L:line()
end
local sk, sv = L:next()
if sk ~= "punct" or sv ~= ";" then
return nil, "expected ; at line " .. L:line()
end
doc.globals[name] = v
if name == "namespace" and type(v) == "string" then
doc.namespace = v
end
elseif pk == "punct" and pv == "{" then
if #doc.blocks >= limit then
return nil, "limit: more than " .. limit .. " blocks"
end
local fields: { [string]: any } = {}
while true do
local fk, fv = L:next()
if fk == "punct" and fv == "}" then
break
end
if fk ~= "word" then
return nil, (L.err or "expected a field name") .. " at line " .. L:line()
end
local ek, ev = L:next()
if ek ~= "punct" or ev ~= "=" then
return nil, "expected = at line " .. L:line()
end
local v, err = value(L)
if err then
return nil, err .. " at line " .. L:line()
end
local sk, sv = L:next()
if sk ~= "punct" or sv ~= ";" then
return nil, "expected ; at line " .. L:line()
end
fields[string.lower(fv)] = v
end
table.insert(doc.blocks, { type = name, fields = fields })
else
return nil, "expected = or { at line " .. L:line()
end
end
return doc, nil
end
-- ------------------------------------------------------------------------------------------------ map
local LINE_FLAGS: { [string]: number } = { blocking = 1, blockmonsters = 2, twosided = 4, dontpegtop = 8, dontpegbottom = 16, secret = 32, blocksound = 64, dontdraw = 128, mapped = 256 }
-- Hexen SPAC activation (bits 10..12) and repeat (bit 9)
local SPAC: { [string]: number } = { playercross = 0, playeruse = 1, monstercross = 2, impact = 3, playerpush = 4, missilecross = 5 }
local function num(f: { [string]: any }, k: string, default: number): number
local v = f[k]
return if type(v) == "number" then v else default
end
local function flag(f: { [string]: any }, k: string): boolean
return f[k] == true
end
local function tex(f: { [string]: any }, k: string, default: string): string
local v = f[k]
return if type(v) == "string" then string.upper(v) else default
end
local function args(f: { [string]: any }): { number }
return { num(f, "arg0", 0), num(f, "arg1", 0), num(f, "arg2", 0), num(f, "arg3", 0), num(f, "arg4", 0) }
end
local function index(f: { [string]: any }, k: string): number?
local v = f[k]
return if type(v) == "number" and v >= 0 and v % 1 == 0 then v + 1 else nil
end
function Udmf.map(doc: Document): (any, string?)
local ns = string.lower(doc.namespace or "")
local hexenBits = ns ~= "doom" and ns ~= "heretic" and ns ~= "strife"
local m = { format = "udmf", namespace = doc.namespace, globals = doc.globals, things = {}, linedefs = {}, sidedefs = {}, vertices = {}, sectors = {}, blocks = {} }
for _, b in doc.blocks do
local f = b.fields
if b.type == "vertex" then
local x, y = f.x, f.y
if type(x) ~= "number" or type(y) ~= "number" then
return nil, "vertex " .. (#m.vertices // 2) .. " has no x / y"
end
table.insert(m.vertices, x)
table.insert(m.vertices, y)
elseif b.type == "linedef" then
local flags = 0
for k, bit in LINE_FLAGS do
if flag(f, k) then
flags += bit
end
end
if hexenBits then
if flag(f, "repeatspecial") then
flags += 512
end
for k, a in SPAC do
if flag(f, k) then
flags = bit32.bor(bit32.band(flags, bit32.bnot(0x1C00)), a * 1024)
end
end
end
local v1, v2 = index(f, "v1"), index(f, "v2")
if not v1 or not v2 then
return nil, "linedef " .. #m.linedefs .. " has no v1 / v2"
end
local a = args(f)
table.insert(m.linedefs, {
v1 = v1,
v2 = v2,
flags = flags,
special = num(f, "special", 0),
tag = if hexenBits then num(f, "id", -1) else a[1],
args = a,
front = index(f, "sidefront"),
back = index(f, "sideback"),
fields = f,
})
elseif b.type == "sidedef" then
local s = index(f, "sector")
if not s then
return nil, "sidedef " .. #m.sidedefs .. " has no sector"
end
table.insert(m.sidedefs, {
xoff = num(f, "offsetx", 0),
yoff = num(f, "offsety", 0),
upper = tex(f, "texturetop", "-"),
lower = tex(f, "texturebottom", "-"),
middle = tex(f, "texturemiddle", "-"),
sector = s,
fields = f,
})
elseif b.type == "sector" then
table.insert(m.sectors, {
floor = num(f, "heightfloor", 0),
ceiling = num(f, "heightceiling", 0),
floorFlat = tex(f, "texturefloor", "-"),
ceilingFlat = tex(f, "textureceiling", "-"),
light = num(f, "lightlevel", 160),
special = num(f, "special", 0),
tag = num(f, "id", 0),
fields = f,
})
elseif b.type == "thing" then
local x, y = f.x, f.y
if type(x) ~= "number" or type(y) ~= "number" then
return nil, "thing " .. #m.things .. " has no x / y"
end
local fl = 0
if flag(f, "skill1") or flag(f, "skill2") then
fl += 1
end
if flag(f, "skill3") then
fl += 2
end
if flag(f, "skill4") or flag(f, "skill5") then
fl += 4
end
if flag(f, "ambush") then
fl += 8
end
if hexenBits then
fl += (if flag(f, "dormant") then 16 else 0) + (if flag(f, "class1") then 32 else 0) + (if flag(f, "class2") then 64 else 0)
+ (if flag(f, "class3") then 128 else 0) + (if flag(f, "single") then 256 else 0) + (if flag(f, "coop") then 512 else 0)
+ (if flag(f, "dm") then 1024 else 0)
else
-- Doom / Boom / MBF: 16 not in single player, 32 not in deathmatch, 64 not in coop, 128 friend
fl += (if flag(f, "single") then 0 else 16) + (if flag(f, "dm") then 0 else 32) + (if flag(f, "coop") then 0 else 64)
+ (if flag(f, "friend") then 128 else 0)
end
table.insert(m.things, {
x = x,
y = y,
z = num(f, "height", 0),
angle = num(f, "angle", 0),
type = num(f, "type", 0),
flags = fl,
tid = num(f, "id", 0),
special = num(f, "special", 0),
args = args(f),
fields = f,
})
else
table.insert(m.blocks, b)
end
end
return m, nil
end
return Udmf
+443
View File
@@ -0,0 +1,443 @@
--!strict
-- codecs/model/DoomMap: Doom / Heretic / Hexen binary map lumps (Hexen when BEHAVIOR is present) and UDMF TEXTMAP to
-- tables and a Scene, with vanilla texture alignment (offsets, unpegged) and the sky hack; z up, map units. From the
-- Unofficial Doom Specs, the Hexen specs and UDMF 1.1; no code taken. Not supported: UDMF slopes / per-part offsets.
local Types = require("@rosetta/Types")
local Polygon = require("@rosetta/codecs/model/Polygon")
local Udmf = require("@rosetta/codecs/model/DoomMap/Udmf")
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 Fields = { [string]: any } -- UDMF: the block's fields as written (lower-case keys)
export type Thing = { x: number, y: number, z: number?, angle: number, type: number, flags: number, tid: number?, special: number?, args: { number }?, fields: Fields? }
export type Linedef = { v1: number, v2: number, flags: number, special: number, tag: number?, args: { number }?, front: number?, back: number?, fields: Fields? }
export type Sidedef = { xoff: number, yoff: number, upper: string, lower: string, middle: string, sector: number, fields: Fields? }
export type Sector = { floor: number, ceiling: number, floorFlat: string, ceilingFlat: string, light: number, special: number, tag: number, fields: Fields? }
export type MapData = {
format: "doom" | "hexen" | "udmf",
namespace: string?, -- UDMF
globals: Fields?, -- UDMF global assignments
blocks: { Udmf.Block }?, -- UDMF blocks other than vertex / linedef / sidedef / sector / thing
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
function Map.udmf(text: string, maxBlocks: number?): (MapData?, string?)
local doc, err = Udmf.parse(text, maxBlocks)
if not doc then
return nil, err
end
return Udmf.map(doc)
end
Map.parseUdmf = Udmf.parse
-- ------------------------------------------------------------------------------------------------ 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
+130
View File
@@ -0,0 +1,130 @@
--!strict
-- codecs/text/Lexer: C-style tokens for id Tech / UDMF text (idLib idLexer conventions, UDMF 1.1 §I; no code taken):
-- // and /* */ comments, "strings" with backslash escapes, numbers (decimal, float, 0x hex), bare words, and single
-- punctuation characters { } ( ) [ ] ; = ,.
local Lexer = {}
export type Kind = "word" | "number" | "string" | "punct"
export type Lexer = {
s: string,
p: number, -- next byte to read, 1-based
n: number,
raw: string?, -- the last token's text as written
err: string?,
next: (self: Lexer) -> (Kind?, any),
peek: (self: Lexer) -> (Kind?, any),
line: (self: Lexer, at: number?) -> number,
}
local find, sub, byte, match = string.find, string.sub, string.byte, string.match
local ESC: { [string]: string } = { n = "\n", t = "\t", r = "\r", ["0"] = "\0" }
local function skip(L: Lexer): number?
local s, p = L.s, L.p
while true do
p = find(s, "[^ \t\r\n\f\v]", p)
if not p then
return nil
end
if byte(s, p) == 47 then
local c2 = byte(s, p + 1)
if c2 == 47 then
local e = find(s, "\n", p + 2, true)
if not e then
return nil
end
p = e + 1
elseif c2 == 42 then
local _, e = find(s, "*/", p + 2, true)
if not e then
L.err = "unterminated comment"
return nil
end
p = e + 1
else
return p
end
else
return p
end
end
end
local function token(L: Lexer): (Kind?, any, number)
local p = skip(L)
if not p then
return nil, nil, L.n + 1
end
local s = L.s
local c = byte(s, p)
if c == 34 then
local parts: { string }? = nil
local q = p + 1
while true do
local e = find(s, "[\"\\]", q)
if not e then
L.err = "unterminated string"
return nil, nil, L.n + 1
end
if byte(s, e) == 34 then
local tail = sub(s, q, e - 1)
if parts then
table.insert(parts, tail)
L.raw = sub(s, p, e)
return "string", table.concat(parts), e + 1
end
L.raw = sub(s, p, e)
return "string", tail, e + 1
end
parts = parts or {}
local ch = sub(s, e + 1, e + 1)
table.insert(parts :: { string }, sub(s, q, e - 1))
table.insert(parts :: { string }, ESC[ch] or ch)
q = e + 2
end
end
if find("{}()[];=,", string.char(c), 1, true) then
L.raw = string.char(c)
return "punct", L.raw, p + 1
end
local w = match(s, "^[^ \t\r\n\f\v{}()%[%];=,\"]+", p) :: string
local e = p + #w
-- a comment right after a word ends it
local cm = find(w, "/[/*]")
if cm then
w = sub(w, 1, cm - 1)
e = p + #w
end
L.raw = w
if (c >= 48 and c <= 57) or c == 45 or c == 43 or c == 46 then
local v = tonumber(w)
if v and not find(w, "[iInN]") then
return "number", v, e
end
end
return "word", w, e
end
local function nextToken(L: Lexer): (Kind?, any)
local k, v, e = token(L)
L.p = e
return k, v
end
local function peek(L: Lexer): (Kind?, any)
local raw = L.raw
local k, v = token(L)
L.raw = raw
return k, v
end
local function line(L: Lexer, at: number?): number
local _, n = string.gsub(sub(L.s, 1, (at or L.p) - 1), "\n", "")
return n + 1
end
function Lexer.new(s: string): Lexer
return { s = s, p = 1, n = #s, raw = nil, err = nil, next = nextToken, peek = peek, line = line }
end
return Lexer