[Snippet] IsUnitChanneling

Scripts

14 years
edited 8 years
This is a small and clear resource that detects if a unit is channeling or not.

Code (jass) Select
/**************************************
*
*   IsUnitChanneling
*   v2.1.0.0
*   By Magtheridon96
*
*   - Tells whether a unit is channeling or not.
*
*   Requirements:
*   -------------
*
*       - RegisterPlayerUnitEvent by Magtheridon96
*           - https://wc3modding.info/4907/snippet-registerplayerunitevent/
*
*       Optional:
*       ---------
*
*           - UnitIndexer by Nestharus
*               - https://wc3modding.info/4607/unitindexer/
*           - Table by Bribe
*               - https://wc3modding.info/4611/snippet-new-table/
*
*   API:
*   ----
*
*       - function IsUnitChanneling takes unit whichUnit returns boolean
*           - Tells whether a unit is channeling or not.
*             (This function is only available if you have UnitIndexer)
*
*       - function IsUnitChannelingById takes integer unitIndex returns boolean
*           - Tells whether a unti is channeling or not given the unit index.
*
**************************************/
library IsUnitChanneling requires optional UnitIndexer, optional Table, RegisterPlayerUnitEvent
   
    private struct OnChannel extends array
        static if LIBRARY_UnitIndexer then
            static boolean array channeling
        else
            static if LIBRARY_Table then
                static key k
                static Table channeling = k
            else
                static hashtable hash = InitHashtable()
            endif
        endif
       
        private static method onEvent takes nothing returns nothing
            static if LIBRARY_UnitIndexer then
                local integer id = GetUnitUserData(GetTriggerUnit())
                set channeling[id] = not channeling[id]
            else
                static if LIBRARY_Table then
                    local integer id = GetHandleId(GetTriggerUnit())
                    set channeling.boolean[id] = not channeling.boolean[id]
                else
                    local integer id = GetHandleId(GetTriggerUnit())
                    call SaveBoolean(hash, 0, id, not LoadBoolean(hash, 0, id))
                endif
            endif
        endmethod
       
        private static method onInit takes nothing returns nothing
           call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_CHANNEL, function thistype.onEvent)
           call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_ENDCAST, function thistype.onEvent)
        endmethod
    endstruct
   
    static if LIBRARY_UnitIndexer then
        function IsUnitChannelingById takes integer id returns boolean
            return OnChannel.channeling[id]
        endfunction
    endif
   
    function IsUnitChanneling takes unit u returns boolean
        static if LIBRARY_UnitIndexer then
            return OnChannel.channeling[GetUnitUserData(u)]
        else
            static if LIBRARY_Table then
                return OnChannel.channeling.boolean[GetHandleId(u)]
            else
                return LoadBoolean(OnChannel.hash, 0, GetHandleId(u))
            endif
        endif
    endfunction
   
endlibrary


Feel free to comment..
13 years
And this snippet has been approved by me :)
13 years
Updated! Now, UnitIndexer is optional.
If you don't have UnitIndexer, it will run on Table, and if you don't have Table, it will use a regular hashtable ^.^