// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // create's basin: items dropped into it go in, a kind to a stack, fluid containers empty into it and fill from it, and // an empty hand takes the items out import { assert, assertEquals } from "@std/assert"; import { ItemStack } from "$/common/inventory.ts"; import type { FluidStack, ItemStack as Stack } from "bworld/shared"; import { apply_basin_recipe, BASIN_SLOTS, BASIN_TANK_CAPACITY, BASIN_TANKS, fill, insert_item, } from "$/mods/create/scripts/processing/basin.ts"; import { setup, visual_for } from "./helpers.ts"; const juice = (item: string) => ({ id: "bworld:juice", data: { item } }); const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b); Deno.test("a basin keeps one stack of each kind of item, and only so many kinds", () => { let { items } = insert_item([], { id: "a:iron", count: 40 }, 64); ({ items } = insert_item(items, { id: "a:gold", count: 1 }, 64)); const more = insert_item(items, { id: "a:iron", count: 40 }, 64); assertEquals(more.inserted, 24); assertEquals(more.items.map((item) => [item.id, item.count]), [["a:iron", 64], ["a:gold", 1]]); // a full stack doesn't spill into another assertEquals(insert_item(more.items, { id: "a:iron", count: 1 }, 64).inserted, 0); // stacks with other data are other kinds, whatever order their data's keys are in const marked = insert_item([], { id: "a:map", count: 1, data: { x: 1, y: 2 } }, 64).items; assertEquals(insert_item(marked, { id: "a:map", count: 1, data: { y: 2, x: 1 } }, 64).items.length, 1); assertEquals(insert_item(marked, { id: "a:map", count: 1, data: { x: 2, y: 2 } }, 64).items.length, 2); const kinds = Array.from({ length: BASIN_SLOTS }, (_, i) => ({ id: `a:item_${i}`, count: 1 })); let full: readonly Stack[] = []; for (const stack of kinds) full = insert_item(full, stack, 64).items; assertEquals(insert_item(full, { id: "a:another", count: 1 }, 64).inserted, 0); }); Deno.test("a basin recipe's fluids each go in a tank of their own", () => { const half = BASIN_TANK_CAPACITY / 2; let tanks: FluidStack[] = fill([], juice("apple"), half, same)!; tanks = fill(tanks, juice("apple"), half, same)!; assertEquals(tanks, [{ fluid: juice("apple"), amount: BASIN_TANK_CAPACITY }]); // it doesn't overflow into another tank assertEquals(fill(tanks, juice("apple"), 1, same), undefined); for (let i = 1; i < BASIN_TANKS; i++) tanks = fill(tanks, juice(`fruit_${i}`), half, same)!; assertEquals(tanks.length, BASIN_TANKS); assertEquals(fill(tanks, juice("another"), 1, same), undefined); }); Deno.test("a basin recipe takes an item for each ingredient, and puts what it makes where recipes don't take it", () => { const max = () => 64; const basin = { items: [{ id: "a:flint", count: 3 }, { id: "a:gravel", count: 1 }], outputs: [{ id: "a:andesite", count: 1 }], tanks: [], }; const andesite = { ingredients: [["a:flint"], ["a:flint"], ["a:gravel", "a:sand"]], results: [{ id: "a:andesite", count: 1 }], fluid_results: [{ fluid: juice("a:stone"), amount: 10 }], }; const made = apply_basin_recipe(basin, andesite, max, same)!; assertEquals(made.items, [{ id: "a:flint", count: 1 }]); assertEquals(made.outputs, [{ id: "a:andesite", count: 2 }]); assertEquals(made.tanks, [{ fluid: juice("a:stone"), amount: 10 }]); // what it made isn't taken again, and the one flint left isn't enough assertEquals(apply_basin_recipe(made, andesite, max, same), undefined); assertEquals( apply_basin_recipe({ ...basin, outputs: [] }, { ...andesite, ingredients: [["a:andesite"]] }, max, same), undefined, ); // nothing is made when what it makes doesn't all fit const full = { ...basin, tanks: [{ fluid: juice("a:stone"), amount: BASIN_TANK_CAPACITY }] }; assertEquals(apply_basin_recipe(full, andesite, max, same), undefined); }); // a basin at 0 100 0, and what's in it async function basin() { const t = await setup(); t.place(0, 100, 0, "create:basin"); const items = () => (t.game.world.get_tile(0, 100, 0)?.client_data?.items as { id: string; count: number }[] | undefined ?? []) .map(({ id, count }) => [id, count]); const tanks = () => t.game.world.get_tile(0, 100, 0)?.client_data?.tanks as FluidStack[] | undefined ?? []; const player = t.game.mods.player(t.game.players()[0]); const hold = (item: { id: string; count: number; data?: unknown } | undefined) => { player.inventory.set(0, item); t.send(1, { type: "select_slot", slot: 0 }); }; return { ...t, items, tanks, player, hold }; } Deno.test("items dropped into a basin go in, and an empty hand takes them out", async () => { const t = await basin(); t.game.spawn_item(0.5, 101.5, 0.5, new ItemStack("bworld:iron_ingot", 3)); t.game.spawn_item(0.5, 102.5, 0.5, new ItemStack("bworld:gold_ingot", 1)); t.ticks(40); assertEquals(t.items(), [["bworld:iron_ingot", 3], ["bworld:gold_ingot", 1]]); assertEquals(t.game.item_entities().length, 0); t.hold(undefined); t.use(0, 100, 0, "top"); assertEquals(t.items(), []); const held = [0, 1, 2, 3, 4, 5, 6, 7, 8].map((slot) => t.player.inventory.get(slot)).filter((item) => item); assertEquals(held.map((item) => [item!.id, item!.count]).sort(), [["bworld:gold_ingot", 1], [ "bworld:iron_ingot", 3, ]]); }); Deno.test("a full fluid container empties into a basin, and an empty one fills from it", async () => { const t = await basin(); t.hold({ id: "bworld:bucket", count: 1, data: { fluid: juice("bworld:apple") } }); t.use(0, 100, 0, "top"); assertEquals(t.tanks(), [{ fluid: juice("bworld:apple"), amount: 1000 }]); assertEquals(t.player.held_item?.data, undefined); // another bucket of it doesn't fit, so it stays full t.hold({ id: "bworld:bucket", count: 1, data: { fluid: juice("bworld:apple") } }); t.use(0, 100, 0, "top"); assertEquals(t.tanks().length, 1); assert(t.player.held_item?.data !== undefined); t.hold({ id: "bworld:bucket", count: 1 }); t.use(0, 100, 0, "top"); assertEquals(t.tanks(), []); assertEquals(t.player.held_item?.data, { fluid: juice("bworld:apple") }); }); Deno.test("a broken basin drops its items", async () => { const t = await basin(); t.game.spawn_item(0.5, 101.5, 0.5, new ItemStack("bworld:iron_ingot", 3)); t.ticks(40); t.game.destroy_block(0, 100, 0); const dropped = t.game.item_entities().map((e) => [e.item.type_id, e.item.amount]).sort(); assertEquals(dropped, [["bworld:iron_ingot", 3], ["create:basin", 1]]); }); Deno.test("a basin is drawn with its fluids side by side and a copy of each item for every few", async () => { const { visual, items, fluids, data } = await visual_for("create:basin", { facing: 4 }, { tanks: [{ fluid: juice("a"), amount: 500 }, { fluid: juice("b"), amount: 1500 }], items: [{ id: "bworld:iron_ingot", count: 1 }, { id: "bworld:gold_ingot", count: 20 }], }); // the second tank is three times as wide, and they reach as high as each other const [first, second] = [...fluids].map(({ box }) => box!); assertEquals(second[1][0] - second[0][0], 3 * (first[1][0] - first[0][0])); assertEquals(first[1][1], second[1][1]); assertEquals(first[1][0], second[0][0]); // more copies of the bigger stack const copies = (id: string) => [...items].filter((item) => item.id === id).length; assert(copies("bworld:gold_ingot") > copies("bworld:iron_ingot")); // bobbing on the fluid, the items move from frame to frame const before = [...items].map((item) => item.transform); visual.on_frame!(10); assert([...items].some((item, i) => JSON.stringify(item.transform) !== JSON.stringify(before[i]))); data.tanks = undefined; data.items = undefined; visual.update!(); assertEquals([items.size, fluids.size], [0, 0]); });