Units: Stacking
Stacking units is a relatively abstract mechanic that is unique to turn-based strategy games. It abstracts large formations of units by representing them as a single unit along with a count of the units in the stack.
Heroes of Might and Magic is the best example I can think of. The screenshot below shows an army composed of four stacks of units:
This screenshot shows a single stack in detail:
An attack by a stack is resolved by running the normal combat calculation for the attack and then multiplying its result by the number of units in the stack. So using the unit above as an example, one would roll a random number between 5 and 8, then multiply that by 6 to get the total damage. That total damage would be further multiplied as normal based on attack and defense values to get the final damage.
Damage is applied as normal, but if enough hit points are applied to kill a unit in the target stack, its size is reduced by 1 and then the health is reset to maximum. The algorithm continues applying damage until all of it has been applied or the target stack’s size is reduced to zero.
Note that this is different from the stacking mechanic used in Civilization III & IV, in which each unit in the attacking stack individually resolves an attack against the healthiest unit in the target stuck until units had attacked. These were often called Stacks of Doom due to how deadly they were, but they were also deeply flawed leading to battles that took hours if you didn’t have the combat animations turned off.
ECS Design
To apply this mechanic to your game you will need to make your regular combat and health systems stack aware. I don’t want to duplicate the code for those already complex systems here, so I’ll just show the component that they will need to factor into their normal calculations.
Components
Stack
- size: Integer
The size is the number of units in the stack.
Entities
Each unit would have Stack in addition to its normal combat or health components.
Systems
CombatSystem
The combat system should be modified to multiply the damage generated by the number of units in the stack.
HealthSystem
The health system should apply damage to the hit points until it reaches zero, then reduce the stack size by one and reset health back to maximum. Repeat until all damage has been absorbed.
DeathSystem
The death system would be changed to only eliminate a unit when the stack size reaches 0.