Files
bworld/tests/door_test.ts
T
2026-10-11 19:48:08 -03:00

149 lines
8.0 KiB
TypeScript

// doors: two blocks tall, facing the way the player looks, opened from either half, broken as one
import { assertEquals } from "@std/assert";
import { Container, ItemStack } from "$/common/inventory.ts";
import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts";
import { shape_at } from "$/common/block_shapes.ts";
import { bake_block, type ModelJson } from "$/common/block_models.ts";
import { type BlockRegistry, EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.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 doors 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, "bworld: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_door 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", "hinge"].map((name) => [name, get_state_value(value, info, name)]),
);
};
const held = () => t.game.players()[0].held_item?.amount;
const dropped = () => t.game.item_entities().map((e) => `${e.item.type_id} ${e.item.amount}`);
return { t, states, held, dropped };
}
Deno.test("a door is placed with its upper half above it, facing the way the player looks", async () => {
const { t, states, held } = await setup();
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.2, 1, 0.5] });
assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:oak_door");
assertEquals(t.game.world.get_block_id(0, 101, 0), "bworld:oak_door");
// alice looks north, and clicked the west half, which is on her left
assertEquals(states(0, 100, 0), { facing: 0, half: 0, open: 0, hinge: 0 });
assertEquals(states(0, 101, 0), { facing: 0, half: 1, open: 0, hinge: 0 });
assertEquals(held(), 63);
// clicking the east half hinges it on the right
t.send(1, { type: "use_block", x: 2, y: 99, z: 0, face: "top", hit: [0.8, 1, 0.5] });
assertEquals(states(2, 100, 0).hinge, 1);
assertEquals(states(2, 101, 0).hinge, 1);
});
Deno.test("a door isn't placed without room for its upper half", async () => {
const { t, held } = await setup();
t.game.set_block(0, 101, 0, "bworld:stone");
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:air");
assertEquals(held(), 64);
});
Deno.test("right clicking either half opens and closes both, and the door turns on its hinge", async () => {
const { t, states } = await setup();
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.2, 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));
// closed, across the south side, where alice is
assertEquals(shape(), [[0, 0, 13 / 16, 1, 1, 1]]);
t.send(1, { type: "use_block", x: 0, y: 101, z: 0, face: "south", hit: [0.5, 0.5, 1] });
assertEquals([states(0, 100, 0).open, states(0, 101, 0).open], [1, 1]);
// swung against the west side, its hinge on alice's left
assertEquals(shape(), [[0, 0, 0, 3 / 16, 1, 1]]);
// nothing placed in the doorway
assertEquals(t.game.world.get_block_id(0, 102, 0), "bworld:air");
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "west", hit: [0, 0.5, 0.5] });
assertEquals([states(0, 100, 0).open, states(0, 101, 0).open], [0, 0]);
});
Deno.test("breaking either half breaks the whole door, which drops one door", async () => {
const { t, dropped } = await setup();
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
t.send(1, { type: "break_block", x: 0, y: 101, z: 0 });
assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:air");
assertEquals(t.game.world.get_block_id(0, 101, 0), "bworld:air");
assertEquals(dropped(), ["bworld:oak_door 1"]);
t.send(1, { type: "use_block", x: 1, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
t.send(1, { type: "break_block", x: 1, y: 100, z: 0 });
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air");
assertEquals(dropped(), ["bworld:oak_door 1", "bworld:oak_door 1"]);
});
Deno.test("a door beside another is hinged away from it, whichever half was clicked, making a double door", async () => {
const { t, states } = await setup();
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.2, 1, 0.5] });
assertEquals(states(0, 100, 0).hinge, 0);
// alice looks north, so the first door is on its left, and it's hinged on the right
t.send(1, { type: "use_block", x: 1, y: 99, z: 0, face: "top", hit: [0.2, 1, 0.5] });
assertEquals(states(1, 100, 0).hinge, 1);
assertEquals(states(1, 101, 0).hinge, 1);
// and one with a door on its right is hinged on the left, a different wood too
t.send(1, { type: "chat", text: "/give bworld:birch_door 1" });
t.send(1, { type: "select_slot", slot: 1 });
t.send(1, { type: "use_block", x: -1, y: 99, z: 0, face: "top", hit: [0.8, 1, 0.5] });
assertEquals(t.game.world.get_block_id(-1, 100, 0), "bworld:birch_door");
assertEquals(states(-1, 100, 0).hinge, 0);
});
Deno.test("a double door opens and closes together, from either door and either half", async () => {
const { t, states } = await setup();
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.2, 1, 0.5] });
t.send(1, { type: "use_block", x: 1, y: 99, z: 0, face: "top", hit: [0.2, 1, 0.5] });
const open = () => [states(0, 100, 0).open, states(0, 101, 0).open, states(1, 100, 0).open, states(1, 101, 0).open];
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
assertEquals(open(), [1, 1, 1, 1]);
t.send(1, { type: "use_block", x: 1, y: 101, z: 0, face: "west", hit: [0, 0.5, 0.5] });
assertEquals(open(), [0, 0, 0, 0]);
// a door beside one but hinged the same way isn't its other half: a third door on the right is hinged on the right
// too, away from the second, and opens on its own
t.send(1, { type: "use_block", x: 2, y: 99, z: 0, face: "top", hit: [0.2, 1, 0.5] });
assertEquals(states(2, 100, 0).hinge, 1);
t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
assertEquals([...open(), states(2, 100, 0).open], [0, 0, 0, 0, 1]);
});
Deno.test("doors are drawn with their wood's bottom and top textures, and their item is flat", async () => {
const { t } = await setup();
const door = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:spruce_door")!;
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
t.game.set_block(0, 100, 0, "bworld:spruce_door");
const value = (half: number) => (t.game.world.get_block_value(0, 100, 0) & 0xffff) | (half << 2 << 16);
const textures = (half: number) => [...new Set(bake_block(door, value(half), models).map((q) => q.texture))].sort();
assertEquals(textures(0), ["bworld:spruce_door_bottom"]);
// the top of the upper half is the bottom texture's edge, like minecraft's
assertEquals(textures(1), ["bworld:spruce_door_bottom", "bworld:spruce_door_top"]);
assertEquals(EverythingRegistry.get<ItemRegistry>("items", "bworld:spruce_door")?.texture_id, "bworld:spruce_door");
});
Deno.test("six planks make three doors of that wood", async () => {
const { game } = await test_game("mods");
const grid = new Container(10);
for (const slot of [0, 1, 3, 4, 6, 7]) grid.set_item(slot, new ItemStack("bworld:cherry_planks", 1));
update_crafting_result(grid, game.recipes.shaped);
const result = grid.get_item(CRAFTING_RESULT_SLOT);
assertEquals([result?.type_id, result?.amount], ["bworld:cherry_door", 3]);
});