408 lines
15 KiB
TypeScript
408 lines
15 KiB
TypeScript
// a recipe viewer like just enough items: every item in a list next to the inventory, searchable, and clicking one (or
|
|
// R and U over any item) shows the recipes that make it and the ones that use it. it only reads recipes, so it works
|
|
// with any mod's, and draws the ones it doesn't know as their items in, then what comes out
|
|
import type { ClientContext, Color, Graphics, InventoryScreenView } from "bworld/client";
|
|
|
|
// items and slots are drawn this big
|
|
const CELL = 54;
|
|
const GAP = 16;
|
|
const TEXT = 22;
|
|
const LINE = 30;
|
|
const BUTTON = 32;
|
|
// catppuccin mocha, like the engine's gui: crust, pink, subtext 0 and blue
|
|
const SLOT_COLOR: Color = [0.07, 0.07, 0.11, 0.6];
|
|
const HOVER_COLOR: Color = [0.96, 0.76, 0.91, 0.35];
|
|
const LABEL_COLOR: Color = [0.65, 0.68, 0.78, 1];
|
|
const MOD_COLOR: Color = [0.54, 0.71, 0.98, 1];
|
|
// alternatives, like "any vegetable", take turns this often
|
|
const CYCLE_MS = 1000;
|
|
|
|
// where each recipe type is made, like jei's recipe catalysts. shaped recipes are made in the inventory
|
|
const CATALYSTS: Record<string, string> = {
|
|
furnace: "bworld:furnace",
|
|
fuel: "bworld:furnace",
|
|
smithing: "bworld:smithing_table",
|
|
"farmersdelight:cooking": "farmersdelight:cooking_pot",
|
|
"farmersdelight:cutting": "farmersdelight:cutting_board",
|
|
};
|
|
const CATEGORY_NAMES: Record<string, string> = {
|
|
shaped: "Crafting",
|
|
furnace: "Smelting",
|
|
fuel: "Fuel",
|
|
smithing: "Smithing",
|
|
};
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
type Recipe = any;
|
|
// one place in a recipe: the items any of which fit, and how many
|
|
type Ingredient = { ids: string[]; count: number };
|
|
type Mode = "recipes" | "uses";
|
|
|
|
export function setup(ctx: ClientContext) {
|
|
ctx.ui.add_inventory_overlay(new RecipeViewer(ctx));
|
|
}
|
|
|
|
class RecipeViewer {
|
|
#page = 0;
|
|
// what the recipe view shows, and where it came from for going back
|
|
#view: { item: string; mode: Mode; page: number } | undefined;
|
|
#history: { item: string; mode: Mode; page: number }[] = [];
|
|
// set on the first frame, once every mod's items and recipes are loaded
|
|
#items: string[] | undefined;
|
|
#recipes: Recipe[] = [];
|
|
|
|
constructor(private ctx: ClientContext) {}
|
|
|
|
on_render(g: Graphics, screen: InventoryScreenView) {
|
|
this.#items ??= this.ctx.items.ids().filter((id) => id !== "bworld:air");
|
|
if (this.#recipes.length === 0) this.#recipes = this.ctx.recipes.list();
|
|
|
|
// the item under the mouse in the list or the recipe view, then in the inventory's slots
|
|
let hovered = this.#item_list(g, screen);
|
|
if (this.#view) hovered = this.#recipe_view(g, screen, this.#view) ?? hovered;
|
|
hovered ??= screen.hovered_item?.id;
|
|
|
|
if (hovered && g.key_pressed("KeyR")) this.#open(hovered, "recipes");
|
|
if (hovered && g.key_pressed("KeyU")) this.#open(hovered, "uses");
|
|
if (this.#view && g.key_pressed("Backspace")) this.#back();
|
|
}
|
|
|
|
#open(item: string, mode: Mode) {
|
|
if (this.#find(item, mode).length === 0) return;
|
|
if (this.#view) this.#history.push(this.#view);
|
|
this.#view = { item, mode, page: 0 };
|
|
}
|
|
|
|
#back() {
|
|
this.#view = this.#history.pop();
|
|
}
|
|
|
|
#find(item: string, mode: Mode): Recipe[] {
|
|
return this.#recipes.filter((recipe) =>
|
|
mode === "recipes"
|
|
? outputs(recipe).some((output) => output.ids.includes(item))
|
|
: inputs(recipe).some((input) => input.ids.includes(item))
|
|
);
|
|
}
|
|
|
|
// every item, in pages in the space right of the screen, with a search box under them. returns the hovered one
|
|
#item_list(g: Graphics, screen: InventoryScreenView): string | undefined {
|
|
const left = screen.x + screen.width + GAP;
|
|
const width = g.width - GAP - left;
|
|
const columns = Math.floor(width / CELL);
|
|
const top = GAP + BUTTON + GAP / 2;
|
|
const rows = Math.floor((g.height - GAP - BUTTON - GAP / 2 - top) / CELL);
|
|
if (columns < 2 || rows < 1) return undefined;
|
|
|
|
const search = g.text_input("search", left, g.height - GAP - BUTTON, columns * CELL).trim().toLowerCase();
|
|
const items = this.#items!.filter((id) => matches_search(this.ctx, id, search));
|
|
const per_page = columns * rows;
|
|
const pages = Math.max(1, Math.ceil(items.length / per_page));
|
|
this.#page = Math.min(this.#page, pages - 1);
|
|
|
|
// the page bar
|
|
if (g.button("<", left, GAP, BUTTON * 1.5, BUTTON)) this.#page = (this.#page + pages - 1) % pages;
|
|
if (g.button(">", left + columns * CELL - BUTTON * 1.5, GAP, BUTTON * 1.5, BUTTON)) {
|
|
this.#page = (this.#page + 1) % pages;
|
|
}
|
|
const label = `${this.#page + 1}/${pages}`;
|
|
g.text(label, left + (columns * CELL - g.measure_text(label)) / 2, GAP + (BUTTON - TEXT) / 2);
|
|
|
|
let hovered: string | undefined;
|
|
items.slice(this.#page * per_page, (this.#page + 1) * per_page).forEach((id, i) => {
|
|
const x = left + (i % columns) * CELL;
|
|
const y = top + Math.floor(i / columns) * CELL;
|
|
if (this.#item_cell(g, id, x, y, false)) hovered = id;
|
|
});
|
|
if (items.length === 0) g.text("Nothing found", left, top, { color: [0.8, 0.8, 0.8, 1] });
|
|
return hovered;
|
|
}
|
|
|
|
// an item that can be hovered for its tooltip and clicked for its recipes (right click, its uses)
|
|
#item_cell(g: Graphics, id: string, x: number, y: number, slot: boolean, count = 1, extra: string[] = []): boolean {
|
|
if (slot) g.rect(x, y, CELL, CELL, SLOT_COLOR);
|
|
g.item(id, x, y, count);
|
|
const { mouse } = g;
|
|
if (mouse.x < x || mouse.y < y || mouse.x >= x + CELL || mouse.y >= y + CELL) return false;
|
|
g.rect(x, y, CELL, CELL, HOVER_COLOR);
|
|
g.item_tooltip(id, mouse.x, mouse.y, [
|
|
...extra,
|
|
{ text: id, color: LABEL_COLOR },
|
|
{ text: this.ctx.items.mod(id), color: MOD_COLOR },
|
|
]);
|
|
if (mouse.pressed) {
|
|
g.consume_click();
|
|
this.#open(id, "recipes");
|
|
} else if (mouse.right_pressed) {
|
|
g.consume_click();
|
|
this.#open(id, "uses");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// the recipes for or with an item, a page at a time, over the screen. returns the hovered item
|
|
#recipe_view(g: Graphics, screen: InventoryScreenView, view: { item: string; mode: Mode; page: number }) {
|
|
const { x, y, width, height } = screen;
|
|
g.block_mouse(x, y, width, height);
|
|
g.panel(x, y, width, height);
|
|
|
|
// header: the item, what's shown, back and close
|
|
const title = `${this.ctx.items.name(view.item)} - ${view.mode === "recipes" ? "Recipes" : "Uses"}`;
|
|
g.text(title, x + GAP, y + GAP, { color: LABEL_COLOR });
|
|
if (g.button("X", x + width - GAP - BUTTON, y + GAP / 2, BUTTON, BUTTON)) {
|
|
this.#view = undefined;
|
|
this.#history = [];
|
|
return undefined;
|
|
}
|
|
if (
|
|
this.#history.length > 0 &&
|
|
g.button("Back", x + width - GAP * 2 - BUTTON * 4, y + GAP / 2, BUTTON * 3, BUTTON)
|
|
) {
|
|
this.#back();
|
|
return undefined;
|
|
}
|
|
|
|
const recipes = this.#find(view.item, view.mode);
|
|
const top = y + GAP + BUTTON;
|
|
const bottom = y + height - GAP - BUTTON;
|
|
const pages = paginate(recipes, bottom - top);
|
|
view.page = Math.min(view.page, pages.length - 1);
|
|
|
|
let hovered: string | undefined;
|
|
let card_y = top;
|
|
for (const recipe of pages[view.page]) {
|
|
hovered = this.#card(g, recipe, x + GAP, card_y, width - GAP * 2) ?? hovered;
|
|
card_y += card_height(recipe);
|
|
}
|
|
const page_count = pages.length;
|
|
|
|
if (page_count > 1) {
|
|
if (g.button("<", x + GAP, bottom, BUTTON * 1.5, BUTTON)) {
|
|
view.page = (view.page + page_count - 1) % page_count;
|
|
}
|
|
if (g.button(">", x + width - GAP - BUTTON * 1.5, bottom, BUTTON * 1.5, BUTTON)) {
|
|
view.page = (view.page + 1) % page_count;
|
|
}
|
|
const label = `${view.page + 1}/${page_count}`;
|
|
g.text(label, x + (width - g.measure_text(label)) / 2, bottom + (BUTTON - TEXT) / 2, {
|
|
color: LABEL_COLOR,
|
|
});
|
|
}
|
|
return hovered;
|
|
}
|
|
|
|
// one recipe: its category and where it's made, then its items. returns the hovered item
|
|
#card(g: Graphics, recipe: Recipe, x: number, y: number, width: number): string | undefined {
|
|
let hovered: string | undefined;
|
|
const cell = (ingredient: Ingredient | undefined, cx: number, cy: number, extra: string[] = []) => {
|
|
if (!ingredient || ingredient.ids.length === 0) {
|
|
g.rect(cx, cy, CELL, CELL, SLOT_COLOR);
|
|
return;
|
|
}
|
|
const id = ingredient.ids[Math.floor(Date.now() / CYCLE_MS) % ingredient.ids.length];
|
|
const any = ingredient.ids.length > 1 ? [`Any of ${ingredient.ids.length}`] : [];
|
|
if (this.#item_cell(g, id, cx, cy, true, ingredient.count, [...any, ...extra])) hovered = id;
|
|
};
|
|
const texture = (id: string, cx: number, cy: number) => g.texture(id, cx, cy, CELL, CELL);
|
|
|
|
g.text(category_name(recipe.type), x, y, { color: LABEL_COLOR });
|
|
const catalyst = CATALYSTS[recipe.type];
|
|
if (catalyst && this.ctx.items.exists(catalyst)) {
|
|
cell({ ids: [catalyst], count: 1 }, x + width - CELL, y, ["Made here"]);
|
|
} else if (recipe.type === "shaped") {
|
|
const text = "In the inventory";
|
|
g.text(text, x + width - g.measure_text(text), y, { color: LABEL_COLOR });
|
|
}
|
|
const top = y + LINE;
|
|
|
|
switch (recipe.type) {
|
|
case "shaped": {
|
|
const width_in = Math.max(...recipe.pattern.map((row: string) => row.length));
|
|
for (let row = 0; row < 3; row++) {
|
|
for (let column = 0; column < 3; column++) {
|
|
const letter = recipe.pattern[row]?.[column];
|
|
const choice = letter && letter !== " " && column < width_in ? recipe.key[letter] : undefined;
|
|
cell(
|
|
choice ? { ids: choice_ids(choice), count: 1 } : undefined,
|
|
x + column * CELL,
|
|
top + row * CELL,
|
|
);
|
|
}
|
|
}
|
|
texture("bworld:arrow_full", x + 3.5 * CELL, top + CELL);
|
|
cell(stack(recipe.result), x + 5 * CELL, top + CELL);
|
|
break;
|
|
}
|
|
case "furnace":
|
|
cell({ ids: choice_ids(recipe.input), count: 1 }, x, top);
|
|
texture("bworld:fire_full", x, top + CELL);
|
|
texture("bworld:arrow_full", x + 1.5 * CELL, top + CELL / 2);
|
|
cell(stack(recipe.output), x + 3 * CELL, top + CELL / 2);
|
|
g.text(seconds(recipe.cook_time), x + 1.5 * CELL, top + CELL * 1.5, { color: LABEL_COLOR });
|
|
break;
|
|
case "fuel":
|
|
cell({ ids: choice_ids(recipe.item), count: 1 }, x, top);
|
|
texture("bworld:fire_full", x, top + CELL);
|
|
g.text(`Burns for ${seconds(recipe.burn_time)}`, x + 1.5 * CELL, top + CELL / 2, {
|
|
color: LABEL_COLOR,
|
|
});
|
|
break;
|
|
case "smithing":
|
|
cell({ ids: [recipe.tool], count: 1 }, x, top + CELL / 2);
|
|
cell(stack(recipe.material), x + CELL, top + CELL / 2);
|
|
cell(recipe.addition ? { ids: [recipe.addition], count: 1 } : undefined, x + 2 * CELL, top + CELL / 2);
|
|
texture("bworld:arrow_full", x + 3.5 * CELL, top + CELL / 2);
|
|
cell({ ids: [recipe.result], count: 1 }, x + 5 * CELL, top + CELL / 2);
|
|
break;
|
|
default: {
|
|
// a mod's own type: the items in its lists in a grid, single items (like a bowl to serve in) under
|
|
// the arrow, then what it makes, one under the other
|
|
const { grid, extras } = mod_recipe_inputs(recipe);
|
|
grid.slice(0, 9).forEach((ingredient, i) =>
|
|
cell(ingredient, x + (i % 3) * CELL, top + Math.floor(i / 3) * CELL)
|
|
);
|
|
texture("bworld:arrow_full", x + 3.5 * CELL, top + CELL / 2);
|
|
extras.slice(0, 1).forEach((ingredient) =>
|
|
cell(ingredient, x + 3.5 * CELL, top + CELL * 1.5, ["Served in"])
|
|
);
|
|
const made = outputs(recipe).slice(0, 3);
|
|
made.forEach((output, i) =>
|
|
cell(output, x + 5 * CELL, made.length > 1 ? top + i * CELL : top + CELL / 2)
|
|
);
|
|
if (typeof recipe.cook_time === "number") {
|
|
g.text(seconds(recipe.cook_time), x + 5 * CELL, top + CELL * 1.5 + (CELL - TEXT) / 2, {
|
|
color: LABEL_COLOR,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return hovered;
|
|
}
|
|
}
|
|
|
|
// a card's height: its label, its rows of items, and a gap
|
|
function card_height(recipe: Recipe): number {
|
|
let rows = 2;
|
|
if (recipe.type === "shaped") rows = 3;
|
|
else if (!(recipe.type in CATEGORY_NAMES)) {
|
|
rows = Math.max(2, Math.ceil(mod_recipe_inputs(recipe).grid.length / 3), outputs(recipe).length);
|
|
}
|
|
return LINE + Math.min(rows, 3) * CELL + GAP;
|
|
}
|
|
|
|
// as many cards on each page as fit, at least one
|
|
function paginate(recipes: Recipe[], height: number): Recipe[][] {
|
|
const pages: Recipe[][] = [[]];
|
|
let used = 0;
|
|
for (const recipe of recipes) {
|
|
const needed = card_height(recipe);
|
|
if (used + needed > height && pages.at(-1)!.length > 0) {
|
|
pages.push([]);
|
|
used = 0;
|
|
}
|
|
pages.at(-1)!.push(recipe);
|
|
used += needed;
|
|
}
|
|
return pages;
|
|
}
|
|
|
|
// shaped keys, furnace inputs and fuels can be one item or a list any of which fits
|
|
function choice_ids(choice: string | string[]): string[] {
|
|
return typeof choice === "string" ? [choice] : choice;
|
|
}
|
|
|
|
// what a recipe makes
|
|
export function outputs(recipe: Recipe): Ingredient[] {
|
|
switch (recipe.type) {
|
|
case "shaped":
|
|
return [stack(recipe.result)];
|
|
case "furnace":
|
|
return [stack(recipe.output)];
|
|
case "fuel":
|
|
return [];
|
|
case "smithing":
|
|
return [{ ids: [recipe.result], count: 1 }];
|
|
default: {
|
|
// one item or stack, or a list of them, like what a cutting board cuts something into
|
|
const output = recipe.result ?? recipe.output;
|
|
return [output].flat().flatMap((output): Ingredient[] => {
|
|
if (typeof output === "string") return [{ ids: [output], count: 1 }];
|
|
if (output && typeof output.id === "string") return [stack(output)];
|
|
return [];
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// what a recipe takes
|
|
export function inputs(recipe: Recipe): Ingredient[] {
|
|
switch (recipe.type) {
|
|
case "shaped":
|
|
return Object.values(recipe.key as Record<string, string | string[]>).map((choice) => ({
|
|
ids: choice_ids(choice),
|
|
count: 1,
|
|
}));
|
|
case "furnace":
|
|
return [{ ids: choice_ids(recipe.input), count: 1 }];
|
|
case "fuel":
|
|
return [{ ids: choice_ids(recipe.item), count: 1 }];
|
|
case "smithing":
|
|
return [{ ids: [recipe.tool], count: 1 }, stack(recipe.material)].concat(
|
|
recipe.addition ? [{ ids: [recipe.addition], count: 1 }] : [],
|
|
);
|
|
default: {
|
|
const { grid, extras } = mod_recipe_inputs(recipe);
|
|
return [...grid, ...extras];
|
|
}
|
|
}
|
|
}
|
|
|
|
// the items in a mod recipe's fields besides its type and result: each item in a list is an ingredient (a list in a
|
|
// list is one ingredient any of whose items fit), and a field that's one item is an extra, like a container
|
|
function mod_recipe_inputs(recipe: Recipe): { grid: Ingredient[]; extras: Ingredient[] } {
|
|
const grid: Ingredient[] = [];
|
|
const extras: Ingredient[] = [];
|
|
const is_id = (value: unknown): value is string =>
|
|
typeof value === "string" && /^[a-z0-9_]+:[a-z0-9_/]+$/.test(value);
|
|
for (const [field, value] of Object.entries(recipe)) {
|
|
if (field === "type" || field === "result" || field === "output") continue;
|
|
if (is_id(value)) {
|
|
extras.push({ ids: [value], count: 1 });
|
|
} else if (Array.isArray(value)) {
|
|
for (const entry of value) {
|
|
if (is_id(entry)) grid.push({ ids: [entry], count: 1 });
|
|
else if (Array.isArray(entry) && entry.every(is_id)) grid.push({ ids: entry, count: 1 });
|
|
}
|
|
}
|
|
}
|
|
return { grid, extras };
|
|
}
|
|
|
|
function stack(value: { id: string; count: number }): Ingredient {
|
|
return { ids: [value.id], count: value.count };
|
|
}
|
|
|
|
// "Crafting", or a mod type's name: farmersdelight:cooking is "Cooking"
|
|
export function category_name(type: string): string {
|
|
if (CATEGORY_NAMES[type]) return CATEGORY_NAMES[type];
|
|
const name = type.split(":")[1] ?? type;
|
|
return name.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
|
|
}
|
|
|
|
function seconds(ticks: number): string {
|
|
const s = ticks / 20;
|
|
return `${Number.isInteger(s) ? s : s.toFixed(1)} s`;
|
|
}
|
|
|
|
// by name or id, or "@mod" for a mod's items, like jei
|
|
function matches_search(ctx: ClientContext, id: string, search: string): boolean {
|
|
if (search === "") return true;
|
|
return search.split(/\s+/).every((word) => {
|
|
if (word.startsWith("@")) {
|
|
const mod = word.slice(1);
|
|
return id.split(":")[0].includes(mod) || ctx.items.mod(id).toLowerCase().includes(mod);
|
|
}
|
|
return ctx.items.name(id).toLowerCase().includes(word) || id.includes(word);
|
|
});
|
|
}
|