World gen stuff

This commit is contained in:
2026-11-13 00:01:59 -03:00
parent e013fbe550
commit e8ca9753f5
25 changed files with 645 additions and 162 deletions
+2 -2
View File
@@ -14,7 +14,7 @@ import { load_client_mods } from "@/client/mods.ts";
import { load_mods } from "@/server/load_mods.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { copy_dir, test_game } from "./helpers.ts";
import { copy_dir, GROUND_BLOCKS, test_game } from "./helpers.ts";
// a block for every rock and its bricks, and one more where the script can't read files, like the server's worker
const CONTENT = `
@@ -57,7 +57,7 @@ function rocks_mod(extra = "") {
write("tags/blocks/rocks/rocks.json", { format_version: 1, tag: { values: ["rocks:granite", "rocks:basalt"] } });
write("worldgen/ores.json", {
format_version: 1,
ores: [{ id: "rocks:basalt", replaces: "bworld:stone", min_y: 5, max_y: 200, scale: 0.07, threshold: 0.3 }],
ores: [{ id: "rocks:basalt", replaces: GROUND_BLOCKS, min_y: 5, max_y: 200, scale: 0.07, threshold: 0.3 }],
});
return dir;
}
+38 -1
View File
@@ -25,6 +25,7 @@ function mods(options: {
flat_default?: boolean;
shape?: Pick<DimensionJson, "min_y" | "max_y" | "sky_light">;
ground?: string;
surface_rule?: unknown;
cell_height?: number;
}) {
const dir = Deno.makeTempDirSync({ prefix: "bworld_dimensions_" });
@@ -73,7 +74,8 @@ function mods(options: {
steep: 100,
biome_source: "flat:flat",
fluids: [{ block: "bworld:water", below: SEA_TOP + 1, where: "open_sky" }],
surface_rule: { type: "block", result_state: { Name: options.ground ?? "bworld:glass" } },
surface_rule: options.surface_rule ??
{ type: "block", result_state: { Name: options.ground ?? "bworld:glass" } },
},
});
write("worldgen/dimension/flat.json", {
@@ -96,6 +98,41 @@ Deno.test("the world is generated by the default dimension's noise settings, fro
}
});
Deno.test("a surface rule's density condition holds where its density function is in range", async () => {
const block = (id: string) => ({ type: "block", result_state: { Name: id } });
const dir = mods({
flat: true,
bworld_default: false,
// glass where x is from 30 to 50, and below y 60 too
surface_rule: {
type: "sequence",
sequence: [
{
type: "condition",
if_true: { type: "density", function: { type: "x" }, min_inclusive: 30, max_exclusive: 50 },
then_run: block("bworld:glass"),
},
{
type: "condition",
if_true: { type: "density", function: { type: "y" }, max_exclusive: 60 },
then_run: block("bworld:glass"),
},
block("bworld:stone"),
],
},
});
const { game } = await test_game(dir);
Deno.removeSync(dir, { recursive: true });
const at = (x: number, y: number) => game.world.get_block_id(x, y, 5);
assertEquals([29, 30, 49, 50].map((x) => at(x, GROUND_TOP)), [
"bworld:stone",
"bworld:glass",
"bworld:glass",
"bworld:stone",
]);
assertEquals([at(60, 60), at(60, 59)], ["bworld:stone", "bworld:glass"]);
});
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);
+29 -1
View File
@@ -13,7 +13,10 @@ import { mod_data } from "@/common/mod_check.ts";
import type { ServerModSource } from "@/server/game/load_mods.ts";
import { GameServer } from "@/server/game/game_server.ts";
import { start_game } from "@/server/game/load_mods.ts";
import { apply_config } from "@/server/load_mods.ts";
import { apply_config, load_mods } from "@/server/load_mods.ts";
import { load_client_mods } from "@/client/mods.ts";
import { read_mod } from "@/common/mod.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { AIR_ID, PROTOCOL_VERSION } from "@/common/protocol.ts";
import { decode_level_chunk } from "@/common/chunk_streaming.ts";
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
@@ -274,6 +277,31 @@ export function stone_floor(game: GameServer, radius = 3) {
}
}
// what the base game's terrain is made of underground, for ores in tests to replace
export const GROUND_BLOCKS = "#bworld:raw_rocks";
// the cubi: textures mods can use without a build
export const ENGINE_TEXTURES = ["cubi:missing", ...Array.from({ length: 9 }, (_, i) => `cubi:break_${i}`)];
// registers a folder of mod sources the way a player gets them: packed, loaded by a server, which adds the blocks
// content scripts register, and the copies it serves loaded on the client
export async function load_as_player(sources: string) {
const folder = Deno.makeTempDirSync({ prefix: "bworld_player_mods_" });
try {
for (const mod of await load_and_check(sources)) {
Deno.writeFileSync(`${folder}/${mod.id}.cubi`, await pack_mod(mod));
}
const mods = await load_mods(folder, ENGINE_TEXTURES);
EverythingRegistry.clear();
await load_client_mods(
mods.map((mod) => mod.listing),
mods.map((mod) => read_mod(mod.client_bytes, mod.listing.file)),
);
} finally {
Deno.removeSync(folder, { recursive: true });
}
}
export function copy_dir(from: string, to: string) {
if (Deno.statSync(from).isDirectory) {
Deno.mkdirSync(to, { recursive: true });
+2 -2
View File
@@ -11,7 +11,7 @@ import { load_worldgen } from "@/common/worldgen_loader.ts";
import { create_mod } from "@/tools/new_mod.ts";
import { PLAYER_MENU_CRAFTING, PROTOCOL_VERSION } from "@/common/protocol.ts";
import { recipe_lookup } from "@/common/mod_loader.ts";
import { container_items, copy_dir, test_game } from "./helpers.ts";
import { container_items, copy_dir, GROUND_BLOCKS, test_game } from "./helpers.ts";
// mods/bworld plus a mod made from the template, in a temp folder
function mods_with_template() {
@@ -175,7 +175,7 @@ Deno.test("mod ores generate into the block they replace", async () => {
format_version: 1,
ores: [{
id: "copper_tools:example_block",
replaces: "bworld:stone",
replaces: GROUND_BLOCKS,
min_y: 0,
max_y: 60,
scale: 0.1,
+1 -3
View File
@@ -12,15 +12,13 @@ import { pack_mod } from "@/tools/pack_mod.ts";
import { create_mod } from "@/tools/new_mod.ts";
import { load_mods } from "@/server/load_mods.ts";
import { start_host } from "@/server/host.ts";
import { copy_dir } from "./helpers.ts";
import { copy_dir, ENGINE_TEXTURES } from "./helpers.ts";
import { PROTOCOL_VERSION } from "@/common/protocol.ts";
import { decode_level_chunk, type LevelChunk } from "@/common/chunk_streaming.ts";
import { decode_chunk, encode_chunk } from "@/server/game/saves.ts";
import { list_chunks, read_chunk, write_chunks } from "@/server/regions.ts";
import { section_index } from "@/common/section.ts";
const ENGINE_TEXTURES = ["cubi:missing", ...Array.from({ length: 9 }, (_, i) => `cubi:break_${i}`)];
// mods/bworld and one made from the template, packed into a folder of .cubi files
async function packed_mods() {
const sources = Deno.makeTempDirSync({ prefix: "bworld_mod_src_" });
+2 -14
View File
@@ -8,10 +8,9 @@ import { EverythingRegistry } from "@/common/everything_registry.ts";
import type { ModListing } from "@/common/mod_loader.ts";
import { read_mod } from "@/common/mod.ts";
import { load_client_mods } from "@/client/mods.ts";
import { load_order } from "@/common/mod_check.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { copy_dir, test_game } from "./helpers.ts";
import { copy_dir, load_as_player, test_game } from "./helpers.ts";
// sifts into what its component says, and a shaped recipe made in code. then sifting and washing as testrecipes' own
// types, found by the item put in
@@ -222,16 +221,5 @@ Deno.test("recipes can only be registered while shared scripts set up", async ()
});
Deno.test("every mod's recipe types are registered on a client too, so it loads them", async () => {
const mods = load_order(await load_and_check("mods"));
const packed = await Promise.all(mods.map(async (mod) => read_mod(await pack_mod(mod), `${mod.id}.cubi`)));
const listings = mods.map((mod): ModListing => ({
id: mod.id,
name: mod.id,
version: "1.0.0",
sha256: "",
file: "",
size: 0,
}));
EverythingRegistry.clear();
await load_client_mods(listings, packed);
await load_as_player("mods");
});
+6 -20
View File
@@ -4,14 +4,8 @@
// item tooltips: the game's lines, the ones client scripts add and where they go
import { assertEquals } from "@std/assert";
import { item_tooltip } from "@/client/gui/tooltip.ts";
import { load_client_mods } from "@/client/mods.ts";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import { ItemStack } from "@/common/inventory.ts";
import type { ModListing } from "@/common/mod_loader.ts";
import { read_mod } from "@/common/mod.ts";
import { load_and_check, load_order } from "@/tools/check_mods.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { copy_dir } from "./helpers.ts";
import { copy_dir, load_as_player } from "./helpers.ts";
const SNACK = "tips:snack";
@@ -47,19 +41,11 @@ async function load(client_script: string) {
}),
);
write("scripts/client.ts", client_script);
const mods = load_order(await load_and_check(dir));
const packed = await Promise.all(mods.map(async (mod) => read_mod(await pack_mod(mod), `${mod.id}.cubi`)));
Deno.removeSync(dir, { recursive: true });
const listings: ModListing[] = mods.map((mod) => ({
id: mod.id,
name: mod.id,
version: "1.0.0",
sha256: "",
file: "",
size: 0,
}));
EverythingRegistry.clear();
await load_client_mods(listings, packed);
try {
await load_as_player(dir);
} finally {
Deno.removeSync(dir, { recursive: true });
}
return (id: string) => item_tooltip(new ItemStack(id)).map((line) => line.text);
}