From 1b30d71a47f78287068f9fa4c6a1bef0ba5aef98 Mon Sep 17 00:00:00 2001 From: Paula Date: Thu, 22 Oct 2026 17:02:14 -0300 Subject: [PATCH] Doors working with verdigris --- mods/bworld/blocks/acacia_door.json | 8 ++- mods/bworld/blocks/acacia_trapdoor.json | 8 ++- mods/bworld/blocks/birch_door.json | 8 ++- mods/bworld/blocks/birch_trapdoor.json | 8 ++- mods/bworld/blocks/cherry_door.json | 8 ++- mods/bworld/blocks/cherry_trapdoor.json | 8 ++- mods/bworld/blocks/dark_oak_door.json | 8 ++- mods/bworld/blocks/dark_oak_trapdoor.json | 8 ++- mods/bworld/blocks/jungle_door.json | 8 ++- mods/bworld/blocks/jungle_trapdoor.json | 8 ++- mods/bworld/blocks/oak_door.json | 8 ++- mods/bworld/blocks/oak_trapdoor.json | 8 ++- mods/bworld/blocks/spruce_door.json | 8 ++- mods/bworld/blocks/spruce_trapdoor.json | 8 ++- mods/bworld/scripts/server.ts | 34 ++++++++- mods/bworld/scripts/verdigris/devices.ts | 3 +- mods/bworld/scripts/verdigris/inputs.ts | 35 +++++++++ mods/bworld/scripts/verdigris/server.ts | 29 ++++++-- mods/bworld/scripts/verdigris/shared.ts | 5 +- mods/bworld/scripts/verdigris/signals.ts | 49 +++++++------ tests/verdigris.ts | 9 ++- tests/verdigris_devices_test.ts | 88 +++++++++++++++++++++++ 22 files changed, 317 insertions(+), 47 deletions(-) create mode 100644 mods/bworld/scripts/verdigris/inputs.ts diff --git a/mods/bworld/blocks/acacia_door.json b/mods/bworld/blocks/acacia_door.json index 99ff353..f6e3739 100644 --- a/mods/bworld/blocks/acacia_door.json +++ b/mods/bworld/blocks/acacia_door.json @@ -30,6 +30,11 @@ "name": "hinge", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -92,7 +97,8 @@ "drops": "bworld:acacia_door", "interactive": true, "components": { - "bworld:door": { "pairs_with": "bworld:wooden_doors" } + "bworld:door": { "pairs_with": "bworld:wooden_doors" }, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/acacia_trapdoor.json b/mods/bworld/blocks/acacia_trapdoor.json index 66f62bb..14e0f1b 100644 --- a/mods/bworld/blocks/acacia_trapdoor.json +++ b/mods/bworld/blocks/acacia_trapdoor.json @@ -22,6 +22,11 @@ "name": "open", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -51,7 +56,8 @@ "drops": "bworld:acacia_trapdoor", "interactive": true, "components": { - "bworld:trapdoor": {} + "bworld:trapdoor": {}, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/birch_door.json b/mods/bworld/blocks/birch_door.json index 499569d..ae681cd 100644 --- a/mods/bworld/blocks/birch_door.json +++ b/mods/bworld/blocks/birch_door.json @@ -30,6 +30,11 @@ "name": "hinge", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -92,7 +97,8 @@ "drops": "bworld:birch_door", "interactive": true, "components": { - "bworld:door": { "pairs_with": "bworld:wooden_doors" } + "bworld:door": { "pairs_with": "bworld:wooden_doors" }, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/birch_trapdoor.json b/mods/bworld/blocks/birch_trapdoor.json index ee865ed..de74f64 100644 --- a/mods/bworld/blocks/birch_trapdoor.json +++ b/mods/bworld/blocks/birch_trapdoor.json @@ -22,6 +22,11 @@ "name": "open", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -51,7 +56,8 @@ "drops": "bworld:birch_trapdoor", "interactive": true, "components": { - "bworld:trapdoor": {} + "bworld:trapdoor": {}, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/cherry_door.json b/mods/bworld/blocks/cherry_door.json index 5dec4b2..3e66041 100644 --- a/mods/bworld/blocks/cherry_door.json +++ b/mods/bworld/blocks/cherry_door.json @@ -30,6 +30,11 @@ "name": "hinge", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -92,7 +97,8 @@ "drops": "bworld:cherry_door", "interactive": true, "components": { - "bworld:door": { "pairs_with": "bworld:wooden_doors" } + "bworld:door": { "pairs_with": "bworld:wooden_doors" }, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/cherry_trapdoor.json b/mods/bworld/blocks/cherry_trapdoor.json index bafafad..ece6151 100644 --- a/mods/bworld/blocks/cherry_trapdoor.json +++ b/mods/bworld/blocks/cherry_trapdoor.json @@ -22,6 +22,11 @@ "name": "open", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -51,7 +56,8 @@ "drops": "bworld:cherry_trapdoor", "interactive": true, "components": { - "bworld:trapdoor": {} + "bworld:trapdoor": {}, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/dark_oak_door.json b/mods/bworld/blocks/dark_oak_door.json index 5a07247..5006f99 100644 --- a/mods/bworld/blocks/dark_oak_door.json +++ b/mods/bworld/blocks/dark_oak_door.json @@ -30,6 +30,11 @@ "name": "hinge", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -92,7 +97,8 @@ "drops": "bworld:dark_oak_door", "interactive": true, "components": { - "bworld:door": { "pairs_with": "bworld:wooden_doors" } + "bworld:door": { "pairs_with": "bworld:wooden_doors" }, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/dark_oak_trapdoor.json b/mods/bworld/blocks/dark_oak_trapdoor.json index 2e3d2c6..4f4cded 100644 --- a/mods/bworld/blocks/dark_oak_trapdoor.json +++ b/mods/bworld/blocks/dark_oak_trapdoor.json @@ -22,6 +22,11 @@ "name": "open", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -51,7 +56,8 @@ "drops": "bworld:oak_trapdoor", "interactive": true, "components": { - "bworld:trapdoor": {} + "bworld:trapdoor": {}, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/jungle_door.json b/mods/bworld/blocks/jungle_door.json index ad4a3ec..c65b6ac 100644 --- a/mods/bworld/blocks/jungle_door.json +++ b/mods/bworld/blocks/jungle_door.json @@ -30,6 +30,11 @@ "name": "hinge", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -92,7 +97,8 @@ "drops": "bworld:jungle_door", "interactive": true, "components": { - "bworld:door": { "pairs_with": "bworld:wooden_doors" } + "bworld:door": { "pairs_with": "bworld:wooden_doors" }, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/jungle_trapdoor.json b/mods/bworld/blocks/jungle_trapdoor.json index 7ede00d..810e9b1 100644 --- a/mods/bworld/blocks/jungle_trapdoor.json +++ b/mods/bworld/blocks/jungle_trapdoor.json @@ -22,6 +22,11 @@ "name": "open", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -51,7 +56,8 @@ "drops": "bworld:jungle_trapdoor", "interactive": true, "components": { - "bworld:trapdoor": {} + "bworld:trapdoor": {}, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/oak_door.json b/mods/bworld/blocks/oak_door.json index d540cb5..bbc87ed 100644 --- a/mods/bworld/blocks/oak_door.json +++ b/mods/bworld/blocks/oak_door.json @@ -30,6 +30,11 @@ "name": "hinge", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -92,7 +97,8 @@ "drops": "bworld:oak_door", "interactive": true, "components": { - "bworld:door": { "pairs_with": "bworld:wooden_doors" } + "bworld:door": { "pairs_with": "bworld:wooden_doors" }, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/oak_trapdoor.json b/mods/bworld/blocks/oak_trapdoor.json index c242cb9..6faaece 100644 --- a/mods/bworld/blocks/oak_trapdoor.json +++ b/mods/bworld/blocks/oak_trapdoor.json @@ -22,6 +22,11 @@ "name": "open", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -51,7 +56,8 @@ "drops": "bworld:oak_trapdoor", "interactive": true, "components": { - "bworld:trapdoor": {} + "bworld:trapdoor": {}, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/spruce_door.json b/mods/bworld/blocks/spruce_door.json index c7f8867..10a529b 100644 --- a/mods/bworld/blocks/spruce_door.json +++ b/mods/bworld/blocks/spruce_door.json @@ -30,6 +30,11 @@ "name": "hinge", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -92,7 +97,8 @@ "drops": "bworld:spruce_door", "interactive": true, "components": { - "bworld:door": { "pairs_with": "bworld:wooden_doors" } + "bworld:door": { "pairs_with": "bworld:wooden_doors" }, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/blocks/spruce_trapdoor.json b/mods/bworld/blocks/spruce_trapdoor.json index ec0df23..cd1549d 100644 --- a/mods/bworld/blocks/spruce_trapdoor.json +++ b/mods/bworld/blocks/spruce_trapdoor.json @@ -22,6 +22,11 @@ "name": "open", "bits": 1, "default": 0 + }, + { + "name": "powered", + "bits": 1, + "default": 0 } ], "variants": { @@ -51,7 +56,8 @@ "drops": "bworld:spruce_trapdoor", "interactive": true, "components": { - "bworld:trapdoor": {} + "bworld:trapdoor": {}, + "bworld:verdigris_input": { "state": "powered" } } } } diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index b5d7835..7166630 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -22,6 +22,7 @@ export function setup(ctx: ServerContext) { register_farming(ctx); register_fishing(ctx); register_door(ctx); + register_trapdoor(ctx); register_buckets(ctx); register_curios(ctx); register_effects(ctx); @@ -882,7 +883,9 @@ function register_growable_block(ctx: ServerContext) { // doors are two blocks, a lower and an upper half, see register_door in shared.ts. one is only placed with air above // it, where the upper half goes, and breaking either half takes the other away with it. only the half that was broken -// drops the door, the other is just taken away +// drops the door, the other is just taken away. a door is open while either half is powered, which each keeps in its +// powered state (see bworld:verdigris_input). it only follows the power changing, so one opened or closed by hand +// stays that way until it does const DOOR = "bworld:door"; function register_door(ctx: ServerContext) { @@ -898,6 +901,11 @@ function register_door(ctx: ServerContext) { ctx.world.set_state(x, y + 1, z, state, ctx.world.get_state(x, y, z, state) ?? 0); } ctx.world.set_state(x, y + 1, z, "half", 1); + // it took its open state from the lower half, but it can be the half that's powered + if (ctx.world.get_state(x, y + 1, z, "powered")) { + ctx.world.set_state(x, y, z, "open", 1); + ctx.world.set_state(x, y + 1, z, "open", 1); + } }); // the halves being taken away, so the other half's on_break doesn't take the first away again @@ -916,5 +924,29 @@ function register_door(ctx: ServerContext) { for (const half of halves) breaking.delete(half); } }, + on_state_change(block, _params, before) { + const { x, y, z } = block; + const powered = ctx.world.get_state(x, y, z, "powered"); + if (powered === before.powered) return; + const other = ctx.world.get_state(x, y, z, "half") ? y - 1 : y + 1; + const paired = ctx.world.get_block(x, other, z) === block.id; + const other_powered = paired && ctx.world.get_state(x, other, z, "powered") === 1; + const open = powered === 1 || other_powered; + if (open === (before.powered === 1 || other_powered)) return; + ctx.world.set_state(x, y, z, "open", open ? 1 : 0); + if (paired) ctx.world.set_state(x, other, z, "open", open ? 1 : 0); + }, + }); +} + +// a trapdoor is open while it's powered, which it keeps in its powered state (see bworld:verdigris_input). it only +// follows the power changing, so one opened or closed by hand stays that way until it does. the rest of it is in +// shared.ts +function register_trapdoor(ctx: ServerContext) { + ctx.components.register_block("bworld:trapdoor", { + on_state_change(block, _params, before) { + const powered = ctx.world.get_state(block.x, block.y, block.z, "powered"); + if (powered !== before.powered) ctx.world.set_state(block.x, block.y, block.z, "open", powered ? 1 : 0); + }, }); } diff --git a/mods/bworld/scripts/verdigris/devices.ts b/mods/bworld/scripts/verdigris/devices.ts index 65aff64..912dfde 100644 --- a/mods/bworld/scripts/verdigris/devices.ts +++ b/mods/bworld/scripts/verdigris/devices.ts @@ -4,7 +4,8 @@ // the blocks that make and use signals, see "Blocks" in REDSTONE.md. what they give out is their bworld:verdigris_source // params, this is what makes their states change import type { BlockRef, Face, Position, ServerContext } from "bworld/server"; -import { power_from, type SignalReader } from "./signals.ts"; +import { power_from } from "./inputs.ts"; +import type { SignalReader } from "./signals.ts"; import { type FacingParam, front_of, OPPOSITE, pack, step } from "./sides.ts"; // a lever's face and facing states, see FacingParam diff --git a/mods/bworld/scripts/verdigris/inputs.ts b/mods/bworld/scripts/verdigris/inputs.ts new file mode 100644 index 0000000..7e6895e --- /dev/null +++ b/mods/bworld/scripts/verdigris/inputs.ts @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// the power blocks take in, see "How Power Works" in REDSTONE.md: a source's output, a solid block's power, and the +// power of wire that lies on them or joins them +import type { Face } from "bworld/shared"; +import { block_power, type InputParams, MAX_POWER, output, sides_of, type SignalReader, WIRE } from "./signals.ts"; +import { OPPOSITE, step } from "./sides.ts"; +import { wire_at } from "./wires.ts"; + +// the power going into the block at x y z from the one toward face: that one's output on that side, its power if it's +// solid, or its power if it's wire lying on this block or joining it. wire that only runs past gives none +export function power_from(world: SignalReader, x: number, y: number, z: number, face: Face) { + const [nx, ny, nz] = step(x, y, z, face); + const id = world.get_block(nx, ny, nz); + if (id === undefined) return 0; + if (id === WIRE) { + const wire = wire_at(world, nx, ny, nz); + const toward = OPPOSITE[face]; + const gives = wire !== undefined && + (wire.faces.includes(toward) || wire.out.some(([, direction]) => direction === toward)); + return gives ? world.get_state(nx, ny, nz, "power") ?? 0 : 0; + } + return Math.max(output(world, nx, ny, nz, id, OPPOSITE[face]), block_power(world, nx, ny, nz, id)); +} + +// the most power going into the input at x y z from the sides it takes power from +export function input_power(world: SignalReader, x: number, y: number, z: number, params: InputParams) { + let most = 0; + for (const face of sides_of(world, x, y, z, params, params.in)) { + most = Math.max(most, power_from(world, x, y, z, face)); + if (most === MAX_POWER) break; + } + return most; +} diff --git a/mods/bworld/scripts/verdigris/server.ts b/mods/bworld/scripts/verdigris/server.ts index fa4930a..d16f7f3 100644 --- a/mods/bworld/scripts/verdigris/server.ts +++ b/mods/bworld/scripts/verdigris/server.ts @@ -1,12 +1,20 @@ // SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later -// verdigris's components, see REDSTONE.md: the wire, which keeps its net's power through the net index, sources, and -// the devices +// verdigris's components, see REDSTONE.md: the wire, which keeps its net's power through the net index, sources, +// inputs, and the devices import type { BlockRef, Face, Position, ServerContext } from "bworld/server"; import { register_lever, register_torch } from "./devices.ts"; import { arms_of, around_edge, can_cover, covers, face_states, faces_of, MAX_ARMS } from "./joins.ts"; -import { type BlockReader, source_lookup, strong_sides } from "./signals.ts"; +import { input_power } from "./inputs.ts"; +import { + type BlockReader, + type InputParams, + params_lookup, + type SignalReader, + type SourceParams, + strong_sides, +} from "./signals.ts"; import { DIRECTIONS, FACES, OPPOSITE, step } from "./sides.ts"; import { NetIndex, type SignalWorld, tell_around_below, wire_at } from "./wires.ts"; @@ -62,10 +70,22 @@ export function register_verdigris(ctx: ServerContext) { }, }); + // keeps whether the block is powered in its state, for the block's own component to react to in on_state_change. + // setting it to what it is does nothing, so most changes around it cost a few reads + ctx.components.register_block("bworld:verdigris_input", { + on_create: (block, params) => set_input(ctx, world, block, params), + on_neighbor_change: (block, params) => set_input(ctx, world, block, params), + }); + register_lever(ctx); register_torch(ctx, world); } +function set_input(ctx: ServerContext, world: SignalReader, block: BlockRef, params: InputParams) { + const powered = input_power(world, block.x, block.y, block.z, params) > 0 ? 1 : 0; + ctx.world.set_state(block.x, block.y, block.z, params.state, powered); +} + // the wires on the next faces of the blocks a wire lies on, which it joins or could join around their edges. they aren't // among the six blocks beside it the engine tells function tell_around_edges(world: SignalWorld, block: BlockRef) { @@ -119,7 +139,8 @@ function signal_world(ctx: ServerContext): SignalWorld { // a chunk that was never generated holds nothing anyone built, and reading it would generate it get_block: (x, y, z) => ctx.world.is_loaded(x, z) ? ctx.world.get_block(x, y, z) : undefined, get_state: (x, y, z, name) => ctx.world.get_state(x, y, z, name), - source: source_lookup(ctx.blocks), + source: params_lookup(ctx.blocks, "bworld:verdigris_source"), + input: params_lookup(ctx.blocks, "bworld:verdigris_input"), solid: (id) => ctx.blocks.solid(id), set_power: (x, y, z, power) => ctx.world.set_state(x, y, z, "power", power, { update_neighbors: false }), neighbor_changed: (x, y, z, from) => ctx.world.neighbor_changed(x, y, z, from), diff --git a/mods/bworld/scripts/verdigris/shared.ts b/mods/bworld/scripts/verdigris/shared.ts index 4825621..91e008f 100644 --- a/mods/bworld/scripts/verdigris/shared.ts +++ b/mods/bworld/scripts/verdigris/shared.ts @@ -5,7 +5,7 @@ // moment they place it. power, delays and clicks are the server's, see server.ts import type { BlockPlacement, Face, SharedContext } from "bworld/shared"; import { arms_of, can_cover, face_states, faces_of } from "./joins.ts"; -import { type BlockReader, source_lookup } from "./signals.ts"; +import { type BlockReader, type InputParams, params_lookup, type SourceParams } from "./signals.ts"; import { FACES, OPPOSITE, step } from "./sides.ts"; export function register_verdigris_rules(ctx: SharedContext) { @@ -65,6 +65,7 @@ function join_reader(ctx: SharedContext): BlockReader { return { get_block: (x, y, z) => ctx.world.get_block(x, y, z), get_state: (x, y, z, name) => ctx.world.get_state(x, y, z, name), - source: source_lookup(ctx.blocks), + source: params_lookup(ctx.blocks, "bworld:verdigris_source"), + input: params_lookup(ctx.blocks, "bworld:verdigris_input"), }; } diff --git a/mods/bworld/scripts/verdigris/signals.ts b/mods/bworld/scripts/verdigris/signals.ts index 5595bce..821df77 100644 --- a/mods/bworld/scripts/verdigris/signals.ts +++ b/mods/bworld/scripts/verdigris/signals.ts @@ -25,12 +25,25 @@ export interface SourceParams { connects?: SideName[]; } +// a bworld:verdigris_input's params: something that keeps whether it's powered in one of its states +export interface InputParams { + // a 1 bit state, 1 while it's powered + state: string; + facing?: FacingParam; + // the sides it takes power from, every side when not set + in?: SideName[]; + // the sides wire joins, in when not set + connects?: SideName[]; +} + export interface BlockReader { // undefined where there's nothing to read, like a chunk that was never generated get_block(x: number, y: number, z: number): Id | undefined; get_state(x: number, y: number, z: number, name: string): number | undefined; // its bworld:verdigris_source params, undefined for a block that isn't one source(id: Id): SourceParams | undefined; + // its bworld:verdigris_input params, undefined for a block that isn't one + input(id: Id): InputParams | undefined; } export interface SignalReader extends BlockReader { @@ -38,25 +51,24 @@ export interface SignalReader extends BlockReader { solid(id: Id): boolean; } -// blocks' bworld:verdigris_source params from ctx.blocks, looked up once per block, since components don't change -export function source_lookup(blocks: { components(id: Id): Record }) { - const params = new Map(); - return (id: Id): SourceParams | undefined => { +// blocks' params for one component from ctx.blocks, like bworld:verdigris_source's, looked up once per block, since +// components don't change +export function params_lookup

(blocks: { components(id: Id): Record }, component: Id) { + const params = new Map(); + return (id: Id): P | undefined => { if (id === AIR) return undefined; - if (!params.has(id)) { - params.set(id, blocks.components(id)["bworld:verdigris_source"] as SourceParams | undefined); - } + if (!params.has(id)) params.set(id, blocks.components(id)[component] as P | undefined); return params.get(id); }; } -// the faces a source's list of sides names, for the source at x y z -function sides_of( +// the faces a source's or input's list of sides names, for the block at x y z +export function sides_of( world: BlockReader, x: number, y: number, z: number, - params: SourceParams, + params: { facing?: FacingParam }, names?: SideName[], ): readonly Face[] { if (!names) return FACES; @@ -93,10 +105,12 @@ export function strong_sides(world: BlockReader, x: number, y: number, z: number return params?.strong ? sides_of(world, x, y, z, params, params.strong) : []; } -// whether wire joins the side toward face of the block at x y z: a source's connects +// whether wire joins the side toward face of the block at x y z: a source's or an input's connects export function connects(world: BlockReader, x: number, y: number, z: number, id: Id, face: Face) { - const params = world.source(id); - return params !== undefined && sides_of(world, x, y, z, params, params.connects ?? params.out).includes(face); + const source = world.source(id); + if (source && sides_of(world, x, y, z, source, source.connects ?? source.out).includes(face)) return true; + const input = world.input(id); + return input !== undefined && sides_of(world, x, y, z, input, input.connects ?? input.in).includes(face); } // the power a solid block gets from the strong sides pointing into it. wire powers it too, but not for wire: a net @@ -127,12 +141,3 @@ export function block_power(world: SignalReader, x: number, y: number, z: number } return most; } - -// the power going into the block at x y z from the one toward face: that one's output on that side, or its power if -// it's solid -export function power_from(world: SignalReader, x: number, y: number, z: number, face: Face) { - const [nx, ny, nz] = step(x, y, z, face); - const id = world.get_block(nx, ny, nz); - if (id === undefined) return 0; - return Math.max(output(world, nx, ny, nz, id, OPPOSITE[face]), block_power(world, nx, ny, nz, id)); -} diff --git a/tests/verdigris.ts b/tests/verdigris.ts index b3b0d21..5047a6f 100644 --- a/tests/verdigris.ts +++ b/tests/verdigris.ts @@ -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("blocks", block)?.components?.["bworld:verdigris_source"] as never, + input: (block) => + EverythingRegistry.get("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 diff --git a/tests/verdigris_devices_test.ts b/tests/verdigris_devices_test.ts index 32a468e..bc9f5d2 100644 --- a/tests/verdigris_devices_test.ts +++ b/tests/verdigris_devices_test.ts @@ -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); +});