Recipe viewer for fishing

This commit is contained in:
2026-10-11 22:58:28 -03:00
parent 3c80938e91
commit 935c0df7af
7 changed files with 113 additions and 5 deletions
+57
View File
@@ -98,9 +98,65 @@ function base_categories(tags: TagApi): RecipeCategory[] {
layout.slot({ ids: [recipe.result], count: 1 }, 90, 0);
},
},
{
// when and where a fish bites, see fishing_recipes
type: FISHING,
title: "Fishing",
width: 180,
height: 54,
catalysts: ["bworld:fishing_rod"],
draw(recipe, layout) {
const fish = recipe.fish as FishParams;
layout.slot({ ids: [recipe.result], count: 1 }, 0, 0);
const lines = [
// all four is "all", which also fits
`Seasons: ${
fish.seasons && fish.seasons.length < 4 ? fish.seasons.map(title_case).join(", ") : "all"
}`,
`Biomes: ${fish.biomes?.map(biome_name).join(", ") ?? "all"}`,
`Time: ${fish.time ? `${clock(fish.time.from)} to ${clock(fish.time.to)}` : "all day"}`,
`In: ${biome_name(fish.liquid ?? "bworld:water")}`,
];
lines.forEach((line, i) => layout.text(line, 24, 1 + i * 13));
},
},
];
}
// 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";
interface FishParams {
seasons?: string[];
biomes?: string[];
time?: { from: number; to: number };
liquid?: string;
}
export function fishing_recipes(ctx: ClientContext): Recipe[] {
return ctx.items.ids().flatMap((id) => {
const fish = ctx.items.components(id)["bworld:fish"];
return fish ? [{ type: FISHING, result: id, fish }] : [];
});
}
// "autumn" is "Autumn", "dark_oak" is "Dark Oak"
function title_case(name: string): string {
return name.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
}
// a biome, liquid or #biome tag by its name: "bworld:swamp" is "Swamp", "#bworld:is_ocean" is "Ocean"
function biome_name(id: string): string {
return title_case(id.replace(/^#/, "").split(":")[1].replace(/^is_/, ""));
}
// hours on a 24 hour clock, fractions as minutes: 6.5 is "6:30"
function clock(hours: number): string {
const minutes = Math.round((hours % 1) * 60);
return `${Math.floor(hours)}:${String(minutes).padStart(2, "0")}`;
}
// a mod's own type, from its recipes' json: the items in its lists in a grid, a single item (like a bowl to serve in)
// under the arrow, then what it makes, one under the other
function mod_category(type: string, tags: TagApi): RecipeCategory {
@@ -144,6 +200,7 @@ export function category_name(type: string): string {
furnace: "Smelting",
fuel: "Fuel",
smithing: "Smithing",
[FISHING]: "Fishing",
};
if (base[type]) return base[type];
const name = type.split(":")[1] ?? type;
+2 -2
View File
@@ -3,7 +3,7 @@
// many recipes as fit, one under the other. the item list stays on the right. escape or the inventory key go back to
// the inventory, and backspace to what was shown before
import type { ClientContext, Graphics, Screen } from "bworld/client";
import { make_categories, type RecipeCategory, type RecipeLayout, SLOT } from "./categories.ts";
import { fishing_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 { type Ingredient, inputs, outputs, type Recipe } from "./recipes.ts";
@@ -32,7 +32,7 @@ export class RecipeIndex {
get categories(): RecipeCategory[] {
if (!this.#categories) {
this.#recipes = this.ctx.recipes.list();
this.#recipes = [...this.ctx.recipes.list(), ...fishing_recipes(this.ctx)];
this.#categories = make_categories(this.ctx, this.#recipes);
}
return this.#categories;