vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 -- Deathstimator support
2  
3 lazyr.deathstimator = {}
4  
5 function lazyr.deathstimator.OnUnitHealth(arg1)
6 if (arg1 == "target") then
7 -- MobInfo-2 required
8 if (not MobHealth_GetTargetCurHP) then
9 return
10 end
11 local hp = lazyr.masks.GetUnitHealth("target")
12 lazyr.targetHealthHistory:AddInfo(hp)
13 end
14 end
15  
16  
17 -- Target Health History Object
18 -- Used for computing the best fit line (slope) for the Target's health.
19 --
20 -- Compute the best slope using the method of least squares.
21 -- http://www.acad.sunytccc.edu/instruct/sbrown/stat/leastsq.htm
22 --
23 -- Wow, I NEVER thought I'd ever use ANYTHING like this in real life.
24 -- Haha, and then, it's only for a computer game.
25 -- Ms. Chen, my high school Calculus teacher, would be so proud. Nah,
26 -- probably still too pissed at me.
27 --
28  
29 lazyr.deathstimator.HealthHistory = {}
30  
31 function lazyr.deathstimator.HealthHistory:New()
32 local obj = {}
33 setmetatable(obj, { __index = self })
34 lazyr.deathstimator.HealthHistory.Reset(obj)
35 return obj
36 end
37 function lazyr.deathstimator.HealthHistory:Reset()
38 self.info = {}
39 self.ctr = 0
40 self.lastCtrComputed = 0
41 self.m = 0
42 end
43 function lazyr.deathstimator.HealthHistory:AddInfo(hp)
44 self.ctr = self.ctr + 1
45 table.insert(self.info, { GetTime(), hp })
46 -- Remove any leading entries older than the desired time window
47 local oldest = math.floor(GetTime()) - lazyr.perPlayerConf.healthHistorySize
48 local ct = 0
49 while true do
50 local healthInfo = self.info[1]
51 if (not healthInfo) then
52 break
53 end
54 local time = healthInfo[1]
55 if (time >= oldest) then
56 break
57 end
58 table.remove(self.info, 1)
59 ct = ct + 1
60 end
61 --lazyr.d("AddInfo: trimmed "..ct.." expired entries.")
62 end
63 function lazyr.deathstimator.HealthHistory:GetN()
64 return table.getn(self.info)
65 end
66 function lazyr.deathstimator.HealthHistory:ComputeSlope()
67 local n = self:GetN()
68 if (n == 0) then
69 return nil
70 end
71 if (self.ctr == self.lastCtrComputed) then
72 return self.m
73 end
74 self.lastCtrComputed = self.ctr
75  
76 local xSum = 0
77 local ySum = 0
78 local xSquaredSum = 0
79 local xySum = 0
80 for idx, healthInfo in ipairs(self.info) do
81 local time = healthInfo[1]
82 local hp = healthInfo[2]
83 -- time is the x access, hp is the y access
84 xSum = xSum + time
85 ySum = ySum + hp
86 xSquaredSum = xSquaredSum + (time * time)
87 xySum = xySum + (time * hp)
88 end
89  
90 if (self.xSquaredSum == 0 or self.xSum == 0) then
91 return nil
92 end
93  
94 self.m = ( (n * xySum) - (xSum * ySum) ) / ( (n * xSquaredSum) - (xSum * xSum) )
95  
96 return self.m
97 end
98  
99  
100  
101  
102 -- Deathstimator minion
103  
104 lazyr.deathstimator.minion = {}
105 lazyr.deathstimator.minion.lastUpdate = 0
106 lazyr.deathstimator.minion.updateInterval = 0.25
107  
108  
109 function lazyr.deathstimator.minion.OnUpdate()
110 if (not lazyr.addOnIsActive) then
111 return
112 end
113 if (not lazyr.perPlayerConf.deathMinionIsVisible) then
114 return
115 end
116  
117 local now = GetTime()
118 if (now >= (lazyr.deathstimator.minion.lastUpdate + lazyr.deathstimator.minion.updateInterval)) then
119 lazyr.deathstimator.minion.lastUpdate = now
120  
121 if (lazyr.perPlayerConf.deathMinionHidesOutOfCombat) then
122 if (not lazyr.isInCombat and LazyRogueDeathstimatorFrame:IsShown()) then
123 lazyr.d("You're not in combat, and the death thing's showing, so I'm hiding it")
124 LazyRogueDeathstimatorFrame:Hide()
125 end
126 if (lazyr.isInCombat and not LazyRogueDeathstimatorFrame:IsShown()) then
127 lazyr.d("You're IN combat, and the death thing's hidden, so I'm showing it")
128 LazyRogueDeathstimatorFrame:Show()
129 end
130 end
131  
132 if (not UnitName("target")) then
133 return
134 end
135  
136 if (lazyr.isInCombat) then
137 local text
138  
139 local n = lazyr.targetHealthHistory:GetN()
140 -- we want at least a couple data points
141 if (n < 3) then
142 text = "...gathering..."
143 else
144 -- m is the slope, or hp per second
145 local m = lazyr.targetHealthHistory:ComputeSlope()
146 if (m) then
147 local currentHp = lazyr.masks.GetUnitHealth("target")
148 local secondsTilDeath = math.abs(currentHp / m)
149 text = "Death in "..string.format("%.1f", secondsTilDeath).."s"..
150 " ("..math.abs(string.format("%.1f", m)).."hp/s)"
151 end
152 end
153 lazyr.deathstimator.minion.SetText(text)
154 end
155 end
156 end
157  
158 function lazyr.deathstimator.minion.OnEnter(button)
159 GameTooltip:SetOwner(button, "ANCHOR_RIGHT")
160 GameTooltip:AddLine("LazyRogue v"..lazyr.version.." Deathstimator.\n")
161 GameTooltip:AddLine("Shift + Left Click to move me around.\n")
162 GameTooltip:Show()
163 end
164  
165 function lazyr.deathstimator.minion.OnLeave(button)
166 GameTooltip:Hide()
167 end
168  
169 function lazyr.deathstimator.minion.SetText(text)
170 if (not text) then
171 text = ""
172 end
173 LazyRogueDeathstimatorText:SetText(text)
174 end
175  
176  
177