86 lines
3.4 KiB
TypeScript
86 lines
3.4 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the kitchen's recipe types, registered by the shared script: what the cooking pot cooks, found by the items in its
|
|
// ingredient slots, and what the cutting board cuts, found by the item on it. see cooking.ts
|
|
import type { SharedContext } from "bworld/shared";
|
|
|
|
export const COOKING = "bworld:cooking";
|
|
export const CUTTING = "bworld:cutting";
|
|
|
|
// how many ingredients the pot holds
|
|
export const INGREDIENT_SLOTS = 6;
|
|
// ticks, for recipes that don't say
|
|
const DEFAULT_COOK_TIME = 200;
|
|
|
|
// a bworld:cooking recipe: the ingredients in any slots. in the json each is an item id or #tag, or a list of
|
|
// those any of which will do. here each is every item that will do
|
|
export interface CookingRecipe {
|
|
ingredients: string[][];
|
|
result: { id: string; count: number };
|
|
// what the meal is served in, like a bowl. without it the meal comes out as soon as it's cooked
|
|
container?: string;
|
|
cook_time: number;
|
|
}
|
|
|
|
// a bworld:cutting recipe, like the java mod's: one ingredient (an item, or a list any of which will do), the
|
|
// tool type that cuts it, and everything one of it is cut into
|
|
export interface CuttingRecipe {
|
|
ingredient: string[];
|
|
tool: string;
|
|
result: { id: string; count: number }[];
|
|
}
|
|
|
|
export function register_cooking_recipes(ctx: SharedContext) {
|
|
// found by the ids of what's in the ingredient slots
|
|
ctx.recipes.register_type<CookingRecipe, string[]>(COOKING, {
|
|
parse(json, read) {
|
|
const ingredients = json.ingredients;
|
|
if (!Array.isArray(ingredients) || ingredients.length < 1 || ingredients.length > INGREDIENT_SLOTS) {
|
|
throw new Error(`ingredients must be 1 to ${INGREDIENT_SLOTS} items`);
|
|
}
|
|
const recipe: CookingRecipe = {
|
|
ingredients: ingredients.map((choice, i) => read.ingredient(choice, `ingredients[${i}]`)),
|
|
result: read.stack(json.result, "result"),
|
|
cook_time: json.cook_time === undefined
|
|
? DEFAULT_COOK_TIME
|
|
: read.positive_int(json.cook_time, "cook_time"),
|
|
};
|
|
if (json.container !== undefined) recipe.container = read.item(json.container, "container");
|
|
return recipe;
|
|
},
|
|
matches: (recipe, items) => recipe.ingredients.length === items.length && pairs_up(recipe.ingredients, items),
|
|
});
|
|
|
|
ctx.recipes.register_type<CuttingRecipe, string>(CUTTING, {
|
|
parse(json, read) {
|
|
if (!Array.isArray(json.ingredients) || json.ingredients.length !== 1) {
|
|
throw new Error("ingredients must be one item, or one list of items any of which will do");
|
|
}
|
|
if (typeof json.tool !== "string") throw new Error(`tool must be a tool type, like "knife"`);
|
|
if (!Array.isArray(json.result) || json.result.length === 0) {
|
|
throw new Error("result must be a list of { id, count }");
|
|
}
|
|
return {
|
|
ingredient: read.ingredient(json.ingredients[0], "ingredients[0]"),
|
|
tool: json.tool,
|
|
result: json.result.map((result: unknown, i: number) => read.stack(result, `result[${i}]`)),
|
|
};
|
|
},
|
|
matches: (recipe, id) => recipe.ingredient.includes(id),
|
|
});
|
|
}
|
|
|
|
// pairs every ingredient with a different item, trying each way round, like a shapeless recipe
|
|
function pairs_up(ingredients: string[][], items: string[], used = new Set<number>()): boolean {
|
|
if (ingredients.length === 0) return true;
|
|
const [first, ...rest] = ingredients;
|
|
for (let i = 0; i < items.length; i++) {
|
|
if (used.has(i) || !first.includes(items[i])) continue;
|
|
used.add(i);
|
|
if (pairs_up(rest, items, used)) return true;
|
|
used.delete(i);
|
|
}
|
|
return false;
|
|
}
|