[Snippet] Damage Calculator

Level 28
Joined
Mar 10, 2009
Messages
5,017
JASS:
/*
=====DamageCalculator v1.1
=====By Mckill2009

I don't know if this site has this snippet already but I'm posting it anyway
to get feedbacks, basically this is a small code that can get the random
damage from a certain unit based on what is written in the object editor.

Take note that the damage return DOES NOT mean the EXACT damage given by
the unit, therefore this does not affect the armour of the attacked unit.

The returned damage computation are based on; (see object editor)
    Primary Attribute (for hero only)
    Damage Base
    Damage Number Of Dice
    Damage Sides Per Die

API:
    static method getHeroDC takes integer mainattribute, real damBase, real damDice, real damDie returns real
        - local real dam = DamageCalculator.getHeroDC(1,2,3,4)    
        - mainattribute (the PRIMARY ATTRIBUTE of the hero)
        - damBase (the DAMAGE BASE)
        - damDice (the DAMAGE NUMBER OF DICE)
        - damDie (the DAMAGE SIDES PER DIE)

    static method getUnitDC takes real damBase, real damDice, real damDie returns real
        - local real dam = DamageCalculator.getUnitDC(1,2,3)
        - damBase (the DAMAGE BASE)
        - damDice (the DAMAGE NUMBER OF DICE)
        - damDie (the DAMAGE SIDES PER DIE)

CREDITS:
- Bribe (for name change)
- Magtheridon96 (for shortening the code even more)
- KnnO (for other suggestions)
*/

library DamageCalculator

struct DamageCalculator extends array

    static method getHeroDC takes integer mainattribute, real damBase, real damDice, real damDie returns real
        return GetRandomReal((mainattribute+damDice)+damBase, (mainattribute+(damDice*damDie))+damBase)
    endmethod
    
    static method getUnitDC takes real damBase, real damDice, real damDie returns real
        return GetRandomReal(damBase+damDice, damBase+(damDice*damDie))
    endmethod    

endstruct

endlibrary

How it works?
  • DC Hero
    • Events
      • Unit - A unit Is attacked
    • Conditions
      • (Unit-type of (Attacking unit)) Equal to Blood Mage
    • Actions
      • Custom script: local real dam = DamageCalculator.getHeroDC(GetHeroInt(GetAttacker(),false), 0, 2, 4)
      • Custom script: call BJDebugMsg(R2S(dam))
  • DC Unit
    • Events
      • Unit - A unit Is attacked
    • Conditions
      • (Unit-type of (Attacking unit)) Equal to Knight
    • Actions
      • Custom script: local real dam = DamageCalculator.getUnitDC(25, 2, 5)
      • Custom script: call BJDebugMsg(R2S(dam))

v1.1
- Integer returned replaced by reals
- Code reduced
 
Last edited:
JASS:
library DamageCalculator /* Requires nothing because mckill2009 is so awesome. */

    struct DamageCalculator extends array
        static method getHero takes integer mainattribute, integer damBase, integer damDice, integer damDie returns integer
            return GetRandomInt(mainattribute + damDice + damBase, damDice * damDie + damBase + mainattribute)
        endmethod
        
        static method getUnit takes integer damBase, integer damDice, integer damDie returns integer
            return GetRandomInt(damBase + damDice, damBase + damDice * damDie)
        endmethod
    endstruct

endlibrary
 
Level 7
Joined
Sep 18, 2011
Messages
195
Because the damage in warcraft is a real number, but the unit's health is integer (I tink).
also you could use it for these functions:
JASS:
UnitDamagePoint(whichUnit,delay,radius,x,y,amount,attack,ranged,attackType,damageType,weaponType)
UnitDamageTarget(whichUnit,target,amount,attack,ranged,attackType,damageType,weaponType)
without having to convert it to integer

edit: like what bribe said
 
Level 34
Joined
Sep 26, 2009
Messages
8,435
I think "3.2" is more full than "3" if the situation permits it. Keep in mind that the damage will then be reduced by damage type/enemy armor type and enemy armor value and possibly increased thanks to a critical strike or something. So that .2 decimal value might amount to something a bit more significant.
 
Top