-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LibTargetedCasts.lua
285 lines (236 loc) · 8.85 KB
/
LibTargetedCasts.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
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
--[================[
LibTargetedCasts
Author: d87
--]================]
local MAJOR, MINOR = "LibTargetedCasts", 5
local lib = LibStub:NewLibrary(MAJOR, MINOR)
if not lib then return end
lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
lib.frame = lib.frame or CreateFrame("Frame")
local f = lib.frame
local callbacks = lib.callbacks
lib.casters = lib.casters or {} -- setmetatable({}, { __mode = "v" })
local casters = lib.casters
local guidsToPurge = {}
local UnitGUID = UnitGUID
local GetTime = GetTime
local tinsert = tinsert
local UnitPlayerOrPetInParty = UnitPlayerOrPetInParty
local UnitPlayerOrPetInRaid = UnitPlayerOrPetInRaid
local UnitIsUnit = UnitIsUnit
local UnitExists = UnitExists
local UnitCastingInfo = UnitCastingInfo
local UnitChannelInfo = UnitChannelInfo
local apiLevel = math.floor(select(4,GetBuildInfo())/10000)
if apiLevel <= 2 then
UnitCastingInfo = function(...)
local name, text, texture, startTimeMS, endTimeMS, isTradeSkill, castID, spellID = _G.UnitCastingInfo(...)
local notInterruptible = false
return name, text, texture, startTimeMS, endTimeMS, isTradeSkill, castID, notInterruptible, spellID
end
UnitChannelInfo = function(...)
local name, text, texture, startTimeMS, endTimeMS, isTradeSkill, spellID = _G.UnitChannelInfo(...)
local notInterruptible = false
return name, text, texture, startTimeMS, endTimeMS, isTradeSkill, notInterruptible, spellID
end
end
f:SetScript("OnEvent", function(self, event, ...)
return self[event](self, event, ...)
end)
local refreshCastTable = function(tbl, ...)
local numArgs = select("#", ...)
for i=1, numArgs do
tbl[i] = select(i, ...)
end
end
local IsGroupUnit = function(unit)
return UnitExists(unit) and (UnitIsUnit(unit, "player") or UnitPlayerOrPetInParty(unit) or UnitPlayerOrPetInRaid(unit))
end
local function UnitIsHostile(unit)
local reaction = UnitReaction(unit, 'player') or 1
return reaction <= 4
end
local function FireCallback(event, guid, ...)
-- TODO: Add unit lookup
callbacks:Fire(event, guid, ...)
end
-- local eventCounter = 0
function f:UNIT_SPELLCAST_COMMON_START(event, castType, srcUnit, castID, spellID)
if UnitIsHostile(srcUnit) then
local dstUnit = srcUnit.."target"
if IsGroupUnit(dstUnit) then
local srcGUID = UnitGUID(srcUnit)
local dstGUID = UnitGUID(dstUnit)
local currentCast = casters[srcGUID]
local name, text, texture, startTimeMS, endTimeMS, isTradeSkill, castID, notInterruptible, spellID
if castType == "CAST" then
name, text, texture, startTimeMS, endTimeMS, isTradeSkill, castID, notInterruptible, spellID = UnitCastingInfo(srcUnit)
else
name, text, texture, startTimeMS, endTimeMS, isTradeSkill, notInterruptible, spellID = UnitChannelInfo(srcUnit)
end
if not name then return end
if currentCast then
refreshCastTable(currentCast, srcGUID, dstGUID, castType, name, text, texture, startTimeMS/1000, endTimeMS/1000, isTradeSkill, castID, notInterruptible, spellID)
else
casters[srcGUID] = { srcGUID, dstGUID, castType, name, text, texture, startTimeMS/1000, endTimeMS/1000, isTradeSkill, castID, notInterruptible, spellID }
end
FireCallback("SPELLCAST_UPDATE", dstGUID)
-- eventCounter = eventCounter + 1
-- if eventCounter > 200 then
-- end
end
end
end
function f:UNIT_SPELLCAST_START(event, ...)
local castType = "CAST"
return self:UNIT_SPELLCAST_COMMON_START(event, castType, ...)
end
f.UNIT_SPELLCAST_DELAYED = f.UNIT_SPELLCAST_START
function f:UNIT_SPELLCAST_CHANNEL_START(event, ...)
local castType = "CHANNEL"
return self:UNIT_SPELLCAST_COMMON_START(event, castType, ...)
end
f.UNIT_SPELLCAST_CHANNEL_UPDATE = f.UNIT_SPELLCAST_CHANNEL_START
function f:UNIT_SPELLCAST_COMMON_STOP(event, castType, srcUnit, castID, spellID)
if UnitIsHostile(srcUnit) then
local srcGUID = UnitGUID(srcUnit)
local currentCast = casters[srcGUID]
if currentCast then
local dstGUID = currentCast[2]
casters[srcGUID] = nil
FireCallback("SPELLCAST_UPDATE", dstGUID)
end
end
end
function f:UNIT_SPELLCAST_STOP(event, ...)
local castType = "CAST"
return self:UNIT_SPELLCAST_COMMON_STOP(event, castType, ...)
end
f.UNIT_SPELLCAST_FAILED = f.UNIT_SPELLCAST_STOP
f.UNIT_SPELLCAST_FAILED_QUIET = f.UNIT_SPELLCAST_STOP
f.UNIT_SPELLCAST_INTERRUPTED = f.UNIT_SPELLCAST_STOP
function f:UNIT_SPELLCAST_CHANNEL_STOP(event, ...)
local castType = "CHANNEL"
return self:UNIT_SPELLCAST_COMMON_STOP(event, castType, ...)
end
function f:UNIT_TARGET(event, srcUnit)
if UnitIsHostile(srcUnit) then
local srcGUID = UnitGUID(srcUnit)
local currentCast = casters[srcGUID]
if currentCast then
local _, dstGUID_old, name, text, texture, startTimeMS, endTimeMS, isTradeSkill, castID, notInterruptible, spellID = unpack(currentCast)
local dstUnit = srcUnit.."target"
local dstGUID_new = UnitGUID(dstUnit)
if dstGUID_old ~= dstGUID_new then
currentCast[2] = dstGUID_new
FireCallback("SPELLCAST_UPDATE", dstGUID_old)
if IsGroupUnit(dstUnit) then
FireCallback("SPELLCAST_UPDATE", dstGUID_new)
end
end
else -- if unit switched target (from not having any) and it's already casting something that isn't tracked
if UnitCastingInfo(srcUnit) then
self:UNIT_SPELLCAST_START(event, srcUnit)
end
if UnitChannelInfo(srcUnit) then
self:UNIT_SPELLCAST_CHANNEL_START(event, srcUnit)
end
end
end
end
function f:NAME_PLATE_UNIT_ADDED(event, srcUnit)
local name, text, texture, startTimeMS, endTimeMS, isTradeSkill, castID, notInterruptible, spellID = UnitCastingInfo(srcUnit)
if spellID then
return self:UNIT_SPELLCAST_START("UNIT_SPELLCAST_START", srcUnit, castID, spellID)
else
name, text, texture, startTimeMS, endTimeMS, isTradeSkill, notInterruptible, spellID = UnitChannelInfo(srcUnit)
if spellID then
return self:UNIT_SPELLCAST_CHANNEL_START("UNIT_SPELLCAST_CHANNEL_START", srcUnit, nil, spellID)
end
end
end
local normalUnits = {
["target"] = true,
["focus"] = true,
["boss1"] = true,
["boss2"] = true,
["boss3"] = true,
["boss4"] = true,
["boss5"] = true,
["arena1"] = true,
["arena2"] = true,
["arena3"] = true,
["arena4"] = true,
["arena5"] = true,
}
function f:NAME_PLATE_UNIT_REMOVED(event, srcUnit)
for unit in pairs(normalUnits) do
if UnitIsUnit(unit, srcUnit) then
return
end
end
local srcGUID = UnitGUID(srcUnit)
local currentCast = casters[srcGUID]
if currentCast then
local dstGUID = currentCast[2]
casters[srcGUID] = nil
FireCallback("SPELLCAST_UPDATE", dstGUID)
end
end
local function PurgeExpired()
for i, guid in ipairs(guidsToPurge) do
casters[guid] = nil
end
table.wipe(guidsToPurge)
end
local returnArray = {}
function lib:GetUnitIncomingCastsTable(unit)
table.wipe(returnArray)
local dstGUID = UnitGUID(unit)
local now = GetTime()
for srcGUID, castInfo in pairs(casters) do
if castInfo[2] == dstGUID then
local endTime = castInfo[8]
local isExpired = endTime < now
if isExpired then
tinsert(guidsToPurge, srcGUID)
else
tinsert(returnArray, castInfo)
end
end
end
PurgeExpired()
return returnArray
end
function lib:GetCastInfoBySourceGUID(srcGUID)
local cast = casters[srcGUID]
if cast then
return unpack(cast)
end
end
-- function lib:GetUnitIncomingCasts(...)
-- self:GetUnitIncomingCastsInternal(...)
-- return unpack(returnArray)
-- end
-- function lib:GetUnitIncomingCastsTable(...)
-- self:GetUnitIncomingCastsInternal(...)
-- return returnArray
-- end
function callbacks.OnUsed()
-- f:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
f:RegisterEvent("UNIT_SPELLCAST_START")
f:RegisterEvent("UNIT_SPELLCAST_DELAYED")
f:RegisterEvent("UNIT_SPELLCAST_STOP")
f:RegisterEvent("UNIT_SPELLCAST_FAILED")
f:RegisterEvent("UNIT_SPELLCAST_FAILED_QUIET")
f:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
f:RegisterEvent("UNIT_TARGET")
f:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START")
f:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE")
f:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP")
f:RegisterEvent("NAME_PLATE_UNIT_ADDED")
f:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
end
function callbacks.OnUnused()
f:UnregisterAllEvents()
end