diff --git a/mods/recipe_viewer/README.md b/mods/recipe_viewer/README.md index 1d2bf13..a02fce5 100644 --- a/mods/recipe_viewer/README.md +++ b/mods/recipe_viewer/README.md @@ -15,7 +15,9 @@ It reads every mod's recipes with `ctx.recipes.list()`. Fish, the items with the get a Fishing category too: clicking one shows its seasons, biomes, hours and the fluid it's caught in, read with `ctx.items.components`, and the fishing rod's uses are every fish. The crab pot gets one too: a recipe for each bait in its `bworld:crabpot` component and each kind of water, showing everything that water's loot table can catch and how -likely each is, read with `ctx.blocks.components` and `ctx.loot.table`. +likely each is, read with `ctx.blocks.components` and `ctx.loot.table`. The juicer gets one for each item with the +`bworld:juiceable` component, showing the juice one of it is pressed into and how many mB, drawn in a fluid slot with +`g.fluid`. - `scripts/categories.ts` has a category for each recipe type, like JEI's `IRecipeCategory`: its title, size, where it's made (`MOD_CATALYSTS` for mods' types) and where its items go. Types without one are drawn from their JSON: the items diff --git a/mods/recipe_viewer/scripts/categories.ts b/mods/recipe_viewer/scripts/categories.ts index de288b7..8a3e28c 100644 --- a/mods/recipe_viewer/scripts/categories.ts +++ b/mods/recipe_viewer/scripts/categories.ts @@ -4,7 +4,7 @@ // how each recipe type is shown, like jei's IRecipeCategory: a title, a fixed size every recipe of it is drawn in, the // blocks it's made in (jei's recipe catalysts), and where its items go. types no category knows get one from their // recipes' json, see mod_category -import type { ClientContext, TagApi } from "bworld/client"; +import type { ClientContext, FluidVariant, TagApi } from "bworld/client"; import { type Ingredient, mod_recipe_inputs, outputs, type Recipe, seconds, stack } from "./recipes.ts"; // in gui pixels, a slot with its border, like minecraft's @@ -17,6 +17,8 @@ const ARROW = 16; export interface RecipeLayout { // a slot, empty when ingredient is undefined. alternatives take turns slot(ingredient: Ingredient | undefined, x: number, y: number, tooltip?: string[]): void; + // an amount of a fluid in a slot, in mB, like jei's fluid ingredients + fluid(fluid: FluidVariant, amount: number, x: number, y: number, tooltip?: string[]): void; texture(id: string, x: number, y: number, w: number, h: number): void; text(text: string, x: number, y: number): void; } @@ -158,9 +160,40 @@ function base_categories(tags: TagApi): RecipeCategory[] { ); }, }, + { + // what the juicer presses an item into, see juicing_recipes + type: JUICING, + title: "Juicing", + width: 116, + height: 18, + catalysts: [JUICER], + draw(recipe, layout) { + const { fluid, amount } = recipe.juice as { fluid: FluidVariant; amount: number }; + layout.slot({ ids: [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); + }, + }, ]; } +// 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 under "juice", which reading a recipe's +// inputs skips. the juice's data is the item it's from, like the juicer makes it +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, 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"; @@ -300,6 +333,7 @@ export function category_name(type: string): string { smithing: "Smithing", [FISHING]: "Fishing", [CRABPOT]: "Crab Pot", + [JUICING]: "Juicing", }; if (base[type]) return base[type]; const name = type.split(":")[1] ?? type; diff --git a/mods/recipe_viewer/scripts/item_list.ts b/mods/recipe_viewer/scripts/item_list.ts index c503dbe..b00cc2c 100644 --- a/mods/recipe_viewer/scripts/item_list.ts +++ b/mods/recipe_viewer/scripts/item_list.ts @@ -3,7 +3,7 @@ // every item, right of the inventory and of the recipes screen, like jei's ingredient list overlay: the page and its // arrows on top, the items, and the search box under them -import type { ClientContext, Color, Graphics } from "bworld/client"; +import type { ClientContext, Color, FluidVariant, Graphics } from "bworld/client"; import { SLOT } from "./categories.ts"; const GAP = 5; @@ -13,6 +13,8 @@ export const SLOT_COLOR: Color = [0.07, 0.07, 0.11, 0.6]; const HOVER_COLOR: Color = [0.96, 0.76, 0.91, 0.35]; export const LABEL_COLOR: Color = [0.65, 0.68, 0.78, 1]; const MOD_COLOR: Color = [0.54, 0.71, 0.98, 1]; +// the engine's for the fluid in an item's tooltip +const FLUID_COLOR: Color = [0.55, 0.75, 0.95, 1]; // show the recipes that make an item, or the ones that use it export type Mode = "recipes" | "uses"; @@ -48,6 +50,33 @@ export function item_cell( return true; } +// an amount of a fluid in a slot, hovered for its name and amount. fluids have no recipes of their own to open +export function fluid_cell( + g: Graphics, + ctx: ClientContext, + fluid: FluidVariant, + amount: number, + x: number, + y: number, + tooltip: string[] = [], +) { + g.rect(x, y, SLOT, SLOT, SLOT_COLOR); + g.fluid(fluid, x, y); + const { mouse } = g; + if (mouse.x < x || mouse.y < y || mouse.x >= x + SLOT || mouse.y >= y + SLOT) return; + g.rect(x, y, SLOT, SLOT, HOVER_COLOR); + g.tooltip( + [ + ctx.fluids.name(fluid), + { text: `${amount} mB`, color: FLUID_COLOR }, + ...tooltip.map((text) => ({ text, color: LABEL_COLOR })), + { text: fluid.id, color: LABEL_COLOR }, + ], + mouse.x, + mouse.y, + ); +} + export class ItemList { // kept between screens, like jei page = 0; diff --git a/mods/recipe_viewer/scripts/recipes_screen.ts b/mods/recipe_viewer/scripts/recipes_screen.ts index b103b99..697a242 100644 --- a/mods/recipe_viewer/scripts/recipes_screen.ts +++ b/mods/recipe_viewer/scripts/recipes_screen.ts @@ -9,12 +9,13 @@ import type { ClientContext, Graphics, Screen } from "bworld/client"; import { crabpot_recipes, fishing_recipes, + juicing_recipes, make_categories, type RecipeCategory, type RecipeLayout, SLOT, } from "./categories.ts"; -import { BUTTON, item_cell, ItemList, LABEL_COLOR, type Mode, SLOT_COLOR } from "./item_list.ts"; +import { BUTTON, fluid_cell, item_cell, ItemList, LABEL_COLOR, type Mode, SLOT_COLOR } from "./item_list.ts"; import { type Ingredient, inputs, outputs, type Recipe } from "./recipes.ts"; // jei's sizes, in gui pixels @@ -42,7 +43,12 @@ export class RecipeIndex { get categories(): RecipeCategory[] { if (!this.#categories) { - this.#recipes = [...this.ctx.recipes.list(), ...fishing_recipes(this.ctx), ...crabpot_recipes(this.ctx)]; + this.#recipes = [ + ...this.ctx.recipes.list(), + ...fishing_recipes(this.ctx), + ...crabpot_recipes(this.ctx), + ...juicing_recipes(this.ctx), + ]; this.#categories = make_categories(this.ctx, this.#recipes); } return this.#categories; @@ -208,6 +214,7 @@ export class RecipesScreen implements Screen { const options = { slot: true, count: ingredient.count, tooltip: [...any, ...tooltip] }; if (this.#cell(g, id, x + dx, y + dy, options)) hovered = id; }, + fluid: (fluid, amount, dx, dy, tooltip) => fluid_cell(g, this.ctx, fluid, amount, x + dx, y + dy, tooltip), texture: (id, dx, dy, w, h) => g.texture(id, x + dx, y + dy, w, h), text: (text, dx, dy) => g.text(text, x + dx, y + dy, { color: LABEL_COLOR }), }; diff --git a/tests/recipe_viewer_test.ts b/tests/recipe_viewer_test.ts index 8e9c8e0..de3e733 100644 --- a/tests/recipe_viewer_test.ts +++ b/tests/recipe_viewer_test.ts @@ -3,10 +3,11 @@ // the recipe viewer mod reads every mod's recipes, its own types included import { assert, assertEquals } from "@std/assert"; -import { category_name } from "../mods/recipe_viewer/scripts/categories.ts"; +import { category_name, type RecipeLayout } from "../mods/recipe_viewer/scripts/categories.ts"; import { inputs as recipe_inputs, outputs } from "../mods/recipe_viewer/scripts/recipes.ts"; import { RecipeIndex } from "../mods/recipe_viewer/scripts/recipes_screen.ts"; import { tag_api } from "$/common/tags.ts"; +import { FluidRules } from "$/common/fluids.ts"; import { type BlockRegistry, EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts"; import type { ClientContext } from "bworld/client"; import { test_game } from "./helpers.ts"; @@ -33,6 +34,22 @@ function client_blocks() { } const client_loot = () => ({ table: (id: string) => EverythingRegistry.get("loot_tables", id) }); +// the context the recipe viewer gets, with these recipes from the mods' files +// deno-lint-ignore no-explicit-any +function client_ctx(recipes: any[] = []) { + return { + recipes: { list: () => recipes }, + tags: tag_api(), + items: client_items(), + blocks: client_blocks(), + loot: client_loot(), + fluids: new FluidRules(() => undefined).api(), + } as unknown as ClientContext; +} + +// a recipe layout that keeps nothing, for the parts a test doesn't look at +const ignore: RecipeLayout = { slot: () => {}, fluid: () => {}, texture: () => {}, text: () => {} }; + Deno.test("the recipe viewer finds what makes and uses an item, in any recipe type", async () => { const { game } = await test_game("mods"); const all = game.recipes.all; @@ -70,13 +87,7 @@ Deno.test("the recipe viewer finds what makes and uses an item, in any recipe ty Deno.test("recipes are found by category in jei's order, and a catalyst's uses are everything made in it", async () => { const { game } = await test_game("mods"); - const ctx = { - recipes: { list: () => game.recipes.all }, - tags: tag_api(), - items: client_items(), - blocks: client_blocks(), - loot: client_loot(), - } as unknown as ClientContext; + const ctx = client_ctx(game.recipes.all); const index = new RecipeIndex(ctx); const titles = index.categories.map((category) => category.title); @@ -95,13 +106,7 @@ Deno.test("recipes are found by category in jei's order, and a catalyst's uses a Deno.test("clicking a fish shows when and where it bites, and the fishing rod's uses are every fish", async () => { await test_game("mods"); - const ctx = { - recipes: { list: () => [] }, - tags: tag_api(), - items: client_items(), - blocks: client_blocks(), - loot: client_loot(), - } as unknown as ClientContext; + const ctx = client_ctx(); const index = new RecipeIndex(ctx); const [fishing] = index.find("bworld:red_mullet", "recipes"); assertEquals(fishing.category.title, "Fishing"); @@ -109,6 +114,7 @@ Deno.test("clicking a fish shows when and where it bites, and the fishing rod's const lines: string[] = []; const slots: string[] = []; fishing.category.draw(fishing.recipes[0], { + ...ignore, slot: (ingredient) => slots.push(ingredient!.ids[0]), texture: () => {}, text: (text) => lines.push(text), @@ -119,7 +125,7 @@ Deno.test("clicking a fish shows when and where it bites, and the fishing rod's // a lava fish without limits, and every fish under the rod const lava_lines: string[] = []; const [lava] = index.find("bworld:prismfish", "recipes"); - lava.category.draw(lava.recipes[0], { slot: () => {}, texture: () => {}, text: (text) => lava_lines.push(text) }); + lava.category.draw(lava.recipes[0], { ...ignore, text: (text) => lava_lines.push(text) }); assertEquals(lava_lines, ["Seasons: all", "Biomes: all", "Time: all day", "In: Lava"]); const fish = index.find("bworld:fishing_rod", "uses")[0].recipes.map((recipe) => recipe.result); assert(fish.includes("bworld:cod") && fish.includes("bworld:prismfish")); @@ -129,13 +135,7 @@ Deno.test("clicking a fish shows when and where it bites, and the fishing rod's Deno.test("a crab pot's recipes are what each bait catches in fresh and salt water, with their chances", async () => { await test_game("mods"); - const ctx = { - recipes: { list: () => [] }, - tags: tag_api(), - items: client_items(), - blocks: client_blocks(), - loot: client_loot(), - } as unknown as ClientContext; + const ctx = client_ctx(); const index = new RecipeIndex(ctx); // grubs are bait for both waters, under the crab pot's tab @@ -152,6 +152,7 @@ Deno.test("a crab pot's recipes are what each bait catches in fresh and salt wat const slots: [string, string[] | undefined][] = []; const lines: string[] = []; seashroom.category.draw(recipe, { + ...ignore, slot: (ingredient, _x, _y, tooltip) => slots.push([ingredient!.ids[0], tooltip]), texture: () => {}, text: (text) => lines.push(text), @@ -167,3 +168,29 @@ Deno.test("a crab pot's recipes are what each bait catches in fresh and salt wat assertEquals(freshwater.result.toSorted(), ["bworld:chorus_snail", "bworld:crayfish", "bworld:leech"]); assertEquals(freshwater.crabpot.catches.map((c: { chance: number }) => c.chance), [1 / 3, 1 / 3, 1 / 3]); }); + +Deno.test("each juiceable item has a juicing recipe that presses it into its own juice", async () => { + await test_game("mods"); + const index = new RecipeIndex(client_ctx()); + + // a strawberry's uses include the juicer pressing it into strawberry juice, shown in a fluid slot + const juicing = index.find("bworld:strawberry", "uses").find((found) => found.category.title === "Juicing")!; + assertEquals(juicing.category.catalysts, ["bworld:juicer"]); + assertEquals(juicing.recipes.length, 1); + const slots: string[] = []; + const fluids: [unknown, number][] = []; + juicing.category.draw(juicing.recipes[0], { + ...ignore, + slot: (ingredient) => slots.push(ingredient!.ids[0]), + fluid: (fluid, amount) => fluids.push([fluid, amount]), + }); + assertEquals(slots, ["bworld:strawberry"]); + const { juice } = EverythingRegistry.get("items", "bworld:strawberry")!.components![ + "bworld:juiceable" + ] as { juice: number }; + assertEquals(fluids, [[{ id: "bworld:juice", data: { item: "bworld:strawberry" } }, juice]]); + + // the juicer's uses are every juiceable item + const pressed = index.find("bworld:juicer", "uses")[0].recipes.map((recipe) => recipe.input); + assert(pressed.includes("bworld:strawberry") && pressed.includes("bworld:melon")); +});