--!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 Mc = require("@formats/codecs/model/Minecraft") 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 Common.int = Mc.int -- 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 Common.ints = Mc.ints -- blockEntity(c, style) / flatBlockEntity(be, style): codecs/model/Minecraft's (shared with model/mca) Common.blockEntity = Mc.blockEntity Common.flatBlockEntity = Mc.flatBlockEntity -- 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