// shared scripts' block rules run on clients too, so what a client shows the moment it places or breaks a block is // what the server ends up with import { assertEquals } from "@std/assert"; import { BlockRules } from "$/common/block_rules.ts"; import { collision_shape_at, covers_face } from "$/common/block_shapes.ts"; import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts"; import { tag_api } from "$/common/tags.ts"; import { AIR_ID } from "$/common/protocol.ts"; import { get_state_value, set_state_value } from "$/common/utils.ts"; import type { Face, SharedWorld } from "$/common/mod_api/shared.ts"; import { setup as setup_shared } from "$/mods/bworld/scripts/shared.ts"; import { test_game } from "./helpers.ts"; type Game = Awaited>; const AREA = { x: [-3, 3], y: [99, 101], z: [-3, 3] }; // every block in the area, as id and state bits function blocks_in(get: (x: number, y: number, z: number) => [string, number]) { const out: string[] = []; for (let x = AREA.x[0]; x <= AREA.x[1]; x++) { for (let y = AREA.y[0]; y <= AREA.y[1]; y++) { for (let z = AREA.z[0]; z <= AREA.z[1]; z++) out.push(`${x} ${y} ${z} ${get(x, y, z).join(" ")}`); } } return out; } function state(t: Game, x: number, y: number, z: number, name: string) { return get_state_value(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!, name); } function server_blocks(t: Game) { return blocks_in((x, y, z) => [t.game.world.get_block_id(x, y, z), t.game.world.get_block_value(x, y, z) >>> 16]); } // a client: its copy of the area, and the bworld shared script's rules running on it the way client/mods.ts does function client_of(t: Game) { const copy = new Map(); for (const line of server_blocks(t)) { const [x, y, z, id, state] = line.split(" "); copy.set(`${x} ${y} ${z}`, [id, Number(state)]); } const at = (x: number, y: number, z: number) => copy.get(`${x} ${y} ${z}`); const info = (id: string) => EverythingRegistry.get("blocks", id); const rules = new BlockRules((_mod, _what, fn) => fn()); const world: SharedWorld = { get_block: (x, y, z) => at(x, y, z)?.[0], get_state(x, y, z, name) { const [id, state] = at(x, y, z) ?? [AIR_ID, 0]; const block = info(id); return block?.states?.some((s) => s.name === name) ? get_state_value(state << 16, block, name) : undefined; }, set_state(x, y, z, name, value) { const [id, state] = at(x, y, z) ?? [AIR_ID, 0]; const block = info(id); if (!block?.states?.some((s) => s.name === name)) return false; const changed = set_state_value(state << 16, block, name, value)! >>> 16; if (changed !== state) { copy.set(`${x} ${y} ${z}`, [id, changed]); rules.notify_neighbors(world.get_block, x, y, z); } return true; }, is_face_sturdy(x, y, z, face: Face) { const [id, state] = at(x, y, z) ?? [AIR_ID, 0]; const block = info(id); return block !== undefined && covers_face(collision_shape_at(block, state << 16), face); }, }; setup_shared({ mod: { id: "bworld", version: "test" }, side: "client", tags: tag_api(), world, components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) }, }); return { // what game_mode does when this client places held against face of x y z, at hit place(id: string, x: number, y: number, z: number, yaw: number, face: Face, hit: [number, number, number]) { const value = rules.placement_value(info(id)!, yaw, { x, y, z, face, hit }); copy.set(`${x} ${y} ${z}`, [id, value >>> 16]); rules.notify_neighbors(world.get_block, x, y, z); }, break(x: number, y: number, z: number) { copy.set(`${x} ${y} ${z}`, [AIR_ID, 0]); rules.notify_neighbors(world.get_block, x, y, z); }, blocks: () => blocks_in((x, y, z) => at(x, y, z)!), }; } // alice on a stone floor at y 99, south of 0 100 0, holding id async function setup(id: string, yaw = 0) { const t = await test_game("mods"); t.join(1, "alice"); for (let x = AREA.x[0]; x <= AREA.x[1]; x++) { for (let z = AREA.z[0]; z <= AREA.z[1]; z++) { t.game.set_block(x, 99, z, "bworld:stone"); for (let y = 100; y <= AREA.y[1]; y++) t.game.set_block(x, y, z, AIR_ID); } } t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw, pitch: 0 }); t.send(1, { type: "chat", text: `/give ${id} 64` }); t.send(1, { type: "select_slot", slot: 0 }); const use = (x: number, y: number, z: number, face: Face, hit: [number, number, number]) => t.send(1, { type: "use_block", x, y, z, face, hit }); return { t, use }; } Deno.test("a client places a fence joined to the fences and walls beside it, the same as the server", async () => { const { t, use } = await setup("bworld:oak_fence"); t.game.set_block(-1, 100, 0, "bworld:oak_fence"); t.game.set_block(0, 100, -1, "bworld:stone"); t.game.set_block(1, 100, 0, "bworld:oak_leaves"); const client = client_of(t); client.place("bworld:oak_fence", 0, 100, 0, 0, "top", [0.5, 1, 0.5]); use(0, 99, 0, "top", [0.5, 1, 0.5]); assertEquals(client.blocks(), server_blocks(t)); // it joined the fence and the stone, and the fence joined it back assertEquals(state(t, 0, 100, 0, "west"), 1); assertEquals(state(t, 0, 100, 0, "north"), 1); assertEquals(state(t, -1, 100, 0, "east"), 1); // breaking it lets go of the one beside it, on both client.break(0, 100, 0); t.send(1, { type: "break_block", x: 0, y: 100, z: 0 }); assertEquals(client.blocks(), server_blocks(t)); assertEquals(state(t, -1, 100, 0, "east"), 0); }); Deno.test("a client places a trapdoor turned and in the half the server puts it", async () => { const { t, use } = await setup("bworld:oak_trapdoor", Math.PI / 2); t.game.set_block(0, 100, 0, "bworld:stone"); const client = client_of(t); // on the upper half of the stone's east side, and on top of the floor facing alice, who looks west client.place("bworld:oak_trapdoor", 1, 100, 0, Math.PI / 2, "east", [1, 0.8, 0.5]); use(0, 100, 0, "east", [1, 0.8, 0.5]); client.place("bworld:oak_trapdoor", 0, 100, 2, Math.PI / 2, "top", [0.5, 1, 0.5]); use(0, 99, 2, "top", [0.5, 1, 0.5]); assertEquals(client.blocks(), server_blocks(t)); assertEquals(state(t, 1, 100, 0, "half"), 1); assertEquals(state(t, 0, 100, 2, "facing"), 1); });