Thread: The Civ IV Combat System

October 26, 2005, 14:07 #1
 Join Date
 24 Sep 2000
 Location
 Latvia, Riga
 Posts
 18,391
 Thanks
 0
 Thanked 5 Times in 2 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
The Civ IV Combat System
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:
Originally posted by DeepO
[..]
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. 57? (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 57 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 57 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 immunetofirststrikes 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)
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)*((1p)^(nk))
C(n,k) is the binomial coefficient, which is (n!)/(k!*(nk)!)
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)*((10.4)^(117)) = 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)*((1p)^(nk)) )
C(n,k) is the binomial coefficient, C(n,k)=(n!)/(k!*(nk)!)
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 = (ki) to (ni))_of_
( f(j;(ni),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)*((1p)^(nk)) )
C(n,k) is the binomial coefficient, C(n,k)=(n!)/(k!*(nk)!)
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 (1retreat_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]Last edited by Solver; May 23, 2006 at 09:05.
Solver, WePlayCiv CoAdministrator
Contact: solveratweplaycivdotcom
I can kill you whenever I please... but not today.  The Cigarette Smoking Man

October 26, 2005, 14:46 #2
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
Heh... I wanted to do something similar in the weekend. You beat me to it, Solver
[edit: for a more comprehensive post, look 10 posts down]
So allow me to contribute:
How do first strikes work?
First, in order to determine how many first strikes a unit A actually got, you need to look at the combatting unit B. If B ignore first strikes, unit A will have 0 first strikes. (very important early on!) If both A and B have first strikes (say A has 3, B has 1) it's the difference that counts (A gets 2, B gets 0).
Once battle is fought, each first strike gives a free round of battle. That means that a round is fought normally, but in case A has a first strike, he can't lose the round. If B would be hit, he gets hit. If the result is negative, it is ignored.
Notice that the base strength of the units is unaffected: if A has a strength (or hp) of 4, first strikes of 2 after modifiers, and B has a modified strength of 6, the first 2 rounds still have a 4/(4+6)=0.4 chance of succeeding, taking 0.2*6*(3*4+6)/(3*6+4)= 0.98 off the strength of B each time they hit. B, however, has 6/(4+6)=.6 chance of succeeding, but instead of taking .2*6*(3*6+4)/(3*4+6)=1.46 off the strength of A, it doesn't hit anything.
Once first strikes are over, combat continues as usual. At most, B has been hit once (but it could be twice or not), which would mean its strength is now at 5. (the average would be a bit higher) A is still at 4. The numbers in the formula don't change, though, so B still has a .6 chance of winning the next round, while A only has .4. With those numbers, B is almost certainly going to win, even if he was hit a little during first strikes.
You would be tempted to think that first strikes are thus unimportant. Nothing is farther from the thruth, you only need to use them well. If the situation is reversed, and a unit C has 6 strength and 2 first strikes, while unit D has 4 strength, the results are very different.
C will hit D in 60% of the cases, and it will do 0.97 damage. D can hit C in 40% of the cases, and will do 0.8 damage. With those numbers, C will hit D once or twice, doing at least 0.97 damage and reducing D to 3.03. Another 4 hits and he's dead... C still needs 6/.98= 6.1 or 7 hits.
(due to roundings, this is a bad example. In reality the number of rounds left will be 3 for C and 6 for D)
If you compare the battle of C with one without first strikes, you would see that C has a much higher chance of not getting damaged, with a sligthly higher chance of actually winning. The bigger the difference in strengths between C and D, the extremer this effect. Adding 2 first strikes to a unit that is already twice as superiour will mean he will win most battles undamaged. As your damage directly affects your strength in the next battle it is very important to keep a good health.
OTOH, if the difference between the strengths decreases, first strikes give a small advantage. You can use that to win fights where e.g. you have a 10% disadvantage. Once the difference is too large, first strikes won't help you to win, though.
First strikes should be used for two different types of units: those that defend and the overwhelming attackers. As defenders are most likely stronger because of the bonusses, they are helped as they can fend of multiple attacks, still remaining strong. It's very annoying to have dozens of units attack a city, where one single defender just shrugs of the attacks.
For the overwhelming attackers, it can keep you going. This especially applies to pillagers: they will remain healthy long enough to pillage that resource far inside the foreign borders, where units with the same strength but no first strikes will get slowly damaged over time to a point where an older unit can win. A tank which is damaged to strength 1 will most definately lose from a spearman of modified strength 5.
DeepO
[edit: wrong damage]
[edit again: wrong damage In this example only a 2% in damage change, though, and no effect on how many rounds are actually needed to win.]
[edit 3: this time I will get it ]Last edited by DeepO; October 31, 2005 at 11:12.

October 26, 2005, 14:54 #3
 Join Date
 06 Sep 2001
 Location
 Henderson, NV USA (GMT 8)
 Posts
 4,331
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 13:29
Is a unit's strength and damage calculation dynamically recalculated each round, or all based on what they were at the start of the combat?

October 26, 2005, 15:03 #4
 Join Date
 24 Sep 2000
 Location
 Latvia, Riga
 Posts
 18,391
 Thanks
 0
 Thanked 5 Times in 2 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
Based on what they were at the start. The strength at the start of the combat is saved, and damage (and probability) are according to that.
Solver, WePlayCiv CoAdministrator
Contact: solveratweplaycivdotcom
I can kill you whenever I please... but not today.  The Cigarette Smoking Man

October 26, 2005, 16:10 #5
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
Re: The Civ IV Combat System
Originally posted by Solver
Then, the unit that won the round does damage to the other unit. The damage the unit does is, if the attacker wins:
A/D * 0.2 * A
If the defender wins:
D/A * 0.2 * D
In other words, if two units of equal strength fight, then the winning unit will do 20% of its strength as damage. If a str6 and a str3 unit fight, then the str6 unit will do 40% of 6 damage per round, whereas the str3 unit will do 10% of 3 damage if it wins the round. Thus, you can see that if one unit is twice as strong as the other one, then it has a much better chance of winning.
The damage a unit does, is 20% of its modified strength
So for strength A: damage = A/5
It could be that it changed, though. But it looks like you're mixing up with part of the probability of success formula
DeepO

October 26, 2005, 16:27 #6
 Join Date
 24 Sep 2000
 Location
 Latvia, Riga
 Posts
 18,391
 Thanks
 0
 Thanked 5 Times in 2 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
I'm fairly sure damage isn't 20% of units strength. A unit twice as powerful as the defender does 40% of its stength as damage.
Solver, WePlayCiv CoAdministrator
Contact: solveratweplaycivdotcom
I can kill you whenever I please... but not today.  The Cigarette Smoking Man

October 26, 2005, 16:44 #7
 Join Date
 13 Mar 1998
 Posts
 5,360
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 16:29
Damage each round:
If attacker wins:
damage = 0.2 * (3*A + D) / (3*D + A)
If defender wins:
damage = 0.2 * (3*D + A) / (3*A + D)

October 26, 2005, 16:58 #8
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
I stand corrected... I'll edit my first strikes post above (as that is still valid )
Oh, there are some little rounding things at work.
DeepO

October 26, 2005, 18:39 #9
 Join Date
 04 Dec 2001
 Posts
 11,117
 Thanks
 0
 Thanked 1 Time in 1 Post
 Local Date
 September 28, 2016
 Local Time
 21:29
Originally posted by alexman
Damage each round:
If attacker wins:
damage = 0.2 * (3*A + D) / (3*D + A)
If defender wins:
damage = 0.2 * (3*D + A) / (3*A + D)

October 26, 2005, 18:44 #10
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
I know... I asked alexman about it. I think it's a little different... but give him a rest, they just released CIV
DeepO

October 26, 2005, 18:46 #11
 Join Date
 13 Mar 1998
 Posts
 5,360
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 16:29
Correct. All units have the same number of HP when healthy, no matter how strong they are. The above formula says that when A == D, a successful hit damages the loser by 20% of his full HP.

October 26, 2005, 19:52 #12
 Join Date
 04 Dec 2001
 Posts
 11,117
 Thanks
 0
 Thanked 1 Time in 1 Post
 Local Date
 September 28, 2016
 Local Time
 21:29
That explains it. I interpreted your formula as (absolute) 0.2 hp damage  which seemed a bit odd.

October 27, 2005, 08:59 #13
 Join Date
 09 Nov 2001
 Posts
 11
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 15:29
Originally posted by alexman
Correct. All units have the same number of HP when healthy, no matter how strong they are. The above formula says that when A == D, a successful hit damages the loser by 20% of his full HP.
A is at strength 4 out of 4
D is at strength 4 out of 8
Let's assume there are no modifiers.
Am I right in thinking that D would have to hit A 5 times to destroy A (since it's doing 20% damage per round), but A would only have to hit D 3 times (2.5 rounding up), since D has already lost half its hit points prior to the battle?

October 27, 2005, 09:08 #14
 Join Date
 29 Apr 2002
 Location
 The Republic of Texas
 Posts
 310
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 15:29
So does the amount of damage go down as the unit is damged?
Correct me if I am wrong, but winning the first round is REALLY important for closely matched units.Got my new computer!!!!

October 27, 2005, 09:51 #15
 Join Date
 04 Dec 2001
 Posts
 11,117
 Thanks
 0
 Thanked 1 Time in 1 Post
 Local Date
 September 28, 2016
 Local Time
 21:29
So the damage done depends solely on the ratio of both strengths?
Alex's formula can be reduced to
d = 0.2 (3S + O)/(3O + S)
where S = strength of the unit itself, and O = strength of the other unit. If we introduce the ratio R = S/O, or S = O * R, the formula becomes
d = 0.2(3OR + O)/(3O + OR)
We can get rid of O...
d = 0.2(3R + 1)/(3 + R)
...and the factor...
d_{S} = (3R + 1)/(5R + 15)
The damage, the other unit inflicts, is
d_{O} = (3 + R)/(5 + 15R)
(we get that by substituting 1/R for R)
If our unit is twice as strong as the others, R = 2, and if our unit wins, it does (3*2 + 1)/(5*2 + 15) = 7 / 25 = 28% damage.
The other unit is half as strong as ours, R = 0.5, and if it wins, it does (3*0.5 + 1)/(5*0.5 + 15) = 2.5/17.5 = 1/7 ~ 14% damage.
AFAIK, the odds of the win of a round also depend only on ratio, out of S/(S + O) and O/(S + O) with our R substitution we get R/(R + 1) and 1/(R + 1)
So in our example above with R=2, we have every round 67% chance to inflict 28% damage, and 33% chance to take 14% damage.
No matter if S = 4 and O = 2, or S = 20 and O = 10.Last edited by Harovan; October 27, 2005 at 09:56.

October 27, 2005, 10:21 #16
 Join Date
 04 Dec 2001
 Posts
 11,117
 Thanks
 0
 Thanked 1 Time in 1 Post
 Local Date
 September 28, 2016
 Local Time
 21:29
If we derive that theory a bit further, we see, that if we multiply odds with damage, we can see the expected damage per round.
It equals for our unit
x_{S} = (R / (R + 1)) * ((3R + 1) / (5R + 15))
or
x_{S} = (3R² + R) / (5R² + 20R + 15)
and equivalently
x_{O} = (R + 3) / (15R² + 20R + 5)
If we take our example above, x_{S} = 28% * 2/3 ~ 19%, and x_{O} = 1/7 * 1/3 = 1/21 ~ 5%.
Or, our unit is expected to kill the other unit in an average of about 56 rounds, while the other unit is expected to kill ours in 21 rounds.
Theoretically only! Because if the fight would go over these 21 rounds and the other unit really wins 7 of them necessary to inflict 100% damage, our unit won 14 at the same time and inflicted 392% damage  hence the other unit would be dead long ago.
So, what would be the real odds of the other unit to win? It has to win 7 rounds and can afford to lose only 3. Or we have to win 4 rounds and can afford to lose 20. I'll check the odds on this another time, when I have more time available. Unless one of you slackers does it.
Sorry for my rambling.Last edited by Harovan; October 27, 2005 at 10:27.

October 27, 2005, 10:42 #17
 Join Date
 26 Nov 2001
 Location
 Waterford, MI
 Posts
 1,433
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 16:29
Originally posted by Solver
Originally posted by Jaybe Is a unit's strength and damage calculation dynamically recalculated each round, or all based on what they were at the start of the combat?
Tom P.
"If you are flamable and have legs you are never blocking a fire exit."  Mitch Hedberg

October 27, 2005, 12:55 #18
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
SR, if you want to stay in one system, it may be easier to adjust Alex' formula to:
If attacker wins:
damage = 0.2 * D * (3*A + D) / (3*D + A)
Internaly, it does count with hp, but you can combine everything into strength. This ties in nicely: you will immediately get the end result and it will make true probability calculations possible. That was what I wanted to publish this weekend, but seeing that I initially made a mistake I'll need to adjust my formulae...
I haven't looked in detail to the rest of your derivation...yet
I'm not sure when, and if, but I thought the combat code will be released when the SDK arrives.
DeepO

October 27, 2005, 12:57 #19
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
Originally posted by padillah
According to this a unit could be killed on a first strike but still manage to kill the attacker on defense. That doesn't seem right.
DeepO

October 27, 2005, 13:18 #20
 Join Date
 26 Nov 2001
 Location
 Waterford, MI
 Posts
 1,433
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 16:29
OK, my mistake. I was confusing the "power" with the HP. you said earlier:
Once first strikes are over, combat continues as usual. At most, B has been hit once (but it could be twice or not), which would mean its strength is now at 5. (the average would be a bit higher) A is still at 4. The numbers in the formula don't change, though, so B still has a .6 chance of winning the next round, while A only has .4. With those numbers, B is almost certainly going to win, even if he was hit a little during first strikes.
Firt Strike should make combat for the other unit harder. It should be a "combat round" in and of itself. I.e. sort out all the First Strikes, deal damage, end of round; First normal combat round, deal damage, end of round; etc..
This way if you are too big you will win anyway, but if you are only one or two points bigger you might want to think twice.
Tom P.
"I like refried beans. That's why I want to try fried beans, 'cause they may taste just as good and we're wasting time."  Mitch Hedberg

October 27, 2005, 13:40 #21
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
Originally posted by padillah
This just seems a little odd to me, like First Strike doesn't matter unless you are already larger. And then you'll probably win anyway so I'm wondering, what's the point?
What the point is? The point is that when you're choosing your promotions, you've got to have some idea what the unit is going to be used for. Remember that when it comes to first strikes, you are dealing with promotions. For certain units, if you've got 5XP (e.g. from barracks and civic choices), you can either choose 2 combat promotions (a flat out +20%), or you choose 2 first strikes promotions (+2 fs). Now, what are you going to choose?
If you want a defender, and you know he will have more strength than any attacker (e.g. because of fortification bonusses), chosing fs is better. If you will fight units of about equal strength, the +20% is better. If you know you're going to face stronger units, build another unit
Firt Strike should make combat for the other unit harder. It should be a "combat round" in and of itself. I.e. sort out all the First Strikes, deal damage, end of round; First normal combat round, deal damage, end of round; etc..
No, it should not be like that. This thing is balanced, why do you think its opponent is +10%? Why are typespecific promotions +25%? You really have a choice. Doing it like you suggest would break the system once you get to higher fs (IIRC you can get to 7 or 8). You wouldn't run into spearmantank problems, but in archertank problems...
That is exactly the border: one fs is in about equal situations comparable to +10%. You will want to think twice.This way if you are too big you will win anyway, but if you are only one or two points bigger you might want to think twice.
DeepO

October 27, 2005, 14:32 #22
 Join Date
 28 Oct 2001
 Posts
 143
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 20:29
A couple of simple questions on combat.
In some review I thought I read there was a way of see the probability of success for a given attack. Is this true? and if so where in the heck do you find it? I have searched in vain to find it in game.
Finally is there a reasonable simple formula for figuring out the probabilities given a modified A and D.
I.e. is there a rule of thumb that a Strength 6 unit will beat a Strength 4 unit 60% of the time. I am pretty sure that isn't right.

October 27, 2005, 14:44 #23
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
Originally posted by Strollen
In some review I thought I read there was a way of see the probability of success for a given attack. Is this true? and if so where in the heck do you find it? I have searched in vain to find it in game.
Finally is there a reasonable simple formula for figuring out the probabilities given a modified A and D.
Yes there is, but no, it's not simple.
I.e. is there a rule of thumb that a Strength 6 unit will beat a Strength 4 unit 60% of the time. I am pretty sure that isn't right.
Whenever you get a ratio of strengths, you can calculate the possibility of success. But first strikes mess up this nice and easy rule of thumb. It most certain is not 60% in the above mentioned case, it will be closer to 99%.
DeepO

October 27, 2005, 16:45 #24
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
Okay. Let's see how much text we can pour into one post
This would not have been possible without the help of many others. Thank you.
Also, sorry Solver, but I'm changing 'the system' to a double value; strength and hp. While it might be a little harder to read, it is how it actually works. It also is in line with Alex' posts.
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. 57? (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 57 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 57 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 immunetofirststrikes 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)
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)*((1p)^(nk))
C(n,k) is the binomial coefficient, which is (n!)/(k!*(nk)!)
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)*((10.4)^(117)) = 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)*((1p)^(nk)) )
C(n,k) is the binomial coefficient, C(n,k)=(n!)/(k!*(nk)!)
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 = (ki) to (ni))_of_
( f(j;(ni),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)*((1p)^(nk)) )
C(n,k) is the binomial coefficient, C(n,k)=(n!)/(k!*(nk)!)
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 (1retreat_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]Last edited by DeepO; November 1, 2005 at 06:27.

October 27, 2005, 17:00 #25
 Join Date
 04 Oct 2005
 Posts
 17
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 20:29
Wow that is some explanation, bits you average stat text book.
Great work thanks.

October 27, 2005, 17:12 #26
 Join Date
 12 Jan 2002
 Posts
 7,017
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 16:29
Originally posted by Strollen
In some review I thought I read there was a way of see the probability of success for a given attack. Is this true? and if so where in the heck do you find it? I have searched in vain to find it in game.And her eyes have all the seeming of a demon's that is dreaming...

October 27, 2005, 17:19 #27
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
It's in? Wow! In case anyone would mind, I'd be interested if the numbers above are (about) correct... I'd check them myself, but that will have to wait until I get the game
DeepO

October 27, 2005, 17:32 #28
 Join Date
 28 Oct 2001
 Posts
 143
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 20:29
Deepo thanks a lot. If I had more than 4 hours of sleep I would even be able to follow the equations. But for my purposes knowing that a 50% strength advantage means a 90%+ chance of defeating an opponent. Is very helpfu.
My biggest complaint right now with Civ 4, is it is very difficult to get any kind of intuitive feel for how likely your are to succeed in an attack. I can do the math in my head for my units (swordsman with city raider II = 6* 1+(.2+.25+.1) = 9.3 ) but it is really hard for my opponents. I mean there are a zillion different icons for promotions + inherent unit advantages. How the heck do I know who is going to defend a city with an archer, spearman, and axeman all with a couple of promotions.
Just saw Domanie post Alt T thank you I am going to try that now.

October 27, 2005, 17:44 #29
 Join Date
 12 Jan 2002
 Location
 supporting Candle'Bre
 Posts
 8,773
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 28, 2016
 Local Time
 22:29
Strollen, without taking into account the rounding, you can have a simple table to see the numbers. Be aware that difference rapidly increases: a unit which is 2/3 of another one might have 10% of succeeding, units which are only half will have a rougher time (my guess is only 2% or even below that).
Also, Re: the intuitive feeling: Don't obsess with the numbers. Horses are good against archers, melee is good against horses, and archers are good against melee. That will get you far!
With promotions, it makes a lot of sense to specialize your units in different roles. Have static defenders, mobile defenders, pillagers, city raiders, scouts, and attackers and try to figure out what would work. It's one of the most fun parts of the game... and certainly not something which can be poured into numbers easily. What would be the effect of a medic vs a combat II promotion, for instance...
DeepO

October 27, 2005, 18:02 #30
 Join Date
 06 Jun 2001
 Location
 Newcastle,Australia
 Posts
 137
 Thanks
 0
 Thanked 0 Times in 0 Posts
 Local Date
 September 29, 2016
 Local Time
 06:29
I must be slow. What is the point of unit differentiation?
I have an army of many units and so does my opponent. Each army chooses(?) 1 unit which then fight. After one has lost the next one steps up.
How is this combined arms? How can an archer support an infrantryman?
Unless I am very wrong this is sounding sillier and sillier by the moment.
Similar Threads

Combat System
By Kc7mxo in forum Civilization IV CreationReplies: 1Last Post: November 1, 2005, 20:32 
Ooh, I am so P****ed at Combat System
By RKC909 in forum Civ3GeneralArchiveReplies: 14Last Post: November 26, 2002, 02:01 
this combat system has to **GO**
By self biased in forum Civ3GeneralArchiveReplies: 209Last Post: November 1, 2002, 04:23 
Does anyone else like the new combat system?
By Pythagoras in forum Civ3GeneralArchiveReplies: 29Last Post: November 28, 2001, 08:39 
Civ 3 combat system
By mrbilll in forum Civ3GeneralArchiveReplies: 0Last Post: November 20, 2001, 05:11
Bookmarks