World shape for dimension
This commit is contained in:
@@ -4,14 +4,15 @@
|
||||
// the server sends the world in whole chunks, like minecraft java: nearest first, in batches the client paces, and
|
||||
// only to players who can see them
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
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 { sent_block, test_game } from "./helpers.ts";
|
||||
|
||||
Deno.test("a chunk comes back as it was encoded, whatever its sections hold", () => {
|
||||
const blocks = new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT);
|
||||
// as tall as a dimension can be made, sections are counted in the message
|
||||
const section = CHUNK_AREA * 16;
|
||||
const blocks = new Uint32Array(section * 24);
|
||||
// section 0 is all one block, 1 has 3 kinds, 2 has 16, 3 has 300 (more than a palette holds), the rest air
|
||||
blocks.fill(7, 0, section);
|
||||
for (let i = 0; i < section; i++) {
|
||||
@@ -131,7 +132,8 @@ Deno.test("block changes only go to players who have the chunk, the others get t
|
||||
// the chunk the server sends is what it has, with the ids join listed
|
||||
assertEquals(alice.blocks[game.world.block_ids["bworld:stone"]], "bworld:stone");
|
||||
const chunk = decode_level_chunk(game.world.level_chunk(0, 0));
|
||||
assertEquals(chunk.blocks[120 * CHUNK_AREA + 5 * CHUNK_SIZE + 3], game.world.get_block_value(3, 120, 5));
|
||||
const y = 120 - game.world.shape.min_y;
|
||||
assertEquals(chunk.blocks[y * CHUNK_AREA + 5 * CHUNK_SIZE + 3], game.world.get_block_value(3, 120, 5));
|
||||
});
|
||||
|
||||
Deno.test("/viewdistance caps every player's view distance, and raising it gives them what they asked for", async () => {
|
||||
|
||||
+99
-7
@@ -4,6 +4,11 @@
|
||||
// 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 { CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { decode_level_chunk } from "@/common/chunk_streaming.ts";
|
||||
import { ItemStack } from "@/common/inventory.ts";
|
||||
import type { DimensionJson } from "@/common/worldgen/data.ts";
|
||||
import { load_and_check } from "@/tools/check_mods.ts";
|
||||
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
|
||||
@@ -12,8 +17,16 @@ 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 }) {
|
||||
// dimension is the default is up to the test, and so are the flat dimension's shape, what its ground is made of and
|
||||
// its generator's cell height
|
||||
function mods(options: {
|
||||
flat: boolean;
|
||||
bworld_default: boolean;
|
||||
flat_default?: boolean;
|
||||
shape?: Pick<DimensionJson, "min_y" | "max_y" | "sky_light">;
|
||||
ground?: string;
|
||||
cell_height?: number;
|
||||
}) {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_dimensions_" });
|
||||
copy_dir("mods/bworld", `${dir}/bworld`);
|
||||
const bworld_dimension = `${dir}/bworld/worldgen/dimension/overworld.json`;
|
||||
@@ -44,7 +57,7 @@ function mods(options: { flat: boolean; bworld_default: boolean; flat_default?:
|
||||
noise_settings: {
|
||||
id: "flat:flat",
|
||||
sea_level: SEA_TOP + 1,
|
||||
default_block: "bworld:glass",
|
||||
default_block: options.ground ?? "bworld:glass",
|
||||
cell_width: 1,
|
||||
terrain: {
|
||||
density: {
|
||||
@@ -52,20 +65,20 @@ function mods(options: { flat: boolean; bworld_default: boolean; flat_default?:
|
||||
argument1: below(GROUND_TOP),
|
||||
argument2: { type: "sub", argument1: { type: "x" }, argument2: GROUND_FROM_X - 0.5 },
|
||||
},
|
||||
cell_height: 1,
|
||||
cell_height: options.cell_height ?? 1,
|
||||
},
|
||||
caves: { density: 1, cell_height: 1 },
|
||||
caves: { density: 1, cell_height: options.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" } },
|
||||
surface_rule: { type: "block", result_state: { Name: options.ground ?? "bworld:glass" } },
|
||||
},
|
||||
});
|
||||
write("worldgen/dimension/flat.json", {
|
||||
format_version: 1,
|
||||
dimension: { id: "flat:flat", generator: "flat:flat", default: options.flat_default ?? true },
|
||||
dimension: { id: "flat:flat", generator: "flat:flat", default: options.flat_default ?? true, ...options.shape },
|
||||
});
|
||||
return dir;
|
||||
}
|
||||
@@ -106,3 +119,82 @@ Deno.test("the server doesn't start without a default dimension, or with two", a
|
||||
);
|
||||
Deno.removeSync(two, { recursive: true });
|
||||
});
|
||||
|
||||
// flat's ground is from x 16 on, x 40 is in chunk 2 at 8 across
|
||||
const GROUND_X = 40;
|
||||
const TALL = { min_y: -64, max_y: 320 };
|
||||
|
||||
Deno.test("a dimension's blocks go from its min_y to max_y - 1, and are sent and saved that way", async () => {
|
||||
const dir = mods({ flat: true, bworld_default: false, shape: TALL });
|
||||
const { game, join } = await test_game(dir);
|
||||
const world = game.world;
|
||||
const alice = join(1, "alice");
|
||||
assertEquals(alice.shape, { ...TALL, sky_light: true });
|
||||
assertEquals(alice.spawn.y, GROUND_TOP + 1, "on the ground, looking down from the top");
|
||||
assertEquals(world.get_block_id(GROUND_X, -64, 0), "bworld:glass", "generated down to the bottom");
|
||||
|
||||
for (const y of [-65, -64, 319, 320]) game.set_block(GROUND_X, y, 0, "bworld:stone");
|
||||
assertEquals(world.get_block_id(GROUND_X, -65, 0), "cubi:air");
|
||||
assertEquals(world.get_block_id(GROUND_X, 320, 0), "cubi:air");
|
||||
|
||||
// what clients get: every block from the bottom up
|
||||
const chunk = decode_level_chunk(world.level_chunk(2, 0));
|
||||
assertEquals(chunk.blocks.length, CHUNK_AREA * 384);
|
||||
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));
|
||||
|
||||
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");
|
||||
});
|
||||
|
||||
Deno.test("under a dimension's ground it's dark, above its top it's open sky, and things fall out of its bottom", async () => {
|
||||
const dir = mods({ flat: true, bworld_default: false, shape: TALL, ground: "bworld:stone" });
|
||||
const { game } = await test_game(dir);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
const world = game.world;
|
||||
assertEquals(world.light_at(GROUND_X, -60, 0), { sky: 0, block: 0 });
|
||||
assertEquals(world.light_at(GROUND_X, 319, 0), { sky: 15, block: 0 });
|
||||
assertEquals(world.light_at(GROUND_X, 400, 0), { sky: 15, block: 0 });
|
||||
|
||||
// the sea side has nothing under the water, items sink out of the world 64 blocks under its bottom
|
||||
const stays = game.entities.spawn_item(-40, -64 - 60, 0, new ItemStack("bworld:stone"));
|
||||
const falls = game.entities.spawn_item(-40, -64 - 70, 0, new ItemStack("bworld:stone"));
|
||||
stays.tick();
|
||||
falls.tick();
|
||||
assert(!stays.removed);
|
||||
assert(falls.removed);
|
||||
});
|
||||
|
||||
Deno.test("in a dimension without sky light only blocks give light", async () => {
|
||||
const dir = mods({ flat: true, bworld_default: false, shape: { sky_light: false } });
|
||||
const { game, join } = await test_game(dir);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
assertEquals(join(1, "alice").shape, { min_y: 0, max_y: 256, sky_light: false });
|
||||
const world = game.world;
|
||||
assertEquals(world.light_at(GROUND_X, GROUND_TOP + 5, 0), { sky: 0, block: 0 }, "in the open");
|
||||
assertEquals(world.light_at(GROUND_X, 300, 0), { sky: 0, block: 0 }, "above the world");
|
||||
game.set_block(GROUND_X, GROUND_TOP + 1, 0, "bworld:light");
|
||||
assertEquals(world.light_at(GROUND_X, GROUND_TOP + 2, 0).sky, 0);
|
||||
assert(world.light_at(GROUND_X, GROUND_TOP + 2, 0).block > 0, "lit by the light block");
|
||||
});
|
||||
|
||||
Deno.test("a dimension's min_y and max_y have to be whole sections, and fit its generator's cells", async () => {
|
||||
const errors = (options: Parameters<typeof mods>[0]) => {
|
||||
const dir = mods(options);
|
||||
const report = load_and_check(dir).find((mod) => mod.id === "flat")!.report;
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
return report.errors.filter((error) => error.includes("worldgen/dimension/flat.json"));
|
||||
};
|
||||
assertEquals(errors({ flat: true, bworld_default: false, shape: TALL }), []);
|
||||
for (const shape of [{ min_y: -8 }, { max_y: 0 }, { min_y: -4096 }, { max_y: 4096 }]) {
|
||||
assertEquals(errors({ flat: true, bworld_default: false, shape }).length, 1, JSON.stringify(shape));
|
||||
}
|
||||
// sections of 16, but the generator samples every 32 blocks
|
||||
const shape = { min_y: -48, max_y: 320 };
|
||||
assert(errors({ flat: true, bworld_default: false, shape, cell_height: 32 })[0].includes("cell_height 32"));
|
||||
|
||||
const dir = mods({ flat: true, bworld_default: false, shape, cell_height: 32 });
|
||||
await assertRejects(() => test_game(dir), Error, "flat.json: can't be generated by flat:flat");
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
@@ -4,17 +4,21 @@
|
||||
// entities are lit from the light the chunk worker sends back with each mesh, one byte per block: block << 4 | sky
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { DEFAULT_SHAPE, type WorldShapeJson } from "@/common/world_shape.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
// the middle chunk's light, with a stone roof at y 20 over x 0-7 and a light block at 12, 5, 8
|
||||
async function mesh_light() {
|
||||
// the middle chunk's light, with a stone roof at y 20 over x 0-7 and a light block at 12, 5, 8. y counts from the
|
||||
// bottom of the world
|
||||
async function mesh_light(shape: WorldShapeJson = DEFAULT_SHAPE) {
|
||||
const volume = CHUNK_AREA * (shape.max_y - shape.min_y);
|
||||
await test_game("mods");
|
||||
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
|
||||
const nid = (id: string) => blocks.findIndex((b) => b?.id === id);
|
||||
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
|
||||
worker.postMessage({
|
||||
type: "init",
|
||||
shape,
|
||||
blocks_registry: JSON.parse(JSON.stringify(blocks)),
|
||||
models: {},
|
||||
textures_info: { "cubi:missing": { x: 0, y: 0, width: 16, height: 16 } },
|
||||
@@ -22,16 +26,16 @@ async function mesh_light() {
|
||||
visuals: [],
|
||||
});
|
||||
|
||||
const chunk = new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT);
|
||||
const chunk = new Uint32Array(volume);
|
||||
const at = (x: number, y: number, z: number) => y * CHUNK_AREA + z * CHUNK_SIZE + x;
|
||||
for (let x = 0; x < 8; x++) {
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) chunk[at(x, 20, z)] = nid("bworld:stone");
|
||||
}
|
||||
chunk[at(12, 5, 8)] = nid("bworld:light");
|
||||
const empty = () => new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT);
|
||||
const empty = () => new Uint32Array(volume);
|
||||
const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()];
|
||||
|
||||
const meshed = await new Promise<{ light: Uint8Array }>((resolve) => {
|
||||
const meshed = await new Promise<{ light: Uint8Array; max_y: number }>((resolve) => {
|
||||
worker.onmessage = (e) => resolve(e.data);
|
||||
worker.postMessage({
|
||||
type: "mesh",
|
||||
@@ -48,12 +52,12 @@ async function mesh_light() {
|
||||
const packed = meshed.light[at(x, y, z)];
|
||||
return { block: packed >> 4, sky: packed & 15 };
|
||||
};
|
||||
return { light, size: meshed.light.length };
|
||||
return { light, size: meshed.light.length, max_y: meshed.max_y };
|
||||
}
|
||||
|
||||
Deno.test("each mesh comes with its chunk's block and sky light, packed a byte per block", async () => {
|
||||
const { light, size } = await mesh_light();
|
||||
assertEquals(size, CHUNK_AREA * CHUNK_HEIGHT);
|
||||
assertEquals(size, DEFAULT_SHAPE.volume);
|
||||
assertEquals(light(12, 30, 8), { block: 0, sky: 15 }, "open sky");
|
||||
assertEquals(light(12, 6, 8), { block: 13, sky: 15 }, "next to the light block");
|
||||
// under the roof, sky light comes in sideways from the open side, a level less each block
|
||||
@@ -61,3 +65,17 @@ Deno.test("each mesh comes with its chunk's block and sky light, packed a byte p
|
||||
assertEquals(light(4, 10, 8).sky, 11);
|
||||
assertEquals(light(4, 20, 8), { block: 0, sky: 0 }, "inside the stone");
|
||||
});
|
||||
|
||||
Deno.test("without sky light, only blocks light the world", async () => {
|
||||
const { light } = await mesh_light({ min_y: 0, max_y: 256, sky_light: false });
|
||||
assertEquals(light(12, 30, 8), { block: 0, sky: 0 }, "the open air is dark");
|
||||
assertEquals(light(12, 6, 8), { block: 13, sky: 0 }, "next to the light block");
|
||||
});
|
||||
|
||||
Deno.test("a dimension that starts below 0 is lit the same, and meshed where its blocks are", async () => {
|
||||
const { light, size, max_y } = await mesh_light({ min_y: -64, max_y: 320, sky_light: true });
|
||||
assertEquals(size, CHUNK_AREA * 384);
|
||||
assertEquals(light(12, 30, 8), { block: 0, sky: 15 });
|
||||
assertEquals(light(4, 10, 8).sky, 11);
|
||||
assertEquals(max_y, -64 + 21, "the top of the roof");
|
||||
});
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// features are code or data, and use each other: data features with types and modifiers scripts register, scripts
|
||||
// placing data features. they run by step, then mod, data before code. see docs/modding/worldgen/index.md
|
||||
import { assert, assertEquals, assertRejects } from "@std/assert";
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
@@ -98,15 +98,16 @@ Deno.test("data features use types and modifiers scripts register, scripts place
|
||||
"testmod:place_one",
|
||||
]);
|
||||
|
||||
const { min_y, max_y } = game.world.shape;
|
||||
const at = (blocks: Uint32Array, x: number, y: number, z: number) =>
|
||||
blocks[y * CHUNK_AREA + z * CHUNK_SIZE + x];
|
||||
blocks[(y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x];
|
||||
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);
|
||||
// the pillar in the middle, which the code feature after it in its step saw
|
||||
let pillar = 0;
|
||||
for (let y = 0; y < CHUNK_HEIGHT; y++) if (at(blocks, 8, y, 8) === glass) pillar++;
|
||||
for (let y = min_y; y < max_y; y++) if (at(blocks, 8, y, 8) === glass) pillar++;
|
||||
assertEquals(pillar, 3);
|
||||
assertEquals((globalThis as unknown as { glass_seen: number }).glass_seen, 3);
|
||||
// the one place_one put at 2, 200, 2
|
||||
|
||||
+4
-2
@@ -12,7 +12,8 @@ import {
|
||||
} from "@/common/everything_registry.ts";
|
||||
import { block_from_json, block_to_json, type BlockJson, validate_block } from "@/common/mod_data.ts";
|
||||
import { cube_face_texture } from "@/common/block_models.ts";
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
|
||||
import { get_state_value, set_state_value } from "@/common/utils.ts";
|
||||
import { cell_index, corner_height, fluid_quads, type FluidCell } from "@/client/workers/fluid_mesh.ts";
|
||||
import { TERRAIN_VERTEX_BYTES } from "@/client/workers/chunk_messages.ts";
|
||||
@@ -172,6 +173,7 @@ async function mesh(blocks: [number, number, number, string, number?][]) {
|
||||
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
|
||||
worker.postMessage({
|
||||
type: "init",
|
||||
shape: DEFAULT_SHAPE,
|
||||
// with the registry's gaps, like the client sends it
|
||||
blocks_registry: registry.map((block) => JSON.parse(JSON.stringify(block))),
|
||||
models: {},
|
||||
@@ -179,7 +181,7 @@ async function mesh(blocks: [number, number, number, string, number?][]) {
|
||||
image: { width: 1024, height: 1024 },
|
||||
visuals: [],
|
||||
});
|
||||
const chunks = Array.from({ length: 9 }, () => new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT));
|
||||
const chunks = Array.from({ length: 9 }, () => new Uint32Array(DEFAULT_SHAPE.volume));
|
||||
for (const [x, y, z, id, level] of blocks) {
|
||||
const chunk = x < 0 ? chunks[3] : chunks[4];
|
||||
chunk[y * CHUNK_AREA + z * CHUNK_SIZE + ((x + CHUNK_SIZE) % CHUNK_SIZE)] = ids[id] | ((level ?? 0) << 16);
|
||||
|
||||
+10
-3
@@ -149,13 +149,20 @@ export async function test_game(
|
||||
|
||||
// the block id and state bits at x, y, z in the chunks among messages, read the way a client does with the table
|
||||
// join sent. undefined when no chunk there was sent
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export function sent_block(messages: any[], join: { blocks: string[] }, x: number, y: number, z: number) {
|
||||
export function sent_block(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
messages: any[],
|
||||
join: { blocks: string[]; shape: { min_y: number } },
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
) {
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
const chunk = messages.findLast((m) => m.type === "level_chunk" && m.x === chunk_x && m.z === chunk_z);
|
||||
if (!chunk) return undefined;
|
||||
const value = chunk.blocks[y * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
|
||||
const ly = y - join.shape.min_y;
|
||||
const value = chunk.blocks[ly * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
|
||||
return [join.blocks[value & ID_MASK], value >>> 16];
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -295,7 +295,7 @@ async function drawn_juicer() {
|
||||
block_visuals.set(juicer, { mod: "bworld", replaces_model: false, create: made.get("bworld:juicer")! });
|
||||
const data: Record<string, unknown> = {};
|
||||
const visuals = new Visuals({ get_block_data: () => data, get_light: () => 0 });
|
||||
visuals.meshed(0, 0, Uint32Array.of(100 * CHUNK_AREA + 1 * CHUNK_SIZE + 1, block_value(juicer, 0)));
|
||||
visuals.meshed(0, 0, 0, Uint32Array.of(100 * CHUNK_AREA + 1 * CHUNK_SIZE + 1, block_value(juicer, 0)));
|
||||
const set = (field: string, value: unknown) => {
|
||||
data[field] = value;
|
||||
visuals.data_changed(1, 100, 1);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { AIR, CHUNK_HEIGHT } from "@/common/constants.ts";
|
||||
import { AIR } from "@/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "@/common/everything_registry.ts";
|
||||
import { block_from_json, block_to_json, item_from_json, item_to_json } from "@/common/mod_data.ts";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
@@ -97,7 +97,7 @@ Deno.test("a mod made from the template loads and runs", async () => {
|
||||
send(1, { type: "chat", text: "/give copper_tools:example_block" });
|
||||
const inventory = inventory_of(take(1));
|
||||
send(1, { type: "select_slot", slot: inventory.findIndex((i: Msg) => i?.id === "copper_tools:example_block") });
|
||||
let y = CHUNK_HEIGHT - 1;
|
||||
let y = game.world.shape.max_y - 1;
|
||||
while (game.world.get_block_nid(1, y, 1) === AIR) y--;
|
||||
// solid ground to build on: placing into water would replace it
|
||||
game.set_block(1, y, 1, "bworld:stone");
|
||||
@@ -154,6 +154,7 @@ Deno.test("the template's worldgen feature places boulders, the same way every t
|
||||
})),
|
||||
ores: worldgen.ores,
|
||||
generator: "bworld:overworld",
|
||||
shape: game.world.shape,
|
||||
});
|
||||
for (let cx = 0; cx < 20; cx++) {
|
||||
assertEquals(
|
||||
|
||||
+6
-2
@@ -17,6 +17,10 @@ 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 { DEFAULT_SHAPE } from "@/common/world_shape.ts";
|
||||
|
||||
// the base game's dimension, which the test worlds play
|
||||
const VOLUME = DEFAULT_SHAPE.volume;
|
||||
|
||||
const ENGINE_TEXTURES = ["cubi:missing", ...Array.from({ length: 9 }, (_, i) => `cubi:break_${i}`)];
|
||||
|
||||
@@ -403,8 +407,8 @@ Deno.test({
|
||||
await write_chunks(
|
||||
region,
|
||||
await Promise.all(saved.map(async ([x, z]) => {
|
||||
const chunk = decode_chunk((await read_chunk(region, x, z))!);
|
||||
chunk.blocks[255 * 256] = chunk.palette.push(["bworld:stone", 0]) - 1;
|
||||
const chunk = decode_chunk((await read_chunk(region, x, z))!, VOLUME);
|
||||
chunk.blocks[VOLUME - 256] = chunk.palette.push(["bworld:stone", 0]) - 1;
|
||||
return { x, z, data: encode_chunk(chunk as Required<typeof chunk>) };
|
||||
})),
|
||||
);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// monsters spawning on their own in the dark near players, like undergarden's rotwalkers, see natural_spawner.ts and
|
||||
// mods/undergarden/worldgen/spawns.json
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { decode_level_chunk } from "@/common/chunk_streaming.ts";
|
||||
import {
|
||||
block_light_emission,
|
||||
@@ -31,8 +31,8 @@ async function setup(roof = "bworld:stone") {
|
||||
const x0 = CHUNK * CHUNK_SIZE;
|
||||
for (let x = x0; x < x0 + CHUNK_SIZE; x++) {
|
||||
for (let z = x0; z < x0 + CHUNK_SIZE; z++) {
|
||||
for (let y = 0; y < FLOOR; y++) world.set_block(x, y, z, "bworld:stone");
|
||||
for (let y = ROOF + 1; y < CHUNK_HEIGHT; y++) world.set_block(x, y, z, "cubi:air");
|
||||
for (let y = world.shape.min_y; y < FLOOR; y++) world.set_block(x, y, z, "bworld:stone");
|
||||
for (let y = ROOF + 1; y < world.shape.max_y; y++) world.set_block(x, y, z, "cubi:air");
|
||||
}
|
||||
}
|
||||
for (let x = x0 - 1; x <= x0 + CHUNK_SIZE; x++) {
|
||||
@@ -86,7 +86,7 @@ Deno.test("the server's light is the same as what clients work out", async () =>
|
||||
emission[nid] = block_light_emission(block);
|
||||
});
|
||||
opacity[REGION_VOID] = 15;
|
||||
const region = new LightRegion();
|
||||
const region = new LightRegion(world.shape);
|
||||
const chunks = [];
|
||||
for (let dz = -1; dz <= 1; dz++) {
|
||||
for (let dx = -1; dx <= 1; dx++) {
|
||||
@@ -97,7 +97,8 @@ Deno.test("the server's light is the same as what clients work out", async () =>
|
||||
region.compute({ opacity, emission });
|
||||
|
||||
const check = (x: number, y: number, z: number) => {
|
||||
const index = y * REGION_LAYER + (z - x0 + CHUNK_SIZE) * REGION_SIZE + x - x0 + CHUNK_SIZE;
|
||||
const index = (y - world.shape.min_y) * REGION_LAYER + (z - x0 + CHUNK_SIZE) * REGION_SIZE + x - x0 +
|
||||
CHUNK_SIZE;
|
||||
const expected = { sky: region.sky[index], block: region.block_light[index] };
|
||||
assertEquals(world.light_at(x, y, z), expected, `at ${x}, ${y}, ${z}`);
|
||||
};
|
||||
|
||||
@@ -54,6 +54,7 @@ async function mesh_chunks(radius: number) {
|
||||
});
|
||||
worker.postMessage({
|
||||
type: "init",
|
||||
shape: game.world.shape,
|
||||
blocks_registry: JSON.parse(JSON.stringify(blocks)),
|
||||
models: {},
|
||||
textures_info: { "cubi:missing": { x: 0, y: 0, width: 16, height: 16 } },
|
||||
|
||||
@@ -59,7 +59,7 @@ Deno.test("a mod's biome generates where its rule, added to the base game's, say
|
||||
if (biome !== "lavender:fields") return;
|
||||
fields++;
|
||||
// its surface: the top block of the column is its sand
|
||||
let y = 255;
|
||||
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"]);
|
||||
});
|
||||
|
||||
+5
-3
@@ -4,7 +4,8 @@
|
||||
// blocks with a "tint" are drawn in their biome's grass or foliage color, blended across biome edges, or a fixed one. water is its biome's water color
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
import { get_biome } from "@/common/worldgen/biomes.ts";
|
||||
import { type LayerMesh, TERRAIN_VERTEX_BYTES } from "@/client/workers/chunk_messages.ts";
|
||||
@@ -22,6 +23,7 @@ async function mesh_block(id: string, x: number, middle: Colors, west: Colors |
|
||||
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
|
||||
worker.postMessage({
|
||||
type: "init",
|
||||
shape: DEFAULT_SHAPE,
|
||||
blocks_registry: JSON.parse(JSON.stringify(blocks)),
|
||||
models: {},
|
||||
textures_info: { "cubi:missing": { x: 0, y: 0, width: 16, height: 16 } },
|
||||
@@ -29,9 +31,9 @@ async function mesh_block(id: string, x: number, middle: Colors, west: Colors |
|
||||
visuals: [],
|
||||
});
|
||||
|
||||
const chunk = new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT);
|
||||
const chunk = new Uint32Array(DEFAULT_SHAPE.volume);
|
||||
chunk[10 * CHUNK_AREA + 8 * CHUNK_SIZE + x] = block_ids[id];
|
||||
const empty = () => new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT);
|
||||
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) => {
|
||||
|
||||
+15
-13
@@ -6,7 +6,8 @@
|
||||
import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert";
|
||||
import { mat4 } from "gl-matrix";
|
||||
import { BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
|
||||
import { block_value } from "@/common/utils.ts";
|
||||
import type { ModListing } from "@/common/mod_loader.ts";
|
||||
import { read_mod } from "@/common/mod.ts";
|
||||
@@ -87,13 +88,14 @@ Deno.test("the chunk mesher lists the blocks with visuals, and leaves out models
|
||||
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
|
||||
worker.postMessage({
|
||||
type: "init",
|
||||
shape: DEFAULT_SHAPE,
|
||||
blocks_registry: JSON.parse(JSON.stringify(EverythingRegistry.get_registry<BlockRegistry>("blocks"))),
|
||||
models: Object.fromEntries(EverythingRegistry.entries("models")),
|
||||
textures_info: { "cubi:missing": { x: 0, y: 0, width: 16, height: 16 } },
|
||||
image: { width: 1024, height: 1024 },
|
||||
visuals: [[nid("bworld:stone"), true], [nid("bworld:cobblestone"), false]],
|
||||
});
|
||||
const chunks = Array.from({ length: 9 }, () => new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT));
|
||||
const chunks = Array.from({ length: 9 }, () => new Uint32Array(DEFAULT_SHAPE.volume));
|
||||
const middle = chunks[4];
|
||||
middle[index_of(1, 64, 1)] = value("bworld:stone");
|
||||
middle[index_of(4, 64, 1)] = value("bworld:cobblestone");
|
||||
@@ -155,12 +157,12 @@ Deno.test("a visual is made with its block, told when its client data changes, a
|
||||
});
|
||||
const shaft = (axis: number) => Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", axis));
|
||||
|
||||
visuals.meshed(0, 0, shaft(1));
|
||||
visuals.meshed(0, 0, 0, shaft(1));
|
||||
assertEquals(log.splice(0), ["create testmod:shaft 1 64 1 axis 1"]);
|
||||
// the half model has one face, the block's own model is a cube
|
||||
assertEquals(drawn(visuals), ["1 quads x1", "6 quads x1"]);
|
||||
// meshed again for something else, it stays
|
||||
visuals.meshed(0, 0, shaft(1));
|
||||
visuals.meshed(0, 0, 0, shaft(1));
|
||||
assertEquals(log.splice(0), []);
|
||||
|
||||
data.speed = 32;
|
||||
@@ -175,15 +177,15 @@ Deno.test("a visual is made with its block, told when its client data changes, a
|
||||
assertEquals(log.splice(0), ["frame 10.5"]);
|
||||
|
||||
// its states changing makes it again
|
||||
visuals.meshed(0, 0, shaft(2));
|
||||
visuals.meshed(0, 0, 0, shaft(2));
|
||||
assertEquals(log.splice(0), ["delete 1 64 1", "create testmod:shaft 1 64 1 axis 2"]);
|
||||
assertEquals(drawn(visuals), ["1 quads x1", "6 quads x1"]);
|
||||
// and it goes with the block, or with the chunk, and so do its instances
|
||||
visuals.meshed(0, 0, new Uint32Array());
|
||||
visuals.meshed(0, 0, 0, new Uint32Array());
|
||||
assertEquals(log.splice(0), ["delete 1 64 1"]);
|
||||
assert(!visuals.has(0, 0));
|
||||
assertEquals(drawn(visuals), []);
|
||||
visuals.meshed(0, 0, shaft(1));
|
||||
visuals.meshed(0, 0, 0, shaft(1));
|
||||
visuals.unload(0, 0);
|
||||
assertEquals(log.splice(0), ["create testmod:shaft 1 64 1 axis 1", "delete 1 64 1"]);
|
||||
assertEquals(drawn(visuals), []);
|
||||
@@ -214,13 +216,13 @@ Deno.test("a visual that throws is logged and left out, and not made again until
|
||||
const console_error = console.error;
|
||||
console.error = (...args) => errors.push(args);
|
||||
try {
|
||||
visuals.meshed(0, 0, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 1)));
|
||||
visuals.meshed(0, 0, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 1)));
|
||||
visuals.meshed(0, 0, 0, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 1)));
|
||||
visuals.meshed(0, 0, 0, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 1)));
|
||||
assertEquals([made, errors.length], [1, 1]);
|
||||
assertEquals(drawn(visuals), []);
|
||||
|
||||
// an on_frame that throws isn't called again
|
||||
visuals.meshed(0, 0, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 2)));
|
||||
visuals.meshed(0, 0, 0, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 2)));
|
||||
for (let frame = 0; frame < 3; frame++) visuals.frame([{ x: 0, z: 0 }], frame);
|
||||
assertEquals([made, errors.length], [3, 2]);
|
||||
} finally {
|
||||
@@ -248,7 +250,7 @@ Deno.test("an instance's placement, spin and light are kept for the gpu, and onl
|
||||
const shafts = Uint32Array.of(
|
||||
...[1, 2, 3].flatMap((x) => [index_of(x, 64, 1), value("testmod:shaft")]),
|
||||
);
|
||||
visuals.meshed(0, 0, shafts);
|
||||
visuals.meshed(0, 0, 0, shafts);
|
||||
const group = [...visuals.groups()].find((group) => group.instances.length > 0)!;
|
||||
const at = (i: number) => [...group.data.subarray(i * INSTANCE_FLOATS, (i + 1) * INSTANCE_FLOATS)];
|
||||
const sent = () => {
|
||||
@@ -273,7 +275,7 @@ Deno.test("an instance's placement, spin and light are kept for the gpu, and onl
|
||||
|
||||
// a new light comes with the chunk's mesh
|
||||
light = 0x0f;
|
||||
visuals.meshed(0, 0, shafts);
|
||||
visuals.meshed(0, 0, 0, shafts);
|
||||
assertEquals([at(0)[21], at(0)[22]], [0.5 / 16, 15.5 / 16]);
|
||||
assertEquals(sent(), [0, 1]);
|
||||
block_visuals.clear();
|
||||
@@ -291,7 +293,7 @@ function one_visual(nid: (id: string) => number, value: (id: string) => number,
|
||||
return {};
|
||||
},
|
||||
});
|
||||
const mesh = (blocks: number[]) => visuals.meshed(0, 0, Uint32Array.from(blocks));
|
||||
const mesh = (blocks: number[]) => visuals.meshed(0, 0, 0, Uint32Array.from(blocks));
|
||||
mesh([index_of(1, 64, 1), value("testmod:shaft")]);
|
||||
return { visuals, mesh };
|
||||
}
|
||||
|
||||
+22
-16
@@ -4,14 +4,18 @@
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { AIR_ID } from "@/common/protocol.ts";
|
||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { CHUNK_VOLUME, decode_chunk, encode_chunk, SavedChunk } from "@/server/game/saves.ts";
|
||||
import { decode_chunk, encode_chunk, SavedChunk } from "@/server/game/saves.ts";
|
||||
import { list_chunks, read_chunk, region_file, write_chunks } from "@/server/regions.ts";
|
||||
import { sent_block, test_game } from "./helpers.ts";
|
||||
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
|
||||
|
||||
// the base game's dimension, which the test worlds play
|
||||
const VOLUME = DEFAULT_SHAPE.volume;
|
||||
|
||||
function chunk_data(seed: number, palette_size = 3) {
|
||||
const palette: [string, number][] = [];
|
||||
for (let i = 0; i < palette_size; i++) palette.push([`test:block_${i}`, i % 5]);
|
||||
const blocks = new Uint16Array(CHUNK_VOLUME).map((_, i) => (i * seed) % palette_size);
|
||||
const blocks = new Uint16Array(VOLUME).map((_, i) => (i * seed) % palette_size);
|
||||
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${(i * seed) % 4}`);
|
||||
return { palette, blocks, biomes };
|
||||
}
|
||||
@@ -21,11 +25,11 @@ const index = (x: number, y: number, z: number) => y * CHUNK_AREA + z * 16 + x;
|
||||
// what a chunk was saved as before chunks had biomes: a bit per block after the blocks instead, for whether world
|
||||
// generation made it
|
||||
function format_1(data: Uint8Array<ArrayBuffer>) {
|
||||
const chunk = decode_chunk(data);
|
||||
const chunk = decode_chunk(data, VOLUME);
|
||||
const biomes = 1 +
|
||||
[...new Set(chunk.biomes)].reduce((sum, id) => sum + 1 + new TextEncoder().encode(id).length, 0) +
|
||||
CHUNK_AREA;
|
||||
const old = new Uint8Array(data.length - biomes + CHUNK_VOLUME / 8);
|
||||
const old = new Uint8Array(data.length - biomes + VOLUME / 8);
|
||||
old.set(data.subarray(0, data.length - biomes));
|
||||
old[0] = 1;
|
||||
return old;
|
||||
@@ -34,16 +38,16 @@ function format_1(data: Uint8Array<ArrayBuffer>) {
|
||||
Deno.test("a chunk's blocks and biomes come back as they were saved, with more than 256 kinds too", () => {
|
||||
for (const palette_size of [1, 3, 256, 300]) {
|
||||
const chunk = chunk_data(7, palette_size);
|
||||
const decoded = decode_chunk(encode_chunk(chunk));
|
||||
const decoded = decode_chunk(encode_chunk(chunk), VOLUME);
|
||||
assertEquals(decoded.palette, chunk.palette);
|
||||
assertEquals(decoded.blocks, chunk.blocks);
|
||||
assertEquals(decoded.biomes, chunk.biomes);
|
||||
}
|
||||
// a byte a block when that's enough
|
||||
assert(encode_chunk(chunk_data(7, 256)).length < encode_chunk(chunk_data(7, 257)).length - CHUNK_VOLUME / 2);
|
||||
assert(encode_chunk(chunk_data(7, 256)).length < encode_chunk(chunk_data(7, 257)).length - VOLUME / 2);
|
||||
|
||||
// format 1 chunks still load, without biomes
|
||||
const old = decode_chunk(format_1(encode_chunk(chunk_data(3, 300))));
|
||||
const old = decode_chunk(format_1(encode_chunk(chunk_data(3, 300))), VOLUME);
|
||||
assertEquals(old.blocks, chunk_data(3, 300).blocks);
|
||||
assertEquals(old.biomes, undefined);
|
||||
});
|
||||
@@ -59,7 +63,9 @@ Deno.test("region files keep chunks, including negative ones, and saving one aga
|
||||
const by_position = async () =>
|
||||
Object.fromEntries(
|
||||
await Promise.all(
|
||||
positions.map(async ([x, z]) => [`${x},${z}`, decode_chunk((await read_chunk(dir, x, z))!).blocks[5]]),
|
||||
positions.map(async (
|
||||
[x, z],
|
||||
) => [`${x},${z}`, decode_chunk((await read_chunk(dir, x, z))!, VOLUME).blocks[5]]),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -97,11 +103,11 @@ Deno.test("every chunk generated is saved whole, and saved again only when it ch
|
||||
for (const chunk of ["0,0", "-2,2", "5,5"]) assert(positions.includes(chunk), `${chunk} is saved`);
|
||||
|
||||
// every block, and each column's biome
|
||||
const chunk = decode_chunk(saved.chunks.find((c) => c.x === 0 && c.z === 0)!.data);
|
||||
const chunk = decode_chunk(saved.chunks.find((c) => c.x === 0 && c.z === 0)!.data, VOLUME);
|
||||
assertEquals(chunk.palette[chunk.blocks[index(3, 120, 5)]], ["bworld:stone", 0]);
|
||||
assert(chunk.palette[chunk.blocks[index(3, 10, 5)]][0] !== AIR_ID);
|
||||
assertEquals(chunk.biomes![index(3, 0, 5)], game.world.biome_at(3, 5));
|
||||
const untouched = decode_chunk(saved.chunks.find((c) => c.x === 5 && c.z === 5)!.data);
|
||||
const untouched = decode_chunk(saved.chunks.find((c) => c.x === 5 && c.z === 5)!.data, VOLUME);
|
||||
assertEquals(
|
||||
Uint32Array.from(untouched.blocks, (block) => {
|
||||
const [id, state] = untouched.palette[block];
|
||||
@@ -135,7 +141,7 @@ async function saved_with_old_terrain() {
|
||||
game.set_block(3, 120, 5, "bworld:stone");
|
||||
const saved = game.save();
|
||||
const at_0_0 = saved.chunks.find((c) => c.x === 0 && c.z === 0)!;
|
||||
const chunk = decode_chunk(at_0_0.data);
|
||||
const chunk = decode_chunk(at_0_0.data, VOLUME);
|
||||
assertEquals(chunk.palette[chunk.blocks[index(3, 121, 5)]][0], AIR_ID);
|
||||
chunk.blocks[index(3, 121, 5)] = chunk.blocks[index(3, 120, 5)];
|
||||
at_0_0.data = encode_chunk(chunk as Required<typeof chunk>);
|
||||
@@ -152,18 +158,18 @@ Deno.test("saved chunks stay as they were saved, whatever generation makes there
|
||||
|
||||
Deno.test("chunks saved before they had biomes get them from generation, and are saved again with them", async () => {
|
||||
const { saved, at_0_0 } = await saved_with_old_terrain();
|
||||
const biomes = decode_chunk(at_0_0.data).biomes!;
|
||||
const biomes = decode_chunk(at_0_0.data, VOLUME).biomes!;
|
||||
at_0_0.data = format_1(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[index(3, 0, 5)]);
|
||||
const again = game.save().chunks.find((c) => c.x === 0 && c.z === 0)!;
|
||||
assertEquals(decode_chunk(again.data).biomes, biomes);
|
||||
assertEquals(decode_chunk(again.data, VOLUME).biomes, biomes);
|
||||
});
|
||||
|
||||
Deno.test("blocks of a mod that's gone are kept in the save until something replaces them", async () => {
|
||||
const { saved, at_0_0 } = await saved_with_old_terrain();
|
||||
const chunk = decode_chunk(at_0_0.data);
|
||||
const chunk = decode_chunk(at_0_0.data, VOLUME);
|
||||
chunk.palette[chunk.blocks[index(3, 120, 5)]] = ["gone:thing", 2];
|
||||
at_0_0.data = encode_chunk(chunk as Required<typeof chunk>);
|
||||
|
||||
@@ -171,12 +177,12 @@ Deno.test("blocks of a mod that's gone are kept in the save until something repl
|
||||
// air stands in for it
|
||||
assertEquals(game.world.get_block_id(3, 120, 5), AIR_ID);
|
||||
game.set_block(0, 120, 0, "bworld:stone");
|
||||
const again = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data);
|
||||
const again = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data, VOLUME);
|
||||
assertEquals(again.palette[again.blocks[index(3, 120, 5)]], ["gone:thing", 2]);
|
||||
assertEquals(again.palette[again.blocks[index(3, 121, 5)]], ["gone:thing", 2]);
|
||||
|
||||
game.set_block(3, 120, 5, "bworld:oak_planks");
|
||||
const replaced = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data);
|
||||
const replaced = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data, VOLUME);
|
||||
assertEquals(replaced.palette[replaced.blocks[index(3, 120, 5)]], ["bworld:oak_planks", 0]);
|
||||
assertEquals(replaced.palette[replaced.blocks[index(3, 121, 5)]], ["gone:thing", 2]);
|
||||
});
|
||||
|
||||
@@ -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_HEIGHT, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
@@ -20,11 +20,13 @@ Deno.test("cacti grow 1 to 3 tall on desert sand, 2 or 3 tries a chunk", async (
|
||||
for (let dx = -3; dx <= 3; dx++) {
|
||||
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 at = (x: number, y: number, z: number) => chunk.blocks[y * CHUNK_AREA + z * CHUNK_SIZE + x] & ID_MASK;
|
||||
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;
|
||||
let in_chunk = 0;
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
for (let y = 1; y < CHUNK_HEIGHT; y++) {
|
||||
for (let y = min_y + 1; y < max_y; y++) {
|
||||
// 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");
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT, SEA_LEVEL } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA, SEA_LEVEL } from "@/common/constants.ts";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
@@ -22,11 +22,12 @@ Deno.test("the overworld generates the same way every time, with water only belo
|
||||
|
||||
for (let i = 0; i < CHUNK_AREA; i++) {
|
||||
assert(blocks[i] !== 0, "the bottom of the world is solid");
|
||||
assertEquals(blocks[(CHUNK_HEIGHT - 1) * CHUNK_AREA + i], 0, "nothing reaches the top of the world");
|
||||
const top = (game.world.shape.height - 1) * CHUNK_AREA + i;
|
||||
assertEquals(blocks[top], 0, "nothing reaches the top of the world");
|
||||
}
|
||||
blocks.forEach((block, i) => {
|
||||
if (block === water) {
|
||||
assert(Math.floor(i / CHUNK_AREA) < SEA_LEVEL, "water above sea level");
|
||||
assert(game.world.shape.min_y + Math.floor(i / CHUNK_AREA) < SEA_LEVEL, "water above sea level");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user