Skip to content

Items ​

An item is data: what it costs, what it gives whoever carries it, what it does when used, and what it is built from. Items are Rules.items; heroes carry them (inventory on the unit type gives the slots), shops sell them (sells on a building), what dies drops them (drops). The engine brings a whole tree of fifty you can take as it is, change, or leave: The default set. Everything here is a field with a default, a setting, or a hook where a game wants its own rule.

Quick start ​

ts
import { heroesPack, type Rules } from '@v12-engine/engine'

export const rules: Rules = {
	id: 'my-game',
	name: 'My game',
	units: [
		{
			id: 'knight',
			name: 'Knight',
			kind: 'unit',
			radius: 0.5,
			sight: 10,
			stats: { health: 600, attackDamage: 30, attackSpeed: 1, attackRange: 0.6, moveSpeed: 4 },
			inventory: 6,
			hero: { growth: { stats: { health: 50 } }, learns: [] }
		},
		{ id: 'trader', name: 'Trader', kind: 'building', radius: 1.5, sight: 6, stats: { health: 900 }, invulnerable: true, sells: ['ironSword', 'brutalAxe'] }
	],
	items: [
		{ id: 'ironSword', name: 'Iron Sword', tier: 'basic', cost: { gold: 350 }, bonus: { stats: { attackDamage: 15 } }, tags: ['attack'] },
		{
			id: 'brutalAxe',
			name: 'Brutal Axe',
			description: '+40 attack damage',
			tier: 'intermediate',
			cost: { gold: 1100 },
			from: ['ironSword', 'ironSword'],
			bonus: { stats: { attackDamage: 40 } },
			tags: ['attack']
		}
	],
	resources: [{ id: 'gold', name: 'Gold', start: 500 }],
	currency: 'gold',
	packs: [heroesPack()]
}

The knight walks up to the trader and buys an Iron Sword for 350. A Brutal Axe is two swords and 400 more: carrying one sword, the knight pays 750, and the sword becomes part of the axe.

Common tasks ​

Make an item ​

ts
{ id: 'stormblade', name: 'Stormblade', tier: 'basic', cost: { gold: 400 }, bonus: { stats: { attackDamage: 15 } }, ability: 'stormStrike' }
  • bonus: stats while carried (stats, flat; percent, shares).
  • ability: cast when the item is used, for no mana. Its hotkey is the item's.
  • passive: an ability whose passive part works while carried: triggers, auras (Triggers and passives).
  • description: the tooltip. The shop shows it with the tags and what the item builds into.

Build one item from others ​

from lists the parts. The price is for the whole: parts the buyer carries are used up and taken off, and so are the parts of parts it carries (priceOf). A recipe that costs less than its parts together is flagged by the rules' check (building it would pay the buyer).

ts
{ id: 'kingsedge', tier: 'legendary', cost: { gold: 3200 }, from: ['recurveBow', 'ironSword', 'keenGlass'] }

Two of the same part: from: ['ironSword', 'ironSword']. A loop (an item among its own parts, however far down) is refused by the check.

Give items tiers ​

tier is where an item sits in a shop and in the tree:

TierWhat it is
consumableUsed up: potions, wards, scrolls (Consumables)
starterWhat a hero buys first
basicThe parts: one thing each
intermediateBuilt from the basics, with the first passives. epic, the old word, is still read as this (itemTier)
legendaryWhat the tree leads to: a unique effect each (Legendaries)
relicLoot of camps, bosses and quests: never sold

Any other word is a tier of the game's own: the shop and the item tree list it after these. hud.shopTiers on the view names and orders what the shop shows.

Sell it in a shop ​

sells: ['ironSword'] on a building makes it a shop; a hero standing near buys (Shops). stock: { max: 2, restock: 60 } limits how many it has.

Drop it as loot ​

ts
{ id: 'ogre', drops: [{ item: 'emberIdol', chance: 0.1 }, { item: 'healingDraught', chance: 0.4 }] }
// or drops: (world, entity, killer) => ['emberIdol']

Relics are made for this: a passive no shop sells, found at a camp or a boss. GameMap.itemTables draws random drops by an item's level and class.

Limit how many of a kind a hero carries ​

group: 'boots' with groupLimit: 1 (or the game's settings.items.groups): one pair, whatever the boots. Buying, picking up or being handed one more is refused and the player told why (Groups).

Give an item in code ​

ts
import { createItem, giveItem, carryRefusal, hasRoomFor } from '@v12-engine/engine'

if (hasRoomFor(world, hero, 'ironSword') && !carryRefusal(world, hero, 'ironSword')) giveItem(world, hero, createItem(world, 'ironSword'))

createItem(world, id, charges?) makes an instance; giveItem puts it in a free slot or a stack of it; takeItem takes one out; pickUp and dropOnGround move it between the ground and a carrier; sell sells it at a shop near. carryRefusal is the engine's rule of what may be carried (a group's limit, then the game's own mayCarry), with the reason as a message.

Make items fit every kind of game ​

An item's words and rules name general things: a hero, a creep, a structure, an ally, a kill. Never a lane, a minion or a tower. Where a game has those, they match through its units' own tags (a minion is a unit tagged minion), so the same item works in a melee game, a campaign and a lanes map. The creeps of a camp belong to the creeps player, everyone's enemy: an effect with affects: 'enemies' reaches them (Creeps).

Check the tree ​

The rules' check (checkRules, the editor's Problems) names, for each item:

  • a part that is no item, an ability or a passive that is none;
  • a loop in the build tree;
  • a price under the sum of its parts;
  • an item nothing sells, builds or drops (in a game that sells or drops any);
  • a groupLimit under 1 or without a group; a stack or a refill on an item with no charges; a sellRate under 0; a unique with no passive;
  • a recipe that breaks its own group limit.

Editor ​

The Data panel edits items as any definition, and the Item tree draws the tree by tier with prices and problems, drag to add a part, search and filter by tag or calling: Data panel, tech tree and item tree. The Tags list names tags and gives them a look; Default sets chooses what the game takes of the engine's items.

Reference ​

ItemDef ​

FieldWhat it does
id, nameIts id (give it a prefix of the game's) and what the player sees
descriptionThe tooltip
costWhat it costs in a shop, by resource
tierconsumable, starter, basic, intermediate (epic read as it), legendary, relic, or a word of the game's
fromThe items it is made of; those carried are used up and taken off the price
bonusStats while carried (stats, percent)
abilityCast when used, for no mana
passiveAn ability whose passive part works while carried
uniqueThe name of its passive: two items sharing it work once (Legendaries)
group, groupLimitAt most so many of the group per carrier (boots: 1)
chargesUses before it is gone (Consumables)
stackHow many of it go in one slot, their charges added
refillHow its charges come back: { shop: true }, { every: 60 }
sellRateThe part of its price a shop gives back, over the game's economy.sellPrice
stockHow many a shop has (max) and the seconds to get one back (restock)
tagsFree words for what it is: searched, filtered, named by a TagDef (Tags)
suitsThe callings it is meant for: the shop's suggestions and filter, never a lock
hotkeyThe key that uses it from the command card
powerUpUsed as it is picked up, never carried (a rune, a tome; the runes pack)
level, classFor random drop tables (GameMap.itemTables)
basedOnThe definition this is a copy of, for its look

Item rules (ItemSettings, settings.items) ​

SettingDefaultWhat it does
groups{ boots: 1 }The most of a group one carrier may have, by group; an item's own groupLimit comes first
uniquetrueWhether passives sharing a unique name work once for all of them
mayCarryThe game's own rule, asked after the engine's: a reason to refuse, or nothing

Functions ​

FunctionWhat it does
priceOf(registry, item, carried)The price for someone carrying these, and the parts it uses up
itemTier(def)The tier, epic read as intermediate
itemsTagged(registry, tag)The items with a tag
carryRefusal(world, carrier, item, uses?)Why it may not be carried, or nothing
groupRefusal(registry, carried, item, uses?)The group's part of it, from the registry alone (for a screen)
groupLimitOf(world, def)The most of its group a carrier may have
hasRoomFor(world, entity, item)A free slot, or a stack with room
createItem, giveItem, takeItem, findItemMake and move instances
pickUp, dropOnGroundThe ground
buy, sell, shopNear, undoBuyShops (Shops)

Hooks and events ​

beforeBuy (cost, or cancel with a reason), beforePickUp and beforeDrop (cancelled with a reason). Events: inventoryChanged, itemPicked, itemUndone. Messages the engine tells the player: inventoryFull, groupLimit, theirGroupLimit, outOfStock, noCharges, nothingToUndo.

Tests ​

packages/engine/tests/items.test.ts (buying, recipes, selling), packages/engine/tests/itemRules.test.ts (groups, stacks, refills, uniques, the checks), packages/engine/tests/defaultItems.test.ts (the default tree, each legendary in a melee, a campaign and a lanes game).