diff --git a/common/mod.ts b/common/mod.ts index ef652cf..7286578 100644 --- a/common/mod.ts +++ b/common/mod.ts @@ -38,10 +38,12 @@ import { type BiomeJson, validate_biome } from "./worldgen/biomes.ts"; import { type BiomeSourceJson, type DensityFunctionJson, + type DimensionJson, type NoiseJson, type NoiseSettingsJson, validate_biome_source, validate_density_function, + validate_dimension, validate_noise, validate_noise_settings, } from "./worldgen/data.ts"; @@ -202,6 +204,7 @@ export function read_mod(bytes: Uint8Array, file: string): Mod { validate_biome_source, problems, ) as BiomeSourceJson[], + dimensions: checked(raw_data.dimensions, "dimensions", validate_dimension, problems) as DimensionJson[], features: checked(raw_data.features, "features", validate_feature, problems) as FeatureJson[], config: raw_data.config, }; @@ -223,6 +226,7 @@ export function read_mod(bytes: Uint8Array, file: string): Mod { ...data.density_functions!, ...data.noise_settings!, ...data.biome_sources!, + ...data.dimensions!, ...data.features!, ]; for (const { id } of [...registered, ...data.loot_tables!, ...data.biomes!, ...worldgen]) { @@ -303,6 +307,7 @@ export function client_copy(mod: Mod): Mod { density_functions: _d, noise_settings: _s, biome_sources: _b, + dimensions: _dim, features: _f, config: _c, ...data diff --git a/common/mod_check.ts b/common/mod_check.ts index 5fbe13f..81a2f9a 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -29,6 +29,7 @@ import { type BiomeSourceJson, density_function_refs, type DensityFunctionJson, + type DimensionJson, new_refs, noise_refs, noise_settings_refs, @@ -68,6 +69,7 @@ export interface LoadedMod { density_functions: { file: string; json: DensityFunctionJson }[]; noise_settings: { file: string; json: NoiseSettingsJson }[]; biome_sources: { file: string; json: BiomeSourceJson }[]; + dimensions: { file: string; json: DimensionJson }[]; features: { file: string; json: FeatureJson }[]; // config.json's settings, see docs/modding/misc/config.md config?: Record; @@ -98,6 +100,7 @@ export function mod_data(mod: LoadedMod): ModData { density_functions: mod.density_functions.map((d) => d.json), noise_settings: mod.noise_settings.map((n) => n.json), biome_sources: mod.biome_sources.map((b) => b.json), + dimensions: mod.dimensions.map((d) => d.json), features: mod.features.map((f) => f.json), config: mod.config, }; @@ -177,7 +180,10 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable(); const all_noise_settings: NoiseSettingsJson[] = []; const feature_owners = new Map(); @@ -213,6 +219,10 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable 1) { + errors.push( + `${file}: only one dimension can be the default, and these all are: ${defaults.join(", ")}`, + ); + } + } for (const { file, json } of mod.biome_sources ?? []) { const refs = new_refs(); biome_source_refs(json, refs); diff --git a/common/mod_loader.ts b/common/mod_loader.ts index d4b4057..edc39f8 100644 --- a/common/mod_loader.ts +++ b/common/mod_loader.ts @@ -34,7 +34,13 @@ import { fluid_exists } from "./fluids.ts"; import { resolve_tags, Tags } from "./tags.ts"; import type { LootTableJson } from "./loot.ts"; import { type BiomeJson, register_biome } from "./worldgen/biomes.ts"; -import type { BiomeSourceJson, DensityFunctionJson, NoiseJson, NoiseSettingsJson } from "./worldgen/data.ts"; +import type { + BiomeSourceJson, + DensityFunctionJson, + DimensionJson, + NoiseJson, + NoiseSettingsJson, +} from "./worldgen/data.ts"; import type { FeatureJson } from "./worldgen/features.ts"; import { register_block_item } from "./utils.ts"; import { find_model, type ModelFileJson, type ModelJson, resolve_models } from "./block_models.ts"; @@ -66,6 +72,8 @@ export interface ModData { density_functions?: DensityFunctionJson[]; noise_settings?: NoiseSettingsJson[]; biome_sources?: BiomeSourceJson[]; + // which noise settings the world is generated with, see DimensionJson. server only too + dimensions?: DimensionJson[]; // data features, see common/worldgen/features.ts. server only too features?: FeatureJson[]; // the mod's settings, its config.json's "config", with the server's changes once the server loads it. server only diff --git a/common/worldgen/data.ts b/common/worldgen/data.ts index 9874352..384b90c 100644 --- a/common/worldgen/data.ts +++ b/common/worldgen/data.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later -// world generation as data: noises, density functions, noise settings (a generator) and biome sources, see +// world generation as data: noises, density functions, noise settings (a generator), biome sources and dimensions, see // docs/modding/worldgen/terrain.md. checked here, compiled by density.ts and generator.ts. the names follow // minecraft's (density functions, the noise router, surface rules) where the idea is the same import * as z from "zod"; @@ -130,6 +130,16 @@ export interface BiomeRuleJson { biome: string; } +// dimensions: a world to play and the noise settings that generate it, like neoforge's level stems. the world plays the +// one with "default": true, and only one loaded mod can have one + +export interface DimensionJson { + id: string; + // the noise settings that generate it + generator: string; + default?: boolean; +} + // what worldgen data refers to, for checking it all exists export interface WorldgenRefs { noises: Set; @@ -313,6 +323,12 @@ const biome_source_schema: z.ZodType = z.object({ }); }); +const dimension_schema: z.ZodType = z.strictObject({ + id: worldgen_id, + generator: z.string("must be a noise settings' id").regex(WORLDGEN_ID_PATTERN), + default: z.boolean().optional(), +}); + export function validate_noise(json: unknown): Problems { return find_problems(noise_schema, json); } @@ -329,6 +345,10 @@ export function validate_biome_source(json: unknown): Problems { return find_problems(biome_source_schema, json); } +export function validate_dimension(json: unknown): Problems { + return find_problems(dimension_schema, json); +} + // what worldgen data that passed its checks refers to export function noise_refs(json: NoiseJson, refs: WorldgenRefs) { diff --git a/docs/modding/worldgen/index.md b/docs/modding/worldgen/index.md index 7f3eb1f..dc4a95d 100644 --- a/docs/modding/worldgen/index.md +++ b/docs/modding/worldgen/index.md @@ -203,13 +203,13 @@ feature. ## Terrain -The terrain is data: noises, density functions, a generator and biome rules, see [Terrain](terrain.md). The base game's -works like Minecraft 1.18+, shaped after the [Tectonic](https://github.com/Apollounknowndev/tectonic) mod: continents, -erosion and ridges noises set every column's height and roughness, and a 3D density is carved into caves. Oceans wind -between large continents, with islands out at sea. Mountain ranges rise where erosion is low, and rivers run along the -valleys between ridges, going underground where they reach mountains. The flatter land is split into regions with their -own shapes: valleys and plateaus (with badlands ridges where it's hot), tiered mesas, rolling hills and jungle pillars, -and wetlands and desert dunes. +The terrain is data: noises, density functions, a generator, biome rules and the dimension that picks the generator, see +[Terrain](terrain.md). The base game's works like Minecraft 1.18+, shaped after the +[Tectonic](https://github.com/Apollounknowndev/tectonic) mod: continents, erosion and ridges noises set every column's +height and roughness, and a 3D density is carved into caves. Oceans wind between large continents, with islands out at +sea. Mountain ranges rise where erosion is low, and rivers run along the valleys between ridges, going underground where +they reach mountains. The flatter land is split into regions with their own shapes: valleys and plateaus (with badlands +ridges where it's hot), tiered mesas, rolling hills and jungle pillars, and wetlands and desert dunes. Which biome each column gets is the generator's biome rules, see [Biome Sources](terrain.md#biome-sources), and what a biome looks like is its own file, see [Biomes](biomes.md). diff --git a/docs/modding/worldgen/terrain.md b/docs/modding/worldgen/terrain.md index 54fdcae..94c9367 100644 --- a/docs/modding/worldgen/terrain.md +++ b/docs/modding/worldgen/terrain.md @@ -5,9 +5,9 @@ mod's `worldgen/` folders. The engine only knows how to evaluate them. The base `mods/bworld/worldgen/`, and a mod can add biomes to it with rules of its own. It works like Minecraft's: noises feed **density functions**, a **noise settings** file puts them together into a -generator, and a **biome source** picks biomes. The names and fields follow Minecraft's where the idea is the same, so -the [Minecraft Wiki](https://minecraft.wiki/w/Density_function) explains most of them. Where bworld differs, it says so -below. +generator, a **biome source** picks biomes, and a **dimension** says which generator the world uses. The names and +fields follow Minecraft's where the idea is the same, so the [Minecraft Wiki](https://minecraft.wiki/w/Density_function) +explains most of them. Where bworld differs, it says so below. These files are server only: they're left out of the copy of a `.cubi` players download. @@ -17,6 +17,7 @@ my_mod/ worldgen/density_function/.json # named density functions worldgen/noise_settings/.json # generators worldgen/biome_source/.json # biome rules, or rules added to another mod's + worldgen/dimension/.json # which generator the world uses ``` Ids can have folders, like `bworld:overworld/erosion` in `worldgen/density_function/overworld/erosion.json`. @@ -156,7 +157,7 @@ A generator. The base game's is `worldgen/noise_settings/overworld.json`: | `fluids` | What empty space becomes, the first that applies: below `below`, where there's `open_sky` (nothing but air above) or where a density function is in a range. | | `surface_rule` | What solid blocks become. | -The world uses `bworld:overworld`. The server doesn't start without it. +The world uses the generator its [dimension](#dimensions) names, the base game's is `bworld:overworld`. ## Biome Sources @@ -244,6 +245,37 @@ The base game's: within 20 blocks of the ground, the top block is the biome's un stone on cliffs) above the snow line, stone on bare cliffs, and the biome's `top` otherwise. Below it, the biome's filler down to its `filler_depth`. Everything else is stone. +## Dimensions + +A dimension is a world to play and the generator that makes it, like NeoForge's `dimension/` files (a level stem). The +base game's is `worldgen/dimension/overworld.json`: + +```json +{ + "format_version": 1, + "dimension": { + "id": "bworld:overworld", + "generator": "bworld:overworld", + "default": true + } +} +``` + +| Field | Meaning | +| ----------- | --------------------------------------------------------- | +| `generator` | The [noise settings](#noise-settings) that generate it. | +| `default` | `true` for the dimension the world plays. False if unset. | + +The world plays the dimension with `"default": true`. Exactly one dimension in the loaded mods can be the default: with +none, or more than one, the server doesn't start, and the error names them. A mod can have dimensions that aren't the +default, but nothing plays them yet. Since a mod can't change another mod's files yet, a mod with a default dimension of +its own, like a flat world, can't be loaded with the base game's for now. + +There's one dimension per world for now, there's no travelling between them yet. + +New players spawn in the middle of the world, on the first column, in rings of chunks around 0, 0, whose top block has +collision. Water has none, so they never spawn in the sea, and neither do plants without collision. + ## Changing the Base Game's Terrain A mod can add biomes to it, but can't replace its files yet: an id defined by two mods is an error. Anything that diff --git a/mods/bworld/worldgen/dimension/overworld.json b/mods/bworld/worldgen/dimension/overworld.json new file mode 100644 index 0000000..897ded9 --- /dev/null +++ b/mods/bworld/worldgen/dimension/overworld.json @@ -0,0 +1,8 @@ +{ + "format_version": 1, + "dimension": { + "id": "bworld:overworld", + "generator": "bworld:overworld", + "default": true + } +} diff --git a/server/game/load_mods.ts b/server/game/load_mods.ts index b321efe..ccab844 100644 --- a/server/game/load_mods.ts +++ b/server/game/load_mods.ts @@ -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, diff --git a/server/game/world.ts b/server/game/world.ts index 211aa56..94e76f8 100644 --- a/server/game/world.ts +++ b/server/game/world.ts @@ -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 }; } } diff --git a/server/load_mods.ts b/server/load_mods.ts index fbb974f..6456e88 100644 --- a/server/load_mods.ts +++ b/server/load_mods.ts @@ -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()], diff --git a/tests/dimension_test.ts b/tests/dimension_test.ts new file mode 100644 index 0000000..cb470ca --- /dev/null +++ b/tests/dimension_test.ts @@ -0,0 +1,108 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// dimensions: the world is generated by the default dimension's noise settings, exactly one mod has one, and players +// spawn on ground that has collision +import { assert, assertEquals, assertRejects } from "@std/assert"; +import { copy_dir, test_game } from "./helpers.ts"; + +// flat's ground: glass up to y 69, only east of x 16. west of it is open sea up to y 59 +const GROUND_TOP = 69; +const SEA_TOP = 59; +const GROUND_FROM_X = 16; + +// mods/bworld, and flat when flat is true: a generator of its own and a dimension using it. which mods say their +// dimension is the default is up to the test +function mods(options: { flat: boolean; bworld_default: boolean; flat_default?: boolean }) { + const dir = Deno.makeTempDirSync({ prefix: "bworld_dimensions_" }); + copy_dir("mods/bworld", `${dir}/bworld`); + const bworld_dimension = `${dir}/bworld/worldgen/dimension/overworld.json`; + const json = JSON.parse(Deno.readTextFileSync(bworld_dimension)); + json.dimension.default = options.bworld_default; + Deno.writeTextFileSync(bworld_dimension, JSON.stringify(json)); + if (!options.flat) return dir; + + const write = (path: string, json: unknown) => { + Deno.mkdirSync(`${dir}/flat/${path.split("/").slice(0, -1).join("/")}`, { recursive: true }); + Deno.writeTextFileSync(`${dir}/flat/${path}`, JSON.stringify(json)); + }; + write("manifest.json", { + format_version: 1, + id: "flat", + name: "Flat", + version: "1.0.0", + dependencies: [{ id: "bworld", version: "*" }], + }); + write("worldgen/biome_source/flat.json", { + format_version: 1, + biome_source: { id: "flat:flat", rules: [{ id: "flat:beach", biome: "bworld:beach" }] }, + }); + // solid where both the y and the x parts are above 0 + const below = (top: number) => ({ type: "sub", argument1: top + 0.5, argument2: { type: "y" } }); + write("worldgen/noise_settings/flat.json", { + format_version: 1, + noise_settings: { + id: "flat:flat", + sea_level: SEA_TOP + 1, + default_block: "bworld:glass", + cell_width: 1, + terrain: { + density: { + type: "min", + argument1: below(GROUND_TOP), + argument2: { type: "sub", argument1: { type: "x" }, argument2: GROUND_FROM_X - 0.5 }, + }, + cell_height: 1, + }, + caves: { density: 1, cell_height: 1 }, + preliminary_surface: GROUND_TOP + 1, + climate: {}, + steep: 100, + biome_source: "flat:flat", + fluids: [{ block: "bworld:water", below: SEA_TOP + 1, where: "open_sky" }], + surface_rule: { type: "block", result_state: { Name: "bworld:glass" } }, + }, + }); + write("worldgen/dimension/flat.json", { + format_version: 1, + dimension: { id: "flat:flat", generator: "flat:flat", default: options.flat_default ?? true }, + }); + return dir; +} + +Deno.test("the world is generated by the default dimension's noise settings, from any mod", async () => { + const dir = mods({ flat: true, bworld_default: false }); + const { game } = await test_game(dir); + Deno.removeSync(dir, { recursive: true }); + const world = game.world; + for (const z of [-20, 0, 37]) { + assertEquals(world.get_block_id(40, GROUND_TOP, z), "bworld:glass"); + assertEquals(world.get_block_id(40, GROUND_TOP + 1, z), "cubi:air"); + assertEquals(world.get_block_id(-40, SEA_TOP, z), "bworld:water"); + assertEquals(world.get_block_id(-40, SEA_TOP + 1, z), "cubi:air"); + } +}); + +Deno.test("players spawn on ground with collision, not on the water in the middle of the world", async () => { + const dir = mods({ flat: true, bworld_default: false }); + const { game, join } = await test_game(dir); + Deno.removeSync(dir, { recursive: true }); + const { x, y, z } = join(1, "alice").spawn; + assert(x >= GROUND_FROM_X, `spawned at x ${x}, over the water`); + assertEquals(y, GROUND_TOP + 1); + assert(game.world.get_block_info(Math.floor(x), y - 1, Math.floor(z))?.has_collision); +}); + +Deno.test("the server doesn't start without a default dimension, or with two", async () => { + const none = mods({ flat: true, bworld_default: false, flat_default: false }); + await assertRejects(() => test_game(none), Error, "no mod has a default dimension"); + Deno.removeSync(none, { recursive: true }); + + const two = mods({ flat: true, bworld_default: true }); + await assertRejects( + () => test_game(two), + Error, + "only one dimension can be the default, and these all are: bworld:overworld (bworld), flat:flat (flat)", + ); + Deno.removeSync(two, { recursive: true }); +}); diff --git a/tests/mod_test.ts b/tests/mod_test.ts index 8de8330..7009309 100644 --- a/tests/mod_test.ts +++ b/tests/mod_test.ts @@ -124,10 +124,12 @@ Deno.test("the server loads every .cubi in its folder, dependencies first, and p assertEquals(copy.scripts.worldgen, undefined, "players get the world from the server, not its generator"); assertEquals(copy.scripts.shared, mod.mod.scripts.shared, "players run the shared rules too"); // the terrain's data stays on the server too, the rest is the same - const { noises, density_functions, noise_settings, biome_sources, ...data } = mod.mod.data; - assertEquals(copy.data, { ...data, noises: [], density_functions: [], noise_settings: [], biome_sources: [] }); + const { noises, density_functions, noise_settings, biome_sources, dimensions, ...data } = mod.mod.data; + const none = { noises: [], density_functions: [], noise_settings: [], biome_sources: [], dimensions: [] }; + assertEquals(copy.data, { ...data, ...none }); if (mod.listing.id === "bworld") { - assert([noises, density_functions, noise_settings, biome_sources].every((list) => list!.length > 0)); + const terrain = [noises, density_functions, noise_settings, biome_sources, dimensions]; + assert(terrain.every((list) => list!.length > 0)); } } assert(mods[1].mod.scripts.server, "the server still has it"); diff --git a/tests/terrain_data_test.ts b/tests/terrain_data_test.ts index b1cd36f..6d7d198 100644 --- a/tests/terrain_data_test.ts +++ b/tests/terrain_data_test.ts @@ -103,10 +103,10 @@ Deno.test("a biome rule that looks at a climate value the noise settings don't h } }); -Deno.test("without a generator the server doesn't start", async () => { +Deno.test("without a dimension to play the server doesn't start", async () => { const dir = with_mod("lonely", {}, false); try { - await assertRejects(() => test_game(dir), Error, "there's no generator bworld:overworld"); + await assertRejects(() => test_game(dir), Error, "no mod has a default dimension"); } finally { Deno.removeSync(dir, { recursive: true }); } diff --git a/tools/check_mods.ts b/tools/check_mods.ts index c2d861e..54308a0 100644 --- a/tools/check_mods.ts +++ b/tools/check_mods.ts @@ -32,6 +32,7 @@ import { validate_biome } from "@/common/worldgen/biomes.ts"; import { validate_biome_source, validate_density_function, + validate_dimension, validate_noise, validate_noise_settings, } from "@/common/worldgen/data.ts"; @@ -103,6 +104,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod { density_functions: [], noise_settings: [], biome_sources: [], + dimensions: [], features: [], textures: [], texture_files: new Map(), @@ -183,6 +185,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod { load_data("worldgen/density_function", "density_function", validate_density_function, mod.density_functions); load_data("worldgen/noise_settings", "noise_settings", validate_noise_settings, mod.noise_settings); load_data("worldgen/biome_source", "biome_source", validate_biome_source, mod.biome_sources); + load_data("worldgen/dimension", "dimension", validate_dimension, mod.dimensions); load_data("worldgen/features", "feature", validate_feature, mod.features); for (const file of walk(`${dir}/models`, ".geo.json")) {