From 31ae2973d711292ea342ff7e33ffcc5016bc66c4 Mon Sep 17 00:00:00 2001 From: Paula Date: Thu, 22 Oct 2026 20:07:00 -0300 Subject: [PATCH] Finish verdigris for now --- CREATE.md | 4 +- MODS.md | 1 + docs/modding/blocks/verdigris.md | 132 +++++++++++++++++++++++ mods/bworld/scripts/verdigris/devices.ts | 4 +- mods/bworld/scripts/verdigris/inputs.ts | 2 +- mods/bworld/scripts/verdigris/joins.ts | 8 +- mods/bworld/scripts/verdigris/server.ts | 7 +- mods/bworld/scripts/verdigris/shared.ts | 4 +- mods/bworld/scripts/verdigris/sides.ts | 2 +- mods/bworld/scripts/verdigris/signals.ts | 2 +- mods/bworld/scripts/verdigris/wires.ts | 12 +-- tests/verdigris_devices_test.ts | 2 +- tests/verdigris_wire_test.ts | 2 +- tools/verdigris_bench.ts | 17 ++- 14 files changed, 165 insertions(+), 34 deletions(-) create mode 100644 docs/modding/blocks/verdigris.md diff --git a/CREATE.md b/CREATE.md index 9b9ad84..effb71d 100644 --- a/CREATE.md +++ b/CREATE.md @@ -309,7 +309,7 @@ crushing, fan processing, steam, fluid splashes. Optional for gameplay, so it ca side) and goggles' overlay. A small API for drawing a label or an item at a point in the world, and a HUD element API, which is in bworld's types but throws today. Setting a scroll value is built, the way 0.5.0.j does it: a block with `"scrollable": true` gets the mouse wheel as `on_scroll`, and `send_message(text, { overlay: true })` shows the value -above the hotbar until the box can show it (see the pulse repeater in REDSTONE.md). +above the hotbar until the box can show it (see [Scrolling](docs/modding/blocks/components.md#scrolling)). **R11. 3D in GUIs** (P3). Drawing a model or block in a screen, turned and animated, for the recipe viewer's Create categories (a press pressing over its recipe, like `compat/jei`), goggles and ponder. @@ -584,7 +584,7 @@ Check: a steam engine on a tank boiler, fed by a pump from water and heated by b - S3, in `mods/bworld` first. - Redstone link, nixie tube, display link and display board, threshold switch, smart observer, analog lever, pulse extender and timer, powered latch and toggle latch, contact, desk bell, sequenced gearshift. The pulse repeater is - bworld's now, see REDSTONE.md. Clutch and gearshift switch to real signals. + bworld's now. Clutch and gearshift switch to real signals. Check: a factory that stops when a vault is full and shows its count on a display board. diff --git a/MODS.md b/MODS.md index 97196c0..76d5afc 100644 --- a/MODS.md +++ b/MODS.md @@ -40,6 +40,7 @@ deno task build && deno task server # packs the mods and starts a se show items - [Visuals](docs/modding/blocks/visuals.md): blocks drawn with code, like a shaft that turns - [Crops](docs/modding/blocks/crops.md): farming without writing code +- [Verdigris](docs/modding/blocks/verdigris.md): blocks that make and use signals, like levers, torches and doors **Items** diff --git a/docs/modding/blocks/verdigris.md b/docs/modding/blocks/verdigris.md new file mode 100644 index 0000000..1930938 --- /dev/null +++ b/docs/modding/blocks/verdigris.md @@ -0,0 +1,132 @@ +# Verdigris + +Verdigris is the base game's redstone: power from 0 to 15 that wire carries from the blocks that make it to the blocks +that use it. A block joins in by listing the base game's [components](components.md) in its JSON: +`bworld:verdigris_source` to give power and `bworld:verdigris_input` to take it. Neither needs code. What a block gives +or takes is one of its [states](states.md), so it's saved, sent to players and drawn with variants like any other state, +and no script calls another mod's. A block that does something when it's powered, like a door opening, reacts to its +input's state in its own component's `on_state_change`. + +## How Power Works + +Every block gives some power out of each of its 6 sides, 0 for most. + +- A **source** gives power out of some of its sides. Some of those are **strong**, and also power a solid block there. +- A [solid block](index.md#solid-blocks), like stone, makes no power, but it's powered by the strong sides pointing into + it and by wire lying on it, and gives that to the blocks around it. That's how a lever on one side of a wall opens a + door on the other. Wire lying on a solid block only takes what strong sides give it, not what other wire does, so a + wire can't keep itself on through the block it lies on. +- **Wire** carries power. Every wire joined to another is one net with one power, the most anything gives into any of + it, however long it is. It joins the sides of blocks that make or take power, and gives its power to the blocks it + lies on and the blocks it joins, not to ones it only runs past. +- An **input** takes power from each of its sides: what the block there gives out of that side, that block's power if + it's solid, or a wire's power when the wire lies on it or joins it. It's powered when any of them gives it more than + 0. + +## Sides + +Both components name a block's sides by face: `north`, `east`, `south`, `west`, `top` and `bottom`. A block that points +somewhere can say `facing`, and then also name `front`, `back` and `sides`, the four around the way it points. + +`facing` is a state's name. With 2 bits it's read as `north`, `east`, `south` and `west`, and with 3 bits as those, then +`top` and `bottom`. For a block on a floor, wall or ceiling, like a lever, it's +`{ "face": "face", "facing": "facing" }`: `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. Its `back` is the block it's on. + +## `bworld:verdigris_source` + +| Param | Default | Meaning | +| ---------- | ---------- | ------------------------------------------------------------------------- | +| `power` | | The power it gives, 0 to 15: a number, or the name of a state that has it | +| `when` | | A 1 bit state. It only gives power while that's 1 | +| `facing` | | Which way it points, see [Sides](#sides) | +| `out` | every side | The sides it gives power out of | +| `strong` | none | The sides that also power a solid block there | +| `connects` | `out` | The sides wire joins | + +Change the states it reads with `ctx.world.set_state` and the blocks beside it hear of it. The component tells the +blocks around the solid blocks its strong sides power, which aren't beside it. + +The verdigris block gives 15 out of every side, always: + +```json +"components": { "bworld:verdigris_source": { "power": 15 } } +``` + +The lever gives 15 while it's `powered`, out of every side, and strongly into the block it's on. Its own component flips +`powered` when it's right clicked: + +```json +"bworld:verdigris_source": { + "power": 15, + "when": "powered", + "facing": { "face": "face", "facing": "facing" }, + "strong": ["back"] +} +``` + +The torch gives power out of every side but the block it's on, and strongly into the block above it: + +```json +"bworld:verdigris_source": { + "power": 15, + "when": "lit", + "facing": "facing", + "out": ["front", "sides"], + "strong": ["top"] +} +``` + +## `bworld:verdigris_input` + +| Param | Default | Meaning | +| ---------- | ---------- | ------------------------------------------------------------- | +| `state` | | A 1 bit state it keeps whether it's powered in: 1 while it is | +| `facing` | | Which way it points, see [Sides](#sides) | +| `in` | every side | The sides it takes power from | +| `connects` | `in` | The sides wire joins | + +The component works out its input when the block is placed and whenever a block beside it changes, and sets the state, +which does nothing when it's already right. It's on or off: a state for how much power comes in isn't supported yet. + +A trapdoor lists it with a `powered` state: + +```json +"components": { + "bworld:trapdoor": {}, + "bworld:verdigris_input": { "state": "powered" } +} +``` + +and its server component opens it when `powered` changes, so one opened or closed by hand stays that way until the power +does: + +```ts +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); + }, +}); +``` + +## Both + +A block can list both, like the pulse repeater, which takes power from behind it and gives it out of its front. Its +component sets `powering` with a [scheduled tick](components.md#scheduled-ticks) once its delay is up: + +```json +"bworld:verdigris_source": { + "power": 15, + "when": "powering", + "facing": "facing", + "out": ["front"], + "strong": ["front"], + "connects": ["front", "back"] +}, +"bworld:verdigris_input": { "state": "powered", "facing": "facing", "in": ["back"] } +``` + +Wire joins a side when either component's `connects` names it. A block whose output leads back to its own input, like an +inverter wired to itself, flips back and forth until the [neighbor update limit](components.md#changes) stops it, unless +it waits a scheduled tick between flips. The torch waits 2 ticks, and burns out for a while when it flips too often. diff --git a/mods/bworld/scripts/verdigris/devices.ts b/mods/bworld/scripts/verdigris/devices.ts index f818ddf..b6390ac 100644 --- a/mods/bworld/scripts/verdigris/devices.ts +++ b/mods/bworld/scripts/verdigris/devices.ts @@ -1,8 +1,8 @@ // 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 +// the blocks that make and use signals. 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 } from "./inputs.ts"; import type { SignalReader } from "./signals.ts"; diff --git a/mods/bworld/scripts/verdigris/inputs.ts b/mods/bworld/scripts/verdigris/inputs.ts index 7e6895e..6097528 100644 --- a/mods/bworld/scripts/verdigris/inputs.ts +++ b/mods/bworld/scripts/verdigris/inputs.ts @@ -1,7 +1,7 @@ // 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 +// the power blocks take in, see docs/modding/blocks/verdigris.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"; diff --git a/mods/bworld/scripts/verdigris/joins.ts b/mods/bworld/scripts/verdigris/joins.ts index 7e2e6d2..9dbc2ae 100644 --- a/mods/bworld/scripts/verdigris/joins.ts +++ b/mods/bworld/scripts/verdigris/joins.ts @@ -1,10 +1,10 @@ // 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 +// which faces of its cell a wire covers and what it joins. 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"; diff --git a/mods/bworld/scripts/verdigris/server.ts b/mods/bworld/scripts/verdigris/server.ts index 9a67166..bcd98ab 100644 --- a/mods/bworld/scripts/verdigris/server.ts +++ b/mods/bworld/scripts/verdigris/server.ts @@ -1,8 +1,7 @@ // 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, -// inputs, and the devices +// verdigris's components: 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_pulse_repeater, register_torch } from "./devices.ts"; import { arms_of, around_edge, can_cover, covers, face_states, faces_of, MAX_ARMS } from "./joins.ts"; @@ -87,8 +86,8 @@ function set_input(ctx: ServerContext, world: SignalReader, block: BlockRef, par 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 +// 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) { diff --git a/mods/bworld/scripts/verdigris/shared.ts b/mods/bworld/scripts/verdigris/shared.ts index e937a4d..23670fc 100644 --- a/mods/bworld/scripts/verdigris/shared.ts +++ b/mods/bworld/scripts/verdigris/shared.ts @@ -1,8 +1,8 @@ // 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 +// verdigris placement and joins. 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, type InputParams, params_lookup, type SourceParams } from "./signals.ts"; diff --git a/mods/bworld/scripts/verdigris/sides.ts b/mods/bworld/scripts/verdigris/sides.ts index 20a3030..224799e 100644 --- a/mods/bworld/scripts/verdigris/sides.ts +++ b/mods/bworld/scripts/verdigris/sides.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later -// faces, and the directions along them a wire lying on one runs, see REDSTONE.md +// faces, and the directions along them a wire lying on one runs import type { Face } from "bworld/shared"; // the six faces in the order of a 3 bit facing state's values: a 2 bit facing's four, then top and bottom diff --git a/mods/bworld/scripts/verdigris/signals.ts b/mods/bworld/scripts/verdigris/signals.ts index 821df77..ef0b8ce 100644 --- a/mods/bworld/scripts/verdigris/signals.ts +++ b/mods/bworld/scripts/verdigris/signals.ts @@ -1,7 +1,7 @@ // 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 +// the power blocks give each other, see docs/modding/blocks/verdigris.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"; diff --git a/mods/bworld/scripts/verdigris/wires.ts b/mods/bworld/scripts/verdigris/wires.ts index 5d73db1..f7d31a0 100644 --- a/mods/bworld/scripts/verdigris/wires.ts +++ b/mods/bworld/scripts/verdigris/wires.ts @@ -1,9 +1,9 @@ // 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. NetIndex keeps the nets between changes, see "The Net Index" +// wire nets: 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. NetIndex keeps the nets between changes import type { Face } from "bworld/shared"; import { cell_through, faces_of, joins_out, ways_out } from "./joins.ts"; import { type BlockReader, MAX_POWER, output, type SignalReader, solid_power } from "./signals.ts"; @@ -77,9 +77,9 @@ function most(inputs: Map) { } // the nets of the wire cells anything has touched, kept between changes so most changes cost a few reads instead of a -// walk, see "The Net Index" in REDSTONE.md. it's only in memory, and a cell it doesn't have is walked the first time -// anything touches it, so it can't hold anything the world doesn't. the wire's server hooks call created, breaking, -// reshaped and beside_changed +// the nets of the wire cells anything has touched, kept between changes so most changes cost a few reads instead of a +// walk. it's only in memory, and a cell it doesn't have is walked the first time anything touches it, so it can't hold +// anything the world doesn't. the wire's server hooks call created, breaking, reshaped and beside_changed export class NetIndex { readonly #world: SignalWorld; // each cell's net, by packed position diff --git a/tests/verdigris_devices_test.ts b/tests/verdigris_devices_test.ts index 74fb6a5..61d13b7 100644 --- a/tests/verdigris_devices_test.ts +++ b/tests/verdigris_devices_test.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later -// the blocks that make and use verdigris signals, see "Blocks" in REDSTONE.md +// the blocks that make and use verdigris signals import { assert, assertAlmostEquals, assertEquals } from "@std/assert"; import { bake_block, type BakedQuad, type ModelJson } from "$/common/block_models.ts"; import { block_shape } from "$/common/block_shapes.ts"; diff --git a/tests/verdigris_wire_test.ts b/tests/verdigris_wire_test.ts index 411cbfc..06f708b 100644 --- a/tests/verdigris_wire_test.ts +++ b/tests/verdigris_wire_test.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later -// verdigris wire, see REDSTONE.md: how it's drawn on the faces of its cell, how it joins, and its nets' power +// verdigris wire: how it's drawn on the faces of its cell, how it joins, and its nets' power import { assert, assertAlmostEquals, assertEquals } from "@std/assert"; import { Alea } from "@paulaboks/rng"; import { bake_block, type BakedQuad, FACE_INDEX, type ModelJson } from "$/common/block_models.ts"; diff --git a/tools/verdigris_bench.ts b/tools/verdigris_bench.ts index 9df10f6..01a87ef 100644 --- a/tools/verdigris_bench.ts +++ b/tools/verdigris_bench.ts @@ -1,15 +1,14 @@ // SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later -// how long verdigris takes on a stone floor, see "Performance" in REDSTONE.md, and how many set_block messages a -// player watching would get: placing a 1000 wire zigzag one wire at a time, a lever switching it, a block placed and -// taken away beside it while it's powered, a wire broken out of its middle and put back, a lever switching a 64×64 -// grid, and a lever switching a tower of 50 torches. with the net index a wire took 0.5 ms to place however big its -// net, a block beside the powered zigzag 0.1 ms and no wire messages, and switching took 15 to 20 ms for the zigzag and -// 50 to 60 ms for the grid, about 15 µs a wire. walking the net on every change, a wire took 30 ms to place in the -// zigzag and 400 ms in the grid, a block beside 20 ms, and switching 35 to 45 ms and 135 ms. the tower's 50 torches -// changing over 102 ticks took about 3 ms more than the ticks did with nothing switched. repeater clocks come with -// repeaters +// how long verdigris takes on a stone floor, and how many set_block messages a player watching would get: placing a +// 1000 wire zigzag one wire at a time, a lever switching it, a block placed and taken away beside it while it's +// powered, a wire broken out of its middle and put back, a lever switching a 64×64 grid, and a lever switching a tower +// of 50 torches. with the net index a wire took 0.5 ms to place however big its net, a block beside the powered zigzag +// 0.1 ms and no wire messages, and switching took 15 to 20 ms for the zigzag and 50 to 60 ms for the grid, about 15 µs +// a wire. walking the net on every change, a wire took 30 ms to place in the zigzag and 400 ms in the grid, a block +// beside 20 ms, and switching 35 to 45 ms and 135 ms. the tower's 50 torches changing over 102 ticks took about 3 ms +// more than the ticks did with nothing switched. repeater clocks come with repeaters // deno run -A tools/verdigris_bench.ts import type { Face } from "../common/mod_api/shared.ts"; import { AIR_ID } from "../common/protocol.ts";