Various bugfixes and workarounds

Now working in pure Lua!
Some parts of the code are very hacky (e.g. noise) and the way new and old codes have been glued together is sometimes to be rewritten.
But at least it works.
This commit is contained in:
Gaël C 2021-06-03 23:30:04 +02:00
parent cb297af047
commit 74733549df
6 changed files with 28 additions and 18 deletions

View File

@ -1,15 +1,16 @@
local EvolutionModel = dofile(mapgen_rivers.modpath .. '/erosion.lua')
local twist = dofile(mapgen_rivers.modpath .. '/twist.lua')
local EvolutionModel = dofile(mapgen_rivers.modpath .. '/terrainlib_lua/erosion.lua')
local twist = dofile(mapgen_rivers.modpath .. '/terrainlib_lua/twist.lua')
local size = {x=1000, y=1000}
local blocksize = 12
local variation_speed = 70
local np_base = {
offset = 0,
scale = 200,
seed = 2469,
octaves = 8,
spread = {x=4000/blocksize, y=4000/blocksize, z=5},
spread = {x=4000/blocksize, y=4000/blocksize, z=4000/blocksize},
persist = 0.6,
lacunarity = 2,
}
@ -20,10 +21,14 @@ local niter = math.ceil(time/time_step)
time_step = time / niter
local function generate()
local nobj_base = minetest.get_perlin_map(np_base, size)
nobj_base:calc_3d_map({x=0, y=0, z=0})
local seed = tonumber(minetest.get_mapgen_setting("seed"))
np_base.seed = (np_base.seed or 0) + seed
local dem = nobj_base:get_map_slice({z=1}, {z=1})
local nobj_base = PerlinNoiseMap(np_base, {x=size.x, y=1, z=size.y})
--nobj_base:calc_3d_map({x=0, y=0, z=0})
--local dem = nobj_base:get_map_slice({z=1}, {z=1})
local dem = nobj_base:get_3d_map_flat({x=0, y=0, z=0})
dem.X = size.x
dem.Y = size.y
@ -31,8 +36,10 @@ local function generate()
model.dem = dem
local ref_dem = model:define_isostasy(dem)
local variation_step = variation_speed * time_step
for i=1, niter do
nobj_base:get_map_slice({z=i+1}, {z=1}, ref_dem)
--nobj_base:get_map_slice({z=i+1}, {z=1}, ref_dem)
nobj_base:get_3d_map_flat({x=0, y=variation_step*i, z=0}, ref_dem)
model:diffuse(time_step)
model:flow()
@ -44,9 +51,12 @@ local function generate()
local mfloor = math.floor
local mmin, mmax = math.min, math.max
local offset_x, offset_y = twist(model.dirs, model.rivers, 5)
for i=1, X*Y do
local dirs = model.dirs
local converter = {2, 1, 4, 3, [0]=0}
for i=1, size.x*size.y do
offset_x[i] = mmin(mmax(offset_x[i]*256, -128), 127)
offset_y[i] = mmin(mmax(offset_y[i]*256, -128), 127)
dirs[i] = converter[dirs[i]] -- TODO Fix this
end
mapgen_rivers.write_map('dem', model.dem, 2)
@ -56,7 +66,7 @@ local function generate()
mapgen_rivers.write_map('offset_x', offset_x, 1)
mapgen_rivers.write_map('offset_y', offset_y, 1)
local sfile = io.open(mapgen_rivers.world_data_path .. 'size', "w")
sfile:write(X..'\n'..Y)
sfile:write(size.x..'\n'..size.y)
sfile:close()
end

View File

@ -2,7 +2,7 @@ mapgen_rivers = {}
local modpath = minetest.get_modpath(minetest.get_current_modname()) .. '/'
mapgen_rivers.modpath = modpath
mapgen_rivers.world_data_path = minetest.get_worldpath() .. '/river_data'
mapgen_rivers.world_data_path = minetest.get_worldpath() .. '/river_data/'
dofile(modpath .. 'settings.lua')

View File

@ -1,4 +1,4 @@
local worldpath = minetest.world_data_path
local worldpath = mapgen_rivers.world_data_path
function mapgen_rivers.load_map(filename, bytes, signed, size)
local file = io.open(worldpath .. filename, 'rb')

View File

@ -4,7 +4,7 @@ if not io.open(mod_data_path .. 'size', 'r') then
mod_data_path = modpath .. 'demo_data/'
end
local world_data_path = minetest.world_data_path
local world_data_path = mapgen_rivers.world_data_path
minetest.mkdir(world_data_path)
dofile(modpath .. 'load.lua')
@ -26,15 +26,15 @@ sfile:close()
local dem = mapgen_rivers.load_map('dem', 2, true, X*Z)
local lakes = mapgen_rivers.load_map('lakes', 2, true, X*Z)
local dirs = mapen_rivers.load_map('dirs', 1, false, X*Z)
local dirs = mapgen_rivers.load_map('dirs', 1, false, X*Z)
local rivers = mapgen_rivers.load_map('rivers', 4, false, X*Z)
local offset_x = load_map('offset_x', 1, true, X*Z)
local offset_x = mapgen_rivers.load_map('offset_x', 1, true, X*Z)
for k, v in ipairs(offset_x) do
offset_x[k] = (v+0.5)/256
end
local offset_z = load_map('offset_y', 1, true, X*Z)
local offset_z = mapgen_rivers.load_map('offset_y', 1, true, X*Z)
for k, v in ipairs(offset_z) do
offset_z[k] = (v+0.5)/256
end

View File

@ -110,7 +110,7 @@ end
local modpath = ""
if minetest then
if minetest.global_exists(mapgen_rivers) then
if minetest.global_exists('mapgen_rivers') then
modpath = mapgen_rivers.modpath .. "terrainlib_lua/"
else
modpath = minetest.get_modpath(minetest.get_current_modname()) .. "terrainlib_lua/"

View File

@ -29,7 +29,7 @@ local flow_methods = {
local function flow_routing(dem, dirs, lakes, method)
method = method or 'semirandom'
flow_local = flow_methods[method] or flow_local_semirandom
local flow_local = flow_methods[method] or flow_local_semirandom
dirs = dirs or {}
lakes = lakes or {}
@ -194,7 +194,7 @@ local function flow_routing(dem, dirs, lakes, method)
end
end
basin_graph = {}
local basin_graph = {}
for n=1, nbasins do
--print(n, nbasins)
local b1, lnk1 = next(lowlevel)