diff --git a/FlightFinder/FlightFinder.lua b/FlightFinder/FlightFinder.lua index 9f8d110..f000e11 100644 --- a/FlightFinder/FlightFinder.lua +++ b/FlightFinder/FlightFinder.lua @@ -104,49 +104,78 @@ local function Project(uiMapID, id) 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])) +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 ok or not mapID or mapID == 0 or c.maps[mapID] then - break + if not b then + return false + elseif a == b then + return true 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 + b = Parent(b) end - chains[id] = c - if log then - log.chain[id] = c.zone or (c.found and "no zone") or "not found" + 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 c + 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 + if log then + log.zone[id] = cached or "none" + end + end + return cached or nil 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 + local u, v = Project(uiMapID, id) + if not u or mapType == Enum.UIMapType.Continent then + return u, v end - return Project(uiMapID, id) + 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 --------------------------------------------------------------------------- @@ -237,7 +266,8 @@ function provider:RefreshAllData() if Visible(id) and Leader(id) then local u, v = Place(mapID, info.mapType, id) if u then - local zone = Chain(id).zone + local continent = Continent(mapID) + local zone = continent and Zone(id, continent) if zone and not learned[zone] then learned[zone] = true LearnZone(zone) @@ -287,7 +317,8 @@ 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 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) @@ -356,7 +387,7 @@ frame:SetScript("OnEvent", function(_, event, name) FlightFinderCharDB = { version = 3 } -- older versions learned every node as known end FlightFinderCharDB.known = FlightFinderCharDB.known or {} - FlightFinderCharDB.log = { map = {}, chain = {} } + FlightFinderCharDB.log = { map = {}, zone = {} } db, known, log = FlightFinderDB, FlightFinderCharDB.known, FlightFinderCharDB.log Attach() elseif name == "Blizzard_WorldMap" then diff --git a/tests/test_ff.lua b/tests/test_ff.lua index bdc1642..9b7186e 100644 --- a/tests/test_ff.lua +++ b/tests/test_ff.lua @@ -14,14 +14,14 @@ end local RECTS = { [1415] = { inst = 0, top = 7467, bottom = -16000, left = 16000, right = -19200, type = 2, parent = 947 }, [1414] = { inst = 1, top = 12800, bottom = -11733, left = 17067, right = -19733, type = 2, parent = 947 }, - [1437] = { inst = 0, top = -1500, bottom = -4800, left = 200, right = -4800, type = 3, parent = 1415 }, -- Wetlands + [1437] = { inst = 0, top = -1500, bottom = -5600, left = 200, right = -4800, type = 3, parent = 1415 }, -- Wetlands (rect reaches over Thelsamar) [1432] = { inst = 0, top = -4500, bottom = -6300, left = -1700, right = -4500, type = 3, parent = 1415 }, -- Loch Modan - [1426] = { inst = 0, top = -3400, bottom = -6600, left = 1500, right = -2500, type = 3, parent = 1415 }, -- Dun Morogh + [1426] = { inst = 0, top = -4200, bottom = -6600, left = 1500, right = -2500, type = 3, parent = 1415 }, -- Dun Morogh [1455] = { inst = 0, top = -4500, bottom = -5200, left = -700, right = -1500, type = 3, parent = 1426 }, -- Ironforge [1434] = { inst = 0, top = -11000, bottom = -15000, left = 2000, right = -2000, type = 3, parent = 1415 }, -- Stranglethorn } --- which zone the client puts a world point in: first matching rect among these, smallest first -local ZONES = { 1455, 1437, 1432, 1426, 1434 } +-- zones the continent map highlights, in hit-test order +local ZONES = { 1432, 1426, 1437, 1434 } Enum = { UIMapType = { Cosmic = 0, World = 1, Continent = 2, Zone = 3, Dungeon = 4, Micro = 5 } } function CreateVector2D(x, y) @@ -40,14 +40,21 @@ C_Map = { if not r then return nil end return r.inst, CreateVector2D(r.top + (r.bottom - r.top) * v.y, r.left + (r.right - r.left) * v.x) end, + -- like Forever: only ever answers with the continent GetMapPosFromWorldPos = function(inst, pos) + return inst == 0 and 1415 or 1414, CreateVector2D(0, 0) + end, + -- the zone under a continent map point, or the continent itself + GetMapInfoAtPosition = function(cont, u, v) + local c = RECTS[cont] + local x, y = c.top + (c.bottom - c.top) * v, c.left + (c.right - c.left) * u for _, id in ipairs(ZONES) do local r = RECTS[id] - if r.inst == inst and pos.x <= r.top and pos.x >= r.bottom and pos.y <= r.left and pos.y >= r.right then - return id, CreateVector2D(0, 0) + if r.parent == cont and r.inst == c.inst and x <= r.top and x >= r.bottom and y <= r.left and y >= r.right then + return { mapID = id, mapType = r.type, parentMapID = cont } end end - return inst == 0 and 1415 or 1414, CreateVector2D(0, 0) + return { mapID = cont, mapType = c.type, parentMapID = 947 } end, CanSetUserWaypointOnMap = function(id) return RECTS[id].type == 3 end, SetUserWaypoint = function(p) waypoint = p end, @@ -157,8 +164,9 @@ check(not show(1437)[7], "known Menethil not marked") apiKnown[1437] = nil check(not show(1415)[7], "Menethil stays known on the continent map") check(show(1415)[8], "Thelsamar still marked on the continent map") --- city node shows on its zone (Ironforge sits in Dun Morogh) +-- city node shows on its zone and on the city map inside it check(show(1426)[6], "Ironforge marked on Dun Morogh") +check(show(1455)[6], "Ironforge marked on the Ironforge city map") -- taxi map: Thelsamar reachable, Ironforge not yet discovered taxi = { { "Thelsamar, Loch Modan", "REACHABLE" }, { "Ironforge, Dun Morogh", "DISTANT" } }