Recipe viewer for crabpots
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user