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
+102
View File
@@ -0,0 +1,102 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the fluid handlers the engine makes for server scripts, 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 {
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 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;
}
+31 -1
View File
@@ -25,6 +25,7 @@ import { physics } from "$/common/physics.ts";
import { clock } from "$/common/time.ts";
import { BlockRules } from "$/common/block_rules.ts";
import { FluidRules } from "$/common/fluids.ts";
import { interact, item_handler, tank_handler, transfer } from "./fluid_handlers.ts";
import type { SharedContext } from "$/common/mod_api/shared.ts";
import { tag_api } from "$/common/tags.ts";
import { loading_mod } from "$/common/hooks.ts";
@@ -38,6 +39,8 @@ import type {
EffectComponent,
EntityRef,
EventSignal,
Face,
FluidHandler,
ItemComponent,
ItemStack,
LivingRef,
@@ -532,6 +535,26 @@ export class ModRuntime {
}
#components_with = new Map<keyof BlockComponent, Map<string, BlockComponentOf[]>>();
// the fluid handler of the block at x y z reached from face, from the first of its components with one
block_fluid_handler(x: number, y: number, z: number, face: Face | undefined): FluidHandler | undefined {
if (face !== undefined && !faces.includes(face)) {
throw new Error(`a face is one of ${faces.join(", ")}, not ${face}`);
}
const id = this.game.world.get_block_id(x, y, z);
const components = this.components_with(id, "fluid_handler");
if (components.length === 0) return undefined;
const block = this.block_ref(x, y, z, id);
for (const { mod, id: component_id, component, params } of components) {
const handler = run_guarded(
mod,
`${component_id} fluid_handler`,
() => component.fluid_handler!(block, params, face),
);
if (handler) return handler;
}
return undefined;
}
// the block at x y z was told the one at from changed, and its rules have had their say: its components'
// on_neighbor_change
#neighbor_changed(x: number, y: number, z: number, from: Position) {
@@ -974,7 +997,14 @@ export class ModRuntime {
return is_solid(block);
},
},
fluids: this.fluid_rules.api(),
fluids: {
...this.fluid_rules.api(),
block_handler: (x, y, z, face) => this.block_fluid_handler(x, y, z, face),
item_handler,
tank_handler,
transfer,
interact,
},
recipes: {
furnace_result: (input) => {
const recipe = game().recipes.furnace.get(input);