# Best way to calculate unit deaths in browser game combat?

My browser game's combat system is written and mechanically functioning well. It's written in PHP and uses a SQL database. I'm happy with the unit balance in relation to one another. I am, however, a little worried about how I'm calculating unit deaths when one player attacks another because the deaths seem to pile up a little fast for my taste.

For this system, a battle doesn't just trigger, calculate winner, and end. Instead, it is allowed to go for several rounds (say one round every 15 mins.) until one side passes a threshold of being too strong for the other player and allows players to send reinforcements between rounds.

Each round, units pair up and attack each other. Essentially what I do is calculate the damage:

AP = Attack Points
HP = Hit Points

Units AP * Quantity * Random Factors * other factors (such as attrition)


I take that and divide by the defending unit's HP to find the number of casualties of defending units. So, for example (simplified to take out some factors), if I have: 500 attackers with 50 AP vs 1000 defenders with 100 HP = 250 deaths. I wonder if that last step could be handled better to reduce the deaths piling up.

Some ideas:

1. I just change all the units with more HP?
2. I make sure to set the Attacking unit's AP to be a max of the defender's HP to make sure they only kill 1 unit. (is that fair if I have less huge units vs many small units?)
3. I spread the damage around more by including the defending unit's quantity more? i.e. in that scenario some are dead and some are 50% damage. (How would I track this every round?). This kind of seems like the best idea, but not sure how i'd track it in the DB.
4. Other better mathematical approaches?

There are several answers in this question which might help: Calculating the output of two armies fighting.

However, one simple answer is just to scale your values. If 1 point of attack power basically negates 1 hit point, and you think that kills too many units, then make 1 point of attack power negate 0.75 hit points (or whatever). eg.

(Attack Power * Quantity of Attackers) * 0.75 / Defending Unit HP = Deaths

These values are yours to do what you want with, so add whatever you need to make the outcome resemble the gameplay you want.

• Ok, I looked into those answers and find them lacking. 1) They either take a deterministic rock, paper, scissor type approach; and 2) the lanchaster's law seems to only care about quantities (larger army winning), which would be fine assuming that all units have the same weapons (stats), but that's not the case here. – MikeCruz13 Jun 3 '12 at 2:35
• The Lanchester laws accurately depict the rate at which one side defeats the other - you have to plug in your own relative power levels. – Kylotan Jun 3 '12 at 9:49

-- It's hard to give you an accurate answer because we don't know how your turns are organized or how works your unit teams work --

Let me make a point about what you have right know.

You have 500 attackers. Each attacker is defined by a number of Attack Power. They fight against defenders. They are 1000 and are defined by a number of Health Points.

It seems logic that 1 attacker can only fight 1 defender at the time (it seems that your game is turn based). For 1:1 combat the basic design would be to say "if AP are bigger than HP then there is as much as death that attackers; every defender dies". But on a real battlefield things are not that simple: people can kill more than one guy with a little luck, or no one with a lack of luck. You will have to add some randomness in your equation in order to basically simulate a real combat. I would suggest to pre-compute the winner of the battle (easy: the biggest, the strongest) and then to make the number of deaths vary a little bit.

For a 100:100 battle with 50AP and 100HP you have to wait 2 turns to kill your defenders with your equation, but if you apply something like:

X * ((Attack Power * Attackers) / (Health Point * Defenders))


With X a random value between 0.75 and 1 (up to 1.2 if the pre-computed winning team is more numerous than the loser one, in order to simulate the 'chance' to kill a little bit more than only with 1:1 ratio), you will obtain unpredictable results (from a player point of view).

Anyway you not necessarily need to manage each unit stats. At the end of the battle (a battle is composed of several turns until one of the team has no more unit), you will count deaths in each team (attackers and defenders) and reduce their actual total number of unit.

• Actually, I do include some randomness. I updated my question above to show more about how things currently work. Also, I can't really predetermine the winner because players can add reinforcements to a fight between rounds. – MikeCruz13 May 28 '12 at 8:58