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
+6 -3
View File
@@ -31,14 +31,15 @@ export async function builder(save?: WorldSave) {
}
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
const player = t.game.players()[0];
// alice uses id, or whatever she's holding, on face of the block at x y z, from right by it
const use = (id: string | undefined, x: number, y: number, z: number, face: Face) => {
// alice uses id, or whatever she's holding, on face of the block at x y z, standing on top of it or at stand, out
// of the way of a block she can bump into
const use = (id: string | undefined, x: number, y: number, z: number, face: Face, stand: Pos = [x, y + 1, z]) => {
if (id) {
t.send(1, { type: "chat", text: `/give ${id}` });
const slot = player.inventory.to_synced_data().findIndex((item) => item?.id === id);
t.send(1, { type: "select_slot", slot });
}
t.game.teleport(player, x + 0.5, y + 1, z + 0.5);
t.game.teleport(player, stand[0] + 0.5, stand[1], stand[2] + 0.5);
t.send(1, { type: "use_block", x, y, z, face, hit: [0.5, 0.5, 0.5] });
};
const states = (x: number, y: number, z: number) =>
@@ -51,6 +52,8 @@ export async function builder(save?: WorldSave) {
get_state: (x, y, z, name) => states(x, y, z)[name],
source: (block) =>
EverythingRegistry.get<BlockRegistry>("blocks", block)?.components?.["bworld:verdigris_source"] as never,
input: (block) =>
EverythingRegistry.get<BlockRegistry>("blocks", block)?.components?.["bworld:verdigris_input"] as never,
};
const covers = (...at: Pos) => FACES.filter((face) => states(...at)[face] === 1);
// the wire cells the wire at x y z joins
+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);
});