diff --git a/docs/modding/resources/recipes.md b/docs/modding/resources/recipes.md index 46be52b..bdb5d1f 100644 --- a/docs/modding/resources/recipes.md +++ b/docs/modding/resources/recipes.md @@ -68,9 +68,11 @@ For the smithing table, which upgrades a tool. It's the base game's own type, re } ``` -An optional `addition` is a third item it also takes, as `{ "id", "count" }` like the material. Upgrading uses up the -tool and `count` of the material and the addition, and the result keeps the tool's `data`, like its -[durability](../items/tools.md#durability). Two smithing recipes with the same tool, material and addition are an error. +An optional `addition` is a third item it also takes, as `{ "id", "count" }` like the material. The tool and each `id` +are [choices](#choices), so `"material": { "id": "#bworld:cobblestones", "count": 5 }` takes 5 of any one cobblestone. +Upgrading uses up the tool and `count` of the material and the addition, and the result keeps the tool's `data`, like +its [durability](../items/tools.md#durability). Two smithing recipes that both fit the same tool, material and addition +are an error. ## Stonecutting: `bworld:stonecutting` @@ -142,8 +144,8 @@ furnace smelts. ## Choices Anywhere a recipe takes a choice of items (`shaped` keys, and the fields a type reads with -[`read.ingredient`](#custom-recipe-types), like smelting's `input` and fuel's `item`), an item [tag](tags.md) means -anything in it fits, and so does a list of ids and tags: +[`read.ingredient`](#custom-recipe-types), like smelting's `input`, fuel's `item` and smithing's `tool`), an item +[tag](tags.md) means anything in it fits, and so does a list of ids and tags: ```json "key": { "P": "#bworld:planks" } diff --git a/mods/bworld/scripts/smithing.ts b/mods/bworld/scripts/smithing.ts index 8c9cdef..fe6a648 100644 --- a/mods/bworld/scripts/smithing.ts +++ b/mods/bworld/scripts/smithing.ts @@ -3,14 +3,20 @@ // smithing recipes: the smithing table upgrades a tool with some of a material and maybe some of one more item. the // type is registered by the shared script, so the server's menu and anything on a client find them the same way -import type { ItemStack, SharedContext } from "cubi/shared"; +import type { ItemStack, RecipeReader, SharedContext } from "cubi/shared"; export const SMITHING = "bworld:smithing"; +// the items any of which fit, from an item, a #tag or a list of them, and how many it takes +export interface SmithingIngredient { + items: string[]; + count: number; +} + export interface SmithingRecipe { - tool: string; - material: { id: string; count: number }; - addition?: { id: string; count: number }; + tool: string[]; + material: SmithingIngredient; + addition?: SmithingIngredient; result: string; } @@ -21,26 +27,43 @@ export interface SmithingInput { addition?: ItemStack; } +// { id, count }, where id is a choice like a shaped key's +function read_ingredient(read: RecipeReader, value: unknown, name: string): SmithingIngredient { + const json = value as { id?: unknown; count?: unknown } | undefined; + return { items: read.ingredient(json?.id, `${name} id`), count: read.positive_int(json?.count, `${name} count`) }; +} + +// whether stack is enough of ingredient. no ingredient fits only an empty slot +function fits(ingredient: SmithingIngredient | undefined, stack: ItemStack | undefined): boolean { + if (!ingredient) return !stack; + return !!stack && ingredient.items.includes(stack.id) && stack.count >= ingredient.count; +} + export function register_smithing(ctx: SharedContext) { // two recipes for the same inputs would leave one that can never be made const seen = new Set(); ctx.recipes.register_type(SMITHING, { parse(json, read) { const recipe: SmithingRecipe = { - tool: read.item(json.tool, "tool"), - material: read.stack(json.material, "material"), + tool: read.ingredient(json.tool, "tool"), + material: read_ingredient(read, json.material, "material"), result: read.item(json.result, "result"), }; - if (json.addition !== undefined) recipe.addition = read.stack(json.addition, "addition"); - const inputs = `${recipe.tool} ${recipe.material.id} ${recipe.addition?.id}`; - if (seen.has(inputs)) throw new Error(`there's already one for ${recipe.tool} with ${recipe.material.id}`); - seen.add(inputs); + if (json.addition !== undefined) recipe.addition = read_ingredient(read, json.addition, "addition"); + for (const tool of recipe.tool) { + for (const material of recipe.material.items) { + for (const addition of recipe.addition?.items ?? [undefined]) { + const inputs = `${tool} ${material} ${addition}`; + if (seen.has(inputs)) throw new Error(`there's already one for ${tool} with ${material}`); + seen.add(inputs); + } + } + } return recipe; }, // with enough of the material and the addition matches: (recipe, input) => - recipe.tool === input.tool.id && recipe.material.id === input.material.id && - input.material.count >= recipe.material.count && recipe.addition?.id === input.addition?.id && - (input.addition?.count ?? 0) >= (recipe.addition?.count ?? 0), + recipe.tool.includes(input.tool.id) && fits(recipe.material, input.material) && + fits(recipe.addition, input.addition), }); } diff --git a/mods/recipe_viewer/scripts/categories.ts b/mods/recipe_viewer/scripts/categories.ts index 7eb1aaf..e9474bc 100644 --- a/mods/recipe_viewer/scripts/categories.ts +++ b/mods/recipe_viewer/scripts/categories.ts @@ -5,7 +5,16 @@ // blocks it's made in (jei's recipe catalysts), and where its items go. types no category knows get one from their // recipes' json, see mod_category import type { ClientContext, FluidVariant, TagApi } from "cubi/client"; -import { fluid_outputs, type Ingredient, mod_recipe_inputs, outputs, type Recipe, seconds, stack } from "./recipes.ts"; +import { + choice_stack, + fluid_outputs, + type Ingredient, + mod_recipe_inputs, + outputs, + type Recipe, + seconds, + stack, +} from "./recipes.ts"; // in gui pixels, a slot with its border, like minecraft's export const SLOT = 18; @@ -98,9 +107,9 @@ function base_categories(tags: TagApi): RecipeCategory[] { height: 18, catalysts: ["bworld:smithing_table"], draw(recipe, layout) { - layout.slot({ ids: [recipe.tool], count: 1 }, 0, 0); - layout.slot(stack(recipe.material), SLOT, 0); - layout.slot(recipe.addition && stack(recipe.addition), SLOT * 2, 0); + layout.slot({ ids: tags.expand(recipe.tool), count: 1 }, 0, 0); + layout.slot(choice_stack(recipe.material, tags), SLOT, 0); + layout.slot(recipe.addition && choice_stack(recipe.addition, tags), SLOT * 2, 0); layout.texture("bworld:arrow_full", 61, 1, ARROW, ARROW); layout.slot({ ids: [recipe.result], count: 1 }, 90, 0); }, diff --git a/mods/recipe_viewer/scripts/recipes.ts b/mods/recipe_viewer/scripts/recipes.ts index 29ecc55..39b0efd 100644 --- a/mods/recipe_viewer/scripts/recipes.ts +++ b/mods/recipe_viewer/scripts/recipes.ts @@ -46,7 +46,8 @@ export function fluid_inputs(recipe: Recipe): FluidAmount[] { return recipe.fluid_ingredients ?? []; } -// what a recipe takes. shaped keys, smelting inputs and fuels can be an item or #tag, or a list any of which fits +// what a recipe takes. shaped keys, smelting inputs, fuels and smithing inputs 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": @@ -59,8 +60,8 @@ export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] { case "bworld:fuel": return [{ ids: tags.expand(recipe.item), count: 1 }]; case "bworld:smithing": - return [{ ids: [recipe.tool], count: 1 }, stack(recipe.material)].concat( - recipe.addition ? [stack(recipe.addition)] : [], + return [{ ids: tags.expand(recipe.tool), count: 1 }, choice_stack(recipe.material, tags)].concat( + recipe.addition ? [choice_stack(recipe.addition, tags)] : [], ); default: { const { grid, extras } = mod_recipe_inputs(recipe, tags); @@ -97,6 +98,11 @@ export function stack(value: { id: string; count: number }): Ingredient { return { ids: [value.id], count: value.count }; } +// { id, count } where id is an item, a #tag or a list of them, like smithing's material +export function choice_stack(value: { id: string | string[]; count: number }, tags: TagApi): Ingredient { + return { ids: tags.expand(value.id), count: value.count }; +} + export function seconds(ticks: number): string { const s = ticks / 20; return `${Number.isInteger(s) ? s : s.toFixed(1)} s`; diff --git a/tests/helpers.ts b/tests/helpers.ts index b827ed9..64a0d4e 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -302,6 +302,25 @@ export async function load_as_player(sources: string) { } } +// the base game, and a mod after it with these recipes +export function recipes_mod(recipes: Record) { + const dir = Deno.makeTempDirSync({ prefix: "bworld_recipes_" }); + copy_dir("mods/bworld", `${dir}/bworld`); + const write = (path: string, json: unknown) => { + Deno.mkdirSync(`${dir}/kiln/${path.split("/").slice(0, -1).join("/")}`, { recursive: true }); + Deno.writeTextFileSync(`${dir}/kiln/${path}`, JSON.stringify(json)); + }; + write("manifest.json", { + format_version: 1, + id: "kiln", + name: "Kiln", + version: "1.0.0", + dependencies: [{ id: "bworld", version: "*" }], + }); + for (const [name, recipe] of Object.entries(recipes)) write(`recipes/${name}.json`, { format_version: 1, recipe }); + return dir; +} + export function copy_dir(from: string, to: string) { if (Deno.statSync(from).isDirectory) { Deno.mkdirSync(to, { recursive: true }); diff --git a/tests/smelting_test.ts b/tests/smelting_test.ts index 4acabe7..63331d3 100644 --- a/tests/smelting_test.ts +++ b/tests/smelting_test.ts @@ -4,29 +4,10 @@ // the base game's bworld:smelting and bworld:fuel recipes, as another mod adding to them sees them import { assertEquals, assertRejects } from "@std/assert"; import { recipe_lookup } from "@/common/mod_loader.ts"; -import { copy_dir, test_game } from "./helpers.ts"; - -// the base game, and a mod after it with these recipes -function kiln_mod(recipes: Record) { - const dir = Deno.makeTempDirSync({ prefix: "bworld_smelting_" }); - copy_dir("mods/bworld", `${dir}/bworld`); - const write = (path: string, json: unknown) => { - Deno.mkdirSync(`${dir}/kiln/${path.split("/").slice(0, -1).join("/")}`, { recursive: true }); - Deno.writeTextFileSync(`${dir}/kiln/${path}`, JSON.stringify(json)); - }; - write("manifest.json", { - format_version: 1, - id: "kiln", - name: "Kiln", - version: "1.0.0", - dependencies: [{ id: "bworld", version: "*" }], - }); - for (const [name, recipe] of Object.entries(recipes)) write(`recipes/${name}.json`, { format_version: 1, recipe }); - return dir; -} +import { recipes_mod, test_game } from "./helpers.ts"; Deno.test("a later fuel recipe for an item changes how long it burns, and leaves the rest of the earlier one", async () => { - const dir = kiln_mod({ coal: { type: "bworld:fuel", item: ["bworld:coal", "bworld:oak_log"], burn_time: 50 } }); + const dir = recipes_mod({ coal: { type: "bworld:fuel", item: ["bworld:coal", "bworld:oak_log"], burn_time: 50 } }); const { game } = await test_game(dir); Deno.removeSync(dir, { recursive: true }); const recipes = recipe_lookup(() => game.recipes); @@ -37,7 +18,7 @@ Deno.test("a later fuel recipe for an item changes how long it burns, and leaves }); Deno.test("two smelting recipes for one item stop the mod loading", async () => { - const dir = kiln_mod({ + const dir = recipes_mod({ iron: { type: "bworld:smelting", input: "bworld:iron_ore", diff --git a/tests/smithing_test.ts b/tests/smithing_test.ts new file mode 100644 index 0000000..42c3488 --- /dev/null +++ b/tests/smithing_test.ts @@ -0,0 +1,43 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// the base game's bworld:smithing recipes, as another mod adding to them sees them +import { assertEquals, assertRejects } from "@std/assert"; +import { recipe_lookup } from "@/common/mod_loader.ts"; +import { recipes_mod, test_game } from "./helpers.ts"; + +const PLANKS_PICKAXE = { + type: "bworld:smithing", + tool: "bworld:wood_pickaxe", + material: { id: "#bworld:planks", count: 3 }, + result: "bworld:stick", +}; + +Deno.test("a smithing material can be a tag, and any of its items fits with enough of it", async () => { + const dir = recipes_mod({ planks_pickaxe: PLANKS_PICKAXE }); + const { game } = await test_game(dir); + Deno.removeSync(dir, { recursive: true }); + const recipes = recipe_lookup(() => game.recipes); + const result = (material: string, count: number) => + recipes.find<{ result: string }>("bworld:smithing", { + tool: { id: "bworld:wood_pickaxe", count: 1 }, + material: { id: material, count }, + })?.result; + assertEquals(result("bworld:oak_planks", 3), "bworld:stick"); + assertEquals(result("bworld:birch_planks", 5), "bworld:stick"); + assertEquals(result("bworld:birch_planks", 2), undefined); + assertEquals(result("bworld:oak_log", 3), undefined); +}); + +Deno.test("two smithing recipes that fit the same inputs stop the mod loading", async () => { + const dir = recipes_mod({ + planks_pickaxe: PLANKS_PICKAXE, + oak_pickaxe: { ...PLANKS_PICKAXE, material: { id: "bworld:oak_planks", count: 1 } }, + }); + await assertRejects( + () => test_game(dir), + Error, + "there's already one for bworld:wood_pickaxe with bworld:oak_planks", + ); + Deno.removeSync(dir, { recursive: true }); +});