Juicer recipes register
This commit is contained in:
@@ -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));
|
||||
}
|
||||
@@ -2,13 +2,14 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the base game's block and item behavior, written against the same api as any other mod
|
||||
import type { BlockRef, Container, FluidVariant, Player, Season, ServerContext } from "bworld/server";
|
||||
import type { BlockRef, Container, Player, Season, ServerContext } from "bworld/server";
|
||||
import { register_curios } from "./curios.ts";
|
||||
import { register_effects } from "./effects.ts";
|
||||
import { register_fishing } from "./fishing.ts";
|
||||
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
|
||||
import { register_verdigris } from "./verdigris/server.ts";
|
||||
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES, type JuicerTank } from "./shared.ts";
|
||||
import { juicing_for, juicing_recipes } from "./juicing.ts";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
register_hoeable(ctx);
|
||||
@@ -848,13 +849,11 @@ function register_buckets(ctx: ServerContext) {
|
||||
});
|
||||
}
|
||||
|
||||
// the juicer has no screen. right clicking it with something juiceable puts that in, one kind at a time, and
|
||||
// pressing it (sneaking with an empty hand) JUICER_PRESSES times turns one of it into its juice, while that fits in the
|
||||
// tank. right clicking it with an empty fluid container fills it from the tank. breaking it drops what's in it, and the
|
||||
// the juicer has no screen. right clicking it with something a juicing recipe takes puts that in, one kind at a time,
|
||||
// and pressing it (sneaking with an empty hand) JUICER_PRESSES times turns one of it into the recipe's juice, while
|
||||
// that fits in the tank. right clicking it with an empty fluid container fills it from the tank. breaking it drops what's in it, and the
|
||||
// juice is lost. clients see what's in it, its juice and how many times it's been pressed toward the next as its
|
||||
// "stack", "tank" and "presses" client data, which its visual draws (see scripts/client.ts)
|
||||
const JUICE = "bworld:juice";
|
||||
|
||||
interface JuicerData {
|
||||
container?: string;
|
||||
// how many times it's been pressed toward the next juice
|
||||
@@ -863,9 +862,10 @@ interface JuicerData {
|
||||
}
|
||||
|
||||
function register_juicer(ctx: ServerContext) {
|
||||
// what juice is made from, read by whatever makes it, like the juicer. the juice's name and color are in shared.ts
|
||||
// what juice is made from and how it looks, read by the juicing recipes and the juice's rules in shared.ts
|
||||
ctx.components.register_item<JuiceableParams>("bworld:juiceable", {});
|
||||
const juiceable = (id: string) => ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined;
|
||||
// checked now, so a broken recipe stops the server rather than the juicer later
|
||||
const recipes = juicing_recipes(ctx);
|
||||
const show = (block: BlockRef, container: Container) => {
|
||||
const item = container.get(0);
|
||||
const data = item?.data !== undefined && { data: item.data };
|
||||
@@ -877,7 +877,7 @@ function register_juicer(ctx: ServerContext) {
|
||||
const held = player.held_item;
|
||||
const container = block_container(ctx, block, 1);
|
||||
const data = block.data as JuicerData;
|
||||
if (held && juiceable(held.id)) {
|
||||
if (held && juicing_for(recipes, held.id)) {
|
||||
held.count = container.add(held)?.count ?? 0;
|
||||
show(block, container);
|
||||
// true even when none fit, so the fruit isn't eaten instead
|
||||
@@ -899,11 +899,11 @@ function register_juicer(ctx: ServerContext) {
|
||||
|
||||
if (held || !player.is_sneaking()) return false;
|
||||
const item = container.get(0);
|
||||
const params = item && juiceable(item.id);
|
||||
if (!item || !params) return false;
|
||||
const juice: FluidVariant = { id: JUICE, data: { item: item.id } };
|
||||
const recipe = item && juicing_for(recipes, item.id);
|
||||
if (!item || !recipe) return false;
|
||||
const { fluid: juice, amount } = recipe.juice;
|
||||
const tank = data.tank;
|
||||
const fits = (tank ? tank.amount : 0) + params.juice <= JUICER_CAPACITY &&
|
||||
const fits = (tank ? tank.amount : 0) + amount <= JUICER_CAPACITY &&
|
||||
(!tank || ctx.fluids.same(tank.fluid, juice));
|
||||
if (!fits) return false;
|
||||
const presses = (data.presses ?? 0) + 1;
|
||||
@@ -914,7 +914,7 @@ function register_juicer(ctx: ServerContext) {
|
||||
}
|
||||
item.count -= 1;
|
||||
show(block, container);
|
||||
const filled = { fluid: juice, amount: (tank?.amount ?? 0) + params.juice };
|
||||
const filled = { fluid: juice, amount: (tank?.amount ?? 0) + amount };
|
||||
block.data = { ...data, presses: 0, tank: filled } satisfies JuicerData;
|
||||
block.set_client_data("tank", filled);
|
||||
block.set_client_data("presses", undefined);
|
||||
|
||||
@@ -3,8 +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
|
||||
// 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
|
||||
import type { FluidVariant, SharedBlock, SharedContext } from "bworld/shared";
|
||||
import { register_juicing } from "./juicing.ts";
|
||||
import { register_verdigris_rules } from "./verdigris/shared.ts";
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
@@ -13,6 +15,7 @@ export function setup(ctx: SharedContext) {
|
||||
register_door(ctx);
|
||||
register_verdigris_rules(ctx);
|
||||
register_juice(ctx);
|
||||
register_juicing(ctx);
|
||||
}
|
||||
|
||||
// the sides a fence connects on, and the face of the block there it connects to
|
||||
|
||||
@@ -161,7 +161,7 @@ function base_categories(tags: TagApi): RecipeCategory[] {
|
||||
},
|
||||
},
|
||||
{
|
||||
// what the juicer presses an item into, see juicing_recipes
|
||||
// what the juicer presses an item into, see JUICING
|
||||
type: JUICING,
|
||||
title: "Juicing",
|
||||
width: 116,
|
||||
@@ -169,7 +169,7 @@ function base_categories(tags: TagApi): RecipeCategory[] {
|
||||
catalysts: [JUICER],
|
||||
draw(recipe, layout) {
|
||||
const [{ fluid, amount }] = fluid_outputs(recipe);
|
||||
layout.slot({ ids: [recipe.input], count: 1 }, 0, 0);
|
||||
layout.slot({ ids: tags.expand(recipe.input), count: 1 }, 0, 0);
|
||||
layout.texture("bworld:arrow_full", 31, 1, ARROW, ARROW);
|
||||
layout.fluid(fluid, amount, 60, 0);
|
||||
layout.text(`${amount} mB`, 82, 5);
|
||||
@@ -178,22 +178,11 @@ function base_categories(tags: TagApi): RecipeCategory[] {
|
||||
];
|
||||
}
|
||||
|
||||
// the base game's juicer, from the items with bworld:juiceable: a recipe of its own type for each, that presses one
|
||||
// of it into its juice. the item is an input like any recipe's, the juice is its fluid result, with the item it's from as
|
||||
// its data like the juicer makes it
|
||||
// the base game's juicer's recipes, which press an item into a fluid, its "fluid_results". bworld registers one for
|
||||
// every item with bworld:juiceable, see mods/bworld/scripts/juicing.ts
|
||||
const JUICING = "bworld:juicing";
|
||||
const JUICER = "bworld:juicer";
|
||||
|
||||
export function juicing_recipes(ctx: ClientContext): Recipe[] {
|
||||
if (!ctx.fluids.exists("bworld:juice")) return [];
|
||||
return ctx.items.ids().flatMap((id) => {
|
||||
const params = ctx.items.components(id)["bworld:juiceable"] as { juice?: number } | undefined;
|
||||
if (!params?.juice) return [];
|
||||
const juice = { fluid: { id: "bworld:juice", data: { item: id } }, amount: params.juice };
|
||||
return [{ type: JUICING, input: id, fluid_results: [juice] }];
|
||||
});
|
||||
}
|
||||
|
||||
// the base game's fish, the items with bworld:fish, as recipes of their own type that make them, so clicking one shows
|
||||
// when and where it bites. their params are under "fish", which reading a recipe's inputs skips
|
||||
const FISHING = "bworld:fishing";
|
||||
|
||||
@@ -9,7 +9,6 @@ import type { ClientContext, Graphics, Screen } from "bworld/client";
|
||||
import {
|
||||
crabpot_recipes,
|
||||
fishing_recipes,
|
||||
juicing_recipes,
|
||||
make_categories,
|
||||
type RecipeCategory,
|
||||
type RecipeLayout,
|
||||
@@ -56,7 +55,6 @@ export class RecipeIndex {
|
||||
...this.ctx.recipes.list(),
|
||||
...fishing_recipes(this.ctx),
|
||||
...crabpot_recipes(this.ctx),
|
||||
...juicing_recipes(this.ctx),
|
||||
];
|
||||
this.#categories = make_categories(this.ctx, this.#recipes);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user