Files
bworld/mods/create/scripts/recipes/processing.ts
T
2026-10-15 13:07:09 -03:00

109 lines
4.8 KiB
TypeScript

// 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. 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";
// how many ingredients and results a type takes at most, like getMaxInputCount and getMaxOutputCount
export interface ProcessingType {
readonly id: Id;
readonly inputs: number;
readonly outputs: number;
}
export const PRESSING: ProcessingType = { id: "create:pressing", inputs: 1, 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
ingredients: Id[][];
results: ProcessingResult[];
}
// 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;
}
// 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[];
}
// 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[] = [];
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) {
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));
}
}
if (!Array.isArray(json?.results) || json.results.length === 0) {
problems.push("results must be a list of { id, count, chance }");
} else {
if (json.results.length > type.outputs) {
problems.push(`it makes ${json.results.length} results, and ${type.id} makes ${type.outputs}`);
}
for (const result of json.results) {
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 };
}
// 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 }) => {
let kept = count;
if (chance < 1) { for (let i = 0; i < count; i++) if (random() > chance) kept--; }
return kept > 0 ? [{ id, count: kept }] : [];
});
}