// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // starts a game server with its mods: registers their data, then runs the worldgen, shared and server phases of their // scripts. the worker loads built mods, tests can load mods straight from their source folders import { ModLoadError, 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 { dimension_shape, type DimensionJson } from "@/common/worldgen/data.ts"; import { cell_height_problem } from "@/common/world_shape.ts"; import { worldgen_bundle } from "@/common/worldgen_bundle.ts"; import { GameHost, GameServer } from "./game_server.ts"; import type { WorldSource } from "./saves.ts"; import { ModRuntime } from "./mod_runtime.ts"; import { prepare_schematic } from "./schematics.ts"; import { register_entity_type } from "./entity/entity_types.ts"; import { mod_entity_factory } from "./entity/mod_entity.ts"; import { set_effect_types } from "./entity/effects.ts"; import { clear_hooks, expose_engine } from "@/common/hooks.ts"; import { common_engine } from "@/common/engine_modules.ts"; import { server_engine } from "./engine_modules.ts"; export interface ServerModSource { listing: ModListing; data: ModData; // importable url of its script: a data: url in the worker, a file url in tests url?: string; } export async function start_game( host: GameHost, save: WorldSource | undefined, default_seed: string, mods: ServerModSource[], ): Promise { // for engine access mods. the last game's hooks go first, a realm only runs one game at a time clear_hooks(); expose_engine({ common: common_engine(), server: server_engine() }); const recipes = register_mod_data(mods.map((mod) => ({ id: mod.listing.id, data: mod.data }))); const runtime = new ModRuntime(); for (const mod of mods) { for (const json of mod.data.entities) { register_entity_type(json.id, mod_entity_factory(json, runtime.entity_behaviors)); } } const dimension = default_dimension(mods); const generator = dimension.generator; const shape = dimension_shape(dimension); const worldgen_scripts = mods.flatMap((mod) => mod.url ? [{ mod: mod.listing.id, url: mod.url, engine_access: mod.listing.engine_access }] : [] ); const worldgen = await load_worldgen({ mods: mods.map((mod) => mod.listing.id), scripts: worldgen_scripts, ores: recipes.ores, generator, shape, }); const { terrain, caves } = worldgen.generator.settings; const problem = cell_height_problem(shape, [terrain.cell_height, caves.cell_height]); if (problem) throw new Error(`dimension ${dimension.id} can't be generated by ${generator}: ${problem}`); // before the world loads, its mobs can have effects set_effect_types(runtime.effect_types()); const game = new GameServer(host, save, default_seed, { listings: mods.map((mod) => mod.listing), recipes, worldgen, runtime, }); // before scripts, so their server phases can place them for (const mod of mods) { for (const schematic of mod.data.schematics ?? []) { try { game.schematics.set(schematic.id!, prepare_schematic(game, schematic.id!, schematic)); } catch (e) { throw new ModLoadError(mod.listing.id, `schematic ${schematic.id}: ${(e as Error).message}`); } } } const scripts = mods.flatMap((mod) => mod.url ? [{ ...script_of(mod), url: mod.url }] : []); // every mod's shared rules first, so server phases can count on any mod's being there await runtime.load_shared_scripts(game, scripts); await runtime.load_scripts(game, scripts); runtime.finish_setup(); game.find_ticking_tiles(); // what a worldgen worker needs to generate the same chunks, see worldgen_pool.ts game.worldgen_bundle = worldgen_bundle( game.world.seed, generator, shape, mods.map((mod) => ({ id: mod.listing.id, data: mod.data, url: mod.url, engine_access: mod.listing.engine_access, })), ); runtime.after.server_start.emit({}); 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, version: mod.listing.version, engine_access: mod.listing.engine_access ?? false, config: mod.data.config ?? {}, }; }