# Ideas for attack damage algorithm (language irrelevant)

I am working on a game and I need ideas for the damage that will be done to the enemy when your player attacks. The total amount of health that the enemy has is called enemyHealth, and has a value of 1000. You start off with a weapon that does 40 points of damage (may be changed.) The player has an attack stat that you can increase, called playerAttack. This value starts off at 1, and has a possible max value of 100 after you level it up many times and make it farther into the game. The amount of damage that the weapon does is cut and dry, and subtracts 40 points from the total 1000 points of health every time the enemy is hit. But what the playerAttack does is add to that value with a percentage. Here is the algorithm I have now. (I've taken out all of the gui, classes, etc. and given the variables very forward names)

double totalDamage = weaponDamage + (weaponDamage*(playerAttack*.05))
enemyHealth -= (int)totalDamage;


This seemed to work great for the most part. So I statrted testing some values...

//enemyHealth ALWAYS starts at 1000
weaponDamage = 50;
playerAttack = 30;


If I set these values, the amount of damage done on the enemy is 125. Seemed like a good number, so I wanted to see what would happen if the players attack was maxed out, but with the weakest starting weapon.

weaponDamage = 50;
playerAttack = 100;


the totalDamage ends up being 300, which would kill an enemy in just a few hits. Even with your attack that high, I wouldn't want the weakest weapon to be able to kill the enemy that fast. I thought about adding defense, but I feel the game will lose consistency and become unbalanced in the long run. Possibly a well designed algorithm for a weapon decrease modifier would work for lower level weapons or something like that. Just need a break from trying to figure out the best way to go about this, and maybe someone that has experience with games and keeping the leveling consistent could give me some ideas/pointers.

• I think we need a little more information. How easy is it to level your playerAttack stat to 100? Considering this is the max value, wouldn't killing the enemy in a few hits be appropriate? (Is the 1000-HP enemy something you would see in the beginning of the game? A standard enemy? Boss?)
– Lee
Dec 20, 2012 at 4:18

The answer provided by Grzegorz Sławecki is already a good one, but I wanted to explain the rationale behind his method and give you the tools to adapt the solutions to your gameplay needs.

The parameters of the present problem are the player's attack level a, weapon damage w, the total inflicted damage in one attack d, the enemy's health H and the minimum number of hits necessary to kill the enemy, let's call it n.

If you want the player to kill in n hits, then his total damage d must be such that

(n-1).d < H ≤ n.d    or in other words, n = ceil(H/d).

d depends on weapon damage w and on the player's attack level a and we can expect weapons to get better as the level increases, so let's write d(a) and w(a) instead of simply d and w. The enemies the player faces are also expected to get tougher so, again, H(a). These are all increasing functions of a, and you want them to satisfy the above inequations. The unknowns of the problem are functions. You set one as a constraint, and you find the others. You do have degrees of freedom though, which is a good thing.

If I understand your question well, you have a precise gameplay in mind, and this gameplay is mainly represented here by the number of hits required to kill the enemy, n(a). Therefore, set n(a) depending on the gameplay you envision for the game and then find the rest of the variables of the problem. This is what you should always do because, as your question shows, your first attempt was to try an algorithm you thought may do and then realized it resulted in undesired gameplay.

Let's suppose, for instance, that you want the player to have to hit more and more times as he progresses in the game. You also want that, as the required number of hits increases, it increases less and less often, so that the player spends a longer part of the game hitting 5 times than 2 times. Here's what n(a) looks like:

The function used is n(a) = ceil( 2/3.sqrt(a) ).

We want H(a)/d(a) to stay within the ranges of values that make n(a) have the desired value and since n(a) = ceil(H(a)/d(a)), these ranges are the following rectangles:

and H(a)/d(a) can naturally be set to 2/3.sqrt(a) so that we get the following graph, with the red curve being H(a)/d(a):

Remark: we can easily find H(a)/d(a) here because we know the function of which n(a) is the ceil, but if our specification for n was less nice, we'd have to make our own fitting function using various tricks. Not all problems are this nice!

So we want H(a)/d(a) to resemble a customized square root function and we know that H and d must be increasing functions. Solutions are aplenty. For example,

H(a) = a . 2/3.sqrt(a)    and    d(a) = a

But we'd like the damage and the enemy's HP to increase an awful lot so that there are big, impressive numbers at the end of the game, just for style, so we instead set

H(a) = a² . 20/3.sqrt(a)    and    d(a) = 10.a²

The whole point, and the best part, is this: you know that your solutions to the problem (H(a) and d(a)) obey the specifications (n(a)), so you get the same n(a), but you have freedom. You know exactly the freedom you have, and you can use it to customize the experience. You should always try to give yourself such freedom while satisfying your most important needs, whenever possible.

Now that we've chosen the one-hit damage d(a), and since d(a) depends on the weapon damage w(a), we can use d(a) as our specification and try to find a w(a) that gives us this d(a). The principles are the same, the problem is different: we want the player to cause more damage as his level increases, even with the weapon remaining the same, and we also want the damage to increase when the weapon alone gets better and the level stays the same.

But what importance should each factor have? Suppose we want level to be more important than weapons: a bigger part of the variations of d(a) = a² should be independent from w(a), for example with

w(a) = 22.sqrt(a)    and, therefore,    d(a) = ( 22.sqrt(a) ) . ( 10/22.a.sqrt(a) ) = w(a).( 10/22.a.sqrt(a) )

We get the following graph for w(a)...

...and still the same d(a), because we again found a solution that obeyed the specification, here d(a), and we do have the properties mentioned above with w and a contributing to the damage (suppose we look at d as a function of a and w: then if a were fixed and we had w vary in the equation d(a,w) = a/30.w, d would still be an increasing function of w, and the same is true if you fix w and make a vary).

This w(a) could give you the value to be displayed in the weapon's ingame description: we would get "Weapon Damage : 220" with the best weapon in the game for instance.

We could have used a completely different specification for our gameplay and therefore for n(a), for example one that makes the number of required hits increase quickly as the game progresses and then plateau, and the resulting solutions would have been different.

• This combines a sound understanding of the mathematics with gameplay as a starting point. An excellent answer. Dec 20, 2012 at 19:52
• @MarcksThomas Well thank you! I wish more people defined their needs clearly and built their games from there. We would see more complete, finished games of quality that way. Dec 20, 2012 at 20:33
• Yes, I agree that this is perfect answer. I read this with pleasure :) Dec 20, 2012 at 21:53
• Fantastic! I wish this sort of mathematics were a more regular part of the systems design literature. (Maybe I should write up my essay on damage vs. shields sometime...) Dec 20, 2012 at 22:22
• Great answer, but why must you use one character variables in your equations? How many times do I ask, "What's a stand for again?" and scroll up? Sep 18, 2013 at 3:05

I would recommend you to design your game from the other direction: First think about how you want your game to play (in terms of game experience, not in terms of numbers), and then design both the formulas and the number-ranges to get that result.

Your gameplay goal is that it takes a certain number of hits to kill an enemy. How many hits depends on three variables:

• power level of the enemy
• power level of the character
• power level of the weapon

You should first create a table of all possible combinations of these three variables being low-, mid- or high-tier (no concrete numbers yet, just "low", "mid" or "high") and how many hits you wish to be required for these combinations.

Then you should try to find a formula and value ranges which approximates your wish-values the best.

The easiest way, is to increase weapon damage between the levels of weapon exponentially.

However, what matters is that You should also define how is HP of enemies growing when they're getting stronger. So, when Your very strong You should kill easy enemies with just a few hits, but those at Your level should be relatively harder to kill than the enemies that were at Your level at the beginning. So I would design it to look somewhat like below:

level   weapon  enemy   attack  total   hits needed
dmg     hp      factor  dmg     to kill
======================================================
1       1       2       5       1.25    1.6
2       4       16      10      6       2.666666667
3       9       54      15      15.75   3.428571429
4       16      128     20      32      4
5       25      250     25      56.25   4.444444444
6       36      432     30      90      4.8
7       49      686     35      134.75  5.090909091
8       64      1024    40      192     5.333333333
9       81      1458    45      263.25  5.538461538
10      100     2000    50      350     5.714285714
11      121     2662    55      453.75  5.866666667
12      144     3456    60      576     6
13      169     4394    65      718.25  6.117647059
14      196     5488    70      882     6.222222222
15      225     6750    75      1068.75 6.315789474
16      256     8192    80      128     6.4
17      289     9826    85      1517.25 6.476190476
18      324     11664   90      1782    6.545454545
19      361     13718   95      2075.75 6.608695652
20      400     16000   100     2400    6.666666667

Where:
1. total dmg is calculated using Your formula
2. weapon dmg = level*level (exponential growth)
3. enemy hp = 2*level*level*level (exponential growth)
4. attack factor = 5*level
5. hits needed describes how many hits are needed being on level x and using weapon on level x, to kill an enemy on level x


I tweaked those constant factors in formulas just to achieve results i think are acceptable, bo what is important is where You want linear growth and where You want exponential.

You should also notice that to e.g. kill enemy lvl 20 having 100 attack and having lvl1 weapon which takes total 6 dmg will take lots of time. If it takes too much in Your opinion, play with constants.

• What does "attack factor" mean? Sep 18, 2013 at 3:01

I think the world of war craft method is one of the best. The players base stats have a low impact on the total damage, with most of the damage modifier coming from bonus stats on gear. That makes it much easier to expand the game and balance it as time goes on