merge ws/quake

This commit is contained in:
4DBug committed 2026-11-01 20:08:48 -06:00
commit 58df9e8a3a
39 files changed
+10044 -1413

No files matched your search

+159
View File
@@ -90,6 +90,40 @@ do
t:eq(grey.meta.source.palette, false, "q1 without a palette: grey")
end
-- Half-Life / Quake II RGB lightmaps
for _, name in { "hl", "q2" } do
local c = Fx[name]
local s = assert(Bsp.decode(Bytes.reader(buffer.fromstring(c.data)))) :: any
local data = buffer.fromstring(c.data)
local la = buffer.readi32(data, (if name == "q2" then 8 + 7 * 8 else 4 + 8 * 8))
local atlas
for _, tx in s.textures do
if tx.name == "lightmap" then
atlas = tx.image
end
end
local placed = {}
for _, r in s.meta.lightmaps do
placed[r.face] = r
end
local bad = 0
for _, want in c.lightmaps do
local r = placed[want.face]
if not r or r.w ~= want.w or r.h ~= want.h then
bad += 1
else
for k = 0, want.w * want.h - 1 do
local px = buffer.readu32(atlas.pixels, ((r.y + k // want.w) * atlas.width + r.x + k % want.w) * 4)
local o = la + want.ofs + k * 3
if px ~= 0xFF000000 + buffer.readu8(data, o + 2) * 65536 + buffer.readu8(data, o + 1) * 256 + buffer.readu8(data, o) then
bad += 1
end
end
end
end
t:check(#c.lightmaps == 12 and bad == 0, name .. ": RGB lightmaps (" .. bad .. " wrong)")
end
do
local q2 = brush("q2", Fx.q2, { files = { ["textures/brick.wal"] = Fx.q2.wal } })
t:eq(q2.textures[1].uri, "textures/brick.wal", "q2: .wal URI")
@@ -214,4 +248,129 @@ do
end
t:eq(threw, 0, "q3: mutated lumps never throw")
end
-- ericw-tools maps: BSP 29 / BSP2 / 2PSB, lightmap atlas, .lit, visibility and hulls
do
local base: any = nil
for _, name in { "bsp29", "bsp2", "2psb" } do
local c = Fx.ericw[name]
local s, err = Bsp.decode(Bytes.reader(buffer.fromstring(c.data)), { lit = c.lit })
t:check(s, name .. ": " .. tostring(err))
local sc = s :: any
t:eq(F.detect(c.data), "bsp", name .. ": F.detect")
t:eq(sc.meta.source.variant, "quake", name .. ": variant")
t:eq(sc.meta.source.layout, ({ bsp29 = nil, bsp2 = "BSP2", ["2psb"] = "2PSB" } :: any)[name], name .. ": layout")
t:eq(sc.meta.skippedFaces, 0, name .. ": every face read")
-- the same compile in three layouts: the same triangles
local all = {}
for _, mesh in sc.meshes do
for _, p in mesh.primitives do
for _, x in Soup.list(Soup.positions(p)) do
table.insert(all, x)
end
end
end
if base then
t:check(near(all, base, 0), name .. ": geometry equals BSP 29's")
else
base = all
end
-- lightmaps: sizes as CalcSurfaceExtents, luxels copied from the lump (coloured from the .lit)
local placed = {}
for _, r in sc.meta.lightmaps do
placed[r.face] = r
end
local atlas
for _, tx in sc.textures do
if tx.name == "lightmap" then
atlas = tx.image
end
end
t:check(atlas ~= nil, name .. ": lightmap atlas")
local data = buffer.fromstring(c.data)
local litBuf = buffer.fromstring(c.lit)
local sizeBad, pixBad = 0, 0
for _, want in c.lightmaps do
local r = placed[want.face]
if not r or r.w ~= want.w or r.h ~= want.h then
sizeBad += 1
else
for k = 0, want.w * want.h - 1, 7 do
local x, y = r.x + k % want.w, r.y + k // want.w
local px = buffer.readu32(atlas.pixels, (y * atlas.width + x) * 4)
local o = 8 + (want.ofs + k) * 3
local exp = 0xFF000000 + buffer.readu8(litBuf, o + 2) * 65536 + buffer.readu8(litBuf, o + 1) * 256 + buffer.readu8(litBuf, o)
if px ~= exp then
pixBad += 1
end
end
end
end
t:eq(sizeBad, 0, name .. ": lightmap sizes (" .. #c.lightmaps .. " faces)")
t:eq(pixBad, 0, name .. ": lightmap luxels from the .lit")
local _ = data
local prim = sc.meshes[1].primitives[1]
t:check(prim.attributes.TEXCOORD_1 ~= nil, name .. ": TEXCOORD_1")
local mat = sc.materials[prim.material]
t:eq(sc.textures[mat.extras.bsp.lightmap].name, "lightmap", name .. ": material names the atlas")
local lu = Types.Scene.reader(prim.attributes.TEXCOORD_1) :: any
local inRange = true
for k = 0, prim.attributes.TEXCOORD_1.count - 1 do
if lu(k, 0) < 0 or lu(k, 0) > 1 or lu(k, 1) < 0 or lu(k, 1) > 1 then
inRange = false
end
end
t:check(inRange, name .. ": lightmap coordinates inside the atlas")
local grey = assert(Bsp.decode(Bytes.reader(buffer.fromstring(c.data)))) :: any
local gr
for _, r in grey.meta.lightmaps do
if r.face == c.lightmaps[1].face then
gr = r
end
end
local gimg
for _, tx in grey.textures do
if tx.name == "lightmap" then
gimg = tx.image
end
end
local v = buffer.readu8(data, 0)
local _ = v
local px = buffer.readu32(gimg.pixels, (gr.y * gimg.width + gr.x) * 4)
t:eq(bit32.band(px, 0xFF), bit32.band(bit32.rshift(px, 8), 0xFF), name .. ": without .lit: grey luxels")
-- visibility as vis wrote it
t:eq(#sc.meta.bsp.leaves, c.leaves, name .. ": leaves")
local pvsBad = 0
for leaf, want in c.pvs do
local got = Bsp.pvs(sc, leaf - 1) or {}
if table.concat(got, ",") ~= table.concat(want, ",") then
pvsBad += 1
end
end
t:eq(pvsBad, 0, name .. ": PVS of every leaf")
t:check(c.visible > 0, name .. ": vis ran")
-- contents in the point hull and the clipping hulls
local cBad = 0
for _, pr in c.probes do
for h = 0, 2 do
local got = Bsp.contents(sc, pr.p[1], pr.p[2], pr.p[3], h)
if got ~= pr.c[h + 1] then
cBad += 1
t:note("%s: contents at %s hull %d: %s, want %s", name, table.concat(pr.p, " "), h, tostring(got), tostring(pr.c[h + 1]))
end
end
end
t:eq(cBad, 0, name .. ": contents at the probes")
t:eq(Bsp.contents(sc, -128, -128, 100, 0), -1, name .. ": the room is empty")
t:eq(Bsp.contents(sc, 0, 0, 100, 0), -2, name .. ": the pillar is solid")
t:eq(Bsp.contents(sc, -250, 0, 100, 1), -2, name .. ": hull 1 is solid near a wall")
t:eq(Bsp.contents(sc, 0, 0, -8, 0), -2, name .. ": the floor is solid")
local leaf = Bsp.leafAt(sc, -128, -128, 24)
t:check(leaf and leaf > 0, name .. ": player start leaf")
local seen = Bsp.pvs(sc, leaf :: number) or {}
t:check(table.find(seen, Bsp.leafAt(sc, 0, 0, 220) :: number) ~= nil, name .. ": the light's leaf is visible from the start")
end
t:fuzz("bsp2", function(b: buffer)
return Bsp.decode(Bytes.reader(b), { lit = Fx.ericw.bsp2.lit })
end, { Fx.ericw.bsp2.data, Fx.ericw["2psb"].data, Fx.ericw.bsp29.data }, { mutations = 60 })
end
t:done()