# Battle Algorithm

### From Conquest Wiki

To conquer the world, one has to win battles; to win battles, one should understand how they are decided.

## The Algorithm

At the beginning of a battle, for each participating army its power against troopers, tanks and bombers is calculated.

- Let's assume a player's fighting army consists of a trooper and a tank. In plains, its power against troopers is 1 + 2 * 1.5 = 4, against tanks 1 * 0.75 + 2 = 2.75 and against bombers 1 * 1.5 + 2 * 0.75 = 3.

Next, the algorithm determines against how much power each player is up against. This is done by taking the percentage of trooper, tank and bomber power in a player's army, multiplying each one with the combined enemy trooper, tank and bomber power, and adding up the result.

- Our army of trooper and tank is up against a bomber, which has a power against troopers of 3 * 0.75 = 2.25 and against tanks 3 * 1.5 = 4.5. So the power we are up against is 1/3 * 2.25 + 2/3 * 4.5 = 3.75, as our power 3 army consists of a trooper with power 1 and a tank with power 2. The bomber, on the other hand, is up against a power of 3 (which is simply the combined power of a trooper and a tank versus a bomber).

The player who is up against the least power, wins the battle. If this value is shared between multiple players, all units are lost.

- In the earlier example, trooper and tank are up against a power of 3.75, while the bomber is up against a power of only 3. This means, the player who controls the bomber wins the battle.

Now the winner's surviving power is determined. This is simply done by dividing the winner's least power-up-against value by the second least power-up-against value, subtracting this value from 1 and multiplying the result with the total power of the winner's army. If both power-up-against values are identical, the winner's power-up-against value is scaled down by a small fraction before the division.

- We divide the bomber's value of 3 by the trooper and tanks value of 3.75, which results in 0.8. Subtracted from 1 and multiplied by 3, the winner's surviving power in our earlier example is 0.6.

The winner's surviving power can be seen as some sort of budget, from which he now buys back units (the survivors). The budget will get reduced by the power of the bought back unit and until the budget is zero of negative. At least one unit will get bought, no matter how small the budget was initially. The type of the returned unit (trooper, tank or bomber) is based on which unit type had the least power up against it. Also, the algorithm tries to keep the ratios between the types fairly intact: If your tanks were up against twice the power your troopers were up against, twice as many troopers are supposed to survive, if possible (based on initial numbers).

- In the trooper and tank vs. bomber scenario, the buyback process is simple. We buy a bomber (the only type of unit we have anyway), the budget goes from 0.6 to -2.4 and we're done.

- Let's assume we had two troopers and two tanks versus this bomber. While troopers and tanks are still up against a power of 3.75, the bomber's power-up-against value is now 6 (2 * 1 * 1.5 + 2 * 2 * 0.75). This means the bomber loses the battle, and, after applying the above calculations, the winner's surviving power results in 2.25. As the troopers were up against a power of only 1/3 * 2.25 = 0.75 and the tanks against 2/3 * 4.5 = 3, we're aiming for a trooper to tank return ratio of 4:1; but with only two troopers fighting for us in the battle, we simply buy both, reducing our budget from 2.25 to 0.25. This result is neither negative nor zero, so we also get a tank back (budget goes from 0.25 to -1.75) and now the algorithm terminates.

## Army Composition

There's one thing that might seem a bit unintuitive: Adding more units to an army doesn't necessarily mean it becomes stronger against a certain other army. If you add units of a type against which the enemy army is especially effective, your overall power decreases and your chance of losing the battle actually increases. This makes the process of dispatching troops more challenging, but also more interesting.

- A trooper fighting a bomber in mountains draws, and both are destroyed (both units have a power of 2.25 against each other). However, if this trooper fights in formation with a tank, the bomber will win the battle (power-up-against of 3.375 for the bomber vs. 3.75 for trooper and tank). The bomber simply has a devastating effect on the enemy tank (which forms the main body of the army with 2 out of 3 power) in mountains and the trooper can't utilize his mountain strength when fighting side by side with a tank.