--!strict -- model/map/Parse: .map text -> entities (key / value pairs in order, brushes and patches as written). Grammar of the -- Quake / Quake II / Half-Life (Valve 220) / Quake III editors (qbsp / q3map2 map parsers; no code taken). local Types = require("@rosetta/Types") local Parse = {} export type Face = { points: { number }, -- 3 points: x, y, z each texture: string, offset: { number }?, -- Quake standard: x, y offset rotation: number?, -- standard / Valve 220 (degrees) scale: { number }?, -- standard / Valve 220 u: { number }?, -- Valve 220: axis x, y, z, offset v: { number }?, matrix: { { number } }?, -- Quake III brush primitives: 2 x 3 flags: { number }?, -- trailing numbers: Quake II / III contents, surface flags, value (Hexen II: light) } export type Brush = { type: string, -- "brush" | "brushDef" | "patchDef2" | "patchDef3" faces: { Face }?, texture: string?, -- patches size: { number }?, -- patches: width, height (columns, points per column as written) params: { number }?, -- patches: the header numbers after width, height points: { number }?, -- patches: 5 numbers (x, y, z, s, t) per control point, in file order (column by column) } export type Entity = { keys: { [string]: string }, brushes: { Brush } } -- keys: an ordered object (Types.object) export type Map = { entities: { Entity }, game: string?, mapFormat: string?, comments: { string } } type St = { s: string, pos: number, n: number, comments: { string } } local function skip(st: St) local s = st.s while true do local p = string.find(s, "[^%s]", st.pos) if not p then st.pos = st.n + 1 return end if string.sub(s, p, p + 1) == "//" then local e = string.find(s, "\n", p, true) or st.n table.insert(st.comments, (string.gsub(string.sub(s, p + 2, e), "^%s*(.-)%s*$", "%1"))) st.pos = e + 1 else st.pos = p return end end end local function peek(st: St): string skip(st) return string.sub(st.s, st.pos, st.pos) end local function expect(st: St, c: string): string? if peek(st) ~= c then return "expected '" .. c .. "' at " .. (st.pos - 1) end st.pos += 1 return nil end local function word(st: St): string? skip(st) local a, b = string.find(st.s, "^[^%s]+", st.pos) if not a then return nil end st.pos = (b :: number) + 1 return string.sub(st.s, a, b) end local function number(st: St): number? skip(st) local a, b = string.find(st.s, "^[^%s%(%)%[%]{}]+", st.pos) if not a then return nil end local v = tonumber(string.sub(st.s, a, b)) if v then st.pos = (b :: number) + 1 end return v end local function quoted(st: St): string? if peek(st) ~= '"' then return nil end local e = string.find(st.s, '"', st.pos + 1, true) if not e then return nil end local v = string.sub(st.s, st.pos + 1, e - 1) st.pos = e + 1 return v end local function numbers(st: St, n: number, out: { number }): string? for _ = 1, n do local v = number(st) if not v then return "expected a number at " .. (st.pos - 1) end table.insert(out, v) end return nil end local function vec(st: St, n: number, open: string, close: string): ({ number }?, string?) local e = expect(st, open) if e then return nil, e end local out = {} e = numbers(st, n, out) if e then return nil, e end e = expect(st, close) if e then return nil, e end return out, nil end local function trailing(st: St): { number }? local out: { number } = {} while true do local c = peek(st) if not string.match(c, "[%d%-%+%.]") then break end local v = number(st) if not v then break end table.insert(out, v) end return if #out > 0 then out else nil end local function plane(st: St): ({ number }?, string?) local pts: { number } = {} for _ = 1, 3 do local p, e = vec(st, 3, "(", ")") if not p then return nil, e end table.move(p, 1, 3, #pts + 1, pts) end return pts, nil end local function face(st: St, bp: boolean): (Face?, string?) local pts, e = plane(st) if not pts then return nil, e end local f: Face = { points = pts, texture = "" } if bp then e = expect(st, "(") if e then return nil, e end local r1, e1 = vec(st, 3, "(", ")") if not r1 then return nil, e1 end local r2, e2 = vec(st, 3, "(", ")") if not r2 then return nil, e2 end e = expect(st, ")") if e then return nil, e end f.matrix = { r1, r2 } end local tex = word(st) if not tex then return nil, "expected a texture name at " .. (st.pos - 1) end f.texture = tex if not bp then if peek(st) == "[" then local u, eu = vec(st, 4, "[", "]") if not u then return nil, eu end local v, ev = vec(st, 4, "[", "]") if not v then return nil, ev end local rest = {} e = numbers(st, 3, rest) if e then return nil, e end f.u, f.v, f.rotation, f.scale = u, v, rest[1], { rest[2], rest[3] } else local rest = {} e = numbers(st, 5, rest) if e then return nil, e end f.offset, f.rotation, f.scale = { rest[1], rest[2] }, rest[3], { rest[4], rest[5] } end end f.flags = trailing(st) return f, nil end local function patch(st: St, kind: string, maxPoints: number): (Brush?, string?) local e = expect(st, "{") if e then return nil, e end local tex = word(st) if not tex then return nil, "expected a patch texture" end local hn = if kind == "patchDef3" then 7 else 5 local head, eh = vec(st, hn, "(", ")") if not head then return nil, eh end local w, h = head[1], head[2] if w ~= math.floor(w) or h ~= math.floor(h) or w < 1 or h < 1 or w * h > maxPoints then return nil, "bad patch size " .. w .. " x " .. h end local pts: { number } = {} e = expect(st, "(") if e then return nil, e end for _ = 1, w do e = expect(st, "(") if e then return nil, e end for _ = 1, h do local p, ep = vec(st, 5, "(", ")") if not p then return nil, ep end table.move(p, 1, 5, #pts + 1, pts) end e = expect(st, ")") if e then return nil, e end end e = expect(st, ")") if e then return nil, e end e = expect(st, "}") if e then return nil, e end return { type = kind, texture = tex, size = { w, h }, params = { table.unpack(head, 3) }, points = pts }, nil end local function brush(st: St, maxPoints: number): (Brush?, string?) local c = peek(st) if c == "(" then local faces: { Face } = {} while peek(st) == "(" do local f, e = face(st, false) if not f then return nil, e end table.insert(faces, f) end local e = expect(st, "}") if e then return nil, e end return { type = "brush", faces = faces }, nil end if c == "}" then st.pos += 1 return { type = "brush", faces = {} }, nil end local kw = word(st) local out: any, err: string? if kw == "brushDef" then local e = expect(st, "{") if e then return nil, e end local faces: { Face } = {} while peek(st) == "(" do local f, ef = face(st, true) if not f then return nil, ef end table.insert(faces, f) end e = expect(st, "}") if e then return nil, e end out = { type = "brushDef", faces = faces } elseif kw == "patchDef2" or kw == "patchDef3" then out, err = patch(st, kw, maxPoints) if not out then return nil, err end elseif kw == "brushDef3" then return nil, "unsupported: Doom 3 / Quake 4 brushDef3" else return nil, "unexpected '" .. tostring(kw) .. "' in a brush at " .. (st.pos - 1) end local e = expect(st, "}") if e then return nil, e end return out, nil end function Parse.parse(s: string, maxBrushes: number?): (Map?, string?, number?) local st: St = { s = s, pos = 1, n = #s, comments = {} } if string.sub(s, 1, 3) == "\239\187\191" then st.pos = 4 end local limit = maxBrushes or 2 ^ 20 local count = 0 local ents: { Entity } = {} while peek(st) ~= "" do local at = st.pos - 1 local e = expect(st, "{") if e then return nil, e, at end local keys: { [string]: string } = {} local order: { string } = {} local brushes: { Brush } = {} while true do local c = peek(st) if c == "}" then st.pos += 1 break elseif c == '"' then local k = quoted(st) local v = quoted(st) if not k or not v then return nil, "bad key / value at " .. (st.pos - 1), st.pos - 1 end if keys[k] == nil then table.insert(order, k) end keys[k] = v elseif c == "{" then st.pos += 1 local b, eb = brush(st, limit) if not b then return nil, eb, st.pos - 1 end count += 1 if count > limit then return nil, "limit: too many brushes", st.pos - 1 end table.insert(brushes, b) elseif c == "" then return nil, "truncated entity", st.pos - 1 else return nil, "unexpected '" .. c .. "' in an entity at " .. (st.pos - 1), st.pos - 1 end end table.insert(ents, { keys = Types.object(keys, order) :: { [string]: string }, brushes = brushes }) end local game, fmt for _, c in st.comments do game = game or string.match(c, "^Game:%s*(.-)%s*$") fmt = fmt or string.match(c, "^Format:%s*(.-)%s*$") end return { entities = ents, game = game, mapFormat = fmt, comments = st.comments }, nil, nil end return Parse