mirror of
https://gitlab.com/gaelysam/mapgen_rivers.git
synced 2025-07-04 09:20:41 +02:00
Compare commits
4 Commits
variable_w
...
settings
Author | SHA1 | Date | |
---|---|---|---|
14163681cc | |||
af7a7ce26d | |||
da98a538bb | |||
b5db63d267 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,4 +6,5 @@ offset_x
|
|||||||
offset_y
|
offset_y
|
||||||
bounds_x
|
bounds_x
|
||||||
bounds_y
|
bounds_y
|
||||||
|
rivers
|
||||||
unused/
|
unused/
|
||||||
|
96
init.lua
96
init.lua
@ -24,22 +24,22 @@ local X = tonumber(sfile:read('*l'))
|
|||||||
local Z = tonumber(sfile:read('*l'))
|
local Z = tonumber(sfile:read('*l'))
|
||||||
|
|
||||||
copy_if_needed('dem')
|
copy_if_needed('dem')
|
||||||
local dem = load_map(worldpath..'dem', 2, true)
|
local dem = load_map(worldpath..'dem', 2, true, X*Z)
|
||||||
copy_if_needed('lakes')
|
copy_if_needed('lakes')
|
||||||
local lakes = load_map(worldpath..'lakes', 2, true)
|
local lakes = load_map(worldpath..'lakes', 2, true, X*Z)
|
||||||
copy_if_needed('bounds_x')
|
copy_if_needed('bounds_x')
|
||||||
local bounds_x = load_map(worldpath..'bounds_x', 4, false)
|
local bounds_x = load_map(worldpath..'bounds_x', 4, false, (X-1)*Z)
|
||||||
copy_if_needed('bounds_y')
|
copy_if_needed('bounds_y')
|
||||||
local bounds_z = load_map(worldpath..'bounds_y', 4, false)
|
local bounds_z = load_map(worldpath..'bounds_y', 4, false, X*(Z-1))
|
||||||
|
|
||||||
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, X*Z)
|
||||||
for k, v in ipairs(offset_x) do
|
for k, v in ipairs(offset_x) do
|
||||||
offset_x[k] = (v+0.5)/256
|
offset_x[k] = (v+0.5)/256
|
||||||
end
|
end
|
||||||
|
|
||||||
copy_if_needed('offset_y')
|
copy_if_needed('offset_y')
|
||||||
local offset_z = load_map(worldpath..'offset_y', 1, true)
|
local offset_z = load_map(worldpath..'offset_y', 1, true, X*Z)
|
||||||
for k, v in ipairs(offset_z) do
|
for k, v in ipairs(offset_z) do
|
||||||
offset_z[k] = (v+0.5)/256
|
offset_z[k] = (v+0.5)/256
|
||||||
end
|
end
|
||||||
@ -66,28 +66,15 @@ local blocksize = 12
|
|||||||
local sea_level = 1
|
local sea_level = 1
|
||||||
local min_catchment = 25
|
local min_catchment = 25
|
||||||
local max_catchment = 40000
|
local max_catchment = 40000
|
||||||
|
local riverbed_slope = 0.4
|
||||||
|
|
||||||
local storage = minetest.get_mod_storage()
|
local get_settings = dofile(modpath .. 'settings.lua')
|
||||||
if storage:contains("blocksize") then
|
|
||||||
blocksize = storage:get_int("blocksize")
|
blocksize = get_settings('blocksize', 'int', blocksize)
|
||||||
else
|
sea_level = get_settings('sea_level', 'int', sea_level)
|
||||||
storage:set_int("blocksize", blocksize)
|
min_catchment = get_settings('min_catchment', 'float', min_catchment)
|
||||||
end
|
max_catchment = get_settings('max_catchment', 'float', max_catchment)
|
||||||
if storage:contains("sea_level") then
|
riverbed_slope = get_settings('riverbed_slope', 'float', riverbed_slope) * blocksize
|
||||||
sea_level = storage:get_int("sea_level")
|
|
||||||
else
|
|
||||||
storage:set_int("sea_level", sea_level)
|
|
||||||
end
|
|
||||||
if storage:contains("min_catchment") then
|
|
||||||
min_catchment = storage:get_float("min_catchment")
|
|
||||||
else
|
|
||||||
storage:set_float("min_catchment", min_catchment)
|
|
||||||
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
|
-- Width coefficients: coefficients solving
|
||||||
-- wfactor * min_catchment ^ wpower = 1/(2*blocksize)
|
-- wfactor * min_catchment ^ wpower = 1/(2*blocksize)
|
||||||
@ -187,6 +174,25 @@ local function generate(minp, maxp, seed)
|
|||||||
river_south = mean
|
river_south = mean
|
||||||
end
|
end
|
||||||
polygon.rivers = {river_west, river_north, river_east, river_south}
|
polygon.rivers = {river_west, river_north, river_east, river_south}
|
||||||
|
|
||||||
|
local around = {0,0,0,0,0,0,0,0}
|
||||||
|
if zp > 0 then
|
||||||
|
around[1] = river_width(bounds_z[iA-X])
|
||||||
|
around[2] = river_width(bounds_z[iB-X])
|
||||||
|
end
|
||||||
|
if xp < X-2 then
|
||||||
|
around[3] = river_width(bounds_x[iB-zp])
|
||||||
|
around[4] = river_width(bounds_x[iC-zp-1])
|
||||||
|
end
|
||||||
|
if zp < Z-2 then
|
||||||
|
around[5] = river_width(bounds_z[iC])
|
||||||
|
around[6] = river_width(bounds_z[iD])
|
||||||
|
end
|
||||||
|
if xp > 0 then
|
||||||
|
around[7] = river_width(bounds_x[iD-zp-2])
|
||||||
|
around[8] = river_width(bounds_x[iA-zp-1])
|
||||||
|
end
|
||||||
|
polygon.river_corners = {math.max(around[8], around[1]), math.max(around[2], around[3]), math.max(around[4], around[5]), math.max(around[6], around[7])}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -199,22 +205,48 @@ local function generate(minp, maxp, seed)
|
|||||||
local i00, i01, i11, i10 = unpack(poly.i)
|
local i00, i01, i11, i10 = unpack(poly.i)
|
||||||
|
|
||||||
local is_river = false
|
local is_river = false
|
||||||
|
local depth_factor = 0
|
||||||
local r_west, r_north, r_east, r_south = unpack(poly.rivers)
|
local r_west, r_north, r_east, r_south = unpack(poly.rivers)
|
||||||
if xf >= r_east then
|
if xf >= r_east then
|
||||||
is_river = true
|
is_river = true
|
||||||
|
depth_factor = xf-r_east
|
||||||
xf = 1
|
xf = 1
|
||||||
elseif xf <= r_west then
|
elseif xf <= r_west then
|
||||||
is_river = true
|
is_river = true
|
||||||
|
depth_factor = r_west-xf
|
||||||
xf = 0
|
xf = 0
|
||||||
end
|
end
|
||||||
if zf >= r_south then
|
if zf >= r_south then
|
||||||
is_river = true
|
is_river = true
|
||||||
|
depth_factor = zf-r_south
|
||||||
zf = 1
|
zf = 1
|
||||||
elseif zf <= r_north then
|
elseif zf <= r_north then
|
||||||
is_river = true
|
is_river = true
|
||||||
|
depth_factor = r_north-zf
|
||||||
zf = 0
|
zf = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not is_river then
|
||||||
|
local c_NW, c_NE, c_SE, c_SW = unpack(poly.river_corners)
|
||||||
|
if xf+zf <= c_NW then
|
||||||
|
is_river = true
|
||||||
|
depth_factor = c_NW-xf-zf
|
||||||
|
xf, zf = 0, 0
|
||||||
|
elseif 1-xf+zf <= c_NE then
|
||||||
|
is_river = true
|
||||||
|
depth_factor = c_NE-1+xf-zf
|
||||||
|
xf, zf = 1, 0
|
||||||
|
elseif 2-xf-zf <= c_SE then
|
||||||
|
is_river = true
|
||||||
|
depth_factor = c_SE-2+xf+zf
|
||||||
|
xf, zf = 1, 1
|
||||||
|
elseif xf+1-zf <= c_SW then
|
||||||
|
is_river = true
|
||||||
|
depth_factor = c_SW-xf-1+zf
|
||||||
|
xf, zf = 0, 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if not is_river then
|
if not is_river then
|
||||||
xf = (xf-r_west) / (r_east-r_west)
|
xf = (xf-r_west) / (r_east-r_west)
|
||||||
zf = (zf-r_north) / (r_south-r_north)
|
zf = (zf-r_north) / (r_south-r_north)
|
||||||
@ -229,19 +261,17 @@ local function generate(minp, maxp, seed)
|
|||||||
xf, zf
|
xf, zf
|
||||||
))
|
))
|
||||||
|
|
||||||
local lake_height = math.floor(poly.lake)
|
local lake_height = math.max(math.floor(poly.lake), terrain_height)
|
||||||
|
if is_river then
|
||||||
|
terrain_height = math.min(math.max(lake_height, sea_level) - math.floor(1+depth_factor*riverbed_slope), terrain_height)
|
||||||
|
end
|
||||||
local is_lake = lake_height > terrain_height
|
local is_lake = lake_height > terrain_height
|
||||||
|
|
||||||
local ivm = a:index(x, minp.y-1, z)
|
local ivm = a:index(x, minp.y-1, z)
|
||||||
|
|
||||||
if terrain_height >= minp.y then
|
if terrain_height >= minp.y then
|
||||||
for y=minp.y, math.min(maxp.y, terrain_height) do
|
for y=minp.y, math.min(maxp.y, terrain_height) do
|
||||||
if y == terrain_height then
|
if y == terrain_height then
|
||||||
if is_lake or y <= sea_level then
|
if is_lake or y <= sea_level then
|
||||||
data[ivm] = c_sand
|
data[ivm] = c_sand
|
||||||
elseif is_river then
|
|
||||||
data[ivm] = c_rwater
|
|
||||||
else
|
else
|
||||||
data[ivm] = c_lawn
|
data[ivm] = c_lawn
|
||||||
end
|
end
|
||||||
|
7
load.lua
7
load.lua
@ -1,11 +1,12 @@
|
|||||||
local function load_map(filename, bytes, signed)
|
local function load_map(filename, bytes, signed, size)
|
||||||
local file = io.open(filename, 'r')
|
local file = io.open(filename, 'r')
|
||||||
local data = file:read('*all')
|
local data = file:read('*all')
|
||||||
|
if #data < bytes*size then
|
||||||
|
data = minetest.decompress(data)
|
||||||
|
end
|
||||||
|
|
||||||
local map = {}
|
local map = {}
|
||||||
|
|
||||||
local size = math.floor(#data/bytes)
|
|
||||||
|
|
||||||
for i=1, size do
|
for i=1, size do
|
||||||
local i0, i1 = (i-1)*bytes+1, i*bytes
|
local i0, i1 = (i-1)*bytes+1, i*bytes
|
||||||
local elements = {data:byte(i0, i1)}
|
local elements = {data:byte(i0, i1)}
|
||||||
|
7
save.py
7
save.py
@ -1,8 +1,13 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
import zlib
|
||||||
|
|
||||||
def save(data, fname, dtype=None):
|
def save(data, fname, dtype=None):
|
||||||
if dtype is not None:
|
if dtype is not None:
|
||||||
data = data.astype(dtype)
|
data = data.astype(dtype)
|
||||||
|
|
||||||
|
bin_data = data.tobytes()
|
||||||
|
bin_data_comp = zlib.compress(bin_data, 9)
|
||||||
|
if len(bin_data_comp) < len(bin_data):
|
||||||
|
bin_data = bin_data_comp
|
||||||
with open(fname, 'wb') as f:
|
with open(fname, 'wb') as f:
|
||||||
f.write(data.tobytes())
|
f.write(bin_data)
|
||||||
|
41
settings.lua
Normal file
41
settings.lua
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
local storage = minetest.get_mod_storage()
|
||||||
|
local settings = minetest.settings
|
||||||
|
|
||||||
|
local function get_settings(key, dtype, default)
|
||||||
|
if storage:contains(key) then
|
||||||
|
if dtype == "string" then
|
||||||
|
return storage:get_string(key)
|
||||||
|
elseif dtype == "int" then
|
||||||
|
return storage:get_int(key)
|
||||||
|
elseif dtype == "float" then
|
||||||
|
return storage:get_float(key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local conf_val = settings:get('mapgen_rivers_' .. key)
|
||||||
|
if conf_val then
|
||||||
|
if dtype == "int" then
|
||||||
|
conf_val = tonumber(conf_val)
|
||||||
|
storage:set_int(key, conf_val)
|
||||||
|
elseif dtype == "float" then
|
||||||
|
conf_val = tonumber(conf_val)
|
||||||
|
storage:set_float(key, conf_val)
|
||||||
|
elseif dtype == "string" then
|
||||||
|
storage:set_string(key, conf_val)
|
||||||
|
end
|
||||||
|
|
||||||
|
return conf_val
|
||||||
|
else
|
||||||
|
if dtype == "int" then
|
||||||
|
storage:set_int(key, default)
|
||||||
|
elseif dtype == "float" then
|
||||||
|
storage:set_float(key, default)
|
||||||
|
elseif dtype == "string" then
|
||||||
|
storage:set_string(key, default)
|
||||||
|
end
|
||||||
|
|
||||||
|
return default
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return get_settings
|
@ -81,6 +81,8 @@ 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')
|
||||||
|
|
||||||
|
save(model.rivers, 'rivers', dtype='>u4')
|
||||||
|
|
||||||
with open('size', 'w') as sfile:
|
with open('size', 'w') as sfile:
|
||||||
sfile.write('{:d}\n{:d}'.format(mapsize, mapsize))
|
sfile.write('{:d}\n{:d}'.format(mapsize, mapsize))
|
||||||
|
|
||||||
|
15
view_map.py
15
view_map.py
@ -1,13 +1,22 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import zlib
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
def load_map(name, dtype, shape):
|
||||||
|
dtype = np.dtype(dtype)
|
||||||
|
with open(name, 'rb') as f:
|
||||||
|
data = f.read()
|
||||||
|
if len(data) < shape[0]*shape[1]*dtype.itemsize:
|
||||||
|
data = zlib.decompress(data)
|
||||||
|
return np.frombuffer(data, dtype=dtype).reshape(shape)
|
||||||
|
|
||||||
shape = np.loadtxt('size', dtype='u4')
|
shape = np.loadtxt('size', dtype='u4')
|
||||||
n = shape[0] * shape[1]
|
n = shape[0] * shape[1]
|
||||||
dem = np.fromfile('dem', dtype='>i2').reshape(shape)
|
dem = load_map('dem', '>i2', shape)
|
||||||
lakes = np.fromfile('lakes', dtype='>i2').reshape(shape)
|
lakes = load_map('lakes', '>i2', shape)
|
||||||
rivers = np.fromfile('rivers', dtype='>u4').reshape(shape)
|
rivers = load_map('rivers', '>u4', shape)
|
||||||
|
|
||||||
plt.subplot(1,3,1)
|
plt.subplot(1,3,1)
|
||||||
plt.pcolormesh(dem, cmap='viridis')
|
plt.pcolormesh(dem, cmap='viridis')
|
||||||
|
Reference in New Issue
Block a user