// SPDX-FileCopyrightText: 2026 Paula Wisnievski // 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, and for the types that can, fluids out as { fluid, amount }. the shared phase // registers each type, so a broken recipe stops the mod loading instead of a machine later. no ctx, tests give what it // needs import type { FluidStack, Id, RecipeType } from "cubi/shared"; // 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, 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; count: number; // how likely each of count is to come out, 1 for always, like ProcessingOutput's chance: number; } export interface ProcessingRecipe { // each ingredient as every item that fits it, one of it each ingredients: Id[][]; results: ProcessingResult[]; // in mB fluid_results: FluidStack[]; } // what reading recipes needs from the game 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; } // the recipe type for ctx.recipes.register_type, saying everything wrong with a broken recipe. it's found by the item // it takes first, like a machine's getRecipe export function processing_type(type: ProcessingType, lookup: RecipeLookup): RecipeType { return { parse(json) { const parsed = parse_processing(json, type, lookup); if ("problems" in parsed) throw new Error(parsed.problems.join("; ")); return parsed; }, matches: (recipe, item) => recipe.ingredients[0]?.includes(item) ?? false, }; } // one recipe, or what's wrong with it, like ProcessingRecipe.validate export function parse_processing( // deno-lint-ignore no-explicit-any json: any, type: ProcessingType, lookup: RecipeLookup, ): ProcessingRecipe | { problems: string[] } { 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 { if (json.ingredients.length > type.inputs) { 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)) : []; if (!valid) problems.push(`${JSON.stringify(choice)} isn't an item, a #tag or a list of them`); else if (unknown.length > 0) problems.push(`it takes ${unknown.join(", ")}, which doesn't exist`); 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 }${fluids}`); } else { // 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}`); } // 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) { problems.push(`${id}'s count must be 1 to 99`); } else if (typeof chance !== "number" || chance <= 0 || chance > 1) { problems.push(`${id}'s chance must be above 0 and at most 1`); } else results.push({ id, count, chance }); } } // none of the types so far can say how long they take or how hot they have to be, like canSpecifyDuration and // 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, fluid_results }; } // what one go of a recipe makes: each result, with each of its count kept by its chance, like rollResults export function roll_results(recipe: ProcessingRecipe, random = Math.random): { id: Id; count: number }[] { return recipe.results.flatMap(({ id, count, chance }) => { let kept = count; if (chance < 1) { for (let i = 0; i < count; i++) if (random() > chance) kept--; } return kept > 0 ? [{ id, count: kept }] : []; }); }