// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later import { assert, assertEquals } from "@std/assert"; import { Container, ItemStack } from "@/common/inventory.ts"; import { CRAFTING_RESULT_SLOT } from "@/common/protocol.ts"; import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts"; import { bake_block, block_item_texture, type ModelJson } from "@/common/block_models.ts"; import { collision_shape_at, covers_face, FULL_SHAPE, shape_at } from "@/common/block_shapes.ts"; import { move_body } from "@/common/physics.ts"; import { get_state_value } from "@/common/utils.ts"; import { update_crafting_result } from "@/server/game/crafting.ts"; import { stone_floor, test_game } from "./helpers.ts"; const Y = 100; // a game with the blocks around x 0..4, z 0 at Y cleared, and what's connected where async function setup() { const { game } = await test_game("mods"); for (let x = -1; x <= 5; x++) { for (let z = -1; z <= 1; z++) game.set_block(x, Y, z, "cubi:air"); } const sides = (x: number, z: number) => { const info = game.world.get_block_info(x, Y, z)!; const value = game.world.get_block_value(x, Y, z); return ["north", "east", "south", "west"].filter((side) => get_state_value(value, info, side) === 1); }; return { game, sides }; } Deno.test("fences join the fences beside them, and let go when one is broken", async () => { const { game, sides } = await setup(); game.set_block(0, Y, 0, "bworld:oak_fence"); assertEquals(sides(0, 0), []); game.set_block(1, Y, 0, "bworld:birch_fence"); game.set_block(2, Y, 0, "bworld:oak_fence"); assertEquals(sides(0, 0), ["east"]); assertEquals(sides(1, 0), ["east", "west"]); assertEquals(sides(2, 0), ["west"]); game.set_block(1, Y, 0, "cubi:air"); assertEquals(sides(0, 0), []); assertEquals(sides(2, 0), []); }); Deno.test("fences join full sides of solid blocks, but not leaves, plants or a slab's half", async () => { const { game, sides } = await setup(); game.set_block(0, Y, 0, "bworld:oak_fence"); game.set_block(0, Y, -1, "bworld:stone"); game.set_block(1, Y, 0, "bworld:oak_leaves"); game.set_block(-1, Y, 0, "bworld:wheat"); assertEquals(sides(0, 0), ["north"]); assert(!covers_face([[0, 0, 0, 1, 0.5, 1]], "north")); assert(covers_face(FULL_SHAPE, "north")); }); Deno.test("a fence is drawn as a post and a bar for each side it joins, and its item is the fence model", async () => { const { game } = await setup(); const fence = EverythingRegistry.get("blocks", "bworld:oak_fence")!; const models = Object.fromEntries(EverythingRegistry.entries("models")); game.set_block(0, Y, 0, "bworld:oak_fence"); const alone = bake_block(fence, game.world.get_block_value(0, Y, 0), models); game.set_block(1, Y, 0, "bworld:oak_fence"); const joined = bake_block(fence, game.world.get_block_value(0, Y, 0), models); // a post is 6 faces, a side is two bars of 5 assertEquals(alone.length, 6); assertEquals(joined.length, 16); // the bars on the east side, turned from the side model's north assert(joined.slice(6).every((quad) => quad.positions.every((v, i) => i % 3 !== 0 || v >= 0.4375))); assert(joined.every((quad) => quad.texture.startsWith("bworld:oak_fence_"))); assertEquals(block_item_texture(fence), undefined); }); Deno.test("fences are pointed at by their post and bars, and are too tall to jump over", async () => { const { game } = await setup(); game.set_block(0, Y, 0, "bworld:oak_fence"); game.set_block(1, Y, 0, "bworld:oak_fence"); const info = game.world.get_block_info(0, Y, 0)!; const value = game.world.get_block_value(0, Y, 0); assertEquals(shape_at(info, value).map((box) => box[4]), [1, 1]); assertEquals(collision_shape_at(info, value).map((box) => box[4]), [1.5, 1.5]); // a fence on its own is just its post assertEquals(collision_shape_at(info, 0), [[6 / 16, 0, 6 / 16, 10 / 16, 1.5, 10 / 16]]); // standing on top of one, a block and a half up const body = { x: 0.5, y: Y + 3, z: 0.5, vx: 0, vy: 0, vz: 0, width: 0.6, height: 1.8 }; for (let i = 0; i < 40; i++) move_body(body, -32, (x, y, z) => game.world.collision_shape(x, y, z)); assertEquals(body.y, Y + 1.5); // walking into its side stops at the post, even a block above the fence's bottom const walker = { x: 0.5, y: Y + 1, z: 2, vx: 0, vy: 0, vz: -4, width: 0.6, height: 1.8 }; for (let i = 0; i < 20; i++) { walker.vz = -4; move_body(walker, 0, (x, y, z) => game.world.collision_shape(x, y, z)); } assertEquals(walker.z, 10 / 16 + 0.3); }); Deno.test("six planks and two sticks make three fences of that wood", async () => { const { game } = await test_game("mods"); const grid = new Container(10); for (const slot of [0, 2, 3, 5]) grid.set_item(slot, new ItemStack("bworld:spruce_planks", 1)); for (const slot of [1, 4]) grid.set_item(slot, new ItemStack("bworld:stick", 1)); update_crafting_result(grid, game.recipes.shaped); const result = grid.get_item(CRAFTING_RESULT_SLOT); assertEquals([result?.type_id, result?.amount], ["bworld:spruce_fence", 3]); }); Deno.test("clicking the ground beside a fence tells the client it's still joined, not a bare post", async () => { const t = await test_game("mods"); t.join(1, "alice"); stone_floor(t.game); t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 }); t.game.tick(); t.game.set_block(0, 100, 0, "bworld:oak_fence"); t.game.set_block(1, 100, 0, "bworld:oak_fence"); const joined = t.game.world.get_block_value(0, 100, 0) >>> 16; assert(joined !== 0); t.take(1); // a fence is thin, so the ground's top beside its post can be clicked. what would go there is the fence's spot, // and the server puts back what's really there t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top" }); const corrections = t.take(1).filter((m) => m.type === "set_block" && m.x === 0 && m.y === 100 && m.z === 0); assertEquals(corrections.map((m) => [m.id, m.state]), [["bworld:oak_fence", joined]]); });