// the shape of the overworld, built the way minecraft 1.18+ does it and shaped after tectonic // (https://github.com/Apollounknowndev/tectonic, MIT): large noises for continents, erosion and ridges feed nested // splines that give each column a target height, how jagged its peaks are and how rough its ground is. a 3d density // around that height decides what's solid, and caves are cut out of it. tectonic's look comes from its splines, // ported here in its units (1 is 128 blocks): oceans that wind between big continents with islands out at sea, // mountain ranges where erosion is low, rivers along the valleys between ridges that go underground through // mountains, and on the flatter land four kinds of region picked by a slow noise and the climate: valleys and // plateaus (and badlands ridges where it's hot), tiered mesas, rolling hills and jungle pillars, and wetlands and dunes import { CHUNK_HEIGHT, SEA_LEVEL } from "$/common/constants.ts"; import { clamp, OctaveNoise2D, OctaveNoise3D, Quantiles, smoothstep } from "./noise.ts"; import { Spline, spline } from "./spline.ts"; // measured with tools/noise_quantiles.ts const QUANTILES_6 = new Quantiles([ -0.833, -0.386, -0.311, -0.256, -0.21, -0.17, -0.133, -0.098, -0.065, -0.032, 0, 0.032, 0.065, 0.098, 0.133, 0.17, 0.21, 0.256, 0.311, 0.386, 0.833, ]); const QUANTILES_4 = new Quantiles([ -0.917, -0.48, -0.393, -0.328, -0.273, -0.222, -0.174, -0.13, -0.086, -0.042, 0, 0.042, 0.086, 0.13, 0.174, 0.222, 0.273, 0.328, 0.393, 0.48, 0.917, ]); const QUANTILES_3 = new Quantiles([ -0.91, -0.468, -0.377, -0.308, -0.249, -0.199, -0.154, -0.113, -0.073, -0.036, 0, 0.036, 0.073, 0.113, 0.154, 0.199, 0.249, 0.308, 0.377, 0.468, 0.91, ]); const QUANTILES_2 = new Quantiles([ -0.962, -0.536, -0.439, -0.368, -0.306, -0.248, -0.195, -0.146, -0.096, -0.046, 0, 0.046, 0.096, 0.146, 0.195, 0.248, 0.306, 0.368, 0.439, 0.536, 0.962, ]); const QUANTILES_TEMPERATURE = new Quantiles([ -0.973, -0.6, -0.509, -0.44, -0.379, -0.319, -0.254, -0.19, -0.127, -0.063, 0, 0.063, 0.127, 0.19, 0.254, 0.319, 0.379, 0.44, 0.509, 0.6, 0.973, ]); // blocks of height per unit of density, how soft the ground's surface is const THICKNESS = 20; // solid ground always ends here, and nothing reaches past the top of the world const TOP_SLIDE_START = CHUNK_HEIGHT - 24; const TOP_SLIDE_END = CHUNK_HEIGHT - 4; const MIN_CAVE_Y = 5; // the climate at a column. continentalness, erosion and ridges are scaled to about tectonic's ranges, temperature and // humidity are spread evenly from -1 to 1 export interface Climate { // tectonic's raw continents: deep ocean below -0.45, ocean to -0.2, the coast to 0, then further and further // inland. islands raise it out at sea continentalness: number; // low is mountains, high is flat land. both ends are flat, see erosion_folded erosion: number; // 0 along valleys, where rivers run, and further from 0 towards the ridges between them ridges: number; temperature: number; humidity: number; } // everything 2d about a column that the density needs, worked out once per column export interface TerrainColumn { climate: Climate; // where the ground's surface is before 3d noise, in blocks height: number; // how much 3d noise moves the ground, in units of density. high where tectonic's terrain is rugged roughness: number; // 0 to 1, how much its peaks are sharp ridges jaggedness: number; // 0 to 1, how close it is to the middle of a river that goes under a mountain here, see cave underground_river: number; } // what tectonic's splines read: the climate, folded, and the small noises its regions are made of type Input = | "continents" | "erosion" | "erosion_folded" | "ridges" | "ridges_folded" | "region" | "regions" | "temperature" | "vegetation" | "lower_tier" | "upper_tier" | "badlands" | "hills" | "wetland" | "pillar"; type TerrainInputs = Record; type S = Spline; const s = (input: Input, points: [number, number | S, number?][]): S => spline(input, points); // tectonic's raw continents from how far the continents noise is from 0: 0 is the middle of an ocean const CONTINENT_SHAPE = s("continents", [[0, 0], [0.4, 0.575, 1], [0.48, 0.68, 1]]); // tectonic's temperature and vegetation indexes, 1 to 5, from its climate parameters const TEMPERATURE_INDEX = s("temperature", [ [-0.48, 1], [-0.42, 2], [-0.18, 2], [-0.12, 3], [0.17, 3], [0.23, 4], [0.52, 4], [0.58, 5], ]); const VEGETATION_INDEX = s("vegetation", [ [-0.38, 1], [-0.32, 2], [-0.13, 2], [-0.07, 3], [0.07, 3], [0.13, 4], [0.27, 4], [0.33, 5], ]); // from here on, tectonic's splines: heights in its units, relative to sea level // a river: down to below sea level where the ridges noise is 0, back up to the land around it const RIVER = s("ridges_folded", [[0.03, -0.06, 0.5], [0.1, 0.01]]); const GENTLE_RIVER = s("ridges_folded", [[0.03, -0.06, 0.5], [0.1, 0.01, 0.1]]); const lower_tier = (low: number, high: number) => s("lower_tier", [[0, low], [0.15, high]]); const badlands = (low: number, high: number) => s("badlands", [[0.45, 0.05], [0.6, low, 1.5], [0.63, high, 0.2]]); // club: valleys between plateaus, with badlands ridges where it's hot const PLATEAUS = s("ridges", [ [-0.5, 0.25, -0.1], [-0.4, lower_tier(0.1, 0.22)], [-0.3, 0.05, -0.8], [-0.1, 0.01], [-0.03, -0.06, -0.5], [0.03, -0.06, 0.5], [0.1, 0.01], [0.3, 0.05, 0.8], [0.4, lower_tier(0.135, 0.28)], [0.5, 0.35, 0.1], ]); const SINGLE_VALLEY = s("ridges", [ [-0.5, 0.25, -0.1], [-0.4, lower_tier(0.1, 0.22)], [-0.3, 0.05, -0.8], [-0.1, 0.01], [-0.03, -0.06, -0.5], [0.03, -0.06, 0.5], [0.075, 0.01], [0.14, 0.05, 0.8], [0.2, lower_tier(0.135, 0.28)], [0.3, 0.35], [0.45, 0.03], ]); const DOUBLE_VALLEY = s("ridges", [ [-0.45, 0.03], [-0.3, 0.25], [-0.2, lower_tier(0.1, 0.22)], [-0.125, 0.05, -0.8], [-0.075, 0.01], [-0.03, -0.06, -0.5], [0.03, -0.06, 0.5], [0.075, 0.01], [0.125, 0.05, 0.8], [0.2, lower_tier(0.135, 0.28)], [0.3, 0.35], [0.45, 0.03], ]); const CLUB = s("temperature", [ [1, PLATEAUS], [2, s("vegetation", [[1, SINGLE_VALLEY], [2, PLATEAUS]])], [3, s("vegetation", [[2, SINGLE_VALLEY], [3, PLATEAUS], [4, PLATEAUS], [5, DOUBLE_VALLEY]])], [4, PLATEAUS], [5, s("ridges_folded", [[0, -0.15, 2.5], [0.1, 0.04], [0.2, badlands(0.15, 0.185)], [0.3, badlands(0.185, 0.5)]])], ]); // heart: rolling hills where it's mild, and in jungles ridges cut by narrow valleys const HEART = s("temperature", [ [ 3, s("vegetation", [ [2, s("ridges_folded", [[0.03, -0.06, 0.5], [0.1, 0.01, 0.1], [0.2, s("hills", [[0, 0.01, 0.225]])]])], [3, GENTLE_RIVER], ]), ], [ 4, s("vegetation", [ [3, GENTLE_RIVER], [ 4, s("ridges", [ [-0.75, 0.05], [-0.175, 0.015], [-0.125, 0.01], [-0.04, -0.06, -0.7], [0.04, -0.06, 0.7], [0.125, 0.01], ]), ], [5, s("ridges_folded", [[0.04, -0.06, 0.7], [0.125, 0.01], [0.175, 0.015], [0.75, 0.05], [0.8, 0.6]])], ]), ], [5, s("ridges_folded", [[0.03, -0.06, 0.5], [0.1, 0.01, 0.15]])], ]); // spade: land in tiers, and tall mesas where it's hot const SPADE = s("temperature", [ [ 4, s("ridges_folded", [ [0.03, -0.06, 0.5], [0.1, 0.01], [0.3, 0.05, 0.8], [0.4, lower_tier(0.1, 0.22)], [0.55, 0.25, 1], [0.6, s("upper_tier", [[0, 0.25], [0.15, 0.45]])], [0.64, 0.5], ]), ], [ 5, s("ridges_folded", [ [0.025, -0.075, 0.03], [0.1, 0.05], [0.175, 0.1, 1.5], [0.2, 0.225, 0.2], [0.4, 0.6, 7], [0.4, 0.6, 0.1], [0.8, 0.7], [0.85, 0.4], [1, 0.4], [1.05, 0.04], ]), ], ]); // diamond: flat land with wetlands, and deserts (their dunes are added on top, see DUNES) const DIAMOND = s("temperature", [ [ 4, s("vegetation", [ [2, GENTLE_RIVER], [ 3, s("ridges_folded", [ [0.03, -0.06, 0.5], [0.1, 0.01], [0.15, s("wetland", [[-0.45, -0.03, 0.03], [-0.3, 0.005], [0.3, 0.005], [0.45, -0.03, -0.03]])], ]), ], ]), ], [5, s("ridges_folded", [[0.03, -0.06, 0.5], [0.1, 0.01, 0.15]])], ]); // which region: by erosion's sign, then the slow region noise const REGIONS = s("erosion", [ [-0.02, s("region", [[-0.1, CLUB], [0.1, HEART]])], [0.04, s("region", [[-0.1, SPADE], [0.1, DIAMOND]])], ]); // the regions, after REGIONS and its height multiplier const REGION = s("regions", [[0, 0, 1]]); const WEAK_REGION = s("regions", [[0, 0, 0.5]]); const HILLS = s("ridges_folded", [[0, -0.1, 1], [0.175, 0.075, 0.3], [0.3, 0.25]]); const LOW_RIDGES = s("ridges_folded", [[0.38, -0.03], [0.45, 0.03]]); const inland = (top: number, high: number, low: number, ridge: number, regions_end: number, hills: number) => s("erosion_folded", [ [0, top], [0.175, high], [0.3, low], [0.375, s("ridges_folded", [[0, -0.1], [0.1, 0.125, 5], [0.1667, 0.225], [0.3, ridge]])], [0.425, WEAK_REGION], [0.5, REGION], [regions_end, REGION], [hills, HILLS], [0.95, HILLS], [1.075, LOW_RIDGES], ]); // shallower than tectonic's, the world goes 64 blocks below sea level instead of 128 const DEEP_OCEAN = -0.33; const OCEAN = -0.17; // target height above sea level: the ocean floor, then coasts, then further inland mountains where erosion is low // and the regions where it's high const OFFSET = s("continents", [ [-0.45, DEEP_OCEAN], [-0.35, OCEAN], [-0.195, -0.14], [ -0.155, s("erosion_folded", [ [0.2, s("ridges_folded", [[0, -0.1], [0.15, 0.15, 3], [0.25, 0.22]]), -0.17], [0.3, s("ridges_folded", [[0.03, -0.06, 0.5], [0.1, 0]])], ]), ], [ 0, s("erosion_folded", [ [0, 0.65], [0.15, 0.425, -1.5], [0.3, 0.33], [0.375, s("ridges_folded", [[0, -0.1], [0.15, 0.15], [0.3, 0.25]])], [0.4, RIVER], [0.95, RIVER], [1.075, LOW_RIDGES], ]), ], [0.1, inland(0.95, 0.55, 0.36, 0.3, 0.825, 0.875)], [0.8, inland(1.1, 0.65, 0.38, 0.33, 0.8, 0.9)], ]); // how jagged the peaks are, 0 to 0.2: only the tallest mountains, deep inland const JAGGEDNESS = s("continents", [[0, 0], [0.175, s("erosion_folded", [[0, 0.2], [0.125, 0]])]]); // tectonic's factor: how steeply density rises below the surface. low is rugged ground full of overhangs, on the // ridges of hilly land const rugged = (ridges: S) => s("erosion_folded", [[0.825, 5.6], [0.875, ridges], [0.975, ridges], [1.025, 5.6]]); const FACTOR = s("continents", [ [-0.19, 5.6], [-0.15, rugged(s("ridges", [[0, 5.6], [0.1, 0.6]]))], [0.03, rugged(s("ridges", [[0, 5.6], [0.1, 0.6]]))], [0.06, rugged(s("ridges_folded", [[0.25, 5.6], [0.4, 1.3]]))], ]); // how tall dunes get, up to 6, in hot flat land of the diamond region const DUNE_HEIGHT = s("temperature", [ [4, 0], [5, s("region", [[0, 0], [0.1, s("ridges_folded", [[0, 0], [0.25, 6]])]])], ]); const DUNES = s("continents", [ [0, 0], [0.05, s("erosion_folded", [[0.4, 0], [0.45, DUNE_HEIGHT], [0.8, DUNE_HEIGHT], [0.85, 0]])], ]); // jungle pillars: tall narrow columns in heart's jungles, on the ridges between its valleys const pillar = (mid: number, high: number) => s("pillar", [[0.05, 0], [0.2, mid, -0.2], [0.5, high, 0.05]]); const JUNGLE_PILLARS = s("region", [ [0, 0], [ 0.1, s("temperature", [ [3, 0], [ 4, s("vegetation", [ [3, 0], [4, s("ridges", [[-0.75, pillar(0.4, 0.7)], [-0.175, pillar(0.25, 0.5)], [-0.125, 0]])], [ 5, s("ridges_folded", [[0.125, 0], [0.175, pillar(0.25, 0.5)], [0.75, pillar(0.45, 0.75)], [ 0.8, 0, ]]), ], ]), ], [5, 0], ]), ], ]); const PILLARS = s("continents", [ [0, 0], [0.1, s("erosion", [[-0.875, 0], [-0.825, JUNGLE_PILLARS], [-0.425, JUNGLE_PILLARS], [-0.375, 0]])], ]); // blocks per unit of tectonic's height. without its vertical scale of 1.125, which is for minecraft's taller world const BLOCKS_PER_UNIT = 128; // the 3d noise's strength at tectonic's usual factor const ROUGHNESS = 0.12; // rivers that reach mountains go under them in a tunnel this tall around sea level, this wide in ridges const RIVER_TUNNEL_BOTTOM = SEA_LEVEL - 5; const RIVER_TUNNEL_TOP = SEA_LEVEL + 4; const RIVER_TUNNEL_WIDTH = 0.035; // only where the ground is this far above the river, otherwise it's an open valley const RIVER_TUNNEL_COVER = 12; export class OverworldTerrain { #continents: OctaveNoise2D; #island_a: OctaveNoise2D; #island_b: OctaveNoise2D; #erosion: OctaveNoise2D; #ridges: OctaveNoise2D; #temperature: OctaveNoise2D; #humidity: OctaveNoise2D; #warp_x: OctaveNoise2D; #warp_z: OctaveNoise2D; #region: OctaveNoise2D; #height_multiplier: OctaveNoise2D; #lower_tier: OctaveNoise2D; #upper_tier: OctaveNoise2D; #badlands: OctaveNoise2D; #hills: OctaveNoise2D; #wetland: OctaveNoise2D; #pillar: OctaveNoise2D; #dune: OctaveNoise2D; #dune_warp: OctaveNoise2D; #mountain_ridges: OctaveNoise2D; #ground: OctaveNoise3D; #cheese: OctaveNoise3D; #spaghetti_a: OctaveNoise3D; #spaghetti_b: OctaveNoise3D; #entrances: OctaveNoise2D; // the amplitudes match the quantile tables above, so each noise can be spread evenly constructor(seed: string) { this.#continents = new OctaveNoise2D(seed, "continents", 2560, [1, 1, 2, 2, 1, 1]); this.#island_a = new OctaveNoise2D(seed, "island_a", 320, [1, 1]); this.#island_b = new OctaveNoise2D(seed, "island_b", 320, [1, 1]); this.#erosion = new OctaveNoise2D(seed, "erosion", 1280, [1, 1, 0, 1, 1]); this.#ridges = new OctaveNoise2D(seed, "ridges", 640, [1, 2, 1]); this.#temperature = new OctaveNoise2D(seed, "temperature", 3072, [1.5, 0, 1]); this.#humidity = new OctaveNoise2D(seed, "humidity", 1536, [1, 1]); this.#warp_x = new OctaveNoise2D(seed, "warp_x", 200, [1, 1]); this.#warp_z = new OctaveNoise2D(seed, "warp_z", 200, [1, 1]); this.#region = new OctaveNoise2D(seed, "region", 2048, [1, 1, 2, 2, 1, 1]); this.#height_multiplier = new OctaveNoise2D(seed, "height_multiplier", 1024, [1, 2, 1]); this.#lower_tier = new OctaveNoise2D(seed, "lower_tier", 512, [1, 2, 1]); this.#upper_tier = new OctaveNoise2D(seed, "upper_tier", 512, [1, 2, 1]); this.#badlands = new OctaveNoise2D(seed, "badlands", 512, [1, 2, 1]); this.#hills = new OctaveNoise2D(seed, "hills", 320, [1, 1]); this.#wetland = new OctaveNoise2D(seed, "wetland", 96, [1, 1]); this.#pillar = new OctaveNoise2D(seed, "pillar", 32, [1, 1]); this.#dune = new OctaveNoise2D(seed, "dune", 96, [1, 1]); this.#dune_warp = new OctaveNoise2D(seed, "dune_warp", 400, [1, 1]); this.#mountain_ridges = new OctaveNoise2D(seed, "mountain_ridges", 256, [1, 1]); this.#ground = new OctaveNoise3D(seed, "ground", 64, [1, 1, 0.5], 1.2); this.#cheese = new OctaveNoise3D(seed, "cheese", 80, [1, 0.5], 0.6); this.#spaghetti_a = new OctaveNoise3D(seed, "spaghetti_a", 64, [1], 0.8); this.#spaghetti_b = new OctaveNoise3D(seed, "spaghetti_b", 64, [1], 0.8); this.#entrances = new OctaveNoise2D(seed, "entrances", 90, [1, 1]); } climate(x: number, z: number): Climate { // a small warp, so coasts and biome edges aren't the noise's smooth blobs const wx = x + this.#warp_x.sample(x, z) * 24; const wz = z + this.#warp_z.sample(x, z) * 24; return { continentalness: this.#continentalness(wx, wz), erosion: QUANTILES_4.even(this.#erosion.sample(wx, wz)) * 1.05, ridges: QUANTILES_3.even(this.#ridges.sample(wx, wz)) * 0.7, temperature: QUANTILES_TEMPERATURE.even(this.#temperature.sample(wx, wz)), humidity: QUANTILES_2.even(this.#humidity.sample(wx, wz)), }; } // like tectonic: land is where the continents noise is far from 0, so oceans wind along where it crosses 0. out // in the deep ocean, islands come up where either of two island noises is high #continentalness(x: number, z: number) { const far = Math.abs(QUANTILES_6.even(this.#continents.sample(x, z))) * 0.85; const continents = -0.8 + CONTINENT_SHAPE.get({ continents: far } as TerrainInputs); if (continents >= -0.5) return continents; // scaled so about 3 in 100 of the deep ocean is island, both noises are spread evenly unlike tectonic's const island = Math.max( QUANTILES_2.even(this.#island_a.sample(x, z)), QUANTILES_2.even(this.#island_b.sample(x, z)), ) * 0.76; const deep = smoothstep(-0.5, -0.8, continents) * 0.7; return Math.max(continents, (island + 0.05) * deep - 0.7); } column(x: number, z: number): TerrainColumn { const climate = this.climate(x, z); const { continentalness, erosion, ridges } = climate; const lower = QUANTILES_3.even(this.#lower_tier.sample(x, z)); const upper = QUANTILES_3.even(this.#upper_tier.sample(x, z)); const inputs: TerrainInputs = { continents: continentalness, erosion, erosion_folded: Math.abs(erosion), ridges, ridges_folded: Math.abs(ridges), region: QUANTILES_6.even(this.#region.sample(x, z)) * 0.5 + 0.1, regions: 0, temperature: TEMPERATURE_INDEX.get({ temperature: climate.temperature } as TerrainInputs), vegetation: VEGETATION_INDEX.get({ vegetation: climate.humidity } as TerrainInputs), lower_tier: Math.abs(lower) * 0.3, upper_tier: Math.abs(upper) * 0.3, badlands: Math.abs(QUANTILES_3.even(this.#badlands.sample(x, z))) * 0.7, hills: Math.abs(QUANTILES_2.even(this.#hills.sample(x, z))), wetland: QUANTILES_2.even(this.#wetland.sample(x, z)) * 0.6, pillar: 1.7 * (QUANTILES_2.even(this.#pillar.sample(x, z)) * 0.7 - 0.35), }; const multiplier = clamp(1.2 + 0.4 * QUANTILES_3.even(this.#height_multiplier.sample(x, z)), 0.8, 1.6); inputs.regions = multiplier * REGIONS.get(inputs); const offset = OFFSET.get(inputs) + PILLARS.get(inputs); let height = offset * BLOCKS_PER_UNIT; // jagged peaks: sharp ridgelines along the tallest mountains const jaggedness = clamp(JAGGEDNESS.get(inputs) / 0.2, 0, 1); if (jaggedness > 0) { const ridge = 1 - Math.abs(this.#mountain_ridges.sample(x, z)); height += jaggedness * 36 * ridge * ridge; } // dunes: long ridges of sand, swept one way const dunes = DUNES.get(inputs) / 6; if (dunes > 0) { const sweep = this.#dune_warp.sample(x, z) * 40; const crest = 1 - Math.abs(this.#dune.sample(x + sweep, z * 0.35)); height += dunes * 7 * crest * crest; } const roughness = ROUGHNESS * 5.6 / FACTOR.get(inputs); const valley = 1 - inputs.ridges_folded / RIVER_TUNNEL_WIDTH; const covered = height > RIVER_TUNNEL_TOP - SEA_LEVEL + RIVER_TUNNEL_COVER; const underground_river = valley > 0 && covered && continentalness > -0.17 ? valley : 0; return { climate, height: SEA_LEVEL + height, roughness, jaggedness, underground_river }; } // positive is solid, before caves density(column: TerrainColumn, x: number, y: number, z: number) { let density = (column.height - y) / THICKNESS + column.roughness * this.#ground.sample(x, y, z); if (y > TOP_SLIDE_START) { density -= smoothstep(TOP_SLIDE_START, TOP_SLIDE_END, y) * 4; } if (y < 1) { density = Math.max(density, 1); } return density; } // negative where a cave is. caves stay under the ground's skin except at entrances, and never break // the floor of oceans and rivers. rivers under mountains are tunnels, see generate_overworld for their water cave(column: TerrainColumn, x: number, y: number, z: number) { if (y < MIN_CAVE_Y) { return 1; } let river = 1; if (column.underground_river > 0 && y >= RIVER_TUNNEL_BOTTOM - 2 && y <= RIVER_TUNNEL_TOP + 2) { const middle = (RIVER_TUNNEL_BOTTOM + RIVER_TUNNEL_TOP) / 2; const half = (RIVER_TUNNEL_TOP - RIVER_TUNNEL_BOTTOM) / 2; river = (Math.max(1 - column.underground_river, Math.abs(y - middle) / half) - 1) * 4; } const depth = column.height - y; if (column.height < SEA_LEVEL + 3 && depth < 14) { return river; } if (depth < 7 && this.#entrances.sample(x, z) < 0.5) { return river; } // cheese caves: big open caverns const cheese = (0.52 - this.#cheese.sample(x, y, z)) * 4; // spaghetti caves: long winding tunnels where two noises are both near zero const a = Math.abs(this.#spaghetti_a.sample(x, y, z)); const b = Math.abs(this.#spaghetti_b.sample(x, y, z)); const spaghetti = (Math.max(a, b) - 0.07) * 8; return Math.min(cheese, spaghetti, river); } }