Dimension api
This commit is contained in:
@@ -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 });
|
||||
});
|
||||
+5
-3
@@ -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");
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user