101 lines
4.5 KiB
TypeScript
101 lines
4.5 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// what a basin holds, like create's BasinBlockEntity's input inventory and tanks: up to BASIN_SLOTS kinds of item, a
|
|
// stack of each, and BASIN_TANKS tanks with a fluid each. a kind of item or fluid only ever goes where there's already
|
|
// some of it, so there's never two stacks or two tanks of one. no ctx, so the server keeps it and the client draws it
|
|
// from the same shapes
|
|
import type { FluidStack, FluidVariant, ItemStack } from "bworld/shared";
|
|
|
|
export const BASIN_SLOTS = 9;
|
|
export const BASIN_TANKS = 2;
|
|
export const BASIN_TANK_CAPACITY = 1000;
|
|
|
|
// how many of stack go into items, and what items is after: into the stack of the same item, or a new one while
|
|
// there's room for another kind, up to max a stack. like BasinInventory.insertItem
|
|
export function insert_item(items: readonly ItemStack[], stack: ItemStack, max: number) {
|
|
const index = items.findIndex((item) => same_item(item, stack));
|
|
if (index === -1 && items.length >= BASIN_SLOTS) return { items, inserted: 0 };
|
|
const had = index === -1 ? 0 : items[index].count;
|
|
const inserted = Math.max(0, Math.min(stack.count, max - had));
|
|
if (inserted === 0) return { items, inserted };
|
|
const into = { ...stack, count: had + inserted };
|
|
return { items: index === -1 ? [...items, into] : items.with(index, into), inserted };
|
|
}
|
|
|
|
// tanks with amount of fluid poured in, or undefined when it doesn't all fit: into the tank that has that fluid, or else
|
|
// an empty one, like SmartFluidTankBehaviour's tanks that each keep to one fluid
|
|
export function fill(
|
|
tanks: readonly FluidStack[],
|
|
fluid: FluidVariant,
|
|
amount: number,
|
|
same: (a: FluidVariant, b: FluidVariant) => boolean,
|
|
): FluidStack[] | undefined {
|
|
const index = tanks.findIndex((tank) => same(tank.fluid, fluid));
|
|
if (index === -1) {
|
|
return tanks.length < BASIN_TANKS && amount <= BASIN_TANK_CAPACITY ? [...tanks, { fluid, amount }] : undefined;
|
|
}
|
|
const total = tanks[index].amount + amount;
|
|
return total <= BASIN_TANK_CAPACITY ? tanks.with(index, { fluid, amount: total }) : undefined;
|
|
}
|
|
|
|
// whether two stacks are of one item: the same id, and the same data whatever order its keys are in
|
|
export function same_item(a: ItemStack, b: ItemStack): boolean {
|
|
return a.id === b.id && canonical(a.data) === canonical(b.data);
|
|
}
|
|
|
|
function canonical(value: unknown): string {
|
|
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
if (value && typeof value === "object") {
|
|
const entries = Object.entries(value).sort(([a], [b]) => a < b ? -1 : 1);
|
|
return `{${entries.map(([key, v]) => `${JSON.stringify(key)}:${canonical(v)}`).join(",")}}`;
|
|
}
|
|
return JSON.stringify(value) ?? "";
|
|
}
|
|
|
|
// what's in a basin: its input items, the items recipes made, which recipes don't take again, and its tanks, like
|
|
// BasinBlockEntity's input and output inventories and its tanks. both inventories are BASIN_SLOTS kinds each
|
|
export interface BasinContents {
|
|
items: readonly ItemStack[];
|
|
outputs: readonly ItemStack[];
|
|
tanks: readonly FluidStack[];
|
|
}
|
|
|
|
// what a basin recipe needs: one item for each ingredient, and what it makes
|
|
export interface BasinRecipe {
|
|
// each ingredient as every item that fits it
|
|
ingredients: readonly (readonly string[])[];
|
|
results: readonly ItemStack[];
|
|
fluid_results: readonly FluidStack[];
|
|
}
|
|
|
|
// the basin once recipe has been made in it, or undefined when it can't be: an item for each ingredient comes out of
|
|
// its input items, the first stack that fits each, and the results have to fit in its outputs and tanks, like
|
|
// BasinRecipe.apply. max_stack is how many of an item a stack holds
|
|
export function apply_basin_recipe(
|
|
basin: BasinContents,
|
|
recipe: BasinRecipe,
|
|
max_stack: (item: ItemStack) => number,
|
|
same: (a: FluidVariant, b: FluidVariant) => boolean,
|
|
): BasinContents | undefined {
|
|
const items = basin.items.map((item) => ({ ...item }));
|
|
for (const ingredient of recipe.ingredients) {
|
|
const from = items.find((item) => item.count > 0 && ingredient.includes(item.id));
|
|
if (!from) return undefined;
|
|
from.count--;
|
|
}
|
|
let outputs = basin.outputs;
|
|
for (const result of recipe.results) {
|
|
const into = insert_item(outputs, result, max_stack(result));
|
|
if (into.inserted < result.count) return undefined;
|
|
outputs = into.items;
|
|
}
|
|
let tanks: readonly FluidStack[] = basin.tanks;
|
|
for (const { fluid, amount } of recipe.fluid_results) {
|
|
const filled = fill(tanks, fluid, amount, same);
|
|
if (!filled) return undefined;
|
|
tanks = filled;
|
|
}
|
|
return { items: items.filter((item) => item.count > 0), outputs, tanks };
|
|
}
|