Recipe viewer for crabpots

This commit is contained in:
2026-10-12 02:39:25 -03:00
parent 726a52b91a
commit 2ebcf55ad3
9 changed files with 201 additions and 13 deletions
+12 -1
View File
@@ -32,7 +32,18 @@
"waterloggable": "bworld:water",
"interactive": true,
"components": {
"bworld:crabpot": {}
"bworld:crabpot": {
"baits": {
"bworld:worms": {
"freshwater": "bworld:gameplay/crabpot_worms_freshwater",
"saltwater": "bworld:gameplay/crabpot_worms_saltwater"
},
"bworld:grubs": {
"freshwater": "bworld:gameplay/crabpot_grubs_freshwater",
"saltwater": "bworld:gameplay/crabpot_grubs_saltwater"
}
}
}
}
}
}
+10 -8
View File
@@ -274,8 +274,11 @@ const RESULT = 3;
// bait
const CRABPOT_BAIT = 0;
const CRABPOT_OUTPUT = 9;
// each bait's loot tables, bworld:gameplay/crabpot_<name>_saltwater and _freshwater
const CRABPOT_BAITS: Record<string, string> = { "bworld:worms": "worms", "bworld:grubs": "grubs" };
// the bworld:crabpot component's params: each bait's loot tables, read by the recipe viewer too
interface CrabpotParams {
baits: Record<string, { freshwater: string; saltwater: string }>;
}
function register_crabpot(ctx: ServerContext) {
const menu_type = ctx.menus.register<BlockRef>("bworld:crabpot", (menu, block) => {
@@ -289,7 +292,7 @@ function register_crabpot(ctx: ServerContext) {
menu.still_valid_near(block);
});
ctx.components.register_block("bworld:crabpot", {
ctx.components.register_block<CrabpotParams>("bworld:crabpot", {
on_interact(block, _params, player) {
menu_type.open(player, block);
return true;
@@ -297,7 +300,7 @@ function register_crabpot(ctx: ServerContext) {
on_break(block) {
drop_contents(ctx, block);
},
on_new_day(block) {
on_new_day(block, params) {
const { x, y, z } = block;
if (ctx.world.get_state(x, y, z, "waterlogged") !== 1) {
return;
@@ -305,13 +308,12 @@ function register_crabpot(ctx: ServerContext) {
// one that's never been opened has nothing in it
const container = block.data?.container && ctx.containers.get(block.data.container);
const bait = container && container.get(CRABPOT_BAIT);
const bait_name = bait && CRABPOT_BAITS[bait.id];
if (!bait || !bait_name) {
const tables = bait && params.baits[bait.id];
if (!bait || !tables) {
return;
}
const water = ctx.tags.has("biomes", "bworld:is_ocean", block.biome) ? "saltwater" : "freshwater";
const table = `bworld:gameplay/crabpot_${bait_name}_${water}`;
const table = ctx.tags.has("biomes", "bworld:is_ocean", block.biome) ? tables.saltwater : tables.freshwater;
const position = { x: x + 0.5, y: y + 0.5, z: z + 0.5 };
for (const item of ctx.loot.roll(table, { position })) {
// onto a stack of it with room, or into an empty output slot
+3 -1
View File
@@ -13,7 +13,9 @@ them, Backspace goes back, and Escape or the inventory key (E) returns to the in
It reads every mod's recipes with `ctx.recipes.list()`. Fish, the items with the base game's `bworld:fish` component,
get a Fishing category too: clicking one shows its seasons, biomes, hours and the liquid it's caught in, read with
`ctx.items.components`, and the fishing rod's uses are every fish.
`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`.
- `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
+83
View File
@@ -120,6 +120,28 @@ function base_categories(tags: TagApi): RecipeCategory[] {
lines.forEach((line, i) => layout.text(line, 24, 1 + i * 13));
},
},
{
// what a crab pot catches with a bait in one kind of water, see crabpot_recipes
type: CRABPOT,
title: "Crab Pot",
width: 168,
height: 54,
catalysts: [CRABPOT_BLOCK],
draw(recipe, layout) {
const { water, catches } = recipe.crabpot as { water: string; catches: Catch[] };
layout.slot({ ids: [recipe.bait], count: 1 }, 0, 0, ["Bait"]);
layout.text(title_case(water), 0, 23);
layout.texture("bworld:arrow_full", 34, 1, ARROW, ARROW);
catches.slice(0, CATCH_COLUMNS * 3).forEach(({ id, chance }, i) =>
layout.slot(
{ ids: [id], count: 1 },
60 + (i % CATCH_COLUMNS) * SLOT,
Math.floor(i / CATCH_COLUMNS) * SLOT,
[`${percent(chance)} chance`],
)
);
},
},
];
}
@@ -141,6 +163,66 @@ export function fishing_recipes(ctx: ClientContext): Recipe[] {
});
}
// the base game's crab pot, from its bworld:crabpot component's params: a recipe of its own type for each bait and
// kind of water, that makes everything that water's loot table can give. the bait is an input like any recipe's, the
// catches and their chances are under "crabpot", which reading a recipe's inputs skips
const CRABPOT = "bworld:crabpot";
const CRABPOT_BLOCK = "bworld:crabpot";
const CATCH_COLUMNS = 6;
interface CrabpotParams {
baits?: Record<string, { freshwater: string; saltwater: string }>;
}
// one item a loot table can give, and how likely it is to be the one
interface Catch {
id: string;
chance: number;
}
export function crabpot_recipes(ctx: ClientContext): Recipe[] {
if (!ctx.blocks.exists(CRABPOT_BLOCK)) return [];
const params = ctx.blocks.components(CRABPOT_BLOCK)[CRABPOT_BLOCK] as CrabpotParams | undefined;
return Object.entries(params?.baits ?? {}).flatMap(([bait, tables]) =>
(["freshwater", "saltwater"] as const).map((water) => {
const caught = catches(ctx, tables[water]);
return { type: CRABPOT, bait, result: caught.map((c) => c.id), crabpot: { water, catches: caught } };
})
);
}
// what one roll of a table's pools gives, by its entries' weights, most likely first. nested tables are worked
// through, a tag's items share its chance, and conditions and functions aren't shown
function catches(ctx: ClientContext, id: string, seen = new Set<string>()): Catch[] {
const table = ctx.loot.table(id);
if (!table || seen.has(id)) return [];
seen.add(id);
const chances = new Map<string, number>();
const add = (item: string, chance: number) => chances.set(item, (chances.get(item) ?? 0) + chance);
for (const pool of table.pools) {
const total = pool.entries.reduce((sum, entry) => sum + (entry.weight ?? 1), 0);
for (const entry of pool.entries) {
const chance = (entry.weight ?? 1) / total;
const type = entry.type ?? "item";
if (type === "item" && entry.item) {
add(entry.item, chance);
} else if (type === "tag" && entry.tag) {
const items = ctx.tags.expand(entry.tag);
for (const item of items) add(item, chance / items.length);
} else if (type === "loot_table" && entry.table) {
for (const inner of catches(ctx, entry.table, seen)) add(inner.id, chance * inner.chance);
}
}
}
return [...chances].map(([id, chance]) => ({ id, chance })).sort((a, b) => b.chance - a.chance);
}
// 0.0625 is "6.3%", 0.5 is "50%"
function percent(chance: number): string {
const value = chance * 100;
return `${Number.isInteger(value) ? value : value.toFixed(1)}%`;
}
// "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(" ");
@@ -201,6 +283,7 @@ export function category_name(type: string): string {
fuel: "Fuel",
smithing: "Smithing",
[FISHING]: "Fishing",
[CRABPOT]: "Crab Pot",
};
if (base[type]) return base[type];
const name = type.split(":")[1] ?? type;
+9 -2
View File
@@ -3,7 +3,14 @@
// 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 { fishing_recipes, make_categories, type RecipeCategory, type RecipeLayout, SLOT } from "./categories.ts";
import {
crabpot_recipes,
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 +39,7 @@ export class RecipeIndex {
get categories(): RecipeCategory[] {
if (!this.#categories) {
this.#recipes = [...this.ctx.recipes.list(), ...fishing_recipes(this.ctx)];
this.#recipes = [...this.ctx.recipes.list(), ...fishing_recipes(this.ctx), ...crabpot_recipes(this.ctx)];
this.#categories = make_categories(this.ctx, this.#recipes);
}
return this.#categories;