stachnie
Theorist
It deals with ratios of unit strength totals. Damage ranges each round from MISS to 6-40 or so (I think - I'd have to look to say for sure). Now there are manipulations from the original formula to account for a number of new effects as well... get's quite intricate.
O.K., I think I've caught it. Damage ranges make it more complicated than I thought, but this is still manageable.
We have two units: attacker (base strength A) and defender (base strength D). Maximum HP for A is AHPmax, for D is DHPMax; initial HP at the start of combat (because they don't need to be at full strength) will be AHPinit and DHPinit; actual HP at the beginning of the round will be AHP and DHP.
Total attack of the attacker will be Atot=A*(1+bonuses-penalties)*(AHP/AHPmax) and total defence of the defender will be Dtot=D(1+bonuses-penalties)*(DHP/DHPmax). A chance to win or lose each round perhaps will be governed by the ratio of Atot/Dtot:
Atot/(Atot+Dtot) to win
Dtot(Atot+Dtot) to lose
Bonuses and penalties may depend on the round, so I will define a set of functions
pn(i,AHP,DHP)
for n=1 to ntot which represent outcome n for the round i. The outcomes may be:
p1(i,AHP,DHP) - A wins but misses
p2(i,AHP,DHP) - A wins and takes minimum possible number of HP
p3(i,AHP,DHP) - A wins and takes minimum possible number of HP + 1
...
- A wins and takes maximum possible number of HP
- A wins but D retreats
- D wins but misses
- D wins and takes minimum possible number of HP
- D wins and takes minimum possible number of HP + 1
...
pntot-1(i, AHP,DHP) - D wins and takes maximum possible number of HP
pntot(i, AHP,DHP) - D wins but A retreats
p1(i,AHP,DHP)+p2(i,AHP,DHP)+p3(i,AHP,DHP)+... should add up to 1.0 for each i, AHP and DHP.
Now we define two arrays representing the state of the battle, S0 and S1. They will be arrays of real numbers indexed (-1...AHPinit) and (-1...BHPinit): these indexes represent actual HP of the attacker and defender. 0 means that the battle is lost, -1 means that the unit retreated.
Before the fight: all elements of S1=0, almost all elements of S0=0, S0(AHPinit,DHPinit)=1.
i:=1 {the number of the round}
@NextRound
FOR k:=1 to AHPinit do
FOR l:=1 to DHPinit do
FOR n:=1 to ntot do
BEGIN
{of course, for a given n only one case is chosen, so the last FOR loop may be replaced by explicit calls of all subsequent expressions with all possible pn's}
{if A loses some HP -> I will call it HPlost}
S1(k-HPlost,l) := S1(k-HPlost,l) + pn(i,k,l)*S0(k,l)
{if D loses some HP}
S1(k,l-HPlost) := S1(k,l-HPlost) + pn(i,k,l)*S0(k,l)
{if A or D wins but misses}
S1(k,l) := S1(k,l) + pn(i,k,l)*S0(k,l)
{if A retreats}
S1(-1,l) := S1(-1,l) + pn(i,k,l)*S0(k,l)
{if A loses and k<=HPlost -> A loses the fight}
S1(0,l) := S1(0,l) + pn(i,k,l)*S0(k,l)
{if D retreats}
S1(k,-1) := S1(k,-1) + pn(i,k,l)*S0(k,l)
{if D loses and l<=HPlost}
S1(k,0) := S1(k,0) + pn(i,k,l)*S0(k,l)
END
FOR k:=1 to AHPinit do S1(k,0):=S1(k,0)+S0(k,0) {add all cases when D lost earlier}
FOR k:=1 to AHPinit do S1(k,-1):=S1(k,-1)+S0(k,-1) {add all cases when D retreated earlier}
FOR l:=1 to DHPinit do S1(0,l):=S1(0,l)+S0(0,l) {add all cases when A lost earlier}
FOR l:=1 to DHPinit do S1(-1,l):=S1(-1,l)+S0(-1,l) {add all cases when A retreated earlier}
i:=i+1
S0:=S1 {copy whole array}
S1:=0 {clear the array}
GOTO @NextRound
The loop ends when for all l=1...DHPinit and k=1...AHPinit S0(k,l)=0 so the only nonzero elements are S0(k,-1), S0(k,0), S0(-1,l) and S0(0,l).
Probability that A wins: the sum of all S0(k,0)
Probability that A wins but D retreats: the sum of all S0(k,-1)
Probability that D wins: the sum of all S0(0,l)
Probability that D wins but A retreats: the sum of all S0(-1,l)
However, usually this will be an infinite loop, because when A or D misses and misses (no loss of HP), it may be repeated again and again. In order to avoid that, it is necessary to include some condition for the cases when A or D misses, e.g. if pn(i,k,l)*S0(k,l) gets below some limit (e.g. 1/(AHPmax*BHPMax*1000000), it gets assigned to the situation when A or D wins but with minimal HP loss for the opponent.
Uff! I think that if I had made no stupid mistakes and if I did not overlook something, it should work.
S.