Hello. I’ve been thinking about a way to handle stats like strength, agility, intelligence, move speed, e.t.c. in my game, and I’m not sure how to make it flexible enough. As an example of what I want to achieve, take the stat system of League of Legends or the Warcraft 3 heroes, or even WoW.
My current idea is reading in a stat file (XML?) for each unit type or character which contain a list of all the stats that kind of unit/character has. This would contain the stat name, maybe an ingame description, e.t.c. My idea here is to have either a simple base value for the stat (a single double variable), or a mathematical function parsed with Exp4J so that the base value can depend on other stats.
A WoW example:
Strength = 10
AttackPower = 5 + Strength * 2
Some stats will be required (HP, Mana, move speed, attack damage, attack speed, e.t.c.) and will be read semi-hardcoded by the unit manager.
To achieve the flexibility of buffs, debuffs, equipment and stat modifiers in general I will need a very powerful system for this. My idea is that each stat has a list of modifiers. These modifiers are either flat bonuses (+5 to strength) or percentage bonuses (+20% to strength). Their actual value will probably be a Exp4J expression, so the modifiers can depend on other stats, even from other units (for example: the slowing effect of a slow debuff is dependent on the caster’s intelligence).
Stacking is controllable by using stack groups IDs, e.g. if you have two modifiers of the same stack group only the most powerful will apply.
I also plan to keep a way to add on-hit and on-cast effects like in League of Legends, which means that buffs, upgrades, etc can add the ability to apply debuffs like slow and poison on attacks, grant new abilities, e.t.c.
With these things I would be able to do exactly what I want. However, there is one problem. Infinite loops with rare buff combinations. I have a good example of this in League of Legends. In LoL there is a champion (hero) called Jax who has a unique passive ability that grants him more max HP from each point of bonus damage and ability power he gets from equipment and buffs. You can also buy an item called Atma’s Impaler that grants bonus damage depending on your max HP. Ouch.
To calculate damage:
Damage = 10 + HP*0.02
HP = 100 + Damage*3
Damage = 10 + (100 + Damage*3)*0.02
Damage = 10 + (100 + (10 + HP*0.02)*3*0.02
Damage = 10 + (100 + (10 + (100 + Damage*3)*0.02)*3*0.02
...
StackOverflowException!
I need some way of detecting and preventing an overflow, while still making it possible to have these buffs at the same time. These calculations might be done pretty often too, so they have to be pretty fast. Of course I will cache the stats, as they only change when their modifiers change.
Has anyone done this before? I mean, somebody has to have made an RPG before or another kind of game that has stats and modifiers. How did/would you implement it?