Files
bworld/tests/trapdoor_test.ts
T
2026-11-03 17:17:37 -03:00

105 lines
5.3 KiB
TypeScript

// 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, type ModelJson } from "$/common/block_models.ts";
import { collision_shape_at, shape_at } from "$/common/block_shapes.ts";
import { get_state_value } from "$/common/utils.ts";
import { update_crafting_result } from "$/server/game/crafting.ts";
import { test_game } from "./helpers.ts";
// alice on a stone floor at y 100, south of 0 100 0 and looking north, with oak trapdoors in hand
async function setup() {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) {
t.game.set_block(x, 99, z, "bworld:stone");
for (let y = 100; y < 103; y++) t.game.set_block(x, y, z, "cubi:air");
}
}
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 });
t.send(1, { type: "chat", text: "/give bworld:oak_trapdoor 64" });
t.send(1, { type: "select_slot", slot: 0 });
const states = (x: number, y: number, z: number) => {
const info = t.game.world.get_block_info(x, y, z)!;
const value = t.game.world.get_block_value(x, y, z);
return Object.fromEntries(["facing", "half", "open"].map((name) => [name, get_state_value(value, info, name)]));
};
return { t, states };
}
Deno.test("on top of a block a trapdoor lies in the bottom half, swinging open away from the player", async () => {
const { t, states } = await setup();
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:oak_trapdoor");
// alice looks north, so it faces her, south
assertEquals(states(0, 100, 0), { facing: 2, half: 0, open: 0 });
});
Deno.test("against the side of a block a trapdoor hangs from it, in the half that was clicked", async () => {
const { t, states } = await setup();
t.game.set_block(0, 100, 0, "bworld:stone");
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.8, 1] });
assertEquals(states(0, 100, 1), { facing: 2, half: 1, open: 0 });
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "east", hit: [1, 0.2, 0.5] });
assertEquals(states(1, 100, 0), { facing: 1, half: 0, open: 0 });
// under a block, in the top half
t.game.set_block(-2, 102, 0, "bworld:stone");
t.send(1, { type: "use_block", x: -2, y: 102, z: 0, face: "bottom", hit: [0.5, 0, 0.5] });
assertEquals(states(-2, 101, 0).half, 1);
});
Deno.test("right clicking a trapdoor opens and closes it, and what it's shaped like goes with it", async () => {
const { t, states } = await setup();
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
const info = t.game.world.get_block_info(0, 100, 0)!;
const shape = () => shape_at(info, t.game.world.get_block_value(0, 100, 0));
assertEquals(shape(), [[0, 0, 0, 1, 3 / 16, 1]]);
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "top", hit: [0.5, 3 / 16, 0.5] });
assertEquals(states(0, 100, 0).open, 1);
// facing south it swung up against the north side, where its hinge is
assertEquals(shape(), [[0, 0, 0, 1, 1, 3 / 16]]);
assertEquals(collision_shape_at(info, t.game.world.get_block_value(0, 100, 0)), shape());
// still one trapdoor in the world, none placed on top of it
assertEquals(t.game.world.get_block_id(0, 101, 0), "cubi:air");
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 3 / 16] });
assertEquals(states(0, 100, 0).open, 0);
});
Deno.test("trapdoors are drawn with their wood's texture, open ones as a panel standing on the hinge side", async () => {
const { t } = await setup();
const trapdoor = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:birch_trapdoor")!;
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
t.game.set_block(0, 100, 0, "bworld:birch_trapdoor");
for (const [open, half] of [[0, 0], [0, 1], [1, 0], [1, 1]]) {
// facing is the first 2 state bits, then half, then open
const value = (t.game.world.get_block_value(0, 100, 0) & 0xffff) | ((open << 3 | half << 2) << 16);
const quads = bake_block(trapdoor, value, models);
assertEquals(quads.length, 6);
assert(quads.every((quad) => quad.texture === "bworld:birch_trapdoor"));
const ys = quads.flatMap((quad) => quad.positions.filter((_, i) => i % 3 === 1));
const zs = quads.flatMap((quad) => quad.positions.filter((_, i) => i % 3 === 2));
if (open) {
// facing north, so against the south side, from the bottom to the top
assertEquals([Math.min(...zs), Math.max(...ys)], [13 / 16, 1]);
} else {
assertEquals([Math.min(...ys), Math.max(...ys)], half ? [13 / 16, 1] : [0, 3 / 16]);
}
}
});
Deno.test("six planks make two trapdoors of that wood", async () => {
const { game } = await test_game("mods");
const grid = new Container(10);
for (let slot = 0; slot < 6; slot++) grid.set_item(slot, new ItemStack("bworld:jungle_planks", 1));
update_crafting_result(grid, game.recipes.shaped);
const result = grid.get_item(CRAFTING_RESULT_SLOT);
assertEquals([result?.type_id, result?.amount], ["bworld:jungle_trapdoor", 2]);
});