Thursday, March 23, 2006

Trading system

The trading system is used when you buy or sell items to a non player character (npc). This first version is really basic.
It consists on a two-column panel with the npc inventory in one column and your inventory in the other column.
Each item in your inventory has a hotkey that allows you to select/deselect it for sale.
Each item in the npc inventory has a hotkey that allows you to select/deselect it for buying.
The panel also shows you how much gold you will have after the transaction. You cannot select an item for buying if you don't have enough gold.

Now the really interresting part is the random generation of the merchant inventory...

Consistency over time, but not constancy

If we generate randomly the inventory each time the player speaks to the merchant, the player will close and reopen the trade panel until he find the item he wants. If the inventory is always the same, there is no use getting back to the merchant.
If this is the first time we visit this npc, we generate a complete random inventory (in fact not really random. see what follows...).
If the player has already talked to this npc, we calculate how much time has passed since last trade and we simulate 1 transaction (1 item sell or bought) for every 10 game minutes.
Thus, if the player come back quickly to the npc, the inventory won't change.
If he came back later (after a few hours or a few days of game time), he will find new items.

Items scarcity

We don't want item types to be randomly present in every shop of the game. To add more diversity to the shops, we use several technics :
- item scarcity
Each item type has a scarcity parameter between 0 and 255. When we look for a random item_type, a random number between 0 and 255 must be greater than the item_type scarcity for that type to be selected. Thus, there will be more daggers and short swords than +3 enchanted elvish double sword.
- shop specialisation
each shop sell and buy only a given type of items. Since item types form a tree, this can be a really general type (weapons) or a very specific one (one handed axes).
(The next features are not currently implemented in TCOD. They may be added at any time.)
- shop quality
Low quality shop will have lots of common items, most of them already worn (there is a durability indicator on most items). High quality shops will have rare objects, most of them brand new.

Items prices

The price of each item type if defined in the items.dat file.
But having every items of the same type at the same price is not fun. Here are some factors that act upon the price :
- durability of the item : a worn item will be cheaper than an brand new item of the same type
- charisma : during inventory generation, each item's price (yours and the merchant's) is negociated using a charisma(CHA) check. If your character has a high CHA, he will buy at lower price and sell at higher. The merchant also has a CHA. Thus, some shops will be cheaper than others if their merchant has a poor CHA.
- the merchant prefered items : some merchants have a preference for items of a specific type (a rare one). They will buy those items at a much higher price than normal.