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 Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  9. 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.

Small Code Snippets

Discussion in 'Triggers & Scripts' started by PurplePoot, Sep 29, 2007.

  1. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    25,547
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    Except that produces procedural coupling. You are basically duplicating that code everywhere you want a random X/Y inside a rect.

    Something like
    GetRandomXInRect(r)
    and
    GetRandomYInRect(r)
    looks neater and keeps with the WC3 style of
    GetRandomLocInRect(r)
    .

    Code (vJASS):
    function GetRandomXInRect takes rect whichRect returns real
        return GetRandomReal(GetRectMinX(whichRect), GetRectMaxX(whichRect))
    endfunction

    function GetRandomYInRect takes rect whichRect returns real
        return GetRandomReal(GetRectMinY(whichRect), GetRectMaxY(whichRect))
    endfunction

    I would use it if I had to get a random position from inside a rect at more than 1 place in the code.
     
  2. Code (vJASS):

    function IsBuildingUnderConstruction takes unit u returns boolean
        if IsUnitType(u,ConvertUnitType(14)) then
            if GetUnitAbilityLevel(u,'Abgs')!= 0 or GetUnitAbilityLevel(u,'Abgl')!= 0 then
                return true
            endif
        else
            if GetUnitAbilityLevel(u,'Abds')!= 0 or GetUnitAbilityLevel(u,'Abdl')!= 0 then
                return true
            endif
        endif
        return false
    endfunction
     


    Returns a boolean if a unit is under construction.
    Tested it with default buildings and works like a charm.
     
  3. Quilnez

    Quilnez

    Joined:
    Oct 12, 2011
    Messages:
    3,251
    Resources:
    37
    Icons:
    2
    Tools:
    1
    Maps:
    7
    Spells:
    21
    Tutorials:
    2
    JASS:
    4
    Resources:
    37
    This is a bit nerdy but it can be improved.
    Code (vJASS):

    function IsBuildingUnderConstruction takes unit u returns boolean
        if IsUnitType(u,ConvertUnitType(14)) then
            return GetUnitAbilityLevel(u,'Abgs')!= 0 or GetUnitAbilityLevel(u,'Abgl')!= 0
        else
            return GetUnitAbilityLevel(u,'Abds')!= 0 or GetUnitAbilityLevel(u,'Abdl')!= 0
        endif
    endfunction
     


    Anyway what is
    ConvertUnitType(14)
    ? If there is a constant for it then better to use it.
     
  4. Wietlol

    Wietlol

    Joined:
    Aug 1, 2013
    Messages:
    4,653
    Resources:
    3
    Spells:
    3
    Resources:
    3
     UNIT_TYPE_UNDEAD 

    Thats it.
     
  5. Thanks, this is the best solution then:
    Code (vJASS):

    function IsBuildingUnderConstruction takes unit u returns boolean
        if IsUnitType(u,UNIT_TYPE_UNDEAD) then
            return GetUnitAbilityLevel(u,'Abgs')!= 0 or GetUnitAbilityLevel(u,'Abgl')!= 0
        else
            return GetUnitAbilityLevel(u,'Abds')!= 0 or GetUnitAbilityLevel(u,'Abdl')!= 0
        endif
    endfunction
     
     
  6. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,856
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Code (vJASS):

    function IsBuildingUnderConstruction takes unit u returns boolean
        if IsUnitType(u,ConvertUnitType(14)) then
            return GetUnitAbilityLevel(u,'Abgs')!= 0 or GetUnitAbilityLevel(u,'Abgl')!= 0
        endif
        return GetUnitAbilityLevel(u,'Abds')!= 0 or GetUnitAbilityLevel(u,'Abdl')!= 0
    endfunction


    btw, your current solution raises an error :)
     
  7. Thanks, haven't thought of that.

    You might want to give me some more details on the error.
    It worked fine for me.
     
  8. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,856
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    ahh I thought it raises (haven't tested it :p)
     
  9. Wietlol

    Wietlol

    Joined:
    Aug 1, 2013
    Messages:
    4,653
    Resources:
    3
    Spells:
    3
    Resources:
    3
    It could give an error that it cant see a return.
    But iirc, it does properly detect returns in if statements and else blocks combined.
     
  10. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Guys, let's do one line then :

    Code (vJASS):

    function IsBuildingUnderConstruction takes unit u returns boolean
        return IsUnitType(u,UNIT_TYPE_UNDEAD) and (GetUnitAbilityLevel(u,'Abgs')!= 0 or GetUnitAbilityLevel(u,'Abgl')!= 0) or GetUnitAbilityLevel(u,'Abds')!= 0 or GetUnitAbilityLevel(u,'Abdl')!= 0
    endfunction
    But will this work only for default human and undead structures ?
    What about custom ones and the other races like night elves ?
     
  11. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,426
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    @Aniki We moved your math functions to the small code snippets thread. Since the merging is done by date, your post ended up on the previous page here:
    Small Code Snippets

    If possible, could you provide a brief comment on the functions/function list? :) It probably isn't necessary for ceil/floor, but it would be helpful for the "nearest" functions.
     
  12. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    397
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    To check if a unit is invulnerable:

    Code (vJASS):
    library InvulnerabilityChecker

        function CheckInvulnerability takes unit u returns boolean
            local real origHP = GetWidgetLife(u)
            local real origMP = GetUnitState(u, UNIT_STATE_MANA)
            local boolean check
            call UnitDamageTarget(u, u, 0.01, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_UNIVERSAL, null)
            set check = GetWidgetLife(u) == origHP and GetUnitState(u, UNIT_STATE_MANA) == origMP
            call SetWidgetLife(u, origHP)
            call SetUnitState(u, UNIT_STATE_MANA, origMP)
            return check
        endfunction

    endlibrary


    False positives from mana shields are accounted for.
     
    Last edited: Sep 4, 2016
  13. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,856
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Code (vJASS):
    if newHP != origHP then
               set check = false
           endif
           if newMP != origMP then
               set check = false
           endif

    -->
    Code (vJASS):
    if newHP != origHP or newMP != origMP then
               set check = false
           endif
     
  14. jakeZinc

    jakeZinc

    Joined:
    Aug 13, 2013
    Messages:
    1,373
    Resources:
    20
    Spells:
    20
    Resources:
    20
    The newhp and mp locals are not needed in a variable, you can directly call it's func once.
     
  15. Aniki

    Aniki

    Joined:
    Nov 7, 2014
    Messages:
    518
    Resources:
    4
    Spells:
    1
    JASS:
    3
    Resources:
    4
    Code (vJASS):

    if newHP != origHP then
               set check = false
           endif
           if newMP != origMP then
               set check = false
           endif
     


    -->
    Code (vJASS):

    if newHP != origHP or newMP != origMP then
               set check = false
           endif
     


    -->
    Code (vJASS):

    set check = newHp == origHP and newMP == origMP
     


    @AGD note that currently the function will report ethereal units as invulnerable, you need to change the attack type to
    ATTACK_TYPE_NORMAL
    , currently it's
    ATTACK_TYPE_CHAOS
    .
     
    Last edited: Sep 4, 2016
  16. jakeZinc

    jakeZinc

    Joined:
    Aug 13, 2013
    Messages:
    1,373
    Resources:
    20
    Spells:
    20
    Resources:
    20
    ^ Or

    Does it really need to include the mana for checking? And is this tested for accuracy? Will it always return false if the unit suddenly have hp or mp regeneration?
    ( You are checking the life state changes btw so it affects the unit that have regen )
     
    Last edited: Sep 4, 2016
  17. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,036
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Remind me why
    IsUnitInvulnerable
    is insufficient?


    Ah, cause it doesn't exist :p silly Blizzard
     
  18. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    397
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Okay, the neccessary changes were made. Thanks for pointing the errors btw.

    @jakeZinc These evaluations are done at a very tiny fraction of a second. And the unitstate regeneration happens at a much greater time interval. Btw, the mana evaluation is included to make sure units with mana shield dont register a false positive.
     
  19. jakeZinc

    jakeZinc

    Joined:
    Aug 13, 2013
    Messages:
    1,373
    Resources:
    20
    Spells:
    20
    Resources:
    20
    Hmmm, I guess so.

    Teka, you can post your randomEnum lib here so that if they can point out something minor then it can be optimized a bit further fellow. :)