-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.lua
569 lines (473 loc) · 16 KB
/
Route.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
local Name, Addon = ...
-- # of hops to track back from previous result
Addon.ROUTE_TRACK_BACK = 15
-- Distance weight to route
Addon.ROUTE_WEIGHT_ROUTE = 0.5
-- Distance weight to a following pull
Addon.ROUTE_WEIGHT_FORWARD = 0.5
-- Distance weight to same group but different sublevels
Addon.ROUTE_WEIGHT_GROUP = 0.7
-- Max difference between the min/max and a given path for it to be considered
Addon.ROUTE_MAX_LENGTH_DIFF = 10
Addon.ROUTE_MAX_WEIGHT_DIFF = 3
-- Max rounds per frame
Addon.ROUTE_MAX_FRAME = 20
-- Scale MAX_FRAME with elapsed time
Addon.ROUTE_MAX_FRAME_SCALE = 0.3
-- Total time to spend on route estimation (in s)
Addon.ROUTE_MAX_TOTAL = 5
local queue, queueSize, weights = {}, 0, {}
local maxLength, minWeight = 0, math.huge
local pulls, groups, portals = {}, {}, {}
local hits, kills = {}, {}
local co, rerun, zoom, retry
-- DEBUG
local ignored = 0
local useRoute = true
local debug = Addon.Debug
local function Node(enemyId, cloneId)
return "e" .. enemyId .. "c" .. cloneId
end
local function Path(path, node)
return path .. "-" .. node .. "-"
end
local function Sub(path, n)
for _=1,n or 1 do
path = path:gsub("%-[^-]+-$", "")
end
return path
end
local function Contains(path, node)
return path:find("%-" .. node .. "%-") ~= nil
end
local function Last(path, enemies)
if path == "" then
local dungeon = Addon.GetCurrentDungeonId()
if Addon.dungeons[dungeon] and Addon.dungeons[dungeon].start then
return nil, Addon.dungeons[dungeon].start
else
for _,poi in ipairs(MDT.mapPOIs[dungeon][1]) do
if poi.type == "graveyard" then
return nil, poi
end
end
end
debug("No starting point!")
return
else
local enemyId, cloneId = path:match("-e(%d+)c(%d+)-$")
if enemyId and cloneId then
return Node(enemyId, cloneId), enemies and enemies[tonumber(enemyId)].clones[tonumber(cloneId)]
end
end
end
local function Length(path)
return path:gsub("e%d+c%d+", ""):len() / 2
end
local function Position(clone)
local grp = clone.g and groups[clone.g]
return grp and grp.sublevel and grp or clone
end
local function Distance(from, to, forceSub)
from, to = Position(from), Position(to)
local fromSub, toSub = forceSub or from.sublevel, forceSub or to.sublevel
if not fromSub or not toSub or fromSub == toSub then
return math.sqrt(math.pow(from.x - to.x, 2) + math.pow(from.y - to.y, 2))
else
local pois = MDT.mapPOIs[Addon.GetCurrentDungeonId()]
local min = math.huge
for _,fromPoi in pairs(pois[fromSub]) do
if fromPoi.type == "mapLink" then
local i = fromPoi.connectionIndex
local fromDist = Distance(from, fromPoi, fromSub)
for _,toPoi in pairs(pois[toSub]) do
if toPoi.type == "mapLink" then
local j = toPoi.connectionIndex
local dist = portals[i][j]
if dist and dist < min then
min = math.min(min, fromDist + dist + Distance(toPoi, to, toSub))
end
end
end
end
end
return min
end
return math.huge
end
local function Weight(path, enemies)
if path == "" then
return 0
elseif not weights[path] then
local parent = Sub(path, 1)
local prevWeight, prevLength = Weight(parent, enemies), Length(parent)
local prevNode, prev = Last(parent, enemies)
local prevPull = prevNode and pulls[prevNode]
local currNode, curr = Last(path, enemies)
local currPull = currNode and pulls[currNode]
-- Base distance
local dist = Distance(prev, curr)
-- Weighted by group
if prev.g and curr.g and prev.g == curr.g then
dist = dist * Addon.ROUTE_WEIGHT_GROUP
end
-- Weighted by direction
if currPull then
if not prevPull then
dist = dist * Addon.ROUTE_WEIGHT_ROUTE
else
local diff = math.max(0.1, abs(currPull - prevPull) / #Addon.GetCurrentPulls())
local forward = currPull > prevPull and Addon.ROUTE_WEIGHT_FORWARD or 1
dist = dist * diff * forward
end
end
weights[path] = prevWeight + (dist - prevWeight) / (prevLength + 1)
-- weights[path] = prevWeight + dist
end
return weights[path]
end
local function CheckPath(path)
local length, weight = Length(path), weights[path]
local result = length > maxLength - Addon.ROUTE_MAX_LENGTH_DIFF
and (length >= maxLength or weight < minWeight + Addon.ROUTE_MAX_WEIGHT_DIFF)
if result then
maxLength = math.max(maxLength, length)
minWeight = math.min(minWeight, weight)
else
ignored = ignored + 1
end
return result
end
local function Enqueue(path)
local weight = weights[path]
queueSize = queueSize + 1
local i, p = queueSize, math.floor(queueSize/2)
while i > 1 and weights[queue[p]] > weight do
queue[i] = queue[p]
i, p = p, math.floor(p/2)
end
queue[i] = path
end
local function Dequeue()
if queueSize > 0 then
local val = queue[1]
queue[1], queue[queueSize], queueSize = queue[queueSize], nil, queueSize - 1
-- Heapify
local min, i, l, r = 1
repeat
i, l, r = min, 2*min, 2*min+1
if l <= queueSize and weights[queue[l]] < weights[queue[min]] then
min = l
end
if r <= queueSize and weights[queue[r]] < weights[queue[min]] then
min = r
end
if min ~= i then
queue[i], queue[min] = queue[min], queue[i]
end
until min == i
return val
end
end
local function DeepSearch(path, enemies, grp)
local enemyId = kills[Length(path)+1]
local minPath
if grp and grp[enemyId] then
for cloneId,clone in pairs(grp[enemyId].clones) do
local node = Node(enemyId, cloneId)
if not Contains(path, node) then
local p = DeepSearch(Path(path, node), enemies, grp)
local w = Weight(p, enemies)
if not minPath or w < weights[minPath] then
minPath = p
if grp.sublevel then
break
end
end
end
end
end
return minPath or path
end
local function WideSearch(path, enemies, grps)
local enemyId = kills[Length(path)+1]
local found
if enemies and enemies[enemyId] then
for cloneId,clone in pairs(enemies[enemyId].clones) do
local node = Node(enemyId, cloneId)
if not Contains(path, node) then
local p = DeepSearch(Path(path, node), enemies, groups[clone.g])
local w = Weight(p, enemies)
if w < math.huge then
found = true
if not clone.g then
Enqueue(p)
elseif not grps[clone.g] or w < weights[grps[clone.g]] then
grps[clone.g] = p
end
end
end
end
end
for _,p in pairs(grps) do
Enqueue(p)
end
return found
end
function Addon.CalculateRoute()
local enemies = Addon.GetCurrentEnemies()
local dungeon = Addon.GetCurrentDungeonId()
local t, i, n, grps = GetTime(), 1, 1, {}
-- Start route
local start = Sub(MDTGuideRoute, Addon.ROUTE_TRACK_BACK)
weights[start] = 0
Enqueue(start)
while true do
local total = GetTime() - t
-- Limit runtime
if total >= Addon.ROUTE_MAX_TOTAL then
Addon.Echo(nil, "Route calculation took too long, switching to enemy forces mode!")
useRoute, rerun = false, false
break
elseif i > Addon.ROUTE_MAX_FRAME * (1 - total * Addon.ROUTE_MAX_FRAME_SCALE / Addon.ROUTE_MAX_TOTAL) then
i = 1
coroutine.yield()
end
local path = Dequeue()
-- Failure
if not path then
Addon.Echo(nil, "Route calculation didn't work, switching to enemy forces mode!")
useRoute, rerun = false, false
break
end
local length = Length(path)
-- Success
if length == #kills then
MDTGuideRoute = path
break
end
-- Find next paths
if CheckPath(path) then
local found = WideSearch(path, enemies, grps)
-- Skip current enemy if no path was found
if not found then
table.remove(kills, length+1)
Enqueue(path)
end
wipe(grps)
end
i, n = i+1, n+1
end
debug("LENGTH", Length(MDTGuideRoute))
debug("WEIGHT", weights[MDTGuideRoute])
debug("LOOPS", n)
debug("TIME", GetTime() - t)
debug("QUEUE", queueSize)
debug("IGNORED", ignored)
wipe(queue)
wipe(weights)
queueSize, maxLength, minWeight = 0, 0, math.huge
ignored = 0
Addon.ColorEnemies()
if zoom then
zoom = rerun
Addon.ZoomToCurrentPull()
end
if rerun then
Addon.UpdateRoute()
end
end
-- ---------------------------------------
-- State
-- ---------------------------------------
function Addon.UseRoute(val)
if val ~= nil then
MDTGuideOptions.route = val
if val == true then
useRoute = true
end
end
return MDTGuideOptions.route and useRoute
end
function Addon.UpdateRoute(z)
zoom = zoom or z
rerun = false
if Addon.IsCurrentInstance() then
if co and coroutine.status(co) == "running" then
rerun = true
else
co = coroutine.create(Addon.CalculateRoute)
local ok, err = coroutine.resume(co)
if not ok then error(err) end
end
end
end
function Addon.AddKill(npcId)
for i,enemy in ipairs(MDT.dungeonEnemies[Addon.currentDungeon]) do
if enemy.id == npcId then
table.insert(kills, i)
return i
end
end
end
function Addon.ClearKills()
wipe(hits)
wipe(kills)
MDTGuideRoute = ""
useRoute = true
end
function Addon.GetCurrentPullByRoute()
local path = MDTGuideRoute
while path and path:len() > 0 do
local node = Last(path)
local n = pulls[node]
if n then
local a, b = Addon.IteratePull(n, function (_, _, cloneId, enemyId, pull)
if not Contains(path, Node(enemyId, cloneId)) then
return n, pull
end
end)
if a then
return a, b
else
local currPulls = Addon.GetCurrentPulls()
if n < #currPulls then n = n + 1 end
return n, currPulls[n]
end
end
path = path:sub(1, -node:len() - 3)
end
end
function Addon.SetDungeon()
wipe(pulls)
Addon.BuildGroups()
Addon.BuildPortals()
Addon.UpdateRoute()
end
function Addon.SetInstanceDungeon(dungeon)
Addon.currentDungeon = dungeon
Addon.ClearKills()
Addon.UpdateRoute()
end
function Addon.BuildGroups()
wipe(groups)
Addon.IteratePulls(function (clone, _, cloneId, enemyId, _, pullId)
pulls[Node(enemyId, cloneId)] = pullId
if clone.g then
groups[clone.g] = groups[clone.g] or { x = 0, y = 0, length = 0 }
local grp = groups[clone.g]
grp[enemyId] = grp[enemyId] or { clones = {} }
grp[enemyId].clones[cloneId] = clone
grp.length = grp.length + 1
if grp.sublevel == nil or grp.sublevel == clone.sublevel then
grp.sublevel = clone.sublevel
grp.x = grp.x + (clone.x - grp.x) / grp.length
grp.y = grp.y + (clone.y - grp.y) / grp.length
else
grp.sublevel = false
grp.x, grp.y = nil
end
end
end)
end
function Addon.BuildPortals()
local start = GetTime()
local levels = MDT.mapPOIs[Addon.GetCurrentDungeonId()]
local numLevels = #levels
wipe(portals)
-- Build cost matrix
for level,pois in pairs(levels) do
for _,poi in pairs(pois) do
if poi.type == "mapLink" then
local i = poi.connectionIndex
portals[i] = portals[i] or {}
for _,poi2 in pairs(pois) do
if poi2.type == "mapLink" then
local j = poi2.connectionIndex
portals[i][j] = i == j and 0 or math.min(portals[i][j] or math.huge, Distance(poi, poi2))
end
end
end
end
end
local n = #portals
-- Find shortest paths
for k=1,n do
for i=1,n do
for j=1,n do
local path = (portals[i][k] or math.huge) + (portals[k][j] or math.huge)
if path < (portals[i][j] or math.huge) then
portals[i][j] = path
end
end
end
end
end
-- ---------------------------------------
-- Events
-- ---------------------------------------
local Frame = CreateFrame("Frame")
local OnEvent = function (_, ev, ...)
if not MDT or MDT:GetDB().devMode then return end
if ev == "PLAYER_ENTERING_WORLD" then
local _, instanceType = IsInInstance()
if instanceType == "party" then
local map = C_Map.GetBestMapForUnit("player")
if map then
local dungeon = Addon.GetInstanceDungeonId(EJ_GetInstanceForMap(map))
if dungeon ~= Addon.currentDungeon then
Addon.SetInstanceDungeon(dungeon)
if dungeon then
Frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
else
Frame:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
end
end
else
retry = {ev, ...}
end
else
if instanceType then
Addon.SetInstanceDungeon()
end
Frame:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
end
elseif ev == "SCENARIO_COMPLETED" or ev == "CHAT_MSG_SYSTEM" and (...):match(Addon.PATTERN_INSTANCE_RESET) then
Addon.SetInstanceDungeon()
Frame:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
elseif ev == "COMBAT_LOG_EVENT_UNFILTERED" then
local _, event, _, _, _, sourceFlags, _, destGUID, _, destFlags = CombatLogGetCurrentEventInfo()
if event == "UNIT_DIED" then
if hits[destGUID] then
hits[destGUID] = nil
local npcId = Addon.GetNPCId(destGUID)
if Addon.AddKill(npcId) and Addon.UseRoute() then
Addon.ZoomToCurrentPull(true)
end
end
elseif event:match("DAMAGE") or event:match("AURA_APPLIED") then
local sourceIsParty = bit.band(sourceFlags, COMBATLOG_OBJECT_AFFILIATION_OUTSIDER) == 0
local destIsEnemy = bit.band(destFlags, COMBATLOG_OBJECT_AFFILIATION_OUTSIDER) > 0 and bit.band(destFlags, COMBATLOG_OBJECT_REACTION_FRIENDLY) == 0
if sourceIsParty and destIsEnemy and not hits[destGUID] then
hits[destGUID] = true
end
end
end
end
-- Resume route calculation
local OnUpdate = function ()
if co and coroutine.status(co) == "suspended" then
local ok, err = coroutine.resume(co)
if not ok then error(err) end
end
if retry then
local args = retry
retry = nil
OnEvent(nil, unpack(args))
end
end
Frame:SetScript("OnEvent", OnEvent)
Frame:SetScript("OnUpdate", OnUpdate)
Frame:RegisterEvent("PLAYER_ENTERING_WORLD")
Frame:RegisterEvent("SCENARIO_COMPLETED")
Frame:RegisterEvent("CHAT_MSG_SYSTEM")