Author Topic: Damage formula  (Read 2442 times)

D.U.P.A.

  • Guest
Damage formula
« on: 17 July 2012, 23:01:43 »
How is determined how damage is dealt to a unit? I know of the armor and damage types, but how does armor affect incoming damage and how many hitpoints are removed after each attack? I don't seem to find it on wiki.

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Damage formula
« Reply #1 on: 18 July 2012, 04:39:54 »
Close, but not quite.

I wrote a handy tool for calculating how damage works a while back, which can be found here.

The actual formula is: floor((attack_strength - armor) * damage_multiplier)

However, we also have to bear in mind the attack speed (a very fast, but weak attack can be stronger than a slow but powerful attack). Damage per world frame is calculated with: (floor((attack_strength - armor) * damage_multiplier)) / (ceil( 1 / (attack_speed / 4000) + 1 ))

There's 40 world frames per second on "regular" speed (the + and - keys modify that), so times the above formula by 40 for damage per second, which is usually the "de facto" method of determining relative strength of attacks in games.

An example of this in use:
Say we have an attack strength of 400 (with a variable of zero, for consistency), the target has 20 armour, but there's no damage multiplier (eg, energy attack on leather armour). That makes the damage formula floor((400 - 20) * 1), which becomes 380 (floor means rounding down). So our attack did 380 damage to the foe. Now assume that the attack speed is 90. That makes the second formula 380 / (ceil(1 / (90 / 4000) + 1), which equals 380 / 46 (ceil means rounding up), or 8.26 damage per frame. At 40 frames per second (regular game speed), the damage per second (DPS) is 8.26* 40, or 330 (90 is a very fast attack speed).
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

D.U.P.A.

  • Guest
Re: Damage formula
« Reply #2 on: 18 July 2012, 14:33:43 »
Basically the system is similar to sc1 attacks. But the additional checking of attack speed is to balance or what? Because surely faster attacks do more damage than slow attack if the faster attack is still big enough and fast. But they can be still at disadvantage, because they need to compare with armor more often, like in simplied form, 200 damage vs 50 armor over 2 seconds will 150 damage in 2 seconds, while 2 attacks of 100 damage vs 50 armor will do 50 damage each second, of a total 100 in 2 seconds. Although that can be balanced with more efficient damage distribution.

Why there is (ceil( 1 / (attack_speed / 4000) + 1 ) , wouldn't be the same if there written (4000/attack_speed) instead of 1/(attack_speed/4000), since +1 is not the fraction?

John.d.h

  • Moderator
  • Airship
  • ********
  • Posts: 3,757
  • I have to go now. My planet needs me.
    • View Profile
Re: Damage formula
« Reply #3 on: 18 July 2012, 19:40:50 »
But they can be still at disadvantage, because they need to compare with armor more often, like in simplied form, 200 damage vs 50 armor over 2 seconds will 150 damage in 2 seconds, while 2 attacks of 100 damage vs 50 armor will do 50 damage each second, of a total 100 in 2 seconds. Although that can be balanced with more efficient damage distribution.
Yes, so for example if a machine gun fires 30 bullets at 100 damage each, then it's going to do less damage against armor than a weapon that fires one shot of 3000 damage.

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Damage formula
« Reply #4 on: 18 July 2012, 21:29:24 »
But they can be still at disadvantage, because they need to compare with armor more often, like in simplied form, 200 damage vs 50 armor over 2 seconds will 150 damage in 2 seconds, while 2 attacks of 100 damage vs 50 armor will do 50 damage each second, of a total 100 in 2 seconds. Although that can be balanced with more efficient damage distribution.
Yes, so for example if a machine gun fires 30 bullets at 100 damage each, then it's going to do less damage against armor than a weapon that fires one shot of 3000 damage.
Which can make for great strategy, though (a light machine gun can be effective against unarmoured foes, while hardly putting a dent in a powerfully armoured foe. The machine gun would also be better suited for multiple weaker foes, while a heavy gun would be reserved for the tough armoured foes). Of course, the AI is too dumb to understand such mechanics, so should be avoided.

Why there is (ceil( 1 / (attack_speed / 4000) + 1 ) , wouldn't be the same if there written (4000/attack_speed) instead of 1/(attack_speed/4000), since +1 is not the fraction?
That is a good question. I suppose the results are the same, so it wouldn't matter, but the equation was originally posted by Silnarm a while back and I guess that's how they did it in the source. Or perhaps the +1 is supposed to have another bracket around it, to prevent division by zero if attack_speed it zero, but I can't judge.
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

D.U.P.A.

  • Guest
Re: Damage formula
« Reply #5 on: 19 July 2012, 00:06:34 »
You can't prevent the division by zero this way, since denominator would be still 0 because of fraction being 0/4000 = 0

However this formula isn't in source code somewhere?

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,238
    • View Profile
Re: Damage formula
« Reply #6 on: 19 July 2012, 05:09:42 »
Here is the damage code for mg (not sure why others would post in the mg forum if they are not looking at the code):

Code: [Select]
float damage = ast->getTotalAttackStrength(attacker->getTotalUpgrade());
int var = ast->getAttackVar();
int armor = attacked->getType()->getTotalArmor(attacked->getTotalUpgrade());
float damageMultiplier = world->getTechTree()->getDamageMultiplier(ast->getAttackType(), attacked->getType()->getArmorType());

//compute damage
damage += random.randRange(-var, var);
damage /= distance+1;
damage -= armor;
damage *= damageMultiplier;

if(damage < 1) {
damage= 1;
}

attacked->setLastAttackerUnitId(attacker->getId());

//damage the unit
if(attacked->decHp(static_cast<int>(damage))) {

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Damage formula
« Reply #7 on: 19 July 2012, 07:30:37 »
And for those who can't read c++: (damage / (distance + 1) - armour) * damage multiplier

Interesting that MegaGlest changed the damage formula. What's with the division by distance? That's the only part that is different from the formula I posted. Well, that and rounding, I guess. Does MegaGlest floor the damage before dealing it, or is there an invisible decimal?
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

Ishmaru

  • Behemoth
  • *******
  • Posts: 1,071
  • um wat??
    • View Profile
    • DelphaDesign
Re: Damage formula
« Reply #8 on: 19 July 2012, 07:53:35 »
Perhaps distance means radius from splash center?
Annex: Conquer the World Release 4 For Pc Mac + Linux
https://forum.megaglest.org/index.php?topic=9570.0
Annex is now on Facebook!
https://www.facebook.com/AnnexConquer

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Damage formula
« Reply #9 on: 19 July 2012, 21:47:07 »
Perhaps distance means radius from splash center?
That would make sense. Forgot that the distance is taken into aspect there. I guess other engines would have that too, but was simply excluded in the formula I was given a while back.
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

Alchemek

  • Worker
  • Posts: 1
    • View Profile
Re: Damage formula
« Reply #10 on: 29 July 2013, 08:30:36 »
Is this formula and other relevant information posted in the tech tree and/or wiki? It would be convenient if it were, if it isn't already. Thanks to the person who started this post and to everyone who replied clearing everything up!

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Damage formula
« Reply #11 on: 29 July 2013, 18:40:16 »
This page has two important formulas: how damage (and armour) work and how skill speed cycles work (ie, how long will a skill of speed x take?).
« Last Edit: 18 June 2016, 13:39:16 by filux »
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert