cc-mc-server/mc/chunk.lua

48 lines
1.2 KiB
Lua
Raw Permalink Normal View History

2024-07-12 16:58:47 +01:00
local util = require("util")
local nibbleArray = require("nibbleArray")
2024-07-12 19:49:56 +01:00
local bit32 = require("bit32")
2024-07-12 16:58:47 +01:00
local MAX_HEIGHT = 128
local chunk = {}
local defaultsTable = {
2024-07-12 19:49:56 +01:00
blocks = {},
metadata = {},
blockLight = {},
skyLight = {}
2024-07-12 16:58:47 +01:00
}
2024-07-12 19:49:56 +01:00
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
2024-07-12 16:58:47 +01:00
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
2024-07-12 19:49:56 +01:00
for i = 1, blockBufferSize / 2 do
table.insert(newTable.metadata, 0)
table.insert(newTable.blockLight, 255)
table.insert(newTable.skyLight, 255)
end
2024-07-12 16:58:47 +01:00
-- Set this table as this """class"""'s metatable
setmetatable(newTable, self)
self.__index = self
return newTable
end
2024-07-12 19:49:56 +01:00
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
2024-07-12 16:58:47 +01:00
return chunk