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,
+3 -3
View File
@@ -169,7 +169,8 @@ export class ServerWorld {
#spawn: { x: number; y: number; z: number } | undefined;
// where new players start: the dry land closest to the middle of the world, like minecraft's world spawn
// where new players start: the ground closest to the middle of the world, like minecraft's world spawn. ground is
// a column whose top block has collision, so not water or a plant
spawn_point() {
if (!this.#spawn) {
this.#spawn = this.#find_spawn();
@@ -178,7 +179,6 @@ export class ServerWorld {
}
#find_spawn() {
const water = this.block_ids["bworld:water"];
// chunks in rings around the middle, one column each
for (let radius = 0; radius <= SPAWN_SEARCH_CHUNKS; radius++) {
for (let cz = -radius; cz <= radius; cz++) {
@@ -188,7 +188,7 @@ export class ServerWorld {
const z = cz * CHUNK_SIZE + CHUNK_SIZE / 2;
let y = CHUNK_HEIGHT - 1;
while (y > 0 && this.get_block_nid(x, y, z) === AIR) y--;
if (y > 0 && this.get_block_nid(x, y, z) !== water) {
if (y > 0 && this.get_block_info(x, y, z)?.has_collision) {
return { x: x + 0.5, y: y + 1, z: z + 0.5 };
}
}
+1
View File
@@ -187,6 +187,7 @@ function as_loaded_mod(file: string, mod: Mod): LoadedMod {
density_functions: entries(mod.data.density_functions ?? []),
noise_settings: entries(mod.data.noise_settings ?? []),
biome_sources: entries(mod.data.biome_sources ?? []),
dimensions: entries(mod.data.dimensions ?? []),
features: entries(mod.data.features ?? []),
config: mod.data.config,
textures: [...mod.textures.keys()],