Update Fork #1

Merged
chmodsayshello merged 696 commits from MineClone5/MineClone5:master into master 2022-03-23 14:40:26 +01:00
1 changed files with 160 additions and 148 deletions
Showing only changes of commit ad188e8474 - Show all commits

View File

@ -22,10 +22,17 @@ local minetest_pos_to_string = minetest.pos_to_string
mcl_mapgen.CS = math_max(1, tonumber(minetest.get_mapgen_setting("chunksize")) or 5) 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.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.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.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 = - math_floor(mcl_mapgen.CS / 2)
mcl_mapgen.OFFSET_NODES = mcl_mapgen.OFFSET * mcl_mapgen.BS mcl_mapgen.OFFSET_NODES = mcl_mapgen.OFFSET * mcl_mapgen.BS
mcl_mapgen.CS_NODES = mcl_mapgen.CS * 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
mcl_mapgen.CHUNK_WITH_SHELL = mcl_mapgen.CS + 2
mcl_mapgen.CHUNK_WITH_SHELL_3D = mcl_mapgen.CHUNK_WITH_SHELL^3
local central_chunk_min_pos = mcl_mapgen.OFFSET * mcl_mapgen.BS 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 local central_chunk_max_pos = central_chunk_min_pos + mcl_mapgen.CS_NODES - 1
@ -78,11 +85,15 @@ local nodes_chunk = 0
local safe_functions = 0 local safe_functions = 0
local BS, CS = mcl_mapgen.BS, mcl_mapgen.CS -- Mapblock size (in nodes), Mapchunk size (in blocks) 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 offset = mcl_mapgen.OFFSET -- Central mapchunk offset (in blocks)
local CS_NODES = mcl_mapgen.CS_NODES -- 80 local CS_NODES = mcl_mapgen.CS_NODES
local LAST_BLOCK = mcl_mapgen.LAST_BLOCK
local CS_3D = CS * CS * CS 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 CHUNK_WITH_SHELL = mcl_mapgen.CHUNK_WITH_SHELL
local CHUNK_WITH_SHELL_3D = mcl_mapgen.CHUNK_WITH_SHELL_3D
local DEFAULT_ORDER = order.DEFAULT local DEFAULT_ORDER = order.DEFAULT
@ -117,24 +128,77 @@ function mcl_mapgen.register_mapgen_block_lvm(callback_function, order)
table.sort(queue_blocks_lvm, function(a, b) return (a.order <= b.order) end) table.sort(queue_blocks_lvm, function(a, b) return (a.order <= b.order) end)
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 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 data, param2_data, light, area
local current_blocks = {}
local current_chunks = {}
local lvm_buffer, lvm_param2_buffer, lvm_light_buffer = {}, {}, {} -- Static buffer pointers local lvm_buffer, lvm_param2_buffer, lvm_light_buffer = {}, {}, {} -- Static buffer pointers
local all_blocks_in_chunk = {}
for x = -1, LAST_BLOCK+1 do
for y = -1, LAST_BLOCK+1 do
for z = -1, LAST_BLOCK+1 do
all_blocks_in_chunk[CHUNK_WITH_SHELL * (CHUNK_WITH_SHELL * y + z) + x] = vector.new(x, y, z)
end
end
end
local chunk_scan_range = {
[-CS_NODES] = {-1 , -1 },
[ 0 ] = {-1 , LAST_BLOCK+1},
[ CS_NODES] = {LAST_BLOCK+1, LAST_BLOCK+1},
}
local function is_chunk_finished(minp)
local center = vector.add(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
return true
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) minetest.register_on_generated(function(minp, maxp, chunkseed)
local minp, maxp, chunkseed = minp, maxp, chunkseed local minp, maxp, chunkseed = minp, maxp, chunkseed
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip") local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
minetest_log("warning", "[mcl_mapgen] New_chunk=" .. minetest_pos_to_string(minp) .. "..." .. minetest_pos_to_string(maxp) .. ", shell=" .. minetest_pos_to_string(emin) .. "..." .. minetest_pos_to_string(emax) .. ", chunkseed=" .. tostring(chunkseed)) minetest_log("action", "[mcl_mapgen] New_chunk=" .. minetest_pos_to_string(minp) .. "..." .. minetest_pos_to_string(maxp) .. ", shell=" .. minetest_pos_to_string(emin) .. "..." .. minetest_pos_to_string(emax) .. ", chunkseed=" .. tostring(chunkseed))
data = vm:get_data(lvm_buffer) data = vm:get_data(lvm_buffer)
area = VoxelArea:new({MinEdge=emin, MaxEdge=emax}) area = VoxelArea:new({MinEdge=emin, MaxEdge=emax})
@ -154,117 +218,71 @@ minetest.register_on_generated(function(minp, maxp, chunkseed)
chunkseed = chunkseed, chunkseed = chunkseed,
} }
local current_blocks = {}
local current_chunks = {}
if safe_functions > 0 then if safe_functions > 0 then
local x0, y0, z0 = minp.x, minp.y, minp.z local ready_blocks = table.copy(all_blocks_in_chunk)
local bx0, by0, bz0 = math_floor(x0/BS), math_floor(y0/BS), math_floor(z0/BS) local p0 = vector.new(minp)
local bx1, by1, bz1 = bx0 + LAST_BLOCK, by0 + LAST_BLOCK, bz0 + LAST_BLOCK -- only for entire chunk check local center = vector.add(p0, HALF_CS_NODES)
for x = -CS_NODES, CS_NODES, CS_NODES do
-- Keep `chunkseed` in `chunks[cx][cy][cz].seed` for further safe usage: for y = -CS_NODES, CS_NODES, CS_NODES do
local cx0, cy0, cz0 = math_floor((bx0-offset)/CS), math_floor((by0-offset)/CS), math_floor((bz0-offset)/CS) for z = -CS_NODES, CS_NODES, CS_NODES do
if not chunks[cx0] then chunks[cx0] = {} end if x ~= 0 or y ~= 0 or z ~= 0 then
if not chunks[cx0][cy0] then chunks[cx0][cy0] = {} end local offset = vector.new(x, y, z)
if not chunks[cx0][cy0][cz0] then local pos = center + offset
chunks[cx0][cy0][cz0] = {seed = chunkseed, counter = 0} 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 else
chunks[cx0][cy0][cz0].seed = chunkseed local scan_range_x = chunk_scan_range[x]
end for cut_x = scan_range_x[1], scan_range_x[2] do
local scan_range_y = chunk_scan_range[y]
local x1, y1, z1, x2, y2, z2 = emin.x, emin.y, emin.z, emax.x, emax.y, emax.z for cut_y = scan_range_y[1], scan_range_y[2] do
local x, y, z = x1, y1, z1 -- iterate 7x7x7 mapchunk, {x,y,z} - first node pos. of mapblock local scan_range_z = chunk_scan_range[z]
local bx, by, bz -- block coords (in blocs) for cut_z = scan_range_z[1], scan_range_z[2] do
local box, boy, boz -- block offsets in chunks (in blocks) ready_blocks[CHUNK_WITH_SHELL * (CHUNK_WITH_SHELL * cut_y + cut_z) + cut_x] = nil
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
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
end end
end end
local blockseed = seed + bx * 7 + by * 243 + bz * 11931 end
end
end
end
end
end
local number_of_blocks = 0
for k, offset in pairs(ready_blocks) do
if queue_blocks_lvm_counter > 0 then if queue_blocks_lvm_counter > 0 then
vm_context.blockseed = blockseed local block_minp = p0 + vector.multiply(offset, BS)
vm_context.minp, vm_context.maxp = {x=x, y=y, z=z}, {x=x+LAST_NODE, y=y+LAST_NODE, z=z+LAST_NODE} local block_maxp = vector.add(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 for _, v in pairs(queue_blocks_lvm) do
v.callback_function(vm_context) v.callback_function(vm_context)
end end
end
if nodes_block > 0 then 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 } current_blocks[#current_blocks + 1] = { minp = block_minp, maxp = block_maxp, seed = blockseed }
end end
else
blocks[bx][by][bz] = current_mapgen_block_writes
end end
z = z + BS number_of_blocks = number_of_blocks + 1
end end
if next(blocks[bx][by]) == nil then blocks[bx][by] = nil end if number_of_blocks == CHUNK_WITH_SHELL_3D then
z = z1 current_chunks[#current_chunks + 1] = p0
y = y + BS
end
if next(blocks[bx]) == nil then blocks[bx] = nil end
y = y1
x = x + BS
end end
end end
if #queue_unsafe_engine > 0 then if #queue_unsafe_engine > 0 then
vm_context.minp, vm_context.maxp = minp, maxp 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 for _, v in pairs(queue_unsafe_engine) do
v.f(vm_context) v.f(vm_context)
end end
@ -278,7 +296,7 @@ minetest.register_on_generated(function(minp, maxp, chunkseed)
vm:set_light_data(light) vm:set_light_data(light)
end end
if vm_context.write or vm_context.write_param2 or vm_context.write_light then if vm_context.write or vm_context.write_param2 or vm_context.write_light then
vm:calc_lighting(minp, maxp, (vm_context.shadow ~= nil) or true) -- TODO: check boundaries vm:calc_lighting(minp, maxp, (vm_context.shadow ~= nil) or true)
vm:write_to_map() vm:write_to_map()
vm:update_liquids() vm:update_liquids()
elseif vm_context.calc_lighting then elseif vm_context.calc_lighting then
@ -286,12 +304,9 @@ minetest.register_on_generated(function(minp, maxp, chunkseed)
end end
end end
for i, b in pairs(current_chunks) do for i, chunk_minp in pairs(current_chunks) do
local cx, cy, cz, seed = b.x, b.y, b.z, b.s local chunk_maxp = vector.add(chunk_minp, LAST_NODE_IN_CHUNK)
local bx, by, bz = cx * CS + offset, cy * CS + offset, cz * CS + offset local chunkseed = getBlockSeed2(chunk_minp)
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}
area = VoxelArea:new({MinEdge=minp, MaxEdge=maxp}) area = VoxelArea:new({MinEdge=minp, MaxEdge=maxp})
vm_context = { vm_context = {
data = data, data = data,
@ -301,17 +316,23 @@ minetest.register_on_generated(function(minp, maxp, chunkseed)
lvm_buffer = lvm_buffer, lvm_buffer = lvm_buffer,
lvm_param2_buffer = lvm_param2_buffer, lvm_param2_buffer = lvm_param2_buffer,
lvm_light_buffer = lvm_light_buffer, lvm_light_buffer = lvm_light_buffer,
emin = minp, emin = chunk_minp,
emax = maxp, emax = chunk_maxp,
minp = minp, minp = chunk_minp,
maxp = maxp, maxp = chunk_maxp,
chunkseed = seed, chunkseed = chunkseedseed,
} }
-- --
-- mcl_mapgen.register_mapgen_lvm(function(vm_context), order_number) --
-- --
for _, v in pairs(queue_chunks_lvm) do for _, v in pairs(queue_chunks_lvm) do
vm_context = v.f(vm_context) vm_context = v.f(vm_context)
end end
-- --
-- mcl_mapgen.register_mapgen(function(minp, maxp, chunkseed, vm_context), order_number) --
-- --
for _, v in pairs(queue_chunks_nodes) do for _, v in pairs(queue_chunks_nodes) do
v.f(minp, maxp, seed, vm_context) v.f(chunk_minp, chunk_maxp, chunkseed, vm_context)
end end
if vm_context.write or vm_context.write_param2 or vm_context.write_light then if vm_context.write or vm_context.write_param2 or vm_context.write_light then
if vm_context.write then if vm_context.write then
@ -330,14 +351,15 @@ minetest.register_on_generated(function(minp, maxp, chunkseed)
elseif vm_context.calc_lighting then elseif vm_context.calc_lighting then
vm:calc_lighting(minp, maxp, (vm_context.shadow ~= nil) or true) vm:calc_lighting(minp, maxp, (vm_context.shadow ~= nil) or true)
end end
current_chunks[i] = nil
end end
for i, b in pairs(current_blocks) do 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 for _, v in pairs(queue_blocks_nodes) do
v.f(b.minp, b.maxp, b.seed) v.f(b.minp, b.maxp, b.seed)
end end
current_blocks[i] = nil
end end
end) end)
@ -391,16 +413,6 @@ mcl_mapgen.minecraft_height_limit = 256
mcl_mapgen.bedrock_is_rough = normal 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
overworld.min = -62 overworld.min = -62
if superflat then if superflat then