Files
2026-10-30 21:16:18 -06:00

258 lines
8.4 KiB
Lua

-- Imports & Exports -------------------------------------------------------------------------------
local core = _G.core
local jvmi = _G.jvmi
local vector = _G.vector
local biomes = jvmi.require_mod("jvmi_biomes")
local C = jvmi.C
local check = jvmi.check
local Region = jvmi.Region
local table = jvmi.table
-- Constants ---------------------------------------------------------------------------------------
local A = C.ALTITUDE
local TERRAIN_TYPES = nil
local TERRAIN_TYPE_NOISE = nil
-- Content Ids -----------------------------------------------------
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")
-- Private -----------------------------------------------------------------------------------------
local private = {
--[[ A shared object which assists in generating regions of the map. ]]--
region = Region:new()
}
--[[
Fill the `Region.META.ALTITUDE` meta data for the given region.
Args:
region:Region - a region of the world being generated. The results of the caluations are
written back to the same map.
]]--
function private.assign_altitudes(region)
local pos = vector:new(0, 0, 0)
region:iterate_columns(function(x, z)
local altitude = 0
pos.x, pos.y = x, z -- odd, but as required by get_2d
for _, blend in ipairs(region:get_meta(Region.META.TERRAIN_BLEND, x, z)) do
local terrain = TERRAIN_TYPES[blend.terrain_type]
local contribution = blend.weight * terrain.noise:get_2d(pos)
altitude = altitude + contribution
end
altitude = math.floor(altitude)
region:set_meta(Region.META.ALTITUDE, x, z, altitude)
end)
end
--[[
Compute the combination of terrain types which contribute to each map location.
This function uses a set of individual terrain types defined by `TERRAIN_TYPES`. An overall
terrain noise generator, `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
`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 = (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
local blend = {}
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
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
end
region:set_meta(Region.META.TERRAIN_BLEND, x, z, blend)
end)
end
--[[
Fill the lowest layer of the world with bedrock covered by stone.
Args:
region:Region - the portion of the world currently being generated
]]--
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:
region:Region - the portion of the world currently being generated
]]--
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
--[[
Fill in base stone terrain using the pre-computed altitudes.
Args:
region:Region - the portion of the world currently being generated
]]--
function private.fill_terrain(region)
region:iterate_fill(function(x, y, z)
local altitude = region:get_meta(Region.META.ALTITUDE, x, z)
check.is_type("altitude", altitude, "number")
if y > altitude then return end
region:set_content_id(x, y, z, STONE_ID)
end)
end
--[[
Make a ValueNoise source scaled to a certain altitude range.
Args:
- highest:int - the highest altitude desired in the range
- lowest:int - the lowest altitude desired in the range
Return:
- a ValueNoise object
]]--
function private.make_noise_in_range(lowest, highest)
return core.get_value_noise({
flags = "eased",
lacunarity = 2,
octaves = 5,
offset = lowest,
persistence = 0.5,
scale = (highest - lowest) / 2,
seed = 16552167,
spread = { x = 128, y = 128, z = 0 },
})
end
--[[ Perform any setup this module requires when it first loads. ]]--
function private.on_load()
private.prepare_terrain_table()
TERRAIN_TYPE_NOISE = core.get_value_noise(C.TERRAIN_TYPE_NOISE_PARAMS)
end
--[[
Starting from scratch, populate all the blocks in the indicated region.
Args:
voxels:VoxelManip - a voxel manipulator containing the current state of the region
fill_min:vector - the lowest corner of the region to fill
fill_max:vector - the highest corner of the region to fill
]]--
function private.populate_region(voxels, fill_min, fill_max)
local region = private.region
region:read_voxels(voxels, fill_min, fill_max)
private.assign_terrain_meta(region)
private.assign_altitudes(region)
private.fill_terrain(region)
private.fill_bedrock(region)
private.fill_ocean(region)
biomes.apply_to(region)
region:save_voxels()
end
--[[
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
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()
TERRAIN_TYPES = table.clone(C.TERRAIN_TYPES)
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 * C.TERRAIN_BLEND / 2
end
if index == #TERRAIN_TYPES then
terrain.range_end = terrain.range_end + C.TERRAIN_BLEND / 2
end
start = terrain.range_end
end
end
-- Script ------------------------------------------------------------------------------------------
private.on_load()
core.register_on_generated(private.populate_region)