Every recipe type registered from shared scripts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-01 21:00:44 -03:00
co-authored by Claude Opus 5.5
parent 65113862f7
commit a7e81b9944
25 changed files with 334 additions and 371 deletions
+15 -16
View File
@@ -2,9 +2,10 @@
// 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 }. 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 { FluidStack, Id } from "bworld/shared";
// stacks out, each with a chance, and for the types that can, fluids out as { fluid, amount }. the shared script
// 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 "bworld/shared";
// how many ingredients, item results and fluid results a type takes at most, like getMaxInputCount,
// getMaxOutputCount and getMaxFluidOutputCount
@@ -42,14 +43,17 @@ export interface RecipeLookup {
fluid_exists(id: Id): boolean;
}
// every recipe of type, from their json, or an error saying what's wrong with each broken one
export function processing_recipes(jsons: unknown[], type: ProcessingType, lookup: RecipeLookup): ProcessingRecipe[] {
const parsed = jsons.map((json) => parse_processing(json, type, lookup));
const problems = parsed.flatMap((recipe, i) =>
"problems" in recipe ? [`${type.id} recipe ${JSON.stringify(jsons[i])}: ${recipe.problems.join("; ")}`] : []
);
if (problems.length > 0) throw new Error(problems.join("\n"));
return parsed as ProcessingRecipe[];
// 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<ProcessingRecipe, Id> {
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
@@ -120,11 +124,6 @@ export function parse_processing(
return problems.length > 0 ? { problems } : { ingredients, results, fluid_results };
}
// the first recipe that takes the item, like a machine's getRecipe
export function processing_for(recipes: ProcessingRecipe[], item: Id): ProcessingRecipe | undefined {
return recipes.find((recipe) => recipe.ingredients[0]?.includes(item));
}
// 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 }) => {