[Snippet] GetUnitRegeneration

Scripts

14 years
edited 8 years
The title explains it all.

Code (jass) Select
1
/********************************************************
2
*
3
*   GetUnitRegeneration
4
*   v2.0.0.0
5
*   By Magtheridon96
6
*
7
*   - Gets unit regeneration values
8
*
9
*   Requirements:
10
*   -------------
11
*
12
*       - UnitIndexer by Nestharus
13
*           - https://wc3modding.info/4607/unitindexer/
14
*       - DamageEvent by Nestharus
15
*           - hiveworkshop.com/forums/jass-resources-412/snippet-damageevent-186829/
16
*       - Heal by Magtheridon96
17
*           - https://wc3modding.info/4910/system-heal/
18
*       - CTL by Nestharus
19
*           - hiveworkshop.com/forums/jass-resources-412/snippet-constant-timer-loop-32-a-201381/
20
*
21
*       Optional:
22
*       ---------
23
*
24
*       - DamageControl by PurgeandFire111
25
*           - hiveworkshop.com/forums/jass-resources-412/system-damage-control-204672/
26
*
27
*   API:
28
*   ----
29
*
30
*       - function IsUnitRegenerating takes unit u returns boolean
31
*           - Tells whether a unit is regenerating or not.
32
*       - function IsUnitRegeneratingById takes integer i returns boolean
33
*           - Tells whether a unit is regenerating or not given the Id.
34
*       - function GetUnitRegeneration takes unit u returns real
35
*           - Gets the amount of regeneration for a given unit.
36
*       - function GetUnitRegenerationById takes integer i returns real
37
*           - Gets the amount of regeneration for a given unit given the Id.
38
*       - function IsRegenerationPositive takes unit u returns boolean
39
*           - Tells whether the regeneration is positive.
40
*       - function IsRegenerationPositiveById takes integer i returns boolean
41
*           - Tells whether the regeneration is positive given the Id.
42
*
43
********************************************************/
44
library GetUnitRegeneration requires UnitIndexer, DamageEvent, Heal, CTL, optional DamageControl
45
    
46
    private struct UnitRegen extends array
47
        
48
        static thistype array next
49
        static thistype array prev
50
        
51
        static real array life
52
        static real array max
53
        static real array damaged
54
        static real array amount
55
        
56
        static boolean array regenerating
57
        
58
        static method damage takes nothing returns nothing
59
            static if LIBRARY_DamageControl then
60
                set damaged[DamageEvent.targetId] = damaged[DamageEvent.targetId] + DamageControl.result
61
            else
62
                set damaged[DamageEvent.targetId] = damaged[DamageEvent.targetId] + DamageEvent.amount
63
            endif
64
        endmethod
65
        
66
        static method heal takes nothing returns nothing
67
            set damaged[Heal.targetId] = damaged[Heal.targetId] - Heal.effective
68
        endmethod
69
        
70
        static method index takes nothing returns nothing
71
            // Add index to list
72
            set next[GetIndexedUnitId()] = 0
73
            set prev[GetIndexedUnitId()] = prev[0]
74
            set next[prev[0]] = GetIndexedUnitId()
75
            set prev[0] = GetIndexedUnitId()
76
            
77
            // Setting current life and max life
78
            set life[GetIndexedUnitId()] = GetWidgetLife(GetIndexedUnit())
79
            set max[GetIndexedUnitId()] = GetUnitState(GetIndexedUnit(), UNIT_STATE_MAX_LIFE)
80
        endmethod
81
        
82
        static method deindex takes nothing returns nothing            
83
            // Remove from list
84
            set next[prev[GetIndexedUnitId()]] = next[GetIndexedUnitId()]
85
            set prev[next[GetIndexedUnitId()]] = prev[GetIndexedUnitId()]
86
            
87
            // Resetting Data
88
            set amount[GetIndexedUnitId()] = 0
89
            set damaged[GetIndexedUnitId()] = 0
90
            
91
            set regenerating[GetIndexedUnitId()] = false
92
        endmethod
93
        
94
        implement CT32
95
        
96
            local thistype this = next[0]
97
            local real hp = 0
98
            local real ml = 0
99
            
100
            loop
101
                exitwhen 0 == this
102
                
103
                set ml = GetUnitState(GetUnitById(this), UNIT_STATE_MAX_LIFE)
104
                
105
                if max[this] != ml then
106
                    set life[this] = life[this] * (ml / max[this])
107
                endif
108
                
109
                set hp = GetWidgetLife(GetUnitById(this)) + damaged[this]
110
                
111
                if hp != life[this] then
112
                    set amount[this] = (hp - life[this]) / 0.03125
113
                    set regenerating[this] = true
114
                    set life[this] = hp - damaged[this]
115
                else
116
                    set regenerating[this] = false
117
                    set amount[this] = 0
118
                endif
119
                
120
                set damaged[this] = 0
121
                set max[this] = ml
122
                
123
                set this = next[this]
124
            endloop
125
            
126
        implement CT32End
127
        
128
        private static code indexCode
129
        private static code deindexCode
130
        private static code damageCode
131
        private static code healCode
132
        
133
        private static method onInit takes nothing returns nothing
134
        
135
            set indexCode = function thistype.index
136
            set deindexCode = function thistype.deindex
137
            set damageCode = function thistype.damage
138
            set healCode = function thistype.heal
139
            
140
            static if LIBRARY_DamageControl then
141
                call DamageControl.FINAL.register(Filter(damageCode))
142
            else
143
                call DamageEvent.ANY.register(Filter(damageCode))
144
            endif
145
            
146
            call RegisterAnyHealEvent(healCode)
147
            call UnitIndexer.INDEX.register(Filter(indexCode))
148
            call UnitIndexer.DEINDEX.register(Filter(deindexCode))
149
        endmethod
150
        
151
    endstruct
152
    
153
    function IsUnitRegenerating takes unit u returns boolean
154
        return UnitRegen.regenerating[GetUnitUserData(u)]
155
    endfunction
156
    
157
    function IsUnitRegeneratingById takes integer i returns boolean
158
        return UnitRegen.regenerating[i]
159
    endfunction
160
    
161
    function GetUnitRegeneration takes unit u returns real
162
        return UnitRegen.amount[GetUnitUserData(u)]
163
    endfunction
164
    
165
    function GetUnitRegenerationById takes integer i returns real
166
        return UnitRegen.amount[i]
167
    endfunction
168
    
169
    function IsRegenerationPositive takes unit u returns boolean
170
        return UnitRegen.amount[GetUnitUserData(u)] >= 0
171
    endfunction
172
    
173
    function IsRegenerationPositiveById takes integer i returns boolean
174
        return UnitRegen.amount[i] >= 0
175
    endfunction
176
    
177
endlibrary
178
179
library IsUnitRegenerating requires GetUnitRegeneration
180
endlibrary


Feel free to comment..
14 years
This one looks really useful, I can think of some stuffs that you can do here, but it will really nice if you can put a list of all the things that you can do with this script. ;)
14 years
Thanks :D
Updated the code. I found a useless line in the deindex method.