# Calculating the output of two armies fighting

I am programming a strategic game using Flash. The game works very similar to the famous game "Travian".

My problem is as follows: I am trying to make the calculation of the troops lost as a result of a fight between two armies. The two armies have different types of units. Some of them are stronger against some other units and weaker against other types.

How can I put that effect of this differences in the equation of the fight?

It seems to be easy if they only have att and def points only, but when it comse to the units type dependency, I am lost.

• Should your battles' outcome be purely deterministic or do you want to use some kind of randomness? Commented Dec 7, 2010 at 10:52
• I don't think it should be tagged as a multiplayer game since that isn't given by the OP-it could be human vs AI. And by 'type dependency', are we talking the classic rock->scissors->paper->rock type thing? Commented Dec 7, 2010 at 16:53

In addition to supporting Amit's suggestion of looking at the Lanchester equations, I just want to add that this is a game design decision, not an empirical fact that we can give you. If you want to take unit type into account, you have to decide what that means. This means choosing an equation that includes all the factors that you want your gameplay to include. If you want infantry to be better than cavalry, then you have to decide what that should mean - eg. how many cavalry do you need to equal 100 infantry? And does it matter who attacks who? You seem to be implying that simply giving infantry and cavalry different attack and defence values isn't good enough - why is that? What else are you trying to represent that can't be captured just by those values?

You have to decide which factors you want to model in your game, as they affect the way the players will approach it. These might include unit size/quantity, unit type, unit experience (eg. veteran status), terrain and environmental effects, differences between attacking and defending if any, whether to model damage and attrition or not, whether to model the passage of time during the combat, the ability to withdraw or flee (possibly including modelling of morale), how much randomness you want in the equation, and so on.

Once you know all this, there are several basic mathematical approaches you can take. You could do a round by round "chance to hit" system like many RPGs have, eg. the d20 combat system. You could do a one-round "attack vs defence" weighted coin toss system like the original Civilisation game does. You could have each side generate a score by adding attributes to a random number and whoever gets the highest value wins. And you can permute these systems to work on a round-by-round basis, or to deduct hit points or morale points, or whatever. Any system can work, but you have to balance it the way you want it to play. As ultimately the choice of how to model the combat is a key part of the game design, and is not something other people can just give to you.

• You are completely right Kylotan. I had some ideas about the fight before starting the design of the game. However, I posted this question asking if there is a better way to design it and find out what is the best way to implement it. I won on the both purpose I guess :) My friend, Could you please tell me where I can find more information about those Mathematical approaches? Also, if I answers all the questions you ask about choosing the system, can you advice me which one work best for my case? Thank you very much for your time :) Commented Dec 11, 2010 at 7:31
• I can't tell you which will be best, but you should probably start simple and improve on that if you need to. Travian seems to have a simple attack vs. defence system, with 2 unit types (infantry and cavalry) and 2 defence scores per unit accordingly. An easy way to resolve a battle, as used in Civilization 1, is to divide the attackers attack score by the total of that plus the defender's defence score. This gives you a percentage. Now pick a random number between 0 and 100 percent. If it's lower than the attacker's attack score, the attackers win. Otherwise the defenders win. Commented Dec 11, 2010 at 14:20

For Solar Realms Elite I was inspired by Lanchester's Equations for modeling warfare. I had several simultaneous fights in each round of battle.

In the first fight, everyone attacked soldiers. In SRE soldiers are best against soldiers (it's not rock paper scissors, but infantry, air attack, and deep space). I set up an attack and defense power where soldiers had the best attack:

attack_strength = 3*soldiers + 1*fighters + 2*cruisers
defense_strength = 10*soldiers


In the second fight, everyone attacked the defense stations. In SRE fighters (air) are best against defense stations (e.g. anti-aircraft):

attack_strength = 1*soldiers + 4*fighters + 2*cruisers
defense_strength = 25*defense_stations


In the third fight, everyone attacked the heavy cruisers. In SRE heavy cruisers are in space and are best against other heavy cruisers:

attack_strength = 1*soldiers + 1*fighters + 10*cruisers
defense_strength = 15*cruisers


(I don't remember what constants I used; those are just examples.) In each round of battle the attackers would lose some fraction of the defense strength and the defenders would lose some fraction of the attack strength. I believe this corresponds to Lanchester's Square Law (equations here). I had added randomness but I don't remember exactly where. After each round of battle the armies would be smaller. I put a maximum limit on the number of rounds; after that, the losing side would retreat.

It wasn't realistic to have the infantry on the ground firing way into deep space, but it worked better for gameplay reasons to have all units be able to fight all other units (at reduced effectiveness).

I tend to say ' if you can't find an explicit solution, seek an implicit one'. You could simulate the battle internally until one army is wiped out or retreats(depending on your game's possible outcomes).

I'd use something like this:

For each iteration of the battle, all units are opportunistic, so they try to do the most damage they can. Each unit selects an enemy unit it is going to attack this round, based on the known advantages/ disadvantages.

Then, all subfights are performed. An example:

Let spearmen be effective against cavalary, cavalaray effective against archers and archers effective against spearman.

In a fight between two armies consisting both of al types of these basic units, all spearman would attack cavalery, all cavalery units would attack archers and all archers would attack spearmen. If for example one side had no archers, the other sides cavalery would select the next best type of target (being the enemy cavalery units)

Each unit-attacks-unit event is resolved seperatly, with the losing unit being damaged or marked as destroyed.

After all the individual fights have been resolved, remove all units which have been critically damged or destroyed.

The next iteration starts using the now reduced armies.

• Really agree with this post. Better to iterate through the battle instead of reducing it to a single equation. And I'd definitely add a bit of randomness to it. There ought to be that ever so slight chance that the weak defender rolls high and scores a critical. Another advantage of iterative is that you can create a narration of the events to show the player. "The swordsmen attacked and soon decimated the footmen, but then the calvary arrived and..." Commented Dec 7, 2010 at 21:39
• I like your answer very much, and I think I will implement it with randomness :) Commented Dec 11, 2010 at 7:15

I'm beta-testing a game that is currently a simple version of Solar Realms (Star Empire Elite), and I started by using something similar to Amit's equations (above). In particular, I liked the idea of having three phases to the battle, where you had to win two of the three. But I also wanted to introduce an element of randomness into the battle, and for that I was influenced by some tabletop games.

Processing is a concern if the game is to scale, so I didn't want to follow the method suggested by sum1stolemyname above, although if your game is using the client to process the results, as opposed to a server, this seems to be a good approach.

I decided to break the battle into two phases (analogous to the three in Amit's model): air and ground. I calculate the attack and defend strength, and adjust the attack strength down by a fraction (to give the defender the advantage). At that point, if the attack strength and the defend strength are equal, the attack has a 50% chance of victory. From there, I adjust the percentage chance of victory up or down based on how much more (or less) strength the attacker has compared to the defender. Here are some simplified equations for air:

air_attack_strength = 1 * soldiers + 10 * fighters
air_defence_strength = 2 * soldiers + 25 * stations

differential = (air_attack_strength - air_defence_strength) * constant

chance_of_victory = 50 - differential


I specify that the chance_of_victory can never be larger than 80 or less than 5. From there, I just generate a random number out of 100, and then carry forward that result to the land battle.

One thing I have not solved to my satisfaction is the casualty rates. But I'm thinking that a good way to figure this out would be to compare the chance_of_victory to the random number generated, and use that to take a fraction of the attacking-defending forces as casualties.