Doors working with verdigris

This commit is contained in:
2026-10-22 17:02:14 -03:00
parent 20e6ac6fbb
commit 1b30d71a47
22 changed files with 317 additions and 47 deletions
+88
View File
@@ -9,6 +9,7 @@ import { type BlockRegistry, EverythingRegistry } from "$/common/everything_regi
import type { Face } from "$/common/mod_api/shared.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { default_block_value, set_state_value } from "$/common/utils.ts";
import { ways_out } from "$/mods/bworld/scripts/verdigris/joins.ts";
import { FACE_OFFSETS, FACES, OPPOSITE, step } from "$/mods/bworld/scripts/verdigris/sides.ts";
import { client_of, server_blocks, test_game } from "./helpers.ts";
import { AREA, type Builder, builder, type Pos } from "./verdigris.ts";
@@ -170,3 +171,90 @@ Deno.test("a torch goes on a floor or a wall but not under a ceiling, and breaks
const dropped = b.t.game.item_entities().filter((e) => e.item.type_id === TORCH).length;
assertEquals(dropped, 2);
});
const DOOR = "bworld:oak_door";
const TRAPDOOR = "bworld:oak_trapdoor";
const open = (b: Builder, at: Pos) => b.states(...at).open === 1;
// an oak door at x 100 z, with its upper half above, placed by alice from the south
function door(b: Builder, x: number, z: number) {
b.use(DOOR, x, 99, z, "top", [x, 100, z + 2]);
assertEquals(b.id(x, 101, z), DOOR);
const halves: Pos[] = [[x, 100, z], [x, 101, z]];
return () => halves.map((at) => open(b, at));
}
Deno.test("a door opens while either of its halves is powered, both halves together", async () => {
const b = await builder();
const door_open = door(b, 0, 0);
// a lever on the floor beside the lower half
b.use("bworld:lever", 1, 99, 0, "top");
b.use(undefined, 1, 100, 0, "top");
assertEquals(door_open(), [true, true]);
b.use(undefined, 1, 100, 0, "top");
assertEquals(door_open(), [false, false]);
// a verdigris block beside the upper half
b.t.game.set_block(-1, 101, 0, "bworld:verdigris_block");
assertEquals(door_open(), [true, true]);
// and the lever, which keeps it open once the block's gone
b.use(undefined, 1, 100, 0, "top");
b.t.game.set_block(-1, 101, 0, AIR_ID);
assertEquals(door_open(), [true, true]);
b.use(undefined, 1, 100, 0, "top");
assertEquals(door_open(), [false, false]);
// placed with only its upper half beside power, it opens
b.t.game.set_block(4, 101, 0, "bworld:verdigris_block");
assertEquals(door(b, 3, 0)(), [true, true]);
});
Deno.test("a door opened or closed by hand stays that way until its power changes", async () => {
const b = await builder();
const door_open = door(b, 0, 0);
b.use("bworld:lever", 1, 99, 0, "top");
const switch_lever = () => b.use(undefined, 1, 100, 0, "top");
const click_door = () => b.use(undefined, 0, 100, 0, "south", [0, 100, 2]);
click_door();
switch_lever();
assertEquals(door_open(), [true, true]);
click_door();
assertEquals(door_open(), [false, false]);
// powering the other half too doesn't change whether it's powered
b.t.game.set_block(-1, 101, 0, "bworld:verdigris_block");
b.t.game.set_block(-1, 101, 0, AIR_ID);
assertEquals(door_open(), [false, false]);
switch_lever();
assertEquals(door_open(), [false, false]);
switch_lever();
assertEquals(door_open(), [true, true]);
});
Deno.test("wire beside a door joins it and opens it", async () => {
const b = await builder();
const door_open = door(b, 0, 0);
// from the door's lower half east along the floor to a lever
b.use("bworld:verdigris", 1, 99, 0, "top");
b.use("bworld:verdigris", 2, 99, 0, "top");
b.use("bworld:lever", 3, 99, 0, "top");
const west = ways_out(["bottom"]).findIndex(([, direction]) => direction === "west");
assertEquals(b.states(1, 100, 0)[`arm_${west}`], 1);
b.use(undefined, 3, 100, 0, "top");
assertEquals(door_open(), [true, true]);
b.use(undefined, 3, 100, 0, "top");
assertEquals(door_open(), [false, false]);
});
Deno.test("a trapdoor opens while it's powered, and opens straight away when it's placed beside power", async () => {
const b = await builder();
b.use(TRAPDOOR, 0, 99, 0, "top", [0, 100, 2]);
const trapdoor: Pos = [0, 100, 0];
assertEquals(b.id(...trapdoor), TRAPDOOR);
b.t.game.set_block(1, 100, 0, "bworld:verdigris_block");
assertEquals(open(b, trapdoor), true);
b.t.game.set_block(1, 100, 0, AIR_ID);
assertEquals(open(b, trapdoor), false);
b.t.game.set_block(4, 100, 0, "bworld:verdigris_block");
b.use(TRAPDOOR, 3, 99, 0, "top", [3, 100, 2]);
assertEquals(open(b, [3, 100, 0]), true);
});