Verdigris torch

This commit is contained in:
2026-10-22 16:31:09 -03:00
parent db0589d965
commit 20e6ac6fbb
12 changed files with 500 additions and 73 deletions
+34
View File
@@ -0,0 +1,34 @@
{
"format_version": 1,
"block": {
"id": "bworld:verdigris_torch",
"textures": { "torch": "bworld:verdigris_torch" },
"model": "bworld:verdigris_torch",
"render_layer": "cutout",
"collision": false,
"shape": [[6, 0, 6, 10, 11, 10]],
"mining": { "toughness": 0 },
"drops": "bworld:verdigris_torch",
"states": [
{ "name": "facing", "bits": 3, "default": 4 },
{ "name": "lit", "bits": 1, "default": 1 }
],
"variants": {
"lit=0": { "textures": { "torch": "bworld:verdigris_torch_off" } },
"facing=0": { "model": "bworld:verdigris_wall_torch", "shape": [[6, 3, 11, 10, 14, 16]] },
"facing=1": { "model": "bworld:verdigris_wall_torch", "y": 90, "shape": [[6, 3, 11, 10, 14, 16]] },
"facing=2": { "model": "bworld:verdigris_wall_torch", "y": 180, "shape": [[6, 3, 11, 10, 14, 16]] },
"facing=3": { "model": "bworld:verdigris_wall_torch", "y": 270, "shape": [[6, 3, 11, 10, 14, 16]] }
},
"components": {
"bworld:verdigris_source": {
"power": 15,
"when": "lit",
"facing": "facing",
"out": ["front", "sides"],
"strong": ["top"]
},
"bworld:verdigris_torch": {}
}
}
}
+35
View File
@@ -0,0 +1,35 @@
{
"format_version": 1,
"model": {
"id": "bworld:verdigris_torch",
"elements": [
{
"from": [7, 0, 7],
"to": [9, 10, 9],
"shade": false,
"faces": {
"top": { "texture": "#torch", "uv": [7, 6, 9, 8] },
"bottom": { "texture": "#torch", "uv": [7, 14, 9, 16] }
}
},
{
"from": [7, 0, 0],
"to": [9, 16, 16],
"shade": false,
"faces": {
"west": { "texture": "#torch", "uv": [0, 0, 16, 16] },
"east": { "texture": "#torch", "uv": [0, 0, 16, 16] }
}
},
{
"from": [0, 0, 7],
"to": [16, 16, 9],
"shade": false,
"faces": {
"north": { "texture": "#torch", "uv": [0, 0, 16, 16] },
"south": { "texture": "#torch", "uv": [0, 0, 16, 16] }
}
}
]
}
}
@@ -0,0 +1,38 @@
{
"format_version": 1,
"model": {
"id": "bworld:verdigris_wall_torch",
"elements": [
{
"from": [7, 3, 15],
"to": [9, 13, 17],
"rotation": { "origin": [8, 3, 16], "axis": "x", "angle": -22.5 },
"shade": false,
"faces": {
"top": { "texture": "#torch", "uv": [7, 6, 9, 8] },
"bottom": { "texture": "#torch", "uv": [7, 14, 9, 16] }
}
},
{
"from": [7, 3, 8],
"to": [9, 19, 24],
"rotation": { "origin": [8, 3, 16], "axis": "x", "angle": -22.5 },
"shade": false,
"faces": {
"west": { "texture": "#torch", "uv": [0, 0, 16, 16] },
"east": { "texture": "#torch", "uv": [0, 0, 16, 16] }
}
},
{
"from": [0, 3, 15],
"to": [16, 19, 17],
"rotation": { "origin": [8, 3, 16], "axis": "x", "angle": -22.5 },
"shade": false,
"faces": {
"north": { "texture": "#torch", "uv": [0, 0, 16, 16] },
"south": { "texture": "#torch", "uv": [0, 0, 16, 16] }
}
}
]
}
}
+75 -4
View File
@@ -4,11 +4,20 @@
// 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";
import { power_from, type SignalReader } from "./signals.ts";
import { type FacingParam, front_of, OPPOSITE, pack, step } from "./sides.ts";
// a lever's face and facing states, see FacingParam
const LEVER_FACING: FacingParam = { face: "face", facing: "facing" };
// how many ticks a torch takes to go out or light after the block it's on changes
const TORCH_DELAY = 2;
// a torch that goes out more than TORCH_BURNOUT times in TORCH_BURNOUT_WINDOW ticks stays out for TORCH_BURNOUT_TICKS,
// so a fast torch clock can't cost the server much
const TORCH_BURNOUT = 8;
const TORCH_BURNOUT_WINDOW = 60;
const TORCH_BURNOUT_TICKS = 160;
// 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", {
@@ -24,10 +33,72 @@ export function register_lever(ctx: ServerContext) {
});
}
// out while the block it's on is powered, lit while it isn't, TORCH_DELAY ticks after that changes, and burnt out for a
// while when that's too often. which ticks it went out on and how long it's burnt out for are only kept in memory, so a
// restart forgets them
export function register_torch(ctx: ServerContext, world: SignalReader) {
const went_out = new Map<number, number[]>();
const burnt_until = new Map<number, number>();
const back = (block: Position) =>
OPPOSITE[front_of("facing", (name) => ctx.world.get_state(block.x, block.y, block.z, name))];
const powered = (block: Position) => power_from(world, block.x, block.y, block.z, back(block)) > 0;
// it's lit while powered or out while not, so it changes TORCH_DELAY ticks from now, unless it already will
const check = (block: Position) => {
const lit = ctx.world.get_state(block.x, block.y, block.z, "lit") === 1;
if (lit === powered(block) && !ctx.world.has_scheduled_tick(block.x, block.y, block.z)) {
ctx.world.schedule_tick(block.x, block.y, block.z, TORCH_DELAY);
}
};
// the ticks the torch at key went out on lately, without the ones too long ago to count
const lately = (key: number, now: number) =>
(went_out.get(key) ?? []).filter((tick) => tick > now - TORCH_BURNOUT_WINDOW);
ctx.components.register_block("bworld:verdigris_torch", {
on_create: check,
on_break(block) {
const key = pack(block.x, block.y, block.z);
went_out.delete(key);
burnt_until.delete(key);
},
on_neighbor_change(block, _params, neighbor) {
if (!breaks_without(ctx, block, back(block), neighbor)) check(block);
},
on_scheduled_tick(block) {
const key = pack(block.x, block.y, block.z);
const now = ctx.system.current_tick;
const lit = ctx.world.get_state(block.x, block.y, block.z, "lit") === 1;
if (lit && powered(block)) {
ctx.world.set_state(block.x, block.y, block.z, "lit", 0);
const ticks = [...lately(key, now), now];
went_out.set(key, ticks);
if (ticks.length > TORCH_BURNOUT) {
burnt_until.set(key, now + TORCH_BURNOUT_TICKS);
ctx.world.schedule_tick(block.x, block.y, block.z, TORCH_BURNOUT_TICKS);
}
} else if (!lit && !powered(block)) {
// a block has one tick waiting at most, so when going out set off a sooner one, the one that ends the
// burnout was dropped, and this one waits out the rest
const until = burnt_until.get(key) ?? now;
if (until > now) {
ctx.world.schedule_tick(block.x, block.y, block.z, until - now);
return;
}
burnt_until.delete(key);
const ticks = lately(key, now);
if (ticks.length > 0) went_out.set(key, ticks);
else went_out.delete(key);
ctx.world.set_state(block.x, block.y, block.z, "lit", 1);
}
},
});
}
// 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
// something to attach to anymore. whether it broke
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);
if (neighbor.x !== x || neighbor.y !== y || neighbor.z !== z) return false;
if (ctx.world.is_face_sturdy(x, y, z, OPPOSITE[face])) return false;
ctx.world.break_block(block.x, block.y, block.z);
return true;
}
+2 -1
View File
@@ -4,7 +4,7 @@
// verdigris's components, see REDSTONE.md: the wire, which keeps its net's power through the net index, sources, and
// the devices
import type { BlockRef, Face, Position, ServerContext } from "bworld/server";
import { register_lever } 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 { type BlockReader, source_lookup, strong_sides } from "./signals.ts";
import { DIRECTIONS, FACES, OPPOSITE, step } from "./sides.ts";
@@ -63,6 +63,7 @@ export function register_verdigris(ctx: ServerContext) {
});
register_lever(ctx);
register_torch(ctx, world);
}
// 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
+11 -2
View File
@@ -42,10 +42,19 @@ export function register_verdigris_rules(ctx: SharedContext) {
return { face: 1, facing: FACES.indexOf(OPPOSITE[side]) };
},
});
// on a floor pointing up, or on a wall pointing out of it. not under a ceiling
ctx.components.register_block("bworld:verdigris_torch", {
state_for_placement(placement) {
const side = hangs_from(ctx, placement);
if (!side || side === "top") return false;
return { 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
// the face toward the block a wire, lever or torch 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);
+23
View File
@@ -113,3 +113,26 @@ export function solid_power(world: SignalReader, x: number, y: number, z: number
}
return most;
}
// the power a solid block has for anything but wire: solid_power, and the power of the wire lying on it
export function block_power(world: SignalReader, x: number, y: number, z: number, id: Id) {
if (!world.solid(id)) return 0;
let most = solid_power(world, x, y, z, id);
for (const face of FACES) {
if (most === MAX_POWER) break;
const [nx, ny, nz] = step(x, y, z, face);
if (world.get_block(nx, ny, nz) === WIRE && world.get_state(nx, ny, nz, OPPOSITE[face]) === 1) {
most = Math.max(most, world.get_state(nx, ny, nz, "power") ?? 0);
}
}
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));
}