Doors working with verdigris
This commit is contained in:
@@ -30,6 +30,11 @@
|
|||||||
"name": "hinge",
|
"name": "hinge",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -92,7 +97,8 @@
|
|||||||
"drops": "bworld:acacia_door",
|
"drops": "bworld:acacia_door",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:door": { "pairs_with": "bworld:wooden_doors" }
|
"bworld:door": { "pairs_with": "bworld:wooden_doors" },
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@
|
|||||||
"name": "open",
|
"name": "open",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -51,7 +56,8 @@
|
|||||||
"drops": "bworld:acacia_trapdoor",
|
"drops": "bworld:acacia_trapdoor",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:trapdoor": {}
|
"bworld:trapdoor": {},
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,11 @@
|
|||||||
"name": "hinge",
|
"name": "hinge",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -92,7 +97,8 @@
|
|||||||
"drops": "bworld:birch_door",
|
"drops": "bworld:birch_door",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:door": { "pairs_with": "bworld:wooden_doors" }
|
"bworld:door": { "pairs_with": "bworld:wooden_doors" },
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@
|
|||||||
"name": "open",
|
"name": "open",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -51,7 +56,8 @@
|
|||||||
"drops": "bworld:birch_trapdoor",
|
"drops": "bworld:birch_trapdoor",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:trapdoor": {}
|
"bworld:trapdoor": {},
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,11 @@
|
|||||||
"name": "hinge",
|
"name": "hinge",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -92,7 +97,8 @@
|
|||||||
"drops": "bworld:cherry_door",
|
"drops": "bworld:cherry_door",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:door": { "pairs_with": "bworld:wooden_doors" }
|
"bworld:door": { "pairs_with": "bworld:wooden_doors" },
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@
|
|||||||
"name": "open",
|
"name": "open",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -51,7 +56,8 @@
|
|||||||
"drops": "bworld:cherry_trapdoor",
|
"drops": "bworld:cherry_trapdoor",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:trapdoor": {}
|
"bworld:trapdoor": {},
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,11 @@
|
|||||||
"name": "hinge",
|
"name": "hinge",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -92,7 +97,8 @@
|
|||||||
"drops": "bworld:dark_oak_door",
|
"drops": "bworld:dark_oak_door",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:door": { "pairs_with": "bworld:wooden_doors" }
|
"bworld:door": { "pairs_with": "bworld:wooden_doors" },
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@
|
|||||||
"name": "open",
|
"name": "open",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -51,7 +56,8 @@
|
|||||||
"drops": "bworld:oak_trapdoor",
|
"drops": "bworld:oak_trapdoor",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:trapdoor": {}
|
"bworld:trapdoor": {},
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,11 @@
|
|||||||
"name": "hinge",
|
"name": "hinge",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -92,7 +97,8 @@
|
|||||||
"drops": "bworld:jungle_door",
|
"drops": "bworld:jungle_door",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:door": { "pairs_with": "bworld:wooden_doors" }
|
"bworld:door": { "pairs_with": "bworld:wooden_doors" },
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@
|
|||||||
"name": "open",
|
"name": "open",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -51,7 +56,8 @@
|
|||||||
"drops": "bworld:jungle_trapdoor",
|
"drops": "bworld:jungle_trapdoor",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:trapdoor": {}
|
"bworld:trapdoor": {},
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,11 @@
|
|||||||
"name": "hinge",
|
"name": "hinge",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -92,7 +97,8 @@
|
|||||||
"drops": "bworld:oak_door",
|
"drops": "bworld:oak_door",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:door": { "pairs_with": "bworld:wooden_doors" }
|
"bworld:door": { "pairs_with": "bworld:wooden_doors" },
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@
|
|||||||
"name": "open",
|
"name": "open",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -51,7 +56,8 @@
|
|||||||
"drops": "bworld:oak_trapdoor",
|
"drops": "bworld:oak_trapdoor",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:trapdoor": {}
|
"bworld:trapdoor": {},
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,11 @@
|
|||||||
"name": "hinge",
|
"name": "hinge",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -92,7 +97,8 @@
|
|||||||
"drops": "bworld:spruce_door",
|
"drops": "bworld:spruce_door",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:door": { "pairs_with": "bworld:wooden_doors" }
|
"bworld:door": { "pairs_with": "bworld:wooden_doors" },
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@
|
|||||||
"name": "open",
|
"name": "open",
|
||||||
"bits": 1,
|
"bits": 1,
|
||||||
"default": 0
|
"default": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "powered",
|
||||||
|
"bits": 1,
|
||||||
|
"default": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"variants": {
|
"variants": {
|
||||||
@@ -51,7 +56,8 @@
|
|||||||
"drops": "bworld:spruce_trapdoor",
|
"drops": "bworld:spruce_trapdoor",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
"bworld:trapdoor": {}
|
"bworld:trapdoor": {},
|
||||||
|
"bworld:verdigris_input": { "state": "powered" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ export function setup(ctx: ServerContext) {
|
|||||||
register_farming(ctx);
|
register_farming(ctx);
|
||||||
register_fishing(ctx);
|
register_fishing(ctx);
|
||||||
register_door(ctx);
|
register_door(ctx);
|
||||||
|
register_trapdoor(ctx);
|
||||||
register_buckets(ctx);
|
register_buckets(ctx);
|
||||||
register_curios(ctx);
|
register_curios(ctx);
|
||||||
register_effects(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
|
// 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
|
// 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";
|
const DOOR = "bworld:door";
|
||||||
|
|
||||||
function register_door(ctx: ServerContext) {
|
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, state, ctx.world.get_state(x, y, z, state) ?? 0);
|
||||||
}
|
}
|
||||||
ctx.world.set_state(x, y + 1, z, "half", 1);
|
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
|
// 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);
|
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);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
// 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
|
// params, this is what makes their states change
|
||||||
import type { BlockRef, Face, Position, ServerContext } from "bworld/server";
|
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";
|
import { type FacingParam, front_of, OPPOSITE, pack, step } from "./sides.ts";
|
||||||
|
|
||||||
// a lever's face and facing states, see FacingParam
|
// a lever's face and facing states, see FacingParam
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -1,12 +1,20 @@
|
|||||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// 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
|
// verdigris's components, see REDSTONE.md: the wire, which keeps its net's power through the net index, sources,
|
||||||
// the devices
|
// inputs, and the devices
|
||||||
import type { BlockRef, Face, Position, ServerContext } from "bworld/server";
|
import type { BlockRef, Face, Position, ServerContext } from "bworld/server";
|
||||||
import { register_lever, register_torch } from "./devices.ts";
|
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 { 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 { DIRECTIONS, FACES, OPPOSITE, step } from "./sides.ts";
|
||||||
import { NetIndex, type SignalWorld, tell_around_below, wire_at } from "./wires.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<InputParams>("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_lever(ctx);
|
||||||
register_torch(ctx, world);
|
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
|
// 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
|
// among the six blocks beside it the engine tells
|
||||||
function tell_around_edges(world: SignalWorld, block: BlockRef) {
|
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
|
// 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_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),
|
get_state: (x, y, z, name) => ctx.world.get_state(x, y, z, name),
|
||||||
source: source_lookup(ctx.blocks),
|
source: params_lookup<SourceParams>(ctx.blocks, "bworld:verdigris_source"),
|
||||||
|
input: params_lookup<InputParams>(ctx.blocks, "bworld:verdigris_input"),
|
||||||
solid: (id) => ctx.blocks.solid(id),
|
solid: (id) => ctx.blocks.solid(id),
|
||||||
set_power: (x, y, z, power) => ctx.world.set_state(x, y, z, "power", power, { update_neighbors: false }),
|
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),
|
neighbor_changed: (x, y, z, from) => ctx.world.neighbor_changed(x, y, z, from),
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
// moment they place it. power, delays and clicks are the server's, see server.ts
|
// moment they place it. power, delays and clicks are the server's, see server.ts
|
||||||
import type { BlockPlacement, Face, SharedContext } from "bworld/shared";
|
import type { BlockPlacement, Face, SharedContext } from "bworld/shared";
|
||||||
import { arms_of, can_cover, face_states, faces_of } from "./joins.ts";
|
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";
|
import { FACES, OPPOSITE, step } from "./sides.ts";
|
||||||
|
|
||||||
export function register_verdigris_rules(ctx: SharedContext) {
|
export function register_verdigris_rules(ctx: SharedContext) {
|
||||||
@@ -65,6 +65,7 @@ function join_reader(ctx: SharedContext): BlockReader {
|
|||||||
return {
|
return {
|
||||||
get_block: (x, y, z) => ctx.world.get_block(x, y, z),
|
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),
|
get_state: (x, y, z, name) => ctx.world.get_state(x, y, z, name),
|
||||||
source: source_lookup(ctx.blocks),
|
source: params_lookup<SourceParams>(ctx.blocks, "bworld:verdigris_source"),
|
||||||
|
input: params_lookup<InputParams>(ctx.blocks, "bworld:verdigris_input"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,12 +25,25 @@ export interface SourceParams {
|
|||||||
connects?: SideName[];
|
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 {
|
export interface BlockReader {
|
||||||
// undefined where there's nothing to read, like a chunk that was never generated
|
// 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_block(x: number, y: number, z: number): Id | undefined;
|
||||||
get_state(x: number, y: number, z: number, name: string): number | 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
|
// its bworld:verdigris_source params, undefined for a block that isn't one
|
||||||
source(id: Id): SourceParams | undefined;
|
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 {
|
export interface SignalReader extends BlockReader {
|
||||||
@@ -38,25 +51,24 @@ export interface SignalReader extends BlockReader {
|
|||||||
solid(id: Id): boolean;
|
solid(id: Id): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// blocks' bworld:verdigris_source params from ctx.blocks, looked up once per block, since components don't change
|
// blocks' params for one component from ctx.blocks, like bworld:verdigris_source's, looked up once per block, since
|
||||||
export function source_lookup(blocks: { components(id: Id): Record<Id, unknown> }) {
|
// components don't change
|
||||||
const params = new Map<Id, SourceParams | undefined>();
|
export function params_lookup<P>(blocks: { components(id: Id): Record<Id, unknown> }, component: Id) {
|
||||||
return (id: Id): SourceParams | undefined => {
|
const params = new Map<Id, P | undefined>();
|
||||||
|
return (id: Id): P | undefined => {
|
||||||
if (id === AIR) return undefined;
|
if (id === AIR) return undefined;
|
||||||
if (!params.has(id)) {
|
if (!params.has(id)) params.set(id, blocks.components(id)[component] as P | undefined);
|
||||||
params.set(id, blocks.components(id)["bworld:verdigris_source"] as SourceParams | undefined);
|
|
||||||
}
|
|
||||||
return params.get(id);
|
return params.get(id);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// the faces a source's list of sides names, for the source at x y z
|
// the faces a source's or input's list of sides names, for the block at x y z
|
||||||
function sides_of(
|
export function sides_of(
|
||||||
world: BlockReader,
|
world: BlockReader,
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
z: number,
|
z: number,
|
||||||
params: SourceParams,
|
params: { facing?: FacingParam },
|
||||||
names?: SideName[],
|
names?: SideName[],
|
||||||
): readonly Face[] {
|
): readonly Face[] {
|
||||||
if (!names) return FACES;
|
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) : [];
|
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) {
|
export function connects(world: BlockReader, x: number, y: number, z: number, id: Id, face: Face) {
|
||||||
const params = world.source(id);
|
const source = world.source(id);
|
||||||
return params !== undefined && sides_of(world, x, y, z, params, params.connects ?? params.out).includes(face);
|
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
|
// 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;
|
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));
|
|
||||||
}
|
|
||||||
|
|||||||
+6
-3
@@ -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 });
|
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
||||||
const player = t.game.players()[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
|
// 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
|
||||||
const use = (id: string | undefined, x: number, y: number, z: number, face: Face) => {
|
// 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) {
|
if (id) {
|
||||||
t.send(1, { type: "chat", text: `/give ${id}` });
|
t.send(1, { type: "chat", text: `/give ${id}` });
|
||||||
const slot = player.inventory.to_synced_data().findIndex((item) => item?.id === id);
|
const slot = player.inventory.to_synced_data().findIndex((item) => item?.id === id);
|
||||||
t.send(1, { type: "select_slot", slot });
|
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] });
|
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) =>
|
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],
|
get_state: (x, y, z, name) => states(x, y, z)[name],
|
||||||
source: (block) =>
|
source: (block) =>
|
||||||
EverythingRegistry.get<BlockRegistry>("blocks", block)?.components?.["bworld:verdigris_source"] as never,
|
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);
|
const covers = (...at: Pos) => FACES.filter((face) => states(...at)[face] === 1);
|
||||||
// the wire cells the wire at x y z joins
|
// the wire cells the wire at x y z joins
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { type BlockRegistry, EverythingRegistry } from "$/common/everything_regi
|
|||||||
import type { Face } from "$/common/mod_api/shared.ts";
|
import type { Face } from "$/common/mod_api/shared.ts";
|
||||||
import { AIR_ID } from "$/common/protocol.ts";
|
import { AIR_ID } from "$/common/protocol.ts";
|
||||||
import { default_block_value, set_state_value } from "$/common/utils.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 { FACE_OFFSETS, FACES, OPPOSITE, step } from "$/mods/bworld/scripts/verdigris/sides.ts";
|
||||||
import { client_of, server_blocks, test_game } from "./helpers.ts";
|
import { client_of, server_blocks, test_game } from "./helpers.ts";
|
||||||
import { AREA, type Builder, builder, type Pos } from "./verdigris.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;
|
const dropped = b.t.game.item_entities().filter((e) => e.item.type_id === TORCH).length;
|
||||||
assertEquals(dropped, 2);
|
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);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user