--!strict -- model/bsp/Tree: the BSP tree, collision hulls and visibility of Quake 1 / BSP2 / 2PSB / Half-Life / Quake II / Quake -- III maps as tables (bspfile.h layouts; Unofficial Quake 3 Map Specs), plus point queries: the leaf and contents at a -- point (Quake 1 hulls 0..3 through nodes / clipnodes as SV_HullPointContents; Quake II leaf contents; Quake III the -- contents of the leaf's brushes) and the potentially visible set of a leaf (run-length zero bytes, as Mod_DecompressVis). local Tree = {} local readi32, readu16, readi16, readf32, readu8, readu32 = buffer.readi32, buffer.readu16, buffer.readi16, buffer.readf32, buffer.readu8, buffer.readu32 export type Lump = { at: number, size: number } export type Plane = { normal: { number }, dist: number, type: number? } export type Node = { plane: number, children: { number }, mins: { number }, maxs: { number }, firstFace: number?, numFaces: number? } -- children: >= 0 node, < 0 leaf -(child + 1) export type Leaf = { contents: number?, cluster: number?, area: number?, visofs: number?, mins: { number }, maxs: { number }, firstFace: number?, numFaces: number?, firstBrush: number?, numBrushes: number?, ambient: { number }?, } export type ClipNode = { plane: number, children: { number } } -- children: >= 0 clipnode, < 0 contents export type Model = { mins: { number }, maxs: { number }, origin: { number }?, headnodes: { number }, visleafs: number?, firstFace: number, numFaces: number } export type Brush = { firstSide: number, numSides: number, contents: number } export type Side = { plane: number, texinfo: number } export type Data = { variant: string, planes: { Plane }, nodes: { Node }, leaves: { Leaf }, clipnodes: { ClipNode }?, models: { Model }, brushes: { Brush }?, brushSides: { Side }?, leafBrushes: { number }?, leafFaces: { number }?, visibility: buffer?, -- the raw lump numClusters: number?, clusterBytes: number?, -- Quake III bytes per cluster vector } local function v3(b: buffer, o: number, kind: string): { number } if kind == "f" then return { readf32(b, o), readf32(b, o + 4), readf32(b, o + 8) } elseif kind == "i" then return { readi32(b, o), readi32(b, o + 4), readi32(b, o + 8) } end return { readi16(b, o), readi16(b, o + 2), readi16(b, o + 4) } end local function slice(b: buffer, base: number, l: Lump): buffer? if l.size <= 0 then return nil end local out = buffer.create(l.size) buffer.copy(out, 0, b, base + l.at, l.size) return out end -- nil, message past maxItems function Tree.read(b: buffer, base: number, lumps: { [string]: Lump }, variant: string, maxItems: number?): (Data?, string?) local limit = maxItems or 2 ^ 22 local q2, q3 = variant == "q2", variant == "q3" local bsp2, rmq = variant == "bsp2", variant == "2psb" local d: Data = { variant = variant, planes = {}, nodes = {}, leaves = {}, models = {} } local function count(l: Lump?, size: number): number return if l then l.size // size else 0 end local P = lumps.planes local pSize = if q3 then 16 else 20 local np = count(P, pSize) if np > limit then return nil, "limit: too many planes" end for i = 0, np - 1 do local o = base + P.at + i * pSize d.planes[i + 1] = { normal = v3(b, o, "f"), dist = readf32(b, o + 12), type = if q3 then nil else readi32(b, o + 16) } end local N = lumps.nodes local nSize = if q3 then 36 elseif q2 then 28 elseif bsp2 then 44 elseif rmq then 32 else 24 local nn = count(N, nSize) if nn > limit then return nil, "limit: too many nodes" end for i = 0, nn - 1 do local o = base + N.at + i * nSize local node: any if q3 or q2 or bsp2 or rmq then local bk = if q3 then "i" elseif bsp2 then "f" else "s" local bs = if bk == "s" then 6 else 12 node = { plane = readi32(b, o), children = { readi32(b, o + 4), readi32(b, o + 8) }, mins = v3(b, o + 12, bk), maxs = v3(b, o + 12 + bs, bk) } if not q3 then local fo = o + 12 + bs * 2 if q2 then node.firstFace, node.numFaces = readu16(b, fo), readu16(b, fo + 2) else node.firstFace, node.numFaces = readu32(b, fo), readu32(b, fo + 4) end end else local c = {} for k = 0, 1 do local p = readu16(b, o + 4 + k * 2) c[k + 1] = if p < nn then p else -(65535 - p) - 1 -- Quakespasm Mod_LoadNodes_S: leaf 65535 - p end node = { plane = readi32(b, o), children = c, mins = v3(b, o + 8, "s"), maxs = v3(b, o + 14, "s"), firstFace = readu16(b, o + 20), numFaces = readu16(b, o + 22) } end d.nodes[i + 1] = node end local L = lumps.leaves or lumps.leafs local lSize = if q3 then 48 elseif q2 then 28 elseif bsp2 then 44 elseif rmq then 32 else 28 local nl = count(L, lSize) if nl > limit then return nil, "limit: too many leaves" end for i = 0, nl - 1 do local o = base + L.at + i * lSize local leaf: any if q3 then leaf = { cluster = readi32(b, o), area = readi32(b, o + 4), mins = v3(b, o + 8, "i"), maxs = v3(b, o + 20, "i"), firstFace = readi32(b, o + 32), numFaces = readi32(b, o + 36), firstBrush = readi32(b, o + 40), numBrushes = readi32(b, o + 44) } elseif q2 then leaf = { contents = readi32(b, o), cluster = readi16(b, o + 4), area = readi16(b, o + 6), mins = v3(b, o + 8, "s"), maxs = v3(b, o + 14, "s"), firstFace = readu16(b, o + 20), numFaces = readu16(b, o + 22), firstBrush = readu16(b, o + 24), numBrushes = readu16(b, o + 26) } else local bk = if bsp2 then "f" else "s" local bs = if bsp2 then 12 else 6 local fo = o + 8 + bs * 2 local big = bsp2 or rmq leaf = { contents = readi32(b, o), visofs = readi32(b, o + 4), mins = v3(b, o + 8, bk), maxs = v3(b, o + 8 + bs, bk), firstFace = if big then readu32(b, fo) else readu16(b, fo), numFaces = if big then readu32(b, fo + 4) else readu16(b, fo + 2) } local ao = fo + (if big then 8 else 4) leaf.ambient = { readu8(b, ao), readu8(b, ao + 1), readu8(b, ao + 2), readu8(b, ao + 3) } end d.leaves[i + 1] = leaf end if not q2 and not q3 then local C = lumps.clipnodes local cSize = if bsp2 or rmq then 12 else 8 local nc = count(C, cSize) if nc > limit then return nil, "limit: too many clipnodes" end local cl: { ClipNode } = {} for i = 0, nc - 1 do local o = base + C.at + i * cSize local c if cSize == 12 then c = { readi32(b, o + 4), readi32(b, o + 8) } else c = {} for k = 0, 1 do local p = readu16(b, o + 4 + k * 2) c[k + 1] = if p > nc then p - 65536 else p -- Quakespasm Mod_LoadClipnodes end end cl[i + 1] = { plane = readi32(b, o), children = c } end d.clipnodes = cl end local M = lumps.models local mSize = if q3 then 40 elseif q2 then 48 else 64 for i = 0, count(M, mSize) - 1 do local o = base + M.at + i * mSize local m: any if q3 then m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), headnodes = { 0 }, firstFace = readi32(b, o + 24), numFaces = readi32(b, o + 28) } elseif q2 then m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), origin = v3(b, o + 24, "f"), headnodes = { readi32(b, o + 36) }, firstFace = readi32(b, o + 40), numFaces = readi32(b, o + 44) } else m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), origin = v3(b, o + 24, "f"), headnodes = { readi32(b, o + 36), readi32(b, o + 40), readi32(b, o + 44), readi32(b, o + 48) }, visleafs = readi32(b, o + 52), firstFace = readi32(b, o + 56), numFaces = readi32(b, o + 60) } end d.models[i + 1] = m end if q2 or q3 then local B, S = lumps.brushes, lumps.brushsides local nb, ns = count(B, 12), count(S, if q3 then 8 else 4) if nb > limit or ns > limit then return nil, "limit: too many brushes" end local brushes: { Brush }, sides: { Side } = {}, {} for i = 0, nb - 1 do local o = base + B.at + i * 12 brushes[i + 1] = { firstSide = readi32(b, o), numSides = readi32(b, o + 4), contents = readi32(b, o + 8) } -- Quake III: a texture index end for i = 0, ns - 1 do if q3 then local o = base + S.at + i * 8 sides[i + 1] = { plane = readi32(b, o), texinfo = readi32(b, o + 4) } else local o = base + S.at + i * 4 sides[i + 1] = { plane = readu16(b, o), texinfo = readi16(b, o + 2) } end end d.brushes, d.brushSides = brushes, sides local LB = lumps.leafbrushes local lbs: { number } = {} for i = 0, count(LB, if q3 then 4 else 2) - 1 do lbs[i + 1] = if q3 then readi32(b, base + LB.at + i * 4) else readu16(b, base + LB.at + i * 2) end d.leafBrushes = lbs if q3 then -- brush contents come from the texture's contents flags local T = lumps.textures local nt = count(T, 72) for _, br in brushes do local t = br.contents br.contents = if t >= 0 and t < nt then readi32(b, base + T.at + t * 72 + 68) else 0 end end end local VL = lumps.visibility or lumps.visdata d.visibility = if VL then slice(b, base, VL) else nil local vis = d.visibility if vis and q2 and buffer.len(vis) >= 4 then d.numClusters = readi32(vis, 0) elseif vis and q3 and buffer.len(vis) >= 8 then d.numClusters, d.clusterBytes = readi32(vis, 0), readi32(vis, 4) end return d, nil end local function side(d: Data, plane: number, x: number, y: number, z: number): number local p = d.planes[plane + 1] if not p then return 0 end local n = p.normal return x * n[1] + y * n[2] + z * n[3] - p.dist end -- the leaf (0-based) at a point, walking the tree of model (0-based, default 0) function Tree.leafAt(d: Data, x: number, y: number, z: number, model: number?): number? local m = d.models[(model or 0) + 1] local node = if m then m.headnodes[1] else 0 for _ = 1, #d.nodes + 1 do if node < 0 then return -node - 1 end local nd = d.nodes[node + 1] if not nd then return nil end node = if side(d, nd.plane, x, y, z) >= 0 then nd.children[1] else nd.children[2] end return nil end -- Quake 1: CONTENTS_* (-1 empty, -2 solid, -3 water, -4 slime, -5 lava, -6 sky) in hull 0..3; Quake II: the leaf's -- CONTENTS_* bits; Quake III: the contents bits of the brushes holding the point function Tree.contents(d: Data, x: number, y: number, z: number, hull: number?, model: number?): number? local h = hull or 0 if (d.variant == "q1" or d.variant == "hl" or d.variant == "bsp2" or d.variant == "2psb") and h > 0 then local m = d.models[(model or 0) + 1] local cl = d.clipnodes or {} local node = if m then m.headnodes[h + 1] or -1 else -1 for _ = 1, #cl + 1 do if node < 0 then return node end local c = cl[node + 1] if not c then return nil end node = if side(d, c.plane, x, y, z) >= 0 then c.children[1] else c.children[2] end return nil end local leaf = Tree.leafAt(d, x, y, z, model) local lf = leaf and d.leaves[leaf + 1] if not lf then return nil end if d.variant ~= "q3" then return lf.contents end local out = 0 local lbs: { number }, brushes: { Brush }, sides: { Side } = d.leafBrushes or {}, d.brushes or {}, d.brushSides or {} local fb: number, nb: number = lf.firstBrush or 0, lf.numBrushes or 0 for k = fb, fb + nb - 1 do local br = brushes[(lbs[k + 1] or -1) + 1] if br then local inside = true for s = br.firstSide, br.firstSide + br.numSides - 1 do local sd = sides[s + 1] if not sd or side(d, sd.plane, x, y, z) > 0 then inside = false break end end if inside then out = bit32.bor(out, br.contents) end end end return out end local function decompress(vis: buffer, at: number, bytes: number, out: { boolean }) local n = buffer.len(vis) local i = 0 while i < bytes and at < n do local c = readu8(vis, at) if c ~= 0 then for bit = 0, 7 do if bit32.band(c, bit32.lshift(1, bit)) ~= 0 then out[i * 8 + bit + 1] = true end end i += 1 at += 1 else if at + 1 >= n then break end i += readu8(vis, at + 1) at += 2 end end end -- the leaves (0-based, sorted) potentially visible from a leaf; nil without visibility data (everything visible) function Tree.pvs(d: Data, leaf: number): { number }? local lf = d.leaves[leaf + 1] local vis = d.visibility if not lf or not vis then return nil end local out: { number } = {} if d.variant == "q2" or d.variant == "q3" then local nc = d.numClusters or 0 local c = lf.cluster or -1 if c < 0 or c >= nc then return out end local bits: { boolean } = {} if d.variant == "q2" then if 4 + nc * 8 > buffer.len(vis) then return nil end decompress(vis, readi32(vis, 4 + c * 8), (nc + 7) // 8, bits) else local sz = d.clusterBytes or 0 local o = 8 + c * sz for k = 0, sz - 1 do if o + k < buffer.len(vis) then local v = readu8(vis, o + k) for bit = 0, 7 do if bit32.band(v, bit32.lshift(1, bit)) ~= 0 then bits[k * 8 + bit + 1] = true end end end end end for i, l in d.leaves do local cl = l.cluster or -1 if cl >= 0 and bits[cl + 1] then table.insert(out, i - 1) end end return out end -- Quake 1: bit k = leaf k + 1 (leaf 0 is the shared solid leaf), visleafs bits local visofs = lf.visofs or -1 if leaf == 0 or visofs < 0 then return out end local n = d.models[1] and d.models[1].visleafs or (#d.leaves - 1) local bits: { boolean } = {} decompress(vis, visofs, (n + 7) // 8, bits) for k = 0, n - 1 do if bits[k + 1] then table.insert(out, k + 1) end end return out end return Tree