Verdigris torch
This commit is contained in:
+3
-1
@@ -267,7 +267,9 @@ export function client_of(game: GameServer, area: Area) {
|
||||
// what game_mode does when this client places held against face of x y z, at hit
|
||||
place(id: string, x: number, y: number, z: number, yaw: number, face: Face, hit: [number, number, number]) {
|
||||
const value = rules.placement_value(info(id)!, yaw, 0, { x, y, z, face, hit, sneaking: false });
|
||||
copy.set(`${x} ${y} ${z}`, [id, value! >>> 16]);
|
||||
// its rules say it can't go there
|
||||
if (value === undefined) return;
|
||||
copy.set(`${x} ${y} ${z}`, [id, value >>> 16]);
|
||||
rules.notify_neighbors(x, y, z);
|
||||
},
|
||||
// what game_mode does when this client right clicks face of x y z, at hit. whether the rules did something
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// what the verdigris tests build with: a player on a stone floor placing things, and the world read the way the
|
||||
// verdigris scripts read it
|
||||
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { Face } from "$/common/mod_api/shared.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { block_states } from "$/common/utils.ts";
|
||||
import type { WorldSave } from "$/server/game/saves.ts";
|
||||
import { joined_cell } from "$/mods/bworld/scripts/verdigris/joins.ts";
|
||||
import type { BlockReader } from "$/mods/bworld/scripts/verdigris/signals.ts";
|
||||
import { FACES } from "$/mods/bworld/scripts/verdigris/sides.ts";
|
||||
import { wire_at } from "$/mods/bworld/scripts/verdigris/wires.ts";
|
||||
import { type Area, test_game } from "./helpers.ts";
|
||||
|
||||
export type Pos = [number, number, number];
|
||||
export const AREA: Area = { x: [-6, 6], y: [99, 104], z: [-6, 6] };
|
||||
|
||||
// alice on a stone floor at y 99, with air above it to build in
|
||||
export async function builder(save?: WorldSave) {
|
||||
const t = await test_game("mods", save);
|
||||
t.join(1, "alice");
|
||||
if (!save) {
|
||||
for (let x = AREA.x[0]; x <= AREA.x[1]; x++) {
|
||||
for (let z = AREA.z[0]; z <= AREA.z[1]; z++) {
|
||||
t.game.set_block(x, 99, z, "bworld:stone");
|
||||
for (let y = 100; y <= AREA.y[1]; y++) t.game.set_block(x, y, z, AIR_ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
||||
const player = t.game.players()[0];
|
||||
// alice uses id, or whatever she's holding, on face of the block at x y z, from right by it
|
||||
const use = (id: string | undefined, x: number, y: number, z: number, face: Face) => {
|
||||
if (id) {
|
||||
t.send(1, { type: "chat", text: `/give ${id}` });
|
||||
const slot = player.inventory.to_synced_data().findIndex((item) => item?.id === id);
|
||||
t.send(1, { type: "select_slot", slot });
|
||||
}
|
||||
t.game.teleport(player, x + 0.5, y + 1, z + 0.5);
|
||||
t.send(1, { type: "use_block", x, y, z, face, hit: [0.5, 0.5, 0.5] });
|
||||
};
|
||||
const states = (x: number, y: number, z: number) =>
|
||||
block_states(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!);
|
||||
const id = (x: number, y: number, z: number) => t.game.world.get_block_id(x, y, z);
|
||||
const power = (...at: Pos) => states(...at).power;
|
||||
// the world the way the wire's own code reads it
|
||||
const reader: BlockReader = {
|
||||
get_block: id,
|
||||
get_state: (x, y, z, name) => states(x, y, z)[name],
|
||||
source: (block) =>
|
||||
EverythingRegistry.get<BlockRegistry>("blocks", block)?.components?.["bworld:verdigris_source"] as never,
|
||||
};
|
||||
const covers = (...at: Pos) => FACES.filter((face) => states(...at)[face] === 1);
|
||||
// the wire cells the wire at x y z joins
|
||||
const joins = (...at: Pos) =>
|
||||
(wire_at(reader, ...at)?.out ?? []).flatMap(([face, direction]) => {
|
||||
const cell = joined_cell(reader, ...at, face, direction);
|
||||
return cell ? [cell.join(" ")] : [];
|
||||
});
|
||||
return { t, use, states, id, power, covers, joins, reader };
|
||||
}
|
||||
|
||||
export type Builder = Awaited<ReturnType<typeof builder>>;
|
||||
@@ -0,0 +1,172 @@
|
||||
// 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
|
||||
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";
|
||||
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { Face } from "$/common/mod_api/shared.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { default_block_value, set_state_value } from "$/common/utils.ts";
|
||||
import { FACE_OFFSETS, FACES, OPPOSITE, step } from "$/mods/bworld/scripts/verdigris/sides.ts";
|
||||
import { client_of, server_blocks, test_game } from "./helpers.ts";
|
||||
import { AREA, type Builder, builder, type Pos } from "./verdigris.ts";
|
||||
|
||||
const TORCH = "bworld:verdigris_torch";
|
||||
|
||||
// how far toward face a point in the block is: 0 on the far side of the block, 1 on that face
|
||||
function toward(point: readonly number[], face: Face) {
|
||||
const offset = FACE_OFFSETS[face];
|
||||
return 0.5 + (point[0] - 0.5) * offset[0] + (point[1] - 0.5) * offset[1] + (point[2] - 0.5) * offset[2];
|
||||
}
|
||||
|
||||
// the middle of a quad
|
||||
function middle(quad: BakedQuad) {
|
||||
return [0, 1, 2].map((axis) =>
|
||||
(quad.positions[axis] + quad.positions[3 + axis] + quad.positions[6 + axis] +
|
||||
quad.positions[9 + axis]) / 4
|
||||
);
|
||||
}
|
||||
|
||||
Deno.test("a torch stands up on a floor, and leans out of a wall from the bottom of the face it's on", async () => {
|
||||
await test_game("mods");
|
||||
const block = EverythingRegistry.get<BlockRegistry>("blocks", TORCH)!;
|
||||
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
|
||||
for (const facing of [0, 1, 2, 3, 4]) {
|
||||
const quads = bake_block(
|
||||
block,
|
||||
set_state_value(default_block_value(0, block), block, "facing", facing)!,
|
||||
models,
|
||||
);
|
||||
// the ends of its stick, by the part of the texture they show: the middle two columns, from row v down 2 rows
|
||||
const end = (v: number) =>
|
||||
middle(
|
||||
quads.find((quad) =>
|
||||
[0, 1, 2, 3].every((k) =>
|
||||
quad.uvs[k * 2] >= 7 && quad.uvs[k * 2] <= 9 && quad.uvs[k * 2 + 1] >= v &&
|
||||
quad.uvs[k * 2 + 1] <= v + 2
|
||||
)
|
||||
)!,
|
||||
);
|
||||
const [top, bottom] = [end(6), end(14)];
|
||||
assert(top[1] > bottom[1], `${facing} points up`);
|
||||
const shape = block_shape(block, { facing, lit: 1 });
|
||||
assertEquals(shape.length, 1);
|
||||
if (facing === 4) {
|
||||
assertAlmostEquals(top[0], bottom[0], 1e-6);
|
||||
assertAlmostEquals(top[2], bottom[2], 1e-6);
|
||||
assertEquals([shape[0][1], shape[0][0] + shape[0][3], shape[0][2] + shape[0][5]], [0, 1, 1]);
|
||||
continue;
|
||||
}
|
||||
const front = FACES[facing];
|
||||
assert(toward(top, front) > toward(bottom, front) + 0.1, `${front} leans out`);
|
||||
assertAlmostEquals(toward(bottom, OPPOSITE[front]), 1, 1e-6, `${front} stands on the wall behind it`);
|
||||
assertAlmostEquals(Math.max(toward(shape[0], OPPOSITE[front]), toward(shape[0].slice(3), OPPOSITE[front])), 1);
|
||||
}
|
||||
});
|
||||
|
||||
// a stone block at x 100 z with a torch on top, a stone block over the torch, and a torch on top of that
|
||||
async function tower(x: number, z: number) {
|
||||
const b = await builder();
|
||||
for (const y of [100, 102]) {
|
||||
b.t.game.set_block(x, y, z, "bworld:stone");
|
||||
b.use(TORCH, x, y, z, "top");
|
||||
}
|
||||
run(b, 10);
|
||||
return { ...b, lower: [x, 101, z] as Pos, upper: [x, 103, z] as Pos };
|
||||
}
|
||||
|
||||
function run(b: Builder, ticks: number) {
|
||||
for (let i = 0; i < ticks; i++) b.t.game.tick();
|
||||
}
|
||||
|
||||
const lit = (b: Builder, at: Pos) => b.states(...at).lit === 1;
|
||||
|
||||
Deno.test("a torch goes out 2 ticks after the block it's on is powered, and lights 2 ticks after it isn't", async () => {
|
||||
const b = await tower(0, 0);
|
||||
// the lower torch powers the block the upper one is on
|
||||
assertEquals([lit(b, b.lower), lit(b, b.upper)], [true, false]);
|
||||
b.use("bworld:lever", 0, 100, 0, "east");
|
||||
b.use(undefined, 1, 100, 0, "top");
|
||||
run(b, 1);
|
||||
assertEquals([lit(b, b.lower), lit(b, b.upper)], [true, false]);
|
||||
run(b, 1);
|
||||
assertEquals([lit(b, b.lower), lit(b, b.upper)], [false, false]);
|
||||
run(b, 2);
|
||||
assertEquals([lit(b, b.lower), lit(b, b.upper)], [false, true]);
|
||||
b.use(undefined, 1, 100, 0, "top");
|
||||
run(b, 2);
|
||||
assertEquals(lit(b, b.lower), true);
|
||||
run(b, 2);
|
||||
assertEquals(lit(b, b.upper), false);
|
||||
});
|
||||
|
||||
Deno.test("a lit torch powers wire beside it and the block above it, but not the block it's on", async () => {
|
||||
const b = await tower(0, 0);
|
||||
// wire beside the lower torch, on the side of the block it's on, and on top of a block over the upper torch
|
||||
b.t.game.set_block(1, 100, 0, "bworld:stone");
|
||||
b.use("bworld:verdigris", 1, 100, 0, "top");
|
||||
b.use("bworld:verdigris", 0, 100, 0, "north");
|
||||
b.t.game.set_block(0, 104, 0, "bworld:stone");
|
||||
b.use("bworld:verdigris", 0, 104, 0, "top");
|
||||
const wires: Pos[] = [[1, 101, 0], [0, 100, -1], [0, 105, 0]];
|
||||
assertEquals(wires.map((at) => b.power(...at)), [15, 0, 0]);
|
||||
// a lever powering the block the lower torch is on puts it out, which lights the upper one
|
||||
b.use("bworld:lever", 0, 100, 0, "west");
|
||||
b.use(undefined, -1, 100, 0, "top");
|
||||
run(b, 4);
|
||||
assertEquals([lit(b, b.lower), lit(b, b.upper)], [false, true]);
|
||||
assertEquals(wires.map((at) => b.power(...at)), [0, 15, 15]);
|
||||
});
|
||||
|
||||
Deno.test("a torch that powers the block it's on blinks, until it burns out for a while", async () => {
|
||||
const b = await builder();
|
||||
// a torch on the side of a block, with a block over it that it powers. wire on that block's side and on top of the
|
||||
// block the torch is on powers that block back
|
||||
b.t.game.set_block(0, 100, 0, "bworld:stone");
|
||||
b.t.game.set_block(1, 101, 0, "bworld:stone");
|
||||
b.use("bworld:verdigris", 0, 100, 0, "top");
|
||||
b.use("bworld:verdigris", 1, 101, 0, "west");
|
||||
b.use(TORCH, 0, 100, 0, "east");
|
||||
const torch: Pos = [1, 100, 0];
|
||||
assertEquals(b.id(...torch), TORCH);
|
||||
const seen: boolean[] = [];
|
||||
for (let i = 0; i < 400; i++) {
|
||||
b.t.game.tick();
|
||||
seen.push(lit(b, torch));
|
||||
}
|
||||
const changes = seen.filter((on, i) => i > 0 && on !== seen[i - 1]).length;
|
||||
assert(seen.slice(0, 20).includes(true) && seen.slice(0, 20).includes(false), "it blinks");
|
||||
// the longest it stayed out
|
||||
let longest = 0;
|
||||
let run_out = 0;
|
||||
for (const on of seen) {
|
||||
run_out = on ? 0 : run_out + 1;
|
||||
longest = Math.max(longest, run_out);
|
||||
}
|
||||
assert(longest > 100, `it burnt out, longest out ${longest}`);
|
||||
assert(seen.lastIndexOf(true) > seen.indexOf(false) + longest, "and lit again");
|
||||
assert(changes < 400 / 4, "it blinked less than it would without burning out");
|
||||
});
|
||||
|
||||
Deno.test("a torch goes on a floor or a wall but not under a ceiling, and breaks when what it's on goes", async () => {
|
||||
const b = await builder();
|
||||
b.t.game.set_block(3, 101, 3, "bworld:stone");
|
||||
// a player sees it the way the server places it
|
||||
const client = client_of(b.t.game, AREA);
|
||||
for (const face of ["top", "south", "bottom"] as const) {
|
||||
client.place(TORCH, ...step(3, 101, 3, face), 0, face, [0.5, 0.5, 0.5]);
|
||||
b.use(TORCH, 3, 101, 3, face);
|
||||
}
|
||||
assertEquals(client.blocks(), server_blocks(b.t.game, AREA));
|
||||
assertEquals([b.states(3, 102, 3).facing, b.states(3, 101, 4).facing], [
|
||||
FACES.indexOf("top"),
|
||||
FACES.indexOf("south"),
|
||||
]);
|
||||
assertEquals(b.id(3, 100, 3), AIR_ID);
|
||||
b.t.game.set_block(3, 101, 3, AIR_ID);
|
||||
assertEquals([b.id(3, 102, 3), b.id(3, 101, 4)], [AIR_ID, AIR_ID]);
|
||||
const dropped = b.t.game.item_entities().filter((e) => e.item.type_id === TORCH).length;
|
||||
assertEquals(dropped, 2);
|
||||
});
|
||||
@@ -9,13 +9,13 @@ import { block_shape, is_solid } from "$/common/block_shapes.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { Face } from "$/common/mod_api/shared.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { block_states, default_block_value, set_state_value } from "$/common/utils.ts";
|
||||
import { default_block_value, set_state_value } from "$/common/utils.ts";
|
||||
import type { SavedChunk, WorldSave } from "$/server/game/saves.ts";
|
||||
import { can_cover, joined_cell, ways_out } from "$/mods/bworld/scripts/verdigris/joins.ts";
|
||||
import type { BlockReader } from "$/mods/bworld/scripts/verdigris/signals.ts";
|
||||
import { can_cover, ways_out } from "$/mods/bworld/scripts/verdigris/joins.ts";
|
||||
import { DIRECTIONS, FACE_OFFSETS, FACES, OPPOSITE, step } from "$/mods/bworld/scripts/verdigris/sides.ts";
|
||||
import { NetIndex, wire_at } from "$/mods/bworld/scripts/verdigris/wires.ts";
|
||||
import { type Area, client_of, server_blocks, test_game } from "./helpers.ts";
|
||||
import { NetIndex } from "$/mods/bworld/scripts/verdigris/wires.ts";
|
||||
import { client_of, server_blocks, test_game } from "./helpers.ts";
|
||||
import { AREA, type Builder, builder, type Pos } from "./verdigris.ts";
|
||||
|
||||
// how far it stands off the face it lies on, in blocks
|
||||
const HEIGHT = 0.25 / 16;
|
||||
@@ -172,56 +172,6 @@ Deno.test("a wire shows its powered texture while it has power, and can be point
|
||||
}
|
||||
});
|
||||
|
||||
type Pos = [number, number, number];
|
||||
const AREA: Area = { x: [-6, 6], y: [99, 104], z: [-6, 6] };
|
||||
|
||||
// alice on a stone floor at y 99, with air above it to build in
|
||||
async function builder(save?: WorldSave) {
|
||||
const t = await test_game("mods", save);
|
||||
t.join(1, "alice");
|
||||
if (!save) {
|
||||
for (let x = AREA.x[0]; x <= AREA.x[1]; x++) {
|
||||
for (let z = AREA.z[0]; z <= AREA.z[1]; z++) {
|
||||
t.game.set_block(x, 99, z, "bworld:stone");
|
||||
for (let y = 100; y <= AREA.y[1]; y++) t.game.set_block(x, y, z, AIR_ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
||||
const player = t.game.players()[0];
|
||||
// alice uses id, or whatever she's holding, on face of the block at x y z, from right by it
|
||||
const use = (id: string | undefined, x: number, y: number, z: number, face: Face) => {
|
||||
if (id) {
|
||||
t.send(1, { type: "chat", text: `/give ${id}` });
|
||||
const slot = player.inventory.to_synced_data().findIndex((item) => item?.id === id);
|
||||
t.send(1, { type: "select_slot", slot });
|
||||
}
|
||||
t.game.teleport(player, x + 0.5, y + 1, z + 0.5);
|
||||
t.send(1, { type: "use_block", x, y, z, face, hit: [0.5, 0.5, 0.5] });
|
||||
};
|
||||
const states = (x: number, y: number, z: number) =>
|
||||
block_states(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!);
|
||||
const id = (x: number, y: number, z: number) => t.game.world.get_block_id(x, y, z);
|
||||
const power = (...at: Pos) => states(...at).power;
|
||||
// the world the way the wire's own code reads it
|
||||
const reader: BlockReader = {
|
||||
get_block: id,
|
||||
get_state: (x, y, z, name) => states(x, y, z)[name],
|
||||
source: (block) =>
|
||||
EverythingRegistry.get<BlockRegistry>("blocks", block)?.components?.["bworld:verdigris_source"] as never,
|
||||
};
|
||||
const covers = (...at: Pos) => FACES.filter((face) => states(...at)[face] === 1);
|
||||
// the wire cells the wire at x y z joins
|
||||
const joins = (...at: Pos) =>
|
||||
(wire_at(reader, ...at)?.out ?? []).flatMap(([face, direction]) => {
|
||||
const cell = joined_cell(reader, ...at, face, direction);
|
||||
return cell ? [cell.join(" ")] : [];
|
||||
});
|
||||
return { t, use, states, id, power, covers, joins, reader };
|
||||
}
|
||||
|
||||
type Builder = Awaited<ReturnType<typeof builder>>;
|
||||
|
||||
// every wire cell these join joins them back
|
||||
function joined_both_ways(b: Builder, wires: Pos[]) {
|
||||
for (const at of wires) {
|
||||
|
||||
Reference in New Issue
Block a user