The pack
inventory
function inventory():Pack<{inventory:typeof rules}>
Install renderer-independent inventory policies.
Helpers
createInventory
function createInventory(items: readonly ItemDefinition[], containers: readonly ContainerDefinition[], slots: readonly string[] = [], credits = 0)
Create an inventory from items and containers, with the named equipment slots (default none) and opening
credits (default 0). Throws on duplicate or empty ids or values out of range.
Classes
Inventory
class Inventory
All operations in a transaction succeed together, including equipment swaps and currency deductions.
readonly items: ItemDefinition[]— Copies of the item definitions given at creation.readonly containers: ContainerDefinition[]— Copies of the container definitions given at creation.state: InventoryState— The live state, replaced by each committedtransactand byrestore. Usesnapshotfor a copy.constructor(items: readonly ItemDefinition[], containers: readonly ContainerDefinition[], slots: readonly string[] = [], credits = 0)item(id: string)— The definition of itemid. Throws 'Unknown item ' if there is none.count(item: string, container?: string)— Total units ofitemheld across every container, or only incontainerif given; equipped items are not counted. Throws if the item is unknown.weight(container: string)— Total weight of the stacks incontainer, equipped items excluded; 0 for an unknown container.transact(operations: readonly InventoryOperation[], preview = false): { /** Whether every operation was valid and the result fits. */ ok: boolean; /** Why the transaction failed, such as 'Not enough items'; the empty string when \ok`. / reason: string; /* * A copy of the resulting state when `ok` (the proposed state when previewing), otherwise of the unchanged * state. */ state: InventoryState; }— Applyoperationsin order to a copy of the state, check capacity, weight and equipment, and commit all of them or none. Withpreviewtrue nothing is committed. Rule failures do not throw: the result hasokfalse, thereason` and the unchanged state.buy(item: string, container: string, count = 1)— Buycountunits (default 1) ofitemintocontainer: deducts price times count in credits and adds the items in one transaction, returning thetransactresult. Too few credits, no room or too much weight giveokfalse. Throws for an unknown item or a count that is not a whole number of 1 or more.snapshot()— An owned deep copy of the state, for saving.restore(state: InventoryState)— Replace the state with a copy ofstateafter checking it against the definitions. Throws on a wrong version, mismatched containers or equipment slots, too many stacks, an overweight container, a bad count or incompatible equipment.
Types
ContainerDefinition
interface ContainerDefinition
A bag, chest or shop shelf: a limited number of stacks and a weight limit.
id: string— Unique non-empty container id.capacity: number— Most stacks the container holds (its slots), a whole number of 0 or more.maxWeight: number— Most total weight its stacks may reach; finite, 0 or more. Equipped items do not count.
InventoryOperation
type InventoryOperation = { /** 'add' puts the items into the container; 'remove' takes them out. */ kind: 'add' | 'remove'; /** Id of the container to add to or remove from. */ container: string; /** Id of the item to add or remove. */ item: string; /** Units to add or remove, a whole number of 1 or more. */ count: number; } | { /** Moves items from one container to another. */ kind: 'transfer'; /** Id of the container the items leave; fails if it holds too few. */ from: string; /** Id of the container the items enter; must differ from `from`. */ to: string; /** Id of the item to move. */ item: string; /** Units to move, a whole number of 1 or more. */ count: number; } | { /** Equips an item taken from a container; any item already in the slot returns to the same container. */ kind: 'equip'; /** Id of the container the item comes from. */ container: string; /** Id of the item to equip; its definition must list `slot`. */ item: string; /** Id of the equipment slot to fill. */ slot: string; } | { /** Empties an equipment slot back into a container. */ kind: 'unequip'; /** Id of the container the item returns to; fails if it does not fit. */ container: string; /** Id of the equipment slot to empty; fails if it is already empty. */ slot: string; } | { /** Adds to or subtracts from the credit balance. */ kind: 'credits'; /** Whole credits to add, or to take away when negative; the balance may not drop below 0. */ amount: number; }
One step of a transaction: add or remove items, move them between containers, equip or unequip, or change credits.
InventoryState
interface InventoryState
The part of an inventory that changes: credits, container contents and equipment. snapshot and restore carry it.
version: 1— Snapshot format version, always 1;restorerejects any other.credits: number— Currency held, a whole number from 0 up to 4294967295.containers: Record<string, ItemStack[]>— Stacks held in each container, by container id, in the order they were allocated.equipment: Record<string, string | null>— Id of the item equipped in each slot, by slot id, or null when the slot is empty. Equipped items are outside container capacity and weight.
ItemDefinition
interface ItemDefinition
A kind of item: how it stacks, weighs, equips and sells.
id: string— Unique non-empty id that stacks, operations and equipment use to refer to the item.name: string— Display name; the rules never read it.maxStack: number— Most units in one stack, a whole number of 1 or more; a larger count fills further stacks, each using one container slot.weight: number— Weight of one unit in the game's own weight units; finite, 0 or more. Totals are compared withContainerDefinition.maxWeight.slots: string[]— Ids of the equipment slots the item can be equipped into; empty if it cannot be equipped.price: number— Creditsbuycharges for one unit, a whole number of 0 or more.tags: string[]— Free labels such as 'weapon' for game code to filter on; the rules never read them.
ItemStack
interface ItemStack
A run of identical items occupying one container slot.
item: string— Id of the item's definition.count: number— Units in the stack, from 1 to the item'smaxStack.


