Move generator code to functions to aid in profiling

This commit is contained in:
ancientmarinerdev 2023-04-16 17:08:48 +01:00 committed by Gitea
parent 01ac9ad685
commit 31b65bac91
1 changed files with 43 additions and 25 deletions

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