wow-addons/FlightFinder/FlightFinder.lua
rope 5f6cdfe6b2 Flight Finder: DISTANT taxi nodes aren't known
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 15:16:07 +00:00

381 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 when Blizzard's map API lists it as discovered for its
-- zone, or when a flight master's taxi map showed it as a destination. Both are remembered
-- per character, so continent maps and zones you haven't opened yet stay right.
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, log
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
-- the map the client places a node on, plus every map above it
local chains = {}
local function Chain(id)
local c = chains[id]
if c then
return c
end
local n = ns.nodes[id]
c = { maps = {} }
local ok, mapID = pcall(C_Map.GetMapPosFromWorldPos, n[1], CreateVector2D(n[2], n[3]))
for _ = 1, 10 do
if not ok or not mapID or mapID == 0 or c.maps[mapID] then
break
end
local info = C_Map.GetMapInfo(mapID)
if not info then
break
end
c.maps[mapID] = true
c.found = true
if not c.zone and info.mapType == Enum.UIMapType.Zone then
c.zone = mapID
end
mapID = info.parentMapID
end
chains[id] = c
if log then
log.chain[id] = c.zone or (c.found and "no zone") or "not found"
end
return c
end
-- whether node id belongs on uiMapID, and where
local function Place(uiMapID, mapType, id)
local c = Chain(id)
if c.found then
if not c.maps[uiMapID] then
return nil
end
elseif mapType ~= Enum.UIMapType.Continent then
return nil -- client can't say which zone it's in: only trust the continent map
end
return Project(uiMapID, id)
end
---------------------------------------------------------------------------
-- Learning which flight points the character has
---------------------------------------------------------------------------
-- Blizzard's own map list for a zone; learns its discovered nodes
local function LearnZone(uiMapID)
if not (C_TaxiMap and C_TaxiMap.GetTaxiNodesForMap) then
return
end
local ok, list = pcall(C_TaxiMap.GetTaxiNodesForMap, uiMapID)
if ok and type(list) == "table" then
-- diagnostic: what the client returns, saved for inspection
local rows = {}
for _, info in ipairs(list) do
local row = {}
for k, v in pairs(info) do
if k ~= "position" then
row[k] = tostring(v)
end
end
rows[#rows + 1] = row
end
log.map[uiMapID] = rows
for _, info in ipairs(list) do
if info.nodeID and not info.isUndiscovered then
known[info.nodeID] = true
end
end
end
end
-- 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
log.taxi = {}
for i = 1, NumTaxiNodes() do
log.taxi[i] = tostring(TaxiNodeName(i)) .. " = " .. tostring(TaxiNodeGetType(i))
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
local learned = {}
for id in pairs(ns.nodes) do
if Visible(id) and Leader(id) then
local u, v = Place(mapID, info.mapType, id)
if u then
local zone = Chain(id).zone
if zone and not learned[zone] then
learned[zone] = true
LearnZone(zone)
end
if not Discovered(id) then
map:AcquirePin(TEMPLATE, id):SetPosition(u, v)
end
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)
self:CheckMouseButtonPassthrough("RightButton")
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
for _, m in ipairs({ Chain(id).zone 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 ~= 3 then
FlightFinderCharDB = { version = 3 } -- older versions learned every node as known
end
FlightFinderCharDB.known = FlightFinderCharDB.known or {}
FlightFinderCharDB.log = { map = {}, chain = {} }
db, known, log = FlightFinderDB, FlightFinderCharDB.known, FlightFinderCharDB.log
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