Fluid handler
This commit is contained in:
@@ -15,7 +15,7 @@ import type {
|
||||
Visual,
|
||||
VisualBlock,
|
||||
} from "bworld/client";
|
||||
import type { Face } from "bworld/shared";
|
||||
import type { Face, FluidStack } from "bworld/shared";
|
||||
import {
|
||||
actual_speed,
|
||||
type Axis,
|
||||
@@ -28,7 +28,7 @@ import {
|
||||
OPPOSITE,
|
||||
} from "./kinetics/connections.ts";
|
||||
import { BASIN_TRAVEL, head_offset, type PressShown } from "./processing/press.ts";
|
||||
import { BASIN_TANK_CAPACITY, BASIN_TANKS, type BasinTank } from "./processing/basin.ts";
|
||||
import { BASIN_TANK_CAPACITY, BASIN_TANKS } from "./processing/basin.ts";
|
||||
|
||||
// a model reaching north turned to reach each way
|
||||
const facing_turns = (model: string): Record<Face, ModelRef> => ({
|
||||
@@ -244,7 +244,7 @@ function basin_visual(block: VisualBlock, instances: Instances): Visual {
|
||||
for (const instance of shown) instance.delete();
|
||||
shown = [];
|
||||
floating = [];
|
||||
const tanks = block.get_client_data<BasinTank[]>("tanks") ?? [];
|
||||
const tanks = block.get_client_data<FluidStack[]>("tanks") ?? [];
|
||||
const total = tanks.reduce((sum, tank) => sum + tank.amount, 0);
|
||||
const full = 1 - (1 - Math.min(1, total / (BASIN_TANKS * BASIN_TANK_CAPACITY))) ** 2;
|
||||
const top = total > 0 ? 2 + 12 * full : 0;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// create's processing recipes, the shape its machines' recipe types share, like ProcessingRecipe: item choices in, and
|
||||
// stacks out, each with a chance, and for the types that can, fluids out as { fluid, amount }. read and checked when the server starts, so a broken one stops it there instead of
|
||||
// in a machine. no ctx, tests give what it needs
|
||||
import type { FluidVariant, Id } from "bworld/shared";
|
||||
import type { FluidStack, Id } from "bworld/shared";
|
||||
|
||||
// how many ingredients, item results and fluid results a type takes at most, like getMaxInputCount,
|
||||
// getMaxOutputCount and getMaxFluidOutputCount
|
||||
@@ -31,7 +31,7 @@ export interface ProcessingRecipe {
|
||||
ingredients: Id[][];
|
||||
results: ProcessingResult[];
|
||||
// in mB
|
||||
fluid_results: { fluid: FluidVariant; amount: number }[];
|
||||
fluid_results: FluidStack[];
|
||||
}
|
||||
|
||||
// what reading recipes needs from the game
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
// create's blocks that do something: so far what turns (create:kinetic), the generators that turn it (the hand crank,
|
||||
// the creative motor and the water wheel), the mechanical press and the basin
|
||||
import type { BlockRef, EntityRef, FluidVariant, Id, ItemStack, ServerContext } from "bworld/server";
|
||||
import type { BlockRef, EntityRef, FluidStack, Id, ItemStack, ServerContext } from "bworld/server";
|
||||
import {
|
||||
actual_speed,
|
||||
along,
|
||||
@@ -30,10 +30,9 @@ import {
|
||||
import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./processing/press.ts";
|
||||
import {
|
||||
apply_basin_recipe,
|
||||
BASIN_TANK_CAPACITY,
|
||||
BASIN_TANKS,
|
||||
type BasinContents,
|
||||
type BasinTank,
|
||||
drain,
|
||||
fill,
|
||||
insert_item,
|
||||
} from "./processing/basin.ts";
|
||||
|
||||
@@ -214,15 +213,20 @@ export function setup(ctx: ServerContext) {
|
||||
register_basin(ctx);
|
||||
}
|
||||
|
||||
// holds items dropped into it and fluid poured in, like create's BasinBlock: an item that lands in it goes in, a full
|
||||
// fluid container used on it empties into it and an empty one fills from it, and an empty hand takes every item out.
|
||||
// what's in it is its "items" and "tanks" client data, kept nowhere else, which its visual draws. breaking it drops
|
||||
// the items, and the fluid is lost
|
||||
// holds items dropped into it and fluid poured in, like create's BasinBlock: an item that lands in it goes in, and an
|
||||
// empty hand takes every item out. its tanks are its fluid handler, which a full fluid container used on it empties
|
||||
// into and an empty one fills from. what's in it is its "items" and "tanks" client data, kept nowhere else, which its
|
||||
// visual draws. breaking it drops the items, and the fluid is lost
|
||||
function register_basin(ctx: ServerContext) {
|
||||
const items_of = (block: BlockRef) => basin_contents(block).items;
|
||||
const tanks_of = (block: BlockRef) => basin_contents(block).tanks;
|
||||
const set_items = (block: BlockRef, items: readonly ItemStack[]) => set_basin(block, { items });
|
||||
const set_tanks = (block: BlockRef, tanks: readonly BasinTank[]) => set_basin(block, { tanks });
|
||||
const tanks = (block: BlockRef) =>
|
||||
ctx.fluids.tank_handler({
|
||||
tanks: BASIN_TANKS,
|
||||
capacity: BASIN_TANK_CAPACITY,
|
||||
get: () => basin_contents(block).tanks,
|
||||
set: (tanks) => set_basin(block, { tanks }),
|
||||
});
|
||||
|
||||
ctx.components.register_block("create:basin", {
|
||||
on_entity_stand(block, _params, entity) {
|
||||
@@ -241,23 +245,9 @@ function register_basin(ctx: ServerContext) {
|
||||
set_basin(block, { items: [], outputs: [] });
|
||||
return true;
|
||||
}
|
||||
const capacity = ctx.items.fluid_capacity(held.id);
|
||||
// anything else is used or placed as if the basin weren't there
|
||||
if (capacity === undefined) return false;
|
||||
const fluid = (held.data as { fluid?: FluidVariant } | undefined)?.fluid;
|
||||
if (fluid) {
|
||||
const tanks = fill(tanks_of(block), fluid, capacity, (a, b) => ctx.fluids.same(a, b));
|
||||
if (tanks) {
|
||||
set_tanks(block, tanks);
|
||||
player.exchange_held({ id: held.id, count: 1 });
|
||||
}
|
||||
} else {
|
||||
const drained = drain(tanks_of(block), capacity);
|
||||
if (drained) {
|
||||
set_tanks(block, drained.tanks);
|
||||
player.exchange_held({ id: held.id, count: 1, data: { fluid: drained.fluid } });
|
||||
}
|
||||
}
|
||||
if (ctx.items.fluid_capacity(held.id) === undefined) return false;
|
||||
ctx.fluids.interact(player, tanks(block));
|
||||
// a fluid container is used up on the basin even when nothing fits, like create's
|
||||
return true;
|
||||
},
|
||||
@@ -265,6 +255,7 @@ function register_basin(ctx: ServerContext) {
|
||||
const { items, outputs } = basin_contents(block);
|
||||
for (const item of [...items, ...outputs]) ctx.world.drop_item(block.x, block.y, block.z, item);
|
||||
},
|
||||
fluid_handler: (block) => tanks(block),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -273,7 +264,7 @@ function basin_contents(block: BlockRef): BasinContents {
|
||||
return {
|
||||
items: block.get_client_data<ItemStack[]>("items") ?? [],
|
||||
outputs: block.get_client_data<ItemStack[]>("outputs") ?? [],
|
||||
tanks: block.get_client_data<BasinTank[]>("tanks") ?? [],
|
||||
tanks: block.get_client_data<FluidStack[]>("tanks") ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user