Juices on the mechanical press !

This commit is contained in:
2026-10-31 00:03:03 -03:00
parent 3734f5707a
commit 23d02c5229
11 changed files with 381 additions and 62 deletions
+11 -6
View File
@@ -27,7 +27,7 @@ import {
type KineticShape,
OPPOSITE,
} from "./kinetics/connections.ts";
import { head_offset, type PressShown } from "./processing/press.ts";
import { BASIN_TRAVEL, head_offset, type PressShown } from "./processing/press.ts";
import { BASIN_TANK_CAPACITY, BASIN_TANKS, type BasinTank } from "./processing/basin.ts";
// a model reaching north turned to reach each way
@@ -170,7 +170,8 @@ export function setup(ctx: ClientContext) {
},
on_frame(time) {
since ??= time;
const now = shown ? head_offset(shown.steps + (time - since) * shown.rate) : 0;
const travel = shown?.basin ? BASIN_TRAVEL : 1;
const now = shown ? head_offset(shown.steps + (time - since) * shown.rate) * travel : 0;
if (now === offset) return;
offset = now;
head.set_transform([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, -offset, 0, 1]);
@@ -231,9 +232,9 @@ function should_offset(axis: Axis, { x, y, z }: VisualBlock) {
// a basin's items are a copy more for every this many
const BASIN_ITEMS_A_COPY = 8;
// what's in a basin, by its "tanks" and "items" client data (see register_basin in server.ts), like BasinRenderer. its
// fluids fill the 12 pixels inside its walls side by side, as wide as their share of it, and as high as all of them
// make it, filling faster at first. its items lie in a ring over the fluid, tipped over and turned toward the middle,
// what's in a basin, by its "tanks", "items" and "outputs" client data (see register_basin in server.ts), like
// BasinRenderer. its fluids fill the 12 pixels inside its walls side by side, as wide as their share of it, and as high
// as all of them make it, filling faster at first. its items lie in a ring over the fluid, tipped over and turned toward the middle,
// one in the middle when it's alone, and bob up and down while there's fluid
function basin_visual(block: VisualBlock, instances: Instances): Visual {
let shown: Instance[] = [];
@@ -256,7 +257,11 @@ function basin_visual(block: VisualBlock, instances: Instances): Visual {
x += width;
}
const items = block.get_client_data<ItemStack[]>("items") ?? [];
// what's going in and what recipes made, together
const items = [
...block.get_client_data<ItemStack[]>("items") ?? [],
...block.get_client_data<ItemStack[]>("outputs") ?? [],
];
const level = Math.min(0.6, Math.max(0.125, top / 16 - 0.3));
const radius = items.length === 1 ? 0 : 0.125;
const random = seeded(block.x * 961 + block.y * 31 + block.z);
+46
View File
@@ -66,3 +66,49 @@ function canonical(value: unknown): string {
}
return JSON.stringify(value) ?? "";
}
// what's in a basin: its input items, the items recipes made, which recipes don't take again, and its tanks, like
// BasinBlockEntity's input and output inventories and its tanks. both inventories are BASIN_SLOTS kinds each
export interface BasinContents {
items: readonly ItemStack[];
outputs: readonly ItemStack[];
tanks: readonly BasinTank[];
}
// what a basin recipe needs: one item for each ingredient, and what it makes
export interface BasinRecipe {
// each ingredient as every item that fits it
ingredients: readonly (readonly string[])[];
results: readonly ItemStack[];
fluid_results: readonly BasinTank[];
}
// the basin once recipe has been made in it, or undefined when it can't be: an item for each ingredient comes out of
// its input items, the first stack that fits each, and the results have to fit in its outputs and tanks, like
// BasinRecipe.apply. max_stack is how many of an item a stack holds
export function apply_basin_recipe(
basin: BasinContents,
recipe: BasinRecipe,
max_stack: (item: ItemStack) => number,
same: (a: FluidVariant, b: FluidVariant) => boolean,
): BasinContents | undefined {
const items = basin.items.map((item) => ({ ...item }));
for (const ingredient of recipe.ingredients) {
const from = items.find((item) => item.count > 0 && ingredient.includes(item.id));
if (!from) return undefined;
from.count--;
}
let outputs = basin.outputs;
for (const result of recipe.results) {
const into = insert_item(outputs, result, max_stack(result));
if (into.inserted < result.count) return undefined;
outputs = into.items;
}
let tanks: readonly BasinTank[] = basin.tanks;
for (const { fluid, amount } of recipe.fluid_results) {
const filled = fill(tanks, fluid, amount, same);
if (!filled) return undefined;
tanks = filled;
}
return { items: items.filter((item) => item.count > 0), outputs, tanks };
}
+7 -1
View File
@@ -12,12 +12,18 @@ export const PRESS_AT = CYCLE / 2;
export const SCAN_TICKS = 10;
// a pressing press's "press" client data, what its visual moves the head by: the step of the cycle it was at when this
// was set, and how many steps a tick it's going
// was set, how many steps a tick it's going, and whether it's pressing into a basin, which its head goes further down
// into, by BASIN_TRAVEL
export interface PressShown {
steps: number;
rate: number;
basin?: boolean;
}
// how far down the head goes into a basin two blocks under it, in blocks, like PressingBehaviour.Mode.BASIN's
// headOffset. on the ground it's one
export const BASIN_TRAVEL = 22 / 16;
// steps of the cycle a tick at a speed in rpm, like getRunningTickSpeed: 1 almost still, up to 60 at 512 rpm
export function cycle_rate(speed: number): number {
if (speed === 0) return 0;
+37 -10
View File
@@ -2,18 +2,22 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// create's processing recipes, the shape its machines' recipe types share, like ProcessingRecipe: item choices in, and
// stacks out, each with a chance. read and checked when the server starts, so a broken one stops it there instead of
// 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 { Id } from "bworld/shared";
import type { FluidVariant, Id } from "bworld/shared";
// how many ingredients and results a type takes at most, like getMaxInputCount and getMaxOutputCount
// how many ingredients, item results and fluid results a type takes at most, like getMaxInputCount,
// getMaxOutputCount and getMaxFluidOutputCount
export interface ProcessingType {
readonly id: Id;
readonly inputs: number;
readonly outputs: number;
readonly fluid_outputs: number;
}
export const PRESSING: ProcessingType = { id: "create:pressing", inputs: 1, outputs: 2 };
export const PRESSING: ProcessingType = { id: "create:pressing", inputs: 1, outputs: 2, fluid_outputs: 0 };
// what a press does to what's in a basin under it, like BasinRecipe's limits
export const COMPACTING: ProcessingType = { id: "create:compacting", inputs: 64, outputs: 4, fluid_outputs: 2 };
export interface ProcessingResult {
id: Id;
@@ -23,9 +27,11 @@ export interface ProcessingResult {
}
export interface ProcessingRecipe {
// each ingredient as every item that fits it
// each ingredient as every item that fits it, one of it each
ingredients: Id[][];
results: ProcessingResult[];
// in mB
fluid_results: { fluid: FluidVariant; amount: number }[];
}
// what reading recipes needs from the game
@@ -33,6 +39,7 @@ export interface RecipeLookup {
// an item choice as its items, like ctx.tags.expand
expand(choice: Id | Id[]): Id[];
item_exists(id: Id): boolean;
fluid_exists(id: Id): boolean;
}
// every recipe of type, from their json, or an error saying what's wrong with each broken one
@@ -55,6 +62,7 @@ export function parse_processing(
const problems: string[] = [];
const ingredients: Id[][] = [];
const results: ProcessingResult[] = [];
const fluid_results: ProcessingRecipe["fluid_results"] = [];
if (!Array.isArray(json?.ingredients) || json.ingredients.length === 0) {
problems.push("ingredients must be a list of item choices");
} else {
@@ -62,6 +70,10 @@ export function parse_processing(
problems.push(`it takes ${json.ingredients.length} ingredients, and ${type.id} takes ${type.inputs}`);
}
for (const choice of json.ingredients) {
if (typeof choice === "object" && choice !== null && "fluid" in choice) {
problems.push("fluid ingredients aren't supported yet");
continue;
}
const choices = [choice].flat();
const valid = choices.length > 0 && choices.every((id) => typeof id === "string");
const unknown = valid ? choices.filter((id: string) => !id.startsWith("#") && !lookup.item_exists(id)) : [];
@@ -70,13 +82,28 @@ export function parse_processing(
else ingredients.push(lookup.expand(choice));
}
}
const fluids = type.fluid_outputs > 0 ? " or { fluid, amount }" : "";
if (!Array.isArray(json?.results) || json.results.length === 0) {
problems.push("results must be a list of { id, count, chance }");
problems.push(`results must be a list of { id, count, chance }${fluids}`);
} else {
if (json.results.length > type.outputs) {
problems.push(`it makes ${json.results.length} results, and ${type.id} makes ${type.outputs}`);
// deno-lint-ignore no-explicit-any
const is_fluid = (result: any) => typeof result === "object" && result !== null && "fluid" in result;
const items = json.results.filter((result: unknown) => !is_fluid(result)).length;
if (items > type.outputs) problems.push(`it makes ${items} items, and ${type.id} makes ${type.outputs}`);
if (json.results.length - items > type.fluid_outputs) {
problems.push(`it makes ${json.results.length - items} fluids, and ${type.id} makes ${type.fluid_outputs}`);
}
for (const result of json.results) {
// deno-lint-ignore no-explicit-any
for (const result of json.results as any[]) {
if (is_fluid(result)) {
const { fluid, amount } = result;
if (typeof fluid?.id !== "string" || !lookup.fluid_exists(fluid.id)) {
problems.push(`it makes the fluid ${JSON.stringify(fluid)}, which doesn't exist`);
} else if (!Number.isInteger(amount) || amount < 1) {
problems.push(`${fluid.id}'s amount must be a whole number of mB above 0`);
} else fluid_results.push({ fluid, amount });
continue;
}
const { id, count = 1, chance = 1 } = result ?? {};
if (typeof id !== "string" || !lookup.item_exists(id)) problems.push(`it makes ${id}, which doesn't exist`);
else if (!Number.isInteger(count) || count < 1 || count > 99) {
@@ -90,7 +117,7 @@ export function parse_processing(
// canRequireHeat
if (json?.processing_time !== undefined) problems.push(`${type.id} can't have a processing_time`);
if (json?.heat_requirement !== undefined) problems.push(`${type.id} can't have a heat_requirement`);
return problems.length > 0 ? { problems } : { ingredients, results };
return problems.length > 0 ? { problems } : { ingredients, results, fluid_results };
}
// the first recipe that takes the item, like a machine's getRecipe
+90 -25
View File
@@ -20,6 +20,7 @@ import {
import { flow_score, motor_step } from "./kinetics/generators.ts";
import { handle_added, handle_removed, type KineticWorld, update_generated } from "./kinetics/spreading.ts";
import {
COMPACTING,
PRESSING,
processing_for,
processing_recipes,
@@ -27,7 +28,14 @@ import {
roll_results,
} from "./recipes/processing.ts";
import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./processing/press.ts";
import { type BasinTank, drain, fill, insert_item } from "./processing/basin.ts";
import {
apply_basin_recipe,
type BasinContents,
type BasinTank,
drain,
fill,
insert_item,
} from "./processing/basin.ts";
interface Config {
max_speed: number;
@@ -73,6 +81,8 @@ interface WheelData extends KineticData {
interface PressData extends KineticData {
// steps through the cycle while it's pressing, see processing/press.ts
pressing?: number;
// it's pressing into the basin under it, not on an item on the ground
basin?: boolean;
// ticks until it looks for something to press again
scan?: number;
}
@@ -82,10 +92,16 @@ export function setup(ctx: ServerContext) {
const world = kinetic_world(ctx, config);
// checked now, so a broken recipe stops the server rather than a press later
const pressing = processing_recipes(ctx.recipes.list(PRESSING.id), PRESSING, {
expand: (choice) => ctx.tags.expand(choice),
item_exists: (id) => ctx.items.exists(id),
});
const lookup = {
expand: (choice: Id | Id[]) => ctx.tags.expand(choice),
item_exists: (id: Id) => ctx.items.exists(id),
fluid_exists: (id: Id) => ctx.fluids.exists(id),
};
const pressing = processing_recipes(ctx.recipes.list(PRESSING.id), PRESSING, lookup);
// the ones that take more ingredients first, so a recipe isn't beaten by one that takes part of what it does, like
// BasinOperatingBlockEntity.getMatchingRecipes
const compacting = processing_recipes(ctx.recipes.list(COMPACTING.id), COMPACTING, lookup)
.sort((a, b) => b.ingredients.length - a.ingredients.length);
ctx.components.register_block<KineticParams>("create:kinetic", {
on_create(block) {
@@ -194,7 +210,7 @@ export function setup(ctx: ServerContext) {
on_second: update_flow,
});
register_press(ctx, pressing);
register_press(ctx, pressing, compacting);
register_basin(ctx);
}
@@ -203,12 +219,10 @@ export function setup(ctx: ServerContext) {
// 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) => block.get_client_data<ItemStack[]>("items") ?? [];
const tanks_of = (block: BlockRef) => block.get_client_data<BasinTank[]>("tanks") ?? [];
const set_items = (block: BlockRef, items: readonly ItemStack[]) =>
block.set_client_data("items", items.length > 0 ? items : undefined);
const set_tanks = (block: BlockRef, tanks: BasinTank[]) =>
block.set_client_data("tanks", tanks.length > 0 ? tanks : undefined);
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 });
ctx.components.register_block("create:basin", {
on_entity_stand(block, _params, entity) {
@@ -222,8 +236,9 @@ function register_basin(ctx: ServerContext) {
on_interact(block, _params, player) {
const held = player.held_item;
if (!held) {
for (const item of items_of(block)) player.give_item(item.id, item.count, item.data);
set_items(block, []);
const { items, outputs } = basin_contents(block);
for (const item of [...items, ...outputs]) player.give_item(item.id, item.count, item.data);
set_basin(block, { items: [], outputs: [] });
return true;
}
const capacity = ctx.items.fluid_capacity(held.id);
@@ -247,16 +262,55 @@ function register_basin(ctx: ServerContext) {
return true;
},
on_break(block) {
for (const item of items_of(block)) ctx.world.drop_item(block.x, block.y, block.z, item);
const { items, outputs } = basin_contents(block);
for (const item of [...items, ...outputs]) ctx.world.drop_item(block.x, block.y, block.z, item);
},
});
}
// what's in a basin, its "items", "outputs" and "tanks" client data, which its visual draws, and nowhere else
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") ?? [],
};
}
// changes the parts of what's in a basin given, an empty one taking its field away
function set_basin(block: BlockRef, contents: Partial<BasinContents>) {
for (const [field, list] of Object.entries(contents)) {
block.set_client_data(field, list.length > 0 ? list : undefined);
}
}
// presses items lying on the ground under it while it turns, like create's MechanicalPressBlockEntity pressing in the
// world: it looks for one it has a recipe for every SCAN_TICKS, then comes down on it, faster the faster it turns. a
// lone item turns into what it's pressed into, and from a stack one at a time is, landing beside the rest. it only
// ticks while it turns
function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) {
// lone item turns into what it's pressed into, and from a stack one at a time is, landing beside the rest. with a
// basin two blocks under it, it presses into that instead, making a compacting recipe in it each time, like
// MechanicalPressBlockEntity as a BasinOperatingBlockEntity. it only ticks while it turns
function register_press(ctx: ServerContext, recipes: ProcessingRecipe[], compacting: ProcessingRecipe[]) {
// the basin two blocks under it, with a block between for the head to come down through, like
// BasinOperatingBlockEntity.getBasin
const basin_under = (block: BlockRef) => {
const basin = ctx.world.get_block_ref(block.x, block.y - 2, block.z);
return basin && "create:basin" in ctx.blocks.components(basin.id) ? basin : undefined;
};
// the basin once the first compacting recipe that can be is made in it, undefined when none can. its results are
// rolled when it's made for real, and all of them have to fit for it to be made at all
const compact = (basin: BlockRef, roll: boolean) => {
const contents = basin_contents(basin);
for (const recipe of compacting) {
const made = apply_basin_recipe(
contents,
{ ...recipe, results: roll ? roll_results(recipe) : recipe.results },
(item) => ctx.items.max_stack(item.id, item.data),
(a, b) => ctx.fluids.same(a, b),
);
if (made) return made;
}
};
// the items lying in the block under it, a bit in from its sides when looking for one, like create's
const items_under = (block: BlockRef, inset: number) =>
ctx.world.entities_in(
@@ -293,20 +347,30 @@ function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) {
block.set_ticking(false);
return;
}
// over a basin it only presses into that
const scan = (data.scan ?? 1) - 1;
const found = scan <= 0 && items_under(block, 0.125).length > 0;
block.data = { ...data, scan: scan <= 0 ? SCAN_TICKS : scan, ...(found && { pressing: 0 }) };
if (found) block.set_client_data("press", { steps: 0, rate } satisfies PressShown);
const basin = scan <= 0 ? basin_under(block) : undefined;
const found = scan <= 0 &&
(basin ? compact(basin, false) !== undefined : items_under(block, 0.125).length > 0);
const mode = basin ? { basin: true } : {};
block.data = { ...data, scan: scan <= 0 ? SCAN_TICKS : scan, ...(found && { pressing: 0, ...mode }) };
if (found) block.set_client_data("press", { steps: 0, rate, ...mode } satisfies PressShown);
return;
}
const steps = data.pressing;
if (steps === PRESS_AT && rate > 0) {
const [item] = items_under(block, 0);
if (item) press(item);
// what's in the basin could have changed since it started
const basin = data.basin ? basin_under(block) : undefined;
const made = basin && compact(basin, true);
if (basin && made) set_basin(basin, made);
else if (!data.basin) {
const [item] = items_under(block, 0);
if (item) press(item);
}
}
// done, and it looks again straight away, for the rest of a stack
if (steps > CYCLE) {
const { pressing: _, scan: __, ...rest } = data;
const { pressing: _, scan: __, basin: ___, ...rest } = data;
block.data = rest;
block.set_client_data("press", undefined);
return;
@@ -315,7 +379,8 @@ function register_press(ctx: ServerContext, recipes: ProcessingRecipe[]) {
const next = steps < PRESS_AT && steps + rate >= PRESS_AT ? PRESS_AT : steps + rate;
block.data = { ...data, pressing: next };
if (block.get_client_data<PressShown>("press")?.rate !== rate) {
block.set_client_data("press", { steps: next, rate } satisfies PressShown);
const mode = data.basin ? { basin: true } : {};
block.set_client_data("press", { steps: next, rate, ...mode } satisfies PressShown);
}
if (rate === 0) block.set_ticking(false);
},
+8 -2
View File
@@ -38,6 +38,7 @@ const MOD_CATALYSTS: Record<string, string[]> = {
"farmersdelight:cooking": ["farmersdelight:cooking_pot"],
"farmersdelight:cutting": ["farmersdelight:cutting_board"],
"create:pressing": ["create:mechanical_press"],
"create:compacting": ["create:mechanical_press", "create:basin"],
};
// the base game's, in the order their tabs go, like jei's vanilla plugin
@@ -293,8 +294,13 @@ function mod_category(type: string, tags: TagApi): RecipeCategory {
);
layout.texture("bworld:arrow_full", 61, 19, ARROW, ARROW);
extras.slice(0, 1).forEach((ingredient) => layout.slot(ingredient, 60, 36, ["Served in"]));
const made = outputs(recipe).slice(0, 3);
made.forEach((output, i) => layout.slot(output, 94, made.length > 1 ? i * SLOT : SLOT));
// items, then fluids, down the right
const made = [...outputs(recipe), ...fluid_outputs(recipe)].slice(0, 3);
made.forEach((output, i) => {
const y = made.length > 1 ? i * SLOT : SLOT;
if ("fluid" in output) layout.fluid(output.fluid, output.amount, 94, y);
else layout.slot(output, 94, y);
});
if (typeof recipe.cook_time === "number") layout.text(seconds(recipe.cook_time), 84, 45);
},
};
+4 -3
View File
@@ -35,10 +35,11 @@ export function outputs(recipe: Recipe): Ingredient[] {
}
}
// the fluids a recipe makes and takes, from its "fluid_results" and "fluid_ingredients", which the viewer's own recipe
// types use, like juicing
// the fluids a recipe makes and takes, from its "fluid_results" and "fluid_ingredients", like juicing, and the
// { fluid, amount } among its "results", like create's
export function fluid_outputs(recipe: Recipe): FluidAmount[] {
return recipe.fluid_results ?? [];
const among = Array.isArray(recipe.results) ? recipe.results.filter((result: Recipe) => result?.fluid) : [];
return [...recipe.fluid_results ?? [], ...among];
}
export function fluid_inputs(recipe: Recipe): FluidAmount[] {