14 years
edited 8 years
This system efficiently handles healing units instantly and over time.
Demo Code
Feel free to comment..
1 /************************************************************2 *3 * Heal4 * v4.0.0.35 * By Magtheridon966 *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.ANY12 * - Fires on any healing event.13 * - This would be used for systems like:14 * - Accurate Unit Regeneration Logs15 * - Accurate Damage Logs16 * - Heal Block/Spell Effect Block17 *18 * - Heal.INSTANT19 * - Fires on instant heal events.20 * - This would be used for systems like:21 * - Texttag Displays22 *23 * - Heal.TIMED24 * - 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 Nestharus31 * - hiveworkshop.com/forums/jass-resources-412/extension-unit-event-172365/32 * - UnitIndexer by Nestharus33 * - hiveworkshop.com/forums/jass-resources-412/system-unit-indexer-172090/34 * - Event by Nestharus35 * - hiveworkshop.com/forums/jass-resources-412/snippet-event-186555/36 * - CTL by Nestharus37 * - hiveworkshop.com/forums/jass-resources-412/snippet-constant-timer-loop-32-a-201381/38 *39 * - API:40 * ----41 *42 * - struct Heal extends array43 * - readonly static Event ANY44 * - readonly static Event TIMED45 * - readonly static Event INSTANT46 * - Heal events47 *48 * - static boolean enabled49 * - Used to enable/disable the system50 *51 * - static method healUnit takes unit source, unit target, real amount returns nothing52 * - static method healUnitOverTime takes unit source, unit target, real amount, real duration returns nothing53 * - Heal a unit instantly or over time54 *55 * - function RegisterInstantHealEvent takes code c returns nothing56 * - function RegisterTimedHealEvent takes code c returns nothing57 * - function RegisterAnyHealEvent takes code c returns nothing58 * - These functions register specific heal events that fire differently.59 *60 * - function GetHealingUnit takes nothing returns unit61 * - function GetHealingUnitId takes nothing returns integer62 * - These functions are used to get the healing unit or its id.63 *64 * - function GetHealedUnit takes nothing returns unit65 * - function GetHealedUnitId takes nothing returns integer66 * - These functions are used to get the healed unit or its id.67 *68 * - function GetHealedAmount takes nothing returns real69 * - function GetEffectiveHealedAmount takes nothing returns real70 * - function GetOriginalLife takes nothing returns real71 * - 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 nothing77 * - function HealUnitOverTime takes unit source, unit target, real amount, real duration returns nothing78 * - 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 endmethod89 endmodule90
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 endif115 if targetId != 0 then
116 call targetId.lock()
117 endif118 endmethod119
120 private static method unlock takes nothing returns nothing
121 if sourceId != 0 then
122 call sourceId.unlock()
123 endif124 if targetId != 0 then
125 call targetId.unlock()
126 endif127 endmethod128
129 implement CTL130
131 local real r
132 local integer u
133
134 implement CTLExpire135
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 endif159 else160 set source[this] = null
161 set target[this] = null
162
163 call this.destroy()
164 endif165
166 implement CTLNull167 implement CTLEnd168
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 endif192 endif193 endmethod194
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 endmethod204
205 implement Init206 endstruct207
208 function RegisterInstantHealEvent takes code c returns nothing
209 call Heal.INSTANT.register(Filter(c))
210 return211 endfunction212
213 function RegisterTimedHealEvent takes code c returns nothing
214 call Heal.TIMED.register(Filter(c))
215 return216 endfunction217
218 function RegisterAnyHealEvent takes code c returns nothing
219 call Heal.ANY.register(Filter(c))
220 return221 endfunction222
223 function GetHealingUnit takes nothing returns unit
224 return GetUnitById(Heal.sourceId)
225 endfunction226
227 function GetHealingUnitId takes nothing returns integer
228 return Heal.sourceId
229 endfunction230
231 function GetHealedUnit takes nothing returns unit
232 return GetUnitById(Heal.targetId)
233 endfunction234
235 function GetHealedUnitId takes nothing returns integer
236 return Heal.targetId
237 endfunction238
239 function GetHealedAmount takes nothing returns real
240 return Heal.amount
241 endfunction242
243 function GetEffectiveHealedAmount takes nothing returns real
244 return Heal.effective
245 endfunction246
247 function GetOriginalLife takes nothing returns real
248 return Heal.original
249 endfunction250
251 function HealUnit takes unit source, unit target, real amount returns nothing
252 call Heal.healUnit(source,target,amount)
253 endfunction254
255 function HealUnitOverTime takes unit source, unit target, real amount, real duration returns nothing
256 call Heal.healUnitOverTime(source,target,amount,duration)
257 endfunction258
259 endlibrary
Demo Code
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 endmethod12 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/sec24 call HealUnitOverTime(null, regen, 500000, 50000)
25
26 call PauseUnit(regen, true)
27 endmethod28 29 endstruct
Feel free to comment..
, thanks for sharing.

(If you're also using my GetUnitRegeneration script)