Fermenting recipes
This commit is contained in:
@@ -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
|
[juicing recipe](../resources/recipes.md#juicing-bworldjuicing) for every item with the component, which is what the
|
||||||
juicer goes by.
|
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 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
|
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.
|
`base_sell_value` can't be sold.
|
||||||
|
|||||||
@@ -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
|
`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.
|
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
|
## Choices
|
||||||
|
|
||||||
Anywhere a recipe takes an item (`key` values, `input`, `item`), an item [tag](tags.md) means anything in it fits, and
|
Anywhere a recipe takes an item (`key` values, `input`, `item`), an item [tag](tags.md) means anything in it fits, and
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"format_version": 1,
|
||||||
|
"fluid": {
|
||||||
|
"id": "bworld:wine",
|
||||||
|
"name": "Wine",
|
||||||
|
"color": "#e8a33d",
|
||||||
|
"texture": "bworld:water_still"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 };
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -43,7 +43,7 @@ function register_compacting(ctx: SharedContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// what reading them needs, which server scripts have
|
// what reading them needs, which server scripts have
|
||||||
interface RecipeReader {
|
export interface RecipeReader {
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
recipes: { list(type: string): any[] };
|
recipes: { list(type: string): any[] };
|
||||||
tags: TagApi;
|
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`);
|
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 results: unknown = json.fluid_results;
|
||||||
const [juice] = Array.isArray(results) ? results : [];
|
const juice = Array.isArray(results) && results.length === 1 ? fluid_stack(ctx, results[0]) : undefined;
|
||||||
const fluid = juice?.fluid?.id;
|
if (!juice) {
|
||||||
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`);
|
throw new Error(`${where}: fluid_results must be one { fluid, amount } of a fluid that exists, in mB`);
|
||||||
}
|
}
|
||||||
return {
|
return { input: ctx.tags.expand(choice as string | string[]), juice };
|
||||||
input: ctx.tags.expand(choice as string | string[]),
|
|
||||||
juice: { fluid: juice.fluid, amount: juice.amount },
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// the first recipe that presses id, undefined when none does
|
||||||
export function juicing_for(recipes: JuicingRecipe[], id: string): JuicingRecipe | undefined {
|
export function juicing_for(recipes: JuicingRecipe[], id: string): JuicingRecipe | undefined {
|
||||||
return recipes.find((recipe) => recipe.input.includes(id));
|
return recipes.find((recipe) => recipe.input.includes(id));
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
|
|||||||
import { register_verdigris } from "./verdigris/server.ts";
|
import { register_verdigris } from "./verdigris/server.ts";
|
||||||
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
|
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
|
||||||
import { juicing_for, juicing_recipes } from "./juicing.ts";
|
import { juicing_for, juicing_recipes } from "./juicing.ts";
|
||||||
|
import { fermentable_recipes } from "./fermenting.ts";
|
||||||
|
|
||||||
export function setup(ctx: ServerContext) {
|
export function setup(ctx: ServerContext) {
|
||||||
register_hoeable(ctx);
|
register_hoeable(ctx);
|
||||||
@@ -935,6 +936,8 @@ interface KegData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function register_keg(ctx: ServerContext) {
|
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) =>
|
const tank = (block: BlockRef, which: keyof KegData) =>
|
||||||
ctx.fluids.tank_handler({
|
ctx.fluids.tank_handler({
|
||||||
tanks: 1,
|
tanks: 1,
|
||||||
|
|||||||
@@ -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
|
// 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
|
// 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
|
// or a door open the moment they click it. and the rules of juice and wine, which name and color them by the item
|
||||||
// the recipes that make it
|
// they're from, and the recipes that make them
|
||||||
import type { SharedBlock, SharedContext } from "bworld/shared";
|
import type { SharedBlock, SharedContext } from "bworld/shared";
|
||||||
|
import { register_fermenting } from "./fermenting.ts";
|
||||||
import { register_juicing } from "./juicing.ts";
|
import { register_juicing } from "./juicing.ts";
|
||||||
import { register_verdigris_rules } from "./verdigris/shared.ts";
|
import { register_verdigris_rules } from "./verdigris/shared.ts";
|
||||||
|
|
||||||
@@ -14,8 +15,9 @@ export function setup(ctx: SharedContext) {
|
|||||||
register_trapdoor(ctx);
|
register_trapdoor(ctx);
|
||||||
register_door(ctx);
|
register_door(ctx);
|
||||||
register_verdigris_rules(ctx);
|
register_verdigris_rules(ctx);
|
||||||
register_juice(ctx);
|
register_juice_and_wine(ctx);
|
||||||
register_juicing(ctx);
|
register_juicing(ctx);
|
||||||
|
register_fermenting(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
// the sides a fence connects on, and the face of the block there it connects to
|
// 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
|
// 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
|
// after that item, "Strawberry Juice", in the color its bworld:juiceable says, and sells for JUICE_VALUE times the
|
||||||
// items it took
|
// items it took. wine is too, fermented from the juice (see fermenting.ts), keeping its fruit: "Strawberry Wine"
|
||||||
function register_juice(ctx: SharedContext) {
|
function register_juice_and_wine(ctx: SharedContext) {
|
||||||
const item_of = (data: unknown) => {
|
const item_of = (data: unknown) => {
|
||||||
const item = (data as { item?: unknown } | undefined)?.item;
|
const item = (data as { item?: unknown } | undefined)?.item;
|
||||||
return typeof item === "string" && ctx.items.exists(item) ? item : undefined;
|
return typeof item === "string" && ctx.items.exists(item) ? item : undefined;
|
||||||
};
|
};
|
||||||
const juiceable = (item: string) => ctx.items.components(item)["bworld:juiceable"] as JuiceableParams | undefined;
|
const juiceable = (item: string) => ctx.items.components(item)["bworld:juiceable"] as JuiceableParams | undefined;
|
||||||
ctx.fluids.register("bworld:juice", {
|
const from_fruit = (kind: string) => ({
|
||||||
name(data) {
|
name(data: unknown) {
|
||||||
const item = item_of(data);
|
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);
|
const item = item_of(data);
|
||||||
return item && juiceable(item)?.color;
|
return item && juiceable(item)?.color;
|
||||||
},
|
},
|
||||||
|
});
|
||||||
|
ctx.fluids.register("bworld:wine", from_fruit("Wine"));
|
||||||
|
ctx.fluids.register("bworld:juice", {
|
||||||
|
...from_fruit("Juice"),
|
||||||
sell_value(data) {
|
sell_value(data) {
|
||||||
const item = item_of(data);
|
const item = item_of(data);
|
||||||
if (!item) return undefined;
|
if (!item) return undefined;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import { AssetManager } from "$/client/assets.ts";
|
|||||||
import { fluid_rules, set_fluid_rules } from "$/client/fluid_rules.ts";
|
import { fluid_rules, set_fluid_rules } from "$/client/fluid_rules.ts";
|
||||||
import { setup as setup_client } from "$/mods/bworld/scripts/client.ts";
|
import { setup as setup_client } from "$/mods/bworld/scripts/client.ts";
|
||||||
import { JUICE_VALUE, type JuiceableParams } from "$/mods/bworld/scripts/shared.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";
|
import { copy_dir, stone_floor, test_game } from "./helpers.ts";
|
||||||
|
|
||||||
const CLOUDBERRY = "testberries:cloudberry";
|
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");
|
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<ItemRegistry>("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
|
// 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
|
// server's rules, the same as a client's
|
||||||
async function drawn_juicer() {
|
async function drawn_juicer() {
|
||||||
|
|||||||
Reference in New Issue
Block a user