No announcement yet.

The Civ IV Combat System

  • Filter
  • Time
  • Show
Clear All
new posts

  • 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. 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

    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

    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:

    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)!)

    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) )
    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)

    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.

    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.

    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.

    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


    [edit: added: how do first strikes get calculated]
    Last edited by Solver; May 23, 2006, 09:05.
    Solver, WePlayCiv Co-Administrator
    Contact: solver-at-weplayciv-dot-com
    I can kill you whenever I please... but not today. - The Cigarette Smoking Man

  • #2
    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.


    [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, 12:12.


    • #3
      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?


      • #4
        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 Co-Administrator
        Contact: solver-at-weplayciv-dot-com
        I can kill you whenever I please... but not today. - The Cigarette Smoking Man


        • #5
          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 str-6 and a str-3 unit fight, then the str-6 unit will do 40% of 6 damage per round, whereas the str-3 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.
          Solver, correct me if I'm wrong, but shouldn't that be:

          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



          • #6
            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 Co-Administrator
            Contact: solver-at-weplayciv-dot-com
            I can kill you whenever I please... but not today. - The Cigarette Smoking Man


            • #7
              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)


              • #8
                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.



                • #9
                  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)
                  Hmmm, for the case A == D (which should be about normal) this would mean damage is always 0.2, no matter if A and D are 2 or 20.


                  • #10
                    I know... I asked alexman about it. I think it's a little different... but give him a rest, they just released CIV



                    • #11
                      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.


                      • #12
                        That explains it. I interpreted your formula as (absolute) 0.2 hp damage - which seemed a bit odd.


                        • #13
                          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 question for you. Given the following situation at the start of a battle:

                          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?


                          • #14
                            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!!!!


                            • #15
                              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...

                              dS = (3R + 1)/(5R + 15)

                              The damage, the other unit inflicts, is

                              dO = (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, 09:56.