Verdigris working !
This commit is contained in:
@@ -3,17 +3,18 @@
|
||||
|
||||
// verdigris wire, see REDSTONE.md: 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";
|
||||
import { block_shape } from "$/common/block_shapes.ts";
|
||||
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 type { WorldSave } from "$/server/game/saves.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 { DIRECTIONS, FACE_OFFSETS, FACES, OPPOSITE } from "$/mods/bworld/scripts/verdigris/sides.ts";
|
||||
import { wire_at } from "$/mods/bworld/scripts/verdigris/wires.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";
|
||||
|
||||
// how far it stands off the face it lies on, in blocks
|
||||
@@ -216,7 +217,7 @@ async function builder(save?: WorldSave) {
|
||||
const cell = joined_cell(reader, ...at, face, direction);
|
||||
return cell ? [cell.join(" ")] : [];
|
||||
});
|
||||
return { t, use, states, id, power, covers, joins };
|
||||
return { t, use, states, id, power, covers, joins, reader };
|
||||
}
|
||||
|
||||
type Builder = Awaited<ReturnType<typeof builder>>;
|
||||
@@ -285,6 +286,30 @@ Deno.test("a wire goes over the edge of a block to the wire on its next face, wh
|
||||
assertEquals([b.joins(3, 101, 1), b.joins(3, 100, 0)], [[], ["3 100 -1"]]);
|
||||
});
|
||||
|
||||
Deno.test("a lever put where a wire went around an edge joins both ends instead, and taken away they join again", async () => {
|
||||
const b = await builder();
|
||||
// on top of a block, over its north edge and down its north face, then along the floor to a lever that's on
|
||||
b.t.game.set_block(3, 100, 1, "bworld:stone");
|
||||
b.use("bworld:verdigris", 3, 100, 1, "top");
|
||||
b.use("bworld:verdigris", 3, 100, 1, "north");
|
||||
b.use("bworld:verdigris", 3, 99, 0, "top");
|
||||
b.use("bworld:verdigris", 3, 99, -1, "top");
|
||||
b.use("bworld:lever", 3, 99, -2, "top");
|
||||
b.use(undefined, 3, 100, -2, "top");
|
||||
const wires: Pos[] = [[3, 101, 1], [3, 100, 0], [3, 100, -1]];
|
||||
assertEquals(wires.map((at) => b.power(...at)), [15, 15, 15]);
|
||||
// hanging from a ceiling in the cell the edge went through. the wires' arms don't change, they join the lever
|
||||
b.t.game.set_block(3, 102, 0, "bworld:stone");
|
||||
b.use("bworld:lever", 3, 102, 0, "bottom");
|
||||
assertEquals(wires.map((at) => b.power(...at)), [0, 15, 15]);
|
||||
b.use(undefined, 3, 101, 0, "top");
|
||||
assertEquals(b.power(3, 101, 1), 15);
|
||||
b.t.game.destroy_block(3, 101, 0);
|
||||
assertEquals(wires.map((at) => b.power(...at)), [15, 15, 15]);
|
||||
b.use(undefined, 3, 100, -2, "top");
|
||||
assertEquals(wires.map((at) => b.power(...at)), [0, 0, 0]);
|
||||
});
|
||||
|
||||
Deno.test("a player sees a wire join the moment they place it, the way the server joins it", async () => {
|
||||
const b = await builder();
|
||||
b.t.game.set_block(0, 100, 0, "bworld:stone");
|
||||
@@ -398,6 +423,64 @@ Deno.test("a world saved and loaded has the same nets", async () => {
|
||||
assertEquals(again.wires.map((at) => again.power(...at)), [0, 0, 0, 0, 0]);
|
||||
});
|
||||
|
||||
Deno.test("whatever is built, broken and switched, each wire has the power walking its net again gives it", async () => {
|
||||
let b = await builder();
|
||||
const rng = new Alea("verdigris nets");
|
||||
const pick = <T>(list: readonly T[]) => list[Math.floor(rng.next() * list.length)];
|
||||
const cell = (): Pos => [pick([-2, -1, 0, 1, 2]), pick([100, 101, 102]), pick([-2, -1, 0, 1, 2])];
|
||||
const cells: Pos[] = [];
|
||||
for (let x = -2; x <= 2; x++) for (let y = 100; y <= 102; y++) for (let z = -2; z <= 2; z++) cells.push([x, y, z]);
|
||||
const solid = (id: string) => id !== AIR_ID && is_solid(EverythingRegistry.get<BlockRegistry>("blocks", id)!);
|
||||
// placed in an empty cell or one with wire, against a solid block beside it
|
||||
const place = (id: string) => {
|
||||
const spots = cells.flatMap((at) =>
|
||||
[AIR_ID, WIRE].includes(b.id(...at))
|
||||
? FACES.filter((face) => solid(b.id(...step(...at, face)))).map((face) => [at, face] as const)
|
||||
: []
|
||||
);
|
||||
if (spots.length === 0) return;
|
||||
const [at, face] = pick(spots);
|
||||
b.use(id, ...step(...at, face), OPPOSITE[face]);
|
||||
};
|
||||
const switch_lever = () => {
|
||||
const levers = cells.filter((at) => b.id(...at) === "bworld:lever");
|
||||
if (levers.length > 0) b.use(undefined, ...pick(levers), "top");
|
||||
};
|
||||
const actions = [
|
||||
...Array(4).fill(() => place("bworld:verdigris")),
|
||||
() => place("bworld:lever"),
|
||||
switch_lever,
|
||||
switch_lever,
|
||||
() => b.t.game.set_block(...cell(), "bworld:stone"),
|
||||
() => b.t.game.set_block(...cell(), "bworld:stone"),
|
||||
() => b.t.game.set_block(...cell(), "bworld:verdigris_block"),
|
||||
() => b.t.game.set_block(...cell(), AIR_ID),
|
||||
() => b.t.game.destroy_block(...cell()),
|
||||
];
|
||||
// every chunk saved so far: a save only has the chunks that changed since the world was loaded
|
||||
const chunks = new Map<string, SavedChunk>();
|
||||
for (let step = 0; step < 400; step++) {
|
||||
// saved and loaded now and then, so some nets are only walked when something touches them
|
||||
if (step % 40 === 39) {
|
||||
const saved = b.t.game.save();
|
||||
for (const chunk of saved.chunks) chunks.set(`${chunk.x} ${chunk.z}`, chunk);
|
||||
b = await builder({ level: saved.level, chunks: [...chunks.values()] });
|
||||
}
|
||||
pick(actions)();
|
||||
// a new index walks each net from scratch, and says which wires don't have its power
|
||||
const wrong: string[] = [];
|
||||
const walked = new NetIndex({
|
||||
...b.reader,
|
||||
solid,
|
||||
set_power: (x, y, z, power) => wrong.push(`${x} ${y} ${z} has ${b.power(x, y, z)}, not ${power}`),
|
||||
neighbor_changed: () => {},
|
||||
});
|
||||
for (const [x, y, z] of cells) if (b.id(x, y, z) === WIRE) walked.beside_changed(x, y, z, { x, y, z });
|
||||
assertEquals(wrong, [], `after step ${step}`);
|
||||
}
|
||||
assert(cells.some((at) => b.id(...at) === WIRE && b.power(...at) > 0), "some wire was powered");
|
||||
});
|
||||
|
||||
Deno.test("wire comes off a face whose block goes, and drops one verdigris for each face it covered", async () => {
|
||||
const b = await builder();
|
||||
const verdigris = () =>
|
||||
|
||||
Reference in New Issue
Block a user