122 lines
5.0 KiB
TypeScript
122 lines
5.0 KiB
TypeScript
// the shape of the world and which biome each column gets, from a generator's data (see generator.ts): its density
|
|
// functions compiled for one seed. generate_overworld calls density and cave through here, so engine access mods can
|
|
// hook them. bworld's overworld is shaped after tectonic (https://github.com/Apollounknowndev/tectonic, MIT), its
|
|
// splines are in mods/bworld/worldgen/density_function/overworld
|
|
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";
|
|
|
|
// 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
|
|
holds(climate: number[], surface: number, steep: boolean): boolean;
|
|
}
|
|
|
|
export class OverworldTerrain {
|
|
readonly seed: string;
|
|
readonly settings: NoiseSettingsJson;
|
|
readonly compiler: DensityCompiler;
|
|
#preliminary: Density;
|
|
#terrain: Density;
|
|
#caves: Density;
|
|
#climate: Density[];
|
|
#rules: CompiledRule[];
|
|
|
|
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));
|
|
}
|
|
|
|
// 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 column, with its ground at surface_y
|
|
biome(column: TerrainColumn, surface_y: number, steep: boolean): string {
|
|
const climate = column.climate ?? this.climate(column.x, column.z);
|
|
for (const rule of this.#rules) {
|
|
if (rule.holds(climate, surface_y, steep)) return rule.biome;
|
|
}
|
|
throw new Error(`no rule of biome source ${this.settings.biome_source} holds at ${column.x}, ${column.z}`);
|
|
}
|
|
|
|
// a column's biome without generating its chunk, from its height before 3d noise, so it can be a little off near
|
|
// coasts and cliffs. for searching far away, like /locatebiome
|
|
estimate_biome(x: number, z: number): { biome: string; y: number } {
|
|
const column = this.column(x, z);
|
|
const y = Math.round(column.height);
|
|
const flat = this.biome(column, y, false);
|
|
// most columns are the same either way, the neighbors are only worth sampling when they aren't
|
|
if (flat === this.biome(column, y, true)) return { biome: flat, y };
|
|
const neighbors = [[x - 1, z], [x + 1, z], [x, z - 1], [x, z + 1]].map(([nx, nz]) => this.column(nx, nz));
|
|
const steep = neighbors.some((neighbor) => Math.abs(neighbor.height - column.height) >= this.settings.steep);
|
|
return { biome: steep ? this.biome(column, y, true) : flat, y };
|
|
}
|
|
}
|
|
|
|
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 {
|
|
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) {
|
|
for (let i = 0; i < tests.length; i++) {
|
|
if (!tests[i](climate, surface_y, steep)) return false;
|
|
}
|
|
return true;
|
|
},
|
|
};
|
|
}
|