Files
rosetta/src/Rosetta/model/buildmap/init.luau
T

271 lines
9.9 KiB
Luau

--!strict
-- model/buildmap: Build engine maps (.map versions 7-9: Duke Nukem 3D, Shadow Warrior, eduke32), from Ken Silverman's
-- BUILDINF.TXT and the eduke32 wiki ("MAP format"); no code taken. Supports: sectors, walls, sprites as raw tables
-- (meta.build), a Scene (sloped floors / ceilings, walls with upper / lower parts at red walls, masked walls, sprites
-- as nodes), v9 room-over-room fields, trailing bytes; writes the raw tables back. Not supported: v5 / v6, Blood's
-- encrypted BLM maps, the eduke32 text format (v10).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Types = require("@rosetta/Types")
local Geometry = require("@rosetta/model/buildmap/Geometry")
-- ------------------------------------------------------------------------------------------------ contract
local BuildMap = {
name = "buildmap",
category = "model",
extensions = { "map" },
mime = { "application/x-build-map" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Polygon" },
caps = { decode = true, encode = true, sprites = true, slopes = true },
}
-- ------------------------------------------------------------------------------------------------ types
export type Sector = Geometry.Sector
export type Wall = Geometry.Wall
export type Sprite = { [string]: number }
export type Map = Geometry.Map
export type DecodeOptions = Geometry.Options & {
maxBytes: number?, -- default 2^28
}
export type EncodeOptions = {
version: number?, -- 7, 8 or 9; default map.version
}
-- ------------------------------------------------------------------------------------------------ constants
-- field name, size (1 / -1 = u8 / i8, 2 / -2 = u16 / i16, -4 = i32), BUILDINF.TXT order
local SECTOR = {
{ "wallptr", -2 }, { "wallnum", -2 }, { "ceilingz", -4 }, { "floorz", -4 }, { "ceilingstat", -2 }, { "floorstat", -2 },
{ "ceilingpicnum", -2 }, { "ceilingheinum", -2 }, { "ceilingshade", -1 }, { "ceilingpal", 1 }, { "ceilingxpanning", 1 },
{ "ceilingypanning", 1 }, { "floorpicnum", -2 }, { "floorheinum", -2 }, { "floorshade", -1 }, { "floorpal", 1 },
{ "floorxpanning", 1 }, { "floorypanning", 1 }, { "visibility", 1 }, { "filler", 1 }, { "lotag", -2 }, { "hitag", -2 },
{ "extra", -2 },
}
local WALL = {
{ "x", -4 }, { "y", -4 }, { "point2", -2 }, { "nextwall", -2 }, { "nextsector", -2 }, { "cstat", -2 }, { "picnum", -2 },
{ "overpicnum", -2 }, { "shade", -1 }, { "pal", 1 }, { "xrepeat", 1 }, { "yrepeat", 1 }, { "xpanning", 1 },
{ "ypanning", 1 }, { "lotag", -2 }, { "hitag", -2 }, { "extra", -2 },
}
local SPRITE = {
{ "x", -4 }, { "y", -4 }, { "z", -4 }, { "cstat", -2 }, { "picnum", -2 }, { "shade", -1 }, { "pal", 1 }, { "clipdist", 1 },
{ "filler", 1 }, { "xrepeat", 1 }, { "yrepeat", 1 }, { "xoffset", -1 }, { "yoffset", -1 }, { "sectnum", -2 },
{ "statnum", -2 }, { "ang", -2 }, { "owner", -2 }, { "xvel", -2 }, { "yvel", -2 }, { "zvel", -2 }, { "lotag", -2 },
{ "hitag", -2 }, { "extra", -2 },
}
local SECTOR_SIZE, WALL_SIZE, SPRITE_SIZE = 40, 32, 44
local HEADER = 22 -- version, posx, posy, posz, ang, cursectnum, then u16 numsectors at 20
local readu16 = buffer.readu16
-- ------------------------------------------------------------------------------------------------ sniff
local function layout(b: buffer, o: number, limit: number): (number?, number?, number?, number?)
if limit - o < HEADER then
return nil
end
local ns = readu16(b, o + 20)
local p = o + HEADER + ns * SECTOR_SIZE
if p + 2 > limit then
return nil
end
local nw = readu16(b, p)
p += 2 + nw * WALL_SIZE
if p + 2 > limit then
return nil
end
local nsp = readu16(b, p)
p += 2 + nsp * SPRITE_SIZE
return ns, nw, nsp, p
end
function BuildMap.sniff(r: Bytes.Reader): number
local b, o, limit = r.buf, r.base, r.limit
if limit - o < HEADER then
return 0
end
local v = buffer.readu32(b, o)
if v ~= 7 and v ~= 8 and v ~= 9 then
return 0
end
local ns, _, _, stop = layout(b, o, limit)
if not ns or not stop or stop > limit then
return 0
end
return if stop == limit then 0.9 else 0.5
end
-- ------------------------------------------------------------------------------------------------ decode
local function readRecord(b: buffer, p: number, fields: { any }): { [string]: number }
local rec: { [string]: number } = {}
for _, f in fields do
local size = f[2]
local v
if size == 1 then
v = buffer.readu8(b, p)
elseif size == -1 then
v = buffer.readi8(b, p)
elseif size == 2 then
v = readu16(b, p)
elseif size == -2 then
v = buffer.readi16(b, p)
else
v = buffer.readi32(b, p)
end
rec[f[1]] = v
p += math.abs(size)
end
return rec
end
function BuildMap.parse(r: Bytes.Reader, opts: DecodeOptions?): (Map?, Err.Err?)
local b, o, limit = r.buf, r.base, r.limit
if limit - o < 4 then
return nil, Err.new(Err.TRUNCATED, "short header", o)
end
if buffer.readstring(b, o, 4) == "BLM\26" then
return nil, Err.new(Err.UNSUPPORTED, "Blood maps (BLM, encrypted) are not supported", o)
end
if buffer.readstring(b, o, 4) == "--ED" then
return nil, Err.new(Err.UNSUPPORTED, "eduke32 text maps (version 10) are not supported", o)
end
local version = buffer.readu32(b, o)
if version == 5 or version == 6 then
return nil, Err.new(Err.UNSUPPORTED, string.format("map version %d (only 7-9)", version), o)
elseif version < 7 or version > 9 then
return nil, Err.new(Err.CORRUPT, string.format("not a Build map (version %d)", version), o)
end
local maxBytes = opts and opts.maxBytes or 2 ^ 28
if limit - o > maxBytes then
return nil, Err.new(Err.LIMIT, string.format("%d bytes is over the %d byte limit", limit - o, maxBytes))
end
local ns, nw, nsp, stop = layout(b, o, limit)
if not ns or not nw or not nsp or not stop or stop > limit then
return nil, Err.new(Err.TRUNCATED, "sector / wall / sprite tables end early", o)
end
local map: Map = {
version = version,
x = buffer.readi32(b, o + 4),
y = buffer.readi32(b, o + 8),
z = buffer.readi32(b, o + 12),
ang = buffer.readi16(b, o + 16),
cursectnum = buffer.readi16(b, o + 18),
sectors = table.create(ns),
walls = table.create(nw),
sprites = table.create(nsp),
}
local p = o + HEADER
for i = 1, ns do
local s = readRecord(b, p, SECTOR) :: any
if s.wallptr < 0 or s.wallnum < 0 or s.wallptr + s.wallnum > nw then
return nil, Err.new(Err.CORRUPT, string.format("sector %d: walls %d + %d out of range", i - 1, s.wallptr, s.wallnum), p)
end
map.sectors[i] = s
p += SECTOR_SIZE
end
p += 2
for i = 1, nw do
map.walls[i] = readRecord(b, p, WALL) :: any
p += WALL_SIZE
end
p += 2
for i = 1, nsp do
map.sprites[i] = readRecord(b, p, SPRITE)
p += SPRITE_SIZE
end
if stop < limit then
local t = buffer.create(limit - stop)
buffer.copy(t, 0, b, stop, limit - stop)
map.trailing = t
end
if version == 9 then
Geometry.yax(map)
end
return map, nil
end
function BuildMap.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
local map, err = BuildMap.parse(r, opts)
if not map then
return nil, err
end
return BuildMap.scene(map, opts), nil
end
function BuildMap.scene(map: Map, opts: Geometry.Options?): Types.Scene
local scene = Geometry.scene(map, opts or {})
scene.meta.build = map
scene.meta.source = {
format = "buildmap",
version = map.version,
sectors = #map.sectors,
walls = #map.walls,
sprites = #map.sprites,
}
return scene
end
-- ------------------------------------------------------------------------------------------------ encode
local function writeRecord(out: Bytes.Writer, rec: { [string]: any }, fields: { any }): string?
for _, f in fields do
local v = rec[f[1]]
if type(v) ~= "number" or v ~= v then
return f[1]
end
local size = math.abs(f[2])
local n = math.floor(v)
if size == 1 then
out:u8(n % 256)
elseif size == 2 then
out:u16le(n % 65536)
else
out:u32le(n % 4294967296)
end
end
return nil
end
function BuildMap.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local map = if type(value) == "table" and type(value.meta) == "table" and value.meta.build then value.meta.build else value
if type(map) ~= "table" or type(map.sectors) ~= "table" or type(map.walls) ~= "table" or type(map.sprites) ~= "table" then
return nil, Err.new(Err.ARG, "needs a Scene from model/buildmap (meta.build) or a map table")
end
local version = math.floor(tonumber(opts and opts.version) or tonumber(map.version) or 7)
if version < 7 or version > 9 then
return nil, Err.new(Err.ARG, "version must be 7, 8 or 9")
end
local ns, nw, nsp = #map.sectors, #map.walls, #map.sprites
if ns > 65535 or nw > 65535 or nsp > 65535 then
return nil, Err.new(Err.ARG, "at most 65535 sectors / walls / sprites")
end
local trailing = if type(map.trailing) == "buffer" then map.trailing else nil
local out = Bytes.writer(HEADER + 6 + ns * SECTOR_SIZE + nw * WALL_SIZE + nsp * SPRITE_SIZE + (if trailing then buffer.len(trailing) else 0))
out:u32le(version)
for _, k in { "x", "y", "z" } do
out:u32le(math.floor(tonumber(map[k]) or 0) % 4294967296)
end
out:u16le(math.floor(tonumber(map.ang) or 0) % 65536)
out:u16le(math.floor(tonumber(map.cursectnum) or 0) % 65536)
for _, part in { { map.sectors, SECTOR, "sector" }, { map.walls, WALL, "wall" }, { map.sprites, SPRITE, "sprite" } } do
local list = part[1] :: { any }
out:u16le(#list)
for i, rec in list do
if type(rec) ~= "table" then
return nil, Err.new(Err.ARG, string.format("%s %d is not a table", part[3] :: string, i - 1))
end
local bad = writeRecord(out, rec, part[2] :: { any })
if bad then
return nil, Err.new(Err.ARG, string.format("%s %d: field %s is not a number", part[3] :: string, i - 1, bad))
end
end
end
if trailing then
out:bytes(trailing)
end
return out:finish(), nil
end
-- ------------------------------------------------------------------------------------------------ extras
BuildMap.slopeZ = Geometry.slopeZ
BuildMap.loops = Geometry.loops
return BuildMap