archive/vswap (Wolfenstein 3D pages: walls, sprites and sounds through a palette passed in) and vector/gamemaps (GAMEMAPS + MAPHEAD, Carmack + RLEW, into a TileMap); test data from the fixture's own writers
This commit is contained in:
1 parent
719abb97c0
commit
e51fd3585e
15 files changed
+3139
-1085
No files matched your search
@@ -0,0 +1,182 @@
|
||||
--!strict
|
||||
-- vector/gamemaps: Wolfenstein 3D / Spear of Destiny maps (GAMEMAPS "TED5v1.0" + MAPHEAD: RLEW tag, 100 header
|
||||
-- offsets; map headers of 3 planes, Carmack then RLEW compressed; ModdingWiki "GameMaps Format", no code taken) into a
|
||||
-- TileMap: one tile layer per plane ("walls", "objects", "plane3") with the raw tile values as data (no tileset).
|
||||
-- Either file loads; the other comes through opts.files / opts.resolve. Not supported: writing, uncompressed
|
||||
-- (MAPTEMP / Wolf3D 1.0) maps, Huffman-compressed (Keen) maps.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Input = require("@rosetta/core/Input")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Expand = require("@rosetta/vector/gamemaps/Expand")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Gamemaps = {
|
||||
name = "gamemaps",
|
||||
category = "vector",
|
||||
extensions = { "wl6", "wl1", "sod", "sdm" },
|
||||
mime = { "application/x-wolf3d-gamemaps" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types" },
|
||||
caps = { decode = true, encode = false, tilemap = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
map: number?, -- 1-based map slot, default the first present
|
||||
name: string?, -- the file name: picks the sibling's extension (GAMEMAPS.WL6 <-> MAPHEAD.WL6)
|
||||
files: { [string]: buffer | string }?,
|
||||
resolve: ((uri: string) -> (buffer | string)?)?,
|
||||
mapHead: (buffer | string)?, -- MAPHEAD given directly
|
||||
gameMaps: (buffer | string)?, -- GAMEMAPS given directly
|
||||
maxTiles: number?, -- default 2^20 per plane
|
||||
}
|
||||
export type MapInfo = { slot: number, name: string, width: number, height: number, offset: number }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local MAGIC = "TED5v1.0"
|
||||
local LAYERS = { "walls", "objects", "plane3" }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Gamemaps.sniff(r: Bytes.Reader): number
|
||||
if r:peek(8) == MAGIC then
|
||||
return 1
|
||||
end
|
||||
-- MAPHEAD: the RLEW tag 0xABCD, then 100 offsets (0 / -1 when absent)
|
||||
if r:remaining() >= 402 and r:remaining() <= 1024 and buffer.readu16(r.buf, r.base) == 0xABCD then
|
||||
local first = buffer.readi32(r.buf, r.base + 2)
|
||||
if first > 0 then
|
||||
return 0.7
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function asBuffer(v: any): buffer?
|
||||
if type(v) == "string" then
|
||||
return buffer.fromstring(v)
|
||||
end
|
||||
return if type(v) == "buffer" then v else nil
|
||||
end
|
||||
|
||||
local function sibling(opts: DecodeOptions?, stem: string): buffer?
|
||||
local name = opts and opts.name
|
||||
local ext = name and string.match(name, "%.([^./\\]+)$") or "WL6"
|
||||
local dir = name and string.match(name, "^(.*[/\\])") or ""
|
||||
for _, n in { stem .. "." .. ext, string.upper(stem .. "." .. ext), string.lower(stem .. "." .. ext) } do
|
||||
local got = Input.sibling(opts, dir .. n) or Input.sibling(opts, n)
|
||||
if got then
|
||||
return got
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
type Pair = { head: buffer, maps: buffer, tag: number }
|
||||
|
||||
local function pair(r: Bytes.Reader, opts: DecodeOptions?): (Pair?, Err.Err?)
|
||||
local self = buffer.create(r.limit - r.base)
|
||||
buffer.copy(self, 0, r.buf, r.base, r.limit - r.base)
|
||||
local head, maps
|
||||
if buffer.len(self) >= 8 and buffer.readstring(self, 0, 8) == MAGIC then
|
||||
maps = self
|
||||
head = asBuffer(opts and opts.mapHead) or sibling(opts, "MAPHEAD")
|
||||
if not head then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "MAPHEAD not available (opts.mapHead / opts.files / opts.resolve)")
|
||||
end
|
||||
else
|
||||
head = self
|
||||
maps = asBuffer(opts and opts.gameMaps) or sibling(opts, "GAMEMAPS")
|
||||
if not maps then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "GAMEMAPS not available (opts.gameMaps / opts.files / opts.resolve)")
|
||||
end
|
||||
end
|
||||
if buffer.len(head) < 2 then
|
||||
return nil, Err.new(Err.TRUNCATED, "MAPHEAD")
|
||||
end
|
||||
return { head = head, maps = maps, tag = buffer.readu16(head, 0) }, nil
|
||||
end
|
||||
|
||||
local function list(p: Pair): { MapInfo }
|
||||
local out: { MapInfo } = {}
|
||||
local hn = math.min(100, (buffer.len(p.head) - 2) // 4)
|
||||
local size = buffer.len(p.maps)
|
||||
for i = 0, hn - 1 do
|
||||
local o = buffer.readi32(p.head, 2 + i * 4)
|
||||
if o > 0 and o + 38 <= size then
|
||||
local nm = buffer.readstring(p.maps, o + 22, 16)
|
||||
local z = string.find(nm, "\0", 1, true)
|
||||
table.insert(out, { slot = i + 1, name = if z then string.sub(nm, 1, z - 1) else nm, width = buffer.readu16(p.maps, o + 18), height = buffer.readu16(p.maps, o + 20), offset = o })
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
function Gamemaps.maps(r: Bytes.Reader, opts: DecodeOptions?): ({ MapInfo }?, Err.Err?)
|
||||
local p, err = pair(r, opts)
|
||||
if not p then
|
||||
return nil, err
|
||||
end
|
||||
return list(p), nil
|
||||
end
|
||||
|
||||
function Gamemaps.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.TileMap?, Err.Err?)
|
||||
local p, err = pair(r, opts)
|
||||
if not p then
|
||||
return nil, err
|
||||
end
|
||||
local maps = list(p)
|
||||
if #maps == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "no maps in MAPHEAD")
|
||||
end
|
||||
local want = opts and opts.map
|
||||
local m: MapInfo? = if want then nil else maps[1]
|
||||
for _, x in maps do
|
||||
if x.slot == want then
|
||||
m = x
|
||||
end
|
||||
end
|
||||
if not m then
|
||||
return nil, Err.new(Err.ARG, "no map in slot " .. tostring(want))
|
||||
end
|
||||
local w, h = m.width, m.height
|
||||
if w == 0 or h == 0 or w * h > (opts and opts.maxTiles or 2 ^ 20) then
|
||||
return nil, Err.new(if w * h == 0 then Err.CORRUPT else Err.LIMIT, "map size " .. w .. " x " .. h)
|
||||
end
|
||||
local b = p.maps
|
||||
local size = buffer.len(b)
|
||||
local tm = Types.TileMap.new(w, h, 64, 64)
|
||||
for k = 0, 2 do
|
||||
local at = buffer.readi32(b, m.offset + k * 4)
|
||||
local n = buffer.readu16(b, m.offset + 12 + k * 2)
|
||||
if at > 0 and n >= 2 then
|
||||
if at + n > size then
|
||||
return nil, Err.new(Err.TRUNCATED, "plane " .. (k + 1) .. " past the end", at)
|
||||
end
|
||||
local carmackLen = buffer.readu16(b, at)
|
||||
local mid, msg = Expand.carmack(b, at + 2, n - 2, carmackLen)
|
||||
if not mid then
|
||||
return nil, Err.from(msg, at)
|
||||
end
|
||||
if carmackLen < 2 or buffer.readu16(mid, 0) ~= w * h * 2 then
|
||||
return nil, Err.new(Err.CORRUPT, "plane " .. (k + 1) .. ": RLEW length is not width x height", at)
|
||||
end
|
||||
local plane, rmsg = Expand.rlew(mid, 2, carmackLen - 2, w * h, p.tag)
|
||||
if not plane then
|
||||
return nil, Err.from(rmsg, at)
|
||||
end
|
||||
local data = table.create(w * h, 0)
|
||||
for i = 0, w * h - 1 do
|
||||
data[i + 1] = buffer.readu16(plane, i * 2)
|
||||
end
|
||||
table.insert(tm.layers, { type = "tiles", id = k + 1, name = LAYERS[k + 1], width = w, height = h, data = data, visible = true, opacity = 1, properties = {} })
|
||||
end
|
||||
end
|
||||
tm.properties.name = m.name
|
||||
tm.meta.source = { format = "gamemaps", map = m.slot, name = m.name, rlewTag = p.tag, maps = maps }
|
||||
return tm, nil
|
||||
end
|
||||
|
||||
return Gamemaps
|
||||
Reference in new issue
Block a user