Items are a major part of roguelike games. For TCOD, I followed Sean Middleditch idea on dungeon dweller : I use a single structure to store all items. Thus, there is no specific code for weapons, armors, lights, potions. Only generic code for generic items. The magic is in the item type definition...
An item type has some standard properties (name, cost, weight, ...) and a list of features. A feature is a characteristic of the item that enable some game mechanism on this item. Now two items of the same type have exactly the same characteristics in TCOD. They are exact clones. Thus, a sword and a +2 sword are two items of different types. Since we don't want to have to define hundreds of item types, they are organised in a tree where every type inherit the characterics of its father. Near the tree root, there are some general types (weapon, armor, container, ingredient, ...). Each type has subtypes that are more and more specific (weapon -> one handed -> sword -> short sword ...)
The whole list of item types is in a plain text file (items.dat) loaded at game start. Features are the only part that is hardcoded. There are lots of features and more will be added later. Each feature control some process that can occur in the game engine. Here are some examples of features :
- attack : using an item with this feature to attack a creature gives you attack bonus (mainly weapons).
- defense : using an item with this feature gives you a defense bonus (mainly armors and shields).
- light : the item produce light (mainly torchs and lantern but why not create luminescent sword blade).
- wear : you have to wear the item on a specific 'body slot' to use it.
- oneshot : using the item destroys it (scrolls, potion, ...)
- onoff : can be enabled/disabled (lantern)
- prereq : the item needs another item to be used (bullets need a pistol)
- catalyser : the item works better with another item (arrows give more damage when thrown with a bow)
- ingredient : can be combined with another item to create a third item (destroying the 2 source items)
- creator : can produce another type of items (pickaxe -> stones, fishing pole -> fishes, ...)
- timeout : the item effects is not infinite
- mapcell : the item only works on certain ground type (water for fishing pole, ...)
Adding a feature to an item type has two consequences :
- a list of variables specific to the feature is added to the item type. These are static or initial properties of the item type.
- another list of variables is added to all items of this type. These are dynamic properties for this item.
Example : for the light feature (item producing light), there are the static properties :
- visiblity range (range at which a map cell is visible due to this item light)
- penumbra range (range at which a map cell is in penumbra due to this item light)
- duration (how much time the light lasts)
- variation (how much the light intensity changes over time=> low for a lantern, high for a torch or a candle)
The dynamic properties are :
- duration (initialized with the item type duration value and decreased over time)
- variation (initialized with the item type variation, but increases when duration is near 0, simulating a torch flickering)
- intensity coef (decreases when duration is near 0 so that a torch light smoothly disappears)