136 lines
6.4 KiB
TypeScript
136 lines
6.4 KiB
TypeScript
import { assert, assertEquals, assertNotEquals } from "@std/assert";
|
|
import { AIR_ID } from "$/common/protocol.ts";
|
|
import { CHUNK_VOLUME, decode_chunk, encode_chunk, is_changed, SavedChunk } from "$/server/game/saves.ts";
|
|
import { read_regions, region_file, write_chunks } from "$/server/regions.ts";
|
|
import { sent_block, test_game } from "./helpers.ts";
|
|
|
|
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 changed = new Uint8Array(CHUNK_VOLUME / 8).map((_, i) => (i * seed) & 0xFF);
|
|
return { palette, blocks, changed };
|
|
}
|
|
|
|
Deno.test("a chunk's blocks 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));
|
|
assertEquals(decoded.palette, chunk.palette);
|
|
assertEquals(decoded.blocks, chunk.blocks);
|
|
assertEquals(decoded.changed, chunk.changed);
|
|
}
|
|
// 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);
|
|
});
|
|
|
|
Deno.test("region files keep chunks, including negative ones, and saving one again doesn't touch the others", async () => {
|
|
const dir = Deno.makeTempDirSync();
|
|
const chunk = (x: number, z: number, seed: number): SavedChunk => ({
|
|
x,
|
|
z,
|
|
data: encode_chunk(chunk_data(seed)),
|
|
});
|
|
const by_position = (chunks: SavedChunk[]) =>
|
|
Object.fromEntries(chunks.map((c) => [`${c.x},${c.z}`, decode_chunk(c.data).blocks[5]]));
|
|
|
|
await write_chunks(dir, [chunk(0, 0, 1), chunk(31, 31, 2), chunk(-1, -1, 3), chunk(-33, 40, 4)]);
|
|
assertEquals(
|
|
[...Deno.readDirSync(dir)].map((e) => e.name).sort(),
|
|
[region_file(-1, -1), region_file(-2, 1), region_file(0, 0)].sort(),
|
|
);
|
|
assertEquals(by_position(await read_regions(dir)), { "0,0": 2, "31,31": 1, "-1,-1": 0, "-33,40": 2 });
|
|
|
|
// saved again, over and over: the file doesn't keep growing, sectors let go of get used again
|
|
const path = `${dir}/${region_file(0, 0)}`;
|
|
await write_chunks(dir, [chunk(0, 0, 2)]);
|
|
const size = Deno.statSync(path).size;
|
|
for (let i = 0; i < 5; i++) await write_chunks(dir, [chunk(0, 0, 2 + (i % 2))]);
|
|
assert(Deno.statSync(path).size <= size + 4096 * 2);
|
|
assertEquals(by_position(await read_regions(dir)), { "0,0": 1, "31,31": 1, "-1,-1": 0, "-33,40": 2 });
|
|
|
|
assertEquals(await read_regions(`${dir}/nothing`), []);
|
|
Deno.removeSync(dir, { recursive: true });
|
|
});
|
|
|
|
Deno.test("changed blocks are saved as whole chunks, and only chunks that changed are saved again", async () => {
|
|
const { game } = await test_game("mods");
|
|
game.set_block(3, 120, 5, "bworld:stone");
|
|
game.set_block(-20, 120, 40, "bworld:planks");
|
|
game.world.set_block(-20, 121, 40, "bworld:furnace", 1);
|
|
const saved = game.save();
|
|
assertEquals(saved.chunks.map((c) => [c.x, c.z]).sort(), [[-2, 2], [0, 0]]);
|
|
|
|
// the chunk has every block, and marks the ones generation didn't make
|
|
const chunk = decode_chunk(saved.chunks.find((c) => c.x === 0)!.data);
|
|
const index = 120 * 256 + 5 * 16 + 3;
|
|
assertEquals(chunk.palette[chunk.blocks[index]], ["bworld:stone", 0]);
|
|
assert(is_changed(chunk, index));
|
|
assert(chunk.palette.some(([id]) => id === "bworld:bedrock" || id === "bworld:stone"));
|
|
assertEquals(chunk.changed.reduce((sum, byte) => sum + byte.toString(2).split("1").length - 1, 0), 1);
|
|
|
|
assertEquals(game.save().chunks, []);
|
|
game.set_block(4, 120, 5, "bworld:stone");
|
|
assertEquals(game.save().chunks.map((c) => [c.x, c.z]), [[0, 0]]);
|
|
|
|
const loaded = await test_game("mods", saved);
|
|
assertEquals(loaded.game.world.get_block_id(3, 120, 5), "bworld:stone");
|
|
assertEquals(loaded.game.world.get_block_id(-20, 120, 40), "bworld:planks");
|
|
assertEquals(loaded.game.world.get_block_value(-20, 121, 40) >>> 16, 1);
|
|
// players get the whole chunks, with what was saved in them
|
|
const join = loaded.join(1, "alice");
|
|
loaded.send(1, { type: "move", x: 3.5, y: 130, z: 5.5, yaw: 0, pitch: 0 });
|
|
loaded.game.tick();
|
|
assertEquals(sent_block(loaded.take(1), join, 3, 120, 5), ["bworld:stone", 0]);
|
|
// nothing to save again after loading
|
|
assertEquals(loaded.game.save().chunks, []);
|
|
});
|
|
|
|
// a saved chunk with a block that isn't marked changed and isn't what generation makes: what's saved from a world
|
|
// generation that's since changed
|
|
async function saved_with_old_terrain() {
|
|
const { game } = await test_game("mods");
|
|
game.set_block(3, 120, 5, "bworld:stone");
|
|
const saved = game.save();
|
|
const chunk = decode_chunk(saved.chunks[0].data);
|
|
const index = 121 * 256 + 5 * 16 + 3;
|
|
assertEquals(chunk.palette[chunk.blocks[index]][0], AIR_ID);
|
|
chunk.blocks[index] = chunk.blocks[120 * 256 + 5 * 16 + 3];
|
|
saved.chunks[0].data = encode_chunk(chunk);
|
|
return { saved, level: JSON.parse(saved.level) };
|
|
}
|
|
|
|
Deno.test("when world generation is the same, blocks it made come from it", async () => {
|
|
const { saved } = await saved_with_old_terrain();
|
|
const { game } = await test_game("mods", saved);
|
|
assertEquals(game.world.get_block_id(3, 121, 5), AIR_ID);
|
|
assertEquals(game.save().chunks, []);
|
|
});
|
|
|
|
Deno.test("when world generation changed, saved chunks keep their terrain and are saved again", async () => {
|
|
const { saved, level } = await saved_with_old_terrain();
|
|
saved.level = JSON.stringify({ ...level, worldgen: "older" });
|
|
const { game } = await test_game("mods", saved);
|
|
assertEquals(game.world.get_block_id(3, 121, 5), "bworld:stone");
|
|
assertEquals(game.world.get_block_id(3, 120, 5), "bworld:stone");
|
|
|
|
const again = game.save();
|
|
assertNotEquals(JSON.parse(again.level).worldgen, "older");
|
|
const chunk = decode_chunk(again.chunks[0].data);
|
|
assert(is_changed(chunk, 121 * 256 + 5 * 16 + 3));
|
|
});
|
|
|
|
Deno.test("blocks of a mod that's gone are kept in the save", async () => {
|
|
const { saved } = await saved_with_old_terrain();
|
|
const chunk = decode_chunk(saved.chunks[0].data);
|
|
chunk.palette[chunk.blocks[120 * 256 + 5 * 16 + 3]] = ["gone:thing", 2];
|
|
saved.chunks[0].data = encode_chunk(chunk);
|
|
|
|
const { game } = await test_game("mods", saved);
|
|
// generation's block stands in for it
|
|
assertNotEquals(game.world.get_block_id(3, 120, 5), "gone:thing");
|
|
game.set_block(0, 120, 0, "bworld:stone");
|
|
const again = decode_chunk(game.save().chunks[0].data);
|
|
assertEquals(again.palette[again.blocks[120 * 256 + 5 * 16 + 3]], ["gone:thing", 2]);
|
|
});
|