Files
bworld/tests/create/generators_test.ts
T
2026-11-04 21:51:26 -03:00

121 lines
5.5 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// create's generators besides the hand crank: the water wheel, turned by water flowing past it, and the creative
// motor, which turns at whatever speed it's scrolled to
import { assert, assertEquals } from "@std/assert";
import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
import { AIR_ID } from "@/common/protocol.ts";
import { flow_score, motor_step } from "@/mods/create/scripts/kinetics/generators.ts";
import { AXIS, FACING, setup, visual_for } from "./helpers.ts";
// the rpm a generator's params say
const speed_of = (id: string, component: string) =>
(EverythingRegistry.get<BlockRegistry>("blocks", id)!.components![component] as { speed: number }).speed;
Deno.test("water flowing along a wheel's paddles turns it, one way or the other, and along its axis doesn't", () => {
// flows by "x y z" around a wheel at 0 0 0 on the z axis
const score = (flows: Record<string, [number, number, number]>) =>
flow_score("z", [0, 0, 0], (x, y, z) => {
const [fx, fy, fz] = flows[`${x} ${y} ${z}`] ?? [0, 0, 0];
return { x: fx, y: fy, z: fz };
});
// seen from the south, over the top toward the east is clockwise, and under the bottom toward the west too
assertEquals(score({ "0 1 0": [1, 0, 0] }), -1);
assertEquals(score({ "0 1 0": [1, 0, 0], "0 -1 0": [-1, 0, 0] }), -1);
assertEquals(score({ "0 1 0": [-1, 0, 0] }), 1);
// falling down its east side
assertEquals(score({ "1 0 0": [0, -1, 0] }), -1);
// both ways at once, along its axis, or straight at it
assertEquals(score({ "0 1 0": [1, 0, 0], "0 -1 0": [1, 0, 0] }), 0);
assertEquals(score({ "0 1 0": [0, 0, 1] }), 0);
assertEquals(score({ "0 1 0": [0, -1, 0] }), 0);
});
Deno.test("a water wheel beside a stream turns what's on its axle, and stops when the water's gone", async () => {
const t = await setup();
// a channel along x at z 1, walled in, with the wheel standing in its south wall on the y axis
for (let x = -4; x <= 4; x++) {
t.game.set_block(x, 100, 2, "bworld:stone");
if (x !== 0) t.game.set_block(x, 100, 0, "bworld:stone");
}
t.game.set_block(-4, 100, 1, "bworld:stone");
t.place(0, 100, 0, "create:water_wheel", "facing", FACING.up);
t.place(0, 101, 0, "create:shaft", "axis", AXIS.y);
assertEquals(t.speed(0, 101, 0), 0);
// flowing east past its north side turns it counterclockwise seen from above
t.game.set_block(-3, 100, 1, "bworld:water");
t.ticks(60);
const speed = speed_of("create:water_wheel", "create:water_wheel");
assertEquals([t.speed(0, 100, 0), t.speed(0, 101, 0)], [speed, speed]);
t.game.set_block(-3, 100, 1, AIR_ID);
t.ticks(100);
assertEquals([t.speed(0, 100, 0), t.speed(0, 101, 0)], [0, 0]);
});
Deno.test("a water wheel can't go beside another one unless they're on the same axle", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give create:water_wheel 64" });
t.send(1, { type: "select_slot", slot: 0 });
// looking north, they face south, on the z axis
t.use(0, 99, 0, "top");
assertEquals(t.state(0, 100, 0, "facing"), FACING.south);
t.use(1, 99, 0, "top");
assertEquals(t.game.world.get_block_id(1, 100, 0), AIR_ID);
t.use(0, 99, -1, "top");
assertEquals(t.game.world.get_block_id(0, 100, -1), "create:water_wheel");
// facing away from the first one's axle, so on the same axis
assertEquals(t.state(0, 100, -1, "facing"), FACING.north);
});
Deno.test("a creative motor's speed goes up by more the faster it is, finer while sneaking, and skips 0", () => {
assertEquals(motor_step(1, true, false, 256), 2);
assert(motor_step(16, true, false, 256) - 16 > motor_step(2, true, false, 256) - 2);
assertEquals(motor_step(16, true, true, 256), 17);
assertEquals(motor_step(1, false, false, 256), -1);
assertEquals(motor_step(-1, true, true, 256), 1);
assertEquals(motor_step(256, true, false, 256), 256);
assertEquals(motor_step(-250, false, false, 256), -256);
});
Deno.test("a creative motor turns what's in front of it, at the speed it's scrolled to", async () => {
const t = await setup();
t.place(1, 100, 0, "create:shaft", "axis", AXIS.x);
t.place(2, 100, 0, "create:shaft", "axis", AXIS.x);
// placed beside an axle, it faces it
t.send(1, { type: "chat", text: "/give create:creative_motor 1" });
t.send(1, { type: "select_slot", slot: 0 });
t.use(0, 99, 0, "top");
assertEquals(t.state(0, 100, 0, "facing"), FACING.east);
// facing east, it turns counterclockwise seen from the east, which is around x
let speed = speed_of("create:creative_motor", "create:creative_motor");
assertEquals([t.speed(1, 100, 0), t.speed(2, 100, 0)], [speed, speed]);
const scroll = (amount: number) =>
t.send(1, { type: "scroll_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1], amount });
scroll(1);
speed = motor_step(speed, true, false, 256);
assertEquals(t.speed(2, 100, 0), speed);
// down past 0, the line turns the other way
while (speed > 0) {
scroll(-1);
speed = motor_step(speed, false, false, 256);
}
assertEquals(t.speed(2, 100, 0), speed);
assert(speed < 0);
});
Deno.test("a creative motor is drawn with its half shaft turning out of its front", async () => {
const { visual, turns, data } = await visual_for("create:creative_motor", { facing: FACING.east }, { speed: 16 });
const shaft = () => [...turns].find(([model]) => model.includes("shaft_half"))![1];
assertEquals(shaft().slice(0, 2), ["x", 16]);
assertEquals(turns.has("own"), false);
data.speed = -64;
visual.update!();
assertEquals(shaft()[1], -64);
});