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
+23 -12
View File
@@ -7,9 +7,9 @@ possible, and to stay fast on both the server and the client when a world has th
Built so far: the importer and Create's imported blocks and items, R1, R2, R3 (`transformed`, `rotating`, `fluid` and
items), R4, R5, R8, S0, S4, S7, S12, stonecutting from S9, the start of P1's kinetics (shafts, cogwheels, gearboxes,
encased shafts and cogs, clutches, gearshifts and chain drives turned by a hand crank, creative motor or water wheel,
with stress and overstress), Create's pressing recipes, the mechanical press pressing items on the ground, and the basin
holding items and fluids. Facts about Create come from reading `ports/create1.21.1` and `ports/create1.19.3`, and facts
about bworld from its source.
with stress and overstress), Create's pressing recipes, the mechanical press pressing items on the ground or compacting
in a basin, and the basin holding items and fluids. Facts about Create come from reading `ports/create1.21.1` and
`ports/create1.19.3`, and facts about bworld from its source.
## Sources
@@ -446,11 +446,12 @@ list of those any of which fits) in place of Java's `{ item }` and `{ tag }`, li
```
`scripts/recipes/` parses all of them in one place: ingredients as item choices, or sized fluid ingredients; results as
`{ id, count, chance }` or fluid stacks; `processing_time` and `heat_requirement`. Built for pressing, without fluids,
durations or heat yet, in `scripts/recipes/processing.ts`, which the server script runs in `setup`. It checks each
type's input and output limits, which come from each recipe class's `getMaxInputCount` and `getMaxOutputCount`. The
recipe viewer gets one reader for this shape, so every Create type shows with no per-type code, and animated categories
(R11) are added per machine on top.
`{ id, count, chance }` or fluid stacks; `processing_time` and `heat_requirement`. Built for pressing and compacting,
with fluid results as `{ fluid, amount }` but no fluid ingredients, durations or heat yet, in
`scripts/recipes/processing.ts`, which the server script runs in `setup`. It checks each type's input and output limits,
which come from each recipe class's `getMaxInputCount` and `getMaxOutputCount`. The recipe viewer gets one reader for
this shape, so every Create type shows with no per-type code, and animated categories (R11) are added per machine on
top.
## Kinetics
@@ -610,10 +611,20 @@ stack of each, and 2 tanks of 1000 mB, a fluid each. An item that lands in it go
it, an empty one fills from its first tank, and an empty hand takes every item out, like `BasinBlock.useItemOn`. What's
in it is only its `items` and `tanks` client data, and its visual draws them like `BasinRenderer`: the fluids side by
side, filling faster at first, and the items in a ring over them, bobbing while there's fluid. Water and lava buckets
aren't fluid containers in bworld, so they don't fill it yet. Its output inventory and tanks, its facing toward an
output, and the item and fluid capabilities (S1, S2) come with mixing and the logistics that move things in and out. For
it the engine got `on_entity_stand`, and an item inside a block only floats out of it when it's inside what it bumps
into, like Minecraft's `noCollision`, so items can lie in a hollow.
aren't fluid containers in bworld, so they don't fill it yet. Its facing toward an output, and the item and fluid
capabilities (S1, S2), come with mixing and the logistics that move things in and out. For it the engine got
`on_entity_stand`, and an item inside a block only floats out of it when it's inside what it bumps into, like
Minecraft's `noCollision`, so items can lie in a hollow.
A press with a basin two blocks under it presses into the basin instead of on the ground, its head coming 22/16 of a
block down, like `PressingBehaviour`'s basin mode. Each cycle it makes the first `create:compacting` recipe that can be
made there, the ones with more ingredients first, like `BasinOperatingBlockEntity.getMatchingRecipes`, and it only
starts one when a recipe can be made. `apply_basin_recipe` in `processing/basin.ts` is `BasinRecipe.apply`: an item for
each ingredient from the basin's input items, item results into its outputs, which recipes don't take again, and fluid
results into its tanks, all or nothing. Outputs are its `outputs` client data, drawn and taken out by hand with the
rest. Unlike Create's, its input and output tanks are the same 2, since no recipe takes fluids yet. bworld's shared
script makes a compacting recipe for every juicing recipe while Create's press and basin are there, so the press makes
juice. Create's own 7 compacting recipes need fluid ingredients or Minecraft items, and aren't imported yet.
### P4: Logistics
+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[] {
+28
View File
@@ -7,6 +7,7 @@ import { assert, assertEquals } from "@std/assert";
import { ItemStack } from "$/common/inventory.ts";
import type { ItemStack as Stack } from "bworld/shared";
import {
apply_basin_recipe,
BASIN_SLOTS,
BASIN_TANK_CAPACITY,
BASIN_TANKS,
@@ -56,6 +57,33 @@ Deno.test("a basin keeps each fluid in a tank of its own, and drains the first",
assertEquals(drained.tanks.length, BASIN_TANKS - 1);
});
Deno.test("a basin recipe takes an item for each ingredient, and puts what it makes where recipes don't take it", () => {
const max = () => 64;
const basin = {
items: [{ id: "a:flint", count: 3 }, { id: "a:gravel", count: 1 }],
outputs: [{ id: "a:andesite", count: 1 }],
tanks: [],
};
const andesite = {
ingredients: [["a:flint"], ["a:flint"], ["a:gravel", "a:sand"]],
results: [{ id: "a:andesite", count: 1 }],
fluid_results: [{ fluid: juice("a:stone"), amount: 10 }],
};
const made = apply_basin_recipe(basin, andesite, max, same)!;
assertEquals(made.items, [{ id: "a:flint", count: 1 }]);
assertEquals(made.outputs, [{ id: "a:andesite", count: 2 }]);
assertEquals(made.tanks, [{ fluid: juice("a:stone"), amount: 10 }]);
// what it made isn't taken again, and the one flint left isn't enough
assertEquals(apply_basin_recipe(made, andesite, max, same), undefined);
assertEquals(
apply_basin_recipe({ ...basin, outputs: [] }, { ...andesite, ingredients: [["a:andesite"]] }, max, same),
undefined,
);
// nothing is made when what it makes doesn't all fit
const full = { ...basin, tanks: [{ fluid: juice("a:stone"), amount: BASIN_TANK_CAPACITY }] };
assertEquals(apply_basin_recipe(full, andesite, max, same), undefined);
});
// a basin at 0 100 0, and what's in it
async function basin() {
const t = await setup();
+79 -1
View File
@@ -2,9 +2,12 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// the mechanical press: turned, it slams down on items lying on the ground under it and presses them with create's
// pressing recipes, one at a time, and it only ticks while it turns
// pressing recipes, one at a time, or with a basin under it makes compacting recipes in that, and it only ticks while
// it turns
import { assert, assertEquals } from "@std/assert";
import { ItemStack } from "$/common/inventory.ts";
import { BASIN_TANK_CAPACITY } from "$/mods/create/scripts/processing/basin.ts";
import { BASIN_TRAVEL, PRESS_AT } from "$/mods/create/scripts/processing/press.ts";
import { FACING, setup, visual_for } from "./helpers.ts";
// a press at 1 101 0 along x, turned by a crank on its west side, with the floor two blocks under it
@@ -113,3 +116,78 @@ Deno.test("a press's head comes down to press halfway through its cycle and goes
visual.on_frame!(70);
assertEquals(down(), 0);
});
// a press at 1 102 0 turned by a crank, over a basin at 1 100 0 with a block between them, and what's in the basin
async function press_over_basin() {
const t = await setup();
t.place(1, 100, 0, "create:basin", "facing", 4);
t.place(0, 102, 0, "create:hand_crank", "facing", FACING.west);
t.place(1, 102, 0, "create:mechanical_press", "facing", FACING.east);
const crank = (ticks: number) => {
for (let i = 0; i < ticks; i += 4) {
t.use(0, 102, 0);
t.ticks(4);
}
};
const client_data = (field: string) => t.game.world.get_tile(1, 100, 0)?.client_data?.[field];
const tanks = () => client_data("tanks") as { fluid: unknown; amount: number }[] | undefined ?? [];
const count = (id: string) =>
(client_data("items") as { id: string; count: number }[] | undefined ?? [])
.find((item) => item.id === id)?.count ?? 0;
const drop = (id: string, count: number) => t.game.spawn_item(1.5, 101.5, 0.5, new ItemStack(id, count));
// the compacting recipe for id's juice, which bworld makes for every juicing recipe while create is there
const juice_of = (id: string) => {
const recipe = t.game.recipes.by_type.get("create:compacting")!
.find((recipe) => (recipe.ingredients as string[])[0] === id)!;
return (recipe.results as { fluid: unknown; amount: number }[])[0];
};
return { ...t, crank, tanks, count, drop, juice_of };
}
Deno.test("a press over a basin makes compacting recipes in it, like juice out of carrots, one each time", async () => {
const t = await press_over_basin();
t.drop("bworld:carrot", 3);
// with something no compacting recipe takes beside them, which it leaves
t.drop("bworld:iron_ingot", 1);
t.ticks(20);
assertEquals([t.count("bworld:carrot"), t.count("bworld:iron_ingot")], [3, 1]);
assertEquals(t.tanks(), []);
for (let i = 0; i < 200 && t.count("bworld:carrot") === 3; i++) t.crank(4);
assertEquals(t.count("bworld:carrot"), 2);
const juice = t.juice_of("bworld:carrot");
assertEquals(t.tanks(), [juice]);
t.crank(600);
assertEquals([t.count("bworld:carrot"), t.count("bworld:iron_ingot")], [0, 1]);
assertEquals(t.tanks(), [{ ...juice, amount: 3 * juice.amount }]);
// it presses into the basin, not on what's around it, so the iron isn't pressed into a sheet
assertEquals(t.game.item_entities().length, 0);
});
Deno.test("a press over a basin stops once what a recipe makes won't fit", async () => {
const t = await press_over_basin();
const per_melon = t.juice_of("bworld:melon").amount;
const fit = Math.floor(BASIN_TANK_CAPACITY / per_melon);
t.drop("bworld:melon", fit + 1);
t.ticks(20);
t.crank(400 * (fit + 1));
assertEquals(t.count("bworld:melon"), 1);
// turning on, it doesn't come down on the one left
t.crank(100);
assertEquals(t.game.world.get_tile(1, 102, 0)?.client_data?.press, undefined);
});
Deno.test("a press's head comes further down into a basin", async () => {
const { visual, transforms, data } = await visual_for("create:mechanical_press", { facing: FACING.east }, {
speed: 32,
press: { steps: PRESS_AT, rate: 0, basin: true },
});
const down = () => -[...transforms].find(([model]) => model.includes("head"))![1][13];
visual.on_frame!(0);
assertEquals(down(), BASIN_TRAVEL);
data.press = { steps: PRESS_AT, rate: 0 };
visual.update!();
visual.on_frame!(1);
assertEquals(down(), 1);
});
+48 -2
View File
@@ -6,7 +6,12 @@ import { assert, assertEquals, assertThrows } from "@std/assert";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import { tag_api } from "$/common/tags.ts";
import type { ClientContext } from "$/common/mod_api/client.ts";
import { parse_processing, PRESSING, processing_recipes } from "$/mods/create/scripts/recipes/processing.ts";
import {
COMPACTING,
parse_processing,
PRESSING,
processing_recipes,
} from "$/mods/create/scripts/recipes/processing.ts";
import { inputs, outputs } from "$/mods/recipe_viewer/scripts/recipes.ts";
import { RecipeIndex } from "$/mods/recipe_viewer/scripts/recipes_screen.ts";
import { test_game } from "../helpers.ts";
@@ -14,6 +19,7 @@ import { test_game } from "../helpers.ts";
const lookup = () => ({
expand: (choice: string | string[]) => tag_api().expand(choice),
item_exists: (id: string) => EverythingRegistry.get("items", id) !== undefined,
fluid_exists: (id: string) => EverythingRegistry.get("fluids", id) !== undefined,
});
Deno.test("create's pressing recipes are read with every item each ingredient stands for", async () => {
@@ -52,6 +58,31 @@ Deno.test("a pressing recipe that a press couldn't make says what's wrong with i
);
});
Deno.test("compacting recipes can make fluids as well as items, and pressing ones can't", async () => {
await test_game("mods");
const juice = { fluid: { id: "bworld:juice", data: { item: "bworld:carrot" } }, amount: 200 };
const parsed = parse_processing({ ingredients: ["bworld:carrot"], results: [juice] }, COMPACTING, lookup());
assertEquals(parsed, { ingredients: [["bworld:carrot"]], results: [], fluid_results: [juice] });
const problems = (json: unknown, type = COMPACTING) => {
const parsed = parse_processing(json, type, lookup());
return "problems" in parsed ? parsed.problems : [];
};
assertEquals(problems({ ingredients: ["bworld:carrot"], results: [juice] }, PRESSING).length, 1);
assertEquals(problems({ ingredients: ["bworld:carrot"], results: [juice, juice, juice] }).length, 1);
assertEquals(
problems({ ingredients: ["bworld:carrot"], results: [{ ...juice, fluid: { id: "a:none" } }] }).length,
1,
);
assertEquals(problems({ ingredients: ["bworld:carrot"], results: [{ ...juice, amount: 0 }] }).length, 1);
// fluids going in aren't supported yet
assertEquals(
problems({ ingredients: [{ fluid: "bworld:juice", amount: 10 }], results: [{ id: "bworld:carrot" }] }),
[
"fluid ingredients aren't supported yet",
],
);
});
Deno.test("the recipe viewer shows pressing recipes under the press, with what they take and make", async () => {
const { game } = await test_game("mods");
const pressing = game.recipes.all.filter((recipe) => recipe.type === PRESSING.id);
@@ -70,6 +101,21 @@ Deno.test("the recipe viewer shows pressing recipes under the press, with what t
blocks: { exists: () => false, components: () => ({}) },
loot: { table: () => undefined },
} as unknown as ClientContext;
const category = new RecipeIndex(ctx).categories.find((category) => category.type === PRESSING.id);
const index = new RecipeIndex(ctx);
const category = index.categories.find((category) => category.type === PRESSING.id);
assertEquals([category?.title, category?.catalysts], ["Pressing", ["create:mechanical_press"]]);
// juice is made by compacting too, by the press over a basin, and drawn as a fluid
const compacting = index.find({ kind: "fluid", id: "bworld:juice" }, "recipes")
.find((found) => found.category.type === COMPACTING.id)!;
assertEquals(compacting.category.catalysts, ["create:mechanical_press", "create:basin"]);
const fluids: unknown[] = [];
const ignore = () => {};
compacting.category.draw(compacting.recipes[0], {
slot: ignore,
texture: ignore,
text: ignore,
fluid: (fluid) => fluids.push(fluid),
});
assertEquals(fluids.length, 1);
});