wow-addons/FlightFinder/FlightFinder.lua
rope 25e3aa4fec Map pins: skip SetPassThroughButtons in combat
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 15:23:28 +00:00

371 lines
12 KiB
Lua

-- Flight Finder: marks the flight masters you haven't discovered yet on zone and continent
-- maps. Flight master positions are in Data.lua (generated from the client's TaxiNodes.db2).
--
-- A flight point counts as discovered once a flight master's taxi map showed it as a
-- destination; that's remembered per character. Blizzard's C_TaxiMap.GetTaxiNodesForMap is
-- no use in Forever: it lists every node in the continent, both factions, as discovered.
local addonName, ns = ...
local PREFIX = "|cff66bbffFlight Finder|r: "
local TEMPLATE = "FlightFinderPinTemplate"
local GLYPH = "Interface\\Minimap\\Tracking\\FlightMaster"
local COLORS = {
Alliance = { 0.35, 0.62, 1.00 },
Horde = { 1.00, 0.32, 0.24 },
Neutral = { 1.00, 0.82, 0.25 },
}
local SAME_PLACE = 400 -- yards: flight masters with one name this close count as one place
local db, known
local function Visible(id)
local f = ns.nodes[id][4]
return f == "Neutral" or f == UnitFactionGroup("player")
end
---------------------------------------------------------------------------
-- Groups: one marker per place, discovered if any of its flight masters is
---------------------------------------------------------------------------
local groups, byName = {}, {}
for id, n in pairs(ns.nodes) do
byName[n[5]] = byName[n[5]] or {}
table.insert(byName[n[5]], id)
end
for _, ids in pairs(byName) do
table.sort(ids)
for _, a in ipairs(ids) do
local g = {}
for _, b in ipairs(ids) do
local na, nb = ns.nodes[a], ns.nodes[b]
if na[1] == nb[1] and math.sqrt((na[2] - nb[2]) ^ 2 + (na[3] - nb[3]) ^ 2) <= SAME_PLACE then
g[#g + 1] = b
end
end
groups[a] = g
end
end
ns.groups, ns.byName = groups, byName
local function Discovered(id)
for _, other in ipairs(groups[id]) do
if known[other] and Visible(other) then
return true
end
end
return false
end
-- the marker for a place goes on its lowest visible node
local function Leader(id)
for _, other in ipairs(groups[id]) do
if Visible(other) then
return other == id
end
end
return false
end
---------------------------------------------------------------------------
-- Projection: world position -> position on a uiMap, and which maps a node sits in
---------------------------------------------------------------------------
local rects = {}
local function Rect(uiMapID)
local cached = rects[uiMapID]
if cached ~= nil then
return cached or nil
end
local r = false
local inst, topLeft = C_Map.GetWorldPosFromMapPos(uiMapID, CreateVector2D(0, 0))
local _, mid = C_Map.GetWorldPosFromMapPos(uiMapID, CreateVector2D(0.5, 0.5))
if inst and topLeft and mid then
local top, left = topLeft:GetXY()
local midTop, midLeft = mid:GetXY()
local height, width = (top - midTop) * 2, (left - midLeft) * 2
if height ~= 0 and width ~= 0 then
r = { inst = inst, top = top, left = left, height = height, width = width }
end
end
rects[uiMapID] = r
return r or nil
end
local function Project(uiMapID, id)
local r, n = Rect(uiMapID), ns.nodes[id]
if not r or r.inst ~= n[1] then
return nil
end
local u, v = (r.left - n[3]) / r.width, (r.top - n[2]) / r.height
if u < 0 or u > 1 or v < 0 or v > 1 then
return nil
end
return u, v
end
ns.Project = Project
local function Parent(uiMapID)
local info = C_Map.GetMapInfo(uiMapID)
return info and info.parentMapID ~= 0 and info.parentMapID or nil
end
-- a is b or one of the maps above it
local function Above(a, b)
for _ = 1, 10 do
if not b then
return false
elseif a == b then
return true
end
b = Parent(b)
end
return false
end
local continents = {}
local function Continent(uiMapID)
local cached = continents[uiMapID]
if cached == nil then
cached = false
local m = uiMapID
for _ = 1, 10 do
local info = m and C_Map.GetMapInfo(m)
if not info then
break
elseif info.mapType == Enum.UIMapType.Continent then
cached = m
break
end
m = info.parentMapID
end
continents[uiMapID] = cached
end
return cached or nil
end
-- The zone a node is in: the one the continent map highlights at its spot. (In Forever,
-- C_Map.GetMapPosFromWorldPos only ever answers with the continent.)
local zones = {}
local function Zone(id, continent)
local key = continent * 100000 + id
local cached = zones[key]
if cached == nil then
cached = false
local u, v = Project(continent, id)
local info = u and C_Map.GetMapInfoAtPosition(continent, u, v)
if info and info.mapID and info.mapID ~= continent then
cached = info.mapID
end
zones[key] = cached
end
return cached or nil
end
-- whether node id belongs on uiMapID, and where
local function Place(uiMapID, mapType, id)
local u, v = Project(uiMapID, id)
if not u or mapType == Enum.UIMapType.Continent then
return u, v
end
local continent = Continent(uiMapID)
local zone = continent and Zone(id, continent)
if zone and not (Above(zone, uiMapID) or Above(uiMapID, zone)) then
return nil -- inside this map's rectangle, but in a neighbouring zone
end
return u, v
end
---------------------------------------------------------------------------
-- Learning which flight points the character has
---------------------------------------------------------------------------
-- a flight master's taxi map lists the faction's nodes on the continent. Forever marks the
-- ones not discovered yet as DISTANT (Blizzard's frame hides those), so only CURRENT and
-- REACHABLE count as known.
local HAVE = { CURRENT = true, REACHABLE = true }
local function LearnTaxiMap()
if not (NumTaxiNodes and TaxiNodeName and TaxiNodeGetType) then
return
end
for i = 1, NumTaxiNodes() do
if HAVE[TaxiNodeGetType(i)] then
for _, id in ipairs(byName[TaxiNodeName(i)] or {}) do
if Visible(id) then
known[id] = true
end
end
end
end
end
ns.LearnTaxiMap = LearnTaxiMap
---------------------------------------------------------------------------
-- Map data provider
---------------------------------------------------------------------------
local SHOWN_ON = {
[Enum.UIMapType.Continent] = true,
[Enum.UIMapType.Zone] = true,
[Enum.UIMapType.Micro or 5] = true,
}
local provider = CreateFromMixins(MapCanvasDataProviderMixin)
ns.provider = provider
function provider:RemoveAllData()
self:GetMap():RemoveAllPinsByTemplate(TEMPLATE)
end
function provider:RefreshAllData()
self:RemoveAllData()
if db.hidden then
return
end
local map = self:GetMap()
local mapID = map:GetMapID()
local info = mapID and C_Map.GetMapInfo(mapID)
if not info or not SHOWN_ON[info.mapType] then
return
end
for id in pairs(ns.nodes) do
if Visible(id) and Leader(id) and not Discovered(id) then
local u, v = Place(mapID, info.mapType, id)
if u then
map:AcquirePin(TEMPLATE, id):SetPosition(u, v)
end
end
end
end
function provider:OnShow()
self:RegisterEvent("TAXI_NODE_STATUS_CHANGED")
end
function provider:OnHide()
self:UnregisterEvent("TAXI_NODE_STATUS_CHANGED")
end
function provider:OnEvent()
self:RefreshAllData()
end
---------------------------------------------------------------------------
-- Pins
---------------------------------------------------------------------------
FlightFinderPinMixin = CreateFromMixins(MapCanvasPinMixin)
function FlightFinderPinMixin:OnLoad()
self:UseFrameLevelType("PIN_FRAME_LEVEL_AREA_POI")
self:SetScalingLimits(1, 1.0, 1.25)
end
function FlightFinderPinMixin:OnAcquired(id)
self.nodeID = id
local c = COLORS[ns.nodes[id][4]]
self.Disc:SetVertexColor(0.06, 0.06, 0.08, 0.9)
self.Ring:SetVertexColor(c[1], c[2], c[3], 1)
self.Glyph:SetTexture(GLYPH)
-- SetPassThroughButtons is protected in combat; a pin reused from the pool keeps its old setting
if not InCombatLockdown() then
self:CheckMouseButtonPassthrough("RightButton")
end
end
-- where a waypoint for the node goes: its zone map, since continents don't take waypoints
local function WaypointPoint(id, mapID)
if not (C_Map.CanSetUserWaypointOnMap and C_Map.SetUserWaypoint and UiMapPoint) then
return nil
end
local continent = Continent(mapID)
for _, m in ipairs({ continent and Zone(id, continent) or mapID, mapID }) do
local u, v = Project(m, id)
if u and C_Map.CanSetUserWaypointOnMap(m) then
return UiMapPoint.CreateFromCoordinates(m, u, v)
end
end
end
function FlightFinderPinMixin:OnMouseEnter()
local n = ns.nodes[self.nodeID]
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
GameTooltip:AddLine(n[5], 1, 1, 1)
local c = COLORS[n[4]]
GameTooltip:AddLine(n[4] == "Neutral" and "Undiscovered flight point" or ("Undiscovered " .. n[4] .. " flight point"), c[1], c[2], c[3])
if WaypointPoint(self.nodeID, self:GetMap():GetMapID()) then
GameTooltip:AddLine("Click to set a waypoint", 0.6, 0.6, 0.6)
end
GameTooltip:Show()
end
function FlightFinderPinMixin:OnMouseLeave()
GameTooltip:Hide()
end
function FlightFinderPinMixin:OnMouseClickAction(button)
if button ~= "LeftButton" then
return
end
local point = WaypointPoint(self.nodeID, self:GetMap():GetMapID())
if point then
C_Map.SetUserWaypoint(point)
if C_SuperTrack and C_SuperTrack.SetSuperTrackedUserWaypoint then
C_SuperTrack.SetSuperTrackedUserWaypoint(true)
end
end
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:RegisterEvent("TAXIMAP_OPENED")
frame:SetScript("OnEvent", function(_, event, name)
if event == "TAXIMAP_OPENED" then
LearnTaxiMap()
Refresh()
elseif name == addonName then
FlightFinderDB = FlightFinderDB or {}
FlightFinderCharDB = FlightFinderCharDB or {}
if FlightFinderCharDB.version ~= 4 then
FlightFinderCharDB = { version = 4 } -- older versions learned nodes the character lacks
end
FlightFinderCharDB.known = FlightFinderCharDB.known or {}
db, known = FlightFinderDB, FlightFinderCharDB.known
Attach()
elseif name == "Blizzard_WorldMap" then
Attach()
end
end)
SLASH_FLIGHTFINDER1 = "/ff"
SlashCmdList.FLIGHTFINDER = function()
db.hidden = not db.hidden
local total, have = 0, 0
for id in pairs(ns.nodes) do
if Visible(id) and Leader(id) then
total = total + 1
if Discovered(id) then
have = have + 1
end
end
end
print(PREFIX .. (db.hidden and "hidden. " or "shown. ") .. string.format("%d / %d flight points known.", have, total))
Refresh()
end