Files
2026-11-07 12:13:43 -03:00

145 lines
6.1 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the shape of the world and which biome each column gets at each height, from a generator's data (see generator.ts):
// its density functions compiled for one seed. generate_noise_chunk calls density and cave through here, so engine access mods can
// hook them
import { type Density, DensityCompiler } from "./density.ts";
import { type DensityJson, type NoiseSettingsJson, range_test, type RangeJson } from "./data.ts";
import type { Generator } from "./generator.ts";
import { BIOME_CELL_HEIGHT } from "@/common/section.ts";
// what the terrain worked out about a column
export interface TerrainColumn {
x: number;
z: number;
// where the ground's surface is before 3d noise, in blocks
height: number;
// its climate values in the order the noise settings name them, when column was asked for them
climate?: number[];
}
interface CompiledRule {
biome: string;
// climate values by their index in the noise settings' climate, the surface's y, steep, and the biome cell's lowest y
holds(climate: number[], surface: number, steep: boolean, y: number): boolean;
}
export class NoiseTerrain {
readonly seed: string;
readonly settings: NoiseSettingsJson;
readonly compiler: DensityCompiler;
#preliminary: Density;
#terrain: Density;
#caves: Density;
#climate: Density[];
#rules: CompiledRule[];
// whether any rule looks at y or depth. when none do, a column's biome is the same all the way up
readonly by_height: boolean;
constructor(seed: string, generator: Generator) {
this.seed = seed;
this.settings = generator.settings;
this.compiler = new DensityCompiler(seed, generator.sources);
const compile = (json: DensityJson, what: string) => {
try {
return this.compiler.compile(json);
} catch (e) {
throw new Error(`noise settings ${this.settings.id}, ${what}: ${(e as Error).message}`);
}
};
this.#preliminary = compile(this.settings.preliminary_surface, "preliminary_surface");
this.#terrain = compile(this.settings.terrain.density, "terrain");
this.#caves = compile(this.settings.caves.density, "caves");
const names = Object.keys(this.settings.climate);
this.#climate = names.map((name) => compile(this.settings.climate[name], `climate.${name}`));
this.#rules = generator.rules.map((rule) => compile_rule(rule.biome, rule.when ?? {}, names));
this.by_height = generator.rules.some((rule) => "y" in (rule.when ?? {}) || "depth" in (rule.when ?? {}));
}
// with_climate works the climate out too, which is cheap right after the height since they share their inputs
column(x: number, z: number, with_climate = false): TerrainColumn {
const height = this.#preliminary(x, 0, z);
return with_climate ? { x, z, height, climate: this.climate(x, z) } : { x, z, height };
}
// the column's climate values, in the order the noise settings name them
climate(x: number, z: number): number[] {
return this.#climate.map((value) => value(x, 0, z));
}
// positive is solid, before caves
density(_column: TerrainColumn, x: number, y: number, z: number): number {
return this.#terrain(x, y, z);
}
// negative where a cave is
cave(_column: TerrainColumn, x: number, y: number, z: number): number {
return this.#caves(x, y, z);
}
// the first of the biome source's rules that holds for the biome cell from y up, in a column with its ground at
// surface_y. the column's climate is worked out once, and kept on it
biome(column: TerrainColumn, surface_y: number, steep: boolean, y: number): string {
const climate = column.climate ??= this.climate(column.x, column.z);
for (const rule of this.#rules) {
if (rule.holds(climate, surface_y, steep, y)) return rule.biome;
}
throw new Error(
`no rule of biome source ${this.settings.biome_source} holds at ${column.x}, ${y}, ${column.z}`,
);
}
// the highest y in a column where it's this biome, the ground's or a biome cell's lowest one below it (cells start
// at min_y), without generating its chunk. that's from its height before 3d noise, so it can be a little off near
// coasts and cliffs. for searching far away, like /locatebiome
locate_biome(biome: string, x: number, z: number, min_y: number): number | undefined {
const column = this.column(x, z, true);
const ground = Math.round(column.height);
let steep = false;
// most columns are the same either way, the neighbors are only worth sampling when they aren't
if (this.biome(column, ground, false, ground) !== this.biome(column, ground, true, ground)) {
const neighbors = [[x - 1, z], [x + 1, z], [x, z - 1], [x, z + 1]].map(([nx, nz]) => this.column(nx, nz));
steep = neighbors.some((neighbor) => Math.abs(neighbor.height - column.height) >= this.settings.steep);
}
if (this.biome(column, ground, steep, ground) === biome) return ground;
if (!this.by_height) return undefined;
const cell = min_y + Math.floor((ground - min_y) / BIOME_CELL_HEIGHT) * BIOME_CELL_HEIGHT;
for (let y = cell - BIOME_CELL_HEIGHT; y >= min_y; y -= BIOME_CELL_HEIGHT) {
if (this.biome(column, ground, steep, y) === biome) return y;
}
return undefined;
}
}
function compile_rule(biome: string, when: Record<string, RangeJson | boolean>, names: string[]): CompiledRule {
const tests: CompiledRule["holds"][] = [];
for (const [name, condition] of Object.entries(when)) {
if (name === "steep") {
tests.push((_climate, _surface, steep) => steep === condition);
} else if (name === "surface") {
const test = range_test(condition as RangeJson);
tests.push((_climate, surface) => test(surface));
} else if (name === "y") {
const test = range_test(condition as RangeJson);
tests.push((_climate, _surface, _steep, y) => test(y));
} else if (name === "depth") {
const test = range_test(condition as RangeJson);
tests.push((_climate, surface, _steep, y) => test(surface - y));
} else {
const index = names.indexOf(name);
const test = range_test(condition as RangeJson);
tests.push((climate) => test(climate[index]));
}
}
return {
biome,
holds(climate, surface_y, steep, y) {
for (let i = 0; i < tests.length; i++) {
if (!tests[i](climate, surface_y, steep, y)) return false;
}
return true;
},
};
}