• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Snippet] IsUnitSpellcasting

Level 7
Joined
Apr 30, 2011
Messages
359
simple IsUnitSpellcasting detection, please read the script + documentation bellow

JASS:
//===================================================================================
//      
//      Is Unit Spellcasting
//      -*- overcold_ice -*-
//      
//     -[*] Requirements:
//          - JNGP
//          - latest version of JassHelper
//      
//     -[*] Optional Requirements:
//          - UnitIndexer               [url]http://www.hiveworkshop.com/forums/jass-resources-412/system-unit-indexer-172090/[/url]
//          - /*New*/ Table             [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
//          - RegisterPlayerUnitEvent   [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-registerplayerunitevent-203338/[/url]
//      
//          For your spell-casting needs
//      This system provides IsUnitSpellcasting functions, enabling users to detect whether
//      the unit is casting a spell, channeling, finished casting, etc.
//      
//     -[*] API:
//      
//      function IsUnitS_$CAST$ takes unit u returns boolean
//      
//      $CAST$:
//          - Casting
//              returns true if the unit is casting in any phase
//          - PreCasting
//              returns true if the unit has begin its casting animation, but hasn't
//              started the ability
//          - Channeling
//              returns true if the unit channels an ability
//          - NotCasting
//              returns true if the unit doesn't cast anything
//          - FinishCasting
//              returns true if the unit has finished casting an ability
//      
//===================================================================================
library IsUnitSpellcasting initializer Init requires optional UnitIndexer, optional /*New*/Table, optional RegisterPlayerUnitEvent
    
    private struct V
        static if LIBRARY_UnitIndexer then
            static boolean array casting
            static boolean array begincs
            static boolean array channel
            static boolean array stopcst
            static boolean array finishc
        elseif LIBRARY_Table then
            static TableArray    table
        else
            static hashtable     ht = InitHashtable()
        endif
        
        static if not LIBRARY_UnitIndexer then
            static constant key  casting_k
            static constant key  begincs_k
            static constant key  channel_k
            static constant key  stopcst_k
            static constant key  finishc_k
        endif
    endstruct
    
    private function CastBegin takes nothing returns nothing
        static if LIBRARY_UnitIndexer then
            local integer i = GetUnitUserData(GetTriggerUnit())
            
            set V.casting [i] = true
            set V.begincs [i] = true
            set V.stopcst [i] = false
            set V.finishc [i] = false
        elseif LIBRARY_Table then
            local integer i = GetHandleId(GetTriggerUnit())
            
            call V.table [i].boolean [V.casting_k] = true
            call V.table [i].boolean [V.begincs_k] = true
            call V.table [i].boolean [V.stopcst_k] = false
            call V.table [i].boolean [V.finishc_k] = false
        else
            local integer i = GetHandleId(GetTriggerUnit())
            
            call SaveBoolean(V.ht, i, V.casting_k, true)
            call SaveBoolean(V.ht, i, V.begincs_k, true)
            call SaveBoolean(V.ht, i, V.stopcst_k, false)
            call SaveBoolean(V.ht, i, V.finishc_k, false)
        endif
    endfunction
    
    private function CastStart takes nothing returns nothing
        static if LIBRARY_UnitIndexer then
            local integer i = GetUnitUserData(GetTriggerUnit())
            
            set V.begincs [i] = false
        elseif LIBRARY_Table then
            local integer i = GetHandleId(GetTriggerUnit())
            
            call V.table [i].boolean [V.begincs_k] = false
        else
            local integer i = GetHandleId(GetTriggerUnit())
            
            call SaveBoolean(V.ht, i, V.begincs_k, false)
        endif
    endfunction
    
    private function ChannelStart takes nothing returns nothing
        static if LIBRARY_UnitIndexer then
            local integer i = GetUnitUserData(GetTriggerUnit())
            
            set V.begincs [i] = false
            set V.channel [i] = true
        elseif LIBRARY_Table then
            local integer i = GetHandleId(GetTriggerUnit())
            
            call V.table [i].boolean [V.begincs_k] = false
            call V.table [i].boolean [V.channel_k] = true
        else
            local integer i = GetHandleId(GetTriggerUnit())
            
            call SaveBoolean(V.ht, i, V.begincs_k, false)
            call SaveBoolean(V.ht, i, V.channel_k, true)
        endif
    endfunction
    
    private function CastEnd takes nothing returns nothing
        static if LIBRARY_UnitIndexer then
            local integer i = GetUnitUserData(GetTriggerUnit())
            
            set V.casting [i] = false
            set V.begincs [i] = false
            set V.channel [i] = false
            set V.stopcst [i] = true
        elseif LIBRARY_Table then
            local integer i = GetHandleId(GetTriggerUnit())
            
            call V.table [i].boolean [V.casting_k] = false
            call V.table [i].boolean [V.begincs_k] = false
            call V.table [i].boolean [V.channel_k] = false
            call V.table [i].boolean [V.stopcst_k] = true
        else
            local integer i = GetHandleId(GetTriggerUnit())
            
            call SaveBoolean(V.ht, i, V.casting_k, false)
            call SaveBoolean(V.ht, i, V.begincs_k, false)
            call SaveBoolean(V.ht, i, V.channel_k, false)
            call SaveBoolean(V.ht, i, V.stopcst_k, true)
        endif
    endfunction
    
    private function CastFinish takes nothing returns nothing
        static if LIBRARY_UnitIndexer then
            local integer i = GetUnitUserData(GetTriggerUnit())
            
            set V.finishc [i] = true
        elseif LIBRARY_Table then
            local integer i = GetHandleId(GetTriggerUnit())
            
            call V.table [i].boolean [V.finishc_k] = true
        else
            local integer i = GetHandleId(GetTriggerUnit())
            
            call SaveBoolean(V.ht, i, V.finishc_k, true)
        endif
    endfunction
    
    private function Init takes nothing returns nothing
        static if LIBRARY_RegisterPlayerUnitEvent then
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST,    function CastBegin)
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_EFFECT,  function CastStart)
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_CHANNEL, function ChannelStart)
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_ENDCAST, function CastEnd)
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_ENDCAST, function CastFinish)
        else
            local trigger t = CreateTrigger()
            local code c = function CastBegin
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_CAST)
            call TriggerAddCondition(t, Filter(c))
            set t = CreateTrigger()
            set c = function CastStart
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
            call TriggerAddCondition(t, Filter(c))
            set t = CreateTrigger()
            set c = function ChannelStart
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_CHANNEL)
            call TriggerAddCondition(t, Filter(c))
            set t = CreateTrigger()
            set c = function CastEnd
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_ENDCAST)
            call TriggerAddCondition(t, Filter(c))
            set t = CreateTrigger()
            set c = function CastFinish
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_FINISH)
            call TriggerAddCondition(t, Filter(c))
            
            set t = null
        endif
        
        static if LIBRARY_Table then
            set V.table = TableArray [8190]
        endif
    endfunction
    
    //====================================================================================
    //  A P I
    //====================================================================================
    function IsUnitS_Casting takes unit u returns boolean
        static if LIBRARY_UnitIndexer then
            return V.casting [GetUnitUserData(u)]
        elseif LIBRARY_Table then
            return V.table [GetHandleId(u)].boolean [V.casting_k]
        else
            return GetSavedBoolean(V.ht, GetHandleId(u), V.casting_k)
        endif
    endfunction
    
    function IsUnitS_PreCasting takes unit u returns boolean
        static if LIBRARY_UnitIndexer then
            return V.begincs [GetUnitUserData(u)]
        elseif LIBRARY_Table then
            return V.table [GetHandleId(u)].boolean [V.begincs_k]
        else
            return GetSavedBoolean(V.ht, GetHandleId(u), V.begincs_k)
        endif
    endfunction
    
    function IsUnitS_Channeling takes unit u returns boolean
        static if LIBRARY_UnitIndexer then
            return V.channel [GetUnitUserData(u)]
        elseif LIBRARY_Table then
            return V.table [GetHandleId(u)].boolean [V.channel_k]
        else
            return GetSavedBoolean(V.ht, GetHandleId(u), V.channel_k)
        endif
    endfunction
    
    function IsUnitS_NotCasting takes unit u returns boolean
        static if LIBRARY_UnitIndexer then
            return V.stopcst [GetUnitUserData(u)]
        elseif LIBRARY_Table then
            return V.table [GetHandleId(u)].boolean [V.stopcst_k]
        else
            return GetSavedBoolean(V.ht, GetHandleId(u), V.stopcst_k)
        endif
    endfunction
    
    function IsUnitS_FinishCasting takes unit u returns boolean
        static if LIBRARY_UnitIndexer then
            return V.finishc [GetUnitUserData(u)]
        elseif LIBRARY_Table then
            return V.table [GetHandleId(u)].boolean [V.finishc_k]
        else
            return GetSavedBoolean(V.ht, GetHandleId(u), V.finishc_k)
        endif
    endfunction
endlibrary
 
Last edited:
Level 7
Joined
Apr 30, 2011
Messages
359
i know why we use Filter to make it . . .
but what is the difference of using that method (code variable) with one that doesn't use any variables?
 
Top