Files
bworld/common/worldgen_loader.ts
T

144 lines
5.1 KiB
TypeScript

// imports mods' worldgen scripts and collects what they and mods' data add to generation: the generator, ores, and
// features, code and data, in the order they run. used by the game server
import type { FeatureBiome, FeatureChunk, FeatureStep, WorldgenContext } from "$/common/mod_api/worldgen.ts";
import type { OreJson } from "$/common/mod_data.ts";
import type { SetupFeature, WorldgenSetup } from "./generation.ts";
import { EverythingRegistry } from "./everything_registry.ts";
import { ModLoadError } from "./mod_loader.ts";
import { loading_mod } from "./hooks.ts";
import { compile_generator } from "./worldgen/generator.ts";
import type { Biome } from "./worldgen/biomes.ts";
import {
compile_feature,
DEFAULT_STEP,
FEATURE_STEPS,
type FeatureJson,
type FeatureType,
type PlacementModifier,
} from "./worldgen/features.ts";
export interface WorldgenSources {
// every mod's id, in load order
mods: string[];
scripts: { mod: string; url: string; engine_access?: boolean }[];
ores: OreJson[][];
// the noise settings the world uses. it and data features come from mods' data, which is already registered
generator: string;
}
// a feature before it's put in order
interface Pending {
feature: SetupFeature;
mod: string;
step: FeatureStep;
// data features go before a mod's code features, sorted by id. code features keep the order they were registered
data: boolean;
order: number;
}
export async function load_worldgen(sources: WorldgenSources): Promise<WorldgenSetup> {
const setup: WorldgenSetup = {
generator: compile_generator(sources.generator),
ores: sources.ores,
features: [],
placeable: new Map(),
};
const pending: Pending[] = [];
const types = new Map<string, FeatureType>();
const modifiers = new Map<string, PlacementModifier>();
const data_features = (EverythingRegistry.get_registry<FeatureJson>("features") ?? []).filter((f) => f);
const biomes = (EverythingRegistry.get_registry<Biome>("biomes") ?? []).filter((b) => b).map((
biome,
): FeatureBiome => ({
id: biome.id,
...(biome.trees
? { trees: { ...biome.trees, kinds: [...biome.trees.kinds], wood: { ...biome.trees.wood } } }
: {}),
}));
for (const { mod, url, engine_access } of sources.scripts) {
const own = (id: string, what: string) => {
if (!id.startsWith(`${mod}:`)) {
throw new ModLoadError(mod, `${what} ${id} must be in the namespace "${mod}"`);
}
};
const ctx: WorldgenContext = {
register_feature(id, generate, options = {}) {
own(id, "feature");
if (pending.some((p) => p.feature.id === id) || data_features.some((f) => f.id === id)) {
throw new ModLoadError(mod, `feature ${id} is registered twice, as code or data`);
}
const step = options.step ?? DEFAULT_STEP;
if (!FEATURE_STEPS.includes(step)) {
throw new ModLoadError(mod, `feature ${id}: step must be one of ${FEATURE_STEPS.join(", ")}`);
}
const listed = options.biomes === undefined ? undefined : [options.biomes].flat();
pending.push({
feature: { id, generate, ...(listed ? { biomes: new Set(listed) } : {}) },
mod,
step,
data: false,
order: pending.length,
});
},
register_feature_type(id, place) {
own(id, "feature type");
if (types.has(id)) throw new ModLoadError(mod, `feature type ${id} is registered twice`);
types.set(id, place as FeatureType);
},
register_placement_modifier(id, modify) {
own(id, "placement modifier");
if (modifiers.has(id)) throw new ModLoadError(mod, `placement modifier ${id} is registered twice`);
modifiers.set(id, modify as PlacementModifier);
},
biomes: () => biomes,
register_terrain(id) {
throw new ModLoadError(
mod,
`can't register terrain ${id}: terrain is data now, see docs/modding/worldgen/terrain.md`,
);
},
};
await loading_mod(mod, engine_access ?? false, async () => {
const module = await import(url);
if (typeof module.setup !== "function") {
throw new ModLoadError(mod, "the worldgen script doesn't export a setup function");
}
await module.setup(ctx);
});
}
// data features last, so they can use the types and modifiers every script registered
for (const json of data_features) {
const mod = json.id.split(":")[0];
let compiled;
try {
compiled = compile_feature(json, types, modifiers);
} catch (e) {
throw new ModLoadError(mod, (e as Error).message);
}
const generate = compiled.generate;
setup.placeable.set(json.id, compiled.place);
pending.push({
feature: { id: json.id, generate: (chunk: FeatureChunk) => generate(chunk) },
mod,
step: json.step ?? DEFAULT_STEP,
data: true,
order: 0,
});
}
// by step, then mod, then a mod's data features by id, then its code features as they were registered
const mod_order = new Map(sources.mods.map((mod, i) => [mod, i]));
const order_of = (mod: string) => mod_order.get(mod) ?? sources.mods.length;
pending.sort((a, b) =>
FEATURE_STEPS.indexOf(a.step) - FEATURE_STEPS.indexOf(b.step) ||
order_of(a.mod) - order_of(b.mod) ||
Number(b.data) - Number(a.data) ||
(a.data ? a.feature.id.localeCompare(b.feature.id) : a.order - b.order)
);
setup.features = pending.map((p) => p.feature);
return setup;
}