-- codecs/model/Minecraft tests: packed index arrays (spanning and not, 1..16 bits, against a bit-by-bit reference), -- Sponge varints, nibble arrays, YZX reordering, block-state strings and their NBT form, every legacy id / data pair -- through legacyState / legacyId, colours (air, families, fallbacks), voxels / blocksOf and nearest-colour blocks. -- The formats using it: tests/model/schematic.luau, tests/model/mca.luau. local T = require("@tests/T") local Mc = require("@formats/codecs/model/Minecraft") local Types = require("@formats/Types") local t = T.suite("minecraft") local P = Mc.Packed local rand = T.rng(3) -- 1. packed arrays against a reference that walks the bits one at a time local function refBit(longs: buffer, p: number): number return bit32.extract(buffer.readu8(longs, p // 8), p % 8, 1) end for bits = 1, 16 do for _, spanning in { true, false } do local count = 100 + rand(300) local max = math.min(2 ^ bits, 70000) local idx = buffer.create(count * 2) for i = 0, count - 1 do buffer.writeu16(idx, i * 2, rand(max)) end local longs = P.pack(idx, count, bits, spanning) t:eq(buffer.len(longs), P.longsNeeded(count, bits, spanning) * 8, bits .. " bits: size") local per = 64 // bits local bad = 0 for i = 0, count - 1 do local p = if spanning then i * bits else (i // per) * 64 + (i % per) * bits local v = 0 for k = 0, bits - 1 do v += refBit(longs, p + k) * 2 ^ k end if v ~= buffer.readu16(idx, i * 2) then bad += 1 end end t:eq(bad, 0, bits .. " bits " .. tostring(spanning) .. ": layout") local back = buffer.create(count * 2) local ok, e = P.unpack(longs, bits, count, spanning, back, max) t:check(ok, tostring(e)) t:eq(buffer.tostring(back), buffer.tostring(idx), bits .. " bits " .. tostring(spanning) .. ": round trip") end end do local out = buffer.create(40) t:check(not P.unpack(buffer.create(8), 4, 20, false, out, 16), "too few longs") local l = P.pack(buffer.fromstring("\5\0"), 1, 4, false) t:check(not P.unpack(l, 4, 1, false, out, 3), "index past the palette") t:eq(P.bitsFor(17, 4), 5, "bitsFor") t:eq(P.bitsFor(1, 2), 2, "bitsFor minimum") end -- 2. varints, nibbles, YZX do local idx = buffer.create(400) for i = 0, 199 do buffer.writeu16(idx, i * 2, (i * 997) % 20000) end local v = P.toVarints(idx, 200) local back = buffer.create(400) t:check(P.varints(v, 0, buffer.len(v), 200, back, 20000), "varints") t:eq(buffer.tostring(back), buffer.tostring(idx), "varints round trip") t:check(not P.varints(v, 0, buffer.len(v) - 1, 200, back, 20000), "varints truncated") local nib = buffer.create(2) P.setNibble(nib, 0, 0, 0xA, true) P.setNibble(nib, 0, 1, 0x3, true) P.setNibble(nib, 0, 2, 0x7, false) t:eq(buffer.readu8(nib, 0), 0xA3, "high nibble first") t:eq(buffer.readu8(nib, 1), 0x07, "low nibble first") t:eq(P.nibble(nib, 0, 1, true), 3, "read nibble") local sx, sy, sz = 3, 4, 5 local yzx = buffer.create(sx * sy * sz * 2) for y = 0, sy - 1 do for z = 0, sz - 1 do for x = 0, sx - 1 do buffer.writeu16(yzx, (x + z * sx + y * sx * sz) * 2, x * 100 + y * 10 + z) end end end local vox = P.fromYzx(yzx, sx, sy, sz) t:eq(buffer.readu16(vox, (2 + sx * (3 + sy * 4)) * 2), 234, "fromYzx") t:eq(buffer.tostring(P.toYzx(vox, sx, sy, sz)), buffer.tostring(yzx), "toYzx") end -- 3. states do local name, props = Mc.parseState("oak_log[axis=y, foo = bar]") t:eq(name, "minecraft:oak_log", "namespace added") t:eq(#props, 2, "props") t:eq(Mc.stateString(name, props), "minecraft:oak_log[axis=y,foo=bar]", "stateString") local c = Mc.stateToNbt("minecraft:chest[facing=north,type=single]") t:eq(table.concat(Types.keys(c.Properties), ","), "facing,type", "NBT property order") t:eq(Mc.stateFromNbt(c), "minecraft:chest[facing=north,type=single]", "stateFromNbt") t:eq(Mc.baseName("minecraft:stone"), "stone", "baseName") t:check(Mc.isAir("minecraft:cave_air") and not Mc.isAir("minecraft:stone"), "isAir") end -- 4. legacy ids: every id / data pair reads back exactly do local bad = 0 for id = 0, 255 do for d = 0, 15 do local s = Mc.legacyState(id, d) local i2, d2 = Mc.legacyId(s) if i2 ~= id or d2 ~= d then bad += 1 end end end t:eq(bad, 0, "legacy round trip") t:eq(Mc.legacyState(35, 14), "minecraft:red_wool", "wool 14") t:eq(Mc.legacyState(5, 2), "minecraft:birch_planks", "planks 2") t:eq(Mc.legacyState(17, 5), "minecraft:spruce_log[data=5]", "log axis bits kept") t:eq(select(1, Mc.legacyId("minecraft:white_concrete")), 251, "modern name -> legacy id") end -- 5. colours do t:eq(Mc.color("minecraft:air"), nil, "air") local function hex(s: string): string local c = Mc.color(s) :: { number } return string.format("%02x%02x%02x%02x", c[1], c[2], c[3], c[4]) end t:eq(hex("minecraft:stone"), "7d7d7dff", "exact") t:eq(hex("minecraft:stone_brick_stairs[facing=east]"), hex("minecraft:stone_bricks"), "stairs -> base") t:eq(hex("minecraft:red_concrete"), "8e2020ff", "dye family") t:eq(hex("minecraft:dark_oak_log"), "3e2f17ff", "wood bark") t:eq(string.sub(hex("minecraft:red_stained_glass"), 7, 8), "80", "stained glass translucent") t:eq(hex("minecraft:wool[data=14]"), hex("minecraft:red_wool"), "legacy data colour") t:eq(hex("mod:something_odd"), "909090ff", "fallback") end -- 6. voxels / blocksOf do local states = { "minecraft:air", "minecraft:stone", "minecraft:red_wool", "minecraft:stone" } local idx = buffer.create(8 * 2) for i = 0, 7 do buffer.writeu16(idx, i * 2, i % 4) end local v = Mc.voxels(2, 2, 2, states, idx) t:eq(buffer.readu8(v.data, 0), 0, "air -> 0") t:eq(buffer.readu8(v.data, 1), buffer.readu8(v.data, 3), "same colour, same index") t:check(buffer.readu8(v.data, 2) ~= buffer.readu8(v.data, 1), "different colours") local s2, i2 = Mc.blocksOf(v) t:check(s2 == states and i2 == idx, "blocksOf keeps the blocks") local g = Types.Voxels.new(2, 1, 1) g.palette[7] = { 240, 118, 19, 255 } buffer.writeu8(g.data, 1, 7) local s3, i3 = Mc.blocksOf(g) t:eq((s3 :: { string })[buffer.readu16(i3, 2) + 1], "minecraft:orange_wool", "nearest block by colour") t:eq((s3 :: { string })[buffer.readu16(i3, 0) + 1], "minecraft:air", "empty -> air") end t:done()