Wooooorld files

This commit is contained in:
2026-11-06 23:41:32 -03:00
parent 1fa47c027d
commit 8e14e9842a
9 changed files with 501 additions and 383 deletions
+19
View File
@@ -154,6 +154,25 @@ Deno.test("a dimension's blocks go from its min_y to max_y - 1, and are sent and
for (const y of [-64, 319]) assertEquals(loaded.game.world.get_block_id(GROUND_X, y, 0), "bworld:stone");
});
Deno.test("a chunk saved at another height keeps the sections both heights have", async () => {
const tall = mods({ flat: true, bworld_default: false, shape: TALL });
const { game } = await test_game(tall);
for (const y of [-64, 0, 255, 319]) game.set_block(GROUND_X, y, 0, "bworld:stone");
const saved = game.save();
const short = mods({ flat: true, bworld_default: false });
const { game: shorter } = await test_game(short, saved);
for (const y of [0, 255]) assertEquals(shorter.world.get_block_id(GROUND_X, y, 0), "bworld:stone");
assertEquals(shorter.world.get_block_id(GROUND_X, 254, 0), "cubi:air");
shorter.set_block(GROUND_X, 1, 0, "bworld:stone");
// and back, where what the shorter one didn't keep is air, like a dimension that grew
const { game: again } = await test_game(tall, shorter.save());
for (const dir of [tall, short]) Deno.removeSync(dir, { recursive: true });
for (const y of [0, 1, 255]) assertEquals(again.world.get_block_id(GROUND_X, y, 0), "bworld:stone");
for (const y of [-64, 319]) assertEquals(again.world.get_block_id(GROUND_X, y, 0), "cubi:air");
});
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);
+3 -6
View File
@@ -17,10 +17,7 @@ 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;
import { section_index } from "@/common/section.ts";
const ENGINE_TEXTURES = ["cubi:missing", ...Array.from({ length: 9 }, (_, i) => `cubi:break_${i}`)];
@@ -407,8 +404,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))!, VOLUME);
chunk.blocks[VOLUME - 256] = chunk.palette.push(["bworld:stone", 0]) - 1;
const chunk = decode_chunk((await read_chunk(region, x, z))!);
chunk.sections.at(-1)!.set(section_index(0, 15, 0), chunk.palette.push(["bworld:stone", 0]) - 1);
return { x, z, data: encode_chunk(chunk as Required<typeof chunk>) };
})),
);
+41 -2
View File
@@ -2,10 +2,19 @@
// 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 { assert, assertEquals, assertThrows } 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 { Reader, Writer } from "@/common/bytes.ts";
import {
ChunkColumn,
column_heights,
read_column,
Section,
SECTION_VOLUME,
to_flat,
write_column,
} from "@/common/section.ts";
import { WorldShape } from "@/common/world_shape.ts";
const SHAPE = new WorldShape({ min_y: -64, max_y: 320, sky_light: true });
@@ -87,3 +96,33 @@ Deno.test("column heights are the highest block and the lowest y that sees the s
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");
});
Deno.test("sections are written and read back as they were, however many kinds of block they hold", () => {
const random = new Alea("paletted");
// one kind, then the most and one more than each palette size holds, past what a palette is for
const kinds = [1, 2, 3, 4, 5, 16, 17, 256, 257, 4096];
const sections = kinds.map((count) => {
// big values too, with state bits
const values = Array.from({ length: count }, (_, i) => (i * 7919 + (i % 3) * 0x8000_0000) >>> 0);
return Section.of(
Uint32Array.from(
{ length: SECTION_VOLUME },
(_, i) => values[i < count ? i : Math.floor(random.next() * count)],
),
);
});
sections.push(new Section(GLASS));
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${i % 3}`);
const writer = new Writer();
write_column(writer, sections, biomes);
const bytes = writer.bytes();
const read = read_column(new Reader(bytes), (value) => value);
assertEquals(to_flat(read.sections), to_flat(sections));
assertEquals(read.biomes, biomes);
assertEquals(read.sections.at(-1)!.blocks, null, "a section of one block stays one value");
// the values as the reader numbers them
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));
});
+81 -51
View File
@@ -4,52 +4,82 @@
import { assert, assertEquals } from "@std/assert";
import { AIR_ID } from "@/common/protocol.ts";
import { CHUNK_AREA } from "@/common/constants.ts";
import { decode_chunk, encode_chunk, SavedChunk } from "@/server/game/saves.ts";
import { ChunkBlocks, 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";
import { DEFAULT_SHAPE, WorldShape } from "@/common/world_shape.ts";
import { ChunkColumn, section_index, to_flat } from "@/common/section.ts";
// the base game's dimension, which the test worlds play
const VOLUME = DEFAULT_SHAPE.volume;
function chunk_data(seed: number, palette_size = 3) {
// 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> {
const palette: [string, number][] = [];
for (let i = 0; i < palette_size; i++) palette.push([`test:block_${i}`, i % 5]);
const blocks = new Uint16Array(VOLUME).map((_, i) => (i * seed) % palette_size);
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, blocks, biomes };
return {
palette,
min_y: shape.min_y,
sections: ChunkColumn.from_flat(0, 0, shape, blocks, biomes).sections,
biomes,
};
}
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, 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 + VOLUME / 8);
old.set(data.subarray(0, data.length - biomes));
old[0] = 1;
return old;
// the palette entry of a saved chunk's block
function saved_block(chunk: ChunkBlocks, x: number, y: number, z: number) {
return chunk.palette[chunk.sections[(y - chunk.min_y) >> 4].get(section_index(x, y, z))];
}
function set_saved_block(chunk: ChunkBlocks, x: number, y: number, z: number, index: number) {
chunk.sections[(y - chunk.min_y) >> 4].set(section_index(x, y, z), index);
}
// a chunk as formats 1 and 2 saved it, 0 to 256 high and big-endian. format 1 had no biomes, and a bit per block after
// the blocks instead, for whether world generation made it
function old_format(format: 1 | 2, chunk: ChunkBlocks) {
const flat = to_flat(chunk.sections);
const wide = chunk.palette.length > 256;
const bytes: number[] = [format, ...u32(chunk.palette.length)];
for (const [id, state] of chunk.palette) bytes.push(...string(id), state >> 8, state & 255);
for (const index of flat) bytes.push(...wide ? [index >> 8, index & 255] : [index]);
if (format === 1) {
bytes.push(...new Uint8Array(flat.length / 8));
} else {
const biome_palette = [...new Set(chunk.biomes)];
bytes.push(biome_palette.length, ...biome_palette.flatMap(string));
bytes.push(...chunk.biomes!.map((biome) => biome_palette.indexOf(biome)));
}
return Uint8Array.from(bytes);
}
const u32 = (value: number) => [value >>> 24, (value >> 16) & 255, (value >> 8) & 255, value & 255];
const string = (text: string) => [text.length, ...new TextEncoder().encode(text)];
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), VOLUME);
const decoded = decode_chunk(encode_chunk(chunk));
assertEquals(decoded.palette, chunk.palette);
assertEquals(decoded.blocks, chunk.blocks);
assertEquals(decoded.min_y, chunk.min_y);
assertEquals(to_flat(decoded.sections), to_flat(chunk.sections));
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 - VOLUME / 2);
// a section that's all one block takes a few bytes
const uniform = chunk_data(0, 1);
assert(encode_chunk(uniform).length < 16 * 8 + 1024);
// format 1 chunks still load, without biomes
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);
// a taller dimension's, from below 0
const tall = chunk_data(5, 40, new WorldShape({ min_y: -64, max_y: 320, sky_light: true }));
const decoded = decode_chunk(encode_chunk(tall));
assertEquals([decoded.min_y, decoded.sections.length], [-64, 24]);
assertEquals(to_flat(decoded.sections), to_flat(tall.sections));
// formats 1 and 2 still load, 1 without biomes
for (const format of [1, 2] as const) {
const chunk = chunk_data(3, 300);
const old = decode_chunk(old_format(format, chunk));
assertEquals([old.min_y, old.palette], [0, chunk.palette]);
assertEquals(to_flat(old.sections), to_flat(chunk.sections));
assertEquals(old.biomes, format === 1 ? undefined : chunk.biomes);
}
});
Deno.test("region files keep chunks, including negative ones, and saving one again doesn't touch the others", async () => {
@@ -65,7 +95,7 @@ Deno.test("region files keep chunks, including negative ones, and saving one aga
await Promise.all(
positions.map(async (
[x, z],
) => [`${x},${z}`, decode_chunk((await read_chunk(dir, x, z))!, VOLUME).blocks[5]]),
) => [`${x},${z}`, decode_chunk((await read_chunk(dir, x, z))!).sections[0].get(5)]),
),
);
@@ -103,13 +133,13 @@ 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, 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, VOLUME);
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));
const untouched = decode_chunk(saved.chunks.find((c) => c.x === 5 && c.z === 5)!.data);
assertEquals(
Uint32Array.from(untouched.blocks, (block) => {
Uint32Array.from(to_flat(untouched.sections), (block) => {
const [id, state] = untouched.palette[block];
return id === AIR_ID ? 0 : game.world.block_ids[id] | (state << 16);
}),
@@ -141,9 +171,9 @@ 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, 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)];
const chunk = decode_chunk(at_0_0.data);
assertEquals(saved_block(chunk, 3, 121, 5)[0], AIR_ID);
set_saved_block(chunk, 3, 121, 5, chunk.palette.indexOf(saved_block(chunk, 3, 120, 5)));
at_0_0.data = encode_chunk(chunk as Required<typeof chunk>);
return { saved, at_0_0 };
}
@@ -158,33 +188,33 @@ 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, VOLUME).biomes!;
at_0_0.data = format_1(at_0_0.data);
const biomes = decode_chunk(at_0_0.data).biomes!;
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[index(3, 0, 5)]);
assertEquals(game.world.biome_at(3, 5), biomes[5 * 16 + 3]);
const again = game.save().chunks.find((c) => c.x === 0 && c.z === 0)!;
assertEquals(decode_chunk(again.data, VOLUME).biomes, biomes);
assertEquals(decode_chunk(again.data).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, VOLUME);
chunk.palette[chunk.blocks[index(3, 120, 5)]] = ["gone:thing", 2];
const chunk = decode_chunk(at_0_0.data);
chunk.palette[chunk.palette.indexOf(saved_block(chunk, 3, 120, 5))] = ["gone:thing", 2];
at_0_0.data = encode_chunk(chunk as Required<typeof chunk>);
const { game } = await test_game("mods", saved);
// 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, 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]);
const again = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data);
assertEquals(saved_block(again, 3, 120, 5), ["gone:thing", 2]);
assertEquals(saved_block(again, 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, 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]);
const replaced = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data);
assertEquals(saved_block(replaced, 3, 120, 5), ["bworld:oak_planks", 0]);
assertEquals(saved_block(replaced, 3, 121, 5), ["gone:thing", 2]);
});
Deno.test("chunks forgotten before they're saved are still saved, and load from the save after", async () => {