63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// smelting and fuel recipes: the furnace cooks one item into another, burning fuel to do it. both are found by an item
|
|
// id, and registered by the shared script like smithing.ts
|
|
import type { SharedContext } from "cubi/shared";
|
|
|
|
export const SMELTING = "bworld:smelting";
|
|
export const FUEL = "bworld:fuel";
|
|
|
|
export interface SmeltingRecipe {
|
|
// the items it smelts
|
|
input: string[];
|
|
output: { id: string; count: number };
|
|
// ticks
|
|
cook_time: number;
|
|
}
|
|
|
|
export interface FuelRecipe {
|
|
items: string[];
|
|
// ticks
|
|
burn_time: number;
|
|
}
|
|
|
|
export function register_smelting(ctx: SharedContext) {
|
|
// two recipes for one item would leave one that can never be made
|
|
const smelted = new Set<string>();
|
|
ctx.recipes.register_type<SmeltingRecipe, string>(SMELTING, {
|
|
parse(json, read) {
|
|
const recipe = {
|
|
input: read.ingredient(json.input, "input"),
|
|
output: read.stack(json.output, "output"),
|
|
cook_time: read.positive_int(json.cook_time, "cook_time"),
|
|
};
|
|
for (const id of recipe.input) {
|
|
if (smelted.has(id)) throw new Error(`there's already one for ${id}`);
|
|
smelted.add(id);
|
|
}
|
|
return recipe;
|
|
},
|
|
matches: (recipe, id) => recipe.input.includes(id),
|
|
});
|
|
|
|
// a later recipe for an item takes it out of the earlier one's items, so a mod can change how long something
|
|
// burns and find gives its recipe
|
|
const burning = new Map<string, FuelRecipe>();
|
|
ctx.recipes.register_type<FuelRecipe, string>(FUEL, {
|
|
parse(json, read) {
|
|
const recipe = {
|
|
items: read.ingredient(json.item, "item"),
|
|
burn_time: read.positive_int(json.burn_time, "burn_time"),
|
|
};
|
|
for (const id of recipe.items) {
|
|
const earlier = burning.get(id);
|
|
if (earlier) earlier.items = earlier.items.filter((other) => other !== id);
|
|
burning.set(id, recipe);
|
|
}
|
|
return recipe;
|
|
},
|
|
matches: (recipe, id) => recipe.items.includes(id),
|
|
});
|
|
}
|