wip
This commit is contained in:
+132
-152
@@ -1,193 +1,169 @@
|
||||
-- Imports & Exports -------------------------------------------------------------------------------
|
||||
|
||||
local biomes = _G["jvmi_biomes"]
|
||||
local core = _G["core"]
|
||||
local math = _G["math"]
|
||||
local VoxelArea = _G["VoxelArea"]
|
||||
local mapgen = _G["mapgen"]
|
||||
local table = _G["jvmi_table"]
|
||||
local vector = _G["vector"]
|
||||
|
||||
-- Since this script runs inside the mapgen environment, the constants won't be in the global table,
|
||||
-- yet, so we need to run the file manually.
|
||||
-- Since this script runs inside the mapgen environment, we can't count on the jvmi_constants mod
|
||||
-- to have been loaded. Instead, we just need to load the constants manually.
|
||||
local jvmi_constants_path = core.get_modpath("jvmi_constants")
|
||||
dofile(jvmi_constants_path.."/constants.lua")
|
||||
local C = _G["jvmi_constants"]
|
||||
local A = C.ALTITUDE
|
||||
|
||||
local Region = mapgen.Region
|
||||
|
||||
|
||||
-- Constants ---------------------------------------------------------------------------------------
|
||||
|
||||
--[[ The amount of overlap (as a perentage of total weight) between adjacent terrain types. ]]--
|
||||
local TERRAIN_BLEND = 0.15
|
||||
local AIR_ID = core.CONTENT_AIR
|
||||
local BEDROCK_ID = core.get_content_id("jvmi_nodes:bedrock")
|
||||
local STONE_ID = core.get_content_id("jvmi_nodes:stone")
|
||||
local WATER_ID = core.get_content_id("jvmi_nodes:water_source")
|
||||
|
||||
--[[ The distance from the center of the map at which only the lowest terrain types are used. ]]--
|
||||
local TERRAIN_RADIUS = 3096
|
||||
|
||||
--[[ The noise generator that decides which terrain type is active at a given x,z location. ]]--
|
||||
local TERRAIN_TYPE_NOISE = core.get_value_noise({
|
||||
flags = "eased",
|
||||
lacunarity = 2,
|
||||
octaves = 1,
|
||||
offset = 0,
|
||||
persistence = 0.5,
|
||||
scale = 1.0,
|
||||
seed = 8381909047,
|
||||
spread = { x = 512, y = 512, z = 0 },
|
||||
})
|
||||
|
||||
--[[
|
||||
A table defining the terrain types used in generating the base terrain.
|
||||
|
||||
These must be listed in average altitude order from lowest to highest. The weights are merely
|
||||
relative values, and can be whatever you like. I find it easiest to think of them as
|
||||
percentages and choose value which tally to 100.
|
||||
|
||||
Bear in mind that noise values are adjusted downward are you approach `TERRAIN_RADIUS` meters
|
||||
from the center of the map, so these weights are only going to be fully reflected in the terrain
|
||||
generated at the center of the map, and will progressively favor lower altitudes as you move
|
||||
toward the edges.
|
||||
]]--
|
||||
local TERRAIN_TYPES = {
|
||||
{ name = "ocean_floor", lowest = A.BEDROCK + 10, highest = A.BEDROCK + 26, weight = 5 },
|
||||
{ name = "ocean_abyss", lowest = A.ABYSS, highest = A.DEEP, weight = 15 },
|
||||
{ name = "ocean_shelf", lowest = A.DEEP, highest = A.SHALLOW, weight = 10 },
|
||||
{ name = "ocean_surface", lowest = A.SURFACE - 4, highest = A.SURFACE, weight = 5 },
|
||||
{ name = "land_plains", lowest = A.PLAIN - 8, highest = A.PLAIN, weight = 20 },
|
||||
{ name = "land_hills", lowest = A.HILL - 32, highest = A.HILL, weight = 25 },
|
||||
{ name = "land_moors", lowest = A.MOOR - 16, highest = A.MOOR, weight = 15 },
|
||||
{ name = "land_mountains", lowest = A.MOUNTAIN - 80, highest = A.MOUNTAIN, weight = 5 },
|
||||
}
|
||||
--[[ Create a deep clone of the shared terrain types as we need to augment the entries. ]]--
|
||||
local TERRAIN_TYPES = nil
|
||||
|
||||
|
||||
-- Private -----------------------------------------------------------------------------------------
|
||||
|
||||
local private = {
|
||||
|
||||
--[[ A shared buffer used to hold raw voxel data during terrain generation. ]]--
|
||||
data = {}
|
||||
--[[ A share object which assists in generating regions of the map. ]]--
|
||||
region = Region:new()
|
||||
}
|
||||
|
||||
--[[
|
||||
Compute the altitude of a certain world location from underlying noise generators.
|
||||
|
||||
This function uses a set of individual terrain types defined by `TERRAIN_TYPES`. An overall
|
||||
terrain noise generator (see: `TERRAIN_TYPE_NOISE`) is used to determine which terrain type
|
||||
should appear at a given location. Each terrain type has its own noise generator, which is
|
||||
then used to determine the altitude at the given location.
|
||||
|
||||
To avoid abrupt lines where we shift from one terrain type to another, the `TERRAIN_BLEND`
|
||||
factor indicates the percentage overlap between adjacent zones where values from both noise
|
||||
generators are smoothly blended into a single value.
|
||||
|
||||
Finally, we want the map to make up an island with a smooth transition from higher-altitude
|
||||
terrain in the center to deep ocean terrain at the edges. So, we modify the values from
|
||||
`TERRAIN_TYPE_NOISE` to force them lower and lower as you move toward `TERRAIN_RADIUS`. By the
|
||||
time you reach that distance from the center of the islands, the terrain selection will be
|
||||
pinned to the lowest possible terrain type.
|
||||
Fill the `Region.META.ALTITUDE` meta data for the given region.
|
||||
|
||||
Args:
|
||||
- pos:vector - the position whose altitude is wanted
|
||||
|
||||
Returns:
|
||||
- an int indicating the highest ground-level block in the x,z coordinate given
|
||||
region:Region - a region of the world being generated. The results of the caluations are
|
||||
written back to the same map.
|
||||
]]--
|
||||
function private.compute_altitude_at(pos)
|
||||
local radius = TERRAIN_RADIUS
|
||||
local radius_factor = ((pos.x * pos.x) + (pos.y * pos.y)) / (radius * radius)
|
||||
radius_factor = math.max(0, math.min(1, 1.0 - radius_factor))
|
||||
function private.assign_altitudes(region)
|
||||
local pos = vector:new()
|
||||
|
||||
local raw_index = (TERRAIN_TYPE_NOISE:get_2d(pos) + 2) / 2
|
||||
raw_index = math.max(0, math.min(1, raw_index))
|
||||
raw_index = raw_index * radius_factor
|
||||
region.iterate_columns(function(x, z)
|
||||
local altitude = 0
|
||||
pos.x, pos.y = x, z
|
||||
|
||||
local band_start = raw_index - TERRAIN_BLEND / 2
|
||||
local band_end = raw_index + TERRAIN_BLEND / 2
|
||||
|
||||
local result = 0
|
||||
for _, terrain in ipairs(TERRAIN_TYPES) do
|
||||
local overlap_start = math.max(band_start, terrain.range_start)
|
||||
local overlap_end = math.min(band_end, terrain.range_end)
|
||||
local overlap = overlap_end - overlap_start
|
||||
|
||||
if overlap > 0 then
|
||||
local factor = overlap / TERRAIN_BLEND
|
||||
result = result + factor * terrain.noise:get_2d(pos)
|
||||
for _, blend in ipairs(region:get_meta(Region.META.TERRAIN_BLEND)) do
|
||||
local terrain = TERRAIN_TYPES[blend.terrain_type]
|
||||
local contribution = blend.weight * terrain.noise:get_2d(pos)
|
||||
altitude = altitude + contribution
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return math.floor(result)
|
||||
--[[
|
||||
]]--
|
||||
function private.assign_biomes(region)
|
||||
|
||||
end
|
||||
|
||||
--[[
|
||||
Compute the combination of terrain types which contribute to each map location.
|
||||
|
||||
This function uses a set of individual terrain types defined by `C.TERRAIN_TYPES`. An overall
|
||||
terrain noise generator, `C.TERRAIN_TYPE_NOISE`, is used to determine which terrain type should
|
||||
appear at a given location.
|
||||
|
||||
To avoid abrupt lines where we shift from one terrain type to another, the `C.TERRAIN_BLEND`
|
||||
factor indicates the percentage overlap between adjacent zones where both types are smoothly
|
||||
blended together.
|
||||
|
||||
Finally, we want the map to make up an island with a smooth transition from higher-altitude
|
||||
terrain in the center to deep ocean terrain at the edges. So, we modify the values from
|
||||
`C.TERRAIN_TYPE_NOISE` to force them lower and lower as you move toward `C.TERRAIN_RADIUS`. By
|
||||
the time you reach that distance from the center of the islands, the terrain selection will be
|
||||
pinned to the lowest possible terrain type.
|
||||
|
||||
The results of these calculations are stored in the `TERRAIN_BLEND` and `TERRAIN_TYPE` meta
|
||||
data for the given region.
|
||||
|
||||
Args:
|
||||
region:Region - the region of the world being generated. The results of the caluations are
|
||||
written back to the same object.
|
||||
]]--
|
||||
function private.assign_terrain_meta(region)
|
||||
local radius_squared = C.TERRAIN_RADIUS * C.TERRAIN_RADIUS
|
||||
local pos = { x = 0, y = 0, z = 0 }
|
||||
|
||||
region.iterate_columns(function(x, z)
|
||||
local radius_weight = ((x * x) + (z * z)) / radius_squared
|
||||
radius_weight = math.max(0, math.min(1, 1.0 - radius_weight))
|
||||
|
||||
pos.x, pos.y = x, z
|
||||
local raw_index = (C.TERRAIN_TYPE_NOISE:get_2d(pos) + 2) / 2
|
||||
raw_index = math.max(0, math.min(1, raw_index))
|
||||
raw_index = raw_index * radius_weight
|
||||
|
||||
local band_start = raw_index - C.TERRAIN_BLEND / 2
|
||||
local band_end = raw_index + C.TERRAIN_BLEND / 2
|
||||
|
||||
local max_weight = 0
|
||||
for terrain_type, terrain in ipairs(TERRAIN_TYPES) do
|
||||
local overlap_start = math.max(band_start, terrain.range_start)
|
||||
local overlap_end = math.min(band_end, terrain.range_end)
|
||||
local overlap = overlap_end - overlap_start
|
||||
|
||||
local blend = {}
|
||||
if overlap > 0 then
|
||||
local weight = overlap / C.TERRAIN_BLEND
|
||||
table.append(blend, { weight = weight, terrain_type = terrain_type })
|
||||
|
||||
if weight >= max_weight then
|
||||
max_weight = weight
|
||||
region:set_meta(Region.META.TERRAIN_TYPE, x, z, terrain_type)
|
||||
end
|
||||
end
|
||||
|
||||
region:set_meta(Region.META_TERRAIN_BLEND, x, z, blend)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
--[[
|
||||
Fill the lowest layer of the world with bedrock covered by stone.
|
||||
|
||||
Args:
|
||||
- area:VoxelArea - an area calibrated to the area to fill
|
||||
- fill_min:vector - the minimal corner of the area to fill
|
||||
- fill_max:vector - the maximal corner of the area to fill
|
||||
region:Region - the portion of the world currently being generated
|
||||
]]--
|
||||
function private.fill_bedrock(area, fill_min, fill_max)
|
||||
if fill_min.y > A.BEDROCK then return end
|
||||
|
||||
local bedrock_id = core.get_content_id("jvmi_nodes:bedrock")
|
||||
local stone_id = core.get_content_id("mapgen_stone")
|
||||
|
||||
for x = fill_min.x, fill_max.x do
|
||||
for z = fill_min.z, fill_max.z do
|
||||
private.data[area:index(x, A.BEDROCK, z)] = bedrock_id
|
||||
private.data[area:index(x, A.BEDROCK + 1, z)] = stone_id
|
||||
end
|
||||
end
|
||||
function private.fill_bedrock(region)
|
||||
region.iterate_plane(A.BEDROCK, function(x, y, z)
|
||||
region:set_content_id(x, y, z, BEDROCK_ID)
|
||||
region:set_content_id(x, y + 1, z, STONE_ID)
|
||||
end)
|
||||
end
|
||||
|
||||
--[[
|
||||
Fill in the oceans by replacing air blocks below sea level with water.
|
||||
|
||||
Args:
|
||||
- area:VoxelArea - an area calibrated to the area to fill
|
||||
- fill_min:vector - the minimal corner of the area to fill
|
||||
- fill_max:vector - the maximal corner of the area to fill
|
||||
region:Region - the portion of the world currently being generated
|
||||
]]--
|
||||
function private.fill_ocean(area, fill_min, fill_max)
|
||||
if fill_min.y > A.SURFACE then return end
|
||||
|
||||
local water_id = core.get_content_id("mapgen_water_source")
|
||||
local y_max = math.min(fill_max.y, A.SURFACE)
|
||||
local y_min = math.max(fill_min.y, A.BEDROCK)
|
||||
|
||||
for x = fill_min.x, fill_max.x do
|
||||
for y = y_min, y_max do
|
||||
for z = fill_min.z, fill_max.z do
|
||||
local index = area:index(x, y, z)
|
||||
if private.data[index] == core.CONTENT_AIR then
|
||||
private.data[index] = water_id
|
||||
end
|
||||
end
|
||||
function private.fill_ocean(region)
|
||||
region.iterate_below(A.SURFACE, function(x, y, z)
|
||||
if region:get_content_id(x, y, z) == AIR_ID then
|
||||
region:set_content_id(x, y, z, WATER_ID)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
--[[
|
||||
Fill in base stone terrain using altitude noise.
|
||||
Fill in base stone terrain using the pre-computed altitudes.
|
||||
|
||||
Args:
|
||||
- area:VoxelArea - an area calibrated to the area to fill
|
||||
- fill_min:vector - the minimal corner of the area to fill
|
||||
- fill_max:vector - the maximal corner of the area to fill
|
||||
region:Region - the portion of the world currently being generated
|
||||
]]--
|
||||
function private.fill_terrain(area, fill_min, fill_max)
|
||||
local pos = { x = 0, y = 0, z = 0 }
|
||||
local stone_id = core.get_content_id("mapgen_stone")
|
||||
local y_max = math.min(fill_max.y, A.SKY)
|
||||
local y_min = math.max(fill_min.y, A.BEDROCK)
|
||||
|
||||
for x = fill_min.x, fill_max.x do
|
||||
for z = fill_min.z, fill_max.z do
|
||||
pos.x, pos.y = x, z
|
||||
local y_terrain = private.compute_altitude_at(pos)
|
||||
|
||||
for y = y_min, math.min(y_max, y_terrain) do
|
||||
private.data[area:index(x, y, z)] = stone_id
|
||||
end
|
||||
end
|
||||
end
|
||||
function private.fill_terrain(region)
|
||||
region:iterate_fill(function(x, y, z)
|
||||
local altitude = region:get_meta(Region.META.ALTITUDE)
|
||||
if y > altitude then return end
|
||||
region:set_content_id(x, y, z, STONE_ID)
|
||||
end)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -214,7 +190,7 @@ function private.make_noise_in_range(lowest, highest)
|
||||
end
|
||||
|
||||
--[[
|
||||
Add computed values to the `TERRAIN_TYPES` table.
|
||||
Clone the `C.TERRAIN_TYPES` table and add pre-computed values to it.
|
||||
|
||||
The table is declared with the fewest values which uniquely define a terrain type, however the
|
||||
terrain calculation requires a few extra fields to aid in its work. This function adds the
|
||||
@@ -223,6 +199,8 @@ end
|
||||
This function *must* be called prior to the start of map generation.
|
||||
]]--
|
||||
function private.prepare_terrain_table()
|
||||
TERRAIN_TYPES = table.clone(C.TERRAIN_TYPES)
|
||||
|
||||
local total_weight = 0
|
||||
for _, terrain in ipairs(TERRAIN_TYPES) do
|
||||
total_weight = total_weight + terrain.weight
|
||||
@@ -235,10 +213,10 @@ function private.prepare_terrain_table()
|
||||
terrain.range_end = start + (terrain.weight / total_weight)
|
||||
|
||||
if index == 1 then
|
||||
terrain.range_start = -1 * TERRAIN_BLEND / 2
|
||||
terrain.range_start = -1 * C.TERRAIN_BLEND / 2
|
||||
end
|
||||
if index == #TERRAIN_TYPES then
|
||||
terrain.range_end = terrain.range_end + TERRAIN_BLEND / 2
|
||||
terrain.range_end = terrain.range_end + C.TERRAIN_BLEND / 2
|
||||
end
|
||||
start = terrain.range_end
|
||||
end
|
||||
@@ -248,17 +226,19 @@ end
|
||||
-- Registrations -----------------------------------------------------------------------------------
|
||||
|
||||
core.register_on_generated(function(voxels, fill_min, fill_max)
|
||||
local emerged_min, emerged_max = voxels:get_emerged_area()
|
||||
local area = VoxelArea(emerged_min, emerged_max)
|
||||
local region = private.region
|
||||
region:read(voxels, fill_min, fill_max)
|
||||
|
||||
voxels:get_data(private.data)
|
||||
private.fill_terrain(area, fill_min, fill_max)
|
||||
private.fill_bedrock(area, fill_min, fill_max)
|
||||
private.fill_ocean(area, fill_min, fill_max)
|
||||
voxels:set_data(private.data)
|
||||
private.assign_terrain_meta(region)
|
||||
private.assign_altitudes(region)
|
||||
|
||||
voxels:calc_lighting()
|
||||
voxels:update_liquids()
|
||||
private.fill_terrain(region)
|
||||
private.fill_bedrock(region)
|
||||
private.fill_ocean(region)
|
||||
|
||||
biomes.apply_biomes(region)
|
||||
|
||||
region:save()
|
||||
end)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user