Files
bworld/server/game/fluid_handlers.ts
2026-11-14 12:18:41 -03:00

125 lines
4.9 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the fluid handlers the engine makes for server phases, see ServerFluidApi in mod_api/server.ts: fluid containers',
// tanks scripts keep, and pouring from one handler into another
import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
import { contained_fluid } from "@/common/inventory.ts";
import type {
FluidAction,
FluidHandler,
FluidHandlerItem,
FluidStack,
ItemStack,
Player,
TankHandlerOptions,
} from "@/common/mod_api/server.ts";
import { same_fluid } from "@/common/fluids.ts";
// one of a stack of fluid containers, full of its whole capacity or empty, undefined for an item that isn't one
export function item_handler(stack: ItemStack): FluidHandlerItem | undefined {
const capacity = EverythingRegistry.get<ItemRegistry>("items", stack.id)?.fluid_container?.capacity;
if (capacity === undefined) return undefined;
let fluid = contained_fluid(stack.id, stack.data);
return {
get container() {
return fluid ? { id: stack.id, count: 1, data: { fluid } } : { id: stack.id, count: 1 };
},
tanks: () => 1,
fluid_in_tank: () => fluid && { fluid, amount: capacity },
tank_capacity: () => capacity,
is_fluid_valid: () => true,
fill(stack, action) {
if (fluid || stack.amount < capacity) return 0;
if (action === "execute") fluid = stack.fluid;
return capacity;
},
drain(what, action) {
if (!fluid) return undefined;
const wanted = typeof what === "number" ? what : same_fluid(what.fluid, fluid) ? what.amount : 0;
if (wanted < capacity) return undefined;
const drained = { fluid, amount: capacity };
if (action === "execute") fluid = undefined;
return drained;
},
};
}
export function tank_handler(options: TankHandlerOptions): FluidHandler {
const { tanks, capacity, get, set } = options;
return {
tanks: () => tanks,
fluid_in_tank: (tank) => get()[tank],
tank_capacity: () => capacity,
is_fluid_valid: () => true,
fill(stack, action) {
if (!EverythingRegistry.get("fluids", stack.fluid.id)) throw new Error(`unknown fluid ${stack.fluid.id}`);
if (!(stack.amount > 0)) return 0;
const now = get();
const index = now.findIndex((tank) => same_fluid(tank.fluid, stack.fluid));
if (index === -1 && now.length >= tanks) return 0;
const had = index === -1 ? 0 : now[index].amount;
const filled = Math.min(stack.amount, capacity - had);
if (filled > 0 && action === "execute") {
const into = { fluid: stack.fluid, amount: had + filled };
set(index === -1 ? [...now, into] : now.with(index, into));
}
return Math.max(0, filled);
},
drain(what, action) {
const now = get();
const index = typeof what === "number" ? 0 : now.findIndex((tank) => same_fluid(tank.fluid, what.fluid));
const tank = now[index];
if (!tank) return undefined;
const drained = Math.min(typeof what === "number" ? what : what.amount, tank.amount);
if (!(drained > 0)) return undefined;
if (action === "execute") {
const left = tank.amount - drained;
set(left > 0 ? now.with(index, { ...tank, amount: left }) : now.toSpliced(index, 1));
}
return { fluid: tank.fluid, amount: drained };
},
};
}
// what to gives of what from would, then that much taken from from and put into to
export function transfer(from: FluidHandler, to: FluidHandler, max: number): FluidStack | undefined {
const offered = from.drain(max, "simulate");
if (!offered) return undefined;
const taken = to.fill(offered, "simulate");
if (taken <= 0) return undefined;
const drained = from.drain({ fluid: offered.fluid, amount: taken }, "execute");
if (!drained) return undefined;
return { fluid: drained.fluid, amount: to.fill(drained, "execute") };
}
export function interact(player: Player, handler: FluidHandler): boolean {
const held = player.held_item;
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;
}