Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need to know the best way to calculate an outcome between 2 types of units. Each unit has 3 factors: Health, AttackPower, Defense

There is also a last factor of anti-troop which says that one trooper is better against a certain type. So this factor must be taken accounted for.

Can someone help me with this ?

Kind regards

Example

Unit1(attacker):

 Amount: 40
 Health/Unit: 10
 AP/Unit: 5
 Defense/Unit: 2

Unit2(defender):

Amount: 20
Health/Unit: 10
AP/Unit: 5
Defense/Unit: 2

How can I get a right outcome of who wins and with how many units left.

share|improve this question
1  
What exactly is the "right outcome"? As the games designer, it's your job to define which unit has which success chance against which other unit under what conditions. We could help you to find the right numbers and formulas to get these results, but comming up with the gameplay is your job. –  Philipp Feb 12 '13 at 14:44
    
That's what I need, the right formula to get the result :) –  Kaizer Feb 12 '13 at 15:00
3  
but what is the result you want? Do You want unit 1 to win always or only with a high propability? Should the victorious unit also suffer damage, and if so how much? What about units with other hp, attack and defense values? Like a 10/2/3 vs. a 10/3/2 unit, or a 20/2/2 vs. a 10/2/4? These are decisions we can't make for you. –  Philipp Feb 12 '13 at 15:05
    
Thanks for your time Philipp. I know what to do now –  Kaizer Feb 12 '13 at 19:34
add comment

closed as not constructive by Josh Petrie, Sean Middleditch, Tetrad Feb 12 '13 at 23:55

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

up vote 4 down vote accepted

Based on the factors provided, a simple way to handle this would be:

EnemyHealth -= (PlayerAttackPower * Boost) - EnemyDefense 

Where Boost would be the percentage of attack power boost you would get based on the unit you are attacking.


Edit

In the case of multiple units, I would do something like this:

// keep attacking until someone is wiped out
while (Players.Count > 0 || Enemies.Count > 0)
{       
    //Loop through all player units
    for (int i = 0; i < Players.Count; i++)
    {          
        //Loop through all enemy units
        for (int j = 0; j < Enemies.Count; j++)
        {
            //Get ratio of Player units vs Enemy units
            double PlayerRatio = Players.Count / Enemies.Count;
            double EnemyRatio = Enemies.Count / Players.Count;

            //Player attacks enemy
            Enemies[j].Health -= (Players[i].Attack * PlayerRatio * Boost) - Enemies[j].Defense;

            //Enemy is dead, remove him
            if (Enemies[j].Health <= 0)
            {
                Enemies.RemoveAt(j);
                j--;
            }
            else    //enemy still alive, attack back at player
            {
                 Players[i].Health -= (Enemies[j].Attack * EnemyRatio * Boost) - Players[i].Defense;
                 if (Players[i].Health <= 0)
                 {
                      Players.RemoveAt(i);
                      i--;
                 }
            }
        }
    }
}
share|improve this answer
    
Hey Jon, thanks for replying. I edited my question a bit to make it more clear. With your calculation I cannot see how many units are survived and how many where destroyed. –  Kaizer Feb 12 '13 at 14:31
1  
that would depend on the input status of each force i.e. how much health did they have at the start, ultimately you would be looking to do a tick by tick until a result was determined, if you have multiple troops you will need to decide how they are used, do they wait in reserve for each one on one battle to complete or can you have a 2 on one fight, who does the attacker attack first the defender with low health or the defender with high health you need to visualise the battle occurring and the necessary steps and complexities involved also is this an rts or some form of automated rpg –  Yakyb Feb 12 '13 at 14:37
    
to put it simple: there are multiple players. You can attack them by sending troops out to them. When they arrive your troops should fight his troops and an outcome is decided (win/lose). A report is needed afterwards with how many troops were killed. It hasnt to be 2 complexed like attack a defender with low health and stuff. –  Kaizer Feb 12 '13 at 14:40
    
Does a troop contain the same kind of units? –  Joetjah Feb 12 '13 at 14:43
1  
+1 for the added code, well thought of! –  Joetjah Feb 12 '13 at 15:08
show 8 more comments

I'm just throwing up a ball here:

Say you have two players. They have the stats HP/AP/Def.

Player 1: 100 / 15 / 10

Player 2: 100 / 22 / 7

Player 1 is a trooper, player 2 is an anti-trooper.

You could be doing a 'hit' each attack. Say player 1 goes first, and hits player 2 with 15 AP. Player 2 has 7 Def, so 15-7 = 6 damage inflicted. But, Player 2 is an anti-trooper, so he gets a x-2 bonus. Player 2 receives 6/2=3 damage, HP left: 97.

You can switch around with the values a bit, or the bonusses gained from being a trooper/anti-trooper. Another thing commonly done is to use the defense not as flatout damage reduction, but as a percentage reduction of the damage gain.

share|improve this answer
    
thanks Joetjah. But now we are talking about 1vs1 unit. Isn't it going to be taking to long when there are 100000vs100000 units ? –  Kaizer Feb 12 '13 at 14:33
add comment

As Joetjah writed, Player 1 will cause 3 damage per hit (pl_1_dph), and Player 2 will cause (22 - 10) = 12 damage per hit (pl_2_dph).

So, 1 unit of Player 1 requires 100 / 3 ~= 33 hits to kill 1 unit of Player 2 (pl_2_hp / pl_1_dph).

100 units of Player 1 will kill 200 units of Player 2 within

(pl_2_hp * pl_2_units) / (pl_1_dph * pl_1_units) = (100 * 200) / (3 * 100) = 20000 / 300 ~= 66 hits

200 units of Player 2 will kill 100 units of Player 1 within

(pl_1_hp * pl_1_units) / (pl_2_dph * pl_2_units) = (100 * 100) / (12 * 200) = 10000 / 2400 ~= 4 hits

As you see, troops of Player 2 will kill all forces of Player 1 within 4 hits. Within that time, Player 1 also attacked 4 times, and will cause 4 * pl_1_units * pl_1_dph = 4 * 100 * 3 = 1200 damage total, which means, that 1200 / pl_2_hp = 1200 / 100 = 12 units of Player 2 will be killed.

Summary: with 100 units (dph = 3) of Player 1 and 200 units (dph = 12) of Player 2, Player 2 will win within 4 steps (hits) and loose only 12 his units.

Algo:

1) compute damage per hit for player 1 and 2 (dont forget "anti-" bonus)

2) compute minimum hits, required for each player to completely kill enemy army

3) compare required hits to choose the winner (as required_hits)

4) compute damage, caused by looser to winner within required_hits hits to decide, how many forces will loose the winner (all units of looser's army was killed)

share|improve this answer
    
this algo isn't very precise. More accurate results can be received with iterative algo: recalculate total damage based on DPH each hit using new numbers of remaining forces, but with small DPH and large armies you will loose some CPU time. It's not important in single-player, but may be important in multiplayer –  ankhzet Feb 12 '13 at 19:19
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.