110 lines
4.5 KiB
TypeScript
110 lines
4.5 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the part of a block's tile data clients see: sent with its chunk, then field by field as it changes, saved, and gone
|
|
// with the block
|
|
import { assertEquals } from "@std/assert";
|
|
import { chunk_key, ClientLevel } from "$/client/level/client_level.ts";
|
|
import { Visuals } from "$/client/rendering/visuals.ts";
|
|
import { AIR_ID } from "$/common/protocol.ts";
|
|
import { copy_dir, stone_floor, test_game } from "./helpers.ts";
|
|
|
|
const SHAFT = [1, 99, 1] as const;
|
|
|
|
async function setup() {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_client_data_" });
|
|
copy_dir("mods/bworld", `${dir}/bworld`);
|
|
const t = await test_game(dir);
|
|
Deno.removeSync(dir, { recursive: true });
|
|
t.join(1, "alice");
|
|
stone_floor(t.game);
|
|
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
|
for (let i = 0; i < 5; i++) t.game.tick();
|
|
t.take(1);
|
|
const block = t.game.mods.block_ref(...SHAFT, "bworld:stone");
|
|
// what conn was told about the block since last asked
|
|
const sent = (conn: number) =>
|
|
t.take(conn).filter((m) => m.type === "block_data" && m.x === SHAFT[0] && m.y === SHAFT[1] && m.z === SHAFT[2])
|
|
.map((m) => m.data);
|
|
return { ...t, block, sent };
|
|
}
|
|
|
|
Deno.test("a block's client data goes to players field by field, only when a field changes", async () => {
|
|
const { block, sent } = await setup();
|
|
block.set_client_data("speed", 16);
|
|
block.set_client_data("axis", "x");
|
|
assertEquals(sent(1), [{ speed: 16 }, { axis: "x" }]);
|
|
|
|
// the same again sends nothing, and a change sends only that field
|
|
block.set_client_data("speed", 16);
|
|
block.set_client_data("speed", 32);
|
|
assertEquals(sent(1), [{ speed: 32 }]);
|
|
assertEquals(block.get_client_data("speed"), 32);
|
|
|
|
// removed, it's null
|
|
block.set_client_data("axis", undefined);
|
|
assertEquals(sent(1), [{ axis: null }]);
|
|
assertEquals(block.get_client_data("axis"), undefined);
|
|
});
|
|
|
|
Deno.test("client data is copied in and out, so changing a value after setting it doesn't change what's sent", async () => {
|
|
const { block, sent } = await setup();
|
|
const value = { rpm: 16 };
|
|
block.set_client_data("speed", value);
|
|
value.rpm = 64;
|
|
block.get_client_data<{ rpm: number }>("speed")!.rpm = 128;
|
|
assertEquals(block.get_client_data("speed"), { rpm: 16 });
|
|
assertEquals(sent(1), [{ speed: { rpm: 16 } }]);
|
|
});
|
|
|
|
Deno.test("a player getting the chunk gets every field with it, and it's saved", async () => {
|
|
const { game, block, join, sent } = await setup();
|
|
block.set_client_data("speed", 16);
|
|
block.set_client_data("axis", "x");
|
|
join(2, "bob");
|
|
for (let i = 0; i < 5; i++) game.tick();
|
|
assertEquals(sent(2), [{ speed: 16, axis: "x" }]);
|
|
|
|
const loaded = await test_game("mods", game.save());
|
|
assertEquals(loaded.game.world.get_tile(...SHAFT)?.client_data, { speed: 16, axis: "x" });
|
|
});
|
|
|
|
Deno.test("breaking the block takes every field away from everyone", async () => {
|
|
const { game, block, sent } = await setup();
|
|
block.set_client_data("speed", 16);
|
|
block.set_client_data("axis", "x");
|
|
sent(1);
|
|
game.set_block(...SHAFT, AIR_ID);
|
|
assertEquals(sent(1), [{ speed: null, axis: null }]);
|
|
assertEquals(game.world.get_tile(...SHAFT), undefined);
|
|
|
|
// air keeps none, and removing a field doesn't make a tile for it
|
|
game.mods.block_ref(...SHAFT, AIR_ID).set_client_data("speed", 16);
|
|
game.mods.block_ref(2, 99, 2, "bworld:stone").set_client_data("speed", undefined);
|
|
assertEquals(sent(1), []);
|
|
assertEquals(game.world.get_tile(...SHAFT), undefined);
|
|
assertEquals(game.world.get_tile(2, 99, 2), undefined);
|
|
});
|
|
|
|
Deno.test("the client keeps the fields it's sent for each block until they're removed", () => {
|
|
// only the parts of a level the client data needs, without its chunk workers
|
|
const level = Object.assign(Object.create(ClientLevel.prototype), {
|
|
chunks: new Map([[chunk_key(0, 0), {}]]),
|
|
block_data: new Map(),
|
|
visuals: new Visuals({ get_block_data: () => undefined, get_light: () => 0 }),
|
|
}) as ClientLevel;
|
|
level.set_block_data(1, 99, 1, { speed: 16, axis: "x" });
|
|
level.set_block_data(1, 99, 1, { speed: 32 });
|
|
assertEquals(level.get_block_data(1, 99, 1), { speed: 32, axis: "x" });
|
|
|
|
level.set_block_data(1, 99, 1, { speed: null });
|
|
assertEquals(level.get_block_data(1, 99, 1), { axis: "x" });
|
|
level.set_block_data(1, 99, 1, { axis: null });
|
|
assertEquals(level.get_block_data(1, 99, 1), undefined);
|
|
assertEquals(level.block_data.size, 0);
|
|
|
|
// not for chunks it doesn't have
|
|
level.set_block_data(40, 99, 1, { speed: 16 });
|
|
assertEquals(level.get_block_data(40, 99, 1), undefined);
|
|
});
|