1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The raddest synthwave tracks were chosen - Check out our Music Contest #12 - Results and congratulate the winners!
    Dismiss Notice
  4. The poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  5. Travel to distant realms and encounter scenes unknown to the common folk. The Greatest of Adventures is upon us with the 8th Cinematic Contest. Join in on a fun ride.
    Dismiss Notice
  6. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  7. Contestants are to create a scene set in the Stone Age. Come and see what you can come up with. We wish you the best of luck!
    Dismiss Notice
  8. Colour outside the lines! Techtree Contest #13 is a go. The contest is optionally paired.
    Dismiss Notice
  9. Greetings cerebrates, our Swarm needs new spawners that will have numerous children. Join the HIVE's 31st Modeling Contest - Spawners and Spawned! The contest is optionally paired.
    Dismiss Notice
  10. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Snippet] Damage Calculator

Discussion in 'JASS Resources' started by mckill2009, Jun 28, 2012.

  1. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    Code (vJASS):

    /*
    =====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))


    Changelogs

    v1.1
    - Integer returned replaced by reals
    - Code reduced
     
    Last edited: Jun 29, 2012
  2. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    CD is not a very appropriate struct name.
     
  3. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    I understand, even CustomDamage is not, but I cant think of anything else that's why I made CuscomDamage to struct name CD ;)...
     
  4. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,021
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    "DamageCalculator".

    Also, inline those locals, you don't work for Blissard.
     
  5. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    And that's a good thing, because I heard Blizzard's coders get minimum wage :3
     
  6. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    Updated:
    name changed & shorten code
     
  7. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Code (vJASS):
    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
     
  8. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,021
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Damage is also real values, so it should be GetRandomReal on both those.
     
  9. KnnO

    KnnO

    Joined:
    Sep 18, 2011
    Messages:
    158
    Resources:
    3
    Icons:
    1
    Maps:
    1
    Spells:
    1
    Resources:
    3
    if so, you should use real values even in the GUI demo
     
  10. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    Thanks Mags...

    if it's in real, whats the difference of taking 32.3 and 32.2 damage?, that's why I made it a
    whole number...
     
  11. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,021
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Because damage is a real number, not an integer. Better to simply use reals which is what people expect damage to be in.
     
  12. KnnO

    KnnO

    Joined:
    Sep 18, 2011
    Messages:
    158
    Resources:
    3
    Icons:
    1
    Maps:
    1
    Spells:
    1
    Resources:
    3
    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:
    Code (vJASS):

    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
     
  13. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Actually, unit health is also a real, and that's why GetWidgetLife returns a real.

    Try placing a unit with 50% HP on the map (having regeneration) and display his current HP every 0.05 seconds.
     
  14. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    I know that all is real but the point of this is to return a whole number, meaning,
    no decimals, so that the damage is full, so instead of 3.2, it is 3 or 4...
    anyway, Ima change it tonight if that's more accurate...
     
  15. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,021
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    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.
     
  16. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    Ok I've changed it...Bribe pls change the title to DamageCalculator, thanks...
     
  17. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Well, this is functional, and it has it's uses.

    I'll go ahead and approve it.
     
  18. gorillabull

    gorillabull

    Joined:
    Jul 17, 2011
    Messages:
    1,368
    Resources:
    2
    Spells:
    2
    Resources:
    2
    its accurate for heroes and extremely inaccurate for units but i like how it gets the raw damage before it is reduced by anything that is 1 feature damage systems dont have
     
  19. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    It only does that because it's simple math ._.
     
  20. mckill2009

    mckill2009

    Joined:
    Mar 10, 2009
    Messages:
    4,696
    Resources:
    34
    Maps:
    5
    Spells:
    27
    JASS:
    2
    Resources:
    34
    pls explain...