-
Notifications
You must be signed in to change notification settings - Fork 13
/
TimeToDie.lua
222 lines (196 loc) · 6.52 KB
/
TimeToDie.lua
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
--- @type MaxDps MaxDps
local _, MaxDps = ...
local GetTime = GetTime
local UnitGUID = UnitGUID
local UnitExists = UnitExists
local TableInsert = tinsert
local TableRemove = tremove
local MathMin = math.min
local wipe = wipe
function MaxDps:InitTTD(maxSamples, interval)
interval = interval or 0.25
maxSamples = maxSamples or 50
if self.ttd and self.ttd.timer then
self:CancelTimer(self.ttd.timer)
self.ttd.timer = nil
end
self.ttd = {
interval = interval,
maxSamples = maxSamples,
HPTable = {},
}
self.ttd.timer = self:ScheduleRepeatingTimer('TimeToDie', interval)
end
function MaxDps:DisableTTD()
if self.ttd.timer then
self:CancelTimer(self.ttd.timer)
end
end
local HPTable = {}
local trackedGuid
function MaxDps:TimeToDie(trackedUnit)
trackedUnit = trackedUnit or 'target'
-- Query current time (throttle updating over time)
local now = GetTime()
-- Current data
local ttd = self.ttd
local guid = UnitGUID(trackedUnit)
if trackedGuid ~= guid then
wipe(HPTable)
trackedGuid = guid
end
if guid and UnitExists(trackedUnit) then
local hpPct = self:TargetPercentHealth() * 100
TableInsert(HPTable, 1, { time = now, hp = hpPct})
if #HPTable > ttd.maxSamples then
TableRemove(HPTable)
end
else
wipe(HPTable)
end
end
function MaxDps:GetTimeToDie()
local seconds = 5*60
local n = #HPTable
if n > 5 then
local a, b
local Ex2, Ex, Exy, Ey = 0, 0, 0, 0
local hpPoint, x, y
for i = 1, n do
hpPoint = HPTable[i]
x, y = hpPoint.time, hpPoint.hp
Ex2 = Ex2 + x * x
Ex = Ex + x
Exy = Exy + x * y
Ey = Ey + y
end
-- Invariant to find matrix inverse
local invariant = 1 / (Ex2 * n - Ex * Ex)
-- Solve for a and b
a = (-Ex * Exy * invariant) + (Ex2 * Ey * invariant)
b = (n * Exy * invariant) - (Ex * Ey * invariant)
if b ~= 0 then
-- Use best fit line to calculate estimated time to reach target health
seconds = (0 - a) / b
seconds = MathMin(5*60, seconds - (GetTime() - 0))
if seconds < 0 then
seconds = 5*60
end
end
end
if WeakAuras then WeakAuras.ScanEvents('MAXDPS_TIME_TO_DIE', seconds) end
return seconds
end
local unitidtable = {}
do
TableInsert(unitidtable,"target")
for i=1,40 do
TableInsert(unitidtable,"nameplate" .. i)
end
end
local function NewTimeToDieTracker()
for i,plate in pairs(unitidtable) do
local unitguid = UnitGUID(plate)
if UnitExists(plate) then
if not MaxDps.ttd then
MaxDps.ttd = {}
end
if not MaxDps.ttd.data then
MaxDps.ttd.data = {}
end
if not MaxDps.ttd.data[plate] then
MaxDps.ttd.data[plate] = {}
end
if not MaxDps.ttd.data[plate].unitguid then
MaxDps.ttd.data[plate].unitguid = unitguid
end
if (not MaxDps.ttd.data[plate].oldHP) or (MaxDps.ttd.data[plate].unitguid ~= unitguid) then
MaxDps.ttd.data[plate].oldHP = UnitHealth(plate)
end
if (MaxDps.ttd.data[plate].unitguid ~= unitguid) then
MaxDps.ttd.data[plate].unitguid = unitguid
end
if MaxDps.ttd.data[plate] then
MaxDps.ttd.data[plate].newHP = UnitHealth(plate)
end
if MaxDps.ttd.data[plate] and MaxDps.ttd.data[plate].oldHP and MaxDps.ttd.data[plate].newHP then
local dps = MaxDps.ttd.data[plate].oldHP - MaxDps.ttd.data[plate].newHP
if dps >= 0 then
MaxDps.ttd.data[plate].DPS = MaxDps.ttd.data[plate].oldHP - MaxDps.ttd.data[plate].newHP
end
end
-- Reset old hp for next calculation
MaxDps.ttd.data[plate].oldHP = UnitHealth(plate)
else
if MaxDps and MaxDps.ttd and MaxDps.ttd.data and MaxDps.ttd.data[plate] then
MaxDps.ttd.data[plate] = nil
end
end
end
end
local newTTDtimer
local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_REGEN_DISABLED")
frame:RegisterEvent("PLAYER_REGEN_ENABLED")
frame:SetScript("OnEvent", function(self, event, ...)
if event == "PLAYER_REGEN_DISABLED" then
--self:ScheduleRepeatingTimer('NewTimeToDie', 1)
newTTDtimer = C_Timer.NewTicker(1,NewTimeToDieTracker)
end
if event == "PLAYER_REGEN_ENABLED" then
--self:ScheduleRepeatingTimer('NewTimeToDie', 1)
if newTTDtimer and not newTTDtimer:IsCancelled() then
newTTDtimer:Cancel()
end
MaxDps.ttd.data = {}
end
end)
-- Function to calculate time till target health reaches a specific percentage
function MaxDps:GetTimeToPct(Pct)
if not UnitExists("target") then
return 500
end
local howFar
local damagePerSecond = (MaxDps.ttd and MaxDps.ttd.data and MaxDps.ttd.data.target and MaxDps.ttd.data.target.DPS and MaxDps.ttd.data.target.DPS) or 0
local timeToReach
local currentHealth = UnitHealth("target")
local goalHP = Pct / 100 * UnitHealthMax("target")
if currentHealth > goalHP then
howFar = UnitHealth("target") - goalHP
timeToReach = howFar / damagePerSecond
if timeToReach == math.huge then
timeToReach = 500
end
else
timeToReach = 0
end
return timeToReach
end
-- Function to find what mod has the longest ttd and how long that is
function MaxDps:MaxAddDuration()
local duration
local durationtotal = 0
if MaxDps.ttd and MaxDps.ttd.data then
for target,data in pairs(MaxDps.ttd.data) do
if target and data and data.DPS and data.DPS>0 then
local howFar
local damagePerSecond = data.DPS
local currentHealth = UnitHealth(target)
local goalHP = 0 / 100 * UnitHealthMax(target)
if currentHealth > goalHP then
howFar = UnitHealth(target) - goalHP
duration = howFar / damagePerSecond
if duration == math.huge then
duration = 500
end
else
duration = 0
end
if duration > durationtotal then
durationtotal = duration
end
end
end
end
return durationtotal
end