-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFlightTracker.lua
More file actions
632 lines (522 loc) · 20 KB
/
FlightTracker.lua
File metadata and controls
632 lines (522 loc) · 20 KB
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
local CreateFrame = CreateFrame
local GetTime = GetTime
local UnitOnTaxi = UnitOnTaxi
local TaxiNodeGetType = TaxiNodeGetType
local TaxiNodeName = TaxiNodeName
local TaxiNodeCost = TaxiNodeCost
local GetZoneText = GetZoneText
local UnitName = UnitName
local GetNumRaidMembers = GetNumRaidMembers
local GetNumPartyMembers = GetNumPartyMembers
local SendChatMessage = SendChatMessage
local GetPlayerBuff = GetPlayerBuff
local CancelPlayerBuff = CancelPlayerBuff
local GetCursorPosition = GetCursorPosition
local tinsert = tinsert
FlightTracker = CreateFrame("Frame", "FlightTracker")
FlightTracker:SetScript("OnEvent", function()
if FlightTracker[event] then FlightTracker[event](FlightTracker) end
end)
FlightTracker:RegisterEvent("ADDON_LOADED")
FlightTracker:RegisterEvent("PLAYER_ENTERING_WORLD")
FlightTracker:RegisterEvent("TAXIMAP_OPENED")
local ADDON_PATH = "Interface\\AddOns\\FlightTracker\\"
local isFlying = false
local isPending = false
local pendingDestName = nil
local pendingCost = 0
local startTime = 0
local originNode = nil
local destNode = nil
local flightTimerFrame = nil
local isTooltipHooked = false
local original_TaxiNodeOnButtonEnter = nil
local cachedOriginNode = nil
StaticPopupDialogs["FLIGHTTRACKER_CONFIRM"] = {
text = "Fly to %s?",
button1 = "Yes",
button2 = "No",
OnAccept = function(data)
FlightTracker.confirming = true
TakeTaxiNode(data.index)
FlightTracker.confirming = false
end,
timeout = 0,
whileDead = 1,
hideOnEscape = 1
}
function FlightTracker:ADDON_LOADED()
if arg1 ~= "FlightTracker" then return end
local playerName = UnitName("player")
if not FlightTrackerDB then FlightTrackerDB = {} end
if not FlightTrackerDB.flights then FlightTrackerDB.flights = {} end
if not FlightTrackerDB.routes then FlightTrackerDB.routes = {} end
if not FlightTrackerDB.checklistExpanded then FlightTrackerDB.checklistExpanded = {} end
if not FlightTrackerDB.char then FlightTrackerDB.char = {} end
if not FlightTrackerDB.char[playerName] then FlightTrackerDB.char[playerName] = {} end
if not FlightTrackerDB.char[playerName].stats then
FlightTrackerDB.char[playerName].stats = {
totalFlights = 0,
totalTime = 0,
totalGold = 0,
longestFlight = { duration = 0, route = "None" }
}
end
self.charStats = FlightTrackerDB.char[playerName].stats
local defaultSettings = {
showTimer = true,
autoDismount = true,
confirmFlight = false,
announceFlight = false,
minimapPos = 45,
showMinimapButton = true,
lockPosition = false,
hideBorder = false
}
if not FlightTrackerDB.settings then
FlightTrackerDB.settings = {}
end
for key, value in pairs(defaultSettings) do
if FlightTrackerDB.settings[key] == nil then
FlightTrackerDB.settings[key] = value
end
end
self:Print("Loaded. Type /ft or /flighttracker to show stats.")
self:CreateTimerFrame()
self:CreateMinimapButton()
tinsert(UISpecialFrames, "FlightTrackerMain")
self:UnregisterEvent("ADDON_LOADED")
end
function FlightTracker:PLAYER_ENTERING_WORLD()
if UnitOnTaxi("player") then
isFlying = true
startTime = GetTime()
destNode = "Unknown"
flightTimerFrame.destText:SetText("In Flight")
flightTimerFrame.zoneText:SetText("")
flightTimerFrame.max = 0
flightTimerFrame:Show()
self:StartMonitor()
else
isFlying = false
isPending = false
flightTimerFrame:Hide()
end
end
function FlightTracker:TAXIMAP_OPENED()
cachedOriginNode = FlightTracker.Util.GetCurrentFlightNode()
self:ScanRoutes()
if FlightTrackerDB.settings.autoDismount then
self:DismountPlayer()
end
self:HookTaxiMap()
end
function FlightTracker:ScanRoutes()
local currentNode = cachedOriginNode
if not currentNode then return end
local faction = UnitFactionGroup("player")
if not faction then return end
if not FlightTrackerDB.routes[currentNode] then
FlightTrackerDB.routes[currentNode] = {}
end
local numNodes = NumTaxiNodes()
for i = 1, numNodes do
if TaxiNodeGetType(i) == "REACHABLE" then
local nodeName = TaxiNodeName(i)
local current = FlightTrackerDB.routes[currentNode][nodeName]
if not current or current == true then
FlightTrackerDB.routes[currentNode][nodeName] = faction
elseif current ~= faction and current ~= "Both" then
FlightTrackerDB.routes[currentNode][nodeName] = "Both"
end
end
end
if FlightTracker.Checklist and FlightTracker.Checklist:IsOpen() then
FlightTracker.Checklist:Refresh()
end
end
function FlightTracker:HookTaxiMap()
if isTooltipHooked then return end
local original_TakeTaxiNode = TakeTaxiNode
TakeTaxiNode = function(index)
local nodeType = TaxiNodeGetType(index)
if nodeType == "REACHABLE" then
local destName = TaxiNodeName(index)
if FlightTrackerDB.settings.confirmFlight and not FlightTracker.confirming then
local dialog = StaticPopup_Show("FLIGHTTRACKER_CONFIRM", destName)
if dialog then
dialog.data = {index = index, name = destName}
end
return
end
FlightTracker:PrepareFlight(index, destName)
end
original_TakeTaxiNode(index)
end
original_TaxiNodeOnButtonEnter = TaxiNodeOnButtonEnter
TaxiNodeOnButtonEnter = function(button)
original_TaxiNodeOnButtonEnter(button)
local index = button:GetID()
if index then
local nodeType = TaxiNodeGetType(index)
if nodeType == "REACHABLE" then
local destName = TaxiNodeName(index)
local origin = cachedOriginNode or FlightTracker.Util.GetCurrentFlightNode()
local key = origin .. " -> " .. destName
local duration = FlightTrackerDB.flights[key]
local timeText = "--:--"
if duration then
timeText = FlightTracker.Util.FormatTime(duration)
end
GameTooltip:AddLine("Flight Time: " .. timeText, 1, 1, 1)
GameTooltip:Show()
end
end
end
isTooltipHooked = true
end
function FlightTracker:PrepareFlight(index, destName)
isPending = true
pendingDestName = destName
pendingCost = TaxiNodeCost(index)
self.pendingStartTime = GetTime()
self:StartMonitor()
end
function FlightTracker:StartMonitor()
self.monitorTimer = 0
self:SetScript("OnUpdate", self.OnUpdateMonitor)
end
function FlightTracker:StopMonitor()
self:SetScript("OnUpdate", nil)
end
function FlightTracker.OnUpdateMonitor()
local self = FlightTracker
-- Throttle the updates to 5 times a second
self.monitorTimer = self.monitorTimer + arg1
if self.monitorTimer < 0.2 then return end
self.monitorTimer = 0
if isPending then
if UnitOnTaxi("player") then
isPending = false
self:StartFlight(pendingDestName, pendingCost)
elseif GetTime() - self.pendingStartTime > 10 then
isPending = false
self:StopMonitor()
end
elseif isFlying then
if not UnitOnTaxi("player") then
self:EndFlight()
end
else
self:StopMonitor()
end
end
function FlightTracker:StartFlight(destination, cost)
isFlying = true
startTime = GetTime()
destNode = destination
-- Update Character Stats
if self.charStats then
self.charStats.totalGold = self.charStats.totalGold + (cost or 0)
self.charStats.totalFlights = self.charStats.totalFlights + 1
end
originNode = cachedOriginNode or FlightTracker.Util.GetCurrentFlightNode()
local key = originNode .. " -> " .. destNode
local knownDuration = FlightTrackerDB.flights[key]
if FlightTrackerDB.settings.announceFlight then
local msg = "Flying to " .. destNode .. "."
if knownDuration then
msg = msg .. " ETA: " .. FlightTracker.Util.FormatTime(knownDuration)
end
if GetNumRaidMembers() > 0 then
SendChatMessage(msg, "RAID")
elseif GetNumPartyMembers() > 0 then
SendChatMessage(msg, "PARTY")
end
end
if FlightTrackerDB.settings.showTimer then
local _, _, node, zone = string.find(destNode, "^(.+), (.+)$")
if not node then
node = destNode
zone = GetZoneText()
end
flightTimerFrame.destText:SetText(node)
flightTimerFrame.zoneText:SetText(zone)
flightTimerFrame.max = knownDuration or 0
flightTimerFrame:Show()
end
if FlightTracker.GUI then FlightTracker.GUI:UpdateStats() end
end
function FlightTracker:EndFlight()
isFlying = false
flightTimerFrame:Hide()
if startTime == 0 then return end
local endTime = GetTime()
local duration = endTime - startTime
if originNode and destNode and duration > 10 then
local key = originNode .. " -> " .. destNode
-- Save flight duration GLOBALLY (keep shortest recorded time)
local existing = FlightTrackerDB.flights[key]
if not existing or duration < existing then
FlightTrackerDB.flights[key] = duration
end
-- Save statistics LOCALLY (Per Character)
if self.charStats then
self.charStats.totalTime = self.charStats.totalTime + duration
if type(self.charStats.longestFlight) ~= "table" then
self.charStats.longestFlight = { duration = self.charStats.longestFlight or 0, route = "" }
end
if duration > self.charStats.longestFlight.duration then
self.charStats.longestFlight.duration = duration
self.charStats.longestFlight.route = key
end
end
self:Print("Landed at " .. destNode .. ". Time: " .. self.Util.FormatTime(duration))
if FlightTracker.GUI then FlightTracker.GUI:UpdateStats() end
if FlightTracker.Checklist and FlightTracker.Checklist:IsOpen() then
FlightTracker.Checklist:Refresh()
end
end
startTime = 0
originNode = nil
destNode = nil
self:StopMonitor()
end
function FlightTracker:DismountPlayer()
if Dismount then
Dismount()
return
end
if not self.scanner then
self.scanner = CreateFrame("GameTooltip", "FlightTrackerScanner", nil, "GameTooltipTemplate")
self.scanner:SetOwner(WorldFrame, "ANCHOR_NONE")
end
for i = 0, 31 do
local index = GetPlayerBuff(i, "HELPFUL")
if index > -1 then
self.scanner:ClearLines()
self.scanner:SetPlayerBuff(index)
local text = FlightTrackerScannerTextLeft2:GetText()
if text and string.find(text, "Increases speed by %d+%%") then
CancelPlayerBuff(index)
return
end
end
end
end
function FlightTracker:CreateMinimapButton()
if self.minimapButton then return end
local b = CreateFrame("Button", "FlightTrackerMinimapButton", Minimap)
b:SetWidth(32)
b:SetHeight(32)
b:SetFrameStrata("MEDIUM")
b:SetFrameLevel(8)
local iconTexture = ADDON_PATH .. "img\\flight"
local t = b:CreateTexture(nil, "BACKGROUND")
t:SetTexture(iconTexture)
t:SetWidth(20)
t:SetHeight(20)
t:SetPoint("CENTER", 0, 0)
b.icon = t
local border = b:CreateTexture(nil, "OVERLAY")
border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
border:SetWidth(52)
border:SetHeight(52)
border:SetPoint("TOPLEFT", 0, 0)
b:RegisterForClicks("LeftButtonUp", "RightButtonUp")
b:SetScript("OnClick", function()
if FlightTracker.GUI then FlightTracker.GUI:Toggle() end
end)
b:SetScript("OnEnter", function()
GameTooltip:SetOwner(this, "ANCHOR_LEFT")
GameTooltip:SetText("Flight Tracker")
GameTooltip:AddLine("Click to open UI", 1, 1, 1)
GameTooltip:AddLine("Shift+Drag to move", 0.7, 0.7, 0.7)
GameTooltip:Show()
end)
b:SetScript("OnLeave", function() GameTooltip:Hide() end)
b:SetMovable(true)
b:RegisterForDrag("LeftButton")
b:SetScript("OnDragStart", function()
if IsShiftKeyDown() then
this:LockHighlight()
this.isDragging = true
end
end)
b:SetScript("OnDragStop", function() this:UnlockHighlight() this.isDragging = false end)
b:SetScript("OnUpdate", function()
if this.isDragging then
local xpos, ypos = GetCursorPosition()
local xmin, ymin = Minimap:GetLeft(), Minimap:GetBottom()
xpos = xmin - xpos / UIParent:GetScale() + 70
ypos = ypos / UIParent:GetScale() - ymin - 70
local angle = math.deg(math.atan2(ypos, xpos))
FlightTrackerDB.settings.minimapPos = angle
FlightTracker:UpdateMinimapButtonPosition()
end
end)
self.minimapButton = b
self:UpdateMinimapButtonPosition()
self:UpdateMinimapButtonVisibility()
end
function FlightTracker:UpdateMinimapButtonVisibility()
if not self.minimapButton then return end
if FlightTrackerDB.settings.showMinimapButton then
self.minimapButton:Show()
else
self.minimapButton:Hide()
end
end
function FlightTracker:UpdateMinimapButtonPosition()
if not self.minimapButton then return end
local angle = FlightTrackerDB.settings.minimapPos or 45
local radius = 80
local x = math.cos(math.rad(angle)) * radius
local y = math.sin(math.rad(angle)) * radius
self.minimapButton:ClearAllPoints()
self.minimapButton:SetPoint("CENTER", "Minimap", "CENTER", -x, y)
end
function FlightTracker:CreateTimerFrame()
local f = CreateFrame("Frame", "FlightTrackerTimer", UIParent)
f:SetWidth(180)
f:SetHeight(64)
f:SetPoint("TOP", 0, -50)
f:SetClampedToScreen(true)
f:SetFrameStrata("HIGH")
f:SetBackdrop({
bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 }
})
f:SetBackdropColor(0.1, 0.1, 0.1, 0.9)
f:SetBackdropBorderColor(0.4, 0.4, 0.4, 1)
f:EnableMouse(true)
f:SetMovable(true)
f:RegisterForDrag("LeftButton")
f:SetScript("OnMouseDown", function()
if IsShiftKeyDown() and arg1 == "LeftButton" then
this:StartMoving()
end
end)
f:SetScript("OnMouseUp", function()
if arg1 == "LeftButton" then this:StopMovingOrSizing() end
end)
f:Hide()
f:SetResizable(true)
f:SetMinResize(140, 64)
f:SetMaxResize(300, 100)
local resizer = CreateFrame("Button", nil, f)
resizer:SetWidth(16)
resizer:SetHeight(16)
resizer:SetPoint("BOTTOMRIGHT", -4, 4)
resizer:SetNormalTexture(ADDON_PATH .. "img\\sizegrabber-up.tga")
resizer:SetHighlightTexture(ADDON_PATH .. "img\\sizegrabber-highlight.tga")
resizer:SetPushedTexture(ADDON_PATH .. "img\\sizegrabber-down.tga")
resizer:SetScript("OnMouseDown", function()
f:StartSizing("BOTTOMRIGHT")
end)
resizer:SetScript("OnMouseUp", function()
f:StopMovingOrSizing()
end)
f.resizer = resizer
f.destText = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
f.destText:SetPoint("TOP", 0, -10)
f.destText:SetText("Destination")
f.destText:SetFont("Fonts\\FRIZQT__.TTF", 12)
f.zoneText = f:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
f.zoneText:SetPoint("TOP", f.destText, "BOTTOM", 0, -2)
f.zoneText:SetText("Zone Name")
f.zoneText:SetTextColor(0.7, 0.7, 0.7)
f.zoneText:SetFont("Fonts\\FRIZQT__.TTF", 10)
f.timerText = f:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
f.timerText:SetPoint("BOTTOM", 0, 10)
f.timerText:SetText("00:00")
f.timerText:SetTextColor(1, 0.82, 0)
f.timerText:SetFont("Fonts\\FRIZQT__.TTF", 16)
f:SetScript("OnSizeChanged", function()
local h = this:GetHeight()
local scale = h / 64
if scale < 0.8 then scale = 0.8 end
this.destText:SetFont("Fonts\\FRIZQT__.TTF", 12 * scale)
this.zoneText:SetFont("Fonts\\FRIZQT__.TTF", 10 * scale)
this.timerText:SetFont("Fonts\\FRIZQT__.TTF", 16 * scale)
end)
local help = CreateFrame("Frame", nil, f)
help:SetWidth(16)
help:SetHeight(16)
help:SetPoint("TOPRIGHT", -4, -4)
help:EnableMouse(true)
local helpText = help:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
helpText:SetPoint("CENTER", 0, 0)
helpText:SetText("?")
helpText:SetTextColor(0.5, 0.5, 0.5)
help:SetScript("OnEnter", function()
GameTooltip:SetOwner(this, "ANCHOR_RIGHT")
GameTooltip:SetText("Shift+Drag to Move")
GameTooltip:Show()
helpText:SetTextColor(1, 1, 1)
end)
help:SetScript("OnLeave", function()
GameTooltip:Hide()
helpText:SetTextColor(0.5, 0.5, 0.5)
end)
f.help = help
f:SetScript("OnUpdate", function()
if not isFlying then return end
if not this.elapsed then this.elapsed = 0 end
this.elapsed = this.elapsed + arg1
if this.elapsed < 0.5 then return end
this.elapsed = 0
local current = GetTime() - startTime
local text = ""
if this.max and this.max > 0 then
local remaining = this.max - current
if remaining < 0 then remaining = 0 end
text = FlightTracker.Util.FormatTime(remaining)
else
text = FlightTracker.Util.FormatTime(current)
end
this.timerText:SetText(text)
end)
flightTimerFrame = f
self:ApplyTimerBorderVisibility()
end
function FlightTracker:ApplyTimerBorderVisibility()
if not flightTimerFrame then return end
if FlightTrackerDB.settings.hideBorder then
flightTimerFrame:SetBackdrop(nil)
flightTimerFrame.resizer:Hide()
flightTimerFrame.help:Hide()
else
flightTimerFrame:SetBackdrop({
bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 }
})
flightTimerFrame:SetBackdropColor(0.1, 0.1, 0.1, 0.9)
flightTimerFrame:SetBackdropBorderColor(0.4, 0.4, 0.4, 1)
flightTimerFrame.resizer:Show()
flightTimerFrame.help:Show()
end
end
SLASH_FLIGHTTRACKER1 = "/ft"
SLASH_FLIGHTTRACKER2 = "/flighttracker"
SlashCmdList["FLIGHTTRACKER"] = function(msg)
if msg == "routes" or msg == "checklist" then
if FlightTracker.Checklist then
FlightTracker.Checklist:Toggle()
else
FlightTracker:Print("Checklist module not loaded.")
end
elseif FlightTracker.GUI then
FlightTracker.GUI:Toggle()
else
FlightTracker:Print("GUI module not loaded.")
end
end
function FlightTracker:Print(msg)
local prefix = "|cffE0C709Flight|cffffffffTracker:|r"
DEFAULT_CHAT_FRAME:AddMessage(prefix .. " " .. tostring(msg))
end