Files
rosetta/src/Rosetta/model/mca/Chunk.luau
T

132 lines
4.2 KiB
Luau

--!strict
-- model/mca/Chunk: an Anvil chunk's blocks (Minecraft wiki "Chunk format"): 1.18+ sections[] block_states
-- (non-spanning, >= 4 bits); 1.13 - 1.17 Level.Sections[] Palette + BlockStates (spanning before DataVersion 2529 =
-- 20w17a); pre-1.13 Blocks / Add / Data nibble arrays. A section is 16 x 16 x 16 cells, x then z then y.
local Types = require("@rosetta/Types")
local Nbt = require("@rosetta/codecs/text/Nbt")
local Mc = require("@rosetta/codecs/model/Minecraft")
local Chunk = {}
local Packed = Mc.Packed
export type Section = { y: number, states: { string }, cells: buffer } -- u16 per cell, 4096, x-z-y order
export type Blocks = {
x: number, -- xPos / zPos
z: number,
dataVersion: number?,
sections: { Section },
blockEntities: { any }, -- absolute x, y, z
entities: { any }, -- before 1.17
}
local function int(v: any): number?
local n = Nbt.number(v)
return if n and n == n and n % 1 == 0 then n else nil
end
local function list(v: any): { any }
return if type(v) == "table" and not Nbt.isTag(v) and not Types.isObject(v) then v else {}
end
local function paletted(y: number, palette: { any }, data: any, minBits: number, spanning: boolean): (Section?, string?)
local states = table.create(#palette) :: { string }
for i, c in palette do
local s = Mc.stateFromNbt(c)
if not s then
return nil, "section " .. y .. ": bad palette entry " .. i
end
states[i] = s
end
if #states == 0 then
return nil, nil
end
local cells = buffer.create(8192)
if #states > 1 or data ~= nil then
if not (Nbt.isTag(data) and data.type == "longArray") then
return nil, "section " .. y .. ": no block state data"
end
local bits = Packed.bitsFor(#states, minBits)
local ok, e = Packed.unpack(data.value, bits, 4096, spanning, cells, #states)
if not ok then
return nil, "section " .. y .. ": " .. tostring(e)
end
end
return { y = y, states = states, cells = cells }, nil
end
function Chunk.blocks(root: any): (Blocks?, string?)
if type(root) ~= "table" or not Types.isObject(root) then
return nil, "chunk NBT is not a compound"
end
local dv = int(root.DataVersion)
local level = if Types.isObject(root.Level) then root.Level else root
local x, z = int(level.xPos), int(level.zPos)
if not x or not z then
return nil, "chunk without xPos / zPos"
end
local out: Blocks = { x = x, z = z, dataVersion = dv, sections = {}, blockEntities = {}, entities = list(level.Entities) }
if root.sections ~= nil then
-- 1.18+
for _, s in list(root.sections) do
local y = int(s.Y)
local bs = s.block_states
if y and type(bs) == "table" and not Nbt.isTag(bs) then
local sec, e = paletted(y, list(bs.palette), bs.data, 4, false)
if e then
return nil, e
end
if sec then
table.insert(out.sections, sec)
end
end
end
out.blockEntities = list(root.block_entities)
return out, nil
end
for _, s in list(level.Sections) do
local y = int(s.Y)
if y then
if s.Palette ~= nil then
local sec, e = paletted(y, list(s.Palette), s.BlockStates, 4, (dv or 0) < 2529)
if e then
return nil, e
end
if sec then
table.insert(out.sections, sec)
end
elseif type(s.Blocks) == "buffer" then
local blocks = s.Blocks :: buffer
if buffer.len(blocks) < 4096 then
return nil, "section " .. y .. ": short Blocks array"
end
local data = if type(s.Data) == "buffer" and buffer.len(s.Data) >= 2048 then s.Data else nil
local add = if type(s.Add) == "buffer" and buffer.len(s.Add) >= 2048 then s.Add else nil
local slots: { [number]: number } = {}
local states: { string } = {}
local cells = buffer.create(8192)
for i = 0, 4095 do
local id = buffer.readu8(blocks, i)
if add then
id += Packed.nibble(add, 0, i, false) * 256
end
local d = if data then Packed.nibble(data, 0, i, false) else 0
local key = id * 16 + d
local slot = slots[key]
if not slot then
table.insert(states, Mc.legacyState(id, d))
slot = #states - 1
slots[key] = slot
end
buffer.writeu16(cells, i * 2, slot :: number)
end
table.insert(out.sections, { y = y, states = states, cells = cells })
end
end
end
out.blockEntities = list(level.TileEntities)
return out, nil
end
return Chunk