Fluid slots and kegs !

This commit is contained in:
2026-10-31 17:35:27 -03:00
parent 65934d34a1
commit 658a6453b7
19 changed files with 494 additions and 34 deletions
+27 -5
View File
@@ -6,6 +6,7 @@
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
import { contained_fluid } from "$/common/inventory.ts";
import type {
FluidAction,
FluidHandler,
FluidHandlerItem,
FluidStack,
@@ -94,9 +95,30 @@ export function transfer(from: FluidHandler, to: FluidHandler, max: number): Flu
export function interact(player: Player, handler: FluidHandler): boolean {
const held = player.held_item;
const item = held && item_handler(held);
if (!item) return false;
const moved = transfer(handler, item, Infinity) ?? transfer(item, handler, Infinity);
if (moved) player.exchange_held(item.container);
return moved !== undefined;
const result = held && use_container(held, handler);
if (result) player.exchange_held(result);
return result !== undefined;
}
// one of a stack of fluid containers filled from handler, or else emptied into it unless it's only drained from, like
// a menu's output tank. with a tank, only that tank's fluid fills it. returns what the one became, undefined when it
// isn't a fluid container or no fluid moved
export function use_container(
stack: ItemStack,
handler: FluidHandler,
options: { tank?: number; drain_only?: boolean } = {},
): ItemStack | undefined {
const item = item_handler(stack);
if (!item) return undefined;
const { tank, drain_only } = options;
const from = tank === undefined ? handler : {
...handler,
drain(what: FluidStack | number, action: FluidAction) {
if (typeof what !== "number") return handler.drain(what, action);
const fluid = handler.fluid_in_tank(tank)?.fluid;
return fluid && handler.drain({ fluid, amount: what }, action);
},
};
const moved = transfer(from, item, Infinity) ?? (drain_only ? undefined : transfer(item, handler, Infinity));
return moved && item.container;
}