140 lines
5.4 KiB
TypeScript
140 lines
5.4 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the server's side of a block hearing about changes: its components' on_neighbor_change, after the shared rules, and
|
|
// on_state_change when its own states change
|
|
import { assertEquals } from "@std/assert";
|
|
import { AIR_ID } from "$/common/protocol.ts";
|
|
import { copy_dir, stone_floor, test_game } from "./helpers.ts";
|
|
|
|
const SERVER_SCRIPT = `
|
|
import type { ServerContext } from "bworld/server";
|
|
export function setup(ctx: ServerContext) {
|
|
const log = (event: unknown[]) => ctx.storage.set("log", [...ctx.storage.get<unknown[]>("log") ?? [], event]);
|
|
ctx.components.register_block("testmod:watcher", {
|
|
on_neighbor_change(block, _params, neighbor) {
|
|
const seen = ctx.world.get_state(block.x, block.y, block.z, "seen");
|
|
log(["neighbor", neighbor.id, neighbor.x, neighbor.y, neighbor.z, seen]);
|
|
},
|
|
on_state_change(block, _params, before) {
|
|
const { x, y, z } = block;
|
|
log(["state", before, { seen: ctx.world.get_state(x, y, z, "seen"), count: ctx.world.get_state(x, y, z, "count") }]);
|
|
},
|
|
});
|
|
// hangs from the block above and falls when it's gone, which only a server handler can do
|
|
ctx.components.register_block("testmod:lantern", {
|
|
on_neighbor_change(block) {
|
|
if (ctx.world.get_block(block.x, block.y + 1, block.z) === "bworld:air") {
|
|
ctx.world.break_block(block.x, block.y, block.z);
|
|
}
|
|
},
|
|
});
|
|
ctx.commands.register("state", {
|
|
description: "", usage: "",
|
|
run([x, y, z, name, value]) { ctx.world.set_state(Number(x), Number(y), Number(z), name, Number(value)); },
|
|
});
|
|
}
|
|
`;
|
|
|
|
// marks the watcher once anything beside it changes
|
|
const SHARED_SCRIPT = `
|
|
import type { SharedContext } from "bworld/shared";
|
|
export function setup(ctx: SharedContext) {
|
|
ctx.components.register_block("testmod:watcher", {
|
|
on_neighbor_change(block) { ctx.world.set_state(block.x, block.y, block.z, "seen", 1); },
|
|
});
|
|
}
|
|
`;
|
|
|
|
function test_mods() {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_neighbors_" });
|
|
copy_dir("mods/bworld", `${dir}/bworld`);
|
|
const mod = `${dir}/testmod`;
|
|
for (const folder of ["blocks", "scripts"]) Deno.mkdirSync(`${mod}/${folder}`, { recursive: true });
|
|
const write = (path: string, json: unknown) => Deno.writeTextFileSync(`${mod}/${path}`, JSON.stringify(json));
|
|
write("manifest.json", {
|
|
format_version: 1,
|
|
id: "testmod",
|
|
name: "Test",
|
|
version: "1.0.0",
|
|
dependencies: [{ id: "bworld", version: "*" }],
|
|
scripts: { server: "scripts/server.ts", shared: "scripts/shared.ts" },
|
|
});
|
|
const block = (id: string, extra: Record<string, unknown>) => ({
|
|
format_version: 1,
|
|
block: { id, textures: "bworld:stone", mining: { toughness: 1 }, ...extra },
|
|
});
|
|
write(
|
|
"blocks/watcher.json",
|
|
block("testmod:watcher", {
|
|
states: [{ name: "seen", bits: 1, default: 0 }, { name: "count", bits: 4, default: 0 }],
|
|
components: { "testmod:watcher": {} },
|
|
}),
|
|
);
|
|
write("blocks/lantern.json", block("testmod:lantern", { components: { "testmod:lantern": {} } }));
|
|
Deno.writeTextFileSync(`${mod}/scripts/server.ts`, SERVER_SCRIPT);
|
|
Deno.writeTextFileSync(`${mod}/scripts/shared.ts`, SHARED_SCRIPT);
|
|
return dir;
|
|
}
|
|
|
|
async function setup() {
|
|
const dir = test_mods();
|
|
const { game, send, join } = await test_game(dir);
|
|
join(1, "alice");
|
|
stone_floor(game);
|
|
send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 });
|
|
// what happened since it was last called
|
|
const log = () => {
|
|
const events = (game.mods.storage.testmod?.log ?? []) as unknown[];
|
|
delete game.mods.storage.testmod?.log;
|
|
return events;
|
|
};
|
|
const command = (text: string) => send(1, { type: "chat", text });
|
|
const cleanup = () => Deno.removeSync(dir, { recursive: true });
|
|
return { game, log, command, cleanup };
|
|
}
|
|
|
|
Deno.test("a component's on_neighbor_change runs after the block's rules, and can change the world", async () => {
|
|
const { game, log, cleanup } = await setup();
|
|
game.set_block(0, 100, 0, "testmod:watcher");
|
|
log();
|
|
|
|
game.set_block(1, 100, 0, "bworld:stone");
|
|
assertEquals(log(), [
|
|
["state", { seen: 0, count: 0 }, { seen: 1, count: 0 }],
|
|
// the rule had marked it already
|
|
["neighbor", "bworld:stone", 1, 100, 0, 1],
|
|
]);
|
|
game.set_block(1, 100, 0, AIR_ID);
|
|
assertEquals(log(), [["neighbor", AIR_ID, 1, 100, 0, 1]], "the block that changed is what's there now");
|
|
|
|
game.set_block(3, 101, 0, "bworld:stone");
|
|
game.set_block(3, 100, 0, "testmod:lantern");
|
|
game.set_block(4, 100, 0, "bworld:stone");
|
|
assertEquals(game.world.get_block_id(3, 100, 0), "testmod:lantern");
|
|
game.set_block(3, 101, 0, AIR_ID);
|
|
assertEquals(game.world.get_block_id(3, 100, 0), AIR_ID, "it fell once what it hung from was gone");
|
|
cleanup();
|
|
});
|
|
|
|
Deno.test("on_state_change gets the states from before, when a script or a rule changes them", async () => {
|
|
const { game, log, command, cleanup } = await setup();
|
|
game.set_block(0, 100, 0, "testmod:watcher");
|
|
log();
|
|
|
|
command("/state 0 100 0 count 3");
|
|
assertEquals(log(), [["state", { seen: 0, count: 0 }, { seen: 0, count: 3 }]]);
|
|
command("/state 0 100 0 count 3");
|
|
assertEquals(log(), [], "setting a state to what it is changes nothing");
|
|
|
|
// a rule
|
|
game.set_block(0, 101, 0, "bworld:stone");
|
|
assertEquals(log()[0], ["state", { seen: 0, count: 3 }, { seen: 1, count: 3 }]);
|
|
|
|
// replacing it is a new block, not a state change
|
|
game.set_block(0, 100, 0, "testmod:watcher");
|
|
assertEquals(log(), []);
|
|
assertEquals(game.world.get_block_value(0, 100, 0) >>> 16, 0);
|
|
cleanup();
|
|
});
|