92 lines
3.8 KiB
TypeScript
92 lines
3.8 KiB
TypeScript
// a generator: noise settings, and the density functions, noises and biome rules it uses. compiled once for each
|
|
// seed, see OverworldTerrain. the data comes from mods' worldgen folders, see docs/modding/worldgen/terrain.md
|
|
import { EverythingRegistry } from "../everything_registry.ts";
|
|
import type { BiomeRuleJson, BiomeSourceJson, DensityFunctionJson, NoiseJson, NoiseSettingsJson } from "./data.ts";
|
|
import type { DensitySources } from "./density.ts";
|
|
import { OverworldTerrain } from "./terrain.ts";
|
|
|
|
// everything worldgen data mods registered, in load order
|
|
export interface WorldgenData {
|
|
noises: NoiseJson[];
|
|
density_functions: DensityFunctionJson[];
|
|
noise_settings: NoiseSettingsJson[];
|
|
biome_sources: BiomeSourceJson[];
|
|
}
|
|
|
|
export class Generator {
|
|
readonly settings: NoiseSettingsJson;
|
|
// the biome source's rules with every mod's added in, the first that holds picks a column's biome
|
|
readonly rules: BiomeRuleJson[];
|
|
readonly sources: DensitySources;
|
|
#seeds = new Map<string, OverworldTerrain>();
|
|
|
|
// throws when the data doesn't add up, like a density function that doesn't exist
|
|
constructor(id: string, data: WorldgenData) {
|
|
const settings = data.noise_settings.find((settings) => settings.id === id);
|
|
if (!settings) throw new Error(`there's no generator ${id}: no mod has worldgen/noise_settings for it`);
|
|
this.settings = settings;
|
|
const noises = new Map(data.noises.map((noise) => [noise.id, noise]));
|
|
const functions = new Map(data.density_functions.map((fn) => [fn.id, fn]));
|
|
this.sources = { noise: (id) => noises.get(id), function: (id) => functions.get(id) };
|
|
this.rules = merge_rules(settings, data.biome_sources);
|
|
// compiling once finds missing noises and functions, and loops, before any chunk is generated
|
|
this.for_seed("");
|
|
}
|
|
|
|
for_seed(seed: string): OverworldTerrain {
|
|
let terrain = this.#seeds.get(seed);
|
|
if (!terrain) {
|
|
terrain = new OverworldTerrain(seed, this);
|
|
this.#seeds.set(seed, terrain);
|
|
}
|
|
return terrain;
|
|
}
|
|
}
|
|
|
|
// the generator called id, from what mods registered
|
|
export function compile_generator(id: string): Generator {
|
|
const all = <T>(registry: string) => (EverythingRegistry.get_registry<T>(registry) ?? []).filter((v) => v);
|
|
return new Generator(id, {
|
|
noises: all("noises"),
|
|
density_functions: all("density_functions"),
|
|
noise_settings: all("noise_settings"),
|
|
biome_sources: all("biome_sources"),
|
|
});
|
|
}
|
|
|
|
// the biome source the settings name, with the rules of every source that extends it put in, in load order
|
|
function merge_rules(settings: NoiseSettingsJson, sources: BiomeSourceJson[]): BiomeRuleJson[] {
|
|
const base = sources.find((source) => source.id === settings.biome_source && source.extends === undefined);
|
|
if (!base) throw new Error(`noise settings ${settings.id}: biome source ${settings.biome_source} doesn't exist`);
|
|
const rules = [...base.rules];
|
|
for (const source of sources.filter((source) => source.extends === base.id)) {
|
|
for (const rule of source.rules) {
|
|
const target = rule.before ?? rule.after!;
|
|
const at = rules.findIndex((other) => other.id === target);
|
|
if (at < 0) {
|
|
throw new Error(
|
|
`biome source ${source.id}: rule ${rule.id} goes ${rule.before ? "before" : "after"} ${target}, ` +
|
|
`which ${base.id} doesn't have`,
|
|
);
|
|
}
|
|
rules.splice(rule.before ? at : at + 1, 0, rule);
|
|
}
|
|
}
|
|
const names = new Set([...Object.keys(settings.climate), "surface", "steep"]);
|
|
const seen = new Set<string>();
|
|
for (const rule of rules) {
|
|
if (seen.has(rule.id)) throw new Error(`biome source ${base.id}: two rules are called ${rule.id}`);
|
|
seen.add(rule.id);
|
|
for (const name of Object.keys(rule.when ?? {})) {
|
|
if (!names.has(name)) {
|
|
throw new Error(
|
|
`biome rule ${rule.id}: ${name} isn't a climate value of ${settings.id}, which has ${
|
|
[...names].join(", ")
|
|
}`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
return rules;
|
|
}
|