Files
rosetta/src/Formats/model/schematic/Mcedit.luau
T

214 lines
6.7 KiB
Luau

--!strict
-- model/schematic/Mcedit: the MCEdit / WorldEdit .schematic layout (Minecraft before 1.13), from the Minecraft wiki
-- "Schematic file format" page: Width / Height / Length, Blocks + Data (+ WorldEdit's AddBlocks high nibbles, high
-- nibble first), Materials = "Alpha", TileEntities, Entities, WEOffset*, and the SchematicaMapping / BlockIDs name
-- tables of Schematica and MCEdit-Unified. Block ids become state strings through codecs/model/Minecraft
-- (legacyState: wool 14 -> minecraft:red_wool) and back (legacyId; blocks unknown before 1.13 become the legacy block
-- nearest in colour).
local Types = require("@formats/Types")
local Nbt = require("@formats/codecs/text/Nbt")
local Mc = require("@formats/codecs/model/Minecraft")
local Common = require("@formats/model/schematic/Common")
local Mcedit = {}
local Packed = Mc.Packed
-- read(root, maxCells) -> { Region }, extra | nil, message
function Mcedit.read(root: any, maxCells: number): ({ Common.Region }?, any)
local blocks = root.Blocks
if type(blocks) ~= "buffer" then
return nil, "no Blocks byte array"
end
local function u16(v: any): any
local n = Common.int(v)
return if n and n < 0 then n + 65536 else n
end
local sx, sy, sz = Common.dims(u16(root.Width), u16(root.Height), u16(root.Length), maxCells)
if not sx then
return nil, sy
end
local n = sx * sy :: number * (sz :: number)
if buffer.len(blocks) < n then
return nil, "truncated: Blocks has " .. buffer.len(blocks) .. " bytes for " .. n .. " cells"
end
local data = if type(root.Data) == "buffer" and buffer.len(root.Data) >= n then root.Data else nil
local add = if type(root.AddBlocks) == "buffer" and buffer.len(root.AddBlocks) >= (n + 1) // 2 then root.AddBlocks else nil
-- id -> name tables of Schematica (name -> id) and MCEdit-Unified (id text -> name)
local names: { [number]: string } = {}
if Types.isObject(root.SchematicaMapping) then
for _, k in Types.keys(root.SchematicaMapping) do
local id = Common.int(root.SchematicaMapping[k])
if id then
names[id] = k
end
end
end
if Types.isObject(root.BlockIDs) then
for _, k in Types.keys(root.BlockIDs) do
local id = tonumber(k)
if id and type(root.BlockIDs[k]) == "string" then
names[id] = root.BlockIDs[k]
end
end
end
local function state(id: number, d: number): string
local name = names[id]
if name then
local base = string.match(name, "^minecraft:(.*)$") or (if string.find(name, ":", 1, true) then nil else name)
if base then
local std = table.find(Mc.Legacy.NAMES, base)
if std then
return Mc.legacyState(std - 1, d)
end
end
return Mc.stateString(if string.find(name, ":", 1, true) then name else "minecraft:" .. name, if d ~= 0 then { { "data", tostring(d) } } else nil)
end
return Mc.legacyState(id, d)
end
local slots: { [number]: number } = {}
local states: { string } = {}
local yzx = buffer.create(n * 2)
for i = 0, n - 1 do
local id = buffer.readu8(blocks, i)
if add then
id += Packed.nibble(add, 0, i, true) * 256
end
local d = if data then buffer.readu8(data, i) % 16 else 0
local key = id * 16 + d
local s = slots[key]
if not s then
table.insert(states, state(id, d))
s = #states - 1
slots[key] = s
end
buffer.writeu16(yzx, i * 2, s :: number)
end
local bes = {}
for _, c in Common.list(root.TileEntities) do
local be = Common.blockEntity(c, "flat")
if be then
table.insert(bes, be)
end
end
return {
{
name = nil,
size = { sx, sy, sz },
position = { Common.int(root.WEOffsetX) or 0, Common.int(root.WEOffsetY) or 0, Common.int(root.WEOffsetZ) or 0 },
states = states,
indices = Packed.fromYzx(yzx, sx, sy :: number, sz :: number),
blockEntities = bes,
entities = Common.list(root.Entities),
},
},
{ materials = root.Materials }
end
-- the legacy blocks a colour can become (full blocks of distinct colours)
local CANDIDATES: { { any } }? = nil
local function candidates(): { { any } }
if CANDIDATES then
return CANDIDATES
end
local out = {}
local function add(id: number, d: number)
local c = Mc.color(Mc.legacyState(id, d))
if c then
table.insert(out, { id, d, c })
end
end
for d = 0, 15 do
add(35, d) -- wool
add(159, d) -- stained clay
add(251, d) -- concrete
end
for d = 0, 5 do
add(5, d) -- planks
end
for _, id in { 1, 2, 3, 4, 7, 12, 13, 24, 41, 42, 45, 48, 49, 57, 80, 82, 87, 88, 89, 98, 121, 133, 152, 155, 168, 172, 173, 174, 179, 201, 216 } do
add(id, 0)
end
CANDIDATES = out
return out
end
local function nearest(state: string): (number, number)
local c = Mc.color(state)
if not c then
return 0, 0
end
if c[4] < 200 then
return 20, 0 -- glass
end
local bid, bd, best = 1, 0, math.huge
for _, k in candidates() do
local kc = k[3]
local dist = (kc[1] - c[1]) ^ 2 + (kc[2] - c[2]) ^ 2 + (kc[3] - c[3]) ^ 2
if dist < best then
bid, bd, best = k[1], k[2], dist
end
end
return bid, bd
end
-- write(region, base?) -> the root compound (named "Schematic") | nil, message. base: a decoded root whose other
-- fields are kept
function Mcedit.write(r: Common.Region, base: any): (any, string?)
local sx, sy, sz = r.size[1], r.size[2], r.size[3]
if sx > 65535 or sy > 65535 or sz > 65535 then
return nil, "a .schematic is at most 65535 cells on each axis"
end
local n = sx * sy * sz
local ids, datas = table.create(#r.states), table.create(#r.states)
local high = false
for i, s in r.states do
local id, d = Mc.legacyId(s)
if id == nil then
id, d = nearest(s)
end
ids[i], datas[i] = id, d
if (id :: number) > 255 then
high = true
end
end
local yzx = Packed.toYzx(r.indices, sx, sy, sz)
local blocks, data = buffer.create(n), buffer.create(n)
local add = if high then buffer.create((n + 1) // 2) else nil
for i = 0, n - 1 do
local s = buffer.readu16(yzx, i * 2) + 1
local id = ids[s] or 0
buffer.writeu8(blocks, i, id % 256)
buffer.writeu8(data, i, datas[s] or 0)
if add then
Packed.setNibble(add, 0, i, id // 256, true)
end
end
local tiles = Nbt.list("compound")
for _, be in r.blockEntities do
table.insert(tiles, Common.flatBlockEntity(be, "flat"))
end
local ents = if #r.entities > 0 then r.entities else Nbt.list("compound")
local fields = {
{ "Width", Nbt.short(sx) },
{ "Height", Nbt.short(sy) },
{ "Length", Nbt.short(sz) },
{ "Materials", "Alpha" },
{ "Blocks", blocks },
{ "Data", data },
{ "AddBlocks", add },
{ "Entities", ents },
{ "TileEntities", tiles },
}
local p = r.position
if p[1] ~= 0 or p[2] ~= 0 or p[3] ~= 0 or (type(base) == "table" and base.WEOffsetX ~= nil) then
table.insert(fields, { "WEOffsetX", p[1] })
table.insert(fields, { "WEOffsetY", p[2] })
table.insert(fields, { "WEOffsetZ", p[3] })
end
local root = Common.merge(base, fields, { SchematicaMapping = true, BlockIDs = true, Add = true })
return root, nil
end
return Mcedit