cc-mc-server/mc/chunk.lua
2024-07-12 19:49:56 +01:00

48 lines
No EOL
1.2 KiB
Lua

local util = require("util")
local nibbleArray = require("nibbleArray")
local bit32 = require("bit32")
local MAX_HEIGHT = 128
local chunk = {}
local defaultsTable = {
blocks = {},
metadata = {},
blockLight = {},
skyLight = {}
}
function chunk:CreateCoordPair(x, z)
return bit32.bor((x >= 0 and 0 or 2147483648), bit32.bor(bit32.rshift(bit32.band(x, 0x7fff), -16), bit32.bor((z >= 0 and 0 or 0x8000), bit32.band(z, 0x7fff))))
end
function chunk:new()
local newTable = util.deepCopyTable(defaultsTable) or {}
local blockBufferSize = 16 * 16 * MAX_HEIGHT
for i = 1, blockBufferSize do
table.insert(newTable.blocks, 0)
end
for i = 1, blockBufferSize / 2 do
table.insert(newTable.metadata, 0)
table.insert(newTable.blockLight, 255)
table.insert(newTable.skyLight, 255)
end
-- Set this table as this """class"""'s metatable
setmetatable(newTable, self)
self.__index = self
return newTable
end
function chunk:setBlock(blockId, x, y, z)
self.blocks[bit32.bor(bit32.rshift(x, -11), bit32.bor(bit32.rshift(z, -7), y)) + 1] = blockId
end
function chunk:getBlockId(x, y, z)
return self.blocks[bit32.bor(bit32.rshift(x, -11), bit32.bor(bit32.rshift(z, -7), y)) + 1]
end
return chunk