A typical RPG/roguelike has equipment, cards, skills etc that can all provide bonuses or penalties in various statistics, primary or derived. For example, movement speed, attack speed, maximum health, etc. There are possibly lots of ways to implement them. After a few unsuccessful theoretically-efficient approaches, the current revision looks reasonable.
Temporary, permanent, recurring and conditional effects
Examples that should be possible include:
- Potion of healing: one-off, adjust current health [permanent]
- Potion of regeneration: every N seconds, adjust current health. Stop after N*K seconds [permanent, recurring]
- Potion of speed: movement speed increased by 20%, for 1 minute [temporary]
- Potion of remove blindness: one-off, sets blindness to false ONLY if the creature is not naturally blind [permanent, conditional]
- Sword of orc slaying: +50% weapon damage (applied when we use an ability that uses the equipped weapon), only if we're attacking orcs [temporary, conditional]
- Boots of the desert: +50% movement speed when crossing the desert biome [conditional]
So, observing such use-cases, I made the following design choices:
- All types of effects/modifiers can have conditions
- Permanent effects write directly to the actual values the affect. A health potion updates the actual health value. If they have conditions, they check them once, before they apply the permanent effect.
- Temporary effects are stored in a separate list. Every time we want to get a particular value (weapon damage, max health, etc), we need to run a system function that gets the base value and applies all effects (if any of them has conditions, it's applied if the condition is satisfied)
- Events are set up for recurring permanent effects
- Events are set up for temporary effects, to remove them from the list when the effect expires.
- Temporary effects can be applied when an item is being carried (figurine), equipped (sword) or used (potion).
A drawback of the separate list for temporary effects is that we have to maintain indirect access to all variables that could be modified by effects, so that the access function always takes into account any effects. Additionally, we can't even cache effects as they can be conditional. So for example, we have functions like "EvaluateMaxHealth", "EvaluateMovementSpeed" etc, that get the base maximum health, then look for any effects that target max health (and pass the conditions, if any) and generate the final value.
In the temporary effects list we have 3 types of effects, based on what they modify:
- Numerical. +1 Skill, +50 health, +15% attack speed.
- Boolean. Set paralyzed or not, deaf or not, able to fly or not, etc.
- Option. Set field of vision algorithm. Probably more to come.
Overall the system should be very flexible and allow for weird effects expressed naturally by data driving, e.g. a potion of lycanthropy that only works in a full moon, over the dead body of a wolf (a date condition and a condition that there's an item pile that contains a wolf corpse under the feet of the player). The only important rule to remember when developing effects is that any values that can be affected by temporary effects need to accessed by these evaluator functions, unless we do not want to take modifiers into account.
Here is a video that shows a few potions in action: healing, emergency healing (only when health is < 20%), alacrity (player moves at incredible speed), X-ray vision (see through walls), oracle (see entire map):
Here's another video that shows the death particle system adjusted to go towards the direction of the hit: