Juices on the mechanical press !
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user