Snippet GetHeroMainAtt

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
GetHeroMainAtt
By Gtam

This snippet will allow people to identify the main attribute of a hero.
Here is 2 snippets. You can choose which one you like.

Firts snippet: Uses ability detection
JASS:
library HeroMainAttribute1
//==========================================================================================\\
//==========================================================================================\\
//== Hero Main Attribute: Sweet Flavor                                                    ==\\
//==  HMA                                                                                 ==\\
//==   By Gtam                                                                            ==\\
//==                                                                                      ==\\
//== This is a little handy snippet that will beable to tell you heroes main attributes.  ==\\
//==                                                                                      ==\\
//== Well as you probably know by know there is no function to tell you a heroes main     ==\\
//== attribute. This is here to fix that.                                                 ==\\
//== This flavor uses abilities to tell the main attribute so its your job to make sure   ==\\
//== that the correct abilities is added to the correct heroes.                           ==\\
//== Dont worry its pretty straight forward.                                              ==\\
//==   Step 1: Add the Ability "Str Hero" to all your strength heroes.                    ==\\
//==   Step 2: Add the Ability "Agi Hero" to all your Agility heroes.                     ==\\
//==   Step 3: Add the Ability "Int Hero" to all you Intelligence heroes.                 ==\\
//==   Step 4: Configure the global variables to the correct raw codes                    ==\\
//==   Step 5: Use this system to detect hero main atributes                              ==\\
//==                                                                                      ==\\
//== Functions                                                                            ==\\
//==                                                                                      ==\\
//==  function IsHeroStr takes unit hero returns boolean                                  ==\\
//==     returns true if hero strength and false if not                                   ==\\
//==                                                                                      ==\\
//==  function IsHeroAgi takes unit hero returns boolean                                  ==\\
//==     returns true if hero agility and false if not                                    ==\\
//==                                                                                      ==\\
//==  function IsHeroInt takes unit hero returns boolean                                  ==\\
//==     returns true if hero intelligence and false if not                               ==\\
//==                                                                                      ==\\
//==  function GetHeroMainAtt takes unit hero, boolean flag returns integer               ==\\
//==     returns the Hero's main attribute amount                                         ==\\
//==       the boolean is if you want bonuses included                                    ==\\
//==                                                                                      ==\\
//==========================================================================================\\
//==========================================================================================\\

    globals
        private constant integer STR_HERO = 'A000' //The "Str Hero" ability raw code
        private constant integer AGI_HERO = 'A001' //The "Agi Hero" ability raw code
        private constant integer INT_HERO = 'A002' //The "Int Hero" ability raw code
    endglobals
    
//=========================================================================================\\
// Functions
    
    function IsHeroStr takes unit hero returns boolean
        return GetUnitAbilityLevel(hero, STR_HERO) > 0
    endfunction
    
    function IsHeroAgi takes unit hero returns boolean
        return GetUnitAbilityLevel(hero, AGI_HERO) > 0
    endfunction
    
    function IsHeroInt takes unit hero returns boolean
        return GetUnitAbilityLevel(hero, INT_HERO) > 0
    endfunction
    
    function GetHeroMainAtt takes unit hero, boolean flag returns integer
        local integer att
        if GetUnitAbilityLevel(hero, STR_HERO) > 0 then
            set att = GetHeroStr(hero, flag)
        elseif GetUnitAbilityLevel(hero, AGI_HERO) > 0 then
            set att = GetHeroAgi(hero, flag)
        elseif GetUnitAbilityLevel(hero, INT_HERO) > 0 then
            set att = GetHeroInt(hero, flag)
        endif
        return att
    endfunction

endlibrary


Second snippet: Uses groups to group heroes.
JASS:
library HeroMainAtt2 initializer Init
//==========================================================================================\\
//==========================================================================================\\
//== Hero Main Attribute: Sour Flavor                                                     ==\\
//==  HMA                                                                                 ==\\
//==   By Gtam                                                                            ==\\
//==                                                                                      ==\\
//== This is a little handy snippet that will beable to tell you heroes main attributes.  ==\\
//==                                                                                      ==\\
//== Well as you probably know by know there is no function to tell you a heroes main     ==\\
//== attribute. This is here to fix that.                                                 ==\\
//== This flavor uses groups to tell the main attribute so its your job to make sure      ==\\
//== that the correct unit is added to the correct group.                                 ==\\
//== Dont worry its pretty straight forward.                                              ==\\
//==   Step 1: Configure all the strenght hero raw codes in the alocated function         ==\\
//==   Step 2: Configure all the agility hero raw codes in the alocated function          ==\\
//==   Step 3: Configure all the intelligence hero raw codes in the alocated function     ==\\
//==   Step 4: Use this system to detect hero main atributes                              ==\\
//==                                                                                      ==\\
//== Functions                                                                            ==\\
//==                                                                                      ==\\
//==  function IsHeroStr takes unit hero returns boolean                                  ==\\
//==     returns true if hero strength and false if not                                   ==\\
//==                                                                                      ==\\
//==  function IsHeroAgi takes unit hero returns boolean                                  ==\\
//==     returns true if hero agility and false if not                                    ==\\
//==                                                                                      ==\\
//==  function IsHeroInt takes unit hero returns boolean                                  ==\\
//==     returns true if hero intelligence and false if not                               ==\\
//==                                                                                      ==\\
//==  function GetHeroMainAtt takes unit hero, boolean flag returns integer               ==\\
//==     returns the Hero's main attribute amount                                         ==\\
//==       the boolean is if you want bonuses included                                    ==\\
//==                                                                                      ==\\
//==========================================================================================\\
//==========================================================================================\\
    
    globals
        private group STR = CreateGroup()
        private group AGI = CreateGroup()
        private group INT = CreateGroup()
        private group ALL = CreateGroup()
        private integer array STRHERO_ID
        private integer array AGIHERO_ID
        private integer array INTHERO_ID
        private integer STRHERO_NUM
        private integer AGIHERO_NUM
        private integer INTHERO_NUM
    endglobals
    
//=========================================================================================\\    
// Configure you strength hero raw codes here    
    private function STRVars takes nothing returns nothing
        set STRHERO_ID[0] = 'Hpal'
        set STRHERO_ID[1] = 'Hmkg'
        set STRHERO_NUM = 2
    endfunction
    
//=========================================================================================\\
// Configure you agility hero raw codes here       
    private function AGIVars takes nothing returns nothing
        set AGIHERO_NUM = 0
    endfunction
    
//=========================================================================================\\
// Configure you intelegence hero raw codes here       
    private function INTVars takes nothing returns nothing
        set INTHERO_ID[0] = 'Hamg'
        set INTHERO_ID[1] = 'Hblm'
        set INTHERO_NUM = 2
    endfunction
    
//=========================================================================================\\
// Do not touch below this point unless you know what your doing <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile    :)" loading="lazy" data-shortname=":)" />    
    private function Filt takes nothing returns boolean
        return IsUnitType(GetFilterUnit(), UNIT_TYPE_HERO) == true
    endfunction
    
    private function GroopLoop takes nothing returns nothing
        local unit u = GetEnumUnit()
        local integer str = STRHERO_NUM
        local integer agi = AGIHERO_NUM 
        local integer int = INTHERO_NUM
        loop
            set str = str - 1
            exitwhen str == -1
            if GetUnitTypeId(u) == STRHERO_ID[str] then
                call GroupAddUnit(STR, u)
            endif
        endloop
        loop
            set agi = agi - 1
            exitwhen agi == -1
            if GetUnitTypeId(u) == AGIHERO_ID[agi] then
                call GroupAddUnit(AGI, u)
            endif
        endloop
        loop
            set int = int - 1
            exitwhen int == -1
            if GetUnitTypeId(u) == INTHERO_ID[int] then
                call GroupAddUnit(INT, u)
            endif
        endloop
    endfunction
    
    private function Conditions takes nothing returns boolean
        return IsUnitType(GetTriggerUnit(), UNIT_TYPE_HERO) == true
    endfunction
    
    private function Actions takes nothing returns nothing
        local unit u = GetTriggerUnit()
        local integer str = STRHERO_NUM
        local integer agi = AGIHERO_NUM 
        local integer int = INTHERO_NUM
        loop
            set str = str - 1
            exitwhen str == -1
            if GetUnitTypeId(u) == STRHERO_ID[str] then
                call GroupAddUnit(STR, u)
            endif
        endloop
        loop
            set agi = agi - 1
            exitwhen agi == -1
            if GetUnitTypeId(u) == AGIHERO_ID[agi] then
                call GroupAddUnit(AGI, u)
            endif
        endloop
        loop
            set int = int - 1
            exitwhen str == -1
            if GetUnitTypeId(u) == INTHERO_ID[int] then
                call GroupAddUnit(INT, u)
            endif
        endloop
    endfunction

    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterEnterRectSimple(t, bj_mapInitialPlayableArea)
        call TriggerAddCondition(t, Condition(function Conditions))
        call TriggerAddAction(t, function Actions)
        call STRVars()
        call AGIVars()
        call INTVars()
        call GroupEnumUnitsInRect(ALL, bj_mapInitialPlayableArea, Condition(function Filt))
        call ForGroup(ALL, function GroopLoop)
    endfunction
    
//=========================================================================================\\
// Functions

    function IsHeroStr takes unit hero returns boolean
        return IsUnitInGroup(hero, STR) == true
    endfunction
    
    function IsHeroAgi takes unit hero returns boolean
        return IsUnitInGroup(hero, AGI) == true
    endfunction
    
    function IsHeroInt takes unit hero returns boolean
        return IsUnitInGroup(hero, INT) == true
    endfunction
    
    function GetHeroMainAtt takes unit hero, boolean flag returns integer
        local integer att
        if IsUnitInGroup(hero, STR) == true then
            set att = GetHeroStr(hero, flag)
        elseif IsUnitInGroup(hero, AGI) == true then
            set att = GetHeroAgi(hero, flag)
        elseif IsUnitInGroup(hero, INT) == true then
            set att = GetHeroInt(hero, flag)
        endif
        return att
    endfunction

endlibrary


Hope its helpfull.
The test map has the Human heroes added to each snippet.
 

Attachments

  • GetHeroMainAtt.w3x
    26.5 KB · Views: 239

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:
//Red flavor, for speed freaks.
library HeroAttribute requires Hash

    globals
        constant integer STRENGTH = 1
        constant integer AGILITY = 2
        constant integer INTELLIGENT = 3
    endglobals  
    
    struct HeroAttribute extends array
        private static integer array data

        static method operator [] takes integer heroId returns integer
            return thistype.data[Hash(heroId)]
        endmethod
        
        static method operator []= takes integer heroId, integer id returns integer
            set thistype.data[Hash(heroId)] = id
        endmethod
    endstruct
endlibrary

JASS:
//Blue flavor, for safety.
library HeroAttribute

    globals
        constant integer STRENGTH = 1
        constant integer AGILITY = 2
        constant integer INTELLIGENT = 3
    endglobals  
    
    struct HeroAttribute extends array
        private static hashtable table = InitHashtable()

        static method operator [] takes integer heroId returns integer
            return LoadInteger(thistype.table,heroId,0)
        endmethod
        
        static method operator []= takes integer heroId, integer id returns integer
            call SaveInteger(thistype.table,heroId,0,id)
        endmethod
    endstruct
endlibrary

JASS:
    set HeroAttributes[&#039;TEST&#039;] = STRENGTH
//Some &quot;KKJ&quot;(kingking&#039;s Jass, lol) here :
function IsHeroStr takes unit whichUnit returns boolean
    return HeroAttributes[GetUnitTypeId(whichUnit)] == STRENGTH
endfunction
function IsHeroAgi takes unit whichUnit returns boolean
    return HeroAttributes[GetUnitTypeId(whichUnit)] == AGILITY
endfunction
function IsHeroInt takes unit whichUnit returns boolean
    return HeroAttributes[GetUnitTypeId(whichUnit)] == INTELLIGENT
endfunction

This mini stuff can beat your large library. =0

Sweet Flavor - It requires you to add abilities to heroes. ><
Sour Flavor - Omg, O(n) searching, pretty slow.
 

Zwiebelchen

You can change this now in User CP.
Reaction score
60
I don't really see the point of this ... You can always get the main attribute of heroes by getting the UnitTypeId and compare it to a hardcoded list.
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
> This will be useless when the next JNGP is released.

Haha... Another JNGP release... :p
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top