Juicer recipes register

This commit is contained in:
2026-10-30 22:35:44 -03:00
parent 43521b3675
commit 4717e45a9a
9 changed files with 171 additions and 42 deletions
+69
View File
@@ -0,0 +1,69 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// juicing recipes: what the juicer presses an item into. the shared script registers one for every item with
// bworld:juiceable, its own juice, and mods can add more like any recipe, as files or from their shared scripts.
// whatever makes juice reads them, like the juicer and create's press over a basin
import type { FluidVariant, SharedContext, TagApi } from "bworld/shared";
import type { JuiceableParams } from "./shared.ts";
export const JUICING = "bworld:juicing";
export interface JuicingRecipe {
// the items it presses
input: string[];
// what one of them makes
juice: { fluid: FluidVariant; amount: number };
}
// a recipe for every juiceable item, pressing one into the juice named after it, as much as its component says
export function register_juicing(ctx: SharedContext) {
for (const id of ctx.items.ids()) {
const params = ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined;
if (!params) continue;
const juice = { fluid: { id: "bworld:juice", data: { item: id } }, amount: params.juice };
ctx.recipes.register(JUICING, { input: id, fluid_results: [juice] });
}
}
// what reading them needs, which server scripts have
interface RecipeReader {
// deno-lint-ignore no-explicit-any
recipes: { list(type: string): any[] };
tags: TagApi;
items: { exists(id: string): boolean };
fluids: { exists(id: string): boolean };
}
// every juicing recipe, in load order. a broken one throws, which stops the server from starting
export function juicing_recipes(ctx: RecipeReader): JuicingRecipe[] {
return ctx.recipes.list(JUICING).map((json, i) => {
const where = `${JUICING} recipe ${i + 1}`;
const choice: unknown = json.input;
const entries = typeof choice === "string" ? [choice] : Array.isArray(choice) ? choice : [];
const exists = (entry: unknown) =>
typeof entry === "string" &&
(entry.startsWith("#") ? ctx.tags.exists("items", entry.slice(1)) : ctx.items.exists(entry));
if (entries.length === 0 || !entries.every(exists)) {
throw new Error(`${where}: input must be an item, a #tag or a list of them, and they must exist`);
}
const results: unknown = json.fluid_results;
const [juice] = Array.isArray(results) ? results : [];
const fluid = juice?.fluid?.id;
if (
!Array.isArray(results) || results.length !== 1 || typeof fluid !== "string" || !ctx.fluids.exists(fluid) ||
!Number.isInteger(juice.amount) || juice.amount < 1
) {
throw new Error(`${where}: fluid_results must be one { fluid, amount } of a fluid that exists, in mB`);
}
return {
input: ctx.tags.expand(choice as string | string[]),
juice: { fluid: juice.fluid, amount: juice.amount },
};
});
}
// the first recipe that presses id, undefined when none does
export function juicing_for(recipes: JuicingRecipe[], id: string): JuicingRecipe | undefined {
return recipes.find((recipe) => recipe.input.includes(id));
}