Files
2026-11-04 21:51:26 -03:00

385 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, a
// block that would turn two ways at once breaks, and a network that takes more than its generators make stops
import { assert, assertEquals } from "@std/assert";
import { AIR_ID } from "@/common/protocol.ts";
import { test_game } from "../helpers.ts";
import { AXIS, FACING, setup, visual_for } from "./helpers.ts";
// 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);
});
// a crank making capacity per rpm, and presses taking impact per rpm
const stress = (capacity: number, impact: number) => ({
stress: { capacity: { "create:hand_crank": capacity }, impact: { "create:mechanical_press": impact } },
});
Deno.test("a network whose blocks take more than its generators make is overstressed, all of it", async () => {
// each press takes half of what the crank makes, at any speed
const t = await setup(stress(8, 4));
const over = t.overstressed;
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 100, 0, "create:mechanical_press", "facing", FACING.east);
t.place(2, 100, 0, "create:mechanical_press", "facing", FACING.east);
t.place(3, 100, 0, "create:shaft", "axis", AXIS.x);
t.use(0, 100, 0);
assertEquals([0, 1, 2, 3].map((x) => over(x, 100, 0)), [false, false, false, false]);
// a third is too many, and every block in the network stops, though its speed is kept
t.place(4, 100, 0, "create:mechanical_press", "facing", FACING.east);
assertEquals([0, 1, 2, 3, 4].map((x) => over(x, 100, 0)), [true, true, true, true, true]);
assertEquals(line(t, 0, 4), [-t.crank, -t.crank, -t.crank, -t.crank, -t.crank]);
// taking it away again is enough
t.game.destroy_block(4, 100, 0);
assertEquals([0, 1, 2, 3].map((x) => over(x, 100, 0)), [false, false, false, false]);
// and blocks that stop turning aren't overstressed any more
t.place(4, 100, 0, "create:mechanical_press", "facing", FACING.east);
t.ticks(40);
assertEquals([0, 1, 2, 3, 4].map((x) => over(x, 100, 0)), [false, false, false, false, false]);
});
Deno.test("a second generator turning a network adds what it makes", async () => {
const t = await setup(stress(8, 6));
const over = t.overstressed;
t.place(0, 100, 0, "create:hand_crank", "facing", FACING.west);
for (let x = 1; x <= 2; x++) t.place(x, 100, 0, "create:mechanical_press", "facing", FACING.east);
t.place(3, 100, 0, "create:hand_crank", "facing", FACING.east);
t.use(0, 100, 0);
assert(over(1, 100, 0));
// sneaking turns this one the same way around x as the first
t.move(true);
t.use(3, 100, 0);
assertEquals([0, 1, 2, 3].map((x) => over(x, 100, 0)), [false, false, false, false]);
// let go, it's overstressed again
for (let i = 0; i < 10; i++) {
t.ticks(4);
t.use(0, 100, 0);
}
assert(over(1, 100, 0));
});
Deno.test("a block takes more the faster it turns", async () => {
// on its own the press takes 3/4 of what the crank makes
const press_behind = async (cog: "create:cogwheel" | "create:large_cogwheel") => {
const t = await setup(stress(8, 6));
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);
// behind the large cog at the crank's speed, or the small one at twice that
const [y, z] = cog === "create:large_cogwheel" ? [100, 0] : [101, 1];
t.place(2, y, z, "create:mechanical_press", "facing", FACING.east);
t.use(0, 100, 0);
assertEquals(Math.abs(t.speed(2, y, z)), cog === "create:large_cogwheel" ? t.crank : 2 * t.crank);
return t.overstressed(2, y, z);
};
assertEquals(await press_behind("create:large_cogwheel"), false);
assertEquals(await press_behind("create:cogwheel"), true);
});
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);
});
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 block in an overstressed network is drawn still", async () => {
const { visual, turns, data } = await visual_for("create:shaft", { axis: AXIS.z }, {
speed: 16,
overstressed: true,
});
assertEquals(turns.get("own")![1], 0);
data.overstressed = undefined;
visual.update!();
assertEquals(turns.get("own")![1], 16);
});
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));
});