3D biomes
This commit is contained in:
@@ -7,7 +7,7 @@ import { assert, assertEquals } from "@std/assert";
|
||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { decode_level_chunk, encode_level_chunk, is_within_view } from "@/common/chunk_streaming.ts";
|
||||
import { PROTOCOL_VERSION } from "@/common/protocol.ts";
|
||||
import { ChunkColumn, section_index, to_flat } from "@/common/section.ts";
|
||||
import { BIOME_CELL_HEIGHT, ChunkColumn, section_index, to_flat } from "@/common/section.ts";
|
||||
import { WorldShape } from "@/common/world_shape.ts";
|
||||
import { sent_block, test_game } from "./helpers.ts";
|
||||
|
||||
@@ -22,22 +22,31 @@ Deno.test("a chunk comes back as it was encoded, whatever its sections hold", ()
|
||||
blocks[2 * section + i] = i % 16;
|
||||
blocks[3 * section + i] = (i % 300) | ((i % 7) << 16);
|
||||
}
|
||||
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => i < 100 ? "bworld:plains" : "bworld:desert");
|
||||
const shape = new WorldShape({ min_y: -64, max_y: 320, sky_light: true });
|
||||
// two biomes across, and a third in the cells from the 40th up
|
||||
const biomes = Array.from(
|
||||
{ length: shape.volume / BIOME_CELL_HEIGHT },
|
||||
(_, i) => i >= 40 * CHUNK_AREA ? "test:caves" : i % CHUNK_AREA < 100 ? "bworld:plains" : "bworld:desert",
|
||||
);
|
||||
|
||||
const decoded = decode_level_chunk(encode_level_chunk(ChunkColumn.from_flat(-3, 70000, shape, blocks, biomes)));
|
||||
const column = ChunkColumn.from_flat(-3, 70000, shape, blocks, biomes);
|
||||
const decoded = decode_level_chunk(encode_level_chunk(column));
|
||||
assertEquals(decoded.x, -3);
|
||||
assertEquals(decoded.z, 70000);
|
||||
assertEquals(to_flat(decoded.sections), blocks);
|
||||
assertEquals(decoded.sections.map((section) => section.blocks === null).indexOf(false), 1, "uniform ones stay so");
|
||||
assertEquals(decoded.biomes, biomes);
|
||||
assertEquals(decoded.biomes, column.biomes);
|
||||
const back = new ChunkColumn(-3, 70000, shape, decoded.sections, decoded.biomes);
|
||||
assertEquals(back.biome_at(-48 + 3, 95, 70000 * 16 + 2), "bworld:plains");
|
||||
assertEquals(back.biome_at(-48 + 3, 96, 70000 * 16 + 2), "test:caves");
|
||||
|
||||
// one biome, and the receiver's numbering
|
||||
const one = decode_level_chunk(
|
||||
encode_level_chunk(ChunkColumn.from_flat(0, 0, shape, blocks, biomes.map(() => "bworld:plains"))),
|
||||
encode_level_chunk(ChunkColumn.from_flat(0, 0, shape, blocks, new Array(CHUNK_AREA).fill("bworld:plains"))),
|
||||
(value) => value === 7 ? 70 : value,
|
||||
);
|
||||
assertEquals(one.biomes, biomes.map(() => "bworld:plains"));
|
||||
assertEquals(one.biomes.palette, ["bworld:plains"]);
|
||||
assertEquals(one.biomes.sections, new Array(24).fill(0));
|
||||
assertEquals(one.sections[0].value, 70);
|
||||
assertEquals(one.sections[1].get(0), 1);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the client's level in tests, apart from tests/helpers.ts so tests that don't use it don't load the renderer
|
||||
import { ClientLevel } from "@/client/level/client_level.ts";
|
||||
import { AssetManager } from "@/client/assets.ts";
|
||||
import type { ToChunkWorker } from "@/client/workers/chunk_messages.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import type { WorldShape } from "@/common/world_shape.ts";
|
||||
|
||||
// a client's level for the loaded mods' blocks, numbered the same as the server's, without a gpu or chunk workers: what
|
||||
// it asks its workers to mesh is kept in requests instead of being sent
|
||||
export function client_level(shape: WorldShape) {
|
||||
AssetManager.instance.assets["cubi:textures"] = { width: 16, height: 16 };
|
||||
AssetManager.instance.assets["cubi:textures_info"] = {};
|
||||
const requests: Extract<ToChunkWorker, { type: "mesh" }>[] = [];
|
||||
const real = globalThis.Worker;
|
||||
globalThis.Worker = class {
|
||||
postMessage(message: ToChunkWorker) {
|
||||
if (message.type === "mesh") requests.push(message);
|
||||
}
|
||||
terminate() {}
|
||||
} as unknown as typeof Worker;
|
||||
try {
|
||||
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
|
||||
const level = new ClientLevel(Array.from(blocks, (block) => block?.id), shape, 2);
|
||||
return { level, requests };
|
||||
} finally {
|
||||
globalThis.Worker = real;
|
||||
}
|
||||
}
|
||||
@@ -4,15 +4,13 @@
|
||||
// the client meshes a section at a time, and a block changing only meshes again the sections it can change the look
|
||||
// or the light of, see ClientLevel's #block_changed
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { ClientLevel } from "@/client/level/client_level.ts";
|
||||
import { AssetManager } from "@/client/assets.ts";
|
||||
import { Camera } from "@/client/camera.ts";
|
||||
import type { ToChunkWorker } from "@/client/workers/chunk_messages.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import { EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { ChunkColumn } from "@/common/section.ts";
|
||||
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
import { client_level } from "./client_level.ts";
|
||||
|
||||
const GROUND_TOP = 63;
|
||||
const ROOF = 200;
|
||||
@@ -21,23 +19,7 @@ const ROOF = 200;
|
||||
// its chunk workers is kept in requests instead of being sent
|
||||
async function meshed_level() {
|
||||
await test_game("mods");
|
||||
AssetManager.instance.assets["cubi:textures"] = { width: 16, height: 16 };
|
||||
AssetManager.instance.assets["cubi:textures_info"] = {};
|
||||
const requests: Extract<ToChunkWorker, { type: "mesh" }>[] = [];
|
||||
const real = globalThis.Worker;
|
||||
globalThis.Worker = class {
|
||||
postMessage(message: ToChunkWorker) {
|
||||
if (message.type === "mesh") requests.push(message);
|
||||
}
|
||||
terminate() {}
|
||||
} as unknown as typeof Worker;
|
||||
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
|
||||
let level: ClientLevel;
|
||||
try {
|
||||
level = new ClientLevel(Array.from(blocks, (block) => block?.id), DEFAULT_SHAPE, 2);
|
||||
} finally {
|
||||
globalThis.Worker = real;
|
||||
}
|
||||
const { level, requests } = client_level(DEFAULT_SHAPE);
|
||||
|
||||
const stone = EverythingRegistry.get_id("blocks", "bworld:stone")!;
|
||||
for (let x = -2; x <= 2; x++) {
|
||||
|
||||
@@ -205,7 +205,7 @@ Deno.test("/locatebiome says where the nearest biome of a kind is", async () =>
|
||||
};
|
||||
|
||||
const answer = say("/locatebiome bworld:plains");
|
||||
const [x, , z] = answer.match(/at (-?\d+) (-?\d+) (-?\d+)/)!.slice(1).map(Number);
|
||||
assertEquals(game.world.estimate_biome(x, z).biome, "bworld:plains");
|
||||
const [x, y, z] = answer.match(/at (-?\d+) (-?\d+) (-?\d+)/)!.slice(1).map(Number);
|
||||
assertEquals(game.world.locate_biome("bworld:plains", x, z), y);
|
||||
assert(say("/locatebiome bworld:nowhere").includes("There's no biome bworld:nowhere"));
|
||||
});
|
||||
|
||||
@@ -131,7 +131,7 @@ Deno.test("every morning a crab pot in water uses its bait to catch something, f
|
||||
const column = (ocean: boolean) => {
|
||||
for (let x = -2048; x <= 2048; x += 32) {
|
||||
for (let z = -2048; z <= 2048; z += 32) {
|
||||
if (tags.has("biomes", "bworld:is_ocean", t.game.world.biome_at(x, z)) === ocean) return [x, z];
|
||||
if (tags.has("biomes", "bworld:is_ocean", t.game.world.biome_at(x, 64, z)) === ocean) return [x, z];
|
||||
}
|
||||
}
|
||||
throw new Error("no such column near spawn");
|
||||
|
||||
@@ -163,7 +163,7 @@ Deno.test("a fish with biomes is only caught in them, by id or #tag", async () =
|
||||
const find = (matches: (biome: string) => boolean) => {
|
||||
for (let x = -4096; x <= 4096; x += 64) {
|
||||
for (let z = -4096; z <= 4096; z += 64) {
|
||||
const biome = t.game.world.biome_at(x, z);
|
||||
const biome = t.game.world.biome_at(x, 64, z);
|
||||
const season = biome_season(biome, t.game.world.time);
|
||||
if (matches(biome) && (season === "summer" || season === "winter")) return { x, y: 64, z };
|
||||
}
|
||||
|
||||
+17
-2
@@ -3,7 +3,8 @@
|
||||
|
||||
// the golden fixture's hashes, shared by the tests that check generation against it: worldgen_golden_test.ts,
|
||||
// worldgen_bundle_test.ts and worldgen_pool_test.ts. hashed by name, since numeric ids move whenever a block is added
|
||||
import { AIR, ID_MASK } from "@/common/constants.ts";
|
||||
import { AIR, CHUNK_AREA, ID_MASK } from "@/common/constants.ts";
|
||||
import { BIOME_CELL_HEIGHT, type ChunkColumn } from "@/common/section.ts";
|
||||
import type { RawChunk } from "@/common/generation.ts";
|
||||
import { AIR_ID } from "@/common/protocol.ts";
|
||||
import { sha256 } from "@/common/utils.ts";
|
||||
@@ -41,7 +42,21 @@ export function raw_hash(raw: RawChunk, name: (value: number) => string): Promis
|
||||
for (let i = 0; i < raw.spills.length; i += 4) {
|
||||
spills.push(raw.spills[i], raw.spills[i + 1], raw.spills[i + 2], name(raw.spills[i + 3]));
|
||||
}
|
||||
return hash(blocks_text(raw.column.to_flat(), name), JSON.stringify(spills), raw.column.biomes.join(","));
|
||||
return hash(blocks_text(raw.column.to_flat(), name), JSON.stringify(spills), biomes_text(raw.column));
|
||||
}
|
||||
|
||||
// each column's biome, x then z, then every biome cell's when any isn't its column's, so a world whose biomes don't
|
||||
// change with height hashes as it did before biomes had heights
|
||||
function biomes_text(column: ChunkColumn) {
|
||||
const { min_y, max_y } = column.shape;
|
||||
const at = (i: number, y: number) => column.biome_at(column.x * 16 + (i & 15), y, column.z * 16 + (i >> 4));
|
||||
const columns = Array.from({ length: CHUNK_AREA }, (_, i) => at(i, min_y));
|
||||
const cells: string[] = [];
|
||||
for (let y = min_y; y < max_y; y += BIOME_CELL_HEIGHT) {
|
||||
for (let i = 0; i < CHUNK_AREA; i++) cells.push(at(i, y));
|
||||
}
|
||||
const flat = cells.every((biome, i) => biome === columns[i % CHUNK_AREA]);
|
||||
return flat ? columns.join(",") : `${columns.join(",")}|${cells.join(",")}`;
|
||||
}
|
||||
|
||||
// what the fixture's "final" is: the chunk with its neighbors' spills
|
||||
|
||||
+3
-1
@@ -199,7 +199,8 @@ export function chunk_worker(
|
||||
|
||||
// the sections of the middle one of 3x3 chunks meshed by a chunk worker, asked for the way the client asks: all of
|
||||
// them, or the indexes in options.sections. the chunks are each block's value in one array, y from the bottom of the
|
||||
// world then z then x, null where there's none. what comes back is each section's mesh, and all of them put together:
|
||||
// world then z then x, null where there's none, all of one biome whose grass, foliage and water colors are each chunk's
|
||||
// options.biome_colors. what comes back is each section's mesh, and all of them put together:
|
||||
// their layers' quads one after another, their light, their visuals with indexes in the whole chunk (like an array's)
|
||||
// and the bounds of them all
|
||||
export async function mesh_column(
|
||||
@@ -231,6 +232,7 @@ export async function mesh_column(
|
||||
first,
|
||||
chunks: columns.map((column) => column?.sections.slice(first, end).map((section) => section.data) ?? null),
|
||||
sky: columns.map((column) => column && column_heights(column, opacity).slice(CHUNK_AREA)),
|
||||
biomes: columns.map((column) => column?.biomes.sections.slice(first, end) ?? null),
|
||||
biome_colors: options.biome_colors ?? chunks.map(() => null),
|
||||
camera: options.camera ?? [0, 0, 0],
|
||||
};
|
||||
|
||||
@@ -72,10 +72,10 @@ Deno.test("blocks, entities, players and the world know their biome's season", a
|
||||
const season_of = (biome: string) => get_biome(biome)?.season ?? "spring";
|
||||
|
||||
// spawn is on dry land
|
||||
const biome = ctx.world.biome_at(0, 0);
|
||||
assertEquals(biome, game.world.biome_at(0, 0));
|
||||
const biome = ctx.world.biome_at(0, 100, 0);
|
||||
assertEquals(biome, game.world.biome_at(0, 100, 0));
|
||||
assertEquals(season_of(biome), "winter");
|
||||
assertEquals(ctx.world.season_at(0.7, 0.2), "winter");
|
||||
assertEquals(ctx.world.season_at(0.7, 100.5, 0.2), "winter");
|
||||
|
||||
game.set_block(0, 100, 0, "testmod:thermometer");
|
||||
assertEquals(created, [{ biome, season: season_of(biome) }]);
|
||||
@@ -89,9 +89,9 @@ Deno.test("blocks, entities, players and the world know their biome's season", a
|
||||
assertEquals([alice.biome, alice.season], [biome, season_of(biome)]);
|
||||
|
||||
// somewhere far off, in a chunk nobody loaded, it's what generation makes there
|
||||
const far = ctx.world.biome_at(5000, -3000);
|
||||
assertEquals(far, game.world.biome_at(5000, -3000));
|
||||
assertEquals(ctx.world.season_at(5000, -3000), season_of(far));
|
||||
const far = ctx.world.biome_at(5000, 64, -3000);
|
||||
assertEquals(far, game.world.biome_at(5000, 64, -3000));
|
||||
assertEquals(ctx.world.season_at(5000, 64, -3000), season_of(far));
|
||||
} finally {
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
+56
-3
@@ -7,10 +7,13 @@ import { Alea } from "@paulaboks/rng";
|
||||
import { AIR, CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { Reader, Writer } from "@/common/bytes.ts";
|
||||
import {
|
||||
BIOME_CELL_HEIGHT,
|
||||
ChunkColumn,
|
||||
column_biomes,
|
||||
column_heights,
|
||||
read_column,
|
||||
Section,
|
||||
SECTION_BIOMES,
|
||||
SECTION_VOLUME,
|
||||
to_flat,
|
||||
write_column,
|
||||
@@ -21,10 +24,12 @@ const SHAPE = new WorldShape({ min_y: -64, max_y: 320, sky_light: true });
|
||||
const [STONE, GLASS, LEAVES] = [1, 2, 3];
|
||||
// light goes through air and glass, leaves and stone stop it
|
||||
const OPACITY = Uint8Array.of(0, 15, 0, 1);
|
||||
// every column's biome all the way up
|
||||
const PLAINS: string[] = new Array(CHUNK_AREA).fill("test:plains");
|
||||
|
||||
function empty_column(x = 0, z = 0) {
|
||||
const sections = Array.from({ length: SHAPE.height / 16 }, () => new Section(AIR));
|
||||
return new ChunkColumn(x, z, SHAPE, sections, new Array(CHUNK_AREA).fill("test:plains"));
|
||||
return new ChunkColumn(x, z, SHAPE, sections, column_biomes(PLAINS, sections.length));
|
||||
}
|
||||
|
||||
Deno.test("a section is one value until a block in it differs", () => {
|
||||
@@ -54,7 +59,7 @@ Deno.test("a column's blocks are at their world coordinates, from min_y to max_y
|
||||
const flat = column.to_flat();
|
||||
assertEquals(flat[0 * CHUNK_AREA + 15 * 16 + 0], STONE);
|
||||
assertEquals(flat[383 * CHUNK_AREA + 0 * 16 + 15], GLASS);
|
||||
const again = ChunkColumn.from_flat(-2, 3, SHAPE, flat, column.biomes);
|
||||
const again = ChunkColumn.from_flat(-2, 3, SHAPE, flat, PLAINS);
|
||||
assertEquals(again.to_flat(), flat);
|
||||
assertEquals(again.sections.filter((section) => section.blocks).length, 2);
|
||||
|
||||
@@ -112,7 +117,14 @@ Deno.test("sections are written and read back as they were, however many kinds o
|
||||
);
|
||||
});
|
||||
sections.push(new Section(GLASS));
|
||||
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${i % 3}`);
|
||||
// three biomes in the lowest 5 sections, one in the rest
|
||||
const biomes = column_biomes(
|
||||
Array.from(
|
||||
{ length: sections.length * SECTION_BIOMES },
|
||||
(_, i) => i < 5 * SECTION_BIOMES ? `test:biome_${i % 3}` : "test:one",
|
||||
),
|
||||
sections.length,
|
||||
);
|
||||
|
||||
const writer = new Writer();
|
||||
write_column(writer, sections, biomes);
|
||||
@@ -126,3 +138,44 @@ Deno.test("sections are written and read back as they were, however many kinds o
|
||||
assertEquals(read_column(new Reader(bytes), (value) => value + 1).sections.at(-1)!.value, GLASS + 1);
|
||||
assertThrows(() => read_column(new Reader(bytes.subarray(0, bytes.length - 1)), (value) => value));
|
||||
});
|
||||
|
||||
Deno.test("a column's biomes are one per block across and every 4 blocks up, and set a cell at a time", () => {
|
||||
// a cave biome from y 0 to 3 under one column, the rest plains
|
||||
const cells = Array.from(
|
||||
{ length: SHAPE.height / BIOME_CELL_HEIGHT * CHUNK_AREA },
|
||||
(_, i) => i === (0 - SHAPE.min_y) / BIOME_CELL_HEIGHT * CHUNK_AREA + 5 * 16 + 4 ? "test:cave" : "test:plains",
|
||||
);
|
||||
const column = ChunkColumn.from_flat(0, 0, SHAPE, new Uint32Array(SHAPE.volume), cells);
|
||||
assertEquals([0, 3].map((y) => column.biome_at(4, y, 5)), ["test:cave", "test:cave"]);
|
||||
assertEquals([-1, 4].map((y) => column.biome_at(4, y, 5)), ["test:plains", "test:plains"]);
|
||||
assertEquals(column.biome_at(5, 0, 5), "test:plains");
|
||||
// above and below the world, the top's and the bottom's
|
||||
assertEquals([column.biome_at(4, 1000, 5), column.biome_at(4, -1000, 5)], ["test:plains", "test:plains"]);
|
||||
// sections with the same biomes all the way up share theirs
|
||||
const plains = column_biomes(PLAINS.map((biome, i) => i < 5 ? "test:desert" : biome), 24);
|
||||
assert(plains.sections.every((section) => section === plains.sections[0]));
|
||||
|
||||
column.set_biome(9, 100, 9, "test:desert");
|
||||
for (let y = 100; y < 104; y++) assertEquals(column.biome_at(9, y, 9), "test:desert");
|
||||
for (const y of [99, 104]) assertEquals(column.biome_at(9, y, 9), "test:plains");
|
||||
assertEquals(column.biome_at(9, 100, 10), "test:plains");
|
||||
// nothing outside the world's height
|
||||
column.set_biome(9, 320, 9, "test:desert");
|
||||
assertEquals(column.biome_at(9, 319, 9), "test:plains");
|
||||
|
||||
// a copy's biomes are its own, even where sections shared theirs
|
||||
const sharing = ChunkColumn.from_flat(
|
||||
0,
|
||||
0,
|
||||
SHAPE,
|
||||
new Uint32Array(SHAPE.volume),
|
||||
PLAINS.map((biome, i) => i < 5 ? "test:desert" : biome),
|
||||
);
|
||||
const copy = sharing.clone();
|
||||
copy.set_biome(9, 0, 0, "test:cave");
|
||||
assertEquals([copy.biome_at(9, 0, 0), sharing.biome_at(9, 0, 0), copy.biome_at(9, 16, 0)], [
|
||||
"test:cave",
|
||||
"test:plains",
|
||||
"test:plains",
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
// docs/modding/worldgen/terrain.md. tests/worldgen_golden_test.ts checks the base game's makes the same world
|
||||
import { assert, assertEquals, assertRejects } from "@std/assert";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
// mods/bworld and a mod with files, in a temp folder
|
||||
@@ -55,15 +56,15 @@ Deno.test("a mod's biome generates where its rule, added to the base game's, say
|
||||
for (let cz = -2; cz < 2; cz++) {
|
||||
const { column } = generate_raw_chunk(cx, cz, "lavender", block_ids, worldgen, default_values);
|
||||
const { min_y, max_y } = game.world.shape;
|
||||
column.biomes.forEach((biome, i) => {
|
||||
if (biome !== "lavender:fields") return;
|
||||
fields++;
|
||||
// its surface: the top block of the column is its sand
|
||||
for (let i = 0; i < CHUNK_AREA; i++) {
|
||||
const [x, z] = [cx * 16 + i % 16, cz * 16 + Math.floor(i / 16)];
|
||||
// its surface: the top block of the column is its sand
|
||||
let y = max_y - 1;
|
||||
while (y > min_y && column.get(x, y, z) === 0) y--;
|
||||
if (column.biome_at(x, y, z) !== "lavender:fields") continue;
|
||||
fields++;
|
||||
assertEquals(column.get(x, y, z), block_ids["bworld:sand"]);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
assert(fields > 100, `only ${fields} columns of lavender fields`);
|
||||
|
||||
+3
-9
@@ -27,13 +27,8 @@ async function mesh_block(id: string, x: number, middle: Colors, west: Colors |
|
||||
const empty = () => new Uint32Array(DEFAULT_SHAPE.volume);
|
||||
const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()];
|
||||
const colors = chunks.map(() => null as Uint32Array | null);
|
||||
const biome_colors = ({ grass, foliage, water = 0 }: Colors) => {
|
||||
const array = new Uint32Array(CHUNK_AREA * 3);
|
||||
array.fill(grass, 0, CHUNK_AREA);
|
||||
array.fill(foliage, CHUNK_AREA, CHUNK_AREA * 2);
|
||||
array.fill(water, CHUNK_AREA * 2);
|
||||
return array;
|
||||
};
|
||||
// the colors of each chunk's one biome
|
||||
const biome_colors = ({ grass, foliage, water = 0 }: Colors) => Uint32Array.of(grass, foliage, water);
|
||||
colors[4] = biome_colors(middle);
|
||||
if (west) colors[3] = biome_colors(west);
|
||||
|
||||
@@ -105,8 +100,7 @@ Deno.test("every column the overworld generates has a biome with grass, foliage
|
||||
blocks.forEach((b, nid) => block_ids[b.id] = nid);
|
||||
for (const [x, z] of [[0, 0], [30, -12], [-80, 45]]) {
|
||||
const { biomes } = generate_raw_chunk(x, z, game.world.seed, block_ids, game.world.worldgen).column;
|
||||
assertEquals(biomes.length, CHUNK_AREA);
|
||||
for (const biome of biomes) {
|
||||
for (const biome of biomes.palette) {
|
||||
const { colors } = get_biome(biome)!;
|
||||
assert(colors.grass !== undefined, `${biome} has no grass color`);
|
||||
assert(colors.foliage !== undefined, `${biome} has no foliage color`);
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// biomes have heights: one per block across and every 4 blocks up, chosen by biome rules that can look at "y" and
|
||||
// "depth", see docs/modding/worldgen/biomes.md. a mod adds an underground biome to the base game's with one rule
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import type { ServerContext } from "@/common/mod_api/server.ts";
|
||||
import { validate_noise_settings } from "@/common/worldgen/data.ts";
|
||||
import { client_level } from "./client_level.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
// mods/bworld and a mod whose biome is everywhere more than 16 blocks under the ground. its scripts keep what a feature
|
||||
// saw of chunk 0, 0's biomes at 8, 8, and the server's context. the feature also sets the cell 9, 201, 9 to it
|
||||
function deep_mod() {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_deep_" });
|
||||
copy_dir("mods/bworld", `${dir}/bworld`);
|
||||
const write = (path: string, content: unknown) => {
|
||||
Deno.mkdirSync(`${dir}/deep/${path.slice(0, path.lastIndexOf("/"))}`, { recursive: true });
|
||||
Deno.writeTextFileSync(`${dir}/deep/${path}`, typeof content === "string" ? content : JSON.stringify(content));
|
||||
};
|
||||
write("manifest.json", {
|
||||
format_version: 1,
|
||||
id: "deep",
|
||||
name: "Deep",
|
||||
version: "1.0.0",
|
||||
dependencies: [{ id: "bworld", version: "*" }],
|
||||
scripts: { worldgen: "scripts/worldgen.ts", server: "scripts/server.ts" },
|
||||
});
|
||||
write("worldgen/biomes/caves.json", {
|
||||
format_version: 1,
|
||||
biome: {
|
||||
id: "deep:caves",
|
||||
surface: { top: "bworld:stone", filler: "bworld:stone", filler_depth: 1 },
|
||||
season: "winter",
|
||||
},
|
||||
});
|
||||
write("worldgen/biome_source/caves.json", {
|
||||
format_version: 1,
|
||||
biome_source: {
|
||||
id: "deep:caves",
|
||||
extends: "bworld:overworld",
|
||||
// before every rule of the base game's, which all hold all the way down
|
||||
rules: [{
|
||||
id: "deep:caves",
|
||||
before: "bworld:river_bed_frozen",
|
||||
when: { depth: { min_exclusive: 16 } },
|
||||
biome: "deep:caves",
|
||||
}],
|
||||
},
|
||||
});
|
||||
write(
|
||||
"scripts/worldgen.ts",
|
||||
`import type { WorldgenContext } from "cubi/worldgen";
|
||||
export function setup(gen: WorldgenContext) {
|
||||
gen.register_feature("deep:look", (chunk) => {
|
||||
if (chunk.x !== 0 || chunk.z !== 0) return;
|
||||
const ground = chunk.height_at(8, 8);
|
||||
(globalThis as any).deep_seen = [chunk.biome_at(8, ground, 8), chunk.biome_at(8, ground - 40, 8)];
|
||||
chunk.set_biome(9, 201, 9, "deep:caves");
|
||||
});
|
||||
}`,
|
||||
);
|
||||
write(
|
||||
"scripts/server.ts",
|
||||
`import type { ServerContext } from "cubi/server";
|
||||
export function setup(ctx: ServerContext) {
|
||||
(globalThis as any).deep_ctx = ctx;
|
||||
}`,
|
||||
);
|
||||
return dir;
|
||||
}
|
||||
|
||||
const seen = () => (globalThis as unknown as { deep_seen: string[] }).deep_seen;
|
||||
const server_ctx = () => (globalThis as unknown as { deep_ctx: ServerContext }).deep_ctx;
|
||||
|
||||
Deno.test("a rule with depth puts a biome under the ground, and the ground keeps its own and its blocks", async () => {
|
||||
const dir = deep_mod();
|
||||
try {
|
||||
const { game } = await test_game(dir);
|
||||
const world = game.world;
|
||||
const ground = world.surface_height(8, 8);
|
||||
const surface = world.biome_at(8, ground, 8);
|
||||
assert(surface !== "deep:caves", "the ground's biome is the base game's");
|
||||
assertEquals(world.biome_at(8, ground - 40, 8), "deep:caves");
|
||||
assertEquals(world.biome_at(8, world.shape.min_y - 100, 8), "deep:caves", "below the world, its bottom's");
|
||||
// features see the same, and set the 4 blocks tall cell a y is in
|
||||
assertEquals(seen(), [surface, "deep:caves"]);
|
||||
assertEquals([199, 200, 203, 204].map((y) => world.biome_at(9, y, 9) === "deep:caves"), [
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
]);
|
||||
|
||||
// the blocks are what the base game makes without it
|
||||
const blocks = [[0, 0], [2, -3]].map(([x, z]) => world.generated_blocks(x, z).to_flat());
|
||||
const plain = (await test_game("mods")).game.world;
|
||||
assertEquals(blocks, [[0, 0], [2, -3]].map(([x, z]) => plain.generated_blocks(x, z).to_flat()));
|
||||
} finally {
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("biomes under the ground are saved, sent to clients, and where entities stand", async () => {
|
||||
const dir = deep_mod();
|
||||
try {
|
||||
const { game, join, send, take } = await test_game(dir);
|
||||
const world = game.world;
|
||||
const ground = world.surface_height(8, 8);
|
||||
const deep = ground - 40;
|
||||
|
||||
// a client reads them from the chunk it's sent
|
||||
const { level } = client_level(world.shape);
|
||||
level.load_chunk(world.level_chunk(0, 0));
|
||||
assertEquals(level.biome_at(8, deep, 8), "deep:caves");
|
||||
assertEquals(level.biome_at(8, ground, 8), world.biome_at(8, ground, 8));
|
||||
level.dispose();
|
||||
|
||||
// scripts read the biome and season at a block, and an entity's at its feet
|
||||
const ctx = server_ctx();
|
||||
assertEquals([ctx.world.biome_at(8.5, deep, 8.5), ctx.world.season_at(8.5, deep, 8.5)], [
|
||||
"deep:caves",
|
||||
"winter",
|
||||
]);
|
||||
const chicken = ctx.world.spawn_entity("bworld:chicken", 8.5, deep, 8.5);
|
||||
assertEquals([chicken.biome, chicken.season], ["deep:caves", "winter"]);
|
||||
|
||||
// /locatebiome finds it under the ground, and says how deep
|
||||
join(1, "alice");
|
||||
send(1, { type: "move", x: 8.5, y: ground + 1, z: 8.5, yaw: 0, pitch: 0 });
|
||||
take(1);
|
||||
send(1, { type: "chat", text: "/locatebiome deep:caves" });
|
||||
const answer = take(1).filter((m) => m.type === "chat").map((m) => m.text).join("\n");
|
||||
const [x, y, z] = answer.match(/at (-?\d+) (-?\d+) (-?\d+)/)!.slice(1).map(Number);
|
||||
assertEquals(world.locate_biome("deep:caves", x, z), y);
|
||||
assert(y < world.locate_biome(world.biome_at(x, 1000, z), x, z)! - 16, `${answer}, not under the ground`);
|
||||
|
||||
// and they come back with the chunk
|
||||
world.set_block(8, ground + 1, 8, "bworld:stone");
|
||||
const loaded = (await test_game(dir, game.save())).game.world;
|
||||
assertEquals(loaded.biome_at(8, deep, 8), "deep:caves");
|
||||
assertEquals(loaded.biome_at(8, ground, 8), world.biome_at(8, ground, 8));
|
||||
} finally {
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("a generator's climate values can't be named like what biome rules look at", async () => {
|
||||
const json =
|
||||
JSON.parse(await Deno.readTextFile("mods/bworld/worldgen/noise_settings/overworld.json")).noise_settings;
|
||||
assertEquals(validate_noise_settings(json), []);
|
||||
for (const name of ["y", "depth"]) {
|
||||
const problems = validate_noise_settings({ ...json, climate: { ...json.climate, [name]: 0 } });
|
||||
assert(problems.some((problem) => problem.includes("climate")), `${name}: ${problems}`);
|
||||
}
|
||||
});
|
||||
+4
-3
@@ -60,15 +60,16 @@ Deno.test("trees grow the wood of their kind and biome", async () => {
|
||||
for (let cx = -8; cx < 8; cx++) {
|
||||
for (let cz = -8; cz < 8; cz++) {
|
||||
const { column } = generate_raw_chunk(cx, cz, "wood-test", ids, game.world.worldgen);
|
||||
const [blocks, biomes] = [column.to_flat(), column.biomes];
|
||||
const blocks = column.to_flat();
|
||||
blocks.forEach((block, i) => {
|
||||
const leaves = leaves_of.get(block);
|
||||
if (leaves) found_leaves.add(leaves);
|
||||
const wood = wood_of.get(block);
|
||||
// the bottom of a trunk
|
||||
if (!wood || !ground.has(blocks[i - CHUNK_AREA])) return;
|
||||
const column = i % CHUNK_AREA;
|
||||
const biome = biomes[column];
|
||||
// the trunk's ground's biome
|
||||
const y = game.world.shape.min_y + Math.floor(i / CHUNK_AREA) - 1;
|
||||
const biome = column.biome_at(cx * 16 + i % 16, y, cz * 16 + Math.floor(i / 16) % 16);
|
||||
const info = get_biome(biome)!;
|
||||
const allowed = info.trees!.kinds.map((kind) =>
|
||||
info.trees!.wood[kind] ?? (kind === "big_oak" ? "oak" : kind)
|
||||
|
||||
+35
-11
@@ -8,7 +8,8 @@ import { ChunkBlocks, decode_chunk, encode_chunk, SavedChunk } from "@/server/ga
|
||||
import { list_chunks, read_chunk, region_file, write_chunks } from "@/server/regions.ts";
|
||||
import { sent_block, test_game } from "./helpers.ts";
|
||||
import { DEFAULT_SHAPE, WorldShape } from "@/common/world_shape.ts";
|
||||
import { ChunkColumn, section_index, to_flat } from "@/common/section.ts";
|
||||
import { biome_in, ChunkColumn, column_biomes, section_index, to_flat } from "@/common/section.ts";
|
||||
import { Writer } from "@/common/bytes.ts";
|
||||
|
||||
// a chunk at the height of the base game's dimension, which the test worlds play
|
||||
function chunk_data(seed: number, palette_size = 3, shape = DEFAULT_SHAPE): Required<ChunkBlocks> {
|
||||
@@ -16,12 +17,8 @@ function chunk_data(seed: number, palette_size = 3, shape = DEFAULT_SHAPE): Requ
|
||||
for (let i = 0; i < palette_size; i++) palette.push([`test:block_${i}`, i % 5]);
|
||||
const blocks = new Uint32Array(shape.volume).map((_, i) => (i * seed) % palette_size);
|
||||
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${(i * seed) % 4}`);
|
||||
return {
|
||||
palette,
|
||||
min_y: shape.min_y,
|
||||
sections: ChunkColumn.from_flat(0, 0, shape, blocks, biomes).sections,
|
||||
biomes,
|
||||
};
|
||||
const { sections, biomes: column_biomes } = ChunkColumn.from_flat(0, 0, shape, blocks, biomes);
|
||||
return { palette, min_y: shape.min_y, sections, biomes: column_biomes };
|
||||
}
|
||||
|
||||
// the palette entry of a saved chunk's block
|
||||
@@ -44,9 +41,14 @@ function old_format(format: 1 | 2, chunk: ChunkBlocks) {
|
||||
if (format === 1) {
|
||||
bytes.push(...new Uint8Array(flat.length / 8));
|
||||
} else {
|
||||
const biome_palette = [...new Set(chunk.biomes)];
|
||||
// each column's biome at the bottom
|
||||
const columns = Array.from(
|
||||
{ length: CHUNK_AREA },
|
||||
(_, i) => biome_in(chunk.biomes!, DEFAULT_SHAPE, i, 0, i >> 4),
|
||||
);
|
||||
const biome_palette = [...new Set(columns)];
|
||||
bytes.push(biome_palette.length, ...biome_palette.flatMap(string));
|
||||
bytes.push(...chunk.biomes!.map((biome) => biome_palette.indexOf(biome)));
|
||||
bytes.push(...columns.map((biome) => biome_palette.indexOf(biome)));
|
||||
}
|
||||
return Uint8Array.from(bytes);
|
||||
}
|
||||
@@ -80,6 +82,28 @@ Deno.test("a chunk's blocks and biomes come back as they were saved, with more t
|
||||
assertEquals(to_flat(old.sections), to_flat(chunk.sections));
|
||||
assertEquals(old.biomes, format === 1 ? undefined : chunk.biomes);
|
||||
}
|
||||
|
||||
// and format 3, from before biomes had heights: each column's biome, which goes all the way up
|
||||
const columns = Array.from({ length: CHUNK_AREA }, (_, i) => i < 10 ? "test:a" : "test:b");
|
||||
const writer = new Writer();
|
||||
writer.u8(3);
|
||||
writer.i16(0);
|
||||
writer.u32(1);
|
||||
writer.string("test:block_0");
|
||||
writer.u16(0);
|
||||
writer.u16(2);
|
||||
writer.string("test:a");
|
||||
writer.string("test:b");
|
||||
for (const biome of columns) writer.u8(biome === "test:a" ? 0 : 1);
|
||||
writer.u16(16);
|
||||
// sections all of the one block
|
||||
for (let i = 0; i < 16; i++) {
|
||||
writer.u8(0);
|
||||
writer.u32(0);
|
||||
}
|
||||
const three = decode_chunk(writer.bytes());
|
||||
assertEquals(three.sections.map((section) => section.value), new Array(16).fill(0));
|
||||
assertEquals(three.biomes, column_biomes(columns, 16));
|
||||
});
|
||||
|
||||
Deno.test("region files keep chunks, including negative ones, and saving one again doesn't touch the others", async () => {
|
||||
@@ -136,7 +160,7 @@ Deno.test("every chunk generated is saved whole, and saved again only when it ch
|
||||
const chunk = decode_chunk(saved.chunks.find((c) => c.x === 0 && c.z === 0)!.data);
|
||||
assertEquals(saved_block(chunk, 3, 120, 5), ["bworld:stone", 0]);
|
||||
assert(saved_block(chunk, 3, 10, 5)[0] !== AIR_ID);
|
||||
assertEquals(chunk.biomes![5 * 16 + 3], game.world.biome_at(3, 5));
|
||||
assertEquals(biome_in(chunk.biomes!, DEFAULT_SHAPE, 3, 120, 5), game.world.biome_at(3, 120, 5));
|
||||
const untouched = decode_chunk(saved.chunks.find((c) => c.x === 5 && c.z === 5)!.data);
|
||||
assertEquals(
|
||||
Uint32Array.from(to_flat(untouched.sections), (block) => {
|
||||
@@ -192,7 +216,7 @@ Deno.test("chunks saved before they had biomes get them from generation, and are
|
||||
at_0_0.data = old_format(1, decode_chunk(at_0_0.data));
|
||||
const { game } = await test_game("mods", saved);
|
||||
assertEquals(game.world.get_block_id(3, 121, 5), "bworld:stone");
|
||||
assertEquals(game.world.biome_at(3, 5), biomes[5 * 16 + 3]);
|
||||
assertEquals(game.world.biome_at(3, 120, 5), biome_in(biomes, DEFAULT_SHAPE, 3, 120, 5));
|
||||
const again = game.save().chunks.find((c) => c.x === 0 && c.z === 0)!;
|
||||
assertEquals(decode_chunk(again.data).biomes, biomes);
|
||||
});
|
||||
|
||||
@@ -31,7 +31,7 @@ Deno.test("cacti grow 1 to 3 tall on desert sand, 2 or 3 tries a chunk", async (
|
||||
// the bottom of each cactus
|
||||
if (at(x, y, z) !== cactus || at(x, y - 1, z) === cactus) continue;
|
||||
assertEquals(at(x, y - 1, z), sand, "on sand");
|
||||
assertEquals(chunk.column.biomes[z * CHUNK_SIZE + x], "bworld:desert");
|
||||
assertEquals(chunk.column.biome_at(x0 + x, y - 1, z0 + z), "bworld:desert");
|
||||
let height = 0;
|
||||
while (at(x, y + height, z) === cactus) height++;
|
||||
heights.push(height);
|
||||
|
||||
Reference in New Issue
Block a user