Freeciv
Advertisement

This page describes in mathematical terms what will be the result of one unit attacking another.

Probability of winning a fight[]

The chances of winning a fight are sometimes counter-intuitive. E.g., what would you say are your chances of winning in the following situation (after taking into account the land advantages and other modifiers): you have an attack of 2 against a defense of 3, and the same number of hit points? Considering that the probability of winning each round is 2/5, you might think that this will give you with a reasonable chance of victory, something like 40%. Lost. You only have 19% chances of winning. The fact that the battle lasts several rounds tends to give a larger advantage to the biggest power than one could think beforehand.

Supposing the reader is aware of the rules of the fight (for each round, take a random number, decide who wins that round, etc.), let's try to find a formula representing the chances of victory.

Notations and basic computations[]

Consider a unit having a strength (all modifications, field, city, unit experience of combat, etc. being applied)[1] and an enemy unit having a defense . The number of hit points of the defending unit is written , the damages per victorious rounds from the attacking unit is .

Let us note the number of victorious rounds required to win the fight. And is the number of rounds that, if lost, implies a defeat ( is for "losing allowance"). When the attacking unit cause one damage per victorious rounds (), is the number of hit points of the defending unit (). More generally,

.

may be computed similarily. The chance of winning each round, let us note it , is

.

Thus the chance of losing a round is

.

Let us also denote the maximal possible number of rounds fought. Usually, since each round results in some unit's loss of points and after a unit is defeated it can't strike any more and its enemy must keep at least one point, and the total number of possible losses is , is equal to

(variant of when it happens: attacker loses round and then wins until the victory). But since version 3.0 of Freeciv this number can be limited by ruleset; so, if , there is some probability that both units survive the battle (limitation greater than or equal to has no effect).[2]

Reasoning[]

Now let us suppose that the fight takes rounds to achieve, and let us compute the probability of victory. Note that we do not know in advance but we suppose for now that it is given. For a given , the probability of victory is the probability that rounds have been won over these tries and less than rounds have been lost. If , then the probability of victory is simply the probability that rounds have been won over these tries (as this implies that less than rounds have been lost). In case of victory, the last round must be the final hit (otherwise the fight would have been stopped earlier). Apart from that, there must be hits among the other rounds. That probability is given by a binomial and is

.

As the last round must be won, we multiply by and get a probability of victory with rounds of

.

For the attacking unit to win the fight, we must have (assuming that )

.

Thus finally the probability of winning equals:

.(1)

Reasoning from the defeat count[]

The freeciv source code uses a slightly different reasoning (look at ./common/combat.c:unit_win_chance() and ./common/combat.c:win_chance()). Let us note the number of rounds lost, which must be smaller than for a victorious attack. Then we have the following probability of victory:

.(2)

We get the formula (1) by using

and

.

In the code,

is def_N_lose,

is att_N_lose,

is def_P_lose1,

is att_P_lose1,

is lr.

Round limit is not currently taken into account in the code, but probably some day it will be.

Generalized reasoning[]

Let's imagine that the fight always consists of rounds, even if usually one of the parts in reality is killed before the number is reached; we now have a set of different imaginary battles. All the real fights have one or more items in this set for which actual rounds are matching real ones and the remaining ones are any possible sequence (for -round battle, there are matching items). We can see that probability of the imaginary subset is equal to the probability of the real battle it matches (since first rounds are similar, following imaginary rounds are independent, and happening of any one of all possible combinations has the possibility of ). Then, any sequence where or more rounds are win matches to a won fight, and any one that has or more rounds lost matches a lost fight (because , imaginary fight can't have losses when in its real part there are wins, etc.) Thus, we come again to the Bernoulli model: the probability of victory is equal to the probability of having or more successes in series. This can be expressed using Bernoulli cumulative distribution function (possibility of getting or less successes)

:

the possibilities of winning, losing and "stalemate" are respectedly (if )

,
,
Failed to parse (syntax error): {\displaystyle P_S=1-P_W-P_L=F_{B\;m,p}(k-1)-F_{B\;m,p}(m-l)=F_{B\;m,q}(l-1)-F_{B\;m,q}(m-k)\text.}

The equivalence of (1), (2) and this form (with ) which expands as

,(3)

can be clearly shown by algebraic transformations, just nobody have done it here yet.

Approximate estimation of the winning chance[]

Binomial distributions are mathematicians' lab mice and are well studied, thus, fast approximate ways to estimate such chances are known. Binomial random values are subject to the central limit theorem, and with sufficiently big approaches normal distribution (first parameter is the mean and the second is the standard deviation of both distributions). The normal distribution is scalable from the standard one with cumulative function

,

so that with the scaling function

,

and thus

.

Since and , we can be almost sure in the battle result if is greater than 3 without extra computations. In the interval, the function is smooth and can be easily approximated by something fast.

The trouble is that if differs much from , the big enough for any credible value of this approximation goes very big. Top estimation of the error is

.

Luckily, for small 's works the Poisson distribution approximation , which parameter equals both mean and dispersion; for big possibilities, just switch with . The function is

,
.

The error is limited by . The Poisson distribution can't be scaled from a single curve, but the row itself is stabilizing fast enough. We can suggest which approximation is better by studying . It's likely for that when it is over 2.3 normal one is better, and when under 1.125, the Poisson one, and in the uncertain gap taking the average of both gives good estimation (especially if the possibility is smaller than 50% since in this region these approximations tend to errate in opposite directions), but this idea requires better mathematical founding. The precision considering this all is about ±4% absolute, and might fit ±0.5% interval for .

Equivalent killer[]

Any attacker is characterized with three parameters - health , strength and firepower (similar ones have each defender; here let's think that ). Two attackers with at least one different parameter won't attack targets in similar manner (prove yourself that the intuitive presumption "twice more fp with twice less strength gives likely similar damage" is false). We can consider an attack on a specific defender: what health should have an attacker of standard strength and firepower to kill it with the same probability as our one. For this purpose, consider a simplified normal approximation (reasonably precise for big ): . The argument of the , say , should be the same. Thus, we get new from the equation where we calculate for a real attacker and take of an actual defender, and take another parameters for our standard attacker

We can clearly see (just move to the left and understand that the bracketed expression is either always positive or always negative) that the "−" solution is false, so

,

or, substituting the properties of the standard unit,

.

This value probably needs rounding to integer and some iterative amendment due to approximation errors, but goes as a quick estimation. For example, to say approximately how many Warriors () are equal to one half-hitpoints Artillery () in attack on an elite Partisan (), we calculate

Precise values (calculated with OOoCalc/Excel function BINOM.DIST, with for Artillery and for Warriors) are 0.689 and 0.635. Really, the best estimated "warrior worth" for Artillery here is 177 ().

We can use the same formula for getting number and/or health (obviously, healthes of attackers with similar fp and strength are additive with rounding up to , i.e. 's are additive) of any specific units which, being sent to conquer a specific target, do it with given probability; for that, we'll use inverted normal distribution function (NORM.S.INV) to get :

Failed to parse (syntax error): {\displaystyle n=\left\lceil l_{s,a_\text{fp}}|_{\Phi^{-1}(P_W)} \over \left\lceil a_\text{hp}/d_\text{fp}\right\rceil\right\rceil,\quad a_\text{hp}=d_\text{fp}\cdot l_{s,a_\text{fp}}|_{\Phi^{-1}(P_W)}} .

In the example above, using this function would give us , and which is slightly more precise but slower to compute. Equivalently kiling attacker can be defined in this way also for units with limited round nuber, but results of their action are so different that it will have less sense.

Note the two things:

  1. We need defender health (and other parameters) to convert attacker health.
  2. We deliberately used an elite unit in the examle above to avoid dealing with veteran status.

Approximation using ratings[]

AI code likes to compare unit strengthes by their (unspecifoc, advisor's) attack/defense ratings . Sometimes the code suggests that and that squared ratings of units acting together (without killstack rule involved) are additive. That is not perfectly precise in general but makes some sense.

Using a robust normal approximation, we get that

.

This function happens to look pretty much alike when but only qualitive alike if this value significantly differs.

Sum of squared ratings has just that sense that it weights stronger units more than just a linear rating sum, so you see not only that one stack is going to beat the another but that many of its units may survive the battle that is usually the actually desired result. Really, if the deviation is relatively small, in a battle of 1 unit against n with the same summary rating usually one unit from either side survives, but for the side of one unit it's 100% and for the other side it's only 1 out of n, so for such battles you have to produce about times more smaller units to keep in line.

The remaining HP[]

Well, maybe you will win. But won't your victory be Pyrrhic? Will your Mech._Inf. after vanquishing Musketeers have enough HP remaining to defend the next turn from a pair of Warriors lurking nearby?

Unlimited match variant[]

Experiment 10v10

Results of the experiment, 10 vs 10.
Artillery hp are 5, 4, 4, 7, 3, 3 (lost ave. 5.67hp, expected 5.97), Partisans' are 8, 4, 6, 10 (lost ave. 13.0, expected 13.75)

Using aforementioned symbols, we can express winning attacker's health points remaining from initial after losing rounds to a defender with firepower

,

here . Probability of having at least hp remained in case of a victory we can express modifying the (2) equation

Failed to parse (syntax error): {\displaystyle \mathrm P(a_\text{hp}'\ge x|\text{win}) = \mathrm P\left(d\le \left\lfloor \frac{a_\text{hp}-x}{d_\text{fp}}\right\rfloor=d_x\text{, suppose }d_x\le m|\text{win}\right)=\\ =\frac{1}{P_W}\sum_{d=0}^{d_x}{k - 1 + d \choose k-1} p^k q^d\text{.}}

The sum looks like the negative binomial distribution , that governs the number of failures in a binomial series before successes, which happen with probability on each test, are achieved:

,

and the probability of losing specific number of rounds

.

Negative binomial distribution has mean ;[3]. If and the mean is moderate, then it can be approximated by Poisson distribution with the same parameter; if , it is closer to normal distribution with a varianse similar to its, which is (and for the distribution becomes geometric regression). But it is defined to the infinity and what we have here has a truncation of results at . The average number of lost round in case of a won battle, due to the properties of the truncated distribution, will be

where the untruncated probability function at specific value

.

Analogically, if the attacker loses, average damage that the defender suffers is

.

Note that equivalent killers are in general not equivalent damagers. In the example above, if Artillery loses, it reduces Partisan's hp (don't forget to multiply on ) at average on 13.75, while hordes of 171, 177 and 180 Warriors do it respectively on 16.4, 16.56 and 16.64, leaving thus like a half less.

If we use the imaginary battle concept, winning after losing rounds is equivalent to winning or more rounds in a -round battle, thus we can also express the cumulative damage distribution via positive binomial function with a sliding parameter

.

Limited match variant[]

Art vs part 15cr

Limited (15 rounds) match variant, same stats: Artillery (7*, 5*, 2, 6*, 2, 1, 5*, 8*, 8*, avg. 4.89, exp. 3.78), Partisans (6, 6, 8, 10*, avg. 7.50, exp. 5.22, some got a glitch in hitbar). * - killed the enemy (expected 7.1% Partisans and 27.5% Artillery).

In battles which both units may survive, the probability of losing or less rounds when this case happens (exactly rounds are fought, ) is

.

This is doubly truncated binomial distribution (only top-truncated if ), mean of which is

The negative binominal winning part, if it exists (), is the same as in the unlimited variant, just goes to a lower point:

.

Thus, the overall chance of remaining alive and with hitpoints after attacking your adversary is (when )

.

The binomial functions here may be approximated for each point in a way similar to aforementioned. If we have a distribution which is tailed up from a head of a negative binomial and a belly of a positive binomial, then the average lost rounds count in case of not dying will be

,

and the variance of a piecewise distribution,[4] denoting means and variances of both parts and their possibilities is

,

where and corresponding moments are calculated for truncated distributions; denoting

- binomial mean shift from lower truncation,
- binomial mean shift from upper truncation and
- negative binomial mean shift from upper truncation[5]

(for general case, coefficiencies not valid for a situation are assigned to zero) we get that

,
Failed to parse (syntax error): {\displaystyle \sigma_1^2 = \sigma^2_{NB[0;m-l] k,p} = \mu_1-(m-k)\left(\frac{kq}p-\mu_1\right)+\mu_1(l+1)\frac{q}{p}-\mu_1^2 =\\ = k\left(\frac{q}p\right)^2-C\left(C+m-(k-1)(1-q/p)\right)\text,}
,
.

Bombardment[]

The bombardment combat is one-sided since the attacker doesn't get any damage in it. It also doesn't kill the defender; if among rounds of it (this number is defined by attacker's bombardment_rate) it loses or more, then it will be left with 1 hitpoint. Thus, while the number of lost rounds converges to the binomial distribution , the lost hitpoints number in case of has a "tail-deflated" law:

the cumulative function for is still for and 1 later, and the average damage is, denoting the overall probability of leaving with 1 hp,

.

Result visualization[]

We can see that in each case there are possible battle outcomes (pairs of units states after a match disregarding veteranship) unless it is bombardment, where the number of defender states is . We can plot possibility of each outcome as a function of extended number of rounds won by the attacker

Here are the charts for the aforementioned Artillery attacking Partisan in an unlimited, a limited and a bombardment combat. Green bars for attacker winning cases, red for losing, and yellow for both survived.

Results of some complex military operations[]

The formulae above were derived for a single attack of one unit on the another. Let's look what happens in little more complicated cases.

N attack 1[]

VetChanceChart

Chances to get veteran ranks for a novice unit after a number of matches survived

Let's consider a simplest case of an action where several units participate: units in one turn attack one defender.[6] The optimal order of attacking a target is a question out of the scope of this article, presume that the order of the attackers is predefined; sending best killer first seems more advantageous for the attacking part for the probability of taking more target's hp before it can get veteran, but unit cost and other considerations may change it. Let each of them has losing limit to defender's defence and attack strength , by the order of attack. What is the chance of the defender to survive the attacks and have hitpoints afterwards?

To answer this question, we must also take into account the probability for the defender who survived an -th match to get a veteran rank and thus a strength bonus;[7] the defender strength against each one is . This rank is in general not independent from since units that have got the status earlier tend to preserve more hp. The formulation for the possibility of given defender state will be recurrent, based on calculation for cases if the defender has got the latest status in the latest match or earlier:

Failed to parse (syntax error): {\displaystyle \mathrm P(d_{\text{hp}n}=h_n, d_{\text{v}n}=v_n) = \sum_{h_{n-1}=h_n}^{d_{\text{hp}0}}\Big(\\ \mathrm P(d_{\text{hp}n-1}=h_{n-1}, d_{\text{v}n}=v_n-1)p_\text{vet}(v_n-1)\mathrm P(w=\frac{h_{n-1}-h_n}{a_{\text{fp}i}}|h_{n-1},s_i, r(v_n-1),l_i)+\\ \;+\mathrm P(d_{\text{hp}n-1}=h_{n-1}, d_{\text{v}n}=v_n)(1-p_\text{vet}(v_n))\mathrm P(w=\frac{h_{n-1}-h_n}{a_{\text{fp}i}}|h_{n-1},s_i, r(v_n),l_i)\Big)} ,

where probabilities of winning rounds for attacker (before not winning the match) is calculated by swapping roles in formulae for above, with zeroes for non-integer parameter values.

Since neither attacker in the series is expected to inflict more then final amount of damage which is less then the defender's health, the actual value of defender's health is not important for this formula (if only there is no bombardment which can possibly beat out in process the last hp); we actually calculate a part of the damage distribution for . If we fix veteran status for each match and thus are known, then the order of the attackers does not have any effect on the final results. During bombardment, no unit can increase its veteran status; so, for a series of bombardments[8] we can apply a formula for the damage they would cause to a unit of endless health and similar strength

and then sum up in one the values that are greater than or equal to and use this number for and the other values for .

Then, since NBD has additive nature, in the case when firepowers and relative strengthes of two or more attackers are similar and matches with them are "win-or-lose", we can think that we have a single attacker with the same round-winning probability as any single unit and a losing capacity summed from theirs. Alas, sum of negative binomial distributed variables with different parameters is not a NBD variable; it's a mixture of a NBD over another distribution which is not good-looking. Then, distributions of the damage by units with different firepowers are stretched in different scales and are not summated as individual ones. The lucky part is that expected values and variances of independent random variables (what the untruncated damage functions are) are additive, so we can get the estimated mean and try to suggest how would the value go so far with (but the actual distribution may be much different from a normal one, so this approximation is unstable).

Specifically, if all matches are unlimited and strengthes for each match are supposed to be known, then we can represent the probability of defeating against all the attackers (with fixed result or in total) as probability to win the rounds we must win to overcome each one , multiplied with the probability of losing them given amount of hp in all possible combinations:[9]

Failed to parse (syntax error): {\displaystyle P_n(b)=\sum\limits_{\sum_{i=1}^n w_i a_{\text{fp}i}=b} \prod_{i=1}^n f_{NB\;q_i,l_i}(w_i) = \\ \;= Q \sum\limits_{\sum_{i=1}^n w_i a_{\text{fp}i}=b} \prod_{i=1}^n {l_i+w_i-1 \choose l_i-1} p_i^{w_i} =\\ \;=Q \sum\limits_{\sum_{i=1}^n w_i a_{\text{fp}i}=b} \frac{\prod_{v=l_{i\min}}^{(l_{i}+w_i)_\max-1}v^{|\{j|l_j\le v < l_j+w_j\}|}}{\prod_{u=1}^{w_{i\max}}u^{|\{j|u\le w_j\}|}} \prod_{i=1}^n p_i^{w_i}}

(formulae for single value, replace on before 's under the sum signs to get cumulative expressions). The recurrent form, however, seems more easy and universal for computing (keeping only as much arrays of reals of length as there are possible veteran ranks plus one buffer is needed to store probabilities of each possible state on any step).

For example, in a common case when we can expect one veteran status change with probability which changes combat chances from to we have to calculate separately matches before and after the status change for watever turn in the middle it happens, and the defender surviving probability will be

Failed to parse (syntax error): {\displaystyle \displaystyle{P_\bar W=(1-p_{\text{vet}I})^{n-1}\sum_{j=0}^{k-1}{L+k-1\choose j}p^j q^{L+k-1-j}+\sum_{i=1}^{n-1}\Big(\\ \;(1-p_{\text{vet}I})^{i-1}p_{\text{vet}I}\sum_{w=0}^{k-1}{L_i+w-1\choose w-1}p_I^w q_I^{L_i} \sum_{j=0}^{k-1-w}{L-L_i+k-1-w\choose j}p_{II}^j q_{II}^{L-L_i+k-1-w-j}\Big)=\\ \;=(1-p_{\text{vet}I})^{n-1}F_{B\;L+k-1,p_I}(k-1)+\\ \;+p_{\text{vet}I}\sum_{i=1}^{n-1}(1-p_{\text{vet}I})^{i-1}q_I^{L_i}\sum_{w=0}^{k-1}{L_i+w-1\choose w-1}p_I^w F_{B\;L_i+k-1-w,p_{II}}}\text,}

where . As you can see from the chart, novice units have around one change expected after surviving 1 to 3 matches, two after 4 to 8, and since then they are more likely Elite than not (about 99% likely just after they have survived their 24th battle). Thus, some least possible combinations we can filter out on early stages of computing (especially ones with higher , since combinations can receive probability growth during iterations only by "shifting" from bigger hp).

Approximation[]

This page is in development. It may lack necessary details and contain information that is not completely accurate.

Without any more sophisticated ways to approximate sum of NBD variables, an approximation using Poisson instead of NBD may be used to estimate the outcome much faster. Denoting untruncated NBD mean for each attacker , we can consider that . The brilliant property of Poisson distribution is that it is additive: if , then . Thus, we can sum up in one all attackers with same firepower; and, since we rarely have in the game firepowers other than 1 and 2, we can reduce the combination of numerous regular attackers to one or two. Some attackers with binomial or mostly binomial damage law also can be with a reasonable precision approximated by Poisson function with , the limitations of this approximation are discussed above (and units with can be considered unlimited); other units can't be grouped such way but may get fast normal or other approximation. In a group of "Poissonable" attackers,

Failed to parse (syntax error): {\displaystyle P_n(b)\approx\sum\limits_{ \sum_{f\in\{a_{\text{fp}i}\}}fw_f=b } \prod_{f\in\{a_{\text{fp}i}\}}f_{P\;\sum_{a_{\text{fp}i}=f}E_i}(w_f) =\\ \;= \exp{\left(-\sum_{i=1}^{n}E_i\right)} \sum\limits_{\sum fw_f=b} \prod_{f\in\{a_{\text{fp}i}\}} \frac{(\sum_{a_{\text{fp}i}=f}E_i)^{w_f}}{w_f!}=\\ \;= \exp\left(-\sum_{i=1}^{n}E_i\right) \sum\limits_{\sum fw_f=b}\frac{ \exp \sum_{f\in\{a_{\text{fp}i}\}} w_f \ln \sum_{a_{\text{fp}i}=f}E_i }{ \prod_{u=1}^{w_{f\max}}u^{|\{j|u\le w_f\}|}} \text.}

For some better precision, we can also calculate separately the sum of damage by "oneshotable" attackers for which the Poisson approximation works worst and straight calculation works best; for a group of such variables with similar fp we can easily get the losing rounds probability component from the recursion .

We can also make a rough but quick estimation of winning chance recursively converting the first attacker to fp and strength of the second (avoiding rounding when possible), adding to the defender's health the points the equivalent one takes at average more then the original and combining the first two defenders into one, and take for each step average veteran bonuses, calculated from sum of corresponding bonuses, multiplied on veteran level possibilities; but the precision of such a procedure is not studied yet and seems to be sometimes surprisingly low.

Prolonged 1v1[]

This page is in development. It may lack necessary details and contain information that is not completely accurate.

Consider now two units with limited combat rounds standing against each other as long as they both live. As you can see on the charts above, units with comparable strength have rather large chance to survive both the attack. After one or two attacks, the attacker will be run off of ovement points/attack limit; then the defender may counter-attack, or just stay in place and wait his hp regenerate a little (and if he stays in a city with Barracks, his hp will be full next turn). And if the battle dures and dures, even unit which has just attacked is considered unmoved and has some HP regeneration, and UNO wonder gives hp anyway[10]. If both are using optimal strategies against each other, the battle might dure long, or will end in a true stalemate if, when both regenerate full hp, their attack odds show up too big to try.

When we consider operations that involve several consequent actions, we need take in account the desirability of the goal for the player regarding the undesirability of obstacles. For each player there is some value for the fact their own unit survived and a price for another player's unit's head (say, for player X against player Y they are ); if one of the unts protects a valuable city, the prices can be much different even if the units' stats are similar. Even if they don't change much in time, a unit spending turns in this battle probably consumes city resources and/or could do something more useful in another place; thus, if an action which may take several turns is planned, its price should be amortized ("amortization" is a common word arond AI code though it is poorly commented). Another thing is the initiative: if both parts have better odds attacking then defending, one who pushes the button first and has faster modem (or is AI) wins it. Also, the number of attacks possible in one turn for many units (that have more than 1 mp, "DamageSlows" flag and no "OneAttack" flag) is proportional to their health; and, if it happens that they have less than 1 full mp, they may be subject to the "tired_attack" effect: their strength falls proportionally. And if the things were not yet complicated enough, if some of the units sees once that the stand turns bad for him in either attacking or defending, it can leave the battlefield and run away/go to solve any worthy problem (if it has yet enough speed or the opponent can't/doesn't want to chase).

Last variant aside, we can assume the system of two units' state as integer eight-dimensional vector of hp, mp, fortified state and veteran status for each unit, on th turn it is . In the sequence of such vectors, each step means X attack, Y attack or turn change. Attacks lower the attacker's mp on a constant (or less down to 0), then lower hitpoints for both with distributions discussed above with some probability of expelling, then reduce movement points according to DamageSlows and possibly give the survivor(s) veteran growth; for them to happen, the attacker must have nonzero mp and a desire to attack this time; and if these conditions are met simultaneously for both units, attacker must win the initiative. The turn change happens when the conditions are unmet for both sides, it restores the hp and the mp (to the hp-related maximum). Except for the initial state, we can be sure that a fortifiable unit which ended previous turn with nonzero mp and has not attacked yet this turn is fortified (because why not).

For not taking too much in scope, we can assume that the preferability of attack on each step depends only on the units' actual state; then the sequence with total probability 1 in a finite number of turns either ends in death of one of the units or stabilizes in a true stalemate (in both cases veteran status outcome(s) might be different). So, if X considers attack on step 0 when the system is in the state and has planning horizont (let us express it in steps rather than in turns for a bit of simplicity), X's total expected amortized income from this action is

Failed to parse (syntax error): {\displaystyle c_{aX}(S_0)=P_{WXa}(S_0)c_{YX}(1)-P_{LXa}(S_0)c_{YX}(1)+\\ +\sum_{\{S_{1Xa}|h_X>0<h_y\}}\mathrm{P}(S_1=S_{1Xa}|\text{in }S_0 \text{ X decided to attack})\sum_{i=2}^{i_\max}\sum_{\forall S}\mathrm{P}(S_{i-1}=S|S_1=S_{1Xa})\left(P_{WX}(S)c_{YX}(i)-P_{LX}(S)c_{XX}(i)\right)} ,

where are probabilities for X to win and lose after pre-battle condition (observing chances and wills of both sides to attack); are battle outcome probabilities on step 1 if X considers to attack regarding chances and will of Y to intercept. The expected income from deciding to keep on fortifying in the same situation can be expressed in similar way; Y can't disallow X to fortify but considers attacking or also fortifying. If one of the expected incomes is positive and is not less than the another, the unit will take that tactic; if they both are negative, then the unit will try to leave the battle.

The probabilities of given states on given turns are calculated recursively - they are derived from the previous state starting from presumed both-survived result after step 1 ; but to know the process of each step one must first know will the participants attack or leave the battle after given states. That means, if the values of each unit for each side on each turn is known to both sides, then for any state theoretically reachable from the starting conditions there are 4 equations for 4 unknown variables (expected incomes), and signs of differences in two pairs of them govern all the equations for the conditions this condition is accessible from in observed number of steps, itself inclusive.

Footnotes[]

  1. Note that actually inside the game unit class strength values are multiplied on POWER_FACTOR from common/combat.h which is 10 and then are operated in integer numbers with rounding down on each step, thus hardened (175% bonus) warrior (basic strength 1) actually has strength 17 against novice who has 10.
  2. The round limit can potentially be influenced by unit health (Combat_Rounds effect with MinHitPoints requirement), but it is calculated once before the combat starts; see max_rounds variable in unit_versus_unit() function in server/combat.c.
  3. Thus, defender rating, the third (after and unit cost) parameter which is taken into account to find the defender in a stack, is proportional to the untruncated distribution mean and serves as the estimation of how much damage the defender causes to the attacker. Seeget_defender in common/combat.c.
  4. This is effectively a mixture of two variables with non-intersecting definition regions, i.e., when we survive, we get NB-distributed damage with some known probability and binomial in the rest of the cases.
  5. Note that
    .
  6. The opposite case, when an attacker attacks several units this way, is obviously rare for the limited number of movement points and/or attacks of the attacker - the targes will just run away, strike back or get some aid.
  7. To spare the game of too much veterans, a setting only_killing_makes_veteran is introduced in Freeciv 3.0.
  8. If we analyze the worst case for the defender, the bombarders come first and the calculated distribution comes on the input of general formula.
  9. The number of such combinations goes from "variants of change for a dollar" problem just with coins minted in different years.
  10. See hp_gain_coord() and unit_restore_hitpoints() in server/unithand.c for the formulation, or the table in Combat#Aftermath; briefly, the HP addition is partially proportional to the unit's maximal health with rounding of percentage with a coefficiency dependent on the unit's activity, partially is constant. On some (hardly ever played) settings units will gain hp faster then they can possibly lose them, then they should decide not to start the battle.

Literature[]

  • Norman L. Johnson, Adrienne W. Kemp, Samuel Kotz.

Univariate Discrete Distributions. 2005

  • Shonkwiler, J.S., Variance of the truncated negative binomial

distribution. Journal of Econometrics (2016)

  • Ken Butler, Michael Stephens. The distribution of a sum of binomial random variables. 1993
  • Furman E. On the convolution of the negative binomial random variables. 2006
Advertisement