11 Commits

Author SHA1 Message Date
1adb4fbece Added an offset of 0.5 on terrain elevation
This prevents rounding errors and improves interpolation on nearly flat areas
2020-04-13 12:27:24 +02:00
13d3e70b66 Implemented variable river width.
Also changed the river data exported by terrain_rivers.py. They will not be compatible with what's generated by older versions.
2020-04-13 12:15:10 +02:00
4b63ed371e Add more information in the polygon table 2020-04-13 10:31:38 +02:00
eba90803fe Removed useless debug print 2020-04-13 10:01:23 +02:00
34de4269ee Add directly a reference to the polygon table in the polygon list, instead of adding an index 2020-04-13 09:54:04 +02:00
4e8288afbe Added screenshot in README 2020-04-13 09:27:41 +02:00
56cebecb13 More robust and faster code for grid twisting on the Lua side.
At chunkgen init, build a list of the polygons instead of calculating them for every node.
2020-04-13 09:27:41 +02:00
b7c6f71635 Implemented grid twisting. Still many possible bugs, potentially clumsy implementation, but it seems to work. 2020-04-13 09:27:41 +02:00
6314117642 Added bounds.py: twists the grid as if the rivers were elastic bounds. Unused for now. 2020-04-13 09:27:41 +02:00
ed34dec4fa Adjust number of octaves in function of map size 2020-04-12 17:26:37 +02:00
538bfb6d6d Added script to view map, using matplotlib 2020-04-12 16:44:29 +02:00
4 changed files with 112 additions and 51 deletions

4
.gitignore vendored
View File

@ -1,9 +1,9 @@
__pycache__/ __pycache__/
dem dem
lakes lakes
links
rivers
size size
offset_x offset_x
offset_y offset_y
bounds_x
bounds_y
unused/ unused/

123
init.lua
View File

@ -27,10 +27,10 @@ copy_if_needed('dem')
local dem = load_map(worldpath..'dem', 2, true) local dem = load_map(worldpath..'dem', 2, true)
copy_if_needed('lakes') copy_if_needed('lakes')
local lakes = load_map(worldpath..'lakes', 2, true) local lakes = load_map(worldpath..'lakes', 2, true)
copy_if_needed('links') copy_if_needed('bounds_x')
local links = load_map(worldpath..'links', 1, false) local bounds_x = load_map(worldpath..'bounds_x', 4, false)
copy_if_needed('rivers') copy_if_needed('bounds_y')
local rivers = load_map(worldpath..'rivers', 4, false) local bounds_z = load_map(worldpath..'bounds_y', 4, false)
copy_if_needed('offset_x') copy_if_needed('offset_x')
local offset_x = load_map(worldpath..'offset_x', 1, true) local offset_x = load_map(worldpath..'offset_x', 1, true)
@ -54,7 +54,7 @@ local function get_point_location(x, z)
return x+offset_x[i], z+offset_z[i] return x+offset_x[i], z+offset_z[i]
end end
local function interp(v00, v01, v10, v11, xf, zf) local function interp(v00, v01, v11, v10, xf, zf)
local v0 = v01*xf + v00*(1-xf) local v0 = v01*xf + v00*(1-xf)
local v1 = v11*xf + v10*(1-xf) local v1 = v11*xf + v10*(1-xf)
return v1*zf + v0*(1-zf) return v1*zf + v0*(1-zf)
@ -65,6 +65,7 @@ local data = {}
local blocksize = 12 local blocksize = 12
local sea_level = 1 local sea_level = 1
local min_catchment = 25 local min_catchment = 25
local max_catchment = 40000
local storage = minetest.get_mod_storage() local storage = minetest.get_mod_storage()
if storage:contains("blocksize") then if storage:contains("blocksize") then
@ -82,6 +83,25 @@ if storage:contains("min_catchment") then
else else
storage:set_float("min_catchment", min_catchment) storage:set_float("min_catchment", min_catchment)
end end
if storage:contains("max_catchment") then
max_catchment = storage:get_float("max_catchment")
else
storage:set_float("max_catchment", max_catchment)
end
-- Width coefficients: coefficients solving
-- wfactor * min_catchment ^ wpower = 1/(2*blocksize)
-- wfactor * max_catchment ^ wpower = 1
local wpower = math.log(2*blocksize)/math.log(max_catchment/min_catchment)
local wfactor = 1 / max_catchment ^ wpower
local function river_width(flow)
flow = math.abs(flow)
if flow < min_catchment then
return 0
end
return math.min(wfactor * flow ^ wpower, 1)
end
local function generate(minp, maxp, seed) local function generate(minp, maxp, seed)
local c_stone = minetest.get_content_id("default:stone") local c_stone = minetest.get_content_id("default:stone")
@ -98,12 +118,10 @@ local function generate(minp, maxp, seed)
local ystride = a.ystride -- Tip : the ystride of a VoxelArea is the number to add to the array index to get the index of the position above. It's faster because it avoids to completely recalculate the index. local ystride = a.ystride -- Tip : the ystride of a VoxelArea is the number to add to the array index to get the index of the position above. It's faster because it avoids to completely recalculate the index.
local chulens = maxp.z - minp.z + 1 local chulens = maxp.z - minp.z + 1
local polygon_number = {}
local polygons = {} local polygons = {}
local xpmin, xpmax = math.max(math.floor(minp.x/blocksize - 0.5), 0), math.min(math.ceil(maxp.x/blocksize), X-2) local xpmin, xpmax = math.max(math.floor(minp.x/blocksize - 0.5), 0), math.min(math.ceil(maxp.x/blocksize), X-2)
local zpmin, zpmax = math.max(math.floor(minp.z/blocksize - 0.5), 0), math.min(math.ceil(maxp.z/blocksize), Z-2) local zpmin, zpmax = math.max(math.floor(minp.z/blocksize - 0.5), 0), math.min(math.ceil(maxp.z/blocksize), Z-2)
local n = 1
local n_filled = 0
for xp = xpmin, xpmax do for xp = xpmin, xpmax do
for zp=zpmin, zpmax do for zp=zpmin, zpmax do
local iA = index(xp, zp) local iA = index(xp, zp)
@ -112,6 +130,7 @@ local function generate(minp, maxp, seed)
local iD = index(xp, zp+1) local iD = index(xp, zp+1)
local poly_x = {offset_x[iA]+xp, offset_x[iB]+xp+1, offset_x[iC]+xp+1, offset_x[iD]+xp} local poly_x = {offset_x[iA]+xp, offset_x[iB]+xp+1, offset_x[iC]+xp+1, offset_x[iD]+xp}
local poly_z = {offset_z[iA]+zp, offset_z[iB]+zp, offset_z[iC]+zp+1, offset_z[iD]+zp+1} local poly_z = {offset_z[iA]+zp, offset_z[iB]+zp, offset_z[iC]+zp+1, offset_z[iD]+zp+1}
local polygon = {x=poly_x, z=poly_z, i={iA, iB, iC, iD}}
local bounds = {} local bounds = {}
local xmin = math.max(math.floor(blocksize*math.min(unpack(poly_x)))+1, minp.x) local xmin = math.max(math.floor(blocksize*math.min(unpack(poly_x)))+1, minp.x)
@ -144,63 +163,75 @@ local function generate(minp, maxp, seed)
local zmax = math.min(math.floor(xlist[l*2]), maxp.z) local zmax = math.min(math.floor(xlist[l*2]), maxp.z)
local i = (x-minp.x) * chulens + (zmin-minp.z) + 1 local i = (x-minp.x) * chulens + (zmin-minp.z) + 1
for z=zmin, zmax do for z=zmin, zmax do
polygon_number[i] = n polygons[i] = polygon
i = i + 1 i = i + 1
n_filled = n_filled + 1
end end
end end
end end
polygons[n] = {x=poly_x, z=poly_z, i={iA, iB, iC, iD}} polygon.dem = {dem[iA], dem[iB], dem[iC], dem[iD]}
n = n + 1 polygon.lake = math.min(lakes[iA], lakes[iB], lakes[iC], lakes[iD])
local river_west = river_width(bounds_z[iA])
local river_north = river_width(bounds_x[iA-zp])
local river_east = 1-river_width(bounds_z[iB])
local river_south = 1-river_width(bounds_x[iD-zp-1])
if river_west > river_east then
local mean = (river_west + river_east) / 2
river_west = mean
river_east = mean
end
if river_north > river_south then
local mean = (river_north + river_south) / 2
river_north = mean
river_south = mean
end
polygon.rivers = {river_west, river_north, river_east, river_south}
end end
end end
local i = 1 local i = 1
for x = minp.x, maxp.x do for x = minp.x, maxp.x do
for z = minp.z, maxp.z do for z = minp.z, maxp.z do
local npoly = polygon_number[i] local poly = polygons[i]
if npoly then if poly then
local poly = polygons[npoly]
local xf, zf = geometry.transform_quadri(poly.x, poly.z, x/blocksize, z/blocksize) local xf, zf = geometry.transform_quadri(poly.x, poly.z, x/blocksize, z/blocksize)
if xf < 0 or xf > 1 or zf < 0 or zf > 1 then
print(xf, zf, x, z)
end
local i00, i01, i11, i10 = unpack(poly.i) local i00, i01, i11, i10 = unpack(poly.i)
local terrain_height = math.floor(interp( local is_river = false
dem[i00], local r_west, r_north, r_east, r_south = unpack(poly.rivers)
dem[i01], if xf >= r_east then
dem[i10], is_river = true
dem[i11], xf = 1
elseif xf <= r_west then
is_river = true
xf = 0
end
if zf >= r_south then
is_river = true
zf = 1
elseif zf <= r_north then
is_river = true
zf = 0
end
if not is_river then
xf = (xf-r_west) / (r_east-r_west)
zf = (zf-r_north) / (r_south-r_north)
end
local vdem = poly.dem
local terrain_height = math.floor(0.5+interp(
vdem[1],
vdem[2],
vdem[3],
vdem[4],
xf, zf xf, zf
)) ))
local lake_height = math.floor(math.min( local lake_height = math.floor(poly.lake)
lakes[i00],
lakes[i01],
lakes[i10],
lakes[i11]
))
local is_lake = lake_height > terrain_height local is_lake = lake_height > terrain_height
local is_river = false
if xf < 1/6 then
if links[i00] == 1 and rivers[i00] >= min_catchment then
is_river = true
elseif links[i10] == 3 and rivers[i10] >= min_catchment then
is_river = true
end
end
if zf < 1/6 then
if links[i00] == 2 and rivers[i00] >= min_catchment then
is_river = true
elseif links[i01] == 4 and rivers[i01] >= min_catchment then
is_river = true
end
end
local ivm = a:index(x, minp.y-1, z) local ivm = a:index(x, minp.y-1, z)

View File

@ -23,7 +23,7 @@ n = np.zeros((mapsize, mapsize))
#micronoise_depth = 0.05 #micronoise_depth = 0.05
params = { params = {
"octaves" : 8, "octaves" : int(np.log2(mapsize)),
"persistence" : 0.5, "persistence" : 0.5,
"lacunarity" : 2., "lacunarity" : 2.,
} }
@ -76,8 +76,8 @@ offset_y = np.clip(np.floor(oy * 256), -128, 127)
save(model.dem, 'dem', dtype='>i2') save(model.dem, 'dem', dtype='>i2')
save(model.lakes, 'lakes', dtype='>i2') save(model.lakes, 'lakes', dtype='>i2')
save(model.dirs, 'links', dtype='u1') save(np.abs(bx), 'bounds_x', dtype='>i4')
save(model.rivers, 'rivers', dtype='>u4') save(np.abs(by), 'bounds_y', dtype='>i4')
save(offset_x, 'offset_x', dtype='i1') save(offset_x, 'offset_x', dtype='i1')
save(offset_y, 'offset_y', dtype='i1') save(offset_y, 'offset_y', dtype='i1')

30
view_map.py Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
shape = np.loadtxt('size', dtype='u4')
n = shape[0] * shape[1]
dem = np.fromfile('dem', dtype='>i2').reshape(shape)
lakes = np.fromfile('lakes', dtype='>i2').reshape(shape)
rivers = np.fromfile('rivers', dtype='>u4').reshape(shape)
plt.subplot(1,3,1)
plt.pcolormesh(dem, cmap='viridis')
plt.gca().set_aspect('equal', 'box')
#plt.colorbar(orientation='horizontal')
plt.title('Raw elevation')
plt.subplot(1,3,2)
plt.pcolormesh(lakes, cmap='viridis')
plt.gca().set_aspect('equal', 'box')
#plt.colorbar(orientation='horizontal')
plt.title('Lake surface elevation')
plt.subplot(1,3,3)
plt.pcolormesh(np.log(rivers), vmin=0, vmax=np.log(n/25), cmap='Blues')
plt.gca().set_aspect('equal', 'box')
#plt.colorbar(orientation='horizontal')
plt.title('Rivers discharge')
plt.show()