• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Snippet] Damage Calculator

Level 29
Joined
Mar 10, 2009
Messages
5,016
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 8
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
 
Top