In Civ4, the combat system ensures much more fair combat outcomes than previously, though a better battle resolution algorithm coupled with the return of firepower. Now, I will explain how exactly battles in Civ4 are resolved.

The below is what is posted by DeepO later in the thread, and I'm putting it into the first post for ease. My original post hda a number of inaccuracies, so here's the correct and accurate system, taking in account all the corrections from Firaxians, etc. Here goes:

The below is what is posted by DeepO later in the thread, and I'm putting it into the first post for ease. My original post hda a number of inaccuracies, so here's the correct and accurate system, taking in account all the corrections from Firaxians, etc. Here goes:

Originally posted by DeepO

[..]

Combat is a 'battle' between 2 values, the strength of the attacker A, and the strength D of the defender. These values are modified depending on promotions and situations, e.g. forest gives an extra 50% defense, which modifies D.

Once modification of A and D is taken into account, it doesn't matter whether the unit is defending or attacking, the rest of the formula is general for both parties.

Each unit starts out with 100 integer hitpoints, and will lose some of them after the end of the battle. Once it reaches zero, the unit is dead. If an archer of strength 3 loses 40 hp, and ends at 60 hp, it will be wounded to 3 * .6 = 1.8 strength. If that archer would have been wounded to 2.5 before starting battle, and would lose 40 hp, those hp are 'worth less'. It would end the battle at 60hp, being 2.5*.6 = 1.5.

Combat is fought in rounds, each rounds being a dice roll between A and D. One side will lose, and hence loses part of his hitpoints. After this a next round is fought. Once one of the hitpoint values reaches zero or below, combat is over and the unit destroyed.

First strikes give 'free' rounds. This means that if the attacker has 2 first strikes, 2 rounds are fought where combat happens as normal, but in case the attacker would lose this result is ignored.

Retreat has no influence on the chance of success, retreat only happens when the unit would have died.

-------------------------------

For every unit, there are actually 2 parameters: let's call them first_strikes, and extra_first_strikes. Some units start with first_strikes already, but normally these parameters are changed by promotion.

In the case of Drill IV, a unit that displays as 5-7 first strikes means he will have 5 sure first strikes (so first_strikes=5), and 2 more possible first strikes (so extra_first_strikes=2).

--------

Before battle is fought, each unit has a step where the number of first strikes is computed. This total for the battle is equal to

Basically, it means that for the battle, anywhere between 5 and 7 first strikes will be assigned to the Drill IV unit.

(Note that I don't kow what kind of randomizer is used for the chance distribution, meaning I can't tell if 5-7 would leadmore to 6 first strikes then it would to 5.)

---------------------

Once a battle is fought, the first thing to check is if the opponent has the immune-to-first-strikes property. If it doesn't, the total falls to 0.

Next, the two battle totals are compared, and the difference is taken. This difference is the number of first strikes the rest of the combat algorithm will use.

So, a Drill IV unit, attacking a Drill II unit, might get 6 first strikes initially (somewhere in between 5 and 7). It faces an opponent of 2 first strikes, which leads to a difference of 4 first strikes.

Both attacker and defender get a bonus from promotions (e.g. Combat 1 = +10% strength) and situations (e.g. forest gives +50% defense for some defenders). These add up to the base value of a unit, which is a units

for the attacker:

Thus, an archer with initially strength 3, but which was wounded and only has 2.5 left, with Combat 1 (+10%), and in a forest (+50%) gets a strength

D=2.5*(+10%+50%)=2.5*1.6=4

Chance of succeeding in each round is a pure die roll. This means that for the attacker, his

Each round inflicts a fixed amount of damage. This damage is subtracted from the hitpoints, where hitpoints start at 100. The damage is dependent on the strengths of the units (see below). If the archer is attacked by a horse archer of strength A=6 (no modifiers as it is unpromoted), the damage done will be 16 hp. After losing that round, the strength of the archer will be 100 - 16 = 84.

The damage done in one round is always relative to 20 hitpoints. But there is one more factor, to 'increase the gap', that is, it will make stronger units hit even more damage on weaker ones.

The result is rounded to integer results. So, for the archer A, he will inflict 20 * 9 / 11 = 16 hp of damage. The horse archer D will inflict 20 * 11 / 9 = 24 hp of damage.

-------------------------

As each hit does a fixed amount of damage, it means that a unit can receive a fixed number of hits before dieing. This number is only dependent on the

In a battle between a archer of D=4 and a horse archer of A = 6, each hit to the archer will amount to 24 hp as calculated above. Thus, an archer will die at the 5th hit. After 4 hits, it would have 100 - (4 * 24) = 4 hitpoints left, the 5th hit is fatal. (it would diminish hitpoints to 4 - 24 = -20)

Similarly, the horse archer dies after 7 hits, as the 6th hit would leave 100 - (6 * 16) = 6. The 7th hit would mean hitpoints equal to -10.

Where round_up() is a function that rounds a fraction to the next integer.

So, again for that archer:

hitpoints=4

damage_done_in_round = 24

needed_hits_to_die = round_up(100/24) = round_up(4.17) = 5

Note that this number is fixed, it is independent from the chance of actually getting hit. Once the archer is hit 5 times it is dead, no matter if the horse is hit once, or 6 times in the mean time.

--------------------------------

We now have everything together for the basic formula. We've got the chance of getting hit, and we've got the number of hits before someone dies. Also, we've got a

This means, that the chance of success for the archer is the chance it wins

In the example of the archer hitting

k = 7

n = 11

chance of success for one round = p=D/(A+D)= 4/(4+6)=0.4

C(11,7) = 330

f(7;11,0.4) = C(11,7)*(0.4^7)*((1-0.4)^(11-7)) = 0.0701

The chance of the archer hitting exactly 7 times is 7%

-----------------------

The easiest way to calculate the total chance of

So, the chance of success is

For the archer, this means that his chance of winning the battle is

chance_of_success = f(7;11,0.4)+f(8;11,0.4)+f(9;11,0.4)+f(10;11,0.4)+f (11;11,0.4) = 0.0701 + 0.02336 + 0.00519 + 0.000692 + 0.0000419 = 0.09935.

The archer has a 9.9% chance of destroying the horse archer.

----------------------

If the archer would have won, he would roughly have 70% chance of being hit 4 times, 23% chance of being hit 3 times, 5% chance of being hit 2 times. (forget the rest, you catch the drift) This works out to .7 * (100 - 4 * 24) + .23 * (100 - 3 * 24) + .05 * (100 - 2 * 24) + ... = 2.8 + 6.4 + 2.4 + .. = 11.64 hp. Which means, given his initial strength of 2.5, he would on average have 0.3 strength left. That is, assuming he has won, in 90% of the cases he would have lost...

------------------------------------------------------------

If an archer has a first strikes promotion, getting to 2 first strikes, it means that the order in which attacks happen does start to matter. If he loses the first 2 strikes, he doesn't lose hitpoints. If he wins one of this 2 first strikes, the damage to the horse archer is done just the same like normally. (okay, I know the horse archer ignores first strikes, so assume a swordsman, it also has strength 6)

If there would be two units with first strikes, the difference counts. E.g. a archer with 2 first strikes, against a longbow with 1, would get 1 first strike. The longbow gets none.

It is tempting to simply up the number_of _needed_hits_to_die for the horse archer in the example used, but that's not exactly correct.

Suppose the sword hits the first 2 rounds. The results are ignored, thus the sword needs to hit the archer 9 times, not 7. The n used above would increase from 11 to 13, virtually.

Suppose on the other hand that the archer hits the first 2 rounds. That means the sword needs to hit 7 times out of 9 of the following rounds. The total number of possible rounds n remains at 11.

The correct way of calculating it, is to calculate the chance of winning the first strike for the archer, times the chance of winning the rest of the battle. this has to be done for each combination of first_strike_wins, and added.

So, calculate the chance of winning none of the first strikes, times winning 8 out of 11 rounds after that. Add to that the chance of winning one first strike, times 7 out of 10 rounds. Add to that the chance of winning two first strikes, times 6 out of 9 rounds following it.

The chance of winning first strikes follows again a binomial distribution. (call it f_fs, to distinguish between first strikes and the 'normal' part of the formula.

I know it seems complicated, but this formula is nicely divided into parts, which each in itself are reasonably simple. It's just that combining everything together makes it look like a mess.

As an example, consider the archer of above, with 2.5 strength and 2 first strikes. It battles a sword of strength 6 (as a horse archer would ignore first strikes)

Chance for the archer to hit 0 first strikes = 0.36

Chance for the archer to hit at least 7 out of 11 rounds following the first strikes = 0.0707

Chance for the archer to hit 1 first strikes = 0.48

Chance for the archer to hit at least 6 out of 10 rounds following the first strikes = 0.114 + 0.067 + 0.013 + ... = 0.194

Chance for the archer to hit 2 first strikes = 0.16

Chance for the archer to hit at least 5 out of 9 rounds following the first strikes = 0.167 + 0.124 + 0.077 + 0.037 + ... = 0.404

Total chance of success is

0.36*0.0707 + 0.48*0.194 + 0.16*0.404 = 0.183

The archer has 18.3% chance of winning.

Retreat is calculated in the last round for the attacker (defenders can't retreat). The round is fought like normal, but in case the attacker would have died, it's retreat chance is taken into account and possibly it can escape. If you somehow would have units with 100% retreat chance, they will never die on the offensive.

Retreat does not happen during first strikes, as long as the defender has first strikes left the attacker can never escape.

Suppose that the horse archer above has a 30% of retreating. If by coincidence the archer would hit the horse archer 7 times during the fight, the horse archer is one hit from dieing.

Next round starts. The odds are calculated, and the horse archer would die. At that moment, a random number is generated and weigthed against the 30% retreat chance... if it is higher the round continues like normal, and the horse archer dies. If it is lower, the round and the battle stops, and the horse archer retreats heavily wounded.

While the archer is only 2/3 the strength of the horse archer, it only wins in 9.9% of the cases... that is a large difference, much larger than expected. And the archer is very lucky as due to roundings, he can survive 5 rounds. If the damage against him would have been 25 instead of 24 now, he would die after 4 rounds, meaning less than 2% chance.

More later. And you can virtually add some of the combats on first strikes in here as well

Sorry if any typos would have crept in, this was all done by hand

DeepO

[edit: added: how do first strikes get calculated]

[..]

**general description**Combat is a 'battle' between 2 values, the strength of the attacker A, and the strength D of the defender. These values are modified depending on promotions and situations, e.g. forest gives an extra 50% defense, which modifies D.

Once modification of A and D is taken into account, it doesn't matter whether the unit is defending or attacking, the rest of the formula is general for both parties.

Each unit starts out with 100 integer hitpoints, and will lose some of them after the end of the battle. Once it reaches zero, the unit is dead. If an archer of strength 3 loses 40 hp, and ends at 60 hp, it will be wounded to 3 * .6 = 1.8 strength. If that archer would have been wounded to 2.5 before starting battle, and would lose 40 hp, those hp are 'worth less'. It would end the battle at 60hp, being 2.5*.6 = 1.5.

*(I'm not 100% sure here, so correct me if the damage inflicted per hp is the same for normal and wounded units)*Combat is fought in rounds, each rounds being a dice roll between A and D. One side will lose, and hence loses part of his hitpoints. After this a next round is fought. Once one of the hitpoint values reaches zero or below, combat is over and the unit destroyed.

First strikes give 'free' rounds. This means that if the attacker has 2 first strikes, 2 rounds are fought where combat happens as normal, but in case the attacker would lose this result is ignored.

Retreat has no influence on the chance of success, retreat only happens when the unit would have died.

-------------------------------

**How many first strikes does a unit get, when it says e.g. 5-7? (That would be Drill III or IV, depending on the unpromoted unit)**For every unit, there are actually 2 parameters: let's call them first_strikes, and extra_first_strikes. Some units start with first_strikes already, but normally these parameters are changed by promotion.

In the case of Drill IV, a unit that displays as 5-7 first strikes means he will have 5 sure first strikes (so first_strikes=5), and 2 more possible first strikes (so extra_first_strikes=2).

--------

Before battle is fought, each unit has a step where the number of first strikes is computed. This total for the battle is equal to

**first_strikes_battle = first_strikes + chance(extra_first_strikes).**Basically, it means that for the battle, anywhere between 5 and 7 first strikes will be assigned to the Drill IV unit.

(Note that I don't kow what kind of randomizer is used for the chance distribution, meaning I can't tell if 5-7 would leadmore to 6 first strikes then it would to 5.)

---------------------

Once a battle is fought, the first thing to check is if the opponent has the immune-to-first-strikes property. If it doesn't, the total falls to 0.

Next, the two battle totals are compared, and the difference is taken. This difference is the number of first strikes the rest of the combat algorithm will use.

So, a Drill IV unit, attacking a Drill II unit, might get 6 first strikes initially (somewhere in between 5 and 7). It faces an opponent of 2 first strikes, which leads to a difference of 4 first strikes.

**Basic formula**__modifiers__Both attacker and defender get a bonus from promotions (e.g. Combat 1 = +10% strength) and situations (e.g. forest gives +50% defense for some defenders). These add up to the base value of a unit, which is a units

*wounded*strength. Once this wounded strength value gets all modifiers, it becomes the modified strength. We'll call that number A for the attacker, and D for the defender. The moment modifiers are taken into account, there is no difference anymore between an attacker and defender though, it's all contained into the modified strength.for the attacker:

**Modifed_strength A = wounded_base_value * (sum_of_modifiers)**Thus, an archer with initially strength 3, but which was wounded and only has 2.5 left, with Combat 1 (+10%), and in a forest (+50%) gets a strength

D=2.5*(+10%+50%)=2.5*1.6=4

__rounds__Chance of succeeding in each round is a pure die roll. This means that for the attacker, his

**chance of winning a round is A/(A+D)**. For the defender it is the dual D/(A+D).Each round inflicts a fixed amount of damage. This damage is subtracted from the hitpoints, where hitpoints start at 100. The damage is dependent on the strengths of the units (see below). If the archer is attacked by a horse archer of strength A=6 (no modifiers as it is unpromoted), the damage done will be 16 hp. After losing that round, the strength of the archer will be 100 - 16 = 84.

__damage in one round__The damage done in one round is always relative to 20 hitpoints. But there is one more factor, to 'increase the gap', that is, it will make stronger units hit even more damage on weaker ones.

**For the attacker, the damage is 20 * (3 * A + D) / (3 * D + A)**

For the defender, the damage is 20 * (3 * D + A) / (3 * A + D)For the defender, the damage is 20 * (3 * D + A) / (3 * A + D)

The result is rounded to integer results. So, for the archer A, he will inflict 20 * 9 / 11 = 16 hp of damage. The horse archer D will inflict 20 * 11 / 9 = 24 hp of damage.

-------------------------

__number of possible hits__As each hit does a fixed amount of damage, it means that a unit can receive a fixed number of hits before dieing. This number is only dependent on the

*initial*strengths of the units.In a battle between a archer of D=4 and a horse archer of A = 6, each hit to the archer will amount to 24 hp as calculated above. Thus, an archer will die at the 5th hit. After 4 hits, it would have 100 - (4 * 24) = 4 hitpoints left, the 5th hit is fatal. (it would diminish hitpoints to 4 - 24 = -20)

Similarly, the horse archer dies after 7 hits, as the 6th hit would leave 100 - (6 * 16) = 6. The 7th hit would mean hitpoints equal to -10.

**needed_hits_to_die = round_up(100/damage_done_in_round)**Where round_up() is a function that rounds a fraction to the next integer.

So, again for that archer:

hitpoints=4

damage_done_in_round = 24

needed_hits_to_die = round_up(100/24) = round_up(4.17) = 5

Note that this number is fixed, it is independent from the chance of actually getting hit. Once the archer is hit 5 times it is dead, no matter if the horse is hit once, or 6 times in the mean time.

--------------------------------

**basic chance of success**We now have everything together for the basic formula. We've got the chance of getting hit, and we've got the number of hits before someone dies. Also, we've got a

*fixed time frame*: in order to win, the archer has to hit the horse archer 7 times, before getting hit 5 times himself.This means, that the chance of success for the archer is the chance it wins

*at least 7 rounds out of 11 rounds*of battle. The chance of winning one round is D/(A+D) for the defending archer. It doesn't matter in which order the rounds are won, it also doesn't matter whether the archer by sheer streak of luck would have won 11 rounds instead of only the first 8.**This is a Bernouilli process, where each event X0..Xn is equal to a round of battle. Each event has a chance p, where p=D/(A+D) for the defender. The chance of getting exactly k=7 hits out of n=11 rounds follows the binomial distribution, where f(k;n,p)=C(n,k)*(p^k)*((1-p)^(n-k))**

C(n,k) is the binomial coefficient, which is (n!)/(k!*(n-k)!)C(n,k) is the binomial coefficient, which is (n!)/(k!*(n-k)!)

In the example of the archer hitting

*exactly*7 times out of 11, the chance is:k = 7

n = 11

chance of success for one round = p=D/(A+D)= 4/(4+6)=0.4

C(11,7) = 330

f(7;11,0.4) = C(11,7)*(0.4^7)*((1-0.4)^(11-7)) = 0.0701

The chance of the archer hitting exactly 7 times is 7%

-----------------------

The easiest way to calculate the total chance of

*at least*7 wins out of 11 rounds is to add the chances together of 7, 8, 9, 10, and 11 wins. There is a complicated formula which does this automatically, but in a program it is easier with a simple for loop...**Chance_of_destroying the horse archer = chance of hitting 7, 8, 9, 10, or 11 times.**So, the chance of success is

**basic_chance_of_success = Sum_(for j = k to n)_of_( f(j;n,p) )**

where

k = needed_hits_opponent

n = (needed_hits_opponent + needed_hits_yourself -1)

p = chance of winning 1 round (=D/(A+D) for the defender)

f(k;n,p)=C(n,k)*(p^k)*((1-p)^(n-k)) )

C(n,k) is the binomial coefficient, C(n,k)=(n!)/(k!*(n-k)!)where

k = needed_hits_opponent

n = (needed_hits_opponent + needed_hits_yourself -1)

p = chance of winning 1 round (=D/(A+D) for the defender)

f(k;n,p)=C(n,k)*(p^k)*((1-p)^(n-k)) )

C(n,k) is the binomial coefficient, C(n,k)=(n!)/(k!*(n-k)!)

For the archer, this means that his chance of winning the battle is

chance_of_success = f(7;11,0.4)+f(8;11,0.4)+f(9;11,0.4)+f(10;11,0.4)+f (11;11,0.4) = 0.0701 + 0.02336 + 0.00519 + 0.000692 + 0.0000419 = 0.09935.

The archer has a 9.9% chance of destroying the horse archer.

----------------------

__hitpoints after battle__If the archer would have won, he would roughly have 70% chance of being hit 4 times, 23% chance of being hit 3 times, 5% chance of being hit 2 times. (forget the rest, you catch the drift) This works out to .7 * (100 - 4 * 24) + .23 * (100 - 3 * 24) + .05 * (100 - 2 * 24) + ... = 2.8 + 6.4 + 2.4 + .. = 11.64 hp. Which means, given his initial strength of 2.5, he would on average have 0.3 strength left. That is, assuming he has won, in 90% of the cases he would have lost...

------------------------------------------------------------

**first strikes**If an archer has a first strikes promotion, getting to 2 first strikes, it means that the order in which attacks happen does start to matter. If he loses the first 2 strikes, he doesn't lose hitpoints. If he wins one of this 2 first strikes, the damage to the horse archer is done just the same like normally. (okay, I know the horse archer ignores first strikes, so assume a swordsman, it also has strength 6)

If there would be two units with first strikes, the difference counts. E.g. a archer with 2 first strikes, against a longbow with 1, would get 1 first strike. The longbow gets none.

It is tempting to simply up the number_of _needed_hits_to_die for the horse archer in the example used, but that's not exactly correct.

Suppose the sword hits the first 2 rounds. The results are ignored, thus the sword needs to hit the archer 9 times, not 7. The n used above would increase from 11 to 13, virtually.

Suppose on the other hand that the archer hits the first 2 rounds. That means the sword needs to hit 7 times out of 9 of the following rounds. The total number of possible rounds n remains at 11.

The correct way of calculating it, is to calculate the chance of winning the first strike for the archer, times the chance of winning the rest of the battle. this has to be done for each combination of first_strike_wins, and added.

So, calculate the chance of winning none of the first strikes, times winning 8 out of 11 rounds after that. Add to that the chance of winning one first strike, times 7 out of 10 rounds. Add to that the chance of winning two first strikes, times 6 out of 9 rounds following it.

The chance of winning first strikes follows again a binomial distribution. (call it f_fs, to distinguish between first strikes and the 'normal' part of the formula.

**complete formula**

total_chance_of_success =

Sum_(for i = 0 to fs)_of_

( f(i;fs,p)*

Sum_(for j = (k-i) to (n-i))_of_

( f(j;(n-i),p)

)

)

where

k = needed_hits_opponent

n = (needed_hits_opponent + needed_hits_yourself -1)

p = chance of winning 1 round (=D/(A+D) for the defender)

fs = total number of first strikes for the unit

f(k;n,p)=C(n,k)*(p^k)*((1-p)^(n-k)) )

C(n,k) is the binomial coefficient, C(n,k)=(n!)/(k!*(n-k)!)

total_chance_of_success =

Sum_(for i = 0 to fs)_of_

( f(i;fs,p)*

Sum_(for j = (k-i) to (n-i))_of_

( f(j;(n-i),p)

)

)

where

k = needed_hits_opponent

n = (needed_hits_opponent + needed_hits_yourself -1)

p = chance of winning 1 round (=D/(A+D) for the defender)

fs = total number of first strikes for the unit

f(k;n,p)=C(n,k)*(p^k)*((1-p)^(n-k)) )

C(n,k) is the binomial coefficient, C(n,k)=(n!)/(k!*(n-k)!)

I know it seems complicated, but this formula is nicely divided into parts, which each in itself are reasonably simple. It's just that combining everything together makes it look like a mess.

__Example__As an example, consider the archer of above, with 2.5 strength and 2 first strikes. It battles a sword of strength 6 (as a horse archer would ignore first strikes)

Chance for the archer to hit 0 first strikes = 0.36

Chance for the archer to hit at least 7 out of 11 rounds following the first strikes = 0.0707

Chance for the archer to hit 1 first strikes = 0.48

Chance for the archer to hit at least 6 out of 10 rounds following the first strikes = 0.114 + 0.067 + 0.013 + ... = 0.194

Chance for the archer to hit 2 first strikes = 0.16

Chance for the archer to hit at least 5 out of 9 rounds following the first strikes = 0.167 + 0.124 + 0.077 + 0.037 + ... = 0.404

Total chance of success is

0.36*0.0707 + 0.48*0.194 + 0.16*0.404 = 0.183

The archer has 18.3% chance of winning.

**Retreat**Retreat is calculated in the last round for the attacker (defenders can't retreat). The round is fought like normal, but in case the attacker would have died, it's retreat chance is taken into account and possibly it can escape. If you somehow would have units with 100% retreat chance, they will never die on the offensive.

Retreat does not happen during first strikes, as long as the defender has first strikes left the attacker can never escape.

Suppose that the horse archer above has a 30% of retreating. If by coincidence the archer would hit the horse archer 7 times during the fight, the horse archer is one hit from dieing.

Next round starts. The odds are calculated, and the horse archer would die. At that moment, a random number is generated and weigthed against the 30% retreat chance... if it is higher the round continues like normal, and the horse archer dies. If it is lower, the round and the battle stops, and the horse archer retreats heavily wounded.

**This has no effect on the chance of success for the attacker: it still has the same chance of destroying the defender. The defender's chance of destroying the attacker is however multiplied with (1-retreat_chance). Retreat happens in retreat_chance*chance_of_success_defender percentage of cases.****interpretation**While the archer is only 2/3 the strength of the horse archer, it only wins in 9.9% of the cases... that is a large difference, much larger than expected. And the archer is very lucky as due to roundings, he can survive 5 rounds. If the damage against him would have been 25 instead of 24 now, he would die after 4 rounds, meaning less than 2% chance.

More later. And you can virtually add some of the combats on first strikes in here as well

Sorry if any typos would have crept in, this was all done by hand

DeepO

[edit: added: how do first strikes get calculated]

## Comment