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

186 lines
6.7 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// farmer's delight's cooking pot: cooks its recipes over a lit furnace, and serves meals into bowls
import { assert, assertEquals } from "@std/assert";
import { ItemStack } from "@/common/inventory.ts";
import { bake_model } from "@/common/block_models.ts";
import { validate_recipe } from "@/common/mod_data.ts";
import { get_state_value, set_state_value } from "@/common/utils.ts";
import { stone_floor, test_game } from "./helpers.ts";
const POT = [0, 101, 0] as const;
const FURNACE = [0, 100, 0] as const;
async function setup() {
const t = await test_game("mods");
t.join(1, "alice");
stone_floor(t.game);
t.send(1, { type: "move", x: 2.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
t.game.set_block(...FURNACE, "bworld:furnace");
t.game.set_block(...POT, "bworld:cooking_pot");
const container = (at: readonly [number, number, number]) => {
const id = (t.game.world.get_tile(...at)?.mod_data as { container: string }).container;
return t.game.containers.get(id)!;
};
const pot = container(POT);
const put = (slot: number, id: string, count = 1) => pot.set_item(slot, new ItemStack(id, count));
const slot = (slot: number) => {
const item = pot.get_item(slot);
return item ? `${item.type_id} ${item.amount}` : "";
};
// something to smelt and coal, so the furnace burns
const light_furnace = () => {
container(FURNACE).set_item(0, new ItemStack("bworld:oak_log", 64));
container(FURNACE).set_item(1, new ItemStack("bworld:coal", 64));
};
const ticks = (n: number) => {
for (let i = 0; i < n; i++) t.game.tick();
};
return { ...t, pot, put, slot, light_furnace, ticks };
}
Deno.test("the pot only cooks over a lit furnace, then serves the meal into bowls", async () => {
const t = await setup();
t.put(0, "bworld:tomato", 2);
t.put(3, "bworld:tomato", 2);
// cold: nothing happens
t.ticks(150);
assertEquals(t.slot(0), "bworld:tomato 2");
assertEquals(t.slot(6), "");
t.light_furnace();
t.ticks(1);
assertEquals(t.game.world.get_block_id(...FURNACE), "bworld:furnace");
t.ticks(100);
// one sauce, one tomato from each slot, waiting in the pot for a bowl
assertEquals(t.slot(6), "bworld:tomato_sauce 1");
assertEquals(t.slot(0), "bworld:tomato 1");
assertEquals(t.slot(3), "bworld:tomato 1");
assertEquals(t.slot(8), "");
t.ticks(100);
assertEquals(t.slot(6), "bworld:tomato_sauce 2");
assertEquals(t.slot(0), "");
// bowls take it out
t.put(7, "bworld:bowl", 5);
t.ticks(1);
assertEquals(t.slot(6), "");
assertEquals(t.slot(7), "bworld:bowl 3");
assertEquals(t.slot(8), "bworld:tomato_sauce 2");
});
Deno.test("ingredients go in any slots, lists take any of their items, and meals without a container come right out", async () => {
const t = await setup();
t.light_furnace();
t.put(5, "bworld:potato");
t.put(1, "bworld:cabbage");
t.ticks(101);
assertEquals(t.slot(8), "bworld:cabbage_rolls 1");
assertEquals(t.slot(6), "");
// something extra matches nothing
t.put(0, "bworld:cabbage");
t.put(1, "bworld:onion");
t.put(2, "bworld:rice");
t.ticks(300);
assertEquals(t.slot(8), "bworld:cabbage_rolls 1");
assertEquals(t.slot(0), "bworld:cabbage 1");
// a different meal waits until the pot's empty of the other
t.pot.set_item(0, undefined);
t.pot.set_item(1, undefined);
t.ticks(100);
assertEquals(t.slot(6), "bworld:cooked_rice 1");
t.put(0, "bworld:tomato", 2);
t.put(1, "bworld:tomato", 2);
t.ticks(300);
assertEquals(t.slot(6), "bworld:cooked_rice 1");
assertEquals(t.slot(0), "bworld:tomato 2");
});
Deno.test("holding a bowl serves the meal straight into your hand, and breaking the pot drops everything", async () => {
const t = await setup();
t.light_furnace();
t.put(0, "bworld:rice", 3);
t.ticks(301);
assertEquals(t.slot(6), "bworld:cooked_rice 3");
t.send(1, { type: "chat", text: "/give bworld:bowl 2" });
const inventory = () => t.game.players()[0].inventory.to_data().filter((i) => i);
t.send(1, { type: "select_slot", slot: 0 });
t.send(1, { type: "use_block", x: POT[0], y: POT[1], z: POT[2], face: "top" });
assertEquals(t.slot(6), "bworld:cooked_rice 2");
assertEquals(inventory().map((i) => `${i!.id} ${i!.count}`), ["bworld:bowl 1", "bworld:cooked_rice 1"]);
t.game.destroy_block(...POT);
const dropped = t.game.item_entities().map((e) => `${e.item.type_id} ${e.item.amount}`);
assert(dropped.includes("bworld:cooked_rice 2"), String(dropped));
});
Deno.test("mods' own recipe types are accepted, and model faces can turn their texture", () => {
assertEquals(validate_recipe({ type: "bworld:cooking", anything: 1 }), []);
assertEquals(validate_recipe({ type: "cooking" }).length, 1);
const face = (rotation?: number) =>
bake_model({
id: "test:m",
elements: [{
from: [0, 0, 0],
to: [16, 16, 16],
faces: { top: { texture: "test:t", uv: [0, 0, 8, 4], rotation } },
}],
}, undefined)[0].uvs;
assertEquals(face(), [0, 4, 8, 4, 8, 0, 0, 0]);
// each corner gets the next corner's uv
assertEquals(face(90), [8, 4, 8, 0, 0, 0, 0, 4]);
assertEquals(face(180), [8, 0, 0, 0, 0, 4, 8, 4]);
});
Deno.test("a stove is placed lit and heats the pot, coal relights it, and standing on it burns", async () => {
const t = await setup();
const info = () => t.game.world.get_block_info(...FURNACE)!;
const lit = () => get_state_value(t.game.world.get_block_value(...FURNACE), info(), "lit");
t.game.set_block(...FURNACE, "bworld:stove");
assertEquals(lit(), 1);
t.put(0, "bworld:rice");
t.ticks(101);
assertEquals(t.slot(6), "bworld:cooked_rice 1");
// out, it doesn't cook
t.game.set_block_state(
...FURNACE,
set_state_value(t.game.world.get_block_value(...FURNACE), info(), "lit", 0)! >>> 16,
);
t.put(0, "bworld:rice");
t.ticks(150);
assertEquals(t.slot(6), "bworld:cooked_rice 1");
// coal lights it again, used up
t.send(1, { type: "chat", text: "/give bworld:coal 2" });
t.send(1, { type: "select_slot", slot: 0 });
t.send(1, { type: "use_block", x: FURNACE[0], y: FURNACE[1], z: FURNACE[2], face: "north" });
assertEquals(lit(), 1);
assertEquals(t.game.players()[0].inventory.get_item(0)?.amount, 1);
// already lit, coal does nothing
t.send(1, { type: "use_block", x: FURNACE[0], y: FURNACE[1], z: FURNACE[2], face: "north" });
assertEquals(t.game.players()[0].inventory.get_item(0)?.amount, 1);
// standing on it: 1 every 10 ticks
t.game.destroy_block(...POT);
t.send(1, { type: "move", x: 0.5, y: 101, z: 0.5, yaw: 0, pitch: 0 });
const health = () => t.game.players()[0].health;
// (full hunger heals some of it back meanwhile, like minecraft)
const before = health();
t.ticks(40);
assert(health() < before, `${health()} of ${before}`);
t.send(1, { type: "move", x: 2.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
const off = health();
t.ticks(40);
assert(health() >= off);
});