Implement tags !

This commit is contained in:
2026-09-30 13:41:29 -03:00
parent dc70ada7c4
commit 342b45d593
51 changed files with 978 additions and 219 deletions
+29 -31
View File
@@ -1,7 +1,7 @@
// 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";
import type { ClientContext, Color, Graphics, InventoryScreenView, TagApi } from "bworld/client";
// items and slots are drawn this big
const CELL = 54;
@@ -81,7 +81,7 @@ class RecipeViewer {
return this.#recipes.filter((recipe) =>
mode === "recipes"
? outputs(recipe).some((output) => output.ids.includes(item))
: inputs(recipe).some((input) => input.ids.includes(item))
: inputs(recipe, this.ctx.tags).some((input) => input.ids.includes(item))
);
}
@@ -165,14 +165,14 @@ class RecipeViewer {
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);
const pages = paginate(recipes, bottom - top, this.ctx.tags);
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);
card_y += card_height(recipe, this.ctx.tags);
}
const page_count = pages.length;
@@ -223,7 +223,7 @@ class RecipeViewer {
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,
choice ? { ids: this.ctx.tags.expand(choice), count: 1 } : undefined,
x + column * CELL,
top + row * CELL,
);
@@ -234,14 +234,14 @@ class RecipeViewer {
break;
}
case "furnace":
cell({ ids: choice_ids(recipe.input), count: 1 }, x, top);
cell({ ids: this.ctx.tags.expand(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);
cell({ ids: this.ctx.tags.expand(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,
@@ -257,7 +257,7 @@ class RecipeViewer {
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);
const { grid, extras } = mod_recipe_inputs(recipe, this.ctx.tags);
grid.slice(0, 9).forEach((ingredient, i) =>
cell(ingredient, x + (i % 3) * CELL, top + Math.floor(i / 3) * CELL)
);
@@ -281,21 +281,21 @@ class RecipeViewer {
}
// a card's height: its label, its rows of items, and a gap
function card_height(recipe: Recipe): number {
function card_height(recipe: Recipe, tags: TagApi): 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);
rows = Math.max(2, Math.ceil(mod_recipe_inputs(recipe, tags).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[][] {
function paginate(recipes: Recipe[], height: number, tags: TagApi): Recipe[][] {
const pages: Recipe[][] = [[]];
let used = 0;
for (const recipe of recipes) {
const needed = card_height(recipe);
const needed = card_height(recipe, tags);
if (used + needed > height && pages.at(-1)!.length > 0) {
pages.push([]);
used = 0;
@@ -306,11 +306,6 @@ function paginate(recipes: Recipe[], height: number): Recipe[][] {
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) {
@@ -334,50 +329,53 @@ export function outputs(recipe: Recipe): Ingredient[] {
}
}
// what a recipe takes
export function inputs(recipe: Recipe): Ingredient[] {
// what a recipe takes. shaped keys, furnace inputs and fuels can be an item or #tag, or a list any of which fits
export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] {
switch (recipe.type) {
case "shaped":
return Object.values(recipe.key as Record<string, string | string[]>).map((choice) => ({
ids: choice_ids(choice),
ids: tags.expand(choice),
count: 1,
}));
case "furnace":
return [{ ids: choice_ids(recipe.input), count: 1 }];
return [{ ids: tags.expand(recipe.input), count: 1 }];
case "fuel":
return [{ ids: choice_ids(recipe.item), count: 1 }];
return [{ ids: tags.expand(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);
const { grid, extras } = mod_recipe_inputs(recipe, tags);
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[] } {
// the items in a mod recipe's fields besides its type and result: each item or #tag 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, tags: TagApi): { 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);
const ingredient = (choice: string | string[]) => ({ ids: tags.expand(choice), count: 1 });
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 });
extras.push(ingredient(value));
} 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 });
if (is_id(entry) || (Array.isArray(entry) && entry.every(is_id))) grid.push(ingredient(entry));
}
}
}
return { grid, extras };
}
// an item id or #tag
function is_id(value: unknown): value is string {
return typeof value === "string" && /^#?[a-z0-9_]+:[a-z0-9_/]+$/.test(value);
}
function stack(value: { id: string; count: number }): Ingredient {
return { ids: [value.id], count: value.count };
}