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.

UnitToggling

Discussion in 'Graveyard' started by Cheezeman, Oct 14, 2009.

  1. Cheezeman

    Cheezeman

    Joined:
    Aug 19, 2008
    Messages:
    437
    Resources:
    2
    Spells:
    1
    Tutorials:
    1
    Resources:
    2
    A very simple module, takes care of most stacking problems of on/off natives like PauseUnit or SetUnitInvulnerable.
    Comes in two versions. A third one is planned but I don't know if it's required.
    Header should explain everything.


    UnitToggling


    Requires UnitIndexingUtils

    Code (vJASS):

    //===========================================================================
    //==    
    //==        UnitToggling
    //==       ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    //==    Written by Cheezeman    
    //==    
    //==    This module lets you use certain natives without having to worry
    //==    about the stacking issue that comes with them.
    //==    For example; You have two abilities that pauses a unit over time. The
    //==    unit will be unpause when the first spell's duration runs out, no matter
    //==    what.
    //==    What do you do?
    //==    You replace PauseUnit() with PauseUnitEx()
    //==    There are many natives included in this pack, some more useful than the
    //==    others
    //==    
    //==    
    //==    Natives
    //==    ¯¯¯¯¯¯¯
    //==    *Note* Functions with > in front of them use hashtable
    //==           rather than integer arrays.
    //==
    //==    call PauseUnitEx( unit whichUnit, boolean flag )
    //==    call ShowUnitEx( unit whichUnit, boolean flag )
    //==    call SetUnitPathingEx( unit whichUnit, boolean flag )
    //==    call SetUnitInvulnerableEx( unit whichUnit, boolean flag )
    //==    call SetUnitExplodedEx( unit whichUnit, boolean flag )
    //==    call SetUnitUseFoodEx( unit whichUnit, boolean flag )
    //==    > call UnitAddAbilityEx( unit whichUnit, integer abilityId )
    //==    > call UnitRemoveAbilityEx( unit whichUnit, integer abilityId )
    //==    
    //==    
    //==    Special version
    //==    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    //==    If your map already use UnitUserData for other purposes than
    //==    unit indexing, you can use UnitToggling - Special version.
    //==    It is, in difference to this standard, completely self-sufficient,
    //==    but a great deal slower.
    //==    I recommend the standard, but if you have no other option you
    //==    should use the special.
    //==    
    //==     ____________________
    //==    I Version 1.3       I
    //==    I Pack 1.0          I
    //==    I Made by Cheezeman I
    //==     ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    //===========================================================================
    library UnitToggling initializer Init requires UnitIndexingUtils
        globals
            private hashtable Hash
            private integer array PauseUnitA
            private integer array ShowUnitA
            private integer array SetUnitInvulnerableA
            private integer array SetUnitExplodedA
            private integer array SetUnitUseFoodA
            private integer array SetUnitPathingA
        endglobals
    //===========================================================================

        //! textmacro UnitToggling_Template takes NATIVE
            local integer i = $NATIVE$A[GetUnitId( u )]
            if flag then
                if i == 0 then
                    call $NATIVE$( u, true )
                endif
                set $NATIVE$A[GetUnitId( u )] = i + 1
            else
                set $NATIVE$A[GetUnitId( u )] = i - 1
                if i - 1 <= 0 then
                    set $NATIVE$A[GetUnitId( u )] = 0
                    call $NATIVE$( u, false )
                endif
            endif
        //! endtextmacro
       
    //===========================================================================
        function PauseUnitEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "PauseUnit" )
        endfunction

        function ShowUnitEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "ShowUnit" )
        endfunction

        function SetUnitInvulnerableEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "SetUnitInvulnerable" )
        endfunction

        function SetUnitExplodedEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "SetUnitExploded" )
        endfunction

        function SetUnitUseFoodEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "SetUnitUseFood" )
        endfunction

        function SetUnitPathingEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "SetUnitPathing" )
        endfunction
    //===========================================================================
        function UnitAddAbilityEx takes unit u, integer abil_id returns nothing
            local integer i = LoadInteger( Hash, abil_id, GetHandleId( u ) )
            if i == 0 then
                call UnitAddAbility( u, abil_id )
            endif
            call SaveInteger( Hash, abil_id, GetHandleId( u ), i + 1 )
        endfunction
       
        function UnitRemoveAbilityEx takes unit u, integer abil_id returns nothing
            local integer i = LoadInteger( Hash, abil_id, GetHandleId( u ) )
            call SaveInteger( Hash, abil_id, GetHandleId( u ), i - 1 )
            if i - 1 <= 0 then
                call RemoveSavedInteger( Hash, abil_id, GetHandleId( u ) )
                call UnitRemoveAbility( u, abil_id )
            endif
        endfunction
    //===========================================================================
        private function Init takes nothing returns nothing
            set Hash = InitHashtable()
        endfunction
    endlibrary
     


    UnitToggling - Special version

    Code (vJASS):

    //===========================================================================
    //==    
    //==        UnitToggling - Special version
    //==       ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    //==    Written by Cheezeman    
    //==    
    //==    This module lets you use certain natives without having to worry
    //==    about the stacking issue that comes with them.
    //==    For example; You have two abilities that pauses a unit over time. The
    //==    unit will be unpause when the first spell's duration runs out, no matter
    //==    what.
    //==    What do you do?
    //==    You replace PauseUnit() with PauseUnitEx()
    //==    There are many natives included in this pack, some more useful than the
    //==    others
    //==    
    //==    
    //==    Natives
    //==    ¯¯¯¯¯¯¯
    //==    call PauseUnitEx( unit whichUnit, boolean flag )
    //==    call ShowUnitEx( unit whichUnit, boolean flag )
    //==    call SetUnitPathingEx( unit whichUnit, boolean flag )
    //==    call SetUnitInvulnerableEx( unit whichUnit, boolean flag )
    //==    call SetUnitExplodedEx( unit whichUnit, boolean flag )
    //==    call SetUnitUseFoodEx( unit whichUnit, boolean flag )
    //==    call UnitAddAbilityEx( unit whichUnit, integer abilityId )
    //==    call UnitRemoveAbilityEx( unit whichUnit, integer abilityId )
    //==    
    //==    
    //==    Special version
    //==    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    //==    This version is, in difference to the standard, completely
    //==    self sufficient (independent of UnitUserData), but on the
    //==    other hand a great deal slower.
    //==    I recommend the standard version, but if you use UnitUserData
    //==    for other purposes than unit indexing you should use this
    //==    special version
    //==    
    //==     ____________________
    //==    I Version 1.3       I
    //==    I Pack 1.0          I
    //==    I Made by Cheezeman I
    //==     ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    //===========================================================================
    library UnitToggling initializer Init
        globals
            private hashtable Hash
            private constant integer PauseUnit_Key              = 0
            private constant integer ShowUnit_Key               = 1
            private constant integer SetUnitPathing_Key         = 2
            private constant integer SetUnitInvulnerable_Key    = 3
            private constant integer SetUnitExploded_Key        = 4
            private constant integer SetUnitUseFood_Key         = 5
            private constant integer UnitAddAbility_Key         = 6
        endglobals
    //===========================================================================

        //! textmacro UnitToggling_Template takes NATIVE
            local integer i = LoadInteger( Hash, $NATIVE$_Key, GetHandleId( u ) )
            if flag then
                if i == 0 then
                    call $NATIVE$( u, true )
                endif
                call SaveInteger( Hash, $NATIVE$_Key, GetHandleId( u ), i + 1 )
            else
                call SaveInteger( Hash, $NATIVE$_Key, GetHandleId( u ), i - 1 )
                if i - 1 <= 0 then
                    call RemoveSavedInteger( Hash, $NATIVE$_Key, GetHandleId( u ) )
                    call $NATIVE$( u, false )
                endif
            endif
        //! endtextmacro

    //===========================================================================
        function PauseUnitEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "PauseUnit" )
        endfunction
    //===========================================================================
        function ShowUnitEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "ShowUnit" )
        endfunction
    //===========================================================================
        function SetUnitInvulnerableEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "SetUnitInvulnerable" )
        endfunction
    //===========================================================================
        function SetUnitExplodedEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "SetUnitExploded" )
        endfunction
    //===========================================================================
        function SetUnitUseFoodEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "SetUnitUseFood" )
        endfunction
    //===========================================================================
        function SetUnitPathingEx takes unit u, boolean flag returns nothing
            //! runtextmacro UnitToggling_Template( "SetUnitPathing" )
        endfunction
    //===========================================================================
        function UnitAddAbilityEx takes unit u, integer abil_id returns nothing
            local integer i = LoadInteger( Hash, abil_id, GetHandleId( u ) )
            if i == 0 then
                call UnitAddAbility( u, abil_id )
            endif
            call SaveInteger( Hash, abil_id, GetHandleId( u ), i + 1 )
        endfunction
       
        function UnitRemoveAbilityEx takes unit u, integer abil_id returns nothing
            local integer i = LoadInteger( Hash, abil_id, GetHandleId( u ) )
            call SaveInteger( Hash, abil_id, GetHandleId( u ), i - 1 )
            if i - 1 <= 0 then
                call RemoveSavedInteger( Hash, abil_id, GetHandleId( u ) )
                call UnitRemoveAbility( u, abil_id )
            endif
        endfunction
    //===========================================================================
        private function Init takes nothing returns nothing
            set Hash = InitHashtable()
        endfunction
    endlibrary
     



    Suggestions of improvment are welcome.
     
    Last edited: Oct 15, 2009
  2. Thanathos

    Thanathos

    Joined:
    Aug 2, 2008
    Messages:
    202
    Resources:
    5
    Spells:
    5
    Resources:
    5
    I have a small question. Why do you create you hashtable in the Init function ? You could create it directly in the globals block and make it constant.
     
  3. YourNameHere

    YourNameHere

    Joined:
    Apr 29, 2007
    Messages:
    745
    Resources:
    4
    Maps:
    1
    Spells:
    2
    JASS:
    1
    Resources:
    4
    That takes up extra loading time.

    e/ lul at my answering times. I should really nerd less.
     
  4. Thanathos

    Thanathos

    Joined:
    Aug 2, 2008
    Messages:
    202
    Resources:
    5
    Spells:
    5
    Resources:
    5
    So using init functions is better than direct initialization ? I thought having the hashtable as constant might be slightly faster when you use it.
     
  5. Mooglefrooglian

    Mooglefrooglian

    Joined:
    Nov 28, 2008
    Messages:
    701
    Resources:
    0
    Resources:
    0
    The documentation says it adds "natives". A trivial issue, but something you may want to change since they are in no way natives. >.>

    Otherwise, great one.
     
  6. Cheezeman

    Cheezeman

    Joined:
    Aug 19, 2008
    Messages:
    437
    Resources:
    2
    Spells:
    1
    Tutorials:
    1
    Resources:
    2
    They're replacment for natives, but indeed not natives.
    Thanks for feedback though.
     
  7. Element of Water

    Element of Water

    Joined:
    Aug 3, 2008
    Messages:
    2,298
    Resources:
    5
    Spells:
    3
    Tutorials:
    1
    JASS:
    1
    Resources:
    5
    It's possible to make constant handles???
     
  8. Cheezeman

    Cheezeman

    Joined:
    Aug 19, 2008
    Messages:
    437
    Resources:
    2
    Spells:
    1
    Tutorials:
    1
    Resources:
    2
    How's that related to my script?
     
  9. Element of Water

    Element of Water

    Joined:
    Aug 3, 2008
    Messages:
    2,298
    Resources:
    5
    Spells:
    3
    Tutorials:
    1
    JASS:
    1
    Resources:
    5
    ^^^^^
     
  10. Cheezeman

    Cheezeman

    Joined:
    Aug 19, 2008
    Messages:
    437
    Resources:
    2
    Spells:
    1
    Tutorials:
    1
    Resources:
    2
    Ah, my bad.
     
  11. azlier

    azlier

    Joined:
    Oct 3, 2008
    Messages:
    354
    Resources:
    4
    JASS:
    4
    Resources:
    4
    Constants are the same speed as non-constants.

    Why, oh why, are you not using a unit indexer?
     
  12. Cheezeman

    Cheezeman

    Joined:
    Aug 19, 2008
    Messages:
    437
    Resources:
    2
    Spells:
    1
    Tutorials:
    1
    Resources:
    2
    What is the difference between unit indexer and unit indexing utils?
     
  13. Anachron

    Anachron

    Joined:
    Sep 9, 2007
    Messages:
    6,167
    Resources:
    66
    Icons:
    49
    Packs:
    2
    Tools:
    1
    Maps:
    3
    Spells:
    9
    Tutorials:
    1
    JASS:
    1
    Resources:
    66
    Atleast the name I guess, lol!
     
  14. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,037
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25