Merge pull request 'Mapgen Performance Improvements' (#3626) from mapgen_performance_improvements into master

Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/3626
Reviewed-by: ancientmarinerdev <ancientmariner_dev@proton.me>
This commit is contained in:
ancientmarinerdev 2023-04-16 17:02:31 +00:00
commit 90b9263f03
3 changed files with 179 additions and 94 deletions

View file

@ -149,28 +149,3 @@ end
if mod_doc then if mod_doc then
doc.add_entry_alias("nodes", "mcl_ocean:seagrass_dirt", "craftitems", "mcl_ocean:seagrass") doc.add_entry_alias("nodes", "mcl_ocean:seagrass_dirt", "craftitems", "mcl_ocean:seagrass")
end end
minetest.register_lbm({
label = "Fix incorrect seagrass",
name = "mcl_ocean:fix_incorrect_seagrass",
nodenames = {"group:seagrass"},
run_at_every_load = false,
action = function(pos, node)
if node.param2 ~= 3 then
node.param2 = 3
minetest.set_node(pos, node)
end
end
})
minetest.register_on_generated(function(minp, maxp, blockseed)
local seagrass = minetest.find_nodes_in_area(minp, maxp, {"group:seagrass"})
for _, sgpos in pairs(seagrass) do
local sgnode = minetest.get_node(sgpos)
if sgnode.param2 ~= 3 then
sgnode.param2 = 3
minetest.set_node(sgpos, sgnode)
end
end
end
)

View file

@ -11,6 +11,43 @@ local function roundN(n, d)
return math.floor(n * m + 0.5) / m return math.floor(n * m + 0.5) / m
end end
local function run_generators (p1, p2, blockseed)
if nodes > 0 then
for _, rec in ipairs(registered_generators) do
if rec.nf then
rec.nf(p1, p2, blockseed)
end
end
end
end
local function update_data (vm, data, data2)
-- Write stuff
vm:set_data(data)
if param2 > 0 then
vm:set_param2_data(data2)
end
end
local function post_generator_processing(vm, minp, maxp, deco_used, deco_table, ore_used, ore_table)
if deco_table then
minetest.generate_decorations(vm,vector.new(minp.x,deco_table.min,minp.z),vector.new(maxp.x,deco_table.max,maxp.z))
elseif deco_used then
minetest.generate_decorations(vm)
end
if ore_table then
minetest.generate_ores(vm,vector.new(minp.x,ore_table.min,minp.z),vector.new(maxp.x,ore_table.max,maxp.z))
elseif ore_used then
minetest.generate_ores(vm)
end
end
local function post_generator_processing_2(vm, p1, p2, shadow)
vm:calc_lighting(p1, p2, shadow)
vm:write_to_map()
vm:update_liquids()
end
minetest.register_on_generated(function(minp, maxp, blockseed) minetest.register_on_generated(function(minp, maxp, blockseed)
local t1 = os.clock() local t1 = os.clock()
local p1, p2 = {x=minp.x, y=minp.y, z=minp.z}, {x=maxp.x, y=maxp.y, z=maxp.z} local p1, p2 = {x=minp.x, y=minp.y, z=minp.z}, {x=maxp.x, y=maxp.y, z=maxp.z}
@ -49,34 +86,13 @@ minetest.register_on_generated(function(minp, maxp, blockseed)
end end
if lvm_used then if lvm_used then
-- Write stuff update_data (vm, data, data2)
vm:set_data(data) post_generator_processing(vm, minp, maxp, deco_used, deco_table, ore_used, ore_table)
if param2 > 0 then post_generator_processing_2(vm, p1, p2, shadow)
vm:set_param2_data(data2)
end
if deco_table then
minetest.generate_decorations(vm,vector.new(minp.x,deco_table.min,minp.z),vector.new(maxp.x,deco_table.max,maxp.z))
elseif deco_used then
minetest.generate_decorations(vm)
end
if ore_table then
minetest.generate_ores(vm,vector.new(minp.x,ore_table.min,minp.z),vector.new(maxp.x,ore_table.max,maxp.z))
elseif ore_used then
minetest.generate_ores(vm)
end
vm:calc_lighting(p1, p2, shadow)
vm:write_to_map()
vm:update_liquids()
end end
end end
if nodes > 0 then run_generators (p1, p2, blockseed)
for _, rec in ipairs(registered_generators) do
if rec.nf then
rec.nf(p1, p2, blockseed)
end
end
end
mcl_vars.add_chunk(minp) mcl_vars.add_chunk(minp)
if logging then if logging then
@ -84,6 +100,8 @@ minetest.register_on_generated(function(minp, maxp, blockseed)
end end
end) end)
function minetest.register_on_generated(node_function) function minetest.register_on_generated(node_function)
mcl_mapgen_core.register_generator("mod_"..minetest.get_current_modname().."_"..tostring(#registered_generators+1), nil, node_function) mcl_mapgen_core.register_generator("mod_"..minetest.get_current_modname().."_"..tostring(#registered_generators+1), nil, node_function)
end end

View file

@ -290,6 +290,63 @@ local function set_grass_palette(minp,maxp,data2,area,biomemap,nodes)
return lvm_used return lvm_used
end end
local function set_foliage_palette(minp,maxp,data2,area,biomemap,nodes)
-- Flat area at y=0 to read biome 3 times faster than 5.3.0.get_biome_data(pos).biome: 43us vs 125us per iteration:
if not biomemap then return end
local aream = VoxelArea:new({MinEdge={x=minp.x, y=0, z=minp.z}, MaxEdge={x=maxp.x, y=0, z=maxp.z}})
local nodes = minetest.find_nodes_in_area(minp, maxp, nodes)
for n=1, #nodes do
local n = nodes[n]
local p_pos = area:index(n.x, n.y, n.z)
local b_pos = aream:index(n.x, 0, n.z)
local bn = minetest.get_biome_name(biomemap[b_pos])
if bn then
local biome = minetest.registered_biomes[bn]
if biome and biome._mcl_biome_type and biome._mcl_foliage_palette_index and data2[p_pos] <= 1 then
data2[p_pos] = biome._mcl_foliage_palette_index
lvm_used = true
elseif biome and biome._mcl_biome_type and biome._mcl_foliage_palette_index and data2[p_pos] > 1 then
data2[p_pos] = (biome._mcl_foliage_palette_index * 8) + data2[p_pos]
lvm_used = true
end
end
end
return lvm_used
end
local function set_water_palette(minp,maxp,data2,area,biomemap,nodes)
-- Flat area at y=0 to read biome 3 times faster than 5.3.0.get_biome_data(pos).biome: 43us vs 125us per iteration:
if not biomemap then return end
local aream = VoxelArea:new({MinEdge={x=minp.x, y=0, z=minp.z}, MaxEdge={x=maxp.x, y=0, z=maxp.z}})
local nodes = minetest.find_nodes_in_area(minp, maxp, nodes)
for n=1, #nodes do
local n = nodes[n]
local p_pos = area:index(n.x, n.y, n.z)
local b_pos = aream:index(n.x, 0, n.z)
local bn = minetest.get_biome_name(biomemap[b_pos])
if bn then
local biome = minetest.registered_biomes[bn]
if biome and biome._mcl_biome_type and biome._mcl_water_palette_index then
data2[p_pos] = biome._mcl_water_palette_index
lvm_used = true
end
end
end
return lvm_used
end
local function set_seagrass_param2(minp,maxp,data2,area,biomemap,nodes)
local aream = VoxelArea:new({MinEdge={x=minp.x, y=0, z=minp.z}, MaxEdge={x=maxp.x, y=0, z=maxp.z}})
local nodes = minetest.find_nodes_in_area(minp, maxp, nodes)
for n=1, #nodes do
local n = nodes[n]
local p_pos = area:index(n.x, n.y, n.z)
data2[p_pos] = 3
lvm_used = true
end
return lvm_used
end
-- Below the bedrock, generate air/void -- Below the bedrock, generate air/void
local function world_structure(vm, data, data2, emin, emax, area, minp, maxp, blockseed) local function world_structure(vm, data, data2, emin, emax, area, minp, maxp, blockseed)
local biomemap --ymin, ymax local biomemap --ymin, ymax
@ -346,23 +403,47 @@ local function world_structure(vm, data, data2, emin, emax, area, minp, maxp, bl
end end
local function block_fixes_grass(vm, data, data2, emin, emax, area, minp, maxp, blockseed) local function block_fixes_grass(vm, data, data2, emin, emax, area, minp, maxp, blockseed)
if maxp.y < mcl_vars.mg_overworld_min then local biomemap = minetest.get_mapgen_object("biomemap")
--minetest.log("Exit grass fix") local lvm_used = false
return local pr = PseudoRandom(blockseed)
else if minp.y <= mcl_vars.mg_overworld_max and maxp.y >= mcl_vars.mg_overworld_min then
--minetest.log("Grass fixes") -- Set param2 (=color) of nodes which use the grass colour palette.
end lvm_used = set_grass_palette(minp,maxp,data2,area,biomemap,{"group:grass_palette"})
end
local biomemap = minetest.get_mapgen_object("biomemap") return lvm_used
local lvm_used = false
if minp.y <= mcl_vars.mg_overworld_max and maxp.y >= mcl_vars.mg_overworld_min then
-- Set param2 (=color) of nodes which use the grass colour palette.
lvm_used = set_grass_palette(minp,maxp,data2,area,biomemap,{"group:grass_palette"})
end
return lvm_used
end end
local function block_fixes_foliage(vm, data, data2, emin, emax, area, minp, maxp, blockseed)
local biomemap = minetest.get_mapgen_object("biomemap")
local lvm_used = false
local pr = PseudoRandom(blockseed)
if minp.y <= mcl_vars.mg_overworld_max and maxp.y >= mcl_vars.mg_overworld_min then
-- Set param2 (=color) of nodes which use the foliage colour palette.
lvm_used = set_foliage_palette(minp,maxp,data2,area,biomemap,{"group:foliage_palette", "group:foliage_palette_wallmounted"})
end
return lvm_used
end
local function block_fixes_water(vm, data, data2, emin, emax, area, minp, maxp, blockseed)
local biomemap = minetest.get_mapgen_object("biomemap")
local lvm_used = false
local pr = PseudoRandom(blockseed)
if minp.y <= mcl_vars.mg_overworld_max and maxp.y >= mcl_vars.mg_overworld_min then
-- Set param2 (=color) of nodes which use the water colour palette.
lvm_used = set_water_palette(minp,maxp,data2,area,biomemap,{"group:water_palette"})
end
return lvm_used
end
local function block_fixes_seagrass(vm, data, data2, emin, emax, area, minp, maxp, blockseed)
local lvm_used = false
local pr = PseudoRandom(blockseed)
if minp.y <= mcl_vars.mg_overworld_max and maxp.y >= mcl_vars.mg_overworld_min then
-- Set param2 of seagrass to 3.
lvm_used = set_seagrass_param2(minp,maxp,data2,area,biomemap,{"group:seagrass"})
end
return lvm_used
end
-- End block fixes: -- End block fixes:
local function end_basic(vm, data, data2, emin, emax, area, minp, maxp, blockseed) local function end_basic(vm, data, data2, emin, emax, area, minp, maxp, blockseed)
@ -391,6 +472,9 @@ end, 9999, true)
if mg_name ~= "v6" and mg_name ~= "singlenode" then if mg_name ~= "v6" and mg_name ~= "singlenode" then
mcl_mapgen_core.register_generator("block_fixes_grass", block_fixes_grass, nil, 9999, true) mcl_mapgen_core.register_generator("block_fixes_grass", block_fixes_grass, nil, 9999, true)
mcl_mapgen_core.register_generator("block_fixes_foliage", block_fixes_foliage, nil, 9999, true)
mcl_mapgen_core.register_generator("block_fixes_water", block_fixes_water, nil, 9999, true)
mcl_mapgen_core.register_generator("block_fixes_seagrass", block_fixes_seagrass, nil, 9999, true)
end end
if mg_name == "v6" then if mg_name == "v6" then
@ -467,8 +551,38 @@ minetest.register_lbm({
end end
}) })
minetest.register_on_generated(function(minp, maxp, blockseed) -- Set correct palette indexes of foliage in new mapblocks. minetest.register_lbm({
local pos1, pos2 = vector.offset(minp, -16, -16, -16), vector.offset(maxp, 16, 16, 16) label = "Fix water palette indexes", -- Set correct palette indexes of water in old mapblocks.
name = "mcl_mapgen_core:fix_water_palette_indexes",
nodenames = {"group:water_palette"},
run_at_every_load = false,
action = function(pos, node)
local water_palette_index = mcl_util.get_palette_indexes_from_pos(pos).water_palette_index
if node.param2 ~= water_palette_index then
node.param2 = water_palette_index
minetest.set_node(pos, node)
end
end
})
minetest.register_lbm({
label = "Fix incorrect seagrass", -- Set correct param2 of seagrass in old mapblocks.
name = "mcl_mapgen_core:fix_incorrect_seagrass",
nodenames = {"group:seagrass"},
run_at_every_load = false,
action = function(pos, node)
if node.param2 ~= 3 then
node.param2 = 3
minetest.set_node(pos, node)
end
end
})
-- We go outside x and y for where trees are placed next to a biome that has already been generated.
-- We go above maxp.y because trees can often get placed close to the top of a generated area and folliage may not
-- be coloured correctly.
local function fix_folliage_missed (minp, maxp)
local pos1, pos2 = vector.offset(minp, -6, 0, -6), vector.offset(maxp, 6, 14, 6)
local foliage = minetest.find_nodes_in_area(pos1, pos2, {"group:foliage_palette", "group:foliage_palette_wallmounted"}) local foliage = minetest.find_nodes_in_area(pos1, pos2, {"group:foliage_palette", "group:foliage_palette_wallmounted"})
for _, fpos in pairs(foliage) do for _, fpos in pairs(foliage) do
local fnode = minetest.get_node(fpos) local fnode = minetest.get_node(fpos)
@ -487,32 +601,10 @@ minetest.register_on_generated(function(minp, maxp, blockseed) -- Set correct pa
end end
end end
end end
)
minetest.register_lbm({ minetest.register_on_generated(function(minp, maxp, blockseed) -- Set correct palette indexes of missed foliage.
label = "Fix water palette indexes", -- Set correct palette indexes of water in old mapblocks. if maxp.y < 0 then
name = "mcl_mapgen_core:fix_water_palette_indexes", return
nodenames = {"group:water_palette"},
run_at_every_load = false,
action = function(pos, node)
local water_palette_index = mcl_util.get_palette_indexes_from_pos(pos).water_palette_index
if node.param2 ~= water_palette_index then
node.param2 = water_palette_index
minetest.set_node(pos, node)
end
end end
}) fix_folliage_missed (minp, maxp)
end)
minetest.register_on_generated(function(minp, maxp, blockseed) -- Set correct palette indexes of water in new mapblocks.
local pos1, pos2 = vector.offset(minp, -16, -16, -16), vector.offset(maxp, 16, 16, 16)
local water = minetest.find_nodes_in_area(pos1, pos2, {"group:water_palette"})
for _, wpos in pairs(water) do
local wnode = minetest.get_node(wpos)
local water_palette_index = mcl_util.get_palette_indexes_from_pos(wpos).water_palette_index
if wnode.param2 ~= water_palette_index then
wnode.param2 = water_palette_index
minetest.set_node(wpos, wnode)
end
end
end
)