Initial commit

Still PoC phase at this stage, but first commit to publicize the idea and allow others to copy or modify the initial code
This commit is contained in:
Emiel van Rooijen 2018-07-12 23:12:55 +02:00 committed by GitHub
parent 278a76f41a
commit 4e76adb03d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 200 additions and 0 deletions

200
mapgen.lua Normal file
View File

@ -0,0 +1,200 @@
local settings = {}
settings.layers = 16
settings.water_height = 0
settings.height_map_seed = 9625423
settings.terrain_type_seed = 2376457
settings.lake_seed = 5232353
settings.cave_seed = 6568239
settings.perlin_worm_start_seed = 1423423
settings.perlin_worm_yaw_seed = 3572245
settings.perlin_worm_pitch_seed = 8763243
local layer_height = 65535 / settings.layers
local half_layer_height = layer_height / 2
-- Base terrain is the lower frequency, lower amplitude noise
-- Terrain type is a multiplier that can dampen terrain to make
-- flat plains, hills or mountains
-- Mountain peak generates peaks when greater than zero, making
-- jagged and rugged peaks on the surface or mountain tops
local height_map
local terrain_type_map
local mountain_peak_map
local lake_map
local height_map_2dmap = {}
local terrain_type_2dmap = {}
local mountain_peak_2dmap = {}
local lake_3dmap = {}
local cave_map
local perlin_worm_start_map
local perlin_worm_yaw_map
local perlin_worm_pitch_map
local cave_3dmap = {}
local perlin_worm_start_3dmap = {}
local perlin_worm_yaw_3dmap = {}
local perlin_worm_pitch_3dmap = {}
local perlin_worms = {}
local height_map_params = {
offset = 0,
scale = 50,
spread = {x=2048, y=2048, z=2048},
seed = settings.height_map_seed,
octaves = 7,
persist = 0.7
}
local terrain_type_params = {
offset = 2.5,
scale = 2.5,
spread = {x=1024, y=1024, z=1024},
seed = settings.terrain_type_seed,
octaves = 6,
persist = 0.6
}
local mountain_peak_params = {
offset = -75,
scale = 125,
spread = {x=256, y=256, z=256},
seed = settings.mountain_peak_seed,
octaves = 7,
persist = 0.6
}
local lake_params = {
offset = 0,
scale = 125,
spread = {x=256, y=256, z=256},
seed = settings.lake_seed,
octaves = 6,
persist = 0.6
}
local cave_params = {
offset = 0,
scale = 10,
spread = {x=128, y=128, z=128},
seed = settings.cave_seed,
octaves = 5,
persist = 0.8
}
local perlin_worm_start_params = {
offset = 0,
scale = 1,
spread = {x=256, y=256, z=256},
seed = settings.perlin_worm_start_seed,
octaves = 7,
persist = 0.7
}
minetest.register_on_generated(function(minp, maxp)
local c_stone = minetest.get_content_id("default:stone")
local c_air = minetest.get_content_id("air")
local c_water = minetest.get_content_id("default:water_source")
local current_layer = "undefined"
for l = (settings.layers / -2), (settings.layers / 2) do
if minp.y >= (l * layer_height) - half_layer_height and minp.y < (l * layer_height) + half_layer_height then
current_layer = l
break
end
end
print("Current layer = "..current_layer)
local sidelen = maxp.x - minp.x + 1
local blocklen = sidelen / 5
--3d
local chulenxyz = {x = sidelen, y = sidelen, z = sidelen}
--2d
local chulenxz = {x = sidelen, y = sidelen, z = 1}
local minposxyz = {x = minp.x, y = minp.y - 1, z = minp.z}
local minposxz = {x = minp.x, y = minp.z}
-- strides for voxelmanip
local ystridevm = sidelen + 32
local zstridevm = ystridevm ^ 2
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
local vm_data = vm:get_data()
height_map = height_map or minetest.get_perlin_map(height_map_params, chulenxz)
terrain_type_map = terrain_type_map or minetest.get_perlin_map(terrain_type_params, chulenxz)
mountain_peak_map = mountain_peak_map or minetest.get_perlin_map(mountain_peak_params, chulenxz)
cave_map = cave_map or minetest.get_perlin_map(cave_params, chulenxyz)
perlin_worm_start_map = perlin_worm_start_map or minetest.get_perlin_map(perlin_worm_start_params, chulenxyz)
height_map:get2dMap_flat(minposxz, height_map_2dmap)
terrain_type_map:get2dMap_flat(minposxz, terrain_type_2dmap)
mountain_peak_map:get2dMap_flat(minposxz, mountain_peak_2dmap)
cave_map:get3dMap_flat(minposxyz, cave_3dmap)
perlin_worm_start_map:get3dMap_flat(minposxyz, perlin_worm_start_3dmap)
-- 3D perlinmap indexes
local nixyz = 1
-- 2D perlinmap indexes
local nixz = 1
local worm_started = false
for z = minp.z, maxp.z do
local niz
for y = minp.y, maxp.y do
local vi = area:index(minp.x, y, z)
for x = minp.x, maxp.x do
local nix
local terrain_type = terrain_type_2dmap[nixz]
local height = terrain_type * height_map_2dmap[nixz]
if mountain_peak_2dmap[nixz] > 0 then
height = height + mountain_peak_2dmap[nixz]
end
if y <= height + (layer_height * current_layer) then
-- if math.abs(cave_3dmap[nixyz]) < 10 then -- + (y / 400) then
vm_data[vi] = c_stone
-- else
-- vm_data[vi] = c_air
-- end
elseif y <= (layer_height * current_layer) + settings.water_height then
vm_data[vi] = c_water
-- else
-- vm_data[vi] = c_air
end
-- Increment noise index.
nixyz = nixyz + 1
nixz = nixz + 1
-- Increment voxelmanip index along x row.
-- The voxelmanip index increases by 1 when
-- moving by 1 node in the +x direction.
vi = vi + 1
end
nixz = nixz - sidelen
end
nixz = nixz + sidelen
end
vm:set_data(vm_data)
-- vm:set_lighting({day=0, night=0})
vm:update_liquids()
vm:calc_lighting()
vm:write_to_map()
end)