Fluid handler

This commit is contained in:
2026-10-31 16:54:24 -03:00
parent 23d02c5229
commit 407ed77385
20 changed files with 602 additions and 127 deletions
+6 -20
View File
@@ -5,17 +5,12 @@
// 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";
import type { FluidStack, 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) {
@@ -31,11 +26,11 @@ export function insert_item(items: readonly ItemStack[], stack: ItemStack, max:
// 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[],
tanks: readonly FluidStack[],
fluid: FluidVariant,
amount: number,
same: (a: FluidVariant, b: FluidVariant) => boolean,
): BasinTank[] | undefined {
): 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;
@@ -44,15 +39,6 @@ export function fill(
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);
@@ -72,7 +58,7 @@ function canonical(value: unknown): string {
export interface BasinContents {
items: readonly ItemStack[];
outputs: readonly ItemStack[];
tanks: readonly BasinTank[];
tanks: readonly FluidStack[];
}
// what a basin recipe needs: one item for each ingredient, and what it makes
@@ -80,7 +66,7 @@ export interface BasinRecipe {
// each ingredient as every item that fits it
ingredients: readonly (readonly string[])[];
results: readonly ItemStack[];
fluid_results: readonly BasinTank[];
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
@@ -104,7 +90,7 @@ export function apply_basin_recipe(
if (into.inserted < result.count) return undefined;
outputs = into.items;
}
let tanks: readonly BasinTank[] = basin.tanks;
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;