Files
bworld/tests/create/press_test.ts
T
2026-10-15 13:07:09 -03:00

106 lines
3.9 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the mechanical press: turned, it slams down on items lying on the ground under it and presses them with create's
// pressing recipes, one at a time, and it only ticks while it turns
import { assert, assertEquals } from "@std/assert";
import { ItemStack } from "$/common/inventory.ts";
import { FACING, setup, visual_for } from "./helpers.ts";
// a press at 1 101 0 along x, turned by a crank on its west side, with the floor two blocks under it
async function press_with_crank() {
const t = await setup();
t.place(0, 101, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 101, 0, "create:mechanical_press", "facing", FACING.east);
// holding use on the crank, which clicks it every 4 ticks
const crank = (ticks: number) => {
for (let i = 0; i < ticks; i += 4) {
t.use(0, 101, 0);
t.ticks(4);
}
};
// what's lying around, added up by item
const lying = () => {
const counts: Record<string, number> = {};
for (const { item } of t.game.item_entities()) counts[item.type_id] = (counts[item.type_id] ?? 0) + item.amount;
return counts;
};
const ingots = (count: number) => t.game.spawn_item(1.5, 100, 0.5, new ItemStack("bworld:iron_ingot", count));
return { ...t, crank, lying, ingots };
}
Deno.test("a turning press slams down on an ingot lying under it and presses it into a sheet", async () => {
const t = await press_with_crank();
t.ingots(1);
// still, it waits
t.ticks(100);
assertEquals(t.lying(), { "bworld:iron_ingot": 1 });
t.crank(200);
assertEquals(t.lying(), { "create:iron_sheet": 1 });
});
Deno.test("from a stack, a press presses one at a time, and each lands beside the rest", async () => {
const t = await press_with_crank();
t.ingots(3);
for (let i = 0; i < 100 && !t.lying()["create:iron_sheet"]; i++) t.crank(4);
assertEquals(t.lying(), { "bworld:iron_ingot": 2, "create:iron_sheet": 1 });
assertEquals(t.game.item_entities().length, 2);
t.crank(600);
assertEquals(t.lying(), { "create:iron_sheet": 3 });
});
Deno.test("a press only ticks while it turns", async () => {
const t = await press_with_crank();
const ticking = () => {
const tile = t.game.world.get_tile(1, 101, 0);
return tile !== undefined && t.game.world.ticks(tile);
};
assertEquals(ticking(), false);
t.crank(8);
assertEquals(ticking(), true);
// the crank lets go and the press stops, nothing under it to press
t.ticks(40);
assertEquals(ticking(), false);
});
Deno.test("a press lines up with an axle beside it, and otherwise faces the player", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give create:mechanical_press 64" });
t.send(1, { type: "select_slot", slot: 0 });
// alice looks north
t.use(-2, 99, 0, "top");
assertEquals(t.state(-2, 100, 0, "facing"), FACING.south);
t.place(2, 100, 0, "create:shaft", "axis", 0);
t.use(3, 99, 0, "top");
assertEquals(t.state(3, 100, 0, "facing"), FACING.west);
});
Deno.test("a press's head comes down to press halfway through its cycle and goes back up, as the server says", async () => {
const { visual, turns, transforms, data } = await visual_for(
"create:mechanical_press",
{ facing: FACING.east },
{ speed: 32 },
);
// the shaft through it turns along its facing
const shaft = [...turns].find(([model]) => model.includes("shaft"))![1];
assertEquals(shaft.slice(0, 2), ["x", 32]);
// how far down the head is, from its transform
const down = () => -([...transforms].find(([model]) => model.includes("head"))?.[1][13] ?? 0);
visual.on_frame!(5);
assertEquals(down(), 0);
// the server says a cycle started, 4 steps of 240 a tick
data.press = { steps: 0, rate: 4 };
visual.update!();
visual.on_frame!(10);
assertEquals(down(), 0);
visual.on_frame!(25);
assert(down() > 0 && down() < 1, `${down()}`);
visual.on_frame!(40);
assertEquals(down(), 1);
visual.on_frame!(70);
assertEquals(down(), 0);
});