mwg API
    Preparing search index...

    Function craft

    • Resolves a recipe against an inventory: every ingredient checked, consumed, and the result added, all in one call - the same small-focused-function shape skillCheck already is, not a crafting subsystem of its own. Inventory only ever stacks, weighs and adds/removes what a game already hands it; nothing there turns one stack of items into a different one, which is the entire gap this closes.

      An ingredient may name an exact id (or any of several), a category ("any herb", matched against the item definition's own category), or a matches predicate, and a quantity is taken from as many matching stacks as it spans.

      All or nothing: if any ingredient is missing or too few, nothing is touched. The allocation runs against a working copy of the stacks first, so two ingredients that could both take the same stack cannot each count it. If every ingredient is present but the result would not fit (Inventory's own capacity, unrelated to crafting itself), the ingredients are put back exactly as they were rather than being spent for nothing.

      Parameters

      Returns boolean

      whether the recipe resolved

      import { craft, Inventory, type Recipe } from '@datamoc/mw_games/actors';

      const bag = new Inventory();
      bag.add({ id: 'sage', quantity: 2, stackable: true, category: 'herb' });
      bag.add({ id: 'ember', quantity: 1, stackable: true, category: 'runestone' });

      const recipe: Recipe = {
      ingredients: [
      { category: 'herb', quantity: 2 },
      { category: 'runestone', quantity: 1 },
      ],
      result: { id: 'elixir', quantity: 1 },
      };

      const made = craft(bag, recipe); // true - any herb plus any runestone becomes an elixir