Continue fine-tuning of terrain generation

This commit is contained in:
Andrew Miner
2026-10-19 08:31:08 -06:00
parent bfd2217cd5
commit 7caf75de09
2 changed files with 119 additions and 32 deletions
+118 -30
View File
@@ -1,10 +1,11 @@
-- Imports & Exports -------------------------------------------------------------------------------
local core = _G["core"]
local dump = _G["dump"]
local math = _G["math"]
local VoxelArea = _G["VoxelArea"]
-- Since this script runs inside the mapgen environment, the constants won't be in the global table,
-- so we need to run the file manually.
local tmg_constants_path = core.get_modpath("tmg_constants")
dofile(tmg_constants_path.."/constants.lua")
local C = _G["tmg_constants"]
@@ -13,8 +14,13 @@ local A = C.ALTITUDE
-- Constants ---------------------------------------------------------------------------------------
local data = {}
--[[ The amount of overlap (as a perentage of total weight) between adjacent terrain types. ]]--
local TERRAIN_BLEND = 0.15
--[[ 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,
@@ -26,36 +32,83 @@ local TERRAIN_TYPE_NOISE = core.get_value_noise({
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_abyss", lowest = A.ABYSS, highest = A.DEEP, noise = nil },
{ name = "ocean_shelf", lowest = A.DEEP, highest = A.SHALLOW, noise = nil },
{ name = "ocean_surface", lowest = A.SHALLOW, highest = A.SURFACE, noise = nil },
{ name = "land_plains", lowest = A.SURFACE, highest = A.PLAIN, noise = nil },
{ name = "land_hills", lowest = A.PLAIN, highest = A.HILL, noise = nil },
{ name = "land_moors", lowest = A.HILL, highest = A.MOOR, noise = nil },
{ name = "land_mountains", lowest = A.MOOR, highest = A.MOUNTAIN, noise = nil },
{ 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 },
}
-- Private Functions -------------------------------------------------------------------------------
-- Private -----------------------------------------------------------------------------------------
local private = {}
local private = {
--[[ A shared buffer used to hold raw voxel data during terrain generation. ]]--
data = {}
}
--[[
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.
Args:
- pos:vector - the position whose altitude is wanted
Returns:
- an int indicating the highest ground-level block in the x,z coordinate given
]]--
function private.compute_altitude_at(pos)
local raw_index = (TERRAIN_TYPE_NOISE:get_2d(pos) + 2) / 2 * #TERRAIN_TYPES
raw_index = math.max(1, math.min(#TERRAIN_TYPES, raw_index))
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))
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
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
for index, terrain in ipairs(TERRAIN_TYPES) do
if not terrain.noise then
terrain.noise = private.make_noise_in_range(terrain.lowest, terrain.highest)
end
local factor = math.max(0, math.min(1, 1 - math.abs(raw_index - index)))
result = result + factor * terrain.noise:get_2d(pos)
if pos.x % 64 == 0 and pos.z % 64 == 0 and factor > 0 then
core.debug("raw_index: "..raw_index..", terrain: "..terrain.name..", factor: "..factor)
if overlap > 0 then
local factor = overlap / TERRAIN_BLEND
result = result + factor * terrain.noise:get_2d(pos)
end
end
@@ -63,7 +116,7 @@ function private.compute_altitude_at(pos)
end
--[[
Fill the lowest layer of the world with bedrock nodes.
Fill the lowest layer of the world with bedrock covered by stone.
Args:
- area:VoxelArea - an area calibrated to the area to fill
@@ -74,10 +127,12 @@ 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("tmg_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
data[area:index(x, A.BEDROCK, z)] = bedrock_id
private.data[area:index(x, A.BEDROCK, z)] = bedrock_id
private.data[area:index(x, A.BEDROCK + 1, z)] = stone_id
end
end
end
@@ -101,8 +156,8 @@ function private.fill_ocean(area, fill_min, fill_max)
for y = y_min, y_max do
for z = fill_min.z, fill_max.z do
local index = area:index(x, y, z)
if data[index] == core.CONTENT_AIR then
data[index] = water_id
if private.data[index] == core.CONTENT_AIR then
private.data[index] = water_id
end
end
end
@@ -129,7 +184,7 @@ function private.fill_terrain(area, fill_min, fill_max)
local y_terrain = private.compute_altitude_at(pos)
for y = y_min, math.min(y_max, y_terrain) do
data[area:index(x, y, z)] = stone_id
private.data[area:index(x, y, z)] = stone_id
end
end
end
@@ -158,18 +213,51 @@ function private.make_noise_in_range(lowest, highest)
})
end
--[[
Add computed values to the `TERRAIN_TYPES` table.
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
interpreted values: `noise`, `range_start`, and `range_end`.
This function *must* be called prior to the start of map generation.
]]--
function private.prepare_terrain_table()
local total_weight = 0
for _, terrain in ipairs(TERRAIN_TYPES) do
total_weight = total_weight + terrain.weight
end
local start = 0
for index, terrain in ipairs(TERRAIN_TYPES) do
terrain.noise = private.make_noise_in_range(terrain.lowest, terrain.highest)
terrain.range_start = start
terrain.range_end = start + (terrain.weight / total_weight)
if index == 1 then
terrain.range_start = -1 * TERRAIN_BLEND / 2
end
if index == #TERRAIN_TYPES then
terrain.range_end = terrain.range_end + TERRAIN_BLEND / 2
end
start = terrain.range_end
end
end
-- Registrations -----------------------------------------------------------------------------------
private.prepare_terrain_table()
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)
voxels:get_data(data)
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(data)
voxels:set_data(private.data)
voxels:calc_lighting()
voxels:update_liquids()