forked from MineClone5/MineClone5
Implement basic part of mapgen API v3
This commit is contained in:
parent
6b848c51d2
commit
7d57bb7d6e
|
@ -19,13 +19,18 @@ local minetest_log = minetest.log
|
|||
local minetest_pos_to_string = minetest.pos_to_string
|
||||
|
||||
-- Calculate mapgen_edge_min/mapgen_edge_max
|
||||
mcl_mapgen.CS = math_max(1, tonumber(minetest.get_mapgen_setting("chunksize")) or 5)
|
||||
mcl_mapgen.BS = math_max(1, core.MAP_BLOCKSIZE or 16)
|
||||
mcl_mapgen.LIMIT = math_max(1, tonumber(minetest.get_mapgen_setting("mapgen_limit")) or 31000)
|
||||
mcl_mapgen.MAX_LIMIT = math_max(1, core.MAX_MAP_GENERATION_LIMIT or 31000) -- might be set to 31000 or removed, see https://github.com/minetest/minetest/issues/10428
|
||||
mcl_mapgen.OFFSET = - math_floor(mcl_mapgen.CS / 2)
|
||||
mcl_mapgen.OFFSET_NODES = mcl_mapgen.OFFSET * mcl_mapgen.BS
|
||||
mcl_mapgen.CS_NODES = mcl_mapgen.CS * mcl_mapgen.BS
|
||||
mcl_mapgen.CS = math_max(1, tonumber(minetest.get_mapgen_setting("chunksize")) or 5)
|
||||
mcl_mapgen.BS = math_max(1, core.MAP_BLOCKSIZE or 16)
|
||||
mcl_mapgen.LIMIT = math_max(1, tonumber(minetest.get_mapgen_setting("mapgen_limit")) or 31000)
|
||||
mcl_mapgen.MAX_LIMIT = 31000 -- MAX_MAP_GENERATION_LIMIT, https://github.com/minetest/minetest/issues/10428
|
||||
mcl_mapgen.OFFSET = - math_floor(mcl_mapgen.CS / 2)
|
||||
mcl_mapgen.OFFSET_NODES = mcl_mapgen.OFFSET * mcl_mapgen.BS
|
||||
mcl_mapgen.CS_NODES = mcl_mapgen.CS * mcl_mapgen.BS
|
||||
mcl_mapgen.LAST_BLOCK = mcl_mapgen.CS - 1
|
||||
mcl_mapgen.LAST_NODE_IN_BLOCK = mcl_mapgen.BS - 1
|
||||
mcl_mapgen.LAST_NODE_IN_CHUNK = mcl_mapgen.CS_NODES - 1
|
||||
mcl_mapgen.HALF_CS_NODES = math_floor(mcl_mapgen.CS_NODES / 2)
|
||||
mcl_mapgen.CS_3D = mcl_mapgen.CS^3
|
||||
|
||||
local central_chunk_min_pos = mcl_mapgen.OFFSET * mcl_mapgen.BS
|
||||
local central_chunk_max_pos = central_chunk_min_pos + mcl_mapgen.CS_NODES - 1
|
||||
|
@ -77,12 +82,14 @@ local nodes_block = 0
|
|||
local nodes_chunk = 0
|
||||
local safe_functions = 0
|
||||
|
||||
local BS, CS = mcl_mapgen.BS, mcl_mapgen.CS -- Mapblock size (in nodes), Mapchunk size (in blocks)
|
||||
local LAST_BLOCK, LAST_NODE = CS - 1, BS - 1 -- First mapblock in chunk (node in mapblock) has number 0, last has THIS number. It's for runtime optimization
|
||||
local offset = mcl_mapgen.OFFSET -- Central mapchunk offset (in blocks)
|
||||
local CS_NODES = mcl_mapgen.CS_NODES -- 80
|
||||
|
||||
local CS_3D = CS * CS * CS
|
||||
local BS, CS = mcl_mapgen.BS, mcl_mapgen.CS -- Mapblock size (in nodes), Mapchunk size (in blocks)
|
||||
local offset = mcl_mapgen.OFFSET -- Central mapchunk offset (in blocks)
|
||||
local CS_NODES = mcl_mapgen.CS_NODES
|
||||
local LAST_BLOCK = mcl_mapgen.LAST_BLOCK
|
||||
local LAST_NODE_IN_BLOCK = mcl_mapgen.LAST_NODE_IN_BLOCK
|
||||
local LAST_NODE_IN_CHUNK = mcl_mapgen.LAST_NODE_IN_CHUNK
|
||||
local HALF_CS_NODES = mcl_mapgen.HALF_CS_NODES
|
||||
local CS_3D = mcl_mapgen.CS_3D
|
||||
|
||||
local DEFAULT_ORDER = order.DEFAULT
|
||||
|
||||
|
@ -117,20 +124,72 @@ function mcl_mapgen.register_mapgen_block_lvm(callback_function, order)
|
|||
table.sort(queue_blocks_lvm, function(a, b) return (a.order <= b.order) end)
|
||||
end
|
||||
|
||||
local storage = minetest.get_mod_storage()
|
||||
local blocks = minetest.deserialize(storage:get_string("mapgen_blocks") or "return {}") or {}
|
||||
local chunks = minetest.deserialize(storage:get_string("mapgen_chunks") or "return {}") or {}
|
||||
minetest.register_on_shutdown(function()
|
||||
storage:set_string("mapgen_chunks", minetest.serialize(chunks))
|
||||
storage:set_string("mapgen_blocks", minetest.serialize(blocks))
|
||||
end)
|
||||
|
||||
local vm_context -- here will be many references and flags, like: param2, light_data, heightmap, biomemap, heatmap, humiditymap, gennotify, write_lvm, write_param2, shadow
|
||||
local data, param2_data, light, area
|
||||
local current_blocks = {}
|
||||
local current_chunks = {}
|
||||
local lvm_buffer, lvm_param2_buffer, lvm_light_buffer = {}, {}, {} -- Static buffer pointers
|
||||
|
||||
local all_blocks_in_chunk = {}
|
||||
for x = 0, CS-1 do
|
||||
for y = 0, CS-1 do
|
||||
for z = 0, CS-1 do
|
||||
all_blocks_in_chunk[CS * (CS * x + y) + z] = vector.new(x, y, z)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local chunk_scan_range = {
|
||||
[-CS_NODES] = {0 , 0 },
|
||||
[ 0 ] = {0 , LAST_BLOCK},
|
||||
[ CS_NODES] = {LAST_BLOCK, LAST_BLOCK},
|
||||
}
|
||||
|
||||
local function is_chunk_finished(minp)
|
||||
local center = minp + HALF_CS_NODES
|
||||
for check_x = center.x - CS_NODES, center.x + CS_NODES, CS_NODES do
|
||||
for check_y = center.y - CS_NODES, center.y + CS_NODES, CS_NODES do
|
||||
for check_z = center.z - CS_NODES, center.z + CS_NODES, CS_NODES do
|
||||
local pos = vector.new(check_x, check_y, check_z)
|
||||
if pos ~= center then
|
||||
minetest_get_voxel_manip():read_from_map(pos, pos)
|
||||
local node = minetest_get_node(pos)
|
||||
if node.name == "ignore" then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function unsigned(v)
|
||||
if v < 0 then
|
||||
v = 0x100000000 - (math.abs(v) % 0x100000000)
|
||||
end
|
||||
return v % 0x100000000
|
||||
end
|
||||
|
||||
local function bitwise_xor_32(a, b)
|
||||
local a = unsigned(a)
|
||||
local b = unsigned(b)
|
||||
local c = 0
|
||||
for n = 31, 0, -1 do
|
||||
local mask = math.floor(2^n)
|
||||
if (a >= mask) ~= (b >= mask) then
|
||||
c = c + mask
|
||||
end
|
||||
a = a % mask
|
||||
b = b % mask
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
local function getBlockSeed2(pos, seed)
|
||||
local seed = seed or mcl_mapgen.seed
|
||||
local n = unsigned(unsigned(1619 * pos.x) + unsigned(31337 * pos.y) + unsigned(52591 * pos.z) + unsigned(1013 * seed))
|
||||
n = bitwise_xor_32(math.floor(n / 0x2000), n)
|
||||
return unsigned((n * unsigned(n * n * 60493 + 19990303) + 1376312589))
|
||||
end
|
||||
|
||||
minetest.register_on_generated(function(minp, maxp, chunkseed)
|
||||
local minp, maxp, chunkseed = minp, maxp, chunkseed
|
||||
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
|
||||
|
@ -154,117 +213,71 @@ minetest.register_on_generated(function(minp, maxp, chunkseed)
|
|||
chunkseed = chunkseed,
|
||||
}
|
||||
|
||||
local current_blocks = {}
|
||||
local current_chunks = {}
|
||||
if safe_functions > 0 then
|
||||
local x0, y0, z0 = minp.x, minp.y, minp.z
|
||||
local bx0, by0, bz0 = math_floor(x0/BS), math_floor(y0/BS), math_floor(z0/BS)
|
||||
local bx1, by1, bz1 = bx0 + LAST_BLOCK, by0 + LAST_BLOCK, bz0 + LAST_BLOCK -- only for entire chunk check
|
||||
|
||||
-- Keep `chunkseed` in `chunks[cx][cy][cz].seed` for further safe usage:
|
||||
local cx0, cy0, cz0 = math_floor((bx0-offset)/CS), math_floor((by0-offset)/CS), math_floor((bz0-offset)/CS)
|
||||
if not chunks[cx0] then chunks[cx0] = {} end
|
||||
if not chunks[cx0][cy0] then chunks[cx0][cy0] = {} end
|
||||
if not chunks[cx0][cy0][cz0] then
|
||||
chunks[cx0][cy0][cz0] = {seed = chunkseed, counter = 0}
|
||||
else
|
||||
chunks[cx0][cy0][cz0].seed = chunkseed
|
||||
end
|
||||
|
||||
local x1, y1, z1, x2, y2, z2 = emin.x, emin.y, emin.z, emax.x, emax.y, emax.z
|
||||
local x, y, z = x1, y1, z1 -- iterate 7x7x7 mapchunk, {x,y,z} - first node pos. of mapblock
|
||||
local bx, by, bz -- block coords (in blocs)
|
||||
local box, boy, boz -- block offsets in chunks (in blocks)
|
||||
while x < x2 do
|
||||
bx = math_floor(x/BS)
|
||||
local block_pos_offset_removed = bx - offset
|
||||
local cx = math_floor(block_pos_offset_removed / CS)
|
||||
box = block_pos_offset_removed % CS
|
||||
if not blocks[bx] then blocks[bx]={} end
|
||||
|
||||
-- We don't know how many calls, including this one, will overwrite this block content!
|
||||
-- Start calculating it with `total_mapgen_block_writes_through_x` variable.
|
||||
-- It can be `8 or less`, if we (speaking of `x` axis) are on chunk edge now,
|
||||
-- or it can be `4 or less` - if we are in the middle of the chunk by `x` axis:
|
||||
|
||||
local total_mapgen_block_writes_through_x = (box > 0 and box < LAST_BLOCK) and 4 or 8
|
||||
while y < y2 do
|
||||
by = math_floor(y/BS)
|
||||
block_pos_offset_removed = by - offset
|
||||
local cy = math_floor(block_pos_offset_removed / CS)
|
||||
boy = block_pos_offset_removed % CS
|
||||
if not blocks[bx][by] then blocks[bx][by]={} end
|
||||
|
||||
-- Here we just divide `total_mapgen_block_writes_through_x` by 2,
|
||||
-- if we are (speaking of `y` axis now) in the middle of the chunk now.
|
||||
-- Or we don't divide it, if not.
|
||||
-- So, basing on `total_mapgen_block_writes_through_x`,
|
||||
--- we calculate `total_mapgen_block_writes_through_y` this way:
|
||||
|
||||
local total_mapgen_block_writes_through_y = (boy > 0 and boy < LAST_BLOCK) and math_floor(total_mapgen_block_writes_through_x / 2) or total_mapgen_block_writes_through_x
|
||||
while z < z2 do
|
||||
bz = math_floor(z/BS)
|
||||
block_pos_offset_removed = bz - offset
|
||||
local cz = math_floor(block_pos_offset_removed / CS)
|
||||
boz = block_pos_offset_removed % CS
|
||||
|
||||
-- Now we do absolutely the same for `z` axis, basing on our previous result
|
||||
-- from `total_mapgen_block_writes_through_y` variable.
|
||||
-- And our final result is in `total_mapgen_block_writes`.
|
||||
-- It can be still 8, derived from `x` calculation, but it can be less!
|
||||
-- It can be even 1, if we are in safe 3x3x3 area of mapchunk:
|
||||
|
||||
local total_mapgen_block_writes = (boz > 0 and boz < LAST_BLOCK) and math_floor(total_mapgen_block_writes_through_y / 2) or total_mapgen_block_writes_through_y
|
||||
|
||||
-- Get current number of writes from the table, or just set it to 1, if accessing first time:
|
||||
|
||||
local current_mapgen_block_writes = blocks[bx][by][bz] and (blocks[bx][by][bz] + 1) or 1
|
||||
|
||||
-- And compare:
|
||||
|
||||
if current_mapgen_block_writes == total_mapgen_block_writes then
|
||||
-- this block shouldn't be overwritten anymore, no need to keep it in memory
|
||||
blocks[bx][by][bz] = nil
|
||||
if not chunks[cx] then chunks[cx] = {} end
|
||||
if not chunks[cx][cy] then chunks[cx][cy] = {} end
|
||||
if not chunks[cx][cy][cz] then
|
||||
if not chunks[cx][cy][cz] then chunks[cx][cy][cz] = {counter = 1} end
|
||||
local ready_blocks = table.copy(all_blocks_in_chunk)
|
||||
local p0 = vector.new(minp)
|
||||
local center = p0 + HALF_CS_NODES
|
||||
for x = -CS_NODES, CS_NODES, CS_NODES do
|
||||
for y = -CS_NODES, CS_NODES, CS_NODES do
|
||||
for z = -CS_NODES, CS_NODES, CS_NODES do
|
||||
if x ~= 0 or y ~= 0 or z ~= 0 then
|
||||
local offset = vector.new(x, y, z)
|
||||
local pos = center + offset
|
||||
minetest_get_voxel_manip():read_from_map(pos, pos)
|
||||
local node = minetest_get_node(pos)
|
||||
local is_generated = node.name ~= "ignore"
|
||||
if is_generated then
|
||||
local adjacent_chunk_pos = p0 + offset
|
||||
if is_chunk_finished(adjacent_chunk_pos) then
|
||||
current_chunks[#current_chunks + 1] = adjacent_chunk_pos
|
||||
end
|
||||
else
|
||||
chunks[cx][cy][cz].counter = chunks[cx][cy][cz].counter + 1
|
||||
if chunks[cx][cy][cz].counter >= CS_3D then
|
||||
current_chunks[#current_chunks+1] = { x = cx, y = cy, z = cz, s = chunks[cx][cy][cz].seed }
|
||||
-- this chunk shouldn't be overwritten anymore, no need to keep it in memory
|
||||
chunks[cx][cy][cz] = nil
|
||||
if next(chunks[cx][cy]) == nil then chunks[cx][cy] = nil end
|
||||
if next(chunks[cx]) == nil then chunks[cx] = nil end
|
||||
local scan_range_x = chunk_scan_range[x]
|
||||
for cut_x = scan_range_x[1], scan_range_x[2] do
|
||||
local scan_range_y = chunk_scan_range[y]
|
||||
for cut_y = scan_range_y[1], scan_range_y[2] do
|
||||
local scan_range_z = chunk_scan_range[z]
|
||||
for cut_z = scan_range_z[1], scan_range_z[2] do
|
||||
ready_blocks[CS * (CS * cut_x + cut_y) + cut_z] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local blockseed = seed + bx * 7 + by * 243 + bz * 11931
|
||||
if queue_blocks_lvm_counter > 0 then
|
||||
vm_context.blockseed = blockseed
|
||||
vm_context.minp, vm_context.maxp = {x=x, y=y, z=z}, {x=x+LAST_NODE, y=y+LAST_NODE, z=z+LAST_NODE}
|
||||
for _, v in pairs(queue_blocks_lvm) do
|
||||
v.callback_function(vm_context)
|
||||
end
|
||||
end
|
||||
if nodes_block > 0 then
|
||||
current_blocks[#current_blocks+1] = { minp = {x=x, y=y, z=z}, maxp = {x=x+LAST_NODE, y=y+LAST_NODE, z=z+LAST_NODE}, seed = blockseed }
|
||||
end
|
||||
else
|
||||
blocks[bx][by][bz] = current_mapgen_block_writes
|
||||
end
|
||||
z = z + BS
|
||||
end
|
||||
if next(blocks[bx][by]) == nil then blocks[bx][by] = nil end
|
||||
z = z1
|
||||
y = y + BS
|
||||
end
|
||||
if next(blocks[bx]) == nil then blocks[bx] = nil end
|
||||
y = y1
|
||||
x = x + BS
|
||||
end
|
||||
local number_of_blocks = 0
|
||||
for k, offset in pairs(ready_blocks) do
|
||||
if queue_blocks_lvm_counter > 0 then
|
||||
local block_minp = p0 + offset * BS
|
||||
local block_maxp = block_minp + LAST_NODE_IN_BLOCK
|
||||
local blockseed = getBlockSeed2(block_minp)
|
||||
vm_context.minp, vm_context.maxp, vm_context.blockseed = block_minp, block_maxp, blockseed
|
||||
-- --
|
||||
-- mcl_mapgen.register_mapgen_block_lvm(function(vm_context), order_number) --
|
||||
-- --
|
||||
for _, v in pairs(queue_blocks_lvm) do
|
||||
v.callback_function(vm_context)
|
||||
end
|
||||
if nodes_block > 0 then
|
||||
current_blocks[#current_blocks + 1] = { minp = block_minp, maxp = block_maxp, seed = blockseed }
|
||||
end
|
||||
end
|
||||
number_of_blocks = number_of_blocks + 1
|
||||
end
|
||||
if number_of_blocks == CS_3D then
|
||||
current_chunks[#current_chunks + 1] = p0
|
||||
end
|
||||
end
|
||||
|
||||
if #queue_unsafe_engine > 0 then
|
||||
vm_context.minp, vm_context.maxp = minp, maxp
|
||||
-- * U N S A F E --
|
||||
-- mcl_mapgen.register_on_generated(function(vm_context), order_number) --
|
||||
-- * U N S A F E --
|
||||
for _, v in pairs(queue_unsafe_engine) do
|
||||
v.f(vm_context)
|
||||
end
|
||||
|
@ -286,12 +299,9 @@ minetest.register_on_generated(function(minp, maxp, chunkseed)
|
|||
end
|
||||
end
|
||||
|
||||
for i, b in pairs(current_chunks) do
|
||||
local cx, cy, cz, seed = b.x, b.y, b.z, b.s
|
||||
local bx, by, bz = cx * CS + offset, cy * CS + offset, cz * CS + offset
|
||||
local x, y, z = bx * BS, by * BS, bz * BS
|
||||
local minp = {x = x, y = y, z = z}
|
||||
local maxp = {x = x + CS_NODES - 1, y = y + CS_NODES - 1, z = z + CS_NODES - 1}
|
||||
for i, chunk_minp in pairs(current_chunks) do
|
||||
local chunk_maxp = chunk_minp + LAST_NODE_IN_CHUNK
|
||||
local chunkseed = getBlockSeed2(chunk_minp)
|
||||
area = VoxelArea:new({MinEdge=minp, MaxEdge=maxp})
|
||||
vm_context = {
|
||||
data = data,
|
||||
|
@ -301,15 +311,21 @@ minetest.register_on_generated(function(minp, maxp, chunkseed)
|
|||
lvm_buffer = lvm_buffer,
|
||||
lvm_param2_buffer = lvm_param2_buffer,
|
||||
lvm_light_buffer = lvm_light_buffer,
|
||||
emin = minp,
|
||||
emax = maxp,
|
||||
minp = minp,
|
||||
maxp = maxp,
|
||||
chunkseed = seed,
|
||||
emin = chunk_minp,
|
||||
emax = chunk_maxp,
|
||||
minp = chunk_minp,
|
||||
maxp = chunk_maxp,
|
||||
chunkseed = chunkseedseed,
|
||||
}
|
||||
-- --
|
||||
-- mcl_mapgen.register_mapgen_lvm(function(vm_context), order_number) --
|
||||
-- --
|
||||
for _, v in pairs(queue_chunks_lvm) do
|
||||
vm_context = v.f(vm_context)
|
||||
end
|
||||
-- --
|
||||
-- mcl_mapgen.register_mapgen(function(minp, maxp, chunkseed, vm_context), order_number) --
|
||||
-- --
|
||||
for _, v in pairs(queue_chunks_nodes) do
|
||||
v.f(minp, maxp, seed, vm_context)
|
||||
end
|
||||
|
@ -330,14 +346,15 @@ minetest.register_on_generated(function(minp, maxp, chunkseed)
|
|||
elseif vm_context.calc_lighting then
|
||||
vm:calc_lighting(minp, maxp, (vm_context.shadow ~= nil) or true)
|
||||
end
|
||||
current_chunks[i] = nil
|
||||
end
|
||||
|
||||
for i, b in pairs(current_blocks) do
|
||||
-- --
|
||||
-- mcl_mapgen.register_mapgen_block(function(minp, maxp, blockseed), order_number) --
|
||||
-- --
|
||||
for _, v in pairs(queue_blocks_nodes) do
|
||||
v.f(b.minp, b.maxp, b.seed)
|
||||
end
|
||||
current_blocks[i] = nil
|
||||
end
|
||||
end)
|
||||
|
||||
|
@ -391,16 +408,6 @@ mcl_mapgen.minecraft_height_limit = 256
|
|||
|
||||
mcl_mapgen.bedrock_is_rough = normal
|
||||
|
||||
--[[ Realm stacking (h is for height)
|
||||
- Overworld (h>=256)
|
||||
- Void (h>=1000)
|
||||
- Realm Barrier (h=11), to allow escaping the End
|
||||
- End (h>=256)
|
||||
- Void (h>=1000)
|
||||
- Nether (h=128)
|
||||
- Void (h>=1000)
|
||||
]]
|
||||
|
||||
-- Overworld
|
||||
overworld.min = -62
|
||||
if superflat then
|
||||
|
|
Loading…
Reference in New Issue