-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRoutes.lua
3722 lines (3439 loc) · 115 KB
/
Routes.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
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
********************************************************************************
Routes
@project-version@
16 October 2014
(Originally written for Live Servers v4.3.0.15050)
(Hotfixed for v6.0.2.19034)
Author: Xaroz @ EU Emerald Dream Alliance & Xinhuan @ US Blackrock Alliance
********************************************************************************
Description:
Routes allow you to draw lines on the worldmap linking nodes together into
an efficient farming route from existing databases. The route will be shown
(by default) on the minimap and zone map as well.
Features:
- Select node-types to build a line upon. The following are supported
* Cartographer_Fishing
* Cartographer_Mining
* Cartographer_Herbalism
* Cartographer_ExtractGas
* Cartographer_Treasure
* GatherMate
* GatherMate2
* Gatherer
* HandyNotes
- Optimize your route using the traveling salesmen problem (TSP) ant
colony optimization (ACO) algorithm
- Background (nonblocking) and foreground (blocking) optimization
- Select color/thickness/transparency/visibility for each route
- For any route created, finding a new node will try to add that as
optimal as possible
- Quick clustering algorithm to merge nearby nodes into a single traveling
point
- Quickly mark entire areas/regions as "out of bounds" or "taboo" to Routes,
meaning your routes will ignore nodes in those areas and avoid cross them
- Fubar plugin available to quickly access your routes
- Cartographer_Waypoints and TomTom support for quickly following a route
- Works with Chinchilla's Expander minimap and SexyMap's HudMap!
- Full in-game help file and FAQ, guiding you step by step on what to do!
Download:
The latest version of Routes is always available on
- http://www.wowace.com/projects/routes/
- http://wow.curse.com/downloads/wow-addons/details/routes.aspx
- http://www.wowinterface.com/downloads/info11401-Routes.html
Localization:
You can contribute by updating/adding localizations using the system on
- http://www.wowace.com/projects/routes/localization/
Contact:
If you find any bugs or have any suggestions, you can contact us on:
Forum: http://forums.wowace.com/showthread.php?t=10369
IRC : Grum or Xinhuan on irc://irc.freenode.org/wowace
Email: Grum ( routes AT grum DOT nl )
Xinhuan ( xinhuan AT gmail DOT com )
Paypal donations are welcome ;)
]]
Routes = LibStub("AceAddon-3.0"):NewAddon("Routes", "AceConsole-3.0", "AceEvent-3.0", "AceHook-3.0")
local Routes = Routes
local L = LibStub("AceLocale-3.0"):GetLocale("Routes", false)
local G = {} -- was Graph-1.0, but we removed the dependency
Routes.G = G
Routes.Dragons = LibStub("HereBeDragons-2.0")
local WoW90 = select(4, GetBuildInfo()) >= 90000
-- database defaults
local db
local defaults = {
global = {
routes = {
['*'] = { -- zone name, stored as the MapFile string constant
['*'] = { -- route name
route = {}, -- point, point, point
color = nil, -- defaults to db.defaults.color if nil
width = nil, -- defaults to db.defaults.width if nil
width_minimap = nil, -- defaults to db.defaults.width_minimap if nil
width_battlemap = nil, -- defaults to db.defaults.width_battlemap if nil
hidden = false, -- boolean
looped = 1, -- looped? 1 is used (instead of true) because initial early code used 1 inside route creation code
visible = true, -- visible?
length = 0, -- length
selection = {
['**'] = false -- Node we're interested in tracking
},
db_type = {
['**'] = false -- db_types used for use with auto show/hide
},
taboos = {
['**'] = false -- taboo regions in effect
},
taboolist = {} -- point, point, point
},
},
},
taboo = {
['*'] = { -- zone name, stored as the MapFile string constant
['*'] = { -- route name
route = {}, -- point, point, point
},
},
},
defaults = { -- r, g, b, a
color = { 1, 0.75, 0.75, 1 },
hidden_color = { 1, 1, 1, 0.5 },
width = 30,
width_minimap = 25,
width_battlemap = 15,
show_hidden = false,
update_distance = 1,
fake_point = -1,
fake_data = 'dummy',
draw_minimap = true,
draw_worldmap = true,
draw_battlemap = true,
draw_indoors = false,
tsp = {
initial_pheromone = 0.1, -- Initial pheromone trail value
alpha = 1, -- Likelihood of ants to follow pheromone trails (larger value == more likely)
beta = 6, -- Likelihood of ants to choose closer nodes (larger value == more likely)
local_decay = 0.2, -- Governs local trail decay rate [0, 1]
local_update = 0.4, -- Amount of pheromone to reinforce local trail update by
global_decay = 0.2, -- Governs global trail decay rate [0, 1]
twoopt_passes = 3, -- Number of times to perform 2-opt passes
two_point_five_opt = false, -- Perform optimized 2-opt pass
},
prof_options = {
['*'] = "Always",
},
use_auto_showhide = false,
waypoint_hit_distance = 50,
line_gaps = true,
line_gaps_skip_cluster = true,
cluster_dist = 60,
callbacks = {
['*'] = true
}
},
}
}
-- Ace Options Table for our addon
local options
-- Plugins table
Routes.plugins = {}
-- Lookup table for aceoptkey-route/taboo conversion
Routes.routekeys = setmetatable({}, { __index = function(t,k) if k == "string" and tonumber(k) then return t[tonumber(k)] end return nil end })
Routes.tabookeys = setmetatable({}, { __index = function(t,k) if k == "string" and tonumber(k) then return t[tonumber(k)] end return nil end })
-- localize some globals
local pairs, next = pairs, next
local tinsert, tremove = tinsert, tremove
local floor = math.floor
local format = string.format
local math_abs = math.abs
local math_sin = math.sin
local math_cos = math.cos
local Minimap = Minimap
local GetPlayerFacing = GetPlayerFacing
------------------------------------------------------------------------------------------------------
-- Data for Localized Zone Names
local function GetZoneName(uiMapID)
-- Change individual zone display format in UI, eg. for the different Dalaran's.
local name = Routes.Dragons:GetLocalizedMap(uiMapID)
-- Outland
if uiMapID == 104 or uiMapID == 107 then
-- Shadowmoon Valley (Outland)
-- Nagrand (Outland)
name = format("%s (%s)", name, Routes.Dragons:GetLocalizedMap(101))
-- Northrend
elseif uiMapID == 125 then
-- Dalaran (Northrend)
name = format("%s (%s)", name, Routes.Dragons:GetLocalizedMap(113))
elseif uiMapID == 2104 then
-- Wintergrasp (BG)
name = format("%s (BG)", name)
elseif uiMapID == 1527 or uiMapID == 1530 then -- Black Empire zones (Uldum, Vale)
name = format("%s (Black Empire)", name)
elseif uiMapID == 2070 then -- Tirisfal Glades after the fall of Undercity
name = format("%s (Lordaeron)", name)
end
return name
end
local function GetZoneNameSafe(uiMapID)
local name = GetZoneName(uiMapID)
return name or ("Zone #%s"):format(tostring(uiMapID))
end
Routes.GetZoneName = GetZoneName
Routes.LZName = setmetatable({}, { __index = function() return 0 end})
local COSMIC_MAP_ID = 946
local WORLD_MAP_ID = 947
local validParentIDs = { [COSMIC_MAP_ID] = true, [WORLD_MAP_ID] = true }
local function validMapParent(id)
-- invalid ID
if not id or id == 0 then return false end
-- parent we alreadychecked
if validParentIDs[id] then return true end
-- get map data
local data = Routes.Dragons.mapData[id]
if not data then return false end
-- only zones and continents
if data.mapType ~= Enum.UIMapType.Zone and data.mapType ~= Enum.UIMapType.Continent then return false end
-- walk up the tree
return validMapParent(data.parent)
end
local function processMapEntries()
for id, data in pairs(Routes.Dragons.mapData) do
if (data.mapType == Enum.UIMapType.Zone or data.mapType == Enum.UIMapType.Continent) and validMapParent(data.parent) then
validParentIDs[data.parent] = true
local name = GetZoneName(id)
--[[
if Routes.LZName[name] and Routes.LZName[name] ~= 0 then
print(("Routes: Name %q already mapped to %d (new: %d)"):format(name, Routes.LZName[name], id))
end
--]]
Routes.LZName[name] = id
end
end
end
-- initialize map list
processMapEntries()
------------------------------------------------------------------------------------------------------
-- Core Routes functions
--[[ Our coordinate format for Routes
Warning: These are convenience functions, most of the :getXY() and :getID()
code are inlined in critical code paths in various functions, changing
the coord storage format requires changing the inlined code in numerous
locations in addition to these 2 functions
]]
function Routes:getID(x, y)
return floor(x * 10000 + 0.5) * 10000 + floor(y * 10000 + 0.5)
end
function Routes:getXY(id)
return floor(id / 10000) / 10000, (id % 10000) / 10000
end
local MinimapShapes = {
-- quadrant booleans (same order as SetTexCoord)
-- {upper-left, lower-left, upper-right, lower-right}
-- true = rounded, false = squared
["ROUND"] = { true, true, true, true},
["SQUARE"] = {false, false, false, false},
["CORNER-TOPLEFT"] = { true, false, false, false},
["CORNER-TOPRIGHT"] = {false, false, true, false},
["CORNER-BOTTOMLEFT"] = {false, true, false, false},
["CORNER-BOTTOMRIGHT"] = {false, false, false, true},
["SIDE-LEFT"] = { true, true, false, false},
["SIDE-RIGHT"] = {false, false, true, true},
["SIDE-TOP"] = { true, false, true, false},
["SIDE-BOTTOM"] = {false, true, false, true},
["TRICORNER-TOPLEFT"] = { true, true, true, false},
["TRICORNER-TOPRIGHT"] = { true, false, true, true},
["TRICORNER-BOTTOMLEFT"] = { true, true, false, true},
["TRICORNER-BOTTOMRIGHT"] = {false, true, true, true},
}
local minimap_radius
local minimap_rotate
local indoors = "indoor"
local MinimapSize = { -- radius of minimap
indoor = {
[0] = 150,
[1] = 120,
[2] = 90,
[3] = 60,
[4] = 40,
[5] = 25,
},
outdoor = {
[0] = 233 + 1/3,
[1] = 200,
[2] = 166 + 2/3,
[3] = 133 + 1/3,
[4] = 100,
[5] = 66 + 2/3,
},
}
local function is_round( dx, dy )
local map_shape = GetMinimapShape and GetMinimapShape() or "ROUND"
local q = 1
if dx > 0 then q = q + 2 end -- right side
-- XXX Tripple check this
if dy > 0 then q = q + 1 end -- bottom side
return MinimapShapes[map_shape][q]
end
local function is_inside( sx, sy, cx, cy, radius )
local dx = sx - cx
local dy = sy - cy
if is_round( dx, dy ) then
return dx*dx+dy*dy <= radius*radius
else
return math_abs( dx ) <= radius and math_abs( dy ) <= radius
end
end
local function GetFacing()
if GetPlayerFacing then return GetPlayerFacing() end
return -MiniMapCompassRing:GetFacing()
end
local last_X, last_Y, last_facing = math.huge, math.huge, math.huge
-- implementation of cache - use zone in the key for an unique identifier
-- because every zone has a different X/Y location and possible yardsizes
local cache_zone, cache_zoneW, cache_zoneH
local X_cache = {}
local Y_cache = {}
local XY_cache_mt = {
__index = function(t, key)
local zone, coord = (';'):split( key )
if cache_zone ~= zone then
cache_zoneW, cache_zoneH = Routes.Dragons:GetZoneSize(tonumber(zone))
cache_zone = zone
end
local X = cache_zoneW * floor(coord / 10000) / 10000
local Y = cache_zoneH * (coord % 10000) / 10000
X_cache[key] = X
Y_cache[key] = Y
-- figure out which one to return
if t == X_cache then return X else return Y end
end
}
setmetatable( X_cache, XY_cache_mt )
setmetatable( Y_cache, XY_cache_mt )
function Routes:DrawMinimapLines(forceUpdate)
if not db.defaults.draw_minimap then
G:HideLines(Minimap)
return
end
local _x, _y, currentZoneID = self.Dragons:GetPlayerZonePosition(true)
-- invalid coordinates - clear map
if not _x or not _y then
G:HideLines(Minimap)
return
end
-- if we are indoors, or the zone we are in is not defined in our tables ... no routes
-- double check zoom as onload doesnt get you the map zoom
indoors = GetCVar("minimapZoom")+0 == Minimap:GetZoom() and "outdoor" or "indoor"
if not db.defaults.draw_indoors and indoors == "indoor" then
G:HideLines(Minimap)
return
end
local defaults = db.defaults
local zoneW, zoneH = self.Dragons:GetZoneSize(currentZoneID)
if not zoneW or zoneW == 0 then return end
local cx, cy = zoneW * _x, zoneH * _y
local facing, sin, cos
if minimap_rotate then
facing = GetFacing()
if not facing then
return
end
end
if (not forceUpdate) and facing == last_facing and (last_X-cx)^2 + (last_Y-cy)^2 < defaults.update_distance^2 then
-- no update!
return
end
G:HideLines(Minimap)
last_X = cx
last_Y = cy
last_facing = facing
if minimap_rotate then
sin = math_sin(facing)
cos = math_cos(facing)
end
if WoW90 then
minimap_radius = C_Minimap.GetViewRadius()
else
minimap_radius = MinimapSize[indoors][Minimap:GetZoom()]
end
local radius = minimap_radius
local radius2 = radius * radius
local minX = cx - radius
local maxX = cx + radius
local minY = cy - radius
local maxY = cy + radius
local div_by_zero_nudge = 0.000001
local minimap_w = Minimap:GetWidth()
local minimap_h = Minimap:GetHeight()
local scale_x = minimap_w / (radius*2)
local scale_y = minimap_h / (radius*2)
local minimapScale = Minimap:GetScale()
for route_name, route_data in pairs( db.routes[ currentZoneID ] ) do
if type(route_data) == "table" and type(route_data.route) == "table" and #route_data.route > 1 then
-- store color/width
local width = (route_data.width_minimap or defaults.width_minimap) / (minimapScale)
local color = route_data.color or defaults.color
-- unless we show hidden
if (not route_data.hidden and (route_data.visible or not defaults.use_auto_showhide)) or defaults.show_hidden then
-- use this special color
if route_data.hidden then
color = defaults.hidden_color
end
-- some state data
local last_x = nil
local last_y = nil
local last_inside = nil
-- if we loop - make sure the 'last' gets filled with the right info
if route_data.looped and route_data.route[ #route_data.route ] ~= defaults.fake_point then
local key = format("%s;%s", currentZoneID, route_data.route[ #route_data.route ])
last_x, last_y = X_cache[key], Y_cache[key]
if minimap_rotate then
local dx = last_x - cx
local dy = last_y - cy
last_x = cx + dx*cos - dy*sin
last_y = cy + dx*sin + dy*cos
end
last_inside = is_inside( last_x, last_y, cx, cy, radius )
end
-- loop over the route
for i = 1, #route_data.route do
local point = route_data.route[i]
local cur_x, cur_y, cur_inside
-- if we have a 'fake point' (gap) - clear current values
if point == defaults.fake_point then
cur_x = nil
cur_y = nil
cur_inside = false
else
local key = format("%s;%s", currentZoneID, point)
cur_x, cur_y = X_cache[key], Y_cache[key]
if minimap_rotate then
local dx = cur_x - cx
local dy = cur_y - cy
cur_x = cx + dx*cos - dy*sin
cur_y = cy + dx*sin + dy*cos
end
cur_inside = is_inside( cur_x, cur_y, cx, cy, radius )
end
-- check if we have any nil values (we cant draw) and check boundingbox
if cur_x and cur_y and last_x and last_y and not (
( cur_x < minX and last_x < minX ) or
( cur_x > maxX and last_x > maxX ) or
( cur_y < minY and last_y < minY ) or
( cur_y > maxY and last_y > maxY )
)
then
-- default all to not drawing
local draw_sx = nil
local draw_sy = nil
local draw_ex = nil
local draw_ey = nil
-- both inside - easy! draw
if cur_inside and last_inside then
draw_sx = last_x
draw_sy = last_y
draw_ex = cur_x
draw_ey = cur_y
else
-- direction of line
local dx = last_x - cur_x
local dy = last_y - cur_y
-- calculate point on perpendicular line
local zx = cx - dy
local zy = cy + dx
-- nudge it a bit so we dont get div by 0 problems
if dx == 0 then dx = div_by_zero_nudge end
if dy == 0 then dy = div_by_zero_nudge end
-- calculate intersection point
local nd = ((cx -last_x)*(cy-zy) - (cx-zx)*(cy -last_y)) /
((cur_x-last_x)*(cy-zy) - (cx-zx)*(cur_y-last_y))
-- perpendicular point (closest to center on the line given)
local px = last_x + nd * -dx
local py = last_y + nd * -dy
-- check range of intersect point
local dpc_x = cx - px
local dpc_y = cy - py
-- distance^2 of the perpendicular point
local lenpc = dpc_x*dpc_x + dpc_y*dpc_y
-- the line can only intersect if the perpendicular point is at
-- least closer than the furthest away point (one of the corners)
if lenpc < 2*radius2 then
-- if inside - ready to draw
if cur_inside then
draw_ex = cur_x
draw_ey = cur_y
else
-- if we're not inside we can still be in the square - if so dont do any intersection
-- calculations yet
if math_abs( cur_x - cx ) < radius and math_abs( cur_y - cy ) < radius then
draw_ex = cur_x
draw_ey = cur_y
else
-- need to intersect against the square
-- likely x/y to intersect with
local minimap_cur_x = cx + radius * (dx < 0 and 1 or -1)
local minimap_cur_y = cy + radius * (dy < 0 and 1 or -1)
-- which intersection is furthest?
local delta_cur_x = (minimap_cur_x - cur_x) / -dx
local delta_cur_y = (minimap_cur_y - cur_y) / -dy
-- dark magic - needs to be changed to positive signs whenever i can care about it
if delta_cur_x < delta_cur_y and delta_cur_x < 0 then
draw_ex = minimap_cur_x
draw_ey = cur_y + -dy*delta_cur_x
else
draw_ex = cur_x + -dx*delta_cur_y
draw_ey = minimap_cur_y
end
-- check if we didn't calculate some wonky offset - has to be inside with
-- some slack on accuracy
if math_abs( draw_ex - cx ) > radius*1.01 or
math_abs( draw_ey - cy ) > radius*1.01
then
draw_ex = nil
draw_ey = nil
end
end
-- we might have a round corner here - lets see if the quarter with the intersection is round
if draw_ex and draw_ey and is_round( draw_ex - cx, draw_ey - cy ) then
-- if we are also within the circle-range
if lenpc < radius2 then
-- circle intersection
local dcx = cx - cur_x
local dcy = cy - cur_y
local len_dc = dcx*dcx + dcy*dcy
local len_d = dx*dx + dy*dy
local len_ddc = dx*dcx + dy*dcy
-- discriminant
local d_sqrt = ( len_ddc*len_ddc - len_d * (len_dc - radius2) )^0.5
-- calculate point
draw_ex = cur_x - dx * (-len_ddc + d_sqrt) / len_d
draw_ey = cur_y - dy * (-len_ddc + d_sqrt) / len_d
-- have to be on the *same* side of the perpendicular point else it's fake
if (draw_ex - px)/math_abs(draw_ex - px) ~= (cur_x- px)/math_abs(cur_x - px) or
(draw_ey - py)/math_abs(draw_ey - py) ~= (cur_y- py)/math_abs(cur_y - py)
then
draw_ex = nil
draw_ey = nil
end
else
draw_ex = nil
draw_ey = nil
end
end
end
-- if inside - ready to draw
if last_inside then
draw_sx = last_x
draw_sy = last_y
else
-- if we're not inside we can still be in the square - if so dont do any intersection
-- calculations yet
if math_abs( last_x - cx ) < radius and math_abs( last_y - cy ) < radius then
draw_sx = last_x
draw_sy = last_y
else
-- need to intersect against the square
-- likely x/y to intersect with
local minimap_last_x = cx + radius * (dx > 0 and 1 or -1)
local minimap_last_y = cy + radius * (dy > 0 and 1 or -1)
-- which intersection is furthest?
local delta_last_x = (minimap_last_x - last_x) / dx
local delta_last_y = (minimap_last_y - last_y) / dy
-- dark magic - needs to be changed to positive signs whenever i can care about it
if delta_last_x < delta_last_y and delta_last_x < 0 then
draw_sx = minimap_last_x
draw_sy = last_y + dy*delta_last_x
else
draw_sx = last_x + dx*delta_last_y
draw_sy = minimap_last_y
end
-- check if we didn't calculate some wonky offset - has to be inside with
-- some slack on accuracy
if math_abs( draw_sx - cx ) > radius*1.01 or
math_abs( draw_sy - cy ) > radius*1.01
then
draw_sx = nil
draw_sy = nil
end
end
-- we might have a round corner here - lets see if the quarter with the intersection is round
if draw_sx and draw_sy and is_round( draw_sx - cx, draw_sy - cy ) then
-- if we are also within the circle-range
if lenpc < radius2 then
-- circle intersection
local dcx = cx - cur_x
local dcy = cy - cur_y
local len_dc = dcx*dcx + dcy*dcy
local len_d = dx*dx + dy*dy
local len_ddc = dx*dcx + dy*dcy
-- discriminant
local d_sqrt = ( len_ddc*len_ddc - len_d * (len_dc - radius2) )^0.5
-- calculate point
draw_sx = cur_x - dx * (-len_ddc - d_sqrt) / len_d
draw_sy = cur_y - dy * (-len_ddc - d_sqrt) / len_d
-- have to be on the *same* side of the perpendicular point else it's fake
if (draw_sx - px)/math_abs(draw_sx - px) ~= (last_x- px)/math_abs(last_x - px) or
(draw_sy - py)/math_abs(draw_sy - py) ~= (last_y- py)/math_abs(last_y - py)
then
draw_sx = nil
draw_sy = nil
end
else
draw_sx = nil
draw_sy = nil
end
end
end
end
end
if draw_sx and draw_sy and draw_ex and draw_ey then
-- translate to left bottom corner and apply scale
draw_sx = (draw_sx - minX) * scale_x
draw_sy = minimap_h - (draw_sy - minY) * scale_y
draw_ex = (draw_ex - minX) * scale_x
draw_ey = minimap_h - (draw_ey - minY) * scale_y
if defaults.line_gaps then
-- shorten the line by 5 pixels (scaled) on endpoints inside the Minimap
local gapConst = 5 / minimapScale
local dx = draw_sx - draw_ex
local dy = draw_sy - draw_ey
local l = (dx*dx + dy*dy)^0.5
local x = gapConst * dx / l
local y = gapConst * dy / l
local shorten1, shorten2
if last_inside then shorten1 = true else shorten1 = false end
if cur_inside then shorten2 = true else shorten2 = false end
if shorten2 and route_data.metadata and defaults.line_gaps_skip_cluster and #route_data.metadata[i] > 1 then
shorten2 = false
end
if shorten1 and route_data.metadata and defaults.line_gaps_skip_cluster and #route_data.metadata[(i-1 == 0) and #route_data.route or i-1] > 1 then
shorten1 = false
end
if shorten1 and shorten2 and l > (gapConst*2) then -- draw if line is 10 or more pixels (scaled)
G:DrawLine( Minimap, draw_sx-x, draw_sy-y, draw_ex+x, draw_ey+y, width, color, "ARTWORK")
elseif shorten1 and not shorten2 and l > gapConst then
G:DrawLine( Minimap, draw_sx-x, draw_sy-y, draw_ex, draw_ey, width, color, "ARTWORK")
elseif shorten2 and not shorten1 and l > gapConst then
G:DrawLine( Minimap, draw_sx, draw_sy, draw_ex+x, draw_ey+y, width, color, "ARTWORK")
elseif not shorten1 and not shorten2 then
G:DrawLine( Minimap, draw_sx, draw_sy, draw_ex, draw_ey, width, color, "ARTWORK")
end
else
G:DrawLine( Minimap, draw_sx, draw_sy, draw_ex, draw_ey, width, color, "ARTWORK")
end
end
end
-- store last point
last_x = cur_x
last_y = cur_y
last_inside = cur_inside
end
end
end
end
end
-- This frame is to throttle InsertNode() and DeleteNode() calls so that
-- redrawing the map lines are delayed by 1 frame. These 2 functions can
-- potentially be spammed by a source database importing nodes.
local throttleFrame = CreateFrame("Frame")
throttleFrame:Hide()
throttleFrame:SetScript("OnUpdate", function(self, elapsed)
Routes:DrawWorldmapLines()
Routes:DrawMinimapLines(true)
self:Hide()
end)
-- Accepts a zone name, coord and node_name
-- for inserting into relevant routes
-- Zone name must be localized, node_name can be english or localized
function Routes:InsertNode(zone, coord, node_name)
for route_name, route_data in pairs( db.routes[ self.LZName[zone] ] ) do
-- for every route check if the route is created with this node
if route_data.selection then
for k, v in pairs(route_data.selection) do
if k == node_name or v == node_name then
-- Add the node
local x, y = self:getXY(coord)
local flag = false
for tabooname, used in pairs(route_data.taboos) do
if used and self:IsNodeInTaboo(x, y, db.taboo[ self.LZName[zone] ][tabooname]) then
flag = true
end
end
if flag then
tinsert(route_data.taboolist, coord)
else
route_data.length = self.TSP:InsertNode(route_data.route, route_data.metadata, self.LZName[zone], coord, route_data.cluster_dist or 65) -- 65 is the old default
throttleFrame:Show()
end
break
end
end
end
end
end
-- Accepts a zone name, coord and node_name
-- for deleting into relevant routes
-- Zone name must be localized, node_name can be english or localized
function Routes:DeleteNode(zone, coord, node_name)
for route_name, route_data in pairs( db.routes[ self.LZName[zone] ] ) do
-- for every route check if the route is created with this node
if route_data.selection then
local flag = false
for k, v in pairs(route_data.selection) do
if k == node_name or v == node_name then
-- Delete the node if it exists in this route
if route_data.metadata then
-- this is a clustered route
for i = 1, #route_data.route do
local num_data = #route_data.metadata[i]
for j = 1, num_data do
if coord == route_data.metadata[i][j] then
-- recalcuate centroid
local x, y = self:getXY(coord)
local cx, cy = self:getXY(route_data.route[i])
if num_data > 1 then
-- more than 1 node in this cluster
cx, cy = (cx * num_data - x) / (num_data-1), (cy * num_data - y) / (num_data-1)
tremove(route_data.metadata[i], j)
route_data.route[i] = self:getID(cx, cy)
else
-- only 1 node in this cluster, just remove it
tremove(route_data.metadata, i)
tremove(route_data.route, i)
end
route_data.length = self.TSP:PathLength(route_data.route, self.LZName[zone])
throttleFrame:Show()
flag = true
break
end
end
if flag then break end
end
else
-- this is not a clustered route
for i = 1, #route_data.route do
if coord == route_data.route[i] then
tremove(route_data.route, i)
route_data.length = self.TSP:PathLength(route_data.route, self.LZName[zone])
throttleFrame:Show()
flag = true
break
end
end
end
if not flag then
-- node not found yet, so search the taboolist
for i = 1, #route_data.taboolist do
if route_data.taboolist[i] == coord then
tremove(route_data.taboolist, i)
flag = true
break
end
end
end
end
if flag then break end
end
end
end
end
-- This function upgrades the Routes old storage format which used mapFiles
-- to the new format using uiMapIDs in WoW 8.0
local HBDMigrate = LibStub("HereBeDragons-Migrate")
function Routes:UpgradeStorageFormat2()
local t = {}
for zone, zone_table in pairs(db.routes) do
if type(zone) == "string" then
-- This zone is a string, not a uiMapID, obtain the ID from HBD-Migrate
local uiMapID = HBDMigrate:GetUIMapIDFromMapFile(zone)
-- if no mapfile was found, maybe this was a named zone from way before
if not uiMapID then
uiMapID = self.LZName[zone]
-- 0 is invalid from the metatable
if uiMapID == 0 then uiMapID = nil end
end
if not uiMapID then
-- invalid zone, delete the whole zone
db.routes[zone] = nil
else
-- We found a match, store the zone_table temporarily first
-- and delete the whole zone (because we cannot insert new
-- keys into db.routes[] while iterating over it)
t[uiMapID] = zone_table
db.routes[zone] = nil
end
end
end
for uiMapID, zone_table in pairs(t) do
-- Now assign the new zone uiMapID keys
db.routes[uiMapID] = zone_table
end
table.wipe(t)
-- Do the same with the taboo table
for zone, zone_table in pairs(db.taboo) do
if type(zone) == "string" then
-- This zone is a string, not a uiMapID, obtain the ID from HBD-Migrate
local uiMapID = HBDMigrate:GetUIMapIDFromMapFile(zone)
-- if no mapfile was found, maybe this was a named zone from way before
if not uiMapID then
uiMapID = self.LZName[zone]
-- 0 is invalid from the metatable
if uiMapID == 0 then uiMapID = nil end
end
if not uiMapID then
-- invalid zone, delete the whole zone
db.taboo[zone] = nil
else
-- We found a match, store the zone_table temporarily first
-- and delete the whole zone (because we cannot insert new
-- keys into db.routes[] while iterating over it)
t[uiMapID] = zone_table
db.taboo[zone] = nil
end
end
end
for uiMapID, zone_table in pairs(t) do
-- Now assign the new zone uiMapID keys
db.taboo[uiMapID] = zone_table
end
-- Reclaim memory for this function
self.UpgradeStorageFormat2 = nil
end
-- Common subtables for zone and table description
local route_zone_args_desc_table = {
type = "description",
name = function(info)
local zone = tonumber(info[2])
local count = 0
for route_name, route_table in pairs(db.routes[zone]) do
if #route_table.route > 0 then
count = count + 1
end
end
return L["You have |cffffd200%d|r route(s) in |cffffd200%s|r."]:format(count, GetZoneNameSafe(zone))
end,
order = 0,
}
local taboo_zone_args_desc_table = {
type = "description",
name = function(info)
local zone = tonumber(info[2])
local count = 0
for taboo_name, taboo_table in pairs(db.taboo[zone]) do
if #taboo_table.route > 0 then
count = count + 1
end
end
return L["You have |cffffd200%d|r taboo region(s) in |cffffd200%s|r."]:format(count, GetZoneNameSafe(zone))
end,
order = 0,
}
------------------------------------------------------------------------------------------------------
-- General event functions
function Routes:OnInitialize()
-- Initialize database
self.db = LibStub("AceDB-3.0"):New("RoutesDB", defaults, true)
db = self.db.global
self.options = options
-- Initialize the ace options table
LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("Routes", options)
local f = function() LibStub("AceConfigDialog-3.0"):Open("Routes") end
self:RegisterChatCommand(L["routes"], f)
if L["routes"] ~= "routes" then
self:RegisterChatCommand("routes", f)
end
-- Upgrade old storage format (which was dependant on LibBabble-Zone-3.0
-- to the new format that doesn't require it
-- Also delete any invalid zones
self:UpgradeStorageFormat2()
-- Generate ace options table for each route
local opts = options.args.routes_group.args
for zone, zone_table in pairs(db.routes) do
if next(zone_table) == nil then
-- cleanup the empty zone
db.routes[zone] = nil
else
local localizedZoneName = GetZoneNameSafe(zone)
opts[tostring(zone)] = {
type = "group",
name = localizedZoneName,
desc = L["Routes in %s"]:format(localizedZoneName),
args = {
desc = route_zone_args_desc_table,
},
}
self.routekeys[zone] = {}
for route, route_table in pairs(zone_table) do
local routekey = route:gsub("%s", "\255") -- can't have spaces in the key
self.routekeys[zone][routekey] = route
opts[tostring(zone)].args[routekey] = self:GetAceOptRouteTable()
route_table.editing = nil -- in case server crashes during edit.
end
end
end
-- Generate ace options table for each taboo region
local opts = options.args.taboo_group.args
for zone, zone_table in pairs(db.taboo) do
if next(zone_table) == nil then
-- cleanup the empty zone
db.taboo[zone] = nil
else
local localizedZoneName = GetZoneNameSafe(zone)
opts[tostring(zone)] = {
type = "group",
name = localizedZoneName,
desc = L["Taboos in %s"]:format(localizedZoneName),
args = {
desc = taboo_zone_args_desc_table,
},
}
self.tabookeys[zone] = {}
for taboo in pairs(zone_table) do
local tabookey = taboo:gsub("%s", "\255") -- can't have spaces in the key