vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --[[ |
2 | |||
3 | Name: MobHealth2 |
||
4 | Version: 2.6 |
||
5 | Author: Wyv (wyv@wp.pl) |
||
6 | Description: Displays health value for mobs. |
||
7 | Original version by Telo. |
||
8 | URL: http://www.curse-gaming.com/mod.php?addid=1087 |
||
9 | http://ui.worldofwar.net/ui.php?id=681 |
||
10 | http://www.wowinterface.com/downloads/fileinfo.php?id=3938 |
||
11 | |||
12 | ]] |
||
13 | |||
14 | |||
15 | -- Current target data |
||
16 | local lTargetData; |
||
17 | |||
18 | -- table of event handlers |
||
19 | local lEventHandler = {}; |
||
20 | |||
21 | local COLOR_BLUE = "|c009090FF"; |
||
22 | local COLOR_WHITE = "|c00FFFFFF"; |
||
23 | local TITLE = "|c003060FFMobHealth2"..COLOR_WHITE; |
||
24 | |||
25 | -------------------------------------------------------------------------------------------------- |
||
26 | -- external functions for macros / scripts |
||
27 | -- - return value: |
||
28 | -- * (current hp or max hp) if a mob is selected and it's health is known |
||
29 | -- * 0 otherwise |
||
30 | -------------------------------------------------------------------------------------------------- |
||
31 | |||
32 | function MobHealth_GetTargetCurHP() |
||
33 | if ( lTargetData ) then |
||
34 | return math.floor(lTargetData.currentHealthPercent * lTargetData.PPP + 0.5); |
||
35 | end |
||
36 | end |
||
37 | |||
38 | function MobHealth_GetTargetMaxHP() |
||
39 | if ( lTargetData ) then |
||
40 | return math.floor(100 * lTargetData.PPP + 0.5); |
||
41 | end |
||
42 | end |
||
43 | |||
44 | -------------------------------------------------------------------------------------------------- |
||
45 | -- external functions for macros / scripts |
||
46 | -- - return value: |
||
47 | -- curHP, maxHP = MobHealth_GetUnitHP(unit) |
||
48 | -- curHP and maxHP equal 0 if unknown |
||
49 | -- (for unit = "target" or unit = "mouseover" etc.) |
||
50 | -- (please use MobHealth_GetTargetCurHP / MaxHP if you are always intrested in unit="target" |
||
51 | -- (they are faster) |
||
52 | -------------------------------------------------------------------------------------------------- |
||
53 | |||
54 | -------------------------------------------------------------------------------------------------- |
||
55 | -- Internal functions |
||
56 | -- - all MobHealthDB access functions are left intact for compatiblity |
||
57 | -- - MobHealth is no longer local (but it's not a recomended way to access mob health - even tho |
||
58 | -- it's better than using MobHealthDB directly) |
||
59 | -------------------------------------------------------------------------------------------------- |
||
60 | |||
61 | function MobHealth_PPP(index) |
||
62 | if ( index and MobHealthDB[index] ) then |
||
63 | local s, e; |
||
64 | local pts; |
||
65 | local pct; |
||
66 | |||
67 | s, e, pts, pct = string.find(MobHealthDB[index], "^(%d+)/(%d+)$"); |
||
68 | if ( pts and pct ) then |
||
69 | pts = pts + 0; |
||
70 | pct = pct + 0; |
||
71 | if ( pct ~= 0 ) then |
||
72 | return pts / pct; |
||
73 | end |
||
74 | end |
||
75 | end |
||
76 | return 0; |
||
77 | end |
||
78 | |||
79 | local function MobHealth_Pts(index) |
||
80 | if ( index and MobHealthDB[index] ) then |
||
81 | local s, e; |
||
82 | local val; |
||
83 | |||
84 | s, e, val = string.find(MobHealthDB[index], "^(%d+)/"); |
||
85 | |||
86 | if ( val ) then |
||
87 | return val + 0; |
||
88 | end |
||
89 | end |
||
90 | return 0; |
||
91 | end |
||
92 | |||
93 | local function MobHealth_Pct(index) |
||
94 | if ( index and MobHealthDB[index] ) then |
||
95 | local s, e; |
||
96 | local val; |
||
97 | |||
98 | s, e, val = string.find(MobHealthDB[index], "/(%d+)$"); |
||
99 | |||
100 | if ( val ) then |
||
101 | return val + 0; |
||
102 | end |
||
103 | end |
||
104 | return 0; |
||
105 | end |
||
106 | |||
107 | local function MobHealth_Set(index, pts, pct) |
||
108 | if ( index ) then |
||
109 | if ( pts ) then |
||
110 | pts = pts + 0; |
||
111 | else |
||
112 | pts = 0; |
||
113 | end |
||
114 | if ( pct ) then |
||
115 | pct = pct + 0; |
||
116 | else |
||
117 | pct = 0; |
||
118 | end |
||
119 | if ( pts == 0 and pct == 0 ) then |
||
120 | MobHealthDB[index] = nil; |
||
121 | else |
||
122 | MobHealthDB[index] = pts.."/"..pct; |
||
123 | end |
||
124 | end |
||
125 | end |
||
126 | |||
127 | |||
128 | |||
129 | local function MobHealthPlayer_Pts(index) |
||
130 | if ( index and MobHealthPlayerDB[index] ) then |
||
131 | local s, e; |
||
132 | local val; |
||
133 | |||
134 | s, e, val = string.find(MobHealthPlayerDB[index], "^(%d+)/"); |
||
135 | |||
136 | if ( val ) then |
||
137 | return val + 0; |
||
138 | end |
||
139 | end |
||
140 | return 0; |
||
141 | end |
||
142 | |||
143 | local function MobHealthPlayer_Pct(index) |
||
144 | if ( index and MobHealthPlayerDB[index] ) then |
||
145 | local s, e; |
||
146 | local val; |
||
147 | |||
148 | s, e, val = string.find(MobHealthPlayerDB[index], "/(%d+)$"); |
||
149 | |||
150 | if ( val ) then |
||
151 | return val + 0; |
||
152 | end |
||
153 | end |
||
154 | return 0; |
||
155 | end |
||
156 | |||
157 | local function MobHealthPlayer_Set(index, pts, pct) |
||
158 | if ( index ) then |
||
159 | if ( pts ) then |
||
160 | pts = pts + 0; |
||
161 | else |
||
162 | pts = 0; |
||
163 | end |
||
164 | if ( pct ) then |
||
165 | pct = pct + 0; |
||
166 | else |
||
167 | pct = 0; |
||
168 | end |
||
169 | if ( pts == 0 and pct == 0 ) then |
||
170 | MobHealthPlayerDB[index] = nil; |
||
171 | else |
||
172 | MobHealthPlayerDB[index] = pts.."/"..pct; |
||
173 | end |
||
174 | end |
||
175 | end |
||
176 | |||
177 | local function MobHealth_Display(currentPct, index) |
||
178 | local field = getglobal("MobHealthText"); |
||
179 | local text = ""; |
||
180 | |||
181 | if ( field ) then |
||
182 | if ( lTargetData and lTargetData.PPP > 0 ) then |
||
183 | local maxhp; |
||
184 | if (not MobHealthConfig["unstablemax"] and lTargetData.maxHP) then |
||
185 | maxhp = lTargetData.maxHP; |
||
186 | else |
||
187 | maxhp = (100 * lTargetData.PPP) + 0.5; |
||
188 | end |
||
189 | if ( currentPct ) then |
||
190 | text = string.format("%d / %d", (currentPct * lTargetData.PPP) + 0.5, maxhp); |
||
191 | else |
||
192 | text = string.format("??? / %d", maxhp); |
||
193 | end |
||
194 | |||
195 | end |
||
196 | field:SetText(text); |
||
197 | end |
||
198 | end |
||
199 | |||
200 | local function MobHealth_SetPos(pos) |
||
201 | MobHealthFrame:SetPoint("TOP", "TargetFrameHealthBar", "BOTTOM", -2, tonumber(pos) ); |
||
202 | DEFAULT_CHAT_FRAME:AddMessage(TITLE.." position set to "..pos); |
||
203 | end |
||
204 | |||
205 | local function MobHealth_VariablesLoaded() |
||
206 | if (not MobHealthDB ) then |
||
207 | MobHealthDB = { }; |
||
208 | else |
||
209 | local index, value; |
||
210 | |||
211 | for index, value in MobHealthDB do |
||
212 | -- Convert the old-style data into the newer, more compact form |
||
213 | if( type(value) == "table" ) then |
||
214 | MobHealth_Set(index, value.damPts, value.damPct); |
||
215 | end |
||
216 | end |
||
217 | end |
||
218 | |||
219 | if (not MobHealthConfig ) then |
||
220 | MobHealthConfig = {}; |
||
221 | elseif (MobHealthConfig["position"]) then |
||
222 | MobHealth_SetPos(MobHealthConfig["position"]); |
||
223 | end |
||
224 | |||
225 | if (not MobHealthPlayerDB) then |
||
226 | MobHealthPlayerDB = {}; |
||
227 | end |
||
228 | |||
229 | end |
||
230 | |||
231 | local function MobHealth_Reset() |
||
232 | DEFAULT_CHAT_FRAME:AddMessage(TITLE.." reseting database"); |
||
233 | MobHealthDB = {}; |
||
234 | MobHealthConfig = {}; |
||
235 | MobHealthPlayerDB = {}; -- this is not saved variable |
||
236 | end |
||
237 | |||
238 | local function MobHealth_ClearTargetData() |
||
239 | if (lTargetData) then |
||
240 | DEFAULT_CHAT_FRAME:AddMessage(TITLE.." reseting data for "..lTargetData.index); |
||
241 | lTargetData.PPP = 0; |
||
242 | lTargetData.firstBattle = true; |
||
243 | lTargetData.totalDamagePoints = 0; |
||
244 | lTargetData.totalChangeHP = 0; |
||
245 | lTargetData.maxHP = nil; |
||
246 | |||
247 | MobHealth_Display(nil, nil); |
||
248 | |||
249 | if (lTargetData.mob) then |
||
250 | MobHealthDB[lTargetData.index] = nil; |
||
251 | else |
||
252 | MobHealthPlayerDB[lTargetData.index] = nil; |
||
253 | end |
||
254 | else |
||
255 | DEFAULT_CHAT_FRAME:AddMessage(TITLE.." no target with hp information in DB found."); |
||
256 | end |
||
257 | |||
258 | end |
||
259 | |||
260 | |||
261 | function MobHealth_CMD(msg) |
||
262 | local _, _, cmd, arg1 = string.find(msg, "(%w+)[ ]?([-%w]*)"); |
||
263 | |||
264 | if (cmd == "pos") then |
||
265 | if (arg1 ~= "") then |
||
266 | MobHealthConfig["position"] = arg1; |
||
267 | MobHealth_SetPos(arg1); |
||
268 | else |
||
269 | DEFAULT_CHAT_FRAME:AddMessage("You need to specify position: /mobhealth2 pos [position]"); |
||
270 | end |
||
271 | elseif (cmd == "stablemax") then |
||
272 | MobHealthConfig["unstablemax"] = nil; |
||
273 | DEFAULT_CHAT_FRAME:AddMessage(TITLE.." using stable max HP display"); |
||
274 | elseif (cmd == "unstablemax") then |
||
275 | MobHealthConfig["unstablemax"] = true; |
||
276 | DEFAULT_CHAT_FRAME:AddMessage(TITLE.." using unstable max HP display"); |
||
277 | elseif (cmd == "reset" and arg1 == "all") then |
||
278 | MobHealth_Reset(); |
||
279 | elseif (cmd == "reset" or cmd == "del" or cmd == "delete" or cmd == "rem" or cmd == "remove" or cmd == "clear") then |
||
280 | MobHealth_ClearTargetData(); |
||
281 | else |
||
282 | DEFAULT_CHAT_FRAME:AddMessage(TITLE.." commands:"); |
||
283 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_BLUE.." /mobhealth2 pos [position]"); |
||
284 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_WHITE.." set position to [position]"); |
||
285 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_WHITE.." (relative to target frame, default 22, negatives also work)"); |
||
286 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_BLUE.." /mobhealth2 stablemax"); |
||
287 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_WHITE.." updates Max mob HP less often (only in first battle with mob"); |
||
288 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_WHITE.." and between battles)"); |
||
289 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_BLUE.." /mobhealth2 unstablemax"); |
||
290 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_WHITE.." always updates Max mob HP"); |
||
291 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_BLUE.." /mobhealth2 reset all"); |
||
292 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_WHITE.." clears whole database!"); |
||
293 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_BLUE.." /mobhealth2 reset/del/delete/rem/remove/clear"); |
||
294 | DEFAULT_CHAT_FRAME:AddMessage(COLOR_WHITE.." clear data for current target"); |
||
295 | end |
||
296 | end |
||
297 | |||
298 | function MobHealth_SaveTargetData() |
||
299 | if (lTargetData) then |
||
300 | if( lTargetData.PPP > 0 and lTargetData.totalChangeHP < 10000 ) then |
||
301 | if (lTargetData.mob) then |
||
302 | MobHealth_Set(lTargetData.index, lTargetData.totalDamagePoints, lTargetData.totalChangeHP); |
||
303 | else |
||
304 | MobHealthPlayer_Set(lTargetData.index, lTargetData.totalDamagePoints, lTargetData.totalChangeHP); |
||
305 | end |
||
306 | end |
||
307 | end |
||
308 | end |
||
309 | |||
310 | -------------------------------------------------------------------------------------------------- |
||
311 | -- OnFoo functions |
||
312 | -------------------------------------------------------------------------------------------------- |
||
313 | |||
314 | function MobHealth_OnLoad() |
||
315 | for key,value in lEventHandler do |
||
316 | -- DEFAULT_CHAT_FRAME:AddMessage("Registering "..key); |
||
317 | this:RegisterEvent(key); |
||
318 | end |
||
319 | |||
320 | SlashCmdList["MOBHEALTH2"] = MobHealth_CMD; |
||
321 | |||
322 | SLASH_MOBHEALTH21 = "/mobhealth2"; |
||
323 | |||
324 | end |
||
325 | |||
326 | function MobHealth_OnEvent(event) |
||
327 | -- DEFAULT_CHAT_FRAME:AddMessage("Event: "..event) |
||
328 | if (lEventHandler[event]) then |
||
329 | lEventHandler[event](arg1, arg2, arg3, arg4); |
||
330 | end |
||
331 | end |
||
332 | |||
333 | -------------------------------------------------------------------------------------------------- |
||
334 | -- Event handlers |
||
335 | -------------------------------------------------------------------------------------------------- |
||
336 | |||
337 | lEventHandler["VARIABLES_LOADED"] = function () |
||
338 | MobHealth_VariablesLoaded(); |
||
339 | end |
||
340 | |||
341 | lEventHandler["UNIT_HEALTH"] = function () |
||
342 | if (arg1 == "target") then |
||
343 | MobHealth_Display(nil, nil); |
||
344 | if( lTargetData ) then |
||
345 | local health = UnitHealth("target"); |
||
346 | if( health and health ~= 0 ) then |
||
347 | local change = lTargetData.currentHealthPercent - health; |
||
348 | local damage = lTargetData.tempDamagePoints; |
||
349 | if( change > 0 and damage > 0 ) then |
||
350 | lTargetData.totalDamagePoints = lTargetData.totalDamagePoints + damage; |
||
351 | lTargetData.totalChangeHP = lTargetData.totalChangeHP + change; |
||
352 | |||
353 | if( lTargetData.totalChangeHP ~= 0 and lTargetData.totalDamagePoints ~= 0 ) then |
||
354 | lTargetData.PPP = lTargetData.totalDamagePoints / lTargetData.totalChangeHP; |
||
355 | end |
||
356 | |||
357 | if (lTargetData.firstBattle) then |
||
358 | -- this is needed for other addons reading directly from DB |
||
359 | MobHealth_SaveTargetData() |
||
360 | end |
||
361 | |||
362 | lTargetData.tempDamagePoints = 0; |
||
363 | |||
364 | elseif ( change ~= 0 ) then -- could be negative so let's be safe |
||
365 | lTargetData.tempDamagePoints = 0; |
||
366 | end |
||
367 | |||
368 | lTargetData.currentHealthPercent = health; |
||
369 | |||
370 | MobHealth_Display(health, lTargetData.index); |
||
371 | end |
||
372 | end |
||
373 | end |
||
374 | end |
||
375 | |||
376 | lEventHandler["UNIT_COMBAT"] = function () |
||
377 | if( lTargetData and arg1 == "target" ) then |
||
378 | lTargetData.tempDamagePoints = lTargetData.tempDamagePoints + arg4; |
||
379 | end |
||
380 | end |
||
381 | |||
382 | lEventHandler["PLAYER_TARGET_CHANGED"] = function () |
||
383 | if (lTargetData) then |
||
384 | -- we have old target, so we should update it's HP in database |
||
385 | MobHealth_SaveTargetData(); |
||
386 | end |
||
387 | |||
388 | local name = UnitName("target"); |
||
389 | if( name ) then |
||
390 | if( UnitCanAttack("player", "target") and not UnitIsDead("target") and not UnitIsFriend("player", "target") ) then |
||
391 | -- Attackable, alive, non-player target |
||
392 | lTargetData = { }; |
||
393 | lTargetData.name = name; |
||
394 | lTargetData.level = UnitLevel("target"); |
||
395 | lTargetData.currentHealthPercent = UnitHealth("target"); |
||
396 | lTargetData.tempDamagePoints = 0; |
||
397 | |||
398 | if (UnitIsPlayer("target") ) then |
||
399 | lTargetData.player = true; |
||
400 | lTargetData.index = lTargetData.name; |
||
401 | lTargetData.totalDamagePoints = MobHealthPlayer_Pts(lTargetData.index); |
||
402 | lTargetData.totalChangeHP = MobHealthPlayer_Pct(lTargetData.index); |
||
403 | else |
||
404 | lTargetData.mob = true; |
||
405 | lTargetData.index = lTargetData.name..":"..lTargetData.level; |
||
406 | lTargetData.totalDamagePoints = MobHealth_Pts(lTargetData.index); |
||
407 | lTargetData.totalChangeHP = MobHealth_Pct(lTargetData.index); |
||
408 | end |
||
409 | |||
410 | |||
411 | if( lTargetData.totalChangeHP ~= 0 and lTargetData.totalDamagePoints ~= 0 ) then |
||
412 | lTargetData.PPP = lTargetData.totalDamagePoints / lTargetData.totalChangeHP; |
||
413 | else |
||
414 | lTargetData.PPP = 0; |
||
415 | end |
||
416 | |||
417 | if (lTargetData.totalChangeHP == 0) then |
||
418 | -- first battle with mob/player |
||
419 | lTargetData.firstBattle = true; |
||
420 | else |
||
421 | lTargetData.maxHP = math.floor(lTargetData.PPP * 100 + 0.5); |
||
422 | end |
||
423 | |||
424 | MobHealth_Display(lTargetData.currentHealthPercent, lTargetData.index); |
||
425 | return; |
||
426 | end |
||
427 | end |
||
428 | |||
429 | -- Unusable target |
||
430 | lTargetData = nil; |
||
431 | |||
432 | MobHealth_Display(nil, nil); |
||
433 | end |
||
434 |