Fluid handler
This commit is contained in:
@@ -8,6 +8,7 @@ import type {
|
||||
ClientContext,
|
||||
ClientMenu,
|
||||
Color,
|
||||
FluidStack,
|
||||
Graphics,
|
||||
Instance,
|
||||
Instances,
|
||||
@@ -17,7 +18,7 @@ import type {
|
||||
VisualBlock,
|
||||
} from "bworld/client";
|
||||
import { MINIGAMES } from "./minigames/mod.ts";
|
||||
import { JUICER_CAPACITY, JUICER_PRESSES, type JuicerTank } from "./shared.ts";
|
||||
import { JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
|
||||
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
|
||||
|
||||
const WIDTH = 176;
|
||||
@@ -65,7 +66,7 @@ function juicer_visual(block: VisualBlock, instances: Instances): Visual {
|
||||
target = press_target();
|
||||
for (const instance of shown) instance.delete();
|
||||
shown = [];
|
||||
const tank = block.get_client_data<JuicerTank>("tank");
|
||||
const tank = block.get_client_data<FluidStack>("tank");
|
||||
if (tank) {
|
||||
const juice = instances.fluid(tank.fluid);
|
||||
const [low, high] = JUICER_INSIDE;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// bworld:juiceable, its own juice, and mods can add more like any recipe, as files or from their shared scripts.
|
||||
// whatever makes juice reads them, like the juicer. with create installed, each is also a create:compacting recipe,
|
||||
// so its press can make juice in a basin
|
||||
import type { FluidVariant, SharedContext, TagApi } from "bworld/shared";
|
||||
import type { FluidStack, SharedContext, TagApi } from "bworld/shared";
|
||||
import type { JuiceableParams } from "./shared.ts";
|
||||
|
||||
export const JUICING = "bworld:juicing";
|
||||
@@ -14,7 +14,7 @@ export interface JuicingRecipe {
|
||||
// the items it presses
|
||||
input: string[];
|
||||
// what one of them makes
|
||||
juice: { fluid: FluidVariant; amount: number };
|
||||
juice: FluidStack;
|
||||
}
|
||||
|
||||
// a recipe for every juiceable item, pressing one into the juice named after it, as much as its component says
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the base game's block and item behavior, written against the same api as any other mod
|
||||
import type { BlockRef, Container, Player, Season, ServerContext } from "bworld/server";
|
||||
import type { BlockRef, Container, FluidHandler, FluidStack, Player, Season, ServerContext } from "bworld/server";
|
||||
import { register_curios } from "./curios.ts";
|
||||
import { register_effects } from "./effects.ts";
|
||||
import { register_fishing } from "./fishing.ts";
|
||||
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
|
||||
import { register_verdigris } from "./verdigris/server.ts";
|
||||
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES, type JuicerTank } from "./shared.ts";
|
||||
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
|
||||
import { juicing_for, juicing_recipes } from "./juicing.ts";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
@@ -851,14 +851,14 @@ function register_buckets(ctx: ServerContext) {
|
||||
|
||||
// the juicer has no screen. right clicking it with something a juicing recipe takes puts that in, one kind at a time,
|
||||
// and pressing it (sneaking with an empty hand) JUICER_PRESSES times turns one of it into the recipe's juice, while
|
||||
// that fits in the tank. right clicking it with an empty fluid container fills it from the tank. breaking it drops what's in it, and the
|
||||
// juice is lost. clients see what's in it, its juice and how many times it's been pressed toward the next as its
|
||||
// "stack", "tank" and "presses" client data, which its visual draws (see scripts/client.ts)
|
||||
// that fits in the tank. its fluid handler only gives the juice out, so right clicking it with an empty fluid container
|
||||
// fills that from the tank. breaking it drops what's in it, and the juice is lost. clients see what's in it, its juice
|
||||
// and how many times it's been pressed toward the next as its "stack", "tank" and "presses" client data, which its
|
||||
// visual draws (see scripts/client.ts)
|
||||
interface JuicerData {
|
||||
container?: string;
|
||||
// how many times it's been pressed toward the next juice
|
||||
presses?: number;
|
||||
tank?: JuicerTank;
|
||||
}
|
||||
|
||||
function register_juicer(ctx: ServerContext) {
|
||||
@@ -871,6 +871,18 @@ function register_juicer(ctx: ServerContext) {
|
||||
const data = item?.data !== undefined && { data: item.data };
|
||||
block.set_client_data("stack", item && { id: item.id, count: item.count, ...data });
|
||||
};
|
||||
const tank = (block: BlockRef) =>
|
||||
ctx.fluids.tank_handler({
|
||||
tanks: 1,
|
||||
capacity: JUICER_CAPACITY,
|
||||
get: () => {
|
||||
const juice = block.get_client_data<FluidStack>("tank");
|
||||
return juice ? [juice] : [];
|
||||
},
|
||||
set: ([juice]) => block.set_client_data("tank", juice),
|
||||
});
|
||||
// what others reach, which juice only comes out of
|
||||
const handler = (block: BlockRef): FluidHandler => ({ ...tank(block), fill: () => 0 });
|
||||
|
||||
ctx.components.register_block("bworld:juicer", {
|
||||
on_interact(block, _params, player) {
|
||||
@@ -883,29 +895,16 @@ function register_juicer(ctx: ServerContext) {
|
||||
// true even when none fit, so the fruit isn't eaten instead
|
||||
return true;
|
||||
}
|
||||
|
||||
const capacity = held && ctx.items.fluid_capacity(held.id);
|
||||
if (held && capacity !== undefined) {
|
||||
if ((held.data as { fluid?: unknown } | undefined)?.fluid !== undefined) return false;
|
||||
const tank = data.tank;
|
||||
if (!tank || tank.amount < capacity) return false;
|
||||
const amount = tank.amount - capacity;
|
||||
const left = amount > 0 ? { ...tank, amount } : undefined;
|
||||
block.data = { ...data, tank: left } satisfies JuicerData;
|
||||
block.set_client_data("tank", left);
|
||||
player.exchange_held({ id: held.id, count: 1, data: { fluid: tank.fluid } });
|
||||
return true;
|
||||
if (held && ctx.items.fluid_capacity(held.id) !== undefined) {
|
||||
return ctx.fluids.interact(player, handler(block));
|
||||
}
|
||||
|
||||
if (held || !player.is_sneaking()) return false;
|
||||
const item = container.get(0);
|
||||
const recipe = item && juicing_for(recipes, item.id);
|
||||
if (!item || !recipe) return false;
|
||||
const { fluid: juice, amount } = recipe.juice;
|
||||
const tank = data.tank;
|
||||
const fits = (tank ? tank.amount : 0) + amount <= JUICER_CAPACITY &&
|
||||
(!tank || ctx.fluids.same(tank.fluid, juice));
|
||||
if (!fits) return false;
|
||||
const juice = tank(block);
|
||||
if (juice.fill(recipe.juice, "simulate") < recipe.juice.amount) return false;
|
||||
const presses = (data.presses ?? 0) + 1;
|
||||
if (presses < JUICER_PRESSES) {
|
||||
block.data = { ...data, presses } satisfies JuicerData;
|
||||
@@ -914,15 +913,15 @@ function register_juicer(ctx: ServerContext) {
|
||||
}
|
||||
item.count -= 1;
|
||||
show(block, container);
|
||||
const filled = { fluid: juice, amount: (tank?.amount ?? 0) + amount };
|
||||
block.data = { ...data, presses: 0, tank: filled } satisfies JuicerData;
|
||||
block.set_client_data("tank", filled);
|
||||
juice.fill(recipe.juice, "execute");
|
||||
block.data = { ...data, presses: 0 } satisfies JuicerData;
|
||||
block.set_client_data("presses", undefined);
|
||||
return true;
|
||||
},
|
||||
on_break(block) {
|
||||
drop_contents(ctx, block);
|
||||
},
|
||||
fluid_handler: (block) => handler(block),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// server and on every client, so players see a fence join or a trapdoor turn the moment they place it, and a trapdoor
|
||||
// or a door open the moment they click it. and the juice's rules, which name and color it by the item it's from, and
|
||||
// the recipes that make it
|
||||
import type { FluidVariant, SharedBlock, SharedContext } from "bworld/shared";
|
||||
import type { SharedBlock, SharedContext } from "bworld/shared";
|
||||
import { register_juicing } from "./juicing.ts";
|
||||
import { register_verdigris_rules } from "./verdigris/shared.ts";
|
||||
|
||||
@@ -147,15 +147,11 @@ export interface JuiceableParams {
|
||||
color: string;
|
||||
}
|
||||
|
||||
// how much juice the juicer holds, in mB, and the juice in it: in its tile data, and in its "tank" client data for its
|
||||
// visual to draw (see register_juicer in server.ts and juicer_visual in client.ts)
|
||||
// how much juice the juicer holds, in mB. the juice in it is its "tank" client data, a FluidStack, which its visual
|
||||
// draws (see register_juicer in server.ts and juicer_visual in client.ts)
|
||||
export const JUICER_CAPACITY = 1000;
|
||||
// how many presses make one of what's in it into juice. its visual lowers its press for each one toward the next
|
||||
export const JUICER_PRESSES = 4;
|
||||
export interface JuicerTank {
|
||||
fluid: FluidVariant;
|
||||
amount: number;
|
||||
}
|
||||
|
||||
// what juice sells for, as a multiple of what the items pressed to make it sell for
|
||||
export const JUICE_VALUE = 1.2;
|
||||
|
||||
@@ -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