14 years
edited 8 years
The title explains it all.
Feel free to comment..
1 /********************************************************2 *3 * GetUnitRegeneration4 * v2.0.0.05 * By Magtheridon966 *7 * - Gets unit regeneration values8 *9 * Requirements:10 * -------------11 *12 * - UnitIndexer by Nestharus13 * - https://wc3modding.info/4607/unitindexer/14 * - DamageEvent by Nestharus15 * - hiveworkshop.com/forums/jass-resources-412/snippet-damageevent-186829/16 * - Heal by Magtheridon9617 * - https://wc3modding.info/4910/system-heal/18 * - CTL by Nestharus19 * - hiveworkshop.com/forums/jass-resources-412/snippet-constant-timer-loop-32-a-201381/20 *21 * Optional:22 * ---------23 *24 * - DamageControl by PurgeandFire11125 * - hiveworkshop.com/forums/jass-resources-412/system-damage-control-204672/26 *27 * API:28 * ----29 *30 * - function IsUnitRegenerating takes unit u returns boolean31 * - Tells whether a unit is regenerating or not.32 * - function IsUnitRegeneratingById takes integer i returns boolean33 * - Tells whether a unit is regenerating or not given the Id.34 * - function GetUnitRegeneration takes unit u returns real35 * - Gets the amount of regeneration for a given unit.36 * - function GetUnitRegenerationById takes integer i returns real37 * - Gets the amount of regeneration for a given unit given the Id.38 * - function IsRegenerationPositive takes unit u returns boolean39 * - Tells whether the regeneration is positive.40 * - function IsRegenerationPositiveById takes integer i returns boolean41 * - 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 else62 set damaged[DamageEvent.targetId] = damaged[DamageEvent.targetId] + DamageEvent.amount
63 endif64 endmethod65
66 static method heal takes nothing returns nothing
67 set damaged[Heal.targetId] = damaged[Heal.targetId] - Heal.effective
68 endmethod69
70 static method index takes nothing returns nothing
71 // Add index to list72 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 life78 set life[GetIndexedUnitId()] = GetWidgetLife(GetIndexedUnit())
79 set max[GetIndexedUnitId()] = GetUnitState(GetIndexedUnit(), UNIT_STATE_MAX_LIFE)
80 endmethod81
82 static method deindex takes nothing returns nothing
83 // Remove from list84 set next[prev[GetIndexedUnitId()]] = next[GetIndexedUnitId()]
85 set prev[next[GetIndexedUnitId()]] = prev[GetIndexedUnitId()]
86
87 // Resetting Data88 set amount[GetIndexedUnitId()] = 0
89 set damaged[GetIndexedUnitId()] = 0
90
91 set regenerating[GetIndexedUnitId()] = false
92 endmethod93
94 implement CT3295
96 local thistype this = next[0]
97 local real hp = 0
98 local real ml = 0
99
100 loop101 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 endif108
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 else116 set regenerating[this] = false
117 set amount[this] = 0
118 endif119
120 set damaged[this] = 0
121 set max[this] = ml
122
123 set this = next[this]
124 endloop125
126 implement CT32End127
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 else143 call DamageEvent.ANY.register(Filter(damageCode))
144 endif145
146 call RegisterAnyHealEvent(healCode)
147 call UnitIndexer.INDEX.register(Filter(indexCode))
148 call UnitIndexer.DEINDEX.register(Filter(deindexCode))
149 endmethod150
151 endstruct152
153 function IsUnitRegenerating takes unit u returns boolean
154 return UnitRegen.regenerating[GetUnitUserData(u)]
155 endfunction156
157 function IsUnitRegeneratingById takes integer i returns boolean
158 return UnitRegen.regenerating[i]
159 endfunction160
161 function GetUnitRegeneration takes unit u returns real
162 return UnitRegen.amount[GetUnitUserData(u)]
163 endfunction164
165 function GetUnitRegenerationById takes integer i returns real
166 return UnitRegen.amount[i]
167 endfunction168
169 function IsRegenerationPositive takes unit u returns boolean
170 return UnitRegen.amount[GetUnitUserData(u)] >= 0
171 endfunction172
173 function IsRegenerationPositiveById takes integer i returns boolean
174 return UnitRegen.amount[i] >= 0
175 endfunction176
177 endlibrary178 179 library IsUnitRegenerating requires GetUnitRegeneration
180 endlibrary
Feel free to comment..
