Files
bworld/tests/create/kinetics_test.ts
T
2026-10-15 12:01:46 -03:00

374 lines
16 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// create's kinetics: shafts take the speed of what turns them, a hand crank turns them while it's being clicked, and a
// block that would turn two ways at once breaks
import { assert, assertEquals } from "@std/assert";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { get_state_value, set_state_value } from "$/common/utils.ts";
import type { Face } from "$/common/mod_api/shared.ts";
import type { ClientContext, Instances, ModelRef, Visual, VisualBlock } from "$/common/mod_api/client.ts";
import { setup as setup_client } from "$/mods/create/scripts/client.ts";
import { stone_floor, test_game } from "../helpers.ts";
// facing and axis states, in the order the importer numbers them
const FACING = { north: 0, east: 1, south: 2, west: 3, up: 4, down: 5 };
const AXIS = { x: 0, y: 1, z: 2 };
// alice standing south of 0 100 0 on a stone floor, looking north, with nothing in her hands. config changes create's
async function setup(config: Record<string, unknown> = {}) {
const t = await test_game("mods", undefined, undefined, { create: config });
const { game } = t;
t.join(1, "alice");
stone_floor(game, 6);
for (let x = -6; x <= 6; x++) {
for (let z = -6; z <= 6; z++) for (let y = 100; y < 104; y++) game.set_block(x, y, z, AIR_ID);
}
const move = (sneaking = false, pitch = 0) =>
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch, ...(sneaking && { sneaking }) });
move();
// a block with one state set, the way a player would have placed it
const place = (x: number, y: number, z: number, id: string, state?: string, value = 0) => {
const info = EverythingRegistry.get<BlockRegistry>("blocks", id)!;
game.set_block(x, y, z, id, undefined, state ? set_state_value(0, info, state, value)! >>> 16 : 0);
};
const speed = (x: number, y: number, z: number) => game.world.get_tile(x, y, z)?.client_data?.speed as number ?? 0;
const state = (x: number, y: number, z: number, name: string) =>
get_state_value(game.world.get_block_value(x, y, z), game.world.get_block_info(x, y, z)!, name);
const use = (x: number, y: number, z: number, face: Face = "south") =>
t.send(1, { type: "use_block", x, y, z, face, hit: [0.5, 0.5, 1] });
const ticks = (n: number) => {
for (let i = 0; i < n; i++) game.tick();
};
// the rpm a crank turns at
const crank =
(EverythingRegistry.get<BlockRegistry>("blocks", "create:hand_crank")!.components!["create:hand_crank"] as {
speed: number;
}).speed;
return { ...t, move, place, speed, state, use, ticks, crank };
}
// a crank at 0 100 0 facing west, its axle in a line of shafts along x from 1 to length
async function crank_and_line(length = 4, config?: Record<string, unknown>) {
const t = await setup(config);
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
for (let x = 1; x <= length; x++) t.place(x, 100, 0, "create:shaft", "axis", AXIS.x);
return t;
}
const line = (t: { speed: (x: number, y: number, z: number) => number }, from: number, to: number) =>
Array.from({ length: to - from + 1 }, (_, i) => t.speed(from + i, 100, 0));
Deno.test("a hand crank turns the line of shafts on its axle while it's being clicked, then it stops", async () => {
const t = await crank_and_line();
// a shaft across the line, and one past a gap, aren't turned
t.place(2, 100, 1, "create:shaft", "axis", AXIS.z);
t.place(6, 100, 0, "create:shaft", "axis", AXIS.x);
// facing west, it turns counterclockwise seen from the west, which is clockwise around x
t.use(0, 100, 0);
assertEquals(line(t, 0, 4), [-t.crank, -t.crank, -t.crank, -t.crank, -t.crank]);
assertEquals([t.speed(2, 100, 1), t.speed(6, 100, 0)], [0, 0]);
// holding use clicks every 4 ticks, which keeps it going
for (let i = 0; i < 10; i++) {
t.ticks(4);
t.use(0, 100, 0);
}
assertEquals(t.speed(4, 100, 0), -t.crank);
t.ticks(40);
assertEquals(line(t, 0, 4), [0, 0, 0, 0, 0]);
// still, nothing ticks
assertEquals(t.game.world.ticks(t.game.world.get_tile(0, 100, 0)!), false);
});
Deno.test("sneaking turns a crank the other way", async () => {
const t = await crank_and_line();
t.move(true);
t.use(0, 100, 0);
assertEquals(line(t, 0, 4), [t.crank, t.crank, t.crank, t.crank, t.crank]);
// turning it the other way while it turns turns the whole line around
t.move(false);
t.use(0, 100, 0);
assertEquals(line(t, 0, 4), [-t.crank, -t.crank, -t.crank, -t.crank, -t.crank]);
});
Deno.test("a shaft placed on a turning line turns with it, and breaking one stops what's past it", async () => {
const t = await crank_and_line();
t.use(0, 100, 0);
t.place(5, 100, 0, "create:shaft", "axis", AXIS.x);
assertEquals(t.speed(5, 100, 0), -t.crank);
t.game.destroy_block(2, 100, 0);
assertEquals(line(t, 0, 5), [-t.crank, -t.crank, 0, 0, 0, 0]);
// put back, it joins them up again
t.place(2, 100, 0, "create:shaft", "axis", AXIS.x);
assertEquals(line(t, 0, 5), [-t.crank, -t.crank, -t.crank, -t.crank, -t.crank, -t.crank]);
// breaking the crank stops all of it
t.game.destroy_block(0, 100, 0);
assertEquals(line(t, 1, 5), [0, 0, 0, 0, 0]);
});
Deno.test("a crank turned against a line another one turns breaks, and drops", async () => {
const t = await crank_and_line(4);
// at the other end, facing east, where turning it turns the line the other way around x
t.place(5, 100, 0, "create:hand_crank", "facing", FACING.east);
t.use(0, 100, 0);
t.use(5, 100, 0);
assertEquals(t.game.world.get_block_id(5, 100, 0), AIR_ID);
assert(t.game.item_entities().some((e) => e.item.type_id === "create:hand_crank"));
assertEquals(line(t, 0, 4), [-t.crank, -t.crank, -t.crank, -t.crank, -t.crank]);
});
Deno.test("two cranks turning a line the same way turn it together, and either keeps it going", async () => {
const t = await crank_and_line(4);
t.place(5, 100, 0, "create:hand_crank", "facing", FACING.east);
t.use(0, 100, 0);
// sneaking turns this one the same way around x as the first
t.move(true);
t.use(5, 100, 0);
assertEquals(t.game.world.get_block_id(5, 100, 0), "create:hand_crank");
assertEquals(line(t, 0, 5), [-t.crank, -t.crank, -t.crank, -t.crank, -t.crank, -t.crank]);
// the first one let go, the second keeps the line turning on its own
for (let i = 0; i < 10; i++) {
t.ticks(4);
t.use(5, 100, 0);
}
assertEquals(line(t, 0, 5), [-t.crank, -t.crank, -t.crank, -t.crank, -t.crank, -t.crank]);
t.ticks(40);
assertEquals(line(t, 0, 5), [0, 0, 0, 0, 0, 0]);
});
Deno.test("a crank that would turn things faster than create's max_speed breaks", async () => {
const t = await crank_and_line(2, { max_speed: 16 });
t.use(0, 100, 0);
assertEquals(t.game.world.get_block_id(0, 100, 0), AIR_ID);
assertEquals(line(t, 1, 2), [0, 0]);
});
Deno.test("turning a crank makes the player hungry", async () => {
const t = await crank_and_line();
const player = t.game.mods.player(t.game.players()[0]);
const before = player.saturation + player.food;
for (let i = 0; i < 100; i++) {
t.use(0, 100, 0);
t.ticks(4);
}
assert(player.saturation + player.food < before);
});
Deno.test("speeds are saved, and a crank still stops after the world is loaded", async () => {
const t = await crank_and_line();
t.use(0, 100, 0);
const loaded = await test_game("mods", t.game.save());
loaded.join(1, "alice");
const speed = (x: number) => loaded.game.world.get_tile(x, 100, 0)?.client_data?.speed as number ?? 0;
assertEquals(speed(3), -t.crank);
for (let i = 0; i < 40; i++) loaded.game.tick();
assertEquals(speed(3), 0);
});
Deno.test("a shaft lines up with the axle it's placed against, and otherwise points the way the player looks", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give create:shaft 64" });
t.send(1, { type: "chat", text: "/give create:hand_crank 64" });
t.send(1, { type: "select_slot", slot: 0 });
// on the floor, looking north and then down
t.use(-2, 99, 0, "top");
assertEquals(t.state(-2, 100, 0, "axis"), AXIS.z);
t.move(false, -1.4);
t.use(-3, 99, 0, "top");
assertEquals(t.state(-3, 100, 0, "axis"), AXIS.y);
// beside the back of a crank facing west, along its axle
t.move();
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
t.use(1, 99, 0, "top");
assertEquals(t.state(1, 100, 0, "axis"), AXIS.x);
// a crank faces away from what it's placed on, or away from an axle beside it, so it turns it
t.send(1, { type: "select_slot", slot: 1 });
t.use(-1, 99, 1, "top");
assertEquals(t.state(-1, 100, 1, "facing"), FACING.up);
t.use(2, 99, 0, "top");
assertEquals(t.state(2, 100, 0, "facing"), FACING.east);
});
Deno.test("small cogs side by side on the same axis turn each other the other way", async () => {
const t = await setup();
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
// a square of them across the crank's axle, and one on another axis beside it
for (const [y, z] of [[100, 0], [101, 0], [100, 1], [101, 1]]) t.place(1, y, z, "create:cogwheel", "axis", AXIS.x);
t.place(1, 100, -1, "create:cogwheel", "axis", AXIS.z);
t.use(0, 100, 0);
const c = t.crank;
assertEquals([t.speed(1, 100, 0), t.speed(1, 101, 0), t.speed(1, 100, 1), t.speed(1, 101, 1)], [-c, c, c, -c]);
assertEquals(t.speed(1, 100, -1), 0);
// the square is a loop that agrees with itself, so it stops cleanly too
t.ticks(40);
assertEquals([t.speed(1, 100, 0), t.speed(1, 101, 0), t.speed(1, 100, 1), t.speed(1, 101, 1)], [0, 0, 0, 0]);
assertEquals(t.game.world.get_block_id(1, 101, 1), "create:cogwheel");
});
Deno.test("a large cog turns a small one at its corner twice as fast, and a small one turns it half as fast", async () => {
const t = await setup();
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 100, 0, "create:large_cogwheel", "axis", AXIS.x);
t.place(1, 101, 1, "create:cogwheel", "axis", AXIS.x);
// at another corner, but on another axis
t.place(1, 101, -1, "create:cogwheel", "axis", AXIS.y);
t.use(0, 100, 0);
assertEquals([t.speed(1, 100, 0), t.speed(1, 101, 1), t.speed(1, 101, -1)], [-t.crank, 2 * t.crank, 0]);
const u = await setup();
u.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
u.place(1, 100, 0, "create:cogwheel", "axis", AXIS.x);
u.place(1, 101, 1, "create:large_cogwheel", "axis", AXIS.x);
u.use(0, 100, 0);
assertEquals([u.speed(1, 100, 0), u.speed(1, 101, 1)], [-u.crank, u.crank / 2]);
});
Deno.test("large cogs on different axes turn each other across an edge", async () => {
const t = await setup();
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 100, 0, "create:large_cogwheel", "axis", AXIS.x);
t.place(2, 101, 0, "create:large_cogwheel", "axis", AXIS.y);
// on the same axis they don't
t.place(1, 101, 1, "create:large_cogwheel", "axis", AXIS.x);
t.use(0, 100, 0);
assertEquals(Math.abs(t.speed(2, 101, 0)), t.crank);
assertEquals(t.speed(1, 101, 1), 0);
});
Deno.test("cogs that would turn something faster than create's max_speed break", async () => {
const t = await setup({ max_speed: 48 });
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 100, 0, "create:large_cogwheel", "axis", AXIS.x);
t.place(1, 101, 1, "create:cogwheel", "axis", AXIS.x);
t.use(0, 100, 0);
const blocks = [[0, 100, 0], [1, 100, 0], [1, 101, 1]] as const;
assert(blocks.some(([x, y, z]) => t.game.world.get_block_id(x, y, z) === AIR_ID));
for (const [x, y, z] of blocks) assert(Math.abs(t.speed(x, y, z)) <= 48);
});
Deno.test("a cog lines up with the cog it's placed against, and can't go where it would cut into another", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give create:cogwheel 64" });
t.send(1, { type: "chat", text: "/give create:large_cogwheel 64" });
t.send(1, { type: "select_slot", slot: 0 });
const held = () => t.game.players()[0].held_item?.amount;
// on the floor it lies flat, and one placed against its side lies flat with it
t.use(0, 99, 0, "top");
assertEquals(t.state(0, 100, 0, "axis"), AXIS.y);
t.use(0, 100, 0, "west");
assertEquals(t.state(-1, 100, 0, "axis"), AXIS.y);
// sneaking, it takes the axis of the face it's clicked onto
t.move(true);
t.use(0, 100, 0, "north");
assertEquals(t.state(0, 100, -1, "axis"), AXIS.z);
t.move();
// a large cog's teeth would cut into the small one beside it, but at its corner they mesh
t.send(1, { type: "select_slot", slot: 1 });
t.use(1, 99, 0, "top");
assertEquals(t.game.world.get_block_id(1, 100, 0), AIR_ID);
assertEquals(held(), 64);
t.use(1, 99, 1, "top");
assertEquals(t.game.world.get_block_id(1, 100, 1), "create:large_cogwheel");
assertEquals(t.state(1, 100, 1, "axis"), AXIS.y);
});
// create's client script with only what its visuals need, and a block for one to draw that records how its instances
// turn, by model: "own" for the block's own model
async function visual_for(
id: string,
states: Record<string, number>,
data: Record<string, unknown>,
[x, y, z] = [0, 100, 0],
) {
await test_game("mods");
const visuals = new Map<string, (block: VisualBlock, instances: Instances) => Visual>();
setup_client({
blocks: {
components: (id: string) =>
structuredClone(EverythingRegistry.get<BlockRegistry>("blocks", id)!.components),
},
render: { register_visual: (id: string, create: never) => visuals.set(id, create) },
} as unknown as ClientContext);
const turns = new Map<string, [string, number, number]>();
const rotating = (model?: ModelRef) => ({
set_rotation: (axis: string, rpm: number, offset = 0) =>
turns.set(model === undefined ? "own" : JSON.stringify(model), [axis, rpm, offset]),
delete() {},
});
const block = { id, x, y, z, states, get_client_data: (field: string) => data[field] as never };
const visual = visuals.get(id)!(block, { rotating, transformed: () => ({}) } as unknown as Instances);
return { visual, turns, data };
}
Deno.test("a shaft is drawn turning around its axis at the speed its client data says", async () => {
const { visual, turns, data } = await visual_for("create:shaft", { axis: AXIS.z }, { speed: 16 });
// its own model, the way its states draw it
assertEquals(turns.get("own")!.slice(0, 2), ["z", 16]);
data.speed = -64;
visual.update!();
assertEquals(turns.get("own")!.slice(0, 2), ["z", -64]);
});
Deno.test("a crank's handle catches up with the speed it turns at, and slows to a stop", async () => {
const { visual, turns, data } = await visual_for("create:hand_crank", { facing: FACING.west }, { speed: -32 });
assertEquals(turns.get("own")!.slice(0, 2), ["x", -32]);
const handle = () => [...turns].find(([model]) => model.includes("handle"))![1];
// degrees it turned by in a tick, after time ticks
let time = 0;
const step = () => {
const before = handle()[2];
visual.on_frame!(++time);
return ((handle()[2] - before) % 360 + 540) % 360 - 180;
};
visual.on_frame!(time);
const first = step();
for (let i = 0; i < 40; i++) step();
// it turns the same way as the shaft, around x, faster once it's caught up
assertEquals(handle()[0], "x");
assert(first < 0 && step() < first, `${first}`);
data.speed = undefined;
visual.update!();
const slowing = step();
assert(slowing < 0, `${slowing}`);
for (let i = 0; i < 100; i++) step();
assert(Math.abs(step()) < 0.01);
});
Deno.test("small cogs beside each other are turned half a tooth apart, so they mesh", async () => {
const offset = async (x: number) =>
(await visual_for("create:cogwheel", { axis: AXIS.y }, {}, [x, 100, 0])).turns.get("own")![2];
assertEquals(Math.abs(await offset(0) - await offset(1)), 22.5);
});
Deno.test("a large cog is drawn as its cog and its shaft, both turning at its speed", async () => {
const { visual, turns, data } = await visual_for("create:large_cogwheel", { axis: AXIS.z }, { speed: 8 }, [
1,
0,
0,
]);
const [cog, shaft] = ["shaftless", 'shaft"'].map((part) => [...turns].find(([model]) => model.includes(part))![1]);
assertEquals([cog.slice(0, 2), shaft.slice(0, 2)], [["z", 8], ["z", 8]]);
// where a small cog wouldn't be offset, it's offset by half its own tooth, and its shaft lines up with other shafts
assertEquals([cog[2], shaft[2]], [11.25, 0]);
data.speed = -8;
visual.update!();
assertEquals(turns.size, 2);
assert([...turns.values()].every(([, rpm]) => rpm === -8));
});