197 lines
5.1 KiB
Luau
197 lines
5.1 KiB
Luau
--!strict
|
|
-- model/d3map/Parse: Doom 3 / Quake 4 editor .map text ("Version 2" / "Version 3"; idMapFile / idMapEntity /
|
|
-- idMapBrush / idMapPatch layouts of the Doom 3 GPL source docs, no code taken) to entities with brushDef3 sides
|
|
-- (plane a b c d with a x + b y + c z + d = 0, normal out of the brush; 2 x 3 texture matrix) and patchDef2 / 3.
|
|
local Lexer = require("@rosetta/codecs/text/Lexer")
|
|
|
|
local Parse = {}
|
|
|
|
export type Side = { plane: { number }, matrix: { { number } }, material: string, flags: { number } }
|
|
export type Prim = {
|
|
type: string, -- "brushDef3" | "patchDef2" | "patchDef3"
|
|
sides: { Side }?,
|
|
material: string?,
|
|
info: { number }?, -- patchDef2: width height 0 0 0; patchDef3: width height subdivX subdivY 0 0 0
|
|
width: number?,
|
|
height: number?,
|
|
points: { number }?, -- x y z s t per control point, in file order (column by column)
|
|
}
|
|
export type Entity = { keys: { [string]: string }, prims: { Prim }, skipped: number }
|
|
export type Map = { version: number, entities: { Entity } }
|
|
|
|
type P = { L: Lexer.Lexer, err: string? }
|
|
|
|
local function fail(p: P, msg: string)
|
|
if not p.err then
|
|
p.err = (p.L.err or msg) .. " at line " .. p.L:line()
|
|
end
|
|
end
|
|
|
|
local function want(p: P, c: string)
|
|
local k, v = p.L:next()
|
|
if k ~= "punct" or v ~= c then
|
|
fail(p, "expected " .. c)
|
|
end
|
|
end
|
|
|
|
local function number(p: P): number
|
|
local k, v = p.L:next()
|
|
if k ~= "number" then
|
|
fail(p, "expected a number")
|
|
return 0
|
|
end
|
|
return v
|
|
end
|
|
|
|
local function tuple(p: P, n: number): { number }
|
|
want(p, "(")
|
|
local out = table.create(n, 0)
|
|
for i = 1, n do
|
|
out[i] = number(p)
|
|
end
|
|
want(p, ")")
|
|
return out
|
|
end
|
|
|
|
local function name(p: P): string
|
|
local k, v = p.L:next()
|
|
if k == "string" or k == "word" then
|
|
return v
|
|
end
|
|
fail(p, "expected a material name")
|
|
return ""
|
|
end
|
|
|
|
local function brushDef3(p: P): Prim
|
|
want(p, "{")
|
|
local sides: { Side } = {}
|
|
while not p.err do
|
|
local k, v = p.L:peek()
|
|
if k == "punct" and v == "}" then
|
|
p.L:next()
|
|
break
|
|
end
|
|
local plane = tuple(p, 4)
|
|
want(p, "(")
|
|
local m1 = tuple(p, 3)
|
|
local m2 = tuple(p, 3)
|
|
want(p, ")")
|
|
local material = name(p)
|
|
local flags: { number } = {}
|
|
while not p.err do
|
|
local fk = p.L:peek()
|
|
if fk ~= "number" then
|
|
break
|
|
end
|
|
table.insert(flags, number(p))
|
|
end
|
|
table.insert(sides, { plane = plane, matrix = { m1, m2 }, material = material, flags = flags })
|
|
end
|
|
return { type = "brushDef3", sides = sides }
|
|
end
|
|
|
|
local function patchDef(p: P, kind: string, maxPoints: number): Prim
|
|
want(p, "{")
|
|
local material = name(p)
|
|
local info = tuple(p, if kind == "patchDef3" then 7 else 5)
|
|
local w, h = info[1], info[2]
|
|
if p.err then
|
|
return { type = kind }
|
|
end
|
|
if w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 or w * h > maxPoints then
|
|
fail(p, "limit: patch size " .. w .. " x " .. h)
|
|
return { type = kind }
|
|
end
|
|
local pts: { number } = table.create(w * h * 5, 0)
|
|
want(p, "(")
|
|
for i = 0, w - 1 do
|
|
want(p, "(")
|
|
for j = 0, h - 1 do
|
|
local v = tuple(p, 5)
|
|
if p.err then
|
|
return { type = kind }
|
|
end
|
|
table.move(v, 1, 5, (i * h + j) * 5 + 1, pts)
|
|
end
|
|
want(p, ")")
|
|
end
|
|
want(p, ")")
|
|
want(p, "}")
|
|
return { type = kind, material = material, info = info, width = w, height = h, points = pts }
|
|
end
|
|
|
|
function Parse.parse(text: string, maxPrims: number?): (Map?, string?)
|
|
local p: P = { L = Lexer.new(text), err = nil }
|
|
local k, v = p.L:next()
|
|
if k ~= "word" or v ~= "Version" then
|
|
return nil, "missing the Version line"
|
|
end
|
|
local version = number(p)
|
|
local m: Map = { version = version, entities = {} }
|
|
local limit = maxPrims or 2 ^ 20
|
|
local count = 0
|
|
while not p.err do
|
|
local ek, ev = p.L:next()
|
|
if not ek then
|
|
if p.L.err then
|
|
fail(p, p.L.err)
|
|
end
|
|
break
|
|
end
|
|
if ek ~= "punct" or ev ~= "{" then
|
|
fail(p, "expected an entity")
|
|
break
|
|
end
|
|
local ent: Entity = { keys = {}, prims = {}, skipped = 0 }
|
|
while not p.err do
|
|
local tk, tv = p.L:next()
|
|
if tk == "string" then
|
|
local vk, vv = p.L:next()
|
|
if vk ~= "string" then
|
|
fail(p, "expected a key value")
|
|
break
|
|
end
|
|
ent.keys[tv] = vv
|
|
elseif tk == "punct" and tv == "{" then
|
|
count += 1
|
|
if count > limit then
|
|
p.err = "limit: more than " .. limit .. " primitives"
|
|
break
|
|
end
|
|
local pk, pv = p.L:peek()
|
|
if pk == "word" and (pv == "brushDef3" or pv == "patchDef2" or pv == "patchDef3") then
|
|
p.L:next()
|
|
local prim = if pv == "brushDef3" then brushDef3(p) else patchDef(p, pv, 2 ^ 16)
|
|
want(p, "}")
|
|
table.insert(ent.prims, prim)
|
|
else
|
|
-- brushDef (Quake III brush primitives) and Version 1 brushes are not read
|
|
ent.skipped += 1
|
|
local depth = 1
|
|
while not p.err and depth > 0 do
|
|
local sk, sv = p.L:next()
|
|
if not sk then
|
|
fail(p, "unterminated primitive")
|
|
elseif sk == "punct" and sv == "{" then
|
|
depth += 1
|
|
elseif sk == "punct" and sv == "}" then
|
|
depth -= 1
|
|
end
|
|
end
|
|
end
|
|
elseif tk == "punct" and tv == "}" then
|
|
break
|
|
else
|
|
fail(p, if tk then "unexpected " .. tostring(tv) else "unterminated entity")
|
|
end
|
|
end
|
|
table.insert(m.entities, ent)
|
|
end
|
|
if p.err then
|
|
return nil, p.err
|
|
end
|
|
return m, nil
|
|
end
|
|
|
|
return Parse
|