// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // starts a game server with its mods: registers their data, then imports worldgen and server scripts. // the worker loads built mods, tests can load mods straight from their source folders 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 { 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 { 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"; // 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; // importable urls of the scripts: data: urls in the worker, file urls in tests server_url?: string; worldgen_url?: string; shared_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 }))); for (const mod of mods) { for (const json of mod.data.entities) register_entity_type(json.id, mod_entity_factory(json)); } const worldgen_scripts = mods.flatMap((mod) => mod.worldgen_url ? [{ mod: mod.listing.id, url: mod.worldgen_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: GENERATOR, }); const runtime = new ModRuntime(); // 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, }); // every mod's shared rules first, so server scripts can count on any mod's being there await runtime.load_shared_scripts( game, mods.flatMap((mod) => mod.shared_url ? [{ ...script_of(mod), url: mod.shared_url }] : []), ); await runtime.load_scripts( game, mods.flatMap((mod) => mod.server_url ? [{ ...script_of(mod), url: mod.server_url }] : []), ); 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, mods.map((mod) => ({ id: mod.listing.id, data: mod.data, worldgen_url: mod.worldgen_url, engine_access: mod.listing.engine_access, })), ); runtime.after.server_start.emit({}); return game; } 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 ?? {}, }; } export function data_url(code: string) { const bytes = new TextEncoder().encode(code); let binary = ""; for (let i = 0; i < bytes.length; i += 0x8000) { binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000)); } return `data:application/javascript;base64,${btoa(binary)}`; }