Move world generation outside the engine and into mods and data files
This commit is contained in:
+116
-16
@@ -1,31 +1,101 @@
|
||||
// imports mods' worldgen scripts and collects what they register. used by client chunk workers and the game server
|
||||
import type { WorldgenContext } from "$/common/mod_api/worldgen.ts";
|
||||
// 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 { WorldgenSetup } from "./generation.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 async function load_worldgen(
|
||||
scripts: { mod: string; url: string; engine_access?: boolean }[],
|
||||
ores: OreJson[],
|
||||
): Promise<WorldgenSetup> {
|
||||
const setup: WorldgenSetup = { ores, features: [] };
|
||||
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;
|
||||
}
|
||||
|
||||
for (const { mod, url, engine_access } of scripts) {
|
||||
// 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) {
|
||||
if (!id.startsWith(`${mod}:`)) {
|
||||
throw new ModLoadError(mod, `feature ${id} must be in the namespace "${mod}"`);
|
||||
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`);
|
||||
}
|
||||
if (setup.features.some((f) => f.id === id)) {
|
||||
throw new ModLoadError(mod, `feature ${id} is registered twice`);
|
||||
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(", ")}`);
|
||||
}
|
||||
setup.features.push({ id, generate });
|
||||
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}: the base terrain is still built into the engine (see docs/modding_plan.md)`,
|
||||
`can't register terrain ${id}: terrain is data now, see docs/modding/worldgen/terrain.md`,
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -39,5 +109,35 @@ export async function load_worldgen(
|
||||
});
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user