Verdigris wire and lever
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "bworld:lever",
|
||||
"textures": { "lever": "bworld:lever", "base": "bworld:cobblestone" },
|
||||
"model": "bworld:lever",
|
||||
"render_layer": "cutout",
|
||||
"collision": false,
|
||||
"interactive": true,
|
||||
"mining": { "toughness": 0.5 },
|
||||
"drops": "bworld:lever",
|
||||
"states": [
|
||||
{ "name": "face", "bits": 2, "default": 0 },
|
||||
{ "name": "facing", "bits": 2, "default": 0 },
|
||||
{ "name": "powered", "bits": 1, "default": 0 }
|
||||
],
|
||||
"variants": {
|
||||
"powered=1": { "model": "bworld:lever_on" },
|
||||
"face=0,facing=1": { "y": 90 },
|
||||
"face=0,facing=2": { "y": 180 },
|
||||
"face=0,facing=3": { "y": 270 },
|
||||
"face=1,facing=0": { "x": 90 },
|
||||
"face=1,facing=1": { "x": 90, "y": 90 },
|
||||
"face=1,facing=2": { "x": 90, "y": 180 },
|
||||
"face=1,facing=3": { "x": 90, "y": 270 },
|
||||
"face=2,facing=0": { "x": 180, "y": 180 },
|
||||
"face=2,facing=1": { "x": 180, "y": 270 },
|
||||
"face=2,facing=2": { "x": 180 },
|
||||
"face=2,facing=3": { "x": 180, "y": 90 }
|
||||
},
|
||||
"components": {
|
||||
"bworld:verdigris_source": {
|
||||
"power": 15,
|
||||
"when": "powered",
|
||||
"facing": { "face": "face", "facing": "facing" },
|
||||
"strong": ["back"]
|
||||
},
|
||||
"bworld:lever": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "bworld:verdigris_block",
|
||||
"textures": "bworld:verdigris_block",
|
||||
"mining": {
|
||||
"toughness": 5,
|
||||
"tool": "pickaxe",
|
||||
"requires_tool": true
|
||||
},
|
||||
"drops": "bworld:verdigris_block",
|
||||
"components": {
|
||||
"bworld:verdigris_source": { "power": 15 }
|
||||
}
|
||||
}
|
||||
}
|
||||
+1773
-466
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"item": {
|
||||
"id": "bworld:verdigris",
|
||||
"texture": "bworld:verdigris",
|
||||
"places": "bworld:verdigris_wire"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"loot_table": {
|
||||
"pools": [
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "block_state",
|
||||
"states": {
|
||||
"north": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"entries": [
|
||||
{
|
||||
"item": "bworld:verdigris"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "block_state",
|
||||
"states": {
|
||||
"east": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"entries": [
|
||||
{
|
||||
"item": "bworld:verdigris"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "block_state",
|
||||
"states": {
|
||||
"south": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"entries": [
|
||||
{
|
||||
"item": "bworld:verdigris"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "block_state",
|
||||
"states": {
|
||||
"west": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"entries": [
|
||||
{
|
||||
"item": "bworld:verdigris"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "block_state",
|
||||
"states": {
|
||||
"top": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"entries": [
|
||||
{
|
||||
"item": "bworld:verdigris"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "block_state",
|
||||
"states": {
|
||||
"bottom": 1
|
||||
}
|
||||
}
|
||||
],
|
||||
"entries": [
|
||||
{
|
||||
"item": "bworld:verdigris"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"model": {
|
||||
"id": "bworld:lever",
|
||||
"elements": [
|
||||
{
|
||||
"from": [5, 0, 4],
|
||||
"to": [11, 3, 12],
|
||||
"faces": {
|
||||
"top": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"bottom": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"north": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"south": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"west": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"east": {
|
||||
"texture": "#base"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"from": [7, 1, 7],
|
||||
"to": [9, 11, 9],
|
||||
"rotation": {
|
||||
"origin": [8, 1, 8],
|
||||
"axis": "x",
|
||||
"angle": -45
|
||||
},
|
||||
"faces": {
|
||||
"top": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 8]
|
||||
},
|
||||
"north": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 16]
|
||||
},
|
||||
"south": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 16]
|
||||
},
|
||||
"west": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 16]
|
||||
},
|
||||
"east": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 16]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"model": {
|
||||
"id": "bworld:lever_on",
|
||||
"elements": [
|
||||
{
|
||||
"from": [5, 0, 4],
|
||||
"to": [11, 3, 12],
|
||||
"faces": {
|
||||
"top": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"bottom": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"north": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"south": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"west": {
|
||||
"texture": "#base"
|
||||
},
|
||||
"east": {
|
||||
"texture": "#base"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"from": [7, 1, 7],
|
||||
"to": [9, 11, 9],
|
||||
"rotation": {
|
||||
"origin": [8, 1, 8],
|
||||
"axis": "x",
|
||||
"angle": 45
|
||||
},
|
||||
"faces": {
|
||||
"top": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 8]
|
||||
},
|
||||
"north": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 16]
|
||||
},
|
||||
"south": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 16]
|
||||
},
|
||||
"west": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 16]
|
||||
},
|
||||
"east": {
|
||||
"texture": "#lever",
|
||||
"uv": [7, 6, 9, 16]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"model": {
|
||||
"id": "bworld:verdigris_wire_corner",
|
||||
"elements": [
|
||||
{
|
||||
"from": [7, 0, 0],
|
||||
"to": [9, 0.25, 7],
|
||||
"faces": {
|
||||
"top": {
|
||||
"texture": "#wire"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"from": [7, 0, 0],
|
||||
"to": [9, 16, 0.25],
|
||||
"faces": {
|
||||
"south": {
|
||||
"texture": "#wire"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"model": {
|
||||
"id": "bworld:verdigris_wire_corner_east",
|
||||
"elements": [
|
||||
{
|
||||
"from": [9, 0, 7],
|
||||
"to": [16, 0.25, 9],
|
||||
"faces": {
|
||||
"top": {
|
||||
"texture": "#wire"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"from": [15.75, 0, 7],
|
||||
"to": [16, 16, 9],
|
||||
"faces": {
|
||||
"west": {
|
||||
"texture": "#wire"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"model": {
|
||||
"id": "bworld:verdigris_wire_edge",
|
||||
"id": "bworld:verdigris_wire_out",
|
||||
"elements": [
|
||||
{
|
||||
"from": [7, 0, -0.25],
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"model": {
|
||||
"id": "bworld:verdigris_wire_edge_east",
|
||||
"id": "bworld:verdigris_wire_out_east",
|
||||
"elements": [
|
||||
{
|
||||
"from": [9, 0, 7],
|
||||
@@ -7,6 +7,7 @@ import { register_curios } from "./curios.ts";
|
||||
import { register_effects } from "./effects.ts";
|
||||
import { register_fishing } from "./fishing.ts";
|
||||
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
|
||||
import { register_verdigris } from "./verdigris/server.ts";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
register_hoeable(ctx);
|
||||
@@ -25,6 +26,7 @@ export function setup(ctx: ServerContext) {
|
||||
register_curios(ctx);
|
||||
register_effects(ctx);
|
||||
register_growable_block(ctx);
|
||||
register_verdigris(ctx);
|
||||
}
|
||||
|
||||
// right clicking with a type of tool turns the block into another, like any hoe on grass
|
||||
|
||||
@@ -5,11 +5,13 @@
|
||||
// server and on every client, so players see a fence join or a trapdoor turn the moment they place it, and a trapdoor
|
||||
// or a door open the moment they click it
|
||||
import type { SharedBlock, SharedContext } from "bworld/shared";
|
||||
import { register_verdigris_rules } from "./verdigris/shared.ts";
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
register_fence(ctx);
|
||||
register_trapdoor(ctx);
|
||||
register_door(ctx);
|
||||
register_verdigris_rules(ctx);
|
||||
}
|
||||
|
||||
// the sides a fence connects on, and the face of the block there it connects to
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// 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 { type FacingParam, front_of, OPPOSITE, step } from "./sides.ts";
|
||||
|
||||
// a lever's face and facing states, see FacingParam
|
||||
const LEVER_FACING: FacingParam = { face: "face", facing: "facing" };
|
||||
|
||||
// switched on and off by right clicking it, and it breaks when what it's on goes
|
||||
export function register_lever(ctx: ServerContext) {
|
||||
ctx.components.register_block("bworld:lever", {
|
||||
on_interact(block) {
|
||||
const powered = ctx.world.get_state(block.x, block.y, block.z, "powered");
|
||||
ctx.world.set_state(block.x, block.y, block.z, "powered", powered ? 0 : 1);
|
||||
return true;
|
||||
},
|
||||
on_neighbor_change(block, _params, neighbor) {
|
||||
const front = front_of(LEVER_FACING, (name) => ctx.world.get_state(block.x, block.y, block.z, name));
|
||||
breaks_without(ctx, block, OPPOSITE[front], neighbor);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// breaks a block that hangs from the block toward face when the neighbor that changed is that block and its face isn't
|
||||
// something to attach to anymore
|
||||
function breaks_without(ctx: ServerContext, block: BlockRef, face: Face, neighbor: Position) {
|
||||
const [x, y, z] = step(block.x, block.y, block.z, face);
|
||||
if (neighbor.x !== x || neighbor.y !== y || neighbor.z !== z) return;
|
||||
if (!ctx.world.is_face_sturdy(x, y, z, OPPOSITE[face])) ctx.world.break_block(block.x, block.y, block.z);
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// which faces of its cell a wire covers and what it joins, see "Joining" in REDSTONE.md. a wire cell covers some of its
|
||||
// six faces, each lying on the block on that side, and the wire on two faces that meet joins at that edge. a face's
|
||||
// other directions lead out of the cell, and its arm_ states say which of those it joins. the shared rule sets them the
|
||||
// same on the server and clients, and works out a join between two cells the same from either end
|
||||
import type { Face } from "bworld/shared";
|
||||
import { AIR, type BlockReader, connects, WIRE } from "./signals.ts";
|
||||
import { DIRECTIONS, FACES, OPPOSITE, step } from "./sides.ts";
|
||||
|
||||
// how many arm_ states a wire has, so how many ways out of its cell its faces can have between them
|
||||
export const MAX_ARMS = 6;
|
||||
|
||||
// whether the wire at x y z covers face of its cell. false when there's no wire
|
||||
export function covers(world: BlockReader, x: number, y: number, z: number, face: Face) {
|
||||
return world.get_block(x, y, z) === WIRE && world.get_state(x, y, z, face) === 1;
|
||||
}
|
||||
|
||||
// the faces of its cell the wire at x y z covers, in FACES' order
|
||||
export function faces_of(world: BlockReader, x: number, y: number, z: number): Face[] {
|
||||
return world.get_block(x, y, z) === WIRE ? FACES.filter((face) => world.get_state(x, y, z, face) === 1) : [];
|
||||
}
|
||||
|
||||
// the ways out of a cell covering faces: each face's directions toward faces it doesn't cover, which can lead to
|
||||
// another cell. in FACES' order, then DIRECTIONS', which is the order of the arm_ states
|
||||
export function ways_out(faces: readonly Face[]): [Face, Face][] {
|
||||
return faces.flatMap((face) =>
|
||||
DIRECTIONS[face].filter((direction) => !faces.includes(direction)).map((direction): [Face, Face] => [
|
||||
face,
|
||||
direction,
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
// whether one wire cell can cover all of faces: its ways out have to fit in its arm_ states. that leaves out two
|
||||
// opposite faces with nothing joining them, and three or four faces all the way around the cell
|
||||
export function can_cover(faces: readonly Face[]) {
|
||||
return faces.length > 0 && ways_out(faces).length <= MAX_ARMS;
|
||||
}
|
||||
|
||||
// what the wire on face of the cell at x y z joins toward direction, out of its cell: the same face of the next cell
|
||||
// ("straight"), the next face of the block it lies on, around that block's edge ("edge"), or a block that makes or
|
||||
// takes power ("block"). wire doesn't block the way around an edge, only the faces it covers matter
|
||||
export type WayOut = "straight" | "edge" | "block";
|
||||
|
||||
export function joins_out(
|
||||
world: BlockReader,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
face: Face,
|
||||
direction: Face,
|
||||
): WayOut | undefined {
|
||||
const next = step(x, y, z, direction);
|
||||
const id = world.get_block(...next);
|
||||
if (id === undefined) return undefined;
|
||||
if (id === WIRE) {
|
||||
if (covers(world, ...next, face)) return "straight";
|
||||
} else if (id !== AIR) {
|
||||
return connects(world, ...next, id, OPPOSITE[direction]) ? "block" : undefined;
|
||||
}
|
||||
return covers(world, ...around_edge(x, y, z, face, direction), OPPOSITE[direction]) ? "edge" : undefined;
|
||||
}
|
||||
|
||||
// the cell past the edge of the block the wire on face lies on, toward direction: where the wire on that block's next
|
||||
// face is
|
||||
export function around_edge(x: number, y: number, z: number, face: Face, direction: Face) {
|
||||
return step(...step(x, y, z, face), direction);
|
||||
}
|
||||
|
||||
// the wire cell a way out joins, undefined when it joins a block that isn't wire or nothing
|
||||
export function joined_cell(world: BlockReader, x: number, y: number, z: number, face: Face, direction: Face) {
|
||||
const way = joins_out(world, x, y, z, face, direction);
|
||||
if (way === "straight") return step(x, y, z, direction);
|
||||
if (way === "edge") return around_edge(x, y, z, face, direction);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// the arm_ states of a wire cell at x y z covering faces, from what's around it
|
||||
export function arms_of(world: BlockReader, x: number, y: number, z: number, faces: readonly Face[]) {
|
||||
const arms: Record<string, number> = {};
|
||||
for (let i = 0; i < MAX_ARMS; i++) arms[`arm_${i}`] = 0;
|
||||
ways_out(faces).forEach(([face, direction], i) => {
|
||||
arms[`arm_${i}`] = joins_out(world, x, y, z, face, direction) ? 1 : 0;
|
||||
});
|
||||
return arms;
|
||||
}
|
||||
|
||||
// a wire cell's face states for faces, all six
|
||||
export function face_states(faces: readonly Face[]) {
|
||||
return Object.fromEntries(FACES.map((face) => [face, faces.includes(face) ? 1 : 0]));
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// verdigris's components, see REDSTONE.md: the wire, which works out its net's power whenever something around it
|
||||
// changes, sources, and the devices
|
||||
import type { BlockRef, Face, Position, ServerContext } from "bworld/server";
|
||||
import { register_lever } 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 { DIRECTIONS, FACES, OPPOSITE, step } from "./sides.ts";
|
||||
import { type SignalWorld, tell_around_below, update_net, wire_at } from "./wires.ts";
|
||||
|
||||
// a wire's states that are its shape: the faces it covers and its arms. its power changing isn't
|
||||
const SHAPE_STATES = [...FACES, ...Array.from({ length: MAX_ARMS }, (_, i) => `arm_${i}`)];
|
||||
|
||||
export function register_verdigris(ctx: ServerContext) {
|
||||
const world = signal_world(ctx);
|
||||
|
||||
ctx.components.register_block("bworld:verdigris_wire", {
|
||||
on_create(block) {
|
||||
tell_around_edges(world, block);
|
||||
update_net(world, block.x, block.y, block.z);
|
||||
},
|
||||
on_break(block) {
|
||||
const wire = wire_at(world, block.x, block.y, block.z);
|
||||
tell_around_edges(world, block);
|
||||
if (wire && wire.power > 0) tell_around_below(world, wire);
|
||||
},
|
||||
on_neighbor_change(block, _params, neighbor) {
|
||||
if (lose_face(ctx, world, block, neighbor)) return;
|
||||
update_net(world, block.x, block.y, block.z);
|
||||
},
|
||||
// it covered another face, or its rule joined or let go of something, which can join two nets or split one
|
||||
on_state_change(block, _params, before) {
|
||||
const changed = SHAPE_STATES.some((name) =>
|
||||
ctx.world.get_state(block.x, block.y, block.z, name) !== before[name]
|
||||
);
|
||||
if (changed) update_net(world, block.x, block.y, block.z);
|
||||
},
|
||||
});
|
||||
|
||||
// the engine tells the blocks beside a source when it changes. these tell the blocks around the solid blocks its
|
||||
// strong sides power, which nothing else would. on_state_change tells them for the sides it had and has, in case it
|
||||
// turned
|
||||
ctx.components.register_block("bworld:verdigris_source", {
|
||||
on_create: (block) => tell_through(world, block, strong_sides(world, block.x, block.y, block.z, block.id)),
|
||||
on_break: (block) => tell_through(world, block, strong_sides(world, block.x, block.y, block.z, block.id)),
|
||||
on_state_change(block, _params, before) {
|
||||
const was: BlockReader = {
|
||||
...world,
|
||||
get_state: (x, y, z, name) =>
|
||||
x === block.x && y === block.y && z === block.z ? before[name] : world.get_state(x, y, z, name),
|
||||
};
|
||||
const sides = new Set([
|
||||
...strong_sides(was, block.x, block.y, block.z, block.id),
|
||||
...strong_sides(world, block.x, block.y, block.z, block.id),
|
||||
]);
|
||||
tell_through(world, block, [...sides]);
|
||||
},
|
||||
});
|
||||
|
||||
register_lever(ctx);
|
||||
}
|
||||
|
||||
// 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) {
|
||||
const faces = faces_of(world, block.x, block.y, block.z);
|
||||
for (const face of faces) {
|
||||
for (const direction of DIRECTIONS[face]) {
|
||||
if (faces.includes(direction)) continue;
|
||||
const [x, y, z] = around_edge(block.x, block.y, block.z, face, direction);
|
||||
if (covers(world, x, y, z, OPPOSITE[direction])) world.neighbor_changed(x, y, z, block);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the face of a wire that lay on the block that changed, when that block's face isn't something to lie on anymore: the
|
||||
// wire comes off it and drops it, or breaks when what's left can't be one cell. whether it did
|
||||
function lose_face(ctx: ServerContext, world: SignalWorld, block: BlockRef, neighbor: Position) {
|
||||
const faces = faces_of(world, block.x, block.y, block.z);
|
||||
const face = faces.find((face) => {
|
||||
const [x, y, z] = step(block.x, block.y, block.z, face);
|
||||
return x === neighbor.x && y === neighbor.y && z === neighbor.z;
|
||||
});
|
||||
if (!face || ctx.world.is_face_sturdy(neighbor.x, neighbor.y, neighbor.z, OPPOSITE[face])) return false;
|
||||
const left = faces.filter((other) => other !== face);
|
||||
if (!can_cover(left)) {
|
||||
ctx.world.break_block(block.x, block.y, block.z);
|
||||
return true;
|
||||
}
|
||||
ctx.world.drop_item(block.x, block.y, block.z, { id: "bworld:verdigris", count: 1 });
|
||||
const states = { ...face_states(left), ...arms_of(world, block.x, block.y, block.z, left) };
|
||||
for (const [name, value] of Object.entries(states)) ctx.world.set_state(block.x, block.y, block.z, name, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
// the blocks around each solid block toward those faces of the block, powered through it
|
||||
function tell_through(world: SignalWorld, block: BlockRef, faces: readonly Face[]) {
|
||||
for (const face of faces) {
|
||||
const [sx, sy, sz] = step(block.x, block.y, block.z, face);
|
||||
const id = world.get_block(sx, sy, sz);
|
||||
if (id === undefined || !world.solid(id)) continue;
|
||||
for (const around of FACES) {
|
||||
if (around === OPPOSITE[face]) continue;
|
||||
world.neighbor_changed(...step(sx, sy, sz, around), { x: sx, y: sy, z: sz });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function signal_world(ctx: ServerContext): SignalWorld {
|
||||
return {
|
||||
// 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),
|
||||
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),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// verdigris placement and joins, see REDSTONE.md. rules, so a player sees a wire join or a lever turn to the wall the
|
||||
// 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 { FACES, OPPOSITE, step } from "./sides.ts";
|
||||
|
||||
export function register_verdigris_rules(ctx: SharedContext) {
|
||||
const world = join_reader(ctx);
|
||||
|
||||
// lies on the face it's placed against, joined to what's around it. placed into a cell that has wire already, the
|
||||
// wire there covers that face too, keeping its power
|
||||
ctx.components.register_block("bworld:verdigris_wire", {
|
||||
state_for_placement(placement) {
|
||||
const face = hangs_from(ctx, placement);
|
||||
if (!face) return false;
|
||||
const { x, y, z } = placement;
|
||||
const covered = faces_of(world, x, y, z);
|
||||
const faces = FACES.filter((other) => other === face || covered.includes(other));
|
||||
if (covered.includes(face) || !can_cover(faces)) return false;
|
||||
const power = covered.length > 0 ? world.get_state(x, y, z, "power") ?? 0 : 0;
|
||||
return { ...face_states(faces), ...arms_of(world, x, y, z, faces), power };
|
||||
},
|
||||
on_neighbor_change(block) {
|
||||
const { x, y, z } = block;
|
||||
for (const [name, arm] of Object.entries(arms_of(world, x, y, z, faces_of(world, x, y, z)))) {
|
||||
ctx.world.set_state(x, y, z, name, arm);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// on a floor, on a wall pointing out of it, or on a ceiling. on a floor or ceiling, facing faces the player
|
||||
ctx.components.register_block("bworld:lever", {
|
||||
state_for_placement(placement): Record<string, number> | false {
|
||||
const side = hangs_from(ctx, placement);
|
||||
if (!side) return false;
|
||||
if (side === "bottom") return { face: 0 };
|
||||
if (side === "top") return { face: 2 };
|
||||
return { face: 1, facing: FACES.indexOf(OPPOSITE[side]) };
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// the face toward the block a wire or lever placed there hangs from: the one it's placed against, if that block's face
|
||||
// is something to attach to
|
||||
function hangs_from(ctx: SharedContext, placement: BlockPlacement): Face | undefined {
|
||||
const side = OPPOSITE[placement.face];
|
||||
const [x, y, z] = step(placement.x, placement.y, placement.z, side);
|
||||
return ctx.world.is_face_sturdy(x, y, z, placement.face) ? side : undefined;
|
||||
}
|
||||
|
||||
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),
|
||||
};
|
||||
}
|
||||
@@ -17,9 +17,47 @@ export const FACE_OFFSETS: Readonly<Record<Face, readonly [number, number, numbe
|
||||
bottom: [0, -1, 0],
|
||||
};
|
||||
|
||||
// the 4 directions along a wire lying on the block toward each face, as join_0 to join_3 number them. they're the same
|
||||
// on every face: a floor wire's north, east, south and west, turned the way the floor turns onto that face, so a wire
|
||||
// is drawn the same few pieces turned by its side. on a wall 0 is always down and 2 up
|
||||
export const OPPOSITE: Readonly<Record<Face, Face>> = {
|
||||
north: "south",
|
||||
east: "west",
|
||||
south: "north",
|
||||
west: "east",
|
||||
top: "bottom",
|
||||
bottom: "top",
|
||||
};
|
||||
|
||||
// the block toward face from x y z
|
||||
export function step(x: number, y: number, z: number, face: Face): [number, number, number] {
|
||||
const [dx, dy, dz] = FACE_OFFSETS[face];
|
||||
return [x + dx, y + dy, z + dz];
|
||||
}
|
||||
|
||||
// which way a block points, from its states: the name of a facing state, read as FACES' values with 2 bits or 3, or
|
||||
// a face and facing state for blocks on a floor, wall or ceiling, like a lever. face 0 is the floor, where it points
|
||||
// up, 1 a wall, where it points the way facing says, and 2 the ceiling, where it points down
|
||||
export type FacingParam = string | { face: string; facing: string };
|
||||
|
||||
export function front_of(facing: FacingParam, state: (name: string) => number | undefined): Face {
|
||||
if (typeof facing === "string") return FACES[state(facing) ?? 0];
|
||||
const on = state(facing.face) ?? 0;
|
||||
return on === 0 ? "top" : on === 2 ? "bottom" : FACES[state(facing.facing) ?? 0];
|
||||
}
|
||||
|
||||
// a block's sides as components name them: faces, or front, back and sides, the four around the way it points
|
||||
export type SideName = Face | "front" | "back" | "sides";
|
||||
|
||||
export function named_sides(names: readonly SideName[], front: Face | undefined): Face[] {
|
||||
return names.flatMap((name): readonly Face[] => {
|
||||
if (name === "front") return front ? [front] : [];
|
||||
if (name === "back") return front ? [OPPOSITE[front]] : [];
|
||||
if (name === "sides") return front ? FACES.filter((face) => face !== front && face !== OPPOSITE[front]) : [];
|
||||
return [name];
|
||||
});
|
||||
}
|
||||
|
||||
// the 4 directions along a wire on each face of its cell, in the order its arm_ states take them (see ways_out in
|
||||
// joins.ts). they're the same on every face: a floor's north, east, south and west, turned the way the floor turns
|
||||
// onto that face, so a wire is drawn the same few pieces turned by face. on a wall 0 is always down and 2 up
|
||||
export const DIRECTIONS: Readonly<Record<Face, readonly [Face, Face, Face, Face]>> = {
|
||||
north: ["bottom", "west", "top", "east"],
|
||||
east: ["bottom", "north", "top", "south"],
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the power blocks give each other, see "How Power Works" in REDSTONE.md. every signal is a block state, so this only
|
||||
// reads states, through a small world instead of ctx so it runs the same in shared scripts, server scripts and tests
|
||||
import type { Face, Id } from "bworld/shared";
|
||||
import { FACES, type FacingParam, front_of, named_sides, OPPOSITE, type SideName, step } from "./sides.ts";
|
||||
|
||||
export const WIRE = "bworld:verdigris_wire";
|
||||
export const AIR = "bworld:air";
|
||||
export const MAX_POWER = 15;
|
||||
|
||||
// a bworld:verdigris_source's params, the block json's: something that gives power out of some of its sides
|
||||
export interface SourceParams {
|
||||
// the state holding the power it gives, 0 to 15, or always this much
|
||||
power: string | number;
|
||||
// a 1 bit state it only gives power while it's 1
|
||||
when?: string;
|
||||
facing?: FacingParam;
|
||||
// the sides it gives power out of, every side when not set
|
||||
out?: SideName[];
|
||||
// the ones of those that also power a solid block there
|
||||
strong?: SideName[];
|
||||
// the sides wire joins, out 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;
|
||||
}
|
||||
|
||||
export interface SignalReader extends BlockReader {
|
||||
// whether it's solid for power: a block that's powered and passes it on, see ctx.blocks.solid
|
||||
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<Id, unknown> }) {
|
||||
const params = new Map<Id, SourceParams | undefined>();
|
||||
return (id: Id): SourceParams | undefined => {
|
||||
if (id === AIR) return undefined;
|
||||
if (!params.has(id)) {
|
||||
params.set(id, blocks.components(id)["bworld:verdigris_source"] as SourceParams | undefined);
|
||||
}
|
||||
return params.get(id);
|
||||
};
|
||||
}
|
||||
|
||||
// the faces a source's list of sides names, for the source at x y z
|
||||
function sides_of(
|
||||
world: BlockReader,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
params: SourceParams,
|
||||
names?: SideName[],
|
||||
): readonly Face[] {
|
||||
if (!names) return FACES;
|
||||
const front = params.facing === undefined
|
||||
? undefined
|
||||
: front_of(params.facing, (name) => world.get_state(x, y, z, name));
|
||||
return named_sides(names, front);
|
||||
}
|
||||
|
||||
// the power the source at x y z gives out of its side toward face, strong or not
|
||||
function gives(world: BlockReader, x: number, y: number, z: number, params: SourceParams, face: Face, strong: boolean) {
|
||||
if (params.when !== undefined && !world.get_state(x, y, z, params.when)) return 0;
|
||||
const sides = strong ? params.strong ?? [] : params.out;
|
||||
if (!sides_of(world, x, y, z, params, sides).includes(face)) return 0;
|
||||
const power = typeof params.power === "number" ? params.power : world.get_state(x, y, z, params.power) ?? 0;
|
||||
return Math.min(power, MAX_POWER);
|
||||
}
|
||||
|
||||
// the power the block at x y z gives out of its side toward face. 0 for a block that isn't a source
|
||||
export function output(world: BlockReader, x: number, y: number, z: number, id: Id, face: Face) {
|
||||
const params = world.source(id);
|
||||
return params ? gives(world, x, y, z, params, face, false) : 0;
|
||||
}
|
||||
|
||||
// what output gives that also powers a solid block there
|
||||
export function strong_output(world: BlockReader, x: number, y: number, z: number, id: Id, face: Face) {
|
||||
const params = world.source(id);
|
||||
return params ? gives(world, x, y, z, params, face, true) : 0;
|
||||
}
|
||||
|
||||
// the faces of the source at x y z that power a solid block there, as its states are now
|
||||
export function strong_sides(world: BlockReader, x: number, y: number, z: number, id: Id): readonly Face[] {
|
||||
const params = world.source(id);
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
// the power a solid block gets from the strong sides pointing into it. wire powers it too, but not for wire: a net
|
||||
// would hold itself on through any block it lies on
|
||||
export function solid_power(world: SignalReader, x: number, y: number, z: number, id: Id) {
|
||||
if (!world.solid(id)) return 0;
|
||||
let most = 0;
|
||||
for (const face of FACES) {
|
||||
const [nx, ny, nz] = step(x, y, z, face);
|
||||
const beside = world.get_block(nx, ny, nz);
|
||||
if (beside === undefined || beside === WIRE) continue;
|
||||
most = Math.max(most, strong_output(world, nx, ny, nz, beside, OPPOSITE[face]));
|
||||
if (most === MAX_POWER) break;
|
||||
}
|
||||
return most;
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// wire nets, see "Power" in REDSTONE.md: a wire cell and every wire cell it reaches through its joins are one net, and
|
||||
// they all have the same power, the most any block gives into any of them. a cell's faces always join each other, so
|
||||
// it's one piece of one net. walked again on every change for now. the net index that keeps walks between changes
|
||||
// comes later
|
||||
import type { Face } from "bworld/shared";
|
||||
import { faces_of, joined_cell, joins_out, ways_out } from "./joins.ts";
|
||||
import { type BlockReader, MAX_POWER, output, type SignalReader, solid_power } from "./signals.ts";
|
||||
import { FACES, OPPOSITE, step } from "./sides.ts";
|
||||
|
||||
export interface SignalWorld extends SignalReader {
|
||||
// sets a wire's power without telling the blocks around it, see ServerWorld.set_state's update_neighbors
|
||||
set_power(x: number, y: number, z: number, power: number): void;
|
||||
// see ServerWorld.neighbor_changed
|
||||
neighbor_changed(x: number, y: number, z: number, from: { x: number; y: number; z: number }): void;
|
||||
}
|
||||
|
||||
export interface Wire {
|
||||
readonly x: number;
|
||||
readonly y: number;
|
||||
readonly z: number;
|
||||
// the faces of its cell it covers
|
||||
readonly faces: readonly Face[];
|
||||
// the ways out of its cell it joins, each a face and a direction, see ways_out
|
||||
readonly out: readonly [Face, Face][];
|
||||
readonly power: number;
|
||||
}
|
||||
|
||||
const key = (x: number, y: number, z: number) => `${x} ${y} ${z}`;
|
||||
|
||||
// the wire at x y z, undefined when there's none
|
||||
export function wire_at(world: BlockReader, x: number, y: number, z: number): Wire | undefined {
|
||||
const faces = faces_of(world, x, y, z);
|
||||
if (faces.length === 0) return undefined;
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
faces,
|
||||
out: ways_out(faces).filter((_, i) => world.get_state(x, y, z, `arm_${i}`) === 1),
|
||||
power: world.get_state(x, y, z, "power") ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
// every wire in the net of the wire at x y z, nothing when there's no wire there
|
||||
export function walk(world: BlockReader, x: number, y: number, z: number): Wire[] {
|
||||
const seen = new Set([key(x, y, z)]);
|
||||
const queue: [number, number, number][] = [[x, y, z]];
|
||||
const wires: Wire[] = [];
|
||||
while (queue.length > 0) {
|
||||
const wire = wire_at(world, ...queue.pop()!);
|
||||
if (!wire) continue;
|
||||
wires.push(wire);
|
||||
for (const [face, direction] of wire.out) {
|
||||
const next = joined_cell(world, wire.x, wire.y, wire.z, face, direction);
|
||||
if (!next || seen.has(key(...next))) continue;
|
||||
seen.add(key(...next));
|
||||
queue.push(next);
|
||||
}
|
||||
}
|
||||
return wires;
|
||||
}
|
||||
|
||||
// the blocks a wire's faces lie on
|
||||
function below(wire: Wire) {
|
||||
return wire.faces.map((face) => ({ face, at: step(wire.x, wire.y, wire.z, face) }));
|
||||
}
|
||||
|
||||
// the power going into a wire: from each block it lies on, a source's output on that side or a solid block's power,
|
||||
// and from the blocks that aren't wire it joins
|
||||
export function wire_input(world: SignalReader, wire: Wire) {
|
||||
let most = 0;
|
||||
for (const { face, at } of below(wire)) {
|
||||
const id = world.get_block(...at);
|
||||
if (id !== undefined) {
|
||||
most = Math.max(most, output(world, ...at, id, OPPOSITE[face]), solid_power(world, ...at, id));
|
||||
}
|
||||
if (most === MAX_POWER) return most;
|
||||
}
|
||||
for (const [face, direction] of wire.out) {
|
||||
if (joins_out(world, wire.x, wire.y, wire.z, face, direction) !== "block") continue;
|
||||
const [nx, ny, nz] = step(wire.x, wire.y, wire.z, direction);
|
||||
most = Math.max(most, output(world, nx, ny, nz, world.get_block(nx, ny, nz)!, OPPOSITE[direction]));
|
||||
if (most === MAX_POWER) return most;
|
||||
}
|
||||
return most;
|
||||
}
|
||||
|
||||
// works out the power of the net the wire at x y z is in, and sets it on each of its wires that doesn't have it. then
|
||||
// each block the net gives power to is told once: the blocks its wires join, the blocks they lie on and the blocks
|
||||
// around those, which are powered through them
|
||||
export function update_net(world: SignalWorld, x: number, y: number, z: number) {
|
||||
const wires = walk(world, x, y, z);
|
||||
let power = 0;
|
||||
for (const wire of wires) {
|
||||
power = Math.max(power, wire_input(world, wire));
|
||||
if (power === MAX_POWER) break;
|
||||
}
|
||||
if (wires.every((wire) => wire.power === power)) return;
|
||||
for (const wire of wires) {
|
||||
if (wire.power !== power) world.set_power(wire.x, wire.y, wire.z, power);
|
||||
}
|
||||
const told = new Set(wires.map((wire) => key(wire.x, wire.y, wire.z)));
|
||||
const tell = (at: [number, number, number], from: { x: number; y: number; z: number }) => {
|
||||
if (told.has(key(...at))) return;
|
||||
told.add(key(...at));
|
||||
world.neighbor_changed(...at, from);
|
||||
};
|
||||
for (const wire of wires) {
|
||||
for (const [face, direction] of wire.out) {
|
||||
if (joins_out(world, wire.x, wire.y, wire.z, face, direction) === "block") {
|
||||
tell(step(wire.x, wire.y, wire.z, direction), wire);
|
||||
}
|
||||
}
|
||||
for (const { at } of below(wire)) {
|
||||
tell(at, wire);
|
||||
for (const face of FACES) tell(step(...at, face), { x: at[0], y: at[1], z: at[2] });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the blocks around the blocks a wire lies on, told when it's gone with power, since nothing else tells them they're
|
||||
// no longer powered through
|
||||
export function tell_around_below(world: SignalWorld, wire: Wire) {
|
||||
for (const { at } of below(wire)) {
|
||||
const from = { x: at[0], y: at[1], z: at[2] };
|
||||
world.neighbor_changed(...at, wire);
|
||||
for (const face of FACES) {
|
||||
const around = step(...at, face);
|
||||
if (around[0] !== wire.x || around[1] !== wire.y || around[2] !== wire.z) {
|
||||
world.neighbor_changed(...around, from);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user