69 lines
3.2 KiB
TypeScript
69 lines
3.2 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 { FluidVariant, ItemStack } from "bworld/shared";
|
|
|
|
export const BASIN_SLOTS = 9;
|
|
export const BASIN_TANKS = 2;
|
|
export const BASIN_TANK_CAPACITY = 1000;
|
|
|
|
export interface BasinTank {
|
|
fluid: FluidVariant;
|
|
amount: number;
|
|
}
|
|
|
|
// 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 BasinTank[],
|
|
fluid: FluidVariant,
|
|
amount: number,
|
|
same: (a: FluidVariant, b: FluidVariant) => boolean,
|
|
): BasinTank[] | 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;
|
|
}
|
|
|
|
// amount of the first tank's fluid taken out, and the tanks left, or undefined when it has less than that. like
|
|
// filling an item from CombinedTankWrapper, which drains the first tank with any
|
|
export function drain(tanks: readonly BasinTank[], amount: number) {
|
|
const [first, ...rest] = tanks;
|
|
if (!first || first.amount < amount) return undefined;
|
|
const left = first.amount - amount;
|
|
return { fluid: first.fluid, tanks: left > 0 ? [{ ...first, amount: left }, ...rest] : rest };
|
|
}
|
|
|
|
// 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) ?? "";
|
|
}
|