Repeater
This commit is contained in:
@@ -258,3 +258,83 @@ Deno.test("a trapdoor opens while it's powered, and opens straight away when it'
|
||||
b.use(TRAPDOOR, 3, 99, 0, "top", [3, 100, 2]);
|
||||
assertEquals(open(b, [3, 100, 0]), true);
|
||||
});
|
||||
|
||||
const PULSE_REPEATER = "bworld:pulse_repeater";
|
||||
|
||||
// a pulse repeater at 0 100 0 placed by alice looking north, so it gives power north, with a lever behind it and wire on
|
||||
// the floor in front of it. the wire's power after each of ticks ticks
|
||||
async function pulse_repeater() {
|
||||
const b = await builder();
|
||||
b.use(PULSE_REPEATER, 0, 99, 0, "top", [0, 100, 2]);
|
||||
assertEquals(b.states(0, 100, 0).facing, FACES.indexOf("north"));
|
||||
b.use("bworld:lever", 0, 99, 1, "top", [0, 100, 3]);
|
||||
b.use("bworld:verdigris", 0, 99, -1, "top", [0, 100, 2]);
|
||||
const switch_lever = () => b.use(undefined, 0, 100, 1, "top", [0, 100, 3]);
|
||||
const wire = (ticks: number) =>
|
||||
Array.from({ length: ticks }, () => {
|
||||
b.t.game.tick();
|
||||
return b.power(0, 100, -1);
|
||||
});
|
||||
const scroll = (amount: number, at: Pos = [0, 100, 0]) =>
|
||||
b.t.send(1, { type: "scroll_block", x: at[0], y: at[1], z: at[2], face: "top", hit: [0.5, 1, 0.5], amount });
|
||||
const overlays = () => b.t.take(1).filter((m) => m.type === "chat" && m.overlay).map((m) => m.text);
|
||||
return { ...b, switch_lever, wire, scroll, overlays };
|
||||
}
|
||||
|
||||
Deno.test("a pulse repeater pulses out of its front its delay after what's behind it is powered, once each time", async () => {
|
||||
const b = await pulse_repeater();
|
||||
b.switch_lever();
|
||||
// 2 ticks by default, then 2 ticks of power, and no more while the lever stays on
|
||||
assertEquals(b.wire(10), [0, 15, 15, 0, 0, 0, 0, 0, 0, 0]);
|
||||
b.switch_lever();
|
||||
assertEquals(b.wire(4), [0, 0, 0, 0]);
|
||||
b.switch_lever();
|
||||
assertEquals(b.wire(4), [0, 15, 15, 0]);
|
||||
});
|
||||
|
||||
Deno.test("scrolling the mouse wheel at a pulse repeater changes its delay, and shows it above the hotbar", async () => {
|
||||
const b = await pulse_repeater();
|
||||
b.overlays();
|
||||
b.scroll(1);
|
||||
assertEquals(b.overlays(), ["Delay: 3 ticks"]);
|
||||
// a tick at a time up to a second, then seconds
|
||||
b.scroll(17);
|
||||
b.scroll(1);
|
||||
assertEquals(b.overlays(), ["Delay: 1 second", "Delay: 2 seconds"]);
|
||||
b.switch_lever();
|
||||
const wire = b.wire(42);
|
||||
assertEquals([wire.indexOf(15), wire.lastIndexOf(15)], [39, 40]);
|
||||
// it goes no further than an hour, and no lower than 2 ticks
|
||||
b.scroll(1000);
|
||||
b.scroll(-1);
|
||||
b.scroll(-1000);
|
||||
assertEquals(b.overlays(), ["Delay: 60 minutes", "Delay: 59 minutes", "Delay: 2 ticks"]);
|
||||
|
||||
// blocks that aren't scrollable and ones out of reach don't hear it
|
||||
b.scroll(1, [0, 99, 0]);
|
||||
b.t.game.teleport(b.t.game.players()[0], 0.5, 100, 20.5);
|
||||
b.scroll(1);
|
||||
assertEquals(b.overlays(), []);
|
||||
assertEquals(b.states(0, 100, 0).delay, 0);
|
||||
});
|
||||
|
||||
Deno.test("right clicking a pulse repeater inverts it, so it's on and its pulse turns it off", async () => {
|
||||
const b = await pulse_repeater();
|
||||
b.use(undefined, 0, 100, 0, "top", [0, 100, 2]);
|
||||
assertEquals(b.power(0, 100, -1), 15);
|
||||
b.switch_lever();
|
||||
assertEquals(b.wire(4), [15, 0, 0, 15]);
|
||||
b.use(undefined, 0, 100, 0, "top", [0, 100, 2]);
|
||||
assertEquals(b.power(0, 100, -1), 0);
|
||||
});
|
||||
|
||||
Deno.test("a pulse repeater only goes on a floor, and breaks when the floor goes", async () => {
|
||||
const b = await builder();
|
||||
b.t.game.set_block(3, 101, 3, "bworld:stone");
|
||||
b.use(PULSE_REPEATER, 3, 101, 3, "south", [3, 101, 6]);
|
||||
assertEquals(b.id(3, 101, 4), AIR_ID);
|
||||
b.use(PULSE_REPEATER, 3, 101, 3, "top", [3, 102, 6]);
|
||||
assertEquals(b.id(3, 102, 3), PULSE_REPEATER);
|
||||
b.t.game.set_block(3, 101, 3, AIR_ID);
|
||||
assertEquals(b.id(3, 102, 3), AIR_ID);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user