Somehow, retreats and hps lost aren't being calculated properly. The final attacker wins, defender wins, and draw probabilities also seem to be affected, I think.
Consider a 2 hp veteran knight attacking a 2 hp regular spear fortified in a town on grassland. This means a modified defense of 2.70, we agree. Thus, the attacker wins 4/6.7=59.7% of the rounds. I think we agree on that.
By your calculations with Oystein, the chance of a retreat, should one occur, is exactly 58% -- 58/(50+50), since the knight is vet and the spear regular. I think we agree on that (unless there's been more recent research I've missed...in which case, where is it?).
Let's go through all the possibilities. With only 2 hps, we can list them all out. I'll use A to mean attacker wins a round and D to mean defender wins a round, so AA means attacker wins and then attacker wins. [Note, I used more exact numbers but am only showing a few to save typing time.]
AA - Happens with probability .597^2 = 35.64% (AHPL = 0, attacker wins)
ADA - No retreat chance since defender at 1 hp when attacker reaches 1 hp - Probability .597*.403*.597 = 14.36% (AHPL = 1, attacker wins)
ADD - No retreat chance - Probability .597*.403*.403 = 9.70% (DHPL = 1, defender wins)
D* - The * is a successful retreat - Probability .402*.58 = 23.37% (AHPL=1, DHPL=0, DRAW)
D*AA - The * is a failed retreat. Since the defender still had 2 hps, the knight rolls to retreat. In this case, it failed, leading to this result - Probability .402*.42*.597*.597 = 6.03% (AHPL = 1, attacker wins)
D*AD - * is a failed retreat - Probability .402*.42*.597*.402 = 4.07% (DHPL = 1, defender wins)
D*D - * is a failed retreat - Probability .402*.42*.402 = 6.82% (DHPL = 0, defender wins)
Total probabilities....
A wins - 35.64 + 14.36 + 6.03 = 56.03%
D wins - 9.70 + 4.07 + 6.82 = 20.59%
DRAW - 23.37%
(AHPL = 0) - 35.64%
(AHPL = 1) - 14.36 + 23.37 + 6.03 = 43.76%
(DHPL = 0) - 23.37 + 6.82 = 30.19%
(DHLP = 1) - 9.70 + 4.07 = 13.77%
The A wins, D wins, and DRAW match your combat calculator, but you show 43.972% for (AHPL = 0) and 35.440% for (AHPL=1), for example, which just has to be wrong.
Also, changing the attackers hps changes the odds of (AHPL=0), which is wrong, because that's always just the chance the attacker wins two consecutively and MUST be the same for all values of attacker hps. Indeed, setting attacker hps to 1 gives 35.643%, which is what (AHPL=0) must be for any attacker hp value (changing nothing else).
You need to look at your retreat logic again. If I can help, let me know.
This simple example doesn't show the error in attacker wins, defender wins, DRAW, but I think it's there and probably the same bug. For example, a 4 hp vet knight attacking a 3 hp regular spear with same terrain, I get 78.4% attacker wins and you get 79.4% with our defensive wins off by similar amounts.
Arathorn