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