Fermenting recipes

This commit is contained in:
2026-10-31 17:50:53 -03:00
parent 658a6453b7
commit b08de6bf81
8 changed files with 159 additions and 20 deletions
+9
View File
@@ -0,0 +1,9 @@
{
"format_version": 1,
"fluid": {
"id": "bworld:wine",
"name": "Wine",
"color": "#e8a33d",
"texture": "bworld:water_still"
}
}
+51
View File
@@ -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 };
});
}
+14 -11
View File
@@ -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<FluidStack> | 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));
+3
View File
@@ -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,
+15 -9
View File
@@ -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;