Files
bworld/tests/create/press_test.ts
T

194 lines
7.7 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, or with a basin under it makes compacting recipes in that, and it only ticks while
// it turns
import { assert, assertEquals } from "@std/assert";
import { ItemStack } from "$/common/inventory.ts";
import { BASIN_TANK_CAPACITY } from "$/mods/create/scripts/processing/basin.ts";
import { BASIN_TRAVEL, PRESS_AT } from "$/mods/create/scripts/processing/press.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(config?: Record<string, unknown>) {
const t = await setup(config);
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("an overstressed press doesn't press", async () => {
// it takes more than the crank makes
const t = await press_with_crank({
stress: { capacity: { "create:hand_crank": 1 }, impact: { "create:mechanical_press": 2 } },
});
t.ingots(1);
t.crank(200);
assertEquals(t.lying(), { "bworld:iron_ingot": 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);
});
// a press at 1 102 0 turned by a crank, over a basin at 1 100 0 with a block between them, and what's in the basin
async function press_over_basin() {
const t = await setup();
t.place(1, 100, 0, "create:basin", "facing", 4);
t.place(0, 102, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 102, 0, "create:mechanical_press", "facing", FACING.east);
const crank = (ticks: number) => {
for (let i = 0; i < ticks; i += 4) {
t.use(0, 102, 0);
t.ticks(4);
}
};
const client_data = (field: string) => t.game.world.get_tile(1, 100, 0)?.client_data?.[field];
const tanks = () => client_data("tanks") as { fluid: unknown; amount: number }[] | undefined ?? [];
const count = (id: string) =>
(client_data("items") as { id: string; count: number }[] | undefined ?? [])
.find((item) => item.id === id)?.count ?? 0;
const drop = (id: string, count: number) => t.game.spawn_item(1.5, 101.5, 0.5, new ItemStack(id, count));
// the compacting recipe for id's juice, which bworld makes for every juicing recipe while create is there
const juice_of = (id: string) => {
const recipe = t.game.recipes.by_type.get("create:compacting")!
.find((recipe) => (recipe.ingredients as string[])[0] === id)!;
return (recipe.results as { fluid: unknown; amount: number }[])[0];
};
return { ...t, crank, tanks, count, drop, juice_of };
}
Deno.test("a press over a basin makes compacting recipes in it, like juice out of carrots, one each time", async () => {
const t = await press_over_basin();
t.drop("bworld:carrot", 3);
// with something no compacting recipe takes beside them, which it leaves
t.drop("bworld:iron_ingot", 1);
t.ticks(20);
assertEquals([t.count("bworld:carrot"), t.count("bworld:iron_ingot")], [3, 1]);
assertEquals(t.tanks(), []);
for (let i = 0; i < 200 && t.count("bworld:carrot") === 3; i++) t.crank(4);
assertEquals(t.count("bworld:carrot"), 2);
const juice = t.juice_of("bworld:carrot");
assertEquals(t.tanks(), [juice]);
t.crank(600);
assertEquals([t.count("bworld:carrot"), t.count("bworld:iron_ingot")], [0, 1]);
assertEquals(t.tanks(), [{ ...juice, amount: 3 * juice.amount }]);
// it presses into the basin, not on what's around it, so the iron isn't pressed into a sheet
assertEquals(t.game.item_entities().length, 0);
});
Deno.test("a press over a basin stops once what a recipe makes won't fit", async () => {
const t = await press_over_basin();
const per_melon = t.juice_of("bworld:melon").amount;
const fit = Math.floor(BASIN_TANK_CAPACITY / per_melon);
t.drop("bworld:melon", fit + 1);
t.ticks(20);
t.crank(400 * (fit + 1));
assertEquals(t.count("bworld:melon"), 1);
// turning on, it doesn't come down on the one left
t.crank(100);
assertEquals(t.game.world.get_tile(1, 102, 0)?.client_data?.press, undefined);
});
Deno.test("a press's head comes further down into a basin", async () => {
const { visual, transforms, data } = await visual_for("create:mechanical_press", { facing: FACING.east }, {
speed: 32,
press: { steps: PRESS_AT, rate: 0, basin: true },
});
const down = () => -[...transforms].find(([model]) => model.includes("head"))![1][13];
visual.on_frame!(0);
assertEquals(down(), BASIN_TRAVEL);
data.press = { steps: PRESS_AT, rate: 0 };
visual.update!();
visual.on_frame!(1);
assertEquals(down(), 1);
});