World shape for dimension
This commit is contained in:
+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]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user