137 lines
4.9 KiB
TypeScript
137 lines
4.9 KiB
TypeScript
// 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 { 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 { 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 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<GameServer> {
|
|
// 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 dimension = default_dimension(mods);
|
|
const generator = dimension.generator;
|
|
const shape = dimension_shape(dimension);
|
|
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,
|
|
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}`);
|
|
|
|
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,
|
|
shape,
|
|
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;
|
|
}
|
|
|
|
// 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 ?? {},
|
|
};
|
|
}
|
|
|
|
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)}`;
|
|
}
|