Tags for smithing recipes
This commit is contained in:
@@ -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<string>();
|
||||
ctx.recipes.register_type<SmithingRecipe, SmithingInput>(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),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user