• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Libraries

Status
Not open for further replies.
Level 15
Joined
Oct 16, 2010
Messages
942
JASS:
//////////////////////////////////////////////////////////////////////////////////////
//                               GetDamage Function                                 //
//   call GetDamage(casting unit, targeted unit, median damage, "magic type")       //
//                                                                                  //
//                                GetHeal Function                                  //
//  call GetHeal(casting unit, targeted unit, median heal amount, "magic type")     //
//                                                                                  //
//                             GetDamageText Function                               //
//  call GetDamageText(targeted unit, amount, crit? (t/f), type ("damage"/"heal")   //
//                                                                                  //
//                             GetImmuneText Function                               //
//                        call GetImmuneText(targeted unit)                         //
//                                                                                  //
//                              GetCritical Function                                //
//                          call GetCritical(casting unit)                          //
//////////////////////////////////////////////////////////////////////////////////////

    globals
        hashtable Hash_dmg = InitHashtable()
    endglobals
    
    
    function GetImmuneText takes unit target returns nothing
        local texttag immunetxt
        
        set immunetxt = CreateTextTag()
        
        
        call SetTextTagPermanent(immunetxt, false)
        call SetTextTagVisibility(immunetxt, true)
        call SetTextTagPosUnit(immunetxt, target, 0)
        call SetTextTagText(immunetxt, "immune", .023)
        call SetTextTagFadepoint(immunetxt, 1)
        call SetTextTagLifespan(immunetxt, 1.5)
        call SetTextTagVelocity(immunetxt, 0, .05)
        call SetTextTagColor(immunetxt, 255, 255, 255, 255)
    endfunction

    
    function GetDamageText takes unit target, real dmg, boolean critical, string runtype returns nothing
        local texttag dmgtxt
        local real txtsize
        local string txtcolor
        local string txtoutput
    
        set dmgtxt = CreateTextTag()
    
        //changes text color depending on whether it's a damage or heal
        if runtype == "damage" then
            set txtcolor = "|cffCD0000"
        elseif runtype == "heal" then
            set txtcolor = "|cff00CD00"
        endif
    
        //increases text size and adds and exclamation mark if it's a crit
        if critical == false then
            set txtsize = .023
            set txtoutput = (txtcolor + I2S(R2I(dmg)) + "|r")
        else
            set txtsize = .03
            set txtoutput = (txtcolor + I2S(R2I(dmg)) + "!|r")
        endif
    
        //applies the necessary texttag properties
        call SetTextTagPermanent(dmgtxt, false)
        call SetTextTagVisibility(dmgtxt, true)
        call SetTextTagPosUnit(dmgtxt, target, 0)
        call SetTextTagText(dmgtxt, txtoutput, txtsize)
        call SetTextTagFadepoint(dmgtxt, 1)
        call SetTextTagLifespan(dmgtxt, 1.5)
        call SetTextTagVelocity(dmgtxt, 0, .05)
        call SetTextTagColor(dmgtxt, 255, 255, 255, 255)
    endfunction
    
    
    function GetCritical takes unit caster returns boolean
        //function rolls to see if the casting unit gets a crit
        
        local integer random = GetRandomInt(1, 100)
        
        if random <= LoadRealBJ(3, GetHandleIdBJ(caster), Hash_dmg) then
            return true
        else
            return false
        endif
    endfunction

    
    function GetDamage takes unit caster, unit target, real dmg, string magictype returns nothing
        local boolean critical
    
        //Converts the median damage to a -10% to +10% range (i.e. 100 becomes 90-110)
        set dmg = GetRandomReal((dmg - (dmg * .1)), (dmg + (dmg * .1)))
            
        //Grabs the casting units black/white magic bonus depending on what the damage type is
        if magictype == "Black" then
            set dmg = dmg + LoadRealBJ(1, GetHandleIdBJ(caster), Hash_dmg)
        elseif magictype == "White" then
            set dmg = dmg + LoadRealBJ(2, GetHandleIdBJ(caster), Hash_dmg)
        endif
    
        //Rolls to see if a crit occurs (dependant upon casting units magic crit %)
        set critical = GetCritical(caster)
        if critical == true then
            set dmg = dmg * 1.5
        endif
    
        //Grabs the targeted units magic defense and calculates that into the total damage (also calls the floating text function)
        if dmg > LoadRealBJ(4, GetHandleIdBJ(target), Hash_dmg) then
            set dmg = dmg - LoadRealBJ(4, GetHandleIdBJ(target), Hash_dmg)
            call GetDamageText(target, dmg, critical, "damage")
        else
            call GetImmuneText(target)
        endif
    
        //Deals the damage
        call UnitDamageTarget(caster, target, dmg, false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS)

    endfunction
    
    
    function GetHeal takes unit caster, unit target, real amount, string magictype returns nothing
        local boolean critical
        
        //Converts the median amount to a -10% to +10% range (i.e. 100 becomes 90-110)
        set amount = GetRandomReal((amount - (amount * .1)), (amount + (amount * .1)))
        
        //Grabs the casting units black/white magic bonus depending on what the heal type is
        if magictype == "Black" then
            set amount = amount + LoadRealBJ(1, GetHandleIdBJ(caster), Hash_dmg)
        elseif magictype == "White" then
            set amount = amount + LoadRealBJ(2, GetHandleIdBJ(caster), Hash_dmg)
        endif
        
        //Rolls to see if a crit occurs (dependant upon casting units magic crit %)
        set critical = GetCritical(caster)
        if critical == true then
            set amount = amount * 1.5
        endif
        
        //calls the floating text function and then increases the unit's health for whatever the heal amount was
        call GetDamageText(target, amount, critical, "heal")
        call SetUnitState(target, UNIT_STATE_LIFE, (GetUnitState(target, UNIT_STATE_LIFE) + amount))
   
    endfunction

I'm trying to put the above into a library, but I keep getting a ton of errors when I try to do it. All i'm doing is writing
JASS:
library abilities
at the top and
JASS:
endlibrary
at the bottom.

Also, and this may be my problem as well, do libraries require an initializer? I am accessing one of the functions in this library with a call statement and the rest of the functions are internal and are only used by the called function.
 
libraries dont need inits, that is optional...

anyway, can you give an example of what errors you get?

and are you using JNGP with the latest jass helper?

or have you tried putting it on the map header and try to see if you get the same errors? coz if it does then, the problem lies within those codes and not because of the library




and this

local texttag dmgtxt
+
set dmgtxt = CreateTextTag()

you can do it like this

local texttag dmgtxt = CreateTextTag()

the same thing with boolean critical...
 
Level 15
Joined
Oct 16, 2010
Messages
942
libraries dont need inits, that is optional...

I thought so, thanks!

anyway, can you give an example of what errors you get?

39 errors
- about 20 of them are "statement outside of function" errors - reffering to various lines
- a few syntax errors
- the rest are undeclared variable errors saying the functions cannot see the global variables I declared

and are you using JNGP with the latest jass helper?

yes to the former, i'll check the latter

EDIT - what's the latest version of jasshelper? I'm using (0.9.l.2)

or have you tried putting it on the map header and try to see if you get the same errors? coz if it does then, the problem lies within those codes and not because of the library

Oui monsieur, it's already in the map header.

and this

local texttag dmgtxt
+
set dmgtxt = CreateTextTag()

you can do it like this

local texttag dmgtxt = CreateTextTag()

the same thing with boolean critical...

doh! i'll change them :)
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
The latest JassHelper is version 0.A.2.B I believe; by the way declaring libraries in the map header (custom script section) shouldn't throw errors - you can define many libraries/scopes in that section (as with any other section).
 
Status
Not open for further replies.
Top