Move world generation outside the engine and into mods and data files

This commit is contained in:
2026-10-11 00:33:59 -03:00
parent afe6e03d4c
commit 3d2b235f64
118 changed files with 8250 additions and 1108 deletions
+125
View File
@@ -20,6 +20,19 @@ import { resolve_tags } from "./tags.ts";
import type { TextureJson } from "./texture_animation.ts";
import { loot_references, type LootTableJson } from "./loot.ts";
import { biome_blocks, type BiomeJson } from "./worldgen/biomes.ts";
import {
biome_source_refs,
type BiomeSourceJson,
density_function_refs,
type DensityFunctionJson,
new_refs,
noise_refs,
noise_settings_refs,
type NoiseJson,
type NoiseSettingsJson,
type WorldgenRefs,
} from "./worldgen/data.ts";
import { feature_refs, type FeatureJson } from "./worldgen/features.ts";
export interface ModReport {
id: string;
@@ -44,6 +57,12 @@ export interface LoadedMod {
tags: { file: string; json: TagJson }[];
loot_tables: { file: string; json: LootTableJson }[];
biomes: { file: string; json: BiomeJson }[];
// the terrain as data, see common/worldgen/data.ts
noises: { file: string; json: NoiseJson }[];
density_functions: { file: string; json: DensityFunctionJson }[];
noise_settings: { file: string; json: NoiseSettingsJson }[];
biome_sources: { file: string; json: BiomeSourceJson }[];
features: { file: string; json: FeatureJson }[];
textures: string[];
// absolute paths by texture id
texture_files: Map<string, string>;
@@ -95,6 +114,16 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
const textures = new Set<string>(engine_textures);
// terrain data by kind, then id
const worldgen_owners: Record<string, Map<string, string[]>> = {
noise: new Map(),
"density function": new Map(),
"noise settings": new Map(),
"biome source": new Map(),
};
const biome_rule_owners = new Map<string, string[]>();
const all_noise_settings: NoiseSettingsJson[] = [];
const feature_owners = new Map<string, string[]>();
for (const mod of mods) {
for (const { json } of mod.blocks) {
@@ -116,6 +145,17 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
for (const { json } of mod.effects) add(effect_owners, json.id, mod.id);
for (const { json } of mod.loot_tables ?? []) loot_tables.add(json.id);
for (const { json } of mod.biomes ?? []) add(biome_owners, json.id, mod.id);
for (const { json } of mod.noises ?? []) add(worldgen_owners.noise, json.id, mod.id);
for (const { json } of mod.density_functions ?? []) add(worldgen_owners["density function"], json.id, mod.id);
for (const { json } of mod.features ?? []) add(feature_owners, json.id, mod.id);
for (const { json } of mod.noise_settings ?? []) {
add(worldgen_owners["noise settings"], json.id, mod.id);
all_noise_settings.push(json);
}
for (const { json } of mod.biome_sources ?? []) {
add(worldgen_owners["biome source"], json.id, mod.id);
for (const rule of json.rules) add(biome_rule_owners, rule.id, mod.id);
}
for (const { json } of mod.tags) {
if (!tag_owners.has(json.registry)) tag_owners.set(json.registry, new Set());
tag_owners.get(json.registry)!.add(json.id);
@@ -275,6 +315,91 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
// mods' conditions and functions are registered by scripts, the server checks them when it starts
for (const id of refs.custom) check_namespace(file, id);
}
// the terrain as data: every id defined once, and everything it uses there
const check_worldgen = (kind: string, file: string, id: string, refs: WorldgenRefs) => {
if ((worldgen_owners[kind].get(id)?.length ?? 0) > 1) {
errors.push(`${file}: ${kind} ${id} is also defined by ${worldgen_owners[kind].get(id)!.join(", ")}`);
}
const exists = (ids: Set<string>, what: string, owners: Map<string, string[]>) => {
for (const used of ids) {
check_namespace(file, used);
if (!owners.has(used)) errors.push(`${file}: ${what} ${used} doesn't exist`);
}
};
exists(refs.noises, "noise", worldgen_owners.noise);
exists(refs.functions, "density function", worldgen_owners["density function"]);
exists(refs.biome_sources, "biome source", worldgen_owners["biome source"]);
exists(refs.biomes, "biome", biome_owners);
for (const block of refs.blocks) uses(file, block, "block");
};
for (const { file, json } of mod.noises ?? []) {
const refs = new_refs();
noise_refs(json, refs);
check_worldgen("noise", file, json.id, refs);
}
for (const { file, json } of mod.density_functions ?? []) {
const refs = new_refs();
density_function_refs(json, refs);
check_worldgen("density function", file, json.id, refs);
}
for (const { file, json } of mod.noise_settings ?? []) {
const refs = new_refs();
noise_settings_refs(json as unknown as Record<string, unknown>, refs);
check_worldgen("noise settings", file, json.id, refs);
}
for (const { file, json } of mod.biome_sources ?? []) {
const refs = new_refs();
biome_source_refs(json as unknown as Record<string, unknown>, refs);
check_worldgen("biome source", file, json.id, refs);
// what its rules look at, in every generator that uses it
const users = all_noise_settings.filter((settings) => settings.biome_source === (json.extends ?? json.id));
for (const settings of users) {
const names = new Set([...Object.keys(settings.climate), "surface", "steep"]);
for (const rule of json.rules) {
for (const name of Object.keys(rule.when ?? {})) {
if (!names.has(name)) {
errors.push(
`${file}: rule ${rule.id} looks at ${name}, which ${settings.id} doesn't have. it has ${
[...names].join(", ")
}`,
);
}
}
}
}
for (const rule of json.rules) {
if ((biome_rule_owners.get(rule.id)?.length ?? 0) > 1) {
errors.push(
`${file}: biome rule ${rule.id} is also defined by ${
biome_rule_owners.get(rule.id)!.join(", ")
}`,
);
}
const target = rule.before ?? rule.after;
if (target !== undefined && !biome_rule_owners.has(target)) {
errors.push(`${file}: rule ${rule.id} goes next to ${target}, which isn't a biome rule`);
}
}
}
for (const { file, json } of mod.features ?? []) {
if ((feature_owners.get(json.id)?.length ?? 0) > 1) {
errors.push(
`${file}: feature ${json.id} is also defined by ${feature_owners.get(json.id)!.join(", ")}`,
);
}
const refs = { blocks: new Set<string>(), biomes: new Set<string>(), custom: new Set<string>() };
feature_refs(json, refs);
for (const block of refs.blocks) uses(file, block, "block");
for (const biome of refs.biomes) {
check_namespace(file, biome);
if (!biome_owners.has(biome)) errors.push(`${file}: biome ${biome} doesn't exist`);
}
// check-mods doesn't run scripts, the server checks these when it starts
for (const custom of refs.custom) {
check_namespace(file, custom);
warnings.push(`${file}: ${custom} isn't built in, a worldgen script must register it`);
}
}
for (const { file, json } of mod.biomes ?? []) {
if ((biome_owners.get(json.id)?.length ?? 0) > 1) {
errors.push(`${file}: biome ${json.id} is also defined by ${biome_owners.get(json.id)!.join(", ")}`);