-- Ship Routes: draws boat, zeppelin and airship routes on the world map, with a marker -- at each dock that says where the ship goes. Route data is in Data.lua (generated). local addonName, ns = ... local PREFIX = "|cff66bbffShip Routes|r: " local TEMPLATE = "ShipRoutesPinTemplate" local ICONS = "Interface\\AddOns\\ShipRoutes\\Icons\\" local GLYPH = { boat = ICONS .. "anchor", air = ICONS .. "zeppelin", exit = ICONS .. "arrow" } local COLORS = { Alliance = { 0.35, 0.62, 1.00 }, Horde = { 1.00, 0.32, 0.24 }, Neutral = { 1.00, 0.82, 0.25 }, } -- dash and gap as a share of the canvas width; width in screen pixels local STYLE = { boat = { dash = 0.010, gap = 0.006, width = 3 }, air = { dash = 0.004, gap = 0.006, width = 2.5 }, jump = { dash = 0.003, gap = 0.010 }, } -- the zone or continent map each instance is drawn from on the world map local HOME_MAP = { [0] = 1415, [1] = 1414, [2991] = 2521 } local NEARBY = 800 -- yards: other departures this close show in a dock's tooltip local db local function Color(route) return COLORS[route.faction or "Neutral"] end local function Visible(route) if not db or not db.mine then return true end local f = route.faction return f == nil or f == "Neutral" or f == UnitFactionGroup("player") end ns.Visible = Visible --------------------------------------------------------------------------- -- Projection: world position -> normalized position on a uiMap --------------------------------------------------------------------------- local function DirectProjector(uiMapID) local inst, topLeft = C_Map.GetWorldPosFromMapPos(uiMapID, CreateVector2D(0, 0)) local _, mid = C_Map.GetWorldPosFromMapPos(uiMapID, CreateVector2D(0.5, 0.5)) if not inst or not topLeft or not mid then return nil end local top, left = topLeft:GetXY() local midTop, midLeft = mid:GetXY() local height, width = (top - midTop) * 2, (left - midLeft) * 2 if height == 0 or width == 0 then return nil end return function(m, x, y) if m ~= inst then return nil end return (left - y) / width, (top - x) / height end end local projectors = {} local function GetProjector(uiMapID) local cached = projectors[uiMapID] if cached ~= nil then return cached or nil end local proj local info = C_Map.GetMapInfo(uiMapID) if info and info.mapType and info.mapType <= Enum.UIMapType.World then -- world maps have no world position of their own: place each home map's rect on it local rects = {} for inst, home in pairs(HOME_MAP) do local minX, maxX, minY, maxY = C_Map.GetMapRectOnMap(home, uiMapID) local direct = DirectProjector(home) if minX and direct and maxX ~= minX then rects[inst] = { direct, minX, maxX, minY, maxY } end end proj = function(m, x, y) local r = rects[m] if not r then return nil end local u, v = r[1](m, x, y) if not u then return nil end return r[2] + u * (r[3] - r[2]), r[4] + v * (r[5] - r[4]) end else proj = DirectProjector(uiMapID) end projectors[uiMapID] = proj or false return proj end ns.GetProjector = GetProjector local function Inside(u, v) return u and u >= 0 and u <= 1 and v >= 0 and v <= 1 end --------------------------------------------------------------------------- -- Text --------------------------------------------------------------------------- -- the stops a ship leaving stop i calls at, in order local function Destinations(route, i) local out, n = {}, #route.stops for k = 1, n - 1 do out[k] = route.stops[(i - 1 + k) % n + 1] end return out end ns.Destinations = Destinations local function RouteText(route, i) local d = Destinations(route, i) local text = route.noun .. " to " .. d[1][1] for k = 2, #d do text = text .. ", then " .. d[k][1] end return text end ns.RouteText = RouteText local function Departures(stop, except) local out = {} for _, route in ipairs(ns.routes) do if route ~= except and Visible(route) then for i, s in ipairs(route.stops) do if s[2] == stop[2] then local dx, dy = s[3] - stop[3], s[4] - stop[4] if dx * dx + dy * dy <= NEARBY * NEARBY then out[#out + 1] = { route = route, index = i } end end end end end return out end local function FillTooltip(tip, pin) local route = pin.route local c = Color(route) if pin.kind == "dock" then local stop = route.stops[pin.index] tip:SetText(stop[1], 1, 1, 1) tip:AddLine(RouteText(route, pin.index), c[1], c[2], c[3]) if route.faction then tip:AddLine(route.faction, 0.6, 0.6, 0.6) end local others = Departures(stop, route) if #others > 0 then tip:AddLine(" ") tip:AddLine("Also from here:", 0.8, 0.8, 0.8) for _, o in ipairs(others) do local oc = Color(o.route) tip:AddLine(RouteText(o.route, o.index), oc[1], oc[2], oc[3]) end end tip:AddLine("Click: show " .. Destinations(route, pin.index)[1][1], 0.5, 0.5, 0.5) else local dest = route.stops[pin.index] tip:SetText(route.noun .. " to " .. dest[1], c[1], c[2], c[3]) tip:AddLine("Click: show " .. dest[1], 0.5, 0.5, 0.5) end end ns.FillTooltip = FillTooltip -- opens the zone map holding a stop, or its continent local function ShowStop(map, stop) local home = HOME_MAP[stop[2]] if not home then return end local target = home local proj = GetProjector(home) local u, v = nil, nil if proj then u, v = proj(stop[2], stop[3], stop[4]) end if u then local info = C_Map.GetMapInfoAtPosition(home, u, v) if info and info.mapID then target = info.mapID end end map:SetMapID(target) end --------------------------------------------------------------------------- -- Data provider: lines on an overlay, pins for docks and map exits --------------------------------------------------------------------------- local provider = CreateFromMixins(MapCanvasDataProviderMixin) ns.provider = provider function provider:OnAdded(map) MapCanvasDataProviderMixin.OnAdded(self, map) self.overlay = CreateFrame("Frame", nil, map:GetCanvas()) self.overlay:SetAllPoints(map:GetCanvas()) self.lines = { BORDER = {}, ARTWORK = {} } self.used = { BORDER = 0, ARTWORK = 0 } self.drawn = {} end function provider:Line(layer) local n = self.used[layer] + 1 self.used[layer] = n local line = self.lines[layer][n] if not line then line = self.overlay:CreateLine(nil, layer) self.lines[layer][n] = line end line:Show() return line end function provider:ClearLines() for layer, list in pairs(self.lines) do for i = 1, #list do list[i]:Hide() end self.used[layer] = 0 end end function provider:Segment(x1, y1, x2, y2, c, alpha, px) local scale = self:GetMap():GetCanvasScale() for _, spec in ipairs({ { "BORDER", px + 2, 0, 0, 0, alpha * 0.6 }, { "ARTWORK", px, c[1], c[2], c[3], alpha } }) do local line = self:Line(spec[1]) line.px = spec[2] line:SetThickness(spec[2] / scale) line:SetColorTexture(spec[3], spec[4], spec[5], spec[6]) line:SetStartPoint("TOPLEFT", self.overlay, x1, -y1) line:SetEndPoint("TOPLEFT", self.overlay, x2, -y2) end end -- pts: flat canvas-unit list {x1, y1, x2, y2, ...} function provider:Dashed(pts, dash, gap, W, H, c, alpha, px) local on, left = true, dash for i = 1, #pts - 3, 2 do local ax, ay, bx, by = pts[i], pts[i + 1], pts[i + 2], pts[i + 3] local dx, dy = bx - ax, by - ay local len = math.sqrt(dx * dx + dy * dy) local offscreen = (ax < 0 and bx < 0) or (ax > W and bx > W) or (ay < 0 and by < 0) or (ay > H and by > H) local t = 0 while t < len do local step = math.min(left, len - t) if on and not offscreen then local s, e = t / len, (t + step) / len self:Segment(ax + dx * s, ay + dy * s, ax + dx * e, ay + dy * e, c, alpha, px) end t = t + step left = left - step if left <= 0 then on = not on left = on and dash or gap end end end end function provider:DrawLines() self:ClearLines() local canvas = self:GetMap():GetCanvas() local W, H = canvas:GetWidth(), canvas:GetHeight() if not W or W == 0 then return end for _, item in ipairs(self.drawn) do local route = item.route local style = STYLE[route.kind] local c = Color(route) local hl = self.highlight local alpha = (hl and hl ~= route) and 0.25 or 0.95 local px = style.width + (hl == route and 1.5 or 0) local parts = item.parts for k, part in ipairs(parts) do if part then local pts = {} for j = 1, #part, 2 do pts[j], pts[j + 1] = part[j] * W, part[j + 1] * H end if route.closed then pts[#pts + 1], pts[#pts + 2] = pts[1], pts[2] end self:Dashed(pts, style.dash * W, style.gap * W, W, H, c, alpha, px) end -- the jump to the next part (teleport or other continent), when both ends show here local nextPart = parts[k % #parts + 1] if not route.closed and part and nextPart then local jump = { part[#part - 1] * W, part[#part] * H, nextPart[1] * W, nextPart[2] * H } self:Dashed(jump, STYLE.jump.dash * W, STYLE.jump.gap * W, W, H, c, alpha * 0.8, px * 0.8) end end end end function provider:SetHighlight(route) if self.highlight ~= route then self.highlight = route self:DrawLines() end end function provider:RemoveAllData() self:GetMap():RemoveAllPinsByTemplate(TEMPLATE) self:ClearLines() self.drawn = {} self.highlight = nil end function provider:RefreshAllData() self:RemoveAllData() if not db or db.hidden then return end local map = self:GetMap() local mapID = map:GetMapID() local proj = mapID and GetProjector(mapID) if not proj then return end self.overlay:SetFrameLevel(map:GetPinFrameLevelsManager():GetValidFrameLevel("PIN_FRAME_LEVEL_MAP_HIGHLIGHT")) local canvas = map:GetCanvas() local W, H = canvas:GetWidth(), canvas:GetHeight() for _, route in ipairs(ns.routes) do if Visible(route) then local item = { route = route, parts = {} } local any = false for k, part in ipairs(route.parts) do local out = false if proj(part.m, part[1], part[2]) then out = {} for j = 1, #part, 2 do out[j], out[j + 1] = proj(part.m, part[j], part[j + 1]) end any = true end item.parts[k] = out end if any then self.drawn[#self.drawn + 1] = item end for i, stop in ipairs(route.stops) do local u, v = proj(stop[2], stop[3], stop[4]) if Inside(u, v) then map:AcquirePin(TEMPLATE, "dock", route, i):SetPosition(u, v) end end -- an arrow where the route leaves this map for somewhere it doesn't show if not route.closed then local parts = item.parts for k, part in ipairs(parts) do local n = part and #part or 0 if n >= 4 and not parts[k % #parts + 1] and route.parts[k].next then local u, v = part[n - 1], part[n] if Inside(u, v) then local dx, dy = (u - part[n - 3]) * W, (v - part[n - 2]) * H local angle = math.atan2(-dy, dx) map:AcquirePin(TEMPLATE, "exit", route, route.parts[k].next, angle):SetPosition(u, v) end end end end end end self:DrawLines() end function provider:OnCanvasScaleChanged() local scale = self:GetMap():GetCanvasScale() for layer, list in pairs(self.lines) do for i = 1, self.used[layer] do list[i]:SetThickness(list[i].px / scale) end end end function provider:OnCanvasSizeChanged() self:RefreshAllData() end --------------------------------------------------------------------------- -- Pins --------------------------------------------------------------------------- ShipRoutesPinMixin = CreateFromMixins(MapCanvasPinMixin) function ShipRoutesPinMixin:OnLoad() self:UseFrameLevelType("PIN_FRAME_LEVEL_AREA_POI") self:SetScalingLimits(1, 1.0, 1.25) end function ShipRoutesPinMixin:OnAcquired(kind, route, index, angle) self.kind, self.route, self.index = kind, route, index local c = Color(route) local exit = kind == "exit" self:SetSize(exit and 15 or 20, exit and 15 or 20) self.Disc:SetVertexColor(0.06, 0.06, 0.08, 0.9) self.Ring:SetVertexColor(c[1], c[2], c[3], 1) self.Glyph:SetTexture(exit and GLYPH.exit or GLYPH[route.kind]) self.Glyph:SetRotation(exit and angle or 0) if exit then self.Glyph:SetVertexColor(c[1], c[2], c[3], 1) else self.Glyph:SetVertexColor(1, 1, 1, 1) end self:CheckMouseButtonPassthrough("RightButton") end function ShipRoutesPinMixin:OnMouseEnter() provider:SetHighlight(self.route) GameTooltip:SetOwner(self, "ANCHOR_RIGHT") FillTooltip(GameTooltip, self) GameTooltip:Show() end function ShipRoutesPinMixin:OnMouseLeave() provider:SetHighlight(nil) GameTooltip:Hide() end function ShipRoutesPinMixin:OnMouseClickAction(button) if button ~= "LeftButton" then return end local stop if self.kind == "dock" then stop = Destinations(self.route, self.index)[1] else stop = self.route.stops[self.index] end ShowStop(self:GetMap(), stop) end --------------------------------------------------------------------------- -- Setup and slash command --------------------------------------------------------------------------- local function Refresh() if provider:GetMap() then provider:RefreshAllData() end end local attached = false local function Attach() if not attached and WorldMapFrame and WorldMapFrame.AddDataProvider then attached = true WorldMapFrame:AddDataProvider(provider) end end local frame = CreateFrame("Frame") frame:RegisterEvent("ADDON_LOADED") frame:SetScript("OnEvent", function(_, _, name) if name == addonName then ShipRoutesDB = ShipRoutesDB or {} db = ShipRoutesDB Attach() elseif name == "Blizzard_WorldMap" then Attach() end end) SLASH_SHIPROUTES1 = "/routes" SlashCmdList.SHIPROUTES = function(msg) msg = (msg or ""):lower():match("^%s*(.-)%s*$") if msg == "mine" then db.mine = true db.hidden = false print(PREFIX .. "showing routes your faction can take.") elseif msg == "all" then db.mine = false db.hidden = false print(PREFIX .. "showing all routes.") elseif msg == "" then db.hidden = not db.hidden print(PREFIX .. (db.hidden and "hidden." or "shown.")) else print(PREFIX .. "/routes toggles the map lines, /routes mine shows only your faction's, /routes all shows every route.") end Refresh() end