Dimension api

This commit is contained in:
2026-11-05 23:34:43 -03:00
parent 34f001285d
commit 4766c9a808
14 changed files with 251 additions and 27 deletions
+21 -6
View File
@@ -6,6 +6,7 @@
import { register_mod_data } from "@/common/mod_loader.ts";
import type { ModData, ModListing } from "@/common/mod_loader.ts";
import { load_worldgen } from "@/common/worldgen_loader.ts";
import type { DimensionJson } from "@/common/worldgen/data.ts";
import { worldgen_bundle } from "@/common/worldgen_bundle.ts";
import { GameHost, GameServer } from "./game_server.ts";
import type { WorldSource } from "./saves.ts";
@@ -17,10 +18,6 @@ import { clear_hooks, expose_engine } from "@/common/hooks.ts";
import { common_engine } from "@/common/engine_modules.ts";
import { server_engine } from "./engine_modules.ts";
// the generator worlds use: a mod's worldgen/noise_settings, the base game's overworld. the server doesn't start
// without it
const GENERATOR = "bworld:overworld";
export interface ServerModSource {
listing: ModListing;
data: ModData;
@@ -45,6 +42,7 @@ export async function start_game(
for (const json of mod.data.entities) register_entity_type(json.id, mod_entity_factory(json));
}
const generator = default_dimension(mods).generator;
const worldgen_scripts = mods.flatMap((mod) =>
mod.worldgen_url
? [{ mod: mod.listing.id, url: mod.worldgen_url, engine_access: mod.listing.engine_access }]
@@ -54,7 +52,7 @@ export async function start_game(
mods: mods.map((mod) => mod.listing.id),
scripts: worldgen_scripts,
ores: recipes.ores,
generator: GENERATOR,
generator,
});
const runtime = new ModRuntime();
@@ -81,7 +79,7 @@ export async function start_game(
// what a worldgen worker needs to generate the same chunks, see worldgen_pool.ts
game.worldgen_bundle = worldgen_bundle(
game.world.seed,
GENERATOR,
generator,
mods.map((mod) => ({
id: mod.listing.id,
data: mod.data,
@@ -94,6 +92,23 @@ export async function start_game(
return game;
}
// the dimension the world plays, the one a mod's worldgen/dimension says is the default. the server doesn't start
// without exactly one
function default_dimension(mods: ServerModSource[]): DimensionJson {
const defaults = mods.flatMap((mod) =>
(mod.data.dimensions ?? []).filter((json) => json.default).map((json) => ({ mod: mod.listing.id, json }))
);
if (defaults.length === 0) {
const ids = mods.map((mod) => mod.listing.id).join(", ") || "none";
throw new Error(`no mod has a default dimension, so there's no world to play. the mods are: ${ids}`);
}
if (defaults.length > 1) {
const which = defaults.map(({ mod, json }) => `${json.id} (${mod})`).join(", ");
throw new Error(`only one dimension can be the default, and these all are: ${which}`);
}
return defaults[0].json;
}
function script_of(mod: ServerModSource) {
return {
mod: mod.listing.id,