World sections
This commit is contained in:
@@ -32,7 +32,7 @@ Deno.test({
|
||||
if (!is_within_view(cx, cz, player.chunks.view_distance, x, z)) continue;
|
||||
const chunk = chunks.get(`${x},${z}`);
|
||||
assert(chunk, `chunk ${x},${z} wasn't sent`);
|
||||
assertEquals(chunk.blocks, t.game.world.generated_blocks(x, z), `chunk ${x},${z}`);
|
||||
assertEquals(chunk.blocks, t.game.world.generated_blocks(x, z).to_flat(), `chunk ${x},${z}`);
|
||||
}
|
||||
}
|
||||
assert(t.pool!.generated - generated > chunks.size, "the pool made them, and their neighbors");
|
||||
|
||||
@@ -7,6 +7,8 @@ import { assert, assertEquals } from "@std/assert";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } 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 } from "@/common/section.ts";
|
||||
import { WorldShape } from "@/common/world_shape.ts";
|
||||
import { sent_block, test_game } from "./helpers.ts";
|
||||
|
||||
Deno.test("a chunk comes back as it was encoded, whatever its sections hold", () => {
|
||||
@@ -21,8 +23,9 @@ Deno.test("a chunk comes back as it was encoded, whatever its sections hold", ()
|
||||
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 });
|
||||
|
||||
const decoded = decode_level_chunk(encode_level_chunk({ x: -3, z: 70000, blocks, biomes }));
|
||||
const decoded = decode_level_chunk(encode_level_chunk(ChunkColumn.from_flat(-3, 70000, shape, blocks, biomes)));
|
||||
assertEquals(decoded.x, -3);
|
||||
assertEquals(decoded.z, 70000);
|
||||
assertEquals(decoded.blocks, blocks);
|
||||
@@ -30,7 +33,7 @@ Deno.test("a chunk comes back as it was encoded, whatever its sections hold", ()
|
||||
|
||||
// one biome, and the receiver's numbering
|
||||
const one = decode_level_chunk(
|
||||
encode_level_chunk({ x: 0, z: 0, blocks, biomes: biomes.map(() => "bworld:plains") }),
|
||||
encode_level_chunk(ChunkColumn.from_flat(0, 0, shape, blocks, biomes.map(() => "bworld:plains"))),
|
||||
(value) => value === 7 ? 70 : value,
|
||||
);
|
||||
assertEquals(one.biomes, biomes.map(() => "bworld:plains"));
|
||||
|
||||
@@ -143,6 +143,12 @@ Deno.test("a dimension's blocks go from its min_y to max_y - 1, and are sent and
|
||||
assertEquals(chunk.blocks[8], world.get_block_value(GROUND_X, -64, 0));
|
||||
assertEquals(chunk.blocks[383 * CHUNK_AREA + 8], world.get_block_value(GROUND_X, 319, 0));
|
||||
|
||||
// a column of ground: glass up to 69 and air above, all one block in every section but the one with the ground's
|
||||
// top in it
|
||||
const generated = world.generated_blocks(2, 0);
|
||||
assertEquals(generated.sections.map((section) => section.blocks !== null).indexOf(true), (64 + 64) / 16);
|
||||
assertEquals(generated.sections.filter((section) => section.blocks !== null).length, 1);
|
||||
|
||||
const loaded = await test_game(dir, game.save());
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
for (const y of [-64, 319]) assertEquals(loaded.game.world.get_block_id(GROUND_X, y, 0), "bworld:stone");
|
||||
|
||||
@@ -104,7 +104,8 @@ Deno.test("data features use types and modifiers scripts register, scripts place
|
||||
const glass = block_ids["bworld:glass"];
|
||||
const cobble = block_ids["bworld:cobblestone"];
|
||||
for (const [cx, cz] of [[0, 0], [3, -2]]) {
|
||||
const { blocks } = generate_raw_chunk(cx, cz, game.world.seed, block_ids, worldgen, default_values);
|
||||
const raw = generate_raw_chunk(cx, cz, game.world.seed, block_ids, worldgen, default_values);
|
||||
const blocks = raw.column.to_flat();
|
||||
// the pillar in the middle, which the code feature after it in its step saw
|
||||
let pillar = 0;
|
||||
for (let y = min_y; y < max_y; y++) if (at(blocks, 8, y, 8) === glass) pillar++;
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ 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.blocks, name), JSON.stringify(spills), raw.biomes.join(","));
|
||||
return hash(blocks_text(raw.column.to_flat(), name), JSON.stringify(spills), raw.column.biomes.join(","));
|
||||
}
|
||||
|
||||
// what the fixture's "final" is: the chunk with its neighbors' spills
|
||||
|
||||
@@ -139,7 +139,8 @@ Deno.test("the template's worldgen feature places boulders, the same way every t
|
||||
for (let cx = 0; cx < 20; cx++) {
|
||||
const plain = generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, without);
|
||||
const modded = generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, worldgen);
|
||||
const differences = plain.blocks.filter((b, i) => b !== modded.blocks[i]).length;
|
||||
const [plain_blocks, modded_blocks] = [plain.column.to_flat(), modded.column.to_flat()];
|
||||
const differences = plain_blocks.filter((b, i) => b !== modded_blocks[i]).length;
|
||||
if (differences > 0) boulders++;
|
||||
assert(differences <= 1, `chunk ${cx} changed ${differences} blocks`);
|
||||
}
|
||||
@@ -158,8 +159,8 @@ Deno.test("the template's worldgen feature places boulders, the same way every t
|
||||
});
|
||||
for (let cx = 0; cx < 20; cx++) {
|
||||
assertEquals(
|
||||
generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, again).blocks,
|
||||
generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, worldgen).blocks,
|
||||
generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, again).column.to_flat(),
|
||||
generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, worldgen).column.to_flat(),
|
||||
);
|
||||
}
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
@@ -186,11 +187,12 @@ Deno.test("mod ores generate into the block they replace", async () => {
|
||||
const ore = game.world.block_ids["copper_tools:example_block"];
|
||||
let found = 0;
|
||||
for (let cx = 0; cx < 4; cx++) {
|
||||
const blocks = generate_raw_chunk(cx, 0, "test-seed", game.world.block_ids, game.world.worldgen).blocks;
|
||||
const raw = generate_raw_chunk(cx, 0, "test-seed", game.world.block_ids, game.world.worldgen);
|
||||
const blocks = raw.column.to_flat();
|
||||
blocks.forEach((b, i) => {
|
||||
if (b === ore) {
|
||||
found++;
|
||||
assert(Math.floor(i / 256) <= 60, "ore above max_y");
|
||||
assert(game.world.shape.min_y + Math.floor(i / 256) <= 60, "ore above max_y");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// chunks' blocks in sections of 16x16x16, see common/section.ts
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { Alea } from "@paulaboks/rng";
|
||||
import { AIR, CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { ChunkColumn, column_heights, Section, SECTION_VOLUME } from "@/common/section.ts";
|
||||
import { WorldShape } from "@/common/world_shape.ts";
|
||||
|
||||
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);
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
Deno.test("a section is one value until a block in it differs", () => {
|
||||
const section = new Section(STONE);
|
||||
section.set(5, STONE);
|
||||
assertEquals(section.blocks, null);
|
||||
section.set(5, GLASS);
|
||||
assertEquals(section.get(5), GLASS);
|
||||
assertEquals(section.get(6), STONE);
|
||||
assertEquals(section.blocks?.length, SECTION_VOLUME);
|
||||
|
||||
assertEquals(Section.of(new Uint32Array(SECTION_VOLUME).fill(GLASS)).data, GLASS);
|
||||
});
|
||||
|
||||
Deno.test("a column's blocks are at their world coordinates, from min_y to max_y - 1", () => {
|
||||
const column = empty_column(-2, 3);
|
||||
column.set(-32, -64, 63, STONE);
|
||||
column.set(-17, 319, 48, GLASS);
|
||||
column.set(-20, 320, 50, STONE);
|
||||
assertEquals(column.get(-32, -64, 63), STONE);
|
||||
assertEquals(column.get(-17, 319, 48), GLASS);
|
||||
assertEquals(column.get(-20, 320, 50), AIR, "above the world");
|
||||
assertEquals(column.get(-20, -65, 50), AIR, "below the world");
|
||||
assertEquals(column.sections.filter((section) => section.blocks).length, 2);
|
||||
|
||||
// flat, y from min_y, then z, then x
|
||||
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);
|
||||
assertEquals(again.to_flat(), flat);
|
||||
assertEquals(again.sections.filter((section) => section.blocks).length, 2);
|
||||
|
||||
const copy = column.clone();
|
||||
copy.set(-32, -64, 63, AIR);
|
||||
assertEquals(column.get(-32, -64, 63), STONE, "a clone's blocks are its own");
|
||||
});
|
||||
|
||||
Deno.test("column heights are the highest block and the lowest y that sees the sky, block by block", () => {
|
||||
const random = new Alea("section-test");
|
||||
const column = empty_column();
|
||||
// stone to y 0, then scattered blocks up to 200, and a glass section in the air
|
||||
for (let y = -64; y <= 0; y++) {
|
||||
for (let i = 0; i < CHUNK_AREA; i++) column.set(i % 16, y, i >> 4, STONE);
|
||||
}
|
||||
for (let n = 0; n < 2000; n++) {
|
||||
const block = [STONE, GLASS, LEAVES][Math.floor(random.next() * 3)];
|
||||
column.set(
|
||||
Math.floor(random.next() * 16),
|
||||
Math.floor(random.next() * 200),
|
||||
Math.floor(random.next() * 16),
|
||||
block,
|
||||
);
|
||||
}
|
||||
// y 240 to 255, all glass, which light passes over whole
|
||||
column.sections[(240 - SHAPE.min_y) / 16] = new Section(GLASS);
|
||||
|
||||
const heights = column_heights(column, OPACITY);
|
||||
for (let i = 0; i < CHUNK_AREA; i++) {
|
||||
const [x, z] = [i % 16, i >> 4];
|
||||
let y = SHAPE.max_y - 1;
|
||||
while (y >= SHAPE.min_y && column.get(x, y, z) === AIR) y--;
|
||||
assertEquals(heights[i], y, `surface of ${x}, ${z}`);
|
||||
while (y >= SHAPE.min_y && OPACITY[column.get(x, y, z)] === 0) y--;
|
||||
assertEquals(heights[CHUNK_AREA + i], y + 1, `sky of ${x}, ${z}`);
|
||||
}
|
||||
|
||||
const nothing = column_heights(empty_column(), OPACITY);
|
||||
assertEquals([nothing[0], nothing[CHUNK_AREA]], [SHAPE.min_y - 1, SHAPE.min_y]);
|
||||
assert(heights.some((height, i) => i < CHUNK_AREA && height === 255), "the glass is the top");
|
||||
});
|
||||
@@ -4,7 +4,6 @@
|
||||
// the terrain is data: noises, density functions, noise settings and biome sources in mods' worldgen folders, see
|
||||
// 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 { CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
@@ -54,14 +53,16 @@ Deno.test("a mod's biome generates where its rule, added to the base game's, say
|
||||
let fields = 0;
|
||||
for (let cx = -2; cx < 2; cx++) {
|
||||
for (let cz = -2; cz < 2; cz++) {
|
||||
const { blocks, biomes } = generate_raw_chunk(cx, cz, "lavender", block_ids, worldgen, default_values);
|
||||
biomes.forEach((biome, column) => {
|
||||
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
|
||||
let y = game.world.shape.height - 1;
|
||||
while (y > 0 && blocks[y * CHUNK_AREA + column] === 0) y--;
|
||||
assertEquals(blocks[y * CHUNK_AREA + column], block_ids["bworld:sand"]);
|
||||
const [x, z] = [cx * 16 + i % 16, cz * 16 + Math.floor(i / 16)];
|
||||
let y = max_y - 1;
|
||||
while (y > min_y && column.get(x, y, z) === 0) y--;
|
||||
assertEquals(column.get(x, y, z), block_ids["bworld:sand"]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@ Deno.test("every column the overworld generates has a biome with grass, foliage
|
||||
const block_ids: Record<string, number> = {};
|
||||
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);
|
||||
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) {
|
||||
const { colors } = get_biome(biome)!;
|
||||
|
||||
+2
-1
@@ -59,7 +59,8 @@ Deno.test("trees grow the wood of their kind and biome", async () => {
|
||||
const found_leaves = new Set<string>();
|
||||
for (let cx = -8; cx < 8; cx++) {
|
||||
for (let cz = -8; cz < 8; cz++) {
|
||||
const { blocks, biomes } = generate_raw_chunk(cx, cz, "wood-test", ids, game.world.worldgen);
|
||||
const { column } = generate_raw_chunk(cx, cz, "wood-test", ids, game.world.worldgen);
|
||||
const [blocks, biomes] = [column.to_flat(), column.biomes];
|
||||
blocks.forEach((block, i) => {
|
||||
const leaves = leaves_of.get(block);
|
||||
if (leaves) found_leaves.add(leaves);
|
||||
|
||||
@@ -113,7 +113,7 @@ Deno.test("every chunk generated is saved whole, and saved again only when it ch
|
||||
const [id, state] = untouched.palette[block];
|
||||
return id === AIR_ID ? 0 : game.world.block_ids[id] | (state << 16);
|
||||
}),
|
||||
game.world.generated_blocks(5, 5),
|
||||
game.world.generated_blocks(5, 5).to_flat(),
|
||||
);
|
||||
|
||||
assertEquals(game.save().chunks, []);
|
||||
@@ -152,7 +152,7 @@ Deno.test("saved chunks stay as they were saved, whatever generation makes there
|
||||
const { saved } = await saved_with_old_terrain();
|
||||
const { game } = await test_game("mods", saved);
|
||||
assertEquals(game.world.get_block_id(3, 121, 5), "bworld:stone");
|
||||
assertEquals(game.world.generated_blocks(0, 0)[index(3, 121, 5)], 0);
|
||||
assertEquals(game.world.generated_blocks(0, 0).get(3, 121, 5), 0);
|
||||
assertEquals(game.save().chunks.filter((c) => c.x === 0 && c.z === 0), []);
|
||||
});
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
// the base game's worldgen features, see mods/bworld/scripts/worldgen.ts
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import { CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
@@ -21,8 +21,9 @@ Deno.test("cacti grow 1 to 3 tall on desert sand, 2 or 3 tries a chunk", async (
|
||||
for (let dz = -3; dz <= 3; dz++) {
|
||||
const chunk = generate_raw_chunk(DESERT_CHUNK.x + dx, DESERT_CHUNK.z + dz, SEED, ids, game.world.worldgen);
|
||||
const { min_y, max_y } = game.world.shape;
|
||||
const at = (x: number, y: number, z: number) =>
|
||||
chunk.blocks[(y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x] & ID_MASK;
|
||||
const x0 = chunk.column.x * CHUNK_SIZE;
|
||||
const z0 = chunk.column.z * CHUNK_SIZE;
|
||||
const at = (x: number, y: number, z: number) => chunk.column.get(x0 + x, y, z0 + z) & ID_MASK;
|
||||
let in_chunk = 0;
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
@@ -30,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.biomes[z * CHUNK_SIZE + x], "bworld:desert");
|
||||
assertEquals(chunk.column.biomes[z * CHUNK_SIZE + x], "bworld:desert");
|
||||
let height = 0;
|
||||
while (at(x, y + height, z) === cactus) height++;
|
||||
heights.push(height);
|
||||
|
||||
@@ -22,7 +22,7 @@ Deno.test("world generation makes the same chunks as recorded", async () => {
|
||||
const raw = generate_raw_chunk(x, z, seed, world.block_ids, world.worldgen, world.default_values);
|
||||
hashes[seed][`${x},${z}`] = {
|
||||
raw: await raw_hash(raw, name),
|
||||
final: await final_hash(world.generated_blocks(x, z), name),
|
||||
final: await final_hash(world.generated_blocks(x, z).to_flat(), name),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,10 @@ Deno.test("the overworld generates the same way every time, with water only belo
|
||||
|
||||
for (let cx = -4; cx < 4; cx++) {
|
||||
for (let cz = -4; cz < 4; cz++) {
|
||||
const { blocks } = generate_raw_chunk(cx, cz, "worldgen-test", ids, game.world.worldgen);
|
||||
const blocks = generate_raw_chunk(cx, cz, "worldgen-test", ids, game.world.worldgen).column.to_flat();
|
||||
assertEquals(
|
||||
blocks,
|
||||
generate_raw_chunk(cx, cz, "worldgen-test", ids, game.world.worldgen).blocks,
|
||||
generate_raw_chunk(cx, cz, "worldgen-test", ids, game.world.worldgen).column.to_flat(),
|
||||
"same chunk, same blocks",
|
||||
);
|
||||
|
||||
@@ -63,7 +63,8 @@ Deno.test("trees grow on the ground, never closer than 4 blocks, even across chu
|
||||
let spilled_leaves = 0;
|
||||
for (let cx = -6; cx < 6; cx++) {
|
||||
for (let cz = -6; cz < 6; cz++) {
|
||||
const { blocks, spills } = generate_raw_chunk(cx, cz, game.world.seed, ids, game.world.worldgen);
|
||||
const { column, spills } = generate_raw_chunk(cx, cz, game.world.seed, ids, game.world.worldgen);
|
||||
const blocks = column.to_flat();
|
||||
blocks.forEach((block, i) => {
|
||||
if (logs.has(block) && ground.has(blocks[i - CHUNK_AREA])) {
|
||||
const x = cx * 16 + (i % 16);
|
||||
|
||||
Reference in New Issue
Block a user