--!strict -- model/schematic/Litematic: .litematic, from LitematicaSchematic / LitematicaBitArray as documented by litemapy: -- Metadata, named Regions (Position, Size may be negative), palette (0 = air), BlockStates spanning longs (>= 2 bits), -- TileEntities relative to the region's minimum corner, Entities and pending ticks. local Types = require("@rosetta/Types") local Nbt = require("@rosetta/codecs/text/Nbt") local Mc = require("@rosetta/codecs/model/Minecraft") local Common = require("@rosetta/model/schematic/Common") local Litematic = {} local Packed = Mc.Packed local function xyz(c: any): { number }? if type(c) ~= "table" then return nil end local x, y, z = Common.int(c.x), Common.int(c.y), Common.int(c.z) if not x or not y or not z then return nil end return { x, y, z } end local function vec(v: { number }): any return Common.compound({ { "x", v[1] }, { "y", v[2] }, { "z", v[3] } }) end function Litematic.read(root: any, maxCells: number): ({ Common.Region }?, any) if not Types.isObject(root.Regions) then return nil, "no Regions" end local out: { Common.Region } = {} local total = 0 local raw: { [string]: any } = {} for _, name in Types.keys(root.Regions) do local reg = root.Regions[name] if type(reg) ~= "table" then return nil, "region " .. name .. " is not a compound" end local pos, size = xyz(reg.Position), xyz(reg.Size) if not pos or not size then return nil, "region " .. name .. ": no Position / Size" end local ax, ay, az = math.abs(size[1]), math.abs(size[2]), math.abs(size[3]) local sx, sy, sz = Common.dims(ax, ay, az, maxCells - total) if not sx then return nil, "region " .. name .. ": " .. tostring(sy) end total += ax * ay * az local states: { string } = {} for i, c in Common.list(reg.BlockStatePalette) do local st = Mc.stateFromNbt(c) if not st then return nil, "region " .. name .. ": bad palette entry " .. i end states[i] = st end if #states == 0 then states[1] = "minecraft:air" end local n = ax * ay * az local yzx = buffer.create(n * 2) local bs = reg.BlockStates if n > 0 then if not (Nbt.isTag(bs) and bs.type == "longArray") then return nil, "region " .. name .. ": no BlockStates" end local bits = Packed.bitsFor(#states, 2) local ok, e = Packed.unpack(bs.value, bits, n, true, yzx, #states) if not ok then return nil, "region " .. name .. ": " .. tostring(e) end end local bes = {} for _, c in Common.list(reg.TileEntities) do local be = Common.blockEntity(c, "flat") if be then table.insert(bes, be) end end table.insert(out, { name = name, size = { ax, ay, az }, position = { pos[1] + (if size[1] < 0 then size[1] + 1 else 0), pos[2] + (if size[2] < 0 then size[2] + 1 else 0), pos[3] + (if size[3] < 0 then size[3] + 1 else 0), }, states = states, indices = Packed.fromYzx(yzx, ax, ay, az), blockEntities = bes, entities = Common.list(reg.Entities), }) raw[name] = reg end return out, { version = Common.int(root.Version), subVersion = Common.int(root.SubVersion), dataVersion = Common.int(root.MinecraftDataVersion), metadata = root.Metadata, regions = raw, } end function Litematic.write(regions: { Common.Region }, version: number, dataVersion: number, base: any, title: string?): (any, string?) local baseRegions = if type(base) == "table" and Types.isObject(base.Regions) then base.Regions else nil local regs, order = {}, {} local lo, hi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge } local blocks, volume = 0, 0 for i, r in regions do local name = r.name or ("Region" .. (if i == 1 then "" else tostring(i))) while regs[name] ~= nil do name ..= "_" end local sx, sy, sz = r.size[1], r.size[2], r.size[3] local n = sx * sy * sz local states, indices = Common.dedupe(r.states, r.indices, n) if states[1] ~= "minecraft:air" then -- Litematica keeps air at index 0 local at = table.find(states, "minecraft:air") local shifted = buffer.create(n * 2) local nst = table.clone(states) if at then nst[1], nst[at] = states[at], states[1] else table.insert(nst, 1, "minecraft:air") end for c = 0, n - 1 do local v = buffer.readu16(indices, c * 2) local w = if at then (if v == 0 then at - 1 elseif v == at - 1 then 0 else v) else v + 1 buffer.writeu16(shifted, c * 2, w) end states, indices = nst, shifted end local palette = Nbt.list("compound") for k, st in states do palette[k] = Mc.stateToNbt(st) end local airLike: { [number]: boolean } = {} for k, st in states do airLike[k - 1] = Mc.isAir(st) end for c = 0, n - 1 do if not airLike[buffer.readu16(indices, c * 2)] then blocks += 1 end end volume += n local bits = Packed.bitsFor(#states, 2) local longs = Packed.pack(Packed.toYzx(indices, sx, sy, sz), n, bits, true) local tiles = Nbt.list("compound") for _, be in r.blockEntities do table.insert(tiles, Common.flatBlockEntity(be, "flat")) end local p = r.position local pos, size = { p[1], p[2], p[3] }, { sx, sy, sz } local b = if baseRegions then baseRegions[name] else nil local bsize = if type(b) == "table" then xyz(b.Size) else nil if bsize and math.abs(bsize[1]) == sx and math.abs(bsize[2]) == sy and math.abs(bsize[3]) == sz then for k = 1, 3 do if bsize[k] < 0 then size[k] = bsize[k] pos[k] = p[k] - bsize[k] - 1 end end end for k = 1, 3 do lo[k] = math.min(lo[k], p[k]) hi[k] = math.max(hi[k], p[k] + r.size[k]) end regs[name] = Common.merge(b, { { "BlockStatePalette", palette }, { "BlockStates", Nbt.longArray(longs) }, { "Entities", if #r.entities > 0 then r.entities else Nbt.list("compound") }, { "PendingBlockTicks", if type(b) == "table" then b.PendingBlockTicks else Nbt.list("compound") }, { "PendingFluidTicks", if type(b) == "table" then b.PendingFluidTicks else Nbt.list("compound") }, { "Position", vec(pos) }, { "Size", vec(size) }, { "TileEntities", tiles }, }) table.insert(order, name) end if #order == 0 then lo, hi = { 0, 0, 0 }, { 0, 0, 0 } end local bm = if type(base) == "table" and type(base.Metadata) == "table" then base.Metadata else nil local metadata = Common.merge(bm, { { "EnclosingSize", vec({ hi[1] - lo[1], hi[2] - lo[2], hi[3] - lo[3] }) }, { "Author", if bm and bm.Author then bm.Author else "" }, { "Description", if bm and bm.Description then bm.Description else "" }, { "Name", title or (if bm and bm.Name then bm.Name else "Unnamed") }, { "RegionCount", #order }, { "TimeCreated", if bm and bm.TimeCreated then bm.TimeCreated else Nbt.long(0) }, { "TimeModified", if bm and bm.TimeModified then bm.TimeModified else Nbt.long(0) }, { "TotalBlocks", blocks }, { "TotalVolume", volume }, }) local regions_ = Types.object(regs, order) return Common.merge(base, { { "MinecraftDataVersion", dataVersion }, { "Version", version }, { "SubVersion", if type(base) == "table" and base.SubVersion ~= nil then base.SubVersion elseif version >= 6 then 1 else nil }, { "Metadata", metadata }, { "Regions", regions_ }, }), nil end return Litematic