-- Imports & Exports ------------------------------------------------------------------------------- local core = _G["core"] local dump = _G["dump"] local math = _G["math"] local VoxelArea = _G["VoxelArea"] local tmg_constants_path = core.get_modpath("tmg_constants") dofile(tmg_constants_path.."/constants.lua") local C = _G["tmg_constants"] local A = C.ALTITUDE -- Constants --------------------------------------------------------------------------------------- local data = {} 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 }, }) 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 }, } -- Private Functions ------------------------------------------------------------------------------- local private = {} 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 result = 0 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) end end return math.floor(result) end --[[ Fill the lowest layer of the world with bedrock nodes. 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 ]]-- 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") 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 end 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 ]]-- 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 data[index] == core.CONTENT_AIR then data[index] = water_id end end end end end --[[ Fill in base stone terrain using altitude noise. 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 ]]-- 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 data[area:index(x, y, z)] = stone_id end end 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 -- 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) voxels:get_data(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:calc_lighting() voxels:update_liquids() end)