Shops and the shop window
A shop is a building that sells. A hero near it buys; the client's shop window searches, filters and shows the build tree; a purchase can be taken back for a few seconds.
Quick start
{
id: 'alchemist',
name: 'Alchemist',
kind: 'building',
radius: 1.6,
footprint: { w: 3, d: 3 },
stats: { health: 1500, armor: 40 },
invulnerable: true,
sight: 8,
sells: ['healingDraught', 'manaDraught', 'sightWard', 'returnScroll']
}Place it for the neutral player to sell to everyone; a shop of a side's sells to that side and its allies. A hero within SHOP_RANGE of it may trade.
Common tasks
Sell the default set
import { defaultItems, itemTier } from '@v12-engine/engine'
const catalogue = defaultItems.filter((item) => itemTier(item) !== 'relic').map((item) => item.id)
// { id: 'armory', sells: catalogue }A shop that lists more of a set than the game took sells what it took: useDefaults takes the rest off (The default set).
Limit stock
stock: { max: 2, restock: 120 } on the item: the shop has two, and gets one back every 120 seconds. Everyone who sees the shop sees how many are left and how far the next one is. A buy with none left is refused (outOfStock).
Price a recipe by what is carried
The buyer pays the item's cost less the parts it carries, which are used up (priceOf). The shop window lights what the hero can afford counting its parts, and shows each ware's whole build tree.
Refuse a buy, and say why
The engine refuses before anything changes, and tells the player: no room (inventoryFull), one more of a group than allowed (groupLimit), too poor, out of stock. The window says so before the buy is tried. Your own rule: settings.items.mayCarry (a reason, or nothing to let it), or the beforeBuy hook (change the cost, or cancel with a reason).
Undo a purchase
settings: { shop: { undo: 10 } } // seconds; 0 for no undoingWithin undo seconds of a buy, while the buyer stands at the shop still and has not used the item, Undo in the window (or undoBuy(world, hero), the undoBuy command) gives all it cost back, takes the item (out of its stack, where it stacked), brings back the parts it used up as they were, and the shop its stock. Purchases are kept on the buyer (purchases), each sent to its player with the seconds left. undoRefusal names why one cannot be undone (nothingToUndo, inventoryFull); itemUndone says it was.
Sell an item
Right-click an item onto the shop, or the sell command: the game's economy.sellPrice share of its price comes back (0.7 when left out), or the item's sellRate. A hero needs a shop it may trade with near (shopNear).
Buy from code
world.issue({ type: 'buy', player, shop: alchemist.id, item: 'healingDraught', hero: knight.id })
buy(world, alchemist, 'healingDraught', player, knight) // the same, from the rulesshopper(world, shop, player, item?) finds who at the shop would buy: the hero named, or the player's hero near.
Hire units at a tavern
hires: ['boat', 'ranger'] on a building: whoever has a unit there can hire; a neutral one hires to everyone. The melee starter's tavern hires any of the eight heroes (Heroes).
The shop window
The client's shop (heroesViewPack, the shop HUD part) opens with the shop key (input.keys.shop, P) or a click on a shop near a hero. In it:
- Search: names, ids, stats, tags, descriptions, a typo too (
searchItems); the letters that matched are lit;#tagis the tag alone. Typing while the window is open goes into the search; Escape empties it, then closes. - Filters: chips of the tags marked
filterand of the game's callings, and For this hero. - For this hero: a row of what suits the hero's callings, the best suited and the dearest first.
- Each ware: its tags and the callings it suits in the tooltip; lit by what the hero can afford with the parts it carries taken off.
- The build tree: the picked item's parts, the ones carried lit, and what it builds into, level by level, clicked through.
- Refusals: why a buy would be refused, before it is tried.
- Undo in the footer, while the engine allows.
hud: { shop: { recommend: true, filters: true, typeToSearch: true, recommended: 8 } } // ShopWindowOptions, the defaultsrecommend: false takes the suggestions away (a game without callings), filters: false the chips, typeToSearch: false leaves the letters to the game's keys. The window keeps clear of the console either way it is laid out (HUD layout). hud.shopTiers names and orders the tiers it shows.
What the window is told is ShopInfo: the hero's callings, the filters, each item's tags, suits, affordability, parts carried and how well it suits, and the latest purchase to undo. A HUD part of your own reads it the same way (Add a HUD part).
Reference
Shop rules (ShopSettings, settings.shop)
| Setting | Default | What it does |
|---|---|---|
undo | 10 | Seconds within which a purchase is undone whole; 0 for no undoing |
Buildings
| Field | Makes it |
|---|---|
sells | A shop |
hires | A tavern or harbour that hires out units |
Commands, functions, events
Commands of the heroes pack: buy, sell, undoBuy, hire (withoutCommands drops one). Functions: buy, sell, undoBuy, undoRefusal, undoable, shopNear, shopper, priceOf, atShop. The shopStock component holds a shop's stock; purchases a buyer's undoable purchases. Events: itemUndone, inventoryChanged. Hooks: beforeBuy.
Tests: packages/engine/tests/items.test.ts, packages/engine/tests/shopUndo.test.ts, packages/client/tests/shopWindow.dom.test.tsx, packages/client/tests/shopDescribe.test.ts.