[System] Heal

Scripts

14 years
edited 8 years
This system efficiently handles healing units instantly and over time.

Code (jass) Select
1
/************************************************************
2
*
3
*   Heal
4
*   v4.0.0.3
5
*   By Magtheridon96
6
*
7
*   - Heals units instantly or over time.
8
*   - Can also allow Damage over time with negative heal amount.
9
*   - Comes with heal events:
10
*
11
*       - Heal.ANY
12
*           - Fires on any healing event.
13
*           - This would be used for systems like:
14
*               - Accurate Unit Regeneration Logs
15
*               - Accurate Damage Logs
16
*               - Heal Block/Spell Effect Block
17
*
18
*       - Heal.INSTANT
19
*           - Fires on instant heal events.
20
*           - This would be used for systems like:
21
*               - Texttag Displays
22
*
23
*       - Heal.TIMED
24
*           - Fires 32x a second during a timed heal.
25
*           - This was only added for a sense of 
26
*             completeness. It may be useful.
27
*
28
*   - Requires:
29
*
30
*       - UnitEvent by Nestharus
31
*           - hiveworkshop.com/forums/jass-resources-412/extension-unit-event-172365/
32
*       - UnitIndexer by Nestharus
33
*           - hiveworkshop.com/forums/jass-resources-412/system-unit-indexer-172090/
34
*       - Event by Nestharus
35
*           - hiveworkshop.com/forums/jass-resources-412/snippet-event-186555/
36
*       - CTL by Nestharus
37
*           - hiveworkshop.com/forums/jass-resources-412/snippet-constant-timer-loop-32-a-201381/
38
*
39
*   - API:
40
*     ----
41
*
42
*       - struct Heal extends array
43
*           - readonly static Event ANY
44
*           - readonly static Event TIMED
45
*           - readonly static Event INSTANT
46
*               - Heal events
47
*
48
*           - static boolean enabled
49
*               - Used to enable/disable the system
50
*
51
*           - static method healUnit takes unit source, unit target, real amount returns nothing
52
*           - static method healUnitOverTime takes unit source, unit target, real amount, real duration returns nothing
53
*               - Heal a unit instantly or over time
54
*
55
*       - function RegisterInstantHealEvent takes code c returns nothing
56
*       - function RegisterTimedHealEvent takes code c returns nothing
57
*       - function RegisterAnyHealEvent takes code c returns nothing
58
*           - These functions register specific heal events that fire differently.
59
*
60
*       - function GetHealingUnit takes nothing returns unit
61
*       - function GetHealingUnitId takes nothing returns integer
62
*           - These functions are used to get the healing unit or its id.
63
*
64
*       - function GetHealedUnit takes nothing returns unit
65
*       - function GetHealedUnitId takes nothing returns integer
66
*           - These functions are used to get the healed unit or its id.
67
*
68
*       - function GetHealedAmount takes nothing returns real
69
*       - function GetEffectiveHealedAmount takes nothing returns real
70
*       - function GetOriginalLife takes nothing returns real
71
*           - These functions are used to get:
72
*               - Amount of HP intended to be healed.
73
*               - Amount of HP actually healed.
74
*               - Original HP before healing.
75
*
76
*       - function HealUnit takes unit source, unit target, real amount returns nothing
77
*       - function HealUnitOverTime takes unit source, unit target, real amount, real duration returns nothing
78
*           - These are the wrappers used for healing units.
79
*
80
************************************************************/
81
library Heal requires UnitIndexer, UnitEvent, Event, CTL
82
83
    private module Init
84
        private static method onInit takes nothing returns nothing
85
            set INSTANT = CreateEvent()
86
            set TIMED = CreateEvent()
87
            set ANY = CreateEvent()
88
        endmethod
89
    endmodule
90
    
91
    struct Heal extends array
92
    
93
        readonly static Event INSTANT
94
        readonly static Event TIMED
95
        readonly static Event ANY
96
        
97
        static boolean enabled = true
98
        
99
        static UnitIndex sourceId = 0
100
        static UnitIndex targetId = 0
101
        
102
        static real amount = 0.0
103
        static real original = 0.0
104
        static real effective = 0.0
105
        
106
        private static unit array source
107
        private static unit array target
108
        private static real array regen
109
        private static real array duration
110
        
111
        private static method lock takes nothing returns nothing
112
            if sourceId != 0 then
113
                call sourceId.lock()
114
            endif
115
            if targetId != 0 then
116
                call targetId.lock()
117
            endif
118
        endmethod
119
        
120
        private static method unlock takes nothing returns nothing
121
            if sourceId != 0 then
122
                call sourceId.unlock()
123
            endif
124
            if targetId != 0 then
125
                call targetId.unlock()
126
            endif
127
        endmethod
128
        
129
        implement CTL
130
        
131
            local real r
132
            local integer u
133
            
134
        implement CTLExpire
135
        
136
            set r = GetWidgetLife(target[this])
137
            set u = GetUnitUserData(target[this])
138
            
139
            if not IsUnitDead(u) and duration[this] <= 0. then
140
                set duration[this] = duration[this] - 0.03125
141
                call SetWidgetLife(target[this], r + regen[this])
142
                
143
                if enabled then
144
                
145
                    set sourceId = GetUnitUserData(source[this])
146
                    set targetId = u
147
                    set amount = regen[this]
148
                    set original = r
149
                    set effective = GetWidgetLife(target[this]) - r
150
                    
151
                    call lock()
152
                    
153
                    call TIMED.fire()
154
                    call ANY.fire()
155
                    
156
                    call unlock()
157
                    
158
                endif
159
            else
160
                set source[this] = null
161
                set target[this] = null
162
                
163
                call this.destroy()
164
            endif
165
            
166
        implement CTLNull
167
        implement CTLEnd
168
        
169
        static method healUnit takes unit source, unit target, real howMuch returns nothing
170
            local real r = GetWidgetLife(target)
171
            local integer u = GetUnitUserData(target)
172
            
173
            if not IsUnitDead(u) then
174
                call SetWidgetLife(target, r + howMuch)
175
                
176
                if enabled then
177
                
178
                    set sourceId = GetUnitUserData(source)
179
                    set targetId = u
180
                    set amount = howMuch
181
                    set original = r
182
                    set effective = GetWidgetLife(target) - r
183
                    
184
                    call lock()
185
                    
186
                    call INSTANT.fire()
187
                    call ANY.fire()
188
                    
189
                    call unlock()
190
                    
191
                endif
192
            endif
193
        endmethod
194
        
195
        static method healUnitOverTime takes unit src, unit trgt, real howMuch, real dur returns nothing
196
            local thistype this = create()
197
            
198
            set source[this] = src
199
            set target[this] = trgt
200
            set regen[this] = 0.03125 * howMuch / dur
201
            
202
            set duration[this] = dur
203
        endmethod
204
        
205
        implement Init
206
    endstruct
207
    
208
    function RegisterInstantHealEvent takes code c returns nothing
209
        call Heal.INSTANT.register(Filter(c))
210
        return
211
    endfunction
212
    
213
    function RegisterTimedHealEvent takes code c returns nothing
214
        call Heal.TIMED.register(Filter(c))
215
        return
216
    endfunction
217
    
218
    function RegisterAnyHealEvent takes code c returns nothing
219
        call Heal.ANY.register(Filter(c))
220
        return
221
    endfunction
222
    
223
    function GetHealingUnit takes nothing returns unit
224
        return GetUnitById(Heal.sourceId)
225
    endfunction
226
    
227
    function GetHealingUnitId takes nothing returns integer
228
        return Heal.sourceId
229
    endfunction
230
    
231
    function GetHealedUnit takes nothing returns unit
232
        return GetUnitById(Heal.targetId)
233
    endfunction
234
    
235
    function GetHealedUnitId takes nothing returns integer
236
        return Heal.targetId
237
    endfunction
238
    
239
    function GetHealedAmount takes nothing returns real
240
        return Heal.amount
241
    endfunction
242
    
243
    function GetEffectiveHealedAmount takes nothing returns real
244
        return Heal.effective
245
    endfunction
246
    
247
    function GetOriginalLife takes nothing returns real
248
        return Heal.original
249
    endfunction
250
    
251
    function HealUnit takes unit source, unit target, real amount returns nothing
252
        call Heal.healUnit(source,target,amount)
253
    endfunction
254
    
255
    function HealUnitOverTime takes unit source, unit target, real amount, real duration returns nothing
256
        call Heal.healUnitOverTime(source,target,amount,duration)
257
    endfunction
258
    
259
endlibrary


Demo Code

Code (jass) Select
1
struct Tester extends array
2
3
    private static unit fighters1
4
    private static unit fighters2
5
6
    private static unit regen
7
8
    private static method instant takes nothing returns nothing
9
        call HealUnit(null, fighters1, 150)
10
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Healed unit for 150 HP")
11
    endmethod
12
13
    private static method onInit takes nothing returns nothing
14
        
15
        set fighters1 = CreateUnit(Player(0), 'Hpal', 128, 0, 180)
16
        set fighters2 = CreateUnit(Player(1), 'Hpal', -128, 0, 0)
17
        call TimerStart(CreateTimer(), 10, true, function thistype.instant)
18
        
19
        set regen = CreateUnit(Player(0), 'Hpal', 0, 1024, 270)
20
        call CreateUnit(Player(1), 'Hpal', 0, 1024, 270)
21
        call SetWidgetLife(regen, GetUnitState(regen, UNIT_STATE_MAX_LIFE) / 2)
22
        
23
        // This will regenerate 500000 HP over 50000 seconds meaning 10 HP/sec
24
        call HealUnitOverTime(null, regen, 500000, 50000)
25
        
26
        call PauseUnit(regen, true)
27
    endmethod
28
29
endstruct


Feel free to comment..
14 years
I have something in mind that this script might be useful, I'll test it if it will work ;), thanks for sharing.
14 years
Thanks man :D
Yeah, this is useful because of the events, and you're forced to use it if you want accurate unit regeneration value retrieval :P (If you're also using my GetUnitRegeneration script)
14 years
Hi Magh, remember me??
14 years
Oh yes. I do remember you.
I hope you don't give yourself the same reputation here as you did on Hive.
14 years
Oh yeah..
But, how to heal like this:
Healing targeted unit for 20% of targeted unit maximum HP....
Please give me the codes :D :D
14 years
call HealUnit(null, udg_YOUR_UNIT, 0.2 * GetUnitState(udg_YOUR_UNIT, UNIT_STATE_MAX_LIFE))
14 years
Thunk'z ;) ;)
But why you banned me on Hive?? Y.Y
14 years
Ok ;) ;)