From b08de6bf81717793ac4aca76a7c5ea7ba8bffa04 Mon Sep 17 00:00:00 2001 From: Paula Date: Sat, 31 Oct 2026 17:50:53 -0300 Subject: [PATCH] Fermenting recipes --- docs/modding/items/food.md | 4 +++ docs/modding/resources/recipes.md | 21 +++++++++++++ mods/bworld/fluids/wine.json | 9 ++++++ mods/bworld/scripts/fermenting.ts | 51 +++++++++++++++++++++++++++++++ mods/bworld/scripts/juicing.ts | 25 ++++++++------- mods/bworld/scripts/server.ts | 3 ++ mods/bworld/scripts/shared.ts | 24 +++++++++------ tests/juice_test.ts | 42 +++++++++++++++++++++++++ 8 files changed, 159 insertions(+), 20 deletions(-) create mode 100644 mods/bworld/fluids/wine.json create mode 100644 mods/bworld/scripts/fermenting.ts diff --git a/docs/modding/items/food.md b/docs/modding/items/food.md index 4c1f088..cc9f2b1 100644 --- a/docs/modding/items/food.md +++ b/docs/modding/items/food.md @@ -46,6 +46,10 @@ Juices from different items are different fluids, so they never share a bottle o [juicing recipe](../resources/recipes.md#juicing-bworldjuicing) for every item with the component, which is what the juicer goes by. +Juice ferments into wine, `bworld:wine`, which keeps the item in its data and is named and colored after it the same +way, "Strawberry Wine". The base game makes a [fermentable recipe](../resources/recipes.md#fermenting-bworldfermentable) +for every juice. + Juice sells for 1.2 times the items it took to make, rounded up, so it's always worth juicing something. A bottle of juice from an item worth 30 that makes 200 mB took 1.25 of them, and sells for 45. Juice from an item without a `base_sell_value` can't be sold. diff --git a/docs/modding/resources/recipes.md b/docs/modding/resources/recipes.md index 2dc0cfe..00a0f1c 100644 --- a/docs/modding/resources/recipes.md +++ b/docs/modding/resources/recipes.md @@ -108,6 +108,27 @@ juice. It's checked when the server starts, like stonecutting: `input` is an item, a `#tag` or a list of them, and `fluid_results` is one fluid, with an amount in mB, that one of it makes. The juicer uses the first recipe that takes the item. +## Fermenting: `bworld:fermentable` + +A fluid that ferments into another over time, like juice into wine. The base game registers one from its +[shared script](#recipes-from-scripts) for every [juice](../items/food.md#juice), into wine from the same item. A file +can add one for anything else. It's checked when the server starts, like juicing: + +```json +{ + "format_version": 1, + "recipe": { + "type": "bworld:fermentable", + "input": { "fluid": { "id": "copper_tools:syrup", "data": { "tree": "bworld:maple" } }, "amount": 1000 }, + "output": { "fluid": { "id": "copper_tools:maple_liqueur" }, "amount": 500 }, + "ticks": 2400 + } +} +``` + +`input` and `output` are each a fluid with an amount in mB: that much of the input ferments into that much of the +output. `ticks` is how long it takes, 20 to a second. + ## Choices Anywhere a recipe takes an item (`key` values, `input`, `item`), an item [tag](tags.md) means anything in it fits, and diff --git a/mods/bworld/fluids/wine.json b/mods/bworld/fluids/wine.json new file mode 100644 index 0000000..c3452f3 --- /dev/null +++ b/mods/bworld/fluids/wine.json @@ -0,0 +1,9 @@ +{ + "format_version": 1, + "fluid": { + "id": "bworld:wine", + "name": "Wine", + "color": "#e8a33d", + "texture": "bworld:water_still" + } +} diff --git a/mods/bworld/scripts/fermenting.ts b/mods/bworld/scripts/fermenting.ts new file mode 100644 index 0000000..014638a --- /dev/null +++ b/mods/bworld/scripts/fermenting.ts @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// fermentable recipes: a fluid that ferments into another over a number of ticks, like juice into wine in the keg. the +// shared script registers one for every juice, and mods can add more like any recipe, as files or from their shared +// scripts +import type { FluidStack, SharedContext } from "bworld/shared"; +import { fluid_stack, type RecipeReader } from "./juicing.ts"; +import type { JuiceableParams } from "./shared.ts"; + +export const FERMENTABLE = "bworld:fermentable"; +// how much of a juice one recipe ferments, and how long it takes for now +const FERMENTED_AMOUNT = 1000; +const FERMENT_TICKS = 1; + +export interface FermentableRecipe { + // how much of which fluid ferments, into what + input: FluidStack; + output: FluidStack; + ticks: number; +} + +// a recipe for every juice, the one each juiceable item makes, fermenting it into wine from the same fruit +export function register_fermenting(ctx: SharedContext) { + for (const id of ctx.items.ids()) { + if (!(ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined)) continue; + ctx.recipes.register(FERMENTABLE, { + input: { fluid: { id: "bworld:juice", data: { item: id } }, amount: FERMENTED_AMOUNT }, + output: { fluid: { id: "bworld:wine", data: { item: id } }, amount: FERMENTED_AMOUNT }, + ticks: FERMENT_TICKS, + }); + } +} + +// every fermentable recipe, in load order. a broken one throws, which stops the server from starting +export function fermentable_recipes(ctx: RecipeReader): FermentableRecipe[] { + return ctx.recipes.list(FERMENTABLE).map((json, i) => { + const where = `${FERMENTABLE} recipe ${i + 1}`; + const input = fluid_stack(ctx, json.input); + const output = fluid_stack(ctx, json.output); + if (!input || !output) { + throw new Error( + `${where}: input and output must each be a { fluid, amount } of a fluid that exists, in mB`, + ); + } + if (!Number.isInteger(json.ticks) || json.ticks < 1) { + throw new Error(`${where}: ticks must be a whole number of ticks, at least 1`); + } + return { input, output, ticks: json.ticks }; + }); +} diff --git a/mods/bworld/scripts/juicing.ts b/mods/bworld/scripts/juicing.ts index 0ee12b4..d5713aa 100644 --- a/mods/bworld/scripts/juicing.ts +++ b/mods/bworld/scripts/juicing.ts @@ -43,7 +43,7 @@ function register_compacting(ctx: SharedContext) { } // what reading them needs, which server scripts have -interface RecipeReader { +export interface RecipeReader { // deno-lint-ignore no-explicit-any recipes: { list(type: string): any[] }; tags: TagApi; @@ -64,21 +64,24 @@ export function juicing_recipes(ctx: RecipeReader): JuicingRecipe[] { 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 - ) { + const juice = Array.isArray(results) && results.length === 1 ? fluid_stack(ctx, results[0]) : undefined; + if (!juice) { 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 }, - }; + return { input: ctx.tags.expand(choice as string | string[]), juice }; }); } +// a recipe's { fluid, amount } of a fluid that exists and a whole number of mB, undefined when it isn't one +export function fluid_stack(ctx: RecipeReader, json: unknown): FluidStack | undefined { + const stack = json as Partial | undefined; + const id = stack?.fluid?.id; + if (typeof id !== "string" || !ctx.fluids.exists(id) || !Number.isInteger(stack!.amount) || stack!.amount! < 1) { + return undefined; + } + return { fluid: stack!.fluid!, amount: stack!.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)); diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index a49c0ea..f747ae4 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -10,6 +10,7 @@ import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts"; import { register_verdigris } from "./verdigris/server.ts"; import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts"; import { juicing_for, juicing_recipes } from "./juicing.ts"; +import { fermentable_recipes } from "./fermenting.ts"; export function setup(ctx: ServerContext) { register_hoeable(ctx); @@ -935,6 +936,8 @@ interface KegData { } function register_keg(ctx: ServerContext) { + // checked now, so a broken recipe stops the server rather than the keg later + fermentable_recipes(ctx); const tank = (block: BlockRef, which: keyof KegData) => ctx.fluids.tank_handler({ tanks: 1, diff --git a/mods/bworld/scripts/shared.ts b/mods/bworld/scripts/shared.ts index b224fe5..8d3efd8 100644 --- a/mods/bworld/scripts/shared.ts +++ b/mods/bworld/scripts/shared.ts @@ -3,9 +3,10 @@ // the base game's block rules: how blocks are placed and how they change with the blocks beside them. they run on the // server and on every client, so players see a fence join or a trapdoor turn the moment they place it, and a trapdoor -// or a door open the moment they click it. and the juice's rules, which name and color it by the item it's from, and -// the recipes that make it +// or a door open the moment they click it. and the rules of juice and wine, which name and color them by the item +// they're from, and the recipes that make them import type { SharedBlock, SharedContext } from "bworld/shared"; +import { register_fermenting } from "./fermenting.ts"; import { register_juicing } from "./juicing.ts"; import { register_verdigris_rules } from "./verdigris/shared.ts"; @@ -14,8 +15,9 @@ export function setup(ctx: SharedContext) { register_trapdoor(ctx); register_door(ctx); register_verdigris_rules(ctx); - register_juice(ctx); + register_juice_and_wine(ctx); register_juicing(ctx); + register_fermenting(ctx); } // the sides a fence connects on, and the face of the block there it connects to @@ -158,22 +160,26 @@ export const JUICE_VALUE = 1.2; // juice is one fluid for every fruit, like create's potion fluid: which fruit sits in its data, { item }. it's named // after that item, "Strawberry Juice", in the color its bworld:juiceable says, and sells for JUICE_VALUE times the -// items it took -function register_juice(ctx: SharedContext) { +// items it took. wine is too, fermented from the juice (see fermenting.ts), keeping its fruit: "Strawberry Wine" +function register_juice_and_wine(ctx: SharedContext) { const item_of = (data: unknown) => { const item = (data as { item?: unknown } | undefined)?.item; return typeof item === "string" && ctx.items.exists(item) ? item : undefined; }; const juiceable = (item: string) => ctx.items.components(item)["bworld:juiceable"] as JuiceableParams | undefined; - ctx.fluids.register("bworld:juice", { - name(data) { + const from_fruit = (kind: string) => ({ + name(data: unknown) { const item = item_of(data); - return item && `${ctx.items.name(item)} Juice`; + return item && `${ctx.items.name(item)} ${kind}`; }, - color(data) { + color(data: unknown) { const item = item_of(data); return item && juiceable(item)?.color; }, + }); + ctx.fluids.register("bworld:wine", from_fruit("Wine")); + ctx.fluids.register("bworld:juice", { + ...from_fruit("Juice"), sell_value(data) { const item = item_of(data); if (!item) return undefined; diff --git a/tests/juice_test.ts b/tests/juice_test.ts index a373303..07545f8 100644 --- a/tests/juice_test.ts +++ b/tests/juice_test.ts @@ -14,6 +14,7 @@ import { AssetManager } from "$/client/assets.ts"; import { fluid_rules, set_fluid_rules } from "$/client/fluid_rules.ts"; import { setup as setup_client } from "$/mods/bworld/scripts/client.ts"; import { JUICE_VALUE, type JuiceableParams } from "$/mods/bworld/scripts/shared.ts"; +import type { FermentableRecipe } from "$/mods/bworld/scripts/fermenting.ts"; import { copy_dir, stone_floor, test_game } from "./helpers.ts"; const CLOUDBERRY = "testberries:cloudberry"; @@ -221,6 +222,47 @@ Deno.test("a broken juicing recipe stops the server from starting", async () => await assertRejects(() => setup({ "broken.json": broken }), Error, "bworld:juicing recipe"); }); +Deno.test("every juice ferments into wine from the same fruit, named after it in its color", async () => { + const { game, fluids } = await setup(); + const fermentable = game.recipes.by_type.get("bworld:fermentable") as unknown as FermentableRecipe[]; + const juiceable = EverythingRegistry.entries("items") + .filter(([, item]) => item.components?.["bworld:juiceable"]); + assert(juiceable.some(([id]) => id === CLOUDBERRY)); + for (const [id, item] of juiceable) { + const recipes = fermentable.filter((recipe) => + fluids.same(recipe.input.fluid, { id: "bworld:juice", data: { item: id } }) + ); + assertEquals(recipes.length, 1, id); + const [recipe] = recipes; + assertEquals(recipe.output.fluid, { id: "bworld:wine", data: { item: id } }); + assert( + recipe.input.amount > 0 && recipe.output.amount > 0 && Number.isInteger(recipe.ticks) && recipe.ticks > 0, + ); + assertEquals(fluids.name(recipe.output.fluid), `${display_name(id)} Wine`); + assertEquals( + fluids.color(recipe.output.fluid), + (item.components!["bworld:juiceable"] as { color: string }).color, + ); + } +}); + +Deno.test("a broken bworld:fermentable recipe stops the server from starting", async () => { + const juice = { fluid: { id: "bworld:juice" }, amount: 100 }; + for ( + const broken of [ + { input: juice, output: { fluid: { id: "nope:nothing" }, amount: 100 }, ticks: 1 }, + { input: juice, output: juice, ticks: 0 }, + { output: juice, ticks: 1 }, + ] + ) { + await assertRejects( + () => setup({ "broken.json": { type: "bworld:fermentable", ...broken } }), + Error, + "bworld:fermentable recipe", + ); + } +}); + // a juicer at 1 100 1 drawn by bworld's client script, and a way to set a field of its client data. its fluids have the // server's rules, the same as a client's async function drawn_juicer() {