mirror of
https://gitlab.com/gaelysam/mapgen_rivers.git
synced 2025-02-22 06:50:29 +01:00
Added time statistics and removed debug prints
This commit is contained in:
parent
5898354dbe
commit
ecd1f0e08f
19
init.lua
19
init.lua
@ -38,7 +38,13 @@ local noise_heat_blend_map = {}
|
|||||||
local mapsize
|
local mapsize
|
||||||
local init = false
|
local init = false
|
||||||
|
|
||||||
|
local sumtime = 0
|
||||||
|
local sumtime2 = 0
|
||||||
|
local ngen = 0
|
||||||
|
|
||||||
local function generate(minp, maxp, seed)
|
local function generate(minp, maxp, seed)
|
||||||
|
print(("[mapgen_rivers] Generating from %s to %s"):format(minetest.pos_to_string(minp), minetest.pos_to_string(maxp)))
|
||||||
|
|
||||||
local chulens = {
|
local chulens = {
|
||||||
x = maxp.x-minp.x+1,
|
x = maxp.x-minp.x+1,
|
||||||
y = maxp.y-minp.y+1,
|
y = maxp.y-minp.y+1,
|
||||||
@ -63,6 +69,7 @@ local function generate(minp, maxp, seed)
|
|||||||
init = true
|
init = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local t0 = os.clock()
|
||||||
local minp2d = {x=minp.x, y=minp.z}
|
local minp2d = {x=minp.x, y=minp.z}
|
||||||
if use_distort then
|
if use_distort then
|
||||||
noise_x_obj:get_3d_map_flat(minp, noise_x_map)
|
noise_x_obj:get_3d_map_flat(minp, noise_x_map)
|
||||||
@ -221,6 +228,18 @@ local function generate(minp, maxp, seed)
|
|||||||
vm:calc_lighting()
|
vm:calc_lighting()
|
||||||
vm:update_liquids()
|
vm:update_liquids()
|
||||||
vm:write_to_map()
|
vm:write_to_map()
|
||||||
|
local t1 = os.clock()
|
||||||
|
|
||||||
|
local t = t1-t0
|
||||||
|
ngen = ngen + 1
|
||||||
|
sumtime = sumtime + t
|
||||||
|
sumtime2 = sumtime2 + t*t
|
||||||
|
print(("[mapgen_rivers] Done in %5.3f s"):format(t))
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_generated(generate)
|
minetest.register_on_generated(generate)
|
||||||
|
minetest.register_on_shutdown(function()
|
||||||
|
local avg = sumtime / ngen
|
||||||
|
local std = math.sqrt(sumtime2/ngen - avg*avg)
|
||||||
|
print(("[mapgen_rivers] Mapgen statistics:\n- Mapgen calls: %4d\n- Mean time: %5.3f s\n- Standard deviation: %5.3f s"):format(avg, std))
|
||||||
|
end)
|
||||||
|
@ -88,8 +88,6 @@ local init = false
|
|||||||
-- On map generation, determine into which polygon every point (in 2D) will fall.
|
-- On map generation, determine into which polygon every point (in 2D) will fall.
|
||||||
-- Also store polygon-specific data
|
-- Also store polygon-specific data
|
||||||
local function make_polygons(minp, maxp)
|
local function make_polygons(minp, maxp)
|
||||||
print("Generating polygon map")
|
|
||||||
print(minp.x, maxp.x, minp.z, maxp.z)
|
|
||||||
|
|
||||||
local grid = mapgen_rivers.grid
|
local grid = mapgen_rivers.grid
|
||||||
local dem = grid.dem
|
local dem = grid.dem
|
||||||
@ -113,7 +111,6 @@ local function make_polygons(minp, maxp)
|
|||||||
-- Determine the minimum and maximum coordinates of the polygons that could be on the chunk, knowing that they have an average size of 'blocksize' and a maximal offset of 0.5 blocksize.
|
-- Determine the minimum and maximum coordinates of the polygons that could be on the chunk, knowing that they have an average size of 'blocksize' and a maximal offset of 0.5 blocksize.
|
||||||
local xpmin, xpmax = math.max(math.floor((minp.x+map_offset.x)/blocksize - 0.5), 0), math.min(math.ceil((maxp.x+map_offset.x)/blocksize + 0.5), X-2)
|
local xpmin, xpmax = math.max(math.floor((minp.x+map_offset.x)/blocksize - 0.5), 0), math.min(math.ceil((maxp.x+map_offset.x)/blocksize + 0.5), X-2)
|
||||||
local zpmin, zpmax = math.max(math.floor((minp.z+map_offset.z)/blocksize - 0.5), 0), math.min(math.ceil((maxp.z+map_offset.z)/blocksize + 0.5), Z-2)
|
local zpmin, zpmax = math.max(math.floor((minp.z+map_offset.z)/blocksize - 0.5), 0), math.min(math.ceil((maxp.z+map_offset.z)/blocksize + 0.5), Z-2)
|
||||||
print(xpmin, xpmax, zpmin, zpmax)
|
|
||||||
|
|
||||||
-- Iterate over the polygons
|
-- Iterate over the polygons
|
||||||
for xp = xpmin, xpmax do
|
for xp = xpmin, xpmax do
|
||||||
@ -135,9 +132,6 @@ local function make_polygons(minp, maxp)
|
|||||||
(offset_z[iC]+zp+1) * blocksize - map_offset.z,
|
(offset_z[iC]+zp+1) * blocksize - map_offset.z,
|
||||||
(offset_z[iD]+zp+1) * blocksize - map_offset.z,
|
(offset_z[iD]+zp+1) * blocksize - map_offset.z,
|
||||||
}
|
}
|
||||||
if xp==xpmin and zp==zpmin then
|
|
||||||
print(xp, zp, poly_x[1], poly_z[1])
|
|
||||||
end
|
|
||||||
local polygon = {x=poly_x, z=poly_z, i={iA, iB, iC, iD}}
|
local polygon = {x=poly_x, z=poly_z, i={iA, iB, iC, iD}}
|
||||||
|
|
||||||
local bounds = {} -- Will be a list of the intercepts of polygon edges for every Z position (scanline algorithm)
|
local bounds = {} -- Will be a list of the intercepts of polygon edges for every Z position (scanline algorithm)
|
||||||
|
@ -2,27 +2,18 @@
|
|||||||
|
|
||||||
local function get_box_size(sigma, n)
|
local function get_box_size(sigma, n)
|
||||||
local v = sigma^2 / n
|
local v = sigma^2 / n
|
||||||
--print('v: '..v)
|
|
||||||
local r_ideal = ((12*v + 1) ^ 0.5 - 1) / 2
|
local r_ideal = ((12*v + 1) ^ 0.5 - 1) / 2
|
||||||
--print('r_ideal: '..r_ideal)
|
|
||||||
local r_down = math.floor(r_ideal)
|
local r_down = math.floor(r_ideal)
|
||||||
--print('r_down: '..r_down)
|
|
||||||
local r_up = math.ceil(r_ideal)
|
local r_up = math.ceil(r_ideal)
|
||||||
--print('r_up: '..r_up)
|
|
||||||
local v_down = ((2*r_down+1)^2 - 1) / 12
|
local v_down = ((2*r_down+1)^2 - 1) / 12
|
||||||
--print('v_down: '..v_down)
|
|
||||||
local v_up = ((2*r_up+1)^2 - 1) / 12
|
local v_up = ((2*r_up+1)^2 - 1) / 12
|
||||||
--print('v_up: '..v_up)
|
|
||||||
local m_ideal = (v - v_down) / (v_up - v_down) * n
|
local m_ideal = (v - v_down) / (v_up - v_down) * n
|
||||||
--print('m_ideal: '..m_ideal)
|
|
||||||
local m = math.floor(m_ideal+0.5)
|
local m = math.floor(m_ideal+0.5)
|
||||||
--print('m: '..m)
|
|
||||||
|
|
||||||
local sizes = {}
|
local sizes = {}
|
||||||
for i=1, n do
|
for i=1, n do
|
||||||
sizes[i] = i<=m and 2*r_up+1 or 2*r_down+1
|
sizes[i] = i<=m and 2*r_up+1 or 2*r_down+1
|
||||||
end
|
end
|
||||||
--print('sizes: '..table.concat(sizes, ', '))
|
|
||||||
|
|
||||||
return sizes
|
return sizes
|
||||||
end
|
end
|
||||||
|
@ -44,7 +44,6 @@ local function flow_routing(dem, dirs, lakes, method)
|
|||||||
dirs.Y = Y
|
dirs.Y = Y
|
||||||
lakes.X = X
|
lakes.X = X
|
||||||
lakes.Y = Y
|
lakes.Y = Y
|
||||||
--print(X, Y)
|
|
||||||
local i = 1
|
local i = 1
|
||||||
local dirs2 = {}
|
local dirs2 = {}
|
||||||
for i=1, X*Y do
|
for i=1, X*Y do
|
||||||
@ -81,7 +80,6 @@ local function flow_routing(dem, dirs, lakes, method)
|
|||||||
|
|
||||||
-- Compute basins and links
|
-- Compute basins and links
|
||||||
local nbasins = #singular
|
local nbasins = #singular
|
||||||
print(nbasins)
|
|
||||||
local basin_id = {}
|
local basin_id = {}
|
||||||
local links = {}
|
local links = {}
|
||||||
local basin_links
|
local basin_links
|
||||||
@ -196,7 +194,6 @@ local function flow_routing(dem, dirs, lakes, method)
|
|||||||
|
|
||||||
local basin_graph = {}
|
local basin_graph = {}
|
||||||
for n=1, nbasins do
|
for n=1, nbasins do
|
||||||
--print(n, nbasins)
|
|
||||||
local b1, lnk1 = next(lowlevel)
|
local b1, lnk1 = next(lowlevel)
|
||||||
lowlevel[b1] = nil
|
lowlevel[b1] = nil
|
||||||
|
|
||||||
@ -204,16 +201,13 @@ local function flow_routing(dem, dirs, lakes, method)
|
|||||||
local lowest = math.huge
|
local lowest = math.huge
|
||||||
local lnk1 = links[b1]
|
local lnk1 = links[b1]
|
||||||
local i = 0
|
local i = 0
|
||||||
--print('Scanning basin '..b1)
|
|
||||||
for bn, bdata in pairs(lnk1) do
|
for bn, bdata in pairs(lnk1) do
|
||||||
--print('- Link '..bn)
|
|
||||||
i = i + 1
|
i = i + 1
|
||||||
if bdata.elev < lowest then
|
if bdata.elev < lowest then
|
||||||
lowest = bdata.elev
|
lowest = bdata.elev
|
||||||
b2 = bn
|
b2 = bn
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
--print('Number of links: '..i..' vs '..nlinks[b1])
|
|
||||||
|
|
||||||
-- Add link to the graph
|
-- Add link to the graph
|
||||||
local bound = lnk1[b2]
|
local bound = lnk1[b2]
|
||||||
@ -226,49 +220,34 @@ local function flow_routing(dem, dirs, lakes, method)
|
|||||||
end
|
end
|
||||||
basin_graph[bb1][bb2] = bound
|
basin_graph[bb1][bb2] = bound
|
||||||
basin_graph[bb2][bb1] = bound
|
basin_graph[bb2][bb1] = bound
|
||||||
--if bb1 == 0 then
|
|
||||||
-- print(bb2)
|
|
||||||
--elseif bb2 == 0 then
|
|
||||||
-- print(bb1)
|
|
||||||
--end
|
|
||||||
|
|
||||||
-- Merge basin b1 into b2
|
-- Merge basin b1 into b2
|
||||||
--print("Merging "..b1.." into "..b2)
|
|
||||||
local lnk2 = links[b2]
|
local lnk2 = links[b2]
|
||||||
-- First, remove the link between b1 and b2
|
-- First, remove the link between b1 and b2
|
||||||
lnk1[b2] = nil
|
lnk1[b2] = nil
|
||||||
lnk2[b1] = nil
|
lnk2[b1] = nil
|
||||||
nlinks[b2] = nlinks[b2] - 1
|
nlinks[b2] = nlinks[b2] - 1
|
||||||
--print('Decreasing link count of '..b2..' ('..nlinks[b2]..')')
|
|
||||||
if nlinks[b2] == 8 then
|
if nlinks[b2] == 8 then
|
||||||
--print('Added to lowlevel')
|
|
||||||
lowlevel[b2] = lnk2
|
lowlevel[b2] = lnk2
|
||||||
end
|
end
|
||||||
--print('Scanning neighbourg of '..b1..' to fix links')
|
|
||||||
-- Look for basin 1's neighbours, and add them to basin 2 if they have a lower pass
|
-- Look for basin 1's neighbours, and add them to basin 2 if they have a lower pass
|
||||||
for bn, bdata in pairs(lnk1) do
|
for bn, bdata in pairs(lnk1) do
|
||||||
--print('- Neighbour '..bn)
|
|
||||||
local lnkn = links[bn]
|
local lnkn = links[bn]
|
||||||
lnkn[b1] = nil
|
lnkn[b1] = nil
|
||||||
|
|
||||||
if lnkn[b2] then
|
if lnkn[b2] then
|
||||||
nlinks[bn] = nlinks[bn] - 1
|
nlinks[bn] = nlinks[bn] - 1
|
||||||
--print('Decreasing link count of '..bn..' ('..nlinks[bn]..')')
|
|
||||||
if nlinks[bn] == 8 then
|
if nlinks[bn] == 8 then
|
||||||
--print('Added to lowlevel')
|
|
||||||
lowlevel[bn] = lnkn
|
lowlevel[bn] = lnkn
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
nlinks[b2] = nlinks[b2] + 1
|
nlinks[b2] = nlinks[b2] + 1
|
||||||
--print('Increasing link count of '..b2..' ('..nlinks[b2]..')')
|
|
||||||
if nlinks[b2] == 9 then
|
if nlinks[b2] == 9 then
|
||||||
--print('Removed from lowlevel')
|
|
||||||
lowlevel[b2] = nil
|
lowlevel[b2] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not lnkn[b2] or lnkn[b2].elev > bdata.elev then
|
if not lnkn[b2] or lnkn[b2].elev > bdata.elev then
|
||||||
--print(' - Redirecting link')
|
|
||||||
lnkn[b2] = bdata
|
lnkn[b2] = bdata
|
||||||
lnk2[bn] = bdata
|
lnk2[bn] = bdata
|
||||||
end
|
end
|
||||||
@ -282,17 +261,13 @@ local function flow_routing(dem, dirs, lakes, method)
|
|||||||
end
|
end
|
||||||
local reverse = {3, 4, 1, 2, [0]=0}
|
local reverse = {3, 4, 1, 2, [0]=0}
|
||||||
for n=1, nbasins do
|
for n=1, nbasins do
|
||||||
--print(n, nbasins)
|
|
||||||
local b1, elev1 = next(queue)
|
local b1, elev1 = next(queue)
|
||||||
queue[b1] = nil
|
queue[b1] = nil
|
||||||
basin_lake[b1] = elev1
|
basin_lake[b1] = elev1
|
||||||
--print('Scanning basin '..b1)
|
|
||||||
for b2, bound in pairs(basin_graph[b1]) do
|
for b2, bound in pairs(basin_graph[b1]) do
|
||||||
--print('Flow '..b2..' into '..b1)
|
|
||||||
-- Make b2 flow into b1
|
-- Make b2 flow into b1
|
||||||
local i = bound.i
|
local i = bound.i
|
||||||
local dir = bound.is_y and 3 or 4
|
local dir = bound.is_y and 3 or 4
|
||||||
--print(basin_id[i])
|
|
||||||
if basin_id[i] ~= b2 then
|
if basin_id[i] ~= b2 then
|
||||||
dir = dir - 2
|
dir = dir - 2
|
||||||
if bound.is_y then
|
if bound.is_y then
|
||||||
@ -303,8 +278,7 @@ local function flow_routing(dem, dirs, lakes, method)
|
|||||||
elseif b1 == 0 then
|
elseif b1 == 0 then
|
||||||
dir = 0
|
dir = 0
|
||||||
end
|
end
|
||||||
--print(basin_id[i])
|
|
||||||
--print('Reversing directions')
|
|
||||||
repeat
|
repeat
|
||||||
dir, dirs[i] = dirs[i], dir
|
dir, dirs[i] = dirs[i], dir
|
||||||
if dir == 1 then
|
if dir == 1 then
|
||||||
@ -363,12 +337,10 @@ local function accumulate(dirs, waterq)
|
|||||||
end
|
end
|
||||||
|
|
||||||
for i1=1, X*Y do
|
for i1=1, X*Y do
|
||||||
--print(i1, ndonors[i1])
|
|
||||||
if ndonors[i1] == 0 then
|
if ndonors[i1] == 0 then
|
||||||
local i2 = i1
|
local i2 = i1
|
||||||
local dir = dirs[i2]
|
local dir = dirs[i2]
|
||||||
local w = waterq[i2]
|
local w = waterq[i2]
|
||||||
--print(dir)
|
|
||||||
while dir > 0 do
|
while dir > 0 do
|
||||||
if dir == 1 then
|
if dir == 1 then
|
||||||
i2 = i2 + X
|
i2 = i2 + X
|
||||||
@ -379,7 +351,6 @@ local function accumulate(dirs, waterq)
|
|||||||
elseif dir == 4 then
|
elseif dir == 4 then
|
||||||
i2 = i2 - 1
|
i2 = i2 - 1
|
||||||
end
|
end
|
||||||
--print('Incrementing '..i2)
|
|
||||||
w = w + waterq[i2]
|
w = w + waterq[i2]
|
||||||
waterq[i2] = w
|
waterq[i2] = w
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user