[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
1
/**************************************
2
*
3
*   IsUnitChanneling
4
*   v2.1.0.0
5
*   By Magtheridon96
6
*
7
*   - Tells whether a unit is channeling or not.
8
*
9
*   Requirements:
10
*   -------------
11
*
12
*       - RegisterPlayerUnitEvent by Magtheridon96
13
*           - https://wc3modding.info/4907/snippet-registerplayerunitevent/
14
*
15
*       Optional:
16
*       ---------
17
*
18
*           - UnitIndexer by Nestharus
19
*               - https://wc3modding.info/4607/unitindexer/
20
*           - Table by Bribe
21
*               - https://wc3modding.info/4611/snippet-new-table/
22
*
23
*   API:
24
*   ----
25
*
26
*       - function IsUnitChanneling takes unit whichUnit returns boolean
27
*           - Tells whether a unit is channeling or not.
28
*             (This function is only available if you have UnitIndexer)
29
*
30
*       - function IsUnitChannelingById takes integer unitIndex returns boolean
31
*           - Tells whether a unti is channeling or not given the unit index.
32
*
33
**************************************/
34
library IsUnitChanneling requires optional UnitIndexer, optional Table, RegisterPlayerUnitEvent
35
    
36
    private struct OnChannel extends array
37
        static if LIBRARY_UnitIndexer then
38
            static boolean array channeling
39
        else
40
            static if LIBRARY_Table then
41
                static key k
42
                static Table channeling = k
43
            else
44
                static hashtable hash = InitHashtable()
45
            endif
46
        endif
47
        
48
        private static method onEvent takes nothing returns nothing
49
            static if LIBRARY_UnitIndexer then
50
                local integer id = GetUnitUserData(GetTriggerUnit())
51
                set channeling[id] = not channeling[id]
52
            else
53
                static if LIBRARY_Table then
54
                    local integer id = GetHandleId(GetTriggerUnit())
55
                    set channeling.boolean[id] = not channeling.boolean[id]
56
                else
57
                    local integer id = GetHandleId(GetTriggerUnit())
58
                    call SaveBoolean(hash, 0, id, not LoadBoolean(hash, 0, id))
59
                endif
60
            endif
61
        endmethod
62
        
63
        private static method onInit takes nothing returns nothing
64
           call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_CHANNEL, function thistype.onEvent)
65
           call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_ENDCAST, function thistype.onEvent)
66
        endmethod
67
    endstruct
68
    
69
    static if LIBRARY_UnitIndexer then
70
        function IsUnitChannelingById takes integer id returns boolean
71
            return OnChannel.channeling[id]
72
        endfunction
73
    endif
74
    
75
    function IsUnitChanneling takes unit u returns boolean
76
        static if LIBRARY_UnitIndexer then
77
            return OnChannel.channeling[GetUnitUserData(u)]
78
        else
79
            static if LIBRARY_Table then
80
                return OnChannel.channeling.boolean[GetHandleId(u)]
81
            else
82
                return LoadBoolean(OnChannel.hash, 0, GetHandleId(u))
83
            endif
84
        endif
85
    endfunction
86
    
87
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 ^.^