ws-voxel: wip: codecs/model/Minecraft, model/schematic, Types.Voxels blocks / colors

This commit is contained in:
4DBug committed 2026-10-31 14:25:41 -05:00
1 parent 998177e5ab
commit 58d7e08397
11 files changed
+2237

No files matched your search

+202
View File
@@ -0,0 +1,202 @@
--!strict
-- model/schematic/Common: what the three schematic flavours share: the region record they read into and write from,
-- NBT field access with type checks, and block entities in one shape ({ pos = { x, y, z }, id, data }: Sponge v3's,
-- converted from / to MCEdit's and Litematica's flat { x, y, z, id, ... } and Sponge v2's { Pos, Id, ... }).
local Types = require("@formats/Types")
local Nbt = require("@formats/codecs/text/Nbt")
local Common = {}
-- a block region: indices are u16 per cell in Types.Voxels order (x fastest, then y, then z) into states
export type Region = {
name: string?,
size: { number }, -- x, y, z cells
position: { number }, -- the minimum corner relative to the schematic origin
states: { string },
indices: buffer,
blockEntities: { any },
entities: { any },
}
-- int(v) -> the integer value of an NBT number (byte / short / int / long tag or plain number) | nil
function Common.int(v: any): number?
local n = Nbt.number(v)
if n == nil or n ~= n or n % 1 ~= 0 then
return nil
end
return n
end
-- dims(a, b, c, max) -> sx, sy, sz | nil, message: positive sizes whose product stays under max cells
function Common.dims(a: any, b: any, c: any, max: number): (number?, any, number?)
local x, y, z = Common.int(a), Common.int(b), Common.int(c)
if not x or not y or not z then
return nil, "missing or non-integer size", nil
end
if x < 0 or y < 0 or z < 0 then
return nil, "negative size", nil
end
if x * y * z > max then
return nil, "limit: " .. x .. " x " .. y .. " x " .. z .. " cells is over maxCells", nil
end
return x, y, z
end
-- ints(v) -> the numbers of an intArray tag or a list of numbers | nil
function Common.ints(v: any): { number }?
if Nbt.isTag(v) and v.type == "intArray" then
return v.value
elseif type(v) == "table" and not Nbt.isTag(v) and v[1] ~= nil then
local out = {}
for i, x in v do
out[i] = Nbt.number(x) or 0
end
return out
end
return nil
end
local function rest(c: any, skip: { [string]: boolean }): { [string]: any }
local out, keys = {}, {}
for _, k in Types.keys(c) do
if not skip[k] then
out[k] = c[k]
table.insert(keys, k)
end
end
return Types.object(out, keys)
end
local FLAT_SKIP = { x = true, y = true, z = true, id = true }
local POS_SKIP = { Pos = true, Id = true }
-- blockEntity(c, style) -> the common shape of one record: style "flat" (MCEdit, Litematica), "pos" (Sponge v1 / v2),
-- "data" (Sponge v3)
function Common.blockEntity(c: any, style: string): any
if type(c) ~= "table" or Nbt.isTag(c) then
return nil
end
if style == "flat" then
return { pos = { Common.int(c.x) or 0, Common.int(c.y) or 0, Common.int(c.z) or 0 }, id = if type(c.id) == "string" then c.id else nil, data = rest(c, FLAT_SKIP) }
end
local p = Common.ints(c.Pos) or { 0, 0, 0 }
local data = if style == "data" then (if type(c.Data) == "table" then c.Data else Types.object()) else rest(c, POS_SKIP)
return { pos = { p[1] or 0, p[2] or 0, p[3] or 0 }, id = if type(c.Id) == "string" then c.Id else nil, data = data }
end
-- flatBlockEntity(be, style) -> the record in a flavour's shape (the inverse of blockEntity)
function Common.flatBlockEntity(be: any, style: string): any
local o = Types.object()
local data = if type(be.data) == "table" then be.data else Types.object()
if style == "flat" then
Types.set(o, "x", be.pos[1])
Types.set(o, "y", be.pos[2])
Types.set(o, "z", be.pos[3])
if be.id then
Types.set(o, "id", be.id)
end
for _, k in Types.keys(data) do
if not FLAT_SKIP[k] then
Types.set(o, k, data[k])
end
end
return o
end
Types.set(o, "Pos", Nbt.intArray({ be.pos[1], be.pos[2], be.pos[3] }))
if be.id then
Types.set(o, "Id", be.id)
end
if style == "data" then
Types.set(o, "Data", data)
else
for _, k in Types.keys(data) do
if not POS_SKIP[k] then
Types.set(o, k, data[k])
end
end
end
return o
end
-- list(v) -> v when it is an NBT list (array), else an empty list
function Common.list(v: any): { any }
if type(v) == "table" and not Nbt.isTag(v) and not Types.isObject(v) then
return v
end
return {}
end
-- compound({ { key, value } }) -> a Types.object with these keys in this order (nil values skipped)
function Common.compound(fields: { { any } }): { [string]: any }
local o, keys = {}, {}
for _, kv in fields do
if kv[2] ~= nil then
o[kv[1]] = kv[2]
table.insert(keys, kv[1])
end
end
return Types.object(o, keys)
end
-- merge(base, fields, drop?) -> a compound of fields ({ { key, value } }, nil values left out) laid out in base's key
-- order (a decoded source compound: its other keys are kept, except those in drop), new keys after
function Common.merge(base: any, fields: { { any } }, drop: { [string]: boolean }?): { [string]: any }
if type(base) ~= "table" or not Types.isObject(base) then
return Common.compound(fields)
end
local mine: { [string]: any } = {}
local present: { [string]: boolean } = {}
for _, kv in fields do
present[kv[1]] = true
mine[kv[1]] = kv[2]
end
local out = {}
for _, k in Types.keys(base) do
if present[k] then
if mine[k] ~= nil then
table.insert(out, { k, mine[k] })
end
elseif not (drop and drop[k]) then
table.insert(out, { k, base[k] })
end
end
local seen: { [string]: boolean } = {}
for _, kv in out do
seen[kv[1]] = true
end
for _, kv in fields do
if not seen[kv[1]] and kv[2] ~= nil then
table.insert(out, kv)
end
end
return Common.compound(out)
end
-- dedupe(states, indices, n) -> states, indices with every state once (indices remapped when needed)
function Common.dedupe(states: { string }, indices: buffer, n: number): ({ string }, buffer)
local first: { [string]: number } = {}
local remap: { number } = {}
local out: { string } = {}
local dup = false
for i, s in states do
local at = first[s]
if at == nil then
table.insert(out, s)
at = #out - 1
first[s] = at
else
dup = true
end
remap[i] = at
end
if not dup then
return states, indices
end
local res = buffer.create(n * 2)
for i = 0, n - 1 do
buffer.writeu16(res, i * 2, remap[buffer.readu16(indices, i * 2) + 1] or 0)
end
return out, res
end
return Common