Region file format

This commit is contained in:
2026-09-27 23:45:07 -03:00
parent a6869eeb59
commit 677ad8115a
18 changed files with 666 additions and 567 deletions
+4 -34
View File
@@ -165,11 +165,13 @@ Deno.test("a new watering can starts empty, and players are told so in its lore"
assertEquals(can.lore, "Water: 0/32");
// lore is worked out for players, it isn't saved
const saved = JSON.parse(t.game.save());
const saved = JSON.parse(t.game.save().level);
const saved_can = saved.players.alice?.inventory?.find((i: Msg) => i?.id === "bworld:watering_can");
assert(!saved_can || saved_can.lore === undefined);
t.game.on_disconnect(1);
const after = JSON.parse(t.game.save()).players.alice.inventory.find((i: Msg) => i?.id === "bworld:watering_can");
const after = JSON.parse(t.game.save().level).players.alice.inventory.find((i: Msg) =>
i?.id === "bworld:watering_can"
);
assertEquals(after.lore, undefined);
});
@@ -182,38 +184,6 @@ Deno.test("items are called by their name, or by one made from their id", async
assertEquals(display_name("test:thing"), "A Thing");
});
Deno.test("chests and furnaces from a world saved before they were mod code keep their items and keep smelting", async () => {
const t = await test_game("mods", Deno.readTextFileSync("tests/fixtures/world_v2.json"), "fixture-seed");
t.join(1, "alice");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const chest = last_container(t.take(1), "screen");
assertEquals(chest.length, 27);
assertEquals(chest[4], { id: "bworld:coal", count: 6 });
t.send(1, { type: "close_screen" });
t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top" });
let messages = t.take(1);
assertEquals(last_container(messages, "screen"), [
{ id: "bworld:iron_ore", count: 2 },
{ id: "bworld:coal", count: 2 },
{ id: "bworld:iron_ingot", count: 1 },
]);
const properties = messages.filter((m) => m.type === "screen_properties").at(-1).properties;
assertEquals(properties, { progress: 50, progress_max: 200, fuel: 751, fuel_max: 1000 });
// 150 more ticks finish the second ingot
for (let i = 0; i < 150; i++) t.game.tick();
messages = t.take(1);
assertEquals(last_container(messages, "screen")[2], { id: "bworld:iron_ingot", count: 2 });
// saved in the new format, nothing lost
const saved = JSON.parse(t.game.save());
assertEquals(saved.version, 3);
assert(saved.tiles.every((tile: Msg) => tile.data === undefined && tile.containers === undefined));
assertEquals(Object.keys(saved.containers).length, 2);
});
Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone, using up exactly that", async () => {
const t = await setup([["bworld:wood_pickaxe", 1], ["bworld:stone", 7]]);
t.game.set_block(1, 100, 0, "bworld:smithing_table");
+3 -3
View File
@@ -133,12 +133,12 @@ Deno.test("the host serves each mod's player copy by its hash, and a game made f
`${build}/assets/textures/index.json`,
JSON.stringify(ENGINE_TEXTURES.slice(1).map((id) => id.split(":")[1])),
);
const world = `${build}/world.json`;
const world = `${build}/world`;
const host = await start_host({
build_dir: build,
server_mods_dir: folder,
world_file: world,
world_dir: world,
seed: "bmod-seed",
on_fatal: (message) => {
throw new Error(message);
@@ -156,7 +156,7 @@ Deno.test("the host serves each mod's player copy by its hash, and a game made f
// the game worker loaded both mods, including the template's server script, and saves
await host.shutdown();
const saved = JSON.parse(Deno.readTextFileSync(world));
const saved = JSON.parse(Deno.readTextFileSync(`${world}/level.json`));
assertEquals(saved.seed, "bmod-seed");
for (const dir of [sources, folder, build]) Deno.removeSync(dir, { recursive: true });
+1 -1
View File
@@ -1,4 +1,4 @@
import { assert, assertEquals } from "@std/assert";
import { assertEquals } from "@std/assert";
import { join } from "@std/path";
import { CLIENT_MANIFEST, client_manifest, sha256, update_client } from "$/desktop/client_updates.ts";
-436
View File
@@ -1,436 +0,0 @@
{
"version": 2,
"seed": "fixture-seed",
"changes": [
[
-3,
99,
-3,
"bworld:stone"
],
[
-3,
99,
-2,
"bworld:stone"
],
[
-3,
99,
-1,
"bworld:stone"
],
[
-2,
99,
-3,
"bworld:stone"
],
[
-2,
99,
-2,
"bworld:stone"
],
[
-2,
99,
-1,
"bworld:stone"
],
[
-1,
99,
-3,
"bworld:stone"
],
[
-1,
99,
-2,
"bworld:stone"
],
[
-1,
99,
-1,
"bworld:stone"
],
[
-3,
99,
0,
"bworld:stone"
],
[
-3,
99,
1,
"bworld:stone"
],
[
-3,
99,
2,
"bworld:stone"
],
[
-3,
99,
3,
"bworld:stone"
],
[
-2,
99,
0,
"bworld:stone"
],
[
-2,
99,
1,
"bworld:stone"
],
[
-2,
99,
2,
"bworld:stone"
],
[
-2,
99,
3,
"bworld:stone"
],
[
-1,
99,
0,
"bworld:stone"
],
[
-1,
99,
1,
"bworld:stone"
],
[
-1,
99,
2,
"bworld:stone"
],
[
-1,
99,
3,
"bworld:stone"
],
[
0,
99,
-3,
"bworld:stone"
],
[
0,
99,
-2,
"bworld:stone"
],
[
0,
99,
-1,
"bworld:stone"
],
[
1,
99,
-3,
"bworld:stone"
],
[
1,
99,
-2,
"bworld:stone"
],
[
1,
99,
-1,
"bworld:stone"
],
[
2,
99,
-3,
"bworld:stone"
],
[
2,
99,
-2,
"bworld:stone"
],
[
2,
99,
-1,
"bworld:stone"
],
[
3,
99,
-3,
"bworld:stone"
],
[
3,
99,
-2,
"bworld:stone"
],
[
3,
99,
-1,
"bworld:stone"
],
[
0,
99,
0,
"bworld:stone"
],
[
0,
99,
1,
"bworld:stone"
],
[
0,
99,
2,
"bworld:stone"
],
[
0,
99,
3,
"bworld:stone"
],
[
1,
99,
0,
"bworld:stone"
],
[
1,
99,
1,
"bworld:stone"
],
[
1,
99,
2,
"bworld:stone"
],
[
1,
99,
3,
"bworld:stone"
],
[
2,
99,
0,
"bworld:stone"
],
[
2,
99,
1,
"bworld:stone"
],
[
2,
99,
2,
"bworld:stone"
],
[
2,
99,
3,
"bworld:stone"
],
[
3,
99,
0,
"bworld:stone"
],
[
3,
99,
1,
"bworld:stone"
],
[
3,
99,
2,
"bworld:stone"
],
[
3,
99,
3,
"bworld:stone"
],
[
1,
100,
0,
"bworld:chest"
],
[
2,
100,
0,
"bworld:furnace"
]
],
"tiles": [
{
"id": "bworld:chest",
"x": 1,
"y": 100,
"z": 0,
"data": {},
"containers": {
"main": [
null,
null,
null,
null,
{
"id": "bworld:coal",
"count": 6
},
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
]
}
},
{
"id": "bworld:furnace",
"x": 2,
"y": 100,
"z": 0,
"data": {
"progress": 50,
"progress_max": 200,
"fuel": 751,
"fuel_max": 1000
},
"containers": {
"main": [
{
"id": "bworld:iron_ore",
"count": 2
},
{
"id": "bworld:coal",
"count": 2
},
{
"id": "bworld:iron_ingot",
"count": 1
}
]
}
}
],
"players": {
"alice": {
"x": 0.5,
"y": 100,
"z": 0.5,
"yaw": 0,
"pitch": 0,
"selected_slot": 0,
"inventory": [
{
"id": "bworld:coal",
"count": 2
},
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
]
}
},
"entities": [],
"mod_storage": {}
}
+2 -1
View File
@@ -5,6 +5,7 @@ import type { ServerModSource } from "$/server/game/load_mods.ts";
import { GameServer } from "$/server/game/game_server.ts";
import { start_game } from "$/server/game/load_mods.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
import type { WorldSave } from "$/server/game/saves.ts";
// mods straight from their source folders, skipping the build: scripts are imported as typescript
export function mod_sources(mods_dir: string): ServerModSource[] {
@@ -37,7 +38,7 @@ export function mod_sources(mods_dir: string): ServerModSource[] {
}
// a game with an in memory host that remembers what it sent to each connection
export async function test_game(mods_dir: string, save?: string, seed = "test-seed") {
export async function test_game(mods_dir: string, save?: WorldSave, seed = "test-seed") {
EverythingRegistry.clear();
const outbox = new Map<number, unknown[]>();
const closed = new Set<number>();
+2 -1
View File
@@ -1,5 +1,6 @@
import { assert, assertEquals } from "@std/assert";
import { copy_dir, test_game } from "./helpers.ts";
import type { WorldSave } from "$/server/game/saves.ts";
// a mod that uses every part of the server api worth testing, and writes down what happened in its storage
const SERVER_SCRIPT = `
@@ -151,7 +152,7 @@ function test_mods() {
return dir;
}
async function setup(save?: string) {
async function setup(save?: WorldSave) {
const dir = test_mods();
const game = await test_game(dir, save);
game.join(1, "alice");
+134
View File
@@ -0,0 +1,134 @@
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 { 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 changes, not the whole chunks
const join = loaded.join(1, "alice");
assertEquals(join.changes.length, 3);
// 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, join } = 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");
assertEquals(join(1, "alice").changes.length, 2);
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]);
});