// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // world generation as data: noises, density functions, noise settings (a generator) and biome sources, see // docs/modding/worldgen/terrain.md. checked here, compiled by density.ts and generator.ts. the names follow // minecraft's (density functions, the noise router, surface rules) where the idea is the same import { ID_PATTERN, is_object, TAG_ID_PATTERN } from "../mod_data.ts"; // ids of worldgen data can have folders, like "bworld:overworld/erosion" export const WORLDGEN_ID_PATTERN = TAG_ID_PATTERN; // noises export interface NoiseJson { id: string; // what it's seeded with, with the world's seed. the id's path when not set seed_name?: string; // the size in blocks of the first octave's features. each octave after it is twice as fine wavelength: number; // each octave's strength, 0 skips one amplitudes: number[]; // 2 (x and z) when not set, or 3 dimensions?: 2 | 3; // 3d noises: how much slower it changes up and down than across vertical_stretch?: number; // the noise's measured percentiles, every 5% (tools/noise_quantiles.ts), for spreading it evenly from -1 to 1. or // another noise's id to use its table, for noises with the same amplitudes quantiles?: number[] | string; } // density functions: a number for every position. a number is a constant, a string is another density function's id export type DensityJson = number | string | DensityObjectJson; export interface DensityObjectJson { type: string; [field: string]: unknown; } // a file in worldgen/density_function: a density function with an id export type DensityFunctionJson = DensityObjectJson & { id: string }; // a range of numbers, any bound can be left out. in range_choice a bound can also be a density function export interface RangeJson { min_inclusive?: number; min_exclusive?: number; max_inclusive?: number; max_exclusive?: number; } export interface SplineJson { coordinate: DensityJson; points: { location: number; value: number | SplineJson; derivative?: number }[]; } // noise settings: a generator export interface NoiseSettingsJson { id: string; sea_level: number; // what everything solid is made of, where the surface rule doesn't say otherwise default_block: string; // density is sampled every cell_width blocks across and each part's cell_height up, and blended in between cell_width: number; terrain: { density: DensityJson; cell_height: number }; // solid only where both terrain and caves are above 0 caves: { density: DensityJson; cell_height: number }; // the ground's height before 3d noise, for surface rules, biomes and finding things far away preliminary_surface: DensityJson; // named values biome rules read, like temperature climate: Record; // a column is steep when its preliminary surface is this much higher or lower than a neighbor's steep: number; biome_source: string; // what fills empty space, the first that applies fluids: FluidJson[]; surface_rule: SurfaceRuleJson; } export interface FluidJson { block: string; // only below this y below: number; // "open_sky": nothing but air above, down from the top of the world. or where a density function is in a range where: "open_sky" | ({ function: DensityJson } & RangeJson); } export type SurfaceRuleJson = | { type: "sequence"; sequence: SurfaceRuleJson[] } | { type: "condition"; if_true: SurfaceConditionJson; then_run: SurfaceRuleJson } | { type: "block"; result_state: { Name: string } } | { type: "biome_surface"; part: BiomeSurfacePart }; export const BIOME_SURFACE_PARTS = ["top", "filler", "underwater_top", "underwater_filler"] as const; export type BiomeSurfacePart = typeof BIOME_SURFACE_PARTS[number]; export type SurfaceConditionJson = // the solid blocks above it before air, 0 for the top one, is at most offset | { type: "stone_depth"; offset: number } // within the biome's filler_depth below the top | { type: "filler_depth" } // at or above the preliminary surface plus offset | { type: "above_preliminary_surface"; offset?: number } // the column's ground, the first solid block from the top, is below sea level - 1 | { type: "surface_underwater" } | { type: "steep" } // the biome's surface has bare_cliffs | { type: "bare_cliffs" } // at or above where a density function says | { type: "y_at_least"; function: DensityJson } | { type: "biome"; biome_is: string[] } | { type: "not"; invert: SurfaceConditionJson }; // biome sources: which biome a column gets export interface BiomeSourceJson { id: string; // adds its rules to another biome source, each before or after one of its rules extends?: string; rules: BiomeRuleJson[]; } export interface BiomeRuleJson { id: string; before?: string; after?: string; // every condition holds: a range of a climate value from the noise settings, of "surface" (the ground's y), or // "steep": true or false. no conditions always holds when?: Record; biome: string; } // what worldgen data refers to, for checking it all exists export interface WorldgenRefs { noises: Set; functions: Set; blocks: Set; biomes: Set; biome_sources: Set; } export function new_refs(): WorldgenRefs { return { noises: new Set(), functions: new Set(), blocks: new Set(), biomes: new Set(), biome_sources: new Set() }; } const ARITHMETIC = ["add", "sub", "mul", "div", "min", "max"]; export const DENSITY_TYPES = [ "constant", "x", "y", "z", "noise", ...ARITHMETIC, "abs", "clamp", "smoothstep", "spline", "range_choice", "cache_2d", ]; const RANGE_KEYS = ["min_inclusive", "min_exclusive", "max_inclusive", "max_exclusive"] as const; export function validate_noise(json: unknown): string[] { const problems: string[] = []; if (!is_object(json)) return ["noise must be an object"]; worldgen_id(json.id, "id", problems); if (json.seed_name !== undefined && typeof json.seed_name !== "string") problems.push("seed_name must be text"); if (!(typeof json.wavelength === "number" && json.wavelength > 0)) problems.push("wavelength must be above 0"); if ( !Array.isArray(json.amplitudes) || json.amplitudes.length === 0 || !json.amplitudes.every((a) => typeof a === "number" && Number.isFinite(a)) || json.amplitudes.every((a) => a === 0) ) { problems.push("amplitudes must be a list of numbers, not all 0"); } if (json.dimensions !== undefined && json.dimensions !== 2 && json.dimensions !== 3) { problems.push("dimensions must be 2 or 3"); } if (json.vertical_stretch !== undefined) { if (json.dimensions !== 3) problems.push("vertical_stretch is only for 3d noises"); if (!(typeof json.vertical_stretch === "number" && json.vertical_stretch > 0)) { problems.push("vertical_stretch must be above 0"); } } const quantiles = json.quantiles; if (quantiles !== undefined && typeof quantiles !== "string") { if ( !Array.isArray(quantiles) || quantiles.length < 2 || !quantiles.every((q, i) => typeof q === "number" && (i === 0 || q > quantiles[i - 1])) ) { problems.push("quantiles must be at least 2 numbers going up, or a noise's id"); } } else if (typeof quantiles === "string") { worldgen_id(quantiles, "quantiles", problems); } return problems; } // the noises a noise file refers to: the one it takes its quantiles from export function noise_refs(json: NoiseJson, refs: WorldgenRefs) { if (typeof json.quantiles === "string") refs.noises.add(json.quantiles); } export function validate_density_function(json: unknown): string[] { if (!is_object(json)) return ["density_function must be an object"]; const problems: string[] = []; worldgen_id(json.id, "id", problems); const { id: _, ...fn } = json; check_density(fn, "", new_refs(), problems); return problems; } export function density_function_refs(json: DensityFunctionJson, refs: WorldgenRefs) { const { id: _, ...fn } = json; check_density(fn, "", refs, []); } // checks a density function, adding what it refers to to refs. at is where it is, for messages export function check_density(json: unknown, at: string, refs: WorldgenRefs, problems: string[]) { const where = at ? `${at}: ` : ""; if (typeof json === "number") { if (!Number.isFinite(json)) problems.push(`${where}must be a finite number`); return; } if (typeof json === "string") { if (WORLDGEN_ID_PATTERN.test(json)) refs.functions.add(json); else problems.push(`${where}"${json}" isn't a density function's id`); return; } if (!is_object(json) || typeof json.type !== "string") { problems.push(`${where}must be a number, a density function's id or { "type": ... }`); return; } const field = (name: string) => check_density(json[name], at ? `${at}.${name}` : name, refs, problems); const number = (name: string) => { if (typeof json[name] !== "number" || !Number.isFinite(json[name])) { problems.push(`${where}${name} must be a number`); } }; switch (json.type) { case "constant": number("argument"); break; case "x": case "y": case "z": break; case "noise": if (typeof json.noise === "string" && WORLDGEN_ID_PATTERN.test(json.noise)) refs.noises.add(json.noise); else problems.push(`${where}noise must be a noise's id`); if (json.even !== undefined && typeof json.even !== "boolean") { problems.push(`${where}even must be true or false`); } for (const axis of ["x", "y", "z"]) if (json[axis] !== undefined) field(axis); break; case "abs": case "cache_2d": field("argument"); break; case "clamp": field("input"); number("min"); number("max"); if ((json.min as number) > (json.max as number)) problems.push(`${where}min is above max`); break; case "smoothstep": field("input"); number("from"); number("to"); if (json.from === json.to) problems.push(`${where}from and to must be different`); break; case "spline": check_spline(json.spline, at ? `${at}.spline` : "spline", refs, problems); break; case "range_choice": field("input"); if (!RANGE_KEYS.some((key) => json[key] !== undefined)) problems.push(`${where}needs at least one bound`); for (const key of RANGE_KEYS) if (json[key] !== undefined) field(key); field("when_in_range"); field("when_out_of_range"); break; default: if (ARITHMETIC.includes(json.type)) { field("argument1"); field("argument2"); } else { problems.push(`${where}type ${json.type} isn't one of ${DENSITY_TYPES.join(", ")}`); } } } function check_spline(json: unknown, at: string, refs: WorldgenRefs, problems: string[]) { if (!is_object(json) || !Array.isArray(json.points) || json.points.length === 0) { problems.push(`${at}: must be { "coordinate": ..., "points": [ ... ] }`); return; } check_density(json.coordinate, `${at}.coordinate`, refs, problems); let last = -Infinity; json.points.forEach((point: unknown, i: number) => { const here = `${at}.points[${i}]`; if (!is_object(point) || typeof point.location !== "number") { problems.push(`${here}: must be { "location", "value", "derivative" }`); return; } if (point.location < last) problems.push(`${here}: locations must go up`); last = point.location; if (typeof point.value !== "number") check_spline(point.value, `${here}.value`, refs, problems); if (point.derivative !== undefined && typeof point.derivative !== "number") { problems.push(`${here}: derivative must be a number`); } }); } export function validate_noise_settings(json: unknown): string[] { if (!is_object(json)) return ["noise_settings must be an object"]; const problems: string[] = []; noise_settings_refs(json, new_refs(), problems); return problems; } export function noise_settings_refs(json: Record, refs: WorldgenRefs, problems: string[] = []) { worldgen_id(json.id, "id", problems); const whole = (name: string, min: number, max: number) => { const value = json[name]; if (!(Number.isInteger(value) && (value as number) >= min && (value as number) <= max)) { problems.push(`${name} must be a whole number from ${min} to ${max}`); } }; whole("sea_level", 0, 256); block_id(json.default_block, "default_block", refs, problems); if (![1, 2, 4, 8, 16].includes(json.cell_width as number)) problems.push("cell_width must be 1, 2, 4, 8 or 16"); for (const part of ["terrain", "caves"]) { const value = json[part]; if (!is_object(value)) { problems.push(`${part} must be { "density", "cell_height" }`); continue; } check_density(value.density, `${part}.density`, refs, problems); if (![1, 2, 4, 8, 16, 32].includes(value.cell_height as number)) { problems.push(`${part}.cell_height must be 1, 2, 4, 8, 16 or 32`); } } check_density(json.preliminary_surface, "preliminary_surface", refs, problems); if (!is_object(json.climate)) { problems.push("climate must be { : }"); } else { for (const [name, value] of Object.entries(json.climate)) { if (!/^[a-z_]+$/.test(name) || name === "surface" || name === "steep") { problems.push(`climate.${name}: names are lowercase letters and _, and not surface or steep`); } check_density(value, `climate.${name}`, refs, problems); } } if (!(typeof json.steep === "number" && json.steep >= 0)) problems.push("steep must be a number, 0 or more"); if (typeof json.biome_source === "string" && WORLDGEN_ID_PATTERN.test(json.biome_source)) { refs.biome_sources.add(json.biome_source); } else { problems.push("biome_source must be a biome source's id"); } if (!Array.isArray(json.fluids)) { problems.push("fluids must be a list"); } else { json.fluids.forEach((fluid: unknown, i: number) => { const at = `fluids[${i}]`; if (!is_object(fluid)) { problems.push(`${at}: must be { "block", "below", "where" }`); return; } block_id(fluid.block, `${at}.block`, refs, problems); if (!Number.isInteger(fluid.below)) problems.push(`${at}.below must be a whole number`); if (fluid.where !== "open_sky") { if (!is_object(fluid.where)) { problems.push(`${at}.where must be "open_sky" or { "function", }`); } else { check_density(fluid.where.function, `${at}.where.function`, refs, problems); check_range(fluid.where, `${at}.where`, problems); } } }); } check_surface_rule(json.surface_rule, "surface_rule", refs, problems); } function check_surface_rule(json: unknown, at: string, refs: WorldgenRefs, problems: string[]) { if (!is_object(json)) { problems.push(`${at}: must be a rule`); return; } switch (json.type) { case "sequence": if (!Array.isArray(json.sequence)) problems.push(`${at}.sequence must be a list of rules`); else json.sequence.forEach((rule, i) => check_surface_rule(rule, `${at}.sequence[${i}]`, refs, problems)); break; case "condition": check_surface_condition(json.if_true, `${at}.if_true`, refs, problems); check_surface_rule(json.then_run, `${at}.then_run`, refs, problems); break; case "block": if (!is_object(json.result_state)) problems.push(`${at}.result_state must be { "Name": }`); else block_id(json.result_state.Name, `${at}.result_state.Name`, refs, problems); break; case "biome_surface": if (!BIOME_SURFACE_PARTS.includes(json.part as BiomeSurfacePart)) { problems.push(`${at}.part must be one of ${BIOME_SURFACE_PARTS.join(", ")}`); } break; default: problems.push(`${at}: type must be sequence, condition, block or biome_surface`); } } function check_surface_condition(json: unknown, at: string, refs: WorldgenRefs, problems: string[]) { if (!is_object(json)) { problems.push(`${at}: must be a condition`); return; } switch (json.type) { case "stone_depth": if (!Number.isInteger(json.offset)) problems.push(`${at}.offset must be a whole number`); break; case "above_preliminary_surface": if (json.offset !== undefined && typeof json.offset !== "number") { problems.push(`${at}.offset must be a number`); } break; case "filler_depth": case "surface_underwater": case "steep": case "bare_cliffs": break; case "y_at_least": check_density(json.function, `${at}.function`, refs, problems); break; case "biome": if (!Array.isArray(json.biome_is) || !json.biome_is.every((b) => typeof b === "string")) { problems.push(`${at}.biome_is must be a list of biome ids`); } else { for (const biome of json.biome_is) refs.biomes.add(biome); } break; case "not": check_surface_condition(json.invert, `${at}.invert`, refs, problems); break; default: problems.push( `${at}: type must be stone_depth, filler_depth, above_preliminary_surface, surface_underwater, steep, ` + `bare_cliffs, y_at_least, biome or not`, ); } } export function validate_biome_source(json: unknown): string[] { if (!is_object(json)) return ["biome_source must be an object"]; const problems: string[] = []; biome_source_refs(json, new_refs(), problems); return problems; } export function biome_source_refs(json: Record, refs: WorldgenRefs, problems: string[] = []) { worldgen_id(json.id, "id", problems); if (json.extends !== undefined) { if (typeof json.extends === "string" && WORLDGEN_ID_PATTERN.test(json.extends)) { refs.biome_sources.add(json.extends); } else problems.push("extends must be a biome source's id"); } if (!Array.isArray(json.rules)) { problems.push("rules must be a list"); return; } json.rules.forEach((rule: unknown, i: number) => { const at = `rules[${i}]`; if (!is_object(rule)) { problems.push(`${at}: must be { "id", "when", "biome" }`); return; } worldgen_id(rule.id, `${at}.id`, problems); const placed = [rule.before, rule.after].filter((id) => id !== undefined); if (json.extends !== undefined && placed.length !== 1) { problems.push(`${at}: a rule added to another biome source needs before or after, one of them`); } if (json.extends === undefined && placed.length > 0) { problems.push(`${at}: before and after are for rules added to another biome source, with extends`); } for (const id of placed) worldgen_id(id, `${at}.before/after`, problems); if (typeof rule.biome === "string" && ID_PATTERN.test(rule.biome)) refs.biomes.add(rule.biome); else problems.push(`${at}.biome must be a biome's id`); if (rule.when !== undefined) { if (!is_object(rule.when)) { problems.push(`${at}.when must be { : , "steep": true or false }`); } else { for (const [name, condition] of Object.entries(rule.when)) { if (name === "steep") { if (typeof condition !== "boolean") problems.push(`${at}.when.steep must be true or false`); } else { check_range(condition, `${at}.when.${name}`, problems); } } } } }); } // a range of numbers, with at least one bound function check_range(json: unknown, at: string, problems: string[]) { if (!is_object(json)) { problems.push(`${at} must be a range like { "min_inclusive": 0 }`); return; } let bounds = 0; for (const key of RANGE_KEYS) { if (json[key] === undefined) continue; bounds++; if (typeof json[key] !== "number") problems.push(`${at}.${key} must be a number`); } if (bounds === 0) problems.push(`${at} needs at least one of ${RANGE_KEYS.join(", ")}`); } // a test of whether a value is in the range, with only the bounds it has: value >= min, value < max export function range_test(range: RangeJson): (value: number) => boolean { const tests: ((value: number) => boolean)[] = []; const { min_inclusive, min_exclusive, max_inclusive, max_exclusive } = range; if (min_inclusive !== undefined) tests.push((value) => value >= min_inclusive); if (min_exclusive !== undefined) tests.push((value) => value > min_exclusive); if (max_inclusive !== undefined) tests.push((value) => value <= max_inclusive); if (max_exclusive !== undefined) tests.push((value) => value < max_exclusive); if (tests.length === 1) return tests[0]; if (tests.length === 2) { const [a, b] = tests; return (value) => a(value) && b(value); } return (value) => tests.every((test) => test(value)); } function worldgen_id(value: unknown, field: string, problems: string[]) { if (!(typeof value === "string" && WORLDGEN_ID_PATTERN.test(value))) { problems.push(`${field} must be an id like "my_mod:thing" or "my_mod:folder/thing"`); } } function block_id(value: unknown, field: string, refs: WorldgenRefs, problems: string[]) { if (typeof value === "string" && ID_PATTERN.test(value)) refs.blocks.add(value); else problems.push(`${field} must be a block's id`); }