-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTracker.lua
208 lines (175 loc) · 5.7 KB
/
Tracker.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
local _, FS = ...
local Tracker = FS:RegisterModule("Tracker")
local max = math.max
-------------------------------------------------------------------------------
-- Tracker config
--------------------------------------------------------------------------------
local tracker_config = {
title = {
type = "description",
name = "|cff64b4ffHostile tracker",
fontSize = "large",
order = 0
},
desc = {
type = "description",
name = "Track nearby hostile units and provides GUID to UnitID service.\n",
fontSize = "medium",
order = 1
},
ref = {
type = "header",
name = "Module reference",
order = 1000
},
docs = FS.Config:MakeDoc("Public API", 2000, {
{":GetUnit ( guid ) -> unitid", "Return a unitid for the given mob, if known."},
{":ParseGUID ( guid ) -> ...", "Parse a GUID string and return components."},
}, "FS.Tracker"),
events = FS.Config:MakeDoc("Emitted events", 3000, {
{"_FOUND ( guid , npcid )", "Emitted when a new unit is discovered."},
{"_DIED ( guid )", "Emitted when a tracked unit has died."},
{"_LOST ( guid )", "Emitted when a tracked unit was lost. " ..
"A unit is considered lost if no events involving this unit was received in the last 5 sec and no one is targetting it."},
{"_REMOVE ( guid , npcid )", "Emitted when a tracked unit has died or was lost."},
}, "FS_TRACKER")
}
--------------------------------------------------------------------------------
function Tracker:OnInitialize()
self.mobs = {}
self.mobs_id = {}
FS.Config:Register("Hostile tracker", tracker_config)
end
function Tracker:OnEnable()
--self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
--self:RegisterEvent("UNIT_TARGET")
--self:RegisterEvent("NAME_PLATE_UNIT_ADDED")
--self.gc = C_Timer.NewTicker(5, function() self:GC() end)
end
--------------------------------------------------------------------------------
-- Private
--------------------------------------------------------------------------------
function Tracker:GetMob(guid, timestamp)
local mob = self.mobs[guid]
if not mob and timestamp then
local unit_type, zero, s, i, z, m, w = self:ParseGUID(guid)
if unit_type ~= "Creature" and unit_type ~= "Vehicule" then return end
mob = {
guid = guid,
server = s,
instance = i,
zone = z,
id = m,
spawn = w,
ping = timestamp,
unitids = {}
}
if not self.mobs_id[m] then
self.mobs_id[m] = { guid }
else
table.insert(self.mobs_id[m], guid)
end
self.mobs[guid] = mob
self:SendMessage("FS_TRACKER_FOUND", guid, m)
elseif timestamp and timestamp > mob.ping then
mob.ping = timestamp
end
return mob
end
function Tracker:RemoveMob(guid)
local data = self.mobs[guid]
if not data then return end
local id = data.id
self.mobs[guid] = nil
local list = self.mobs_id[id]
for i, g in ipairs(list) do
if g == guid then
table.remove(list, i)
break
end
end
if #list == 0 then
self.mobs_id[id] = nil
end
self:SendMessage("FS_TRACKER_REMOVE", guid, data.id)
end
function Tracker:GC()
local now = GetTime()
for guid, data in pairs(self.mobs) do
local unit = self:GetUnit(guid, data)
if not unit and now - data.ping > 5 then
self:SendMessage("FS_TRACKER_LOST", guid, data.id)
self:RemoveMob(guid)
end
end
end
--------------------------------------------------------------------------------
-- Public
--------------------------------------------------------------------------------
function Tracker:ParseGUID(guid, only_type)
local offset = guid:find("-")
if not offset then return guid end
local unit_type = guid:sub(1, offset - 1)
if only_type then return unit_type end
if unit_type == "Player" then
local s, u = guid:match("(.-)-(.+)", offset + 1)
return unit_type, tonumber(s), u
else
local x, s, i, z, m, u = guid:match("(.-)-(.-)-(.-)-(.-)-(.-)-(.+)", offset + 1)
return unit_type, tonumber(x), tonumber(s), tonumber(i), tonumber(z), tonumber(m), u
end
end
function Tracker:GetUnit(guid, mob)
if not mob then mob = self:GetMob(guid) end
if not mob then return end
for unit in pairs(mob.unitids) do
if UnitExists(unit) and UnitGUID(unit) == guid then
return unit
else
mob.unitids[unit] = nil
end
end
end
--------------------------------------------------------------------------------
-- Events
--------------------------------------------------------------------------------
function Tracker:COMBAT_LOG_EVENT_UNFILTERED(_, _, event, _, ...)
if self[event] then
self[event](self, ...)
end
end
function Tracker:SWING_DAMAGE(source, _, _, _, dest, _)
local source_t = self:ParseGUID(source, true)
local dest_t = self:ParseGUID(dest, true)
local now = GetTime()
if source_t == "Creature" or source_t == "Vehicule" then self:GetMob(source, now) end
if dest_t == "Creature" or dest_t == "Vehicule" then self:GetMob(dest, now) end
end
Tracker.SPELL_DAMAGE = Tracker.SWING_DAMAGE
Tracker.SPELL_CAST_SUCCESS = Tracker.SWING_DAMAGE
function Tracker:UNIT_DIED(source, _, _, _, dest)
if self.mobs[dest] then
self:SendMessage("FS_TRACKER_DIED", dest)
self:RemoveMob(dest)
end
end
Tracker.UNIT_DESTROYED = Tracker.UNIT_DIED
Tracker.UNIT_DISSIPATES = Tracker.UNIT_DIED
function Tracker:UNIT_TARGET(_, unit)
local target = unit .. "target"
if not UnitExists(target) then return end
local target_guid = UnitGUID(target)
local target_type = self:ParseGUID(target_guid, true)
if target_type == "Creature" or target_type == "Vehicule" then
local mob = self:GetMob(target_guid, GetTime())
mob.unitids[target] = true
end
end
function Tracker:NAME_PLATE_UNIT_ADDED(_, nameplate)
local nameplate_guid = UnitGUID(nameplate)
local nameplate_type = self:ParseGUID(nameplate_guid, true)
if nameplate_type == "Creature" or nameplate_type == "Vehicule" then
local mob = self:GetMob(nameplate_guid, GetTime())
mob.unitids[nameplate] = true
end
end