Quantcast

Initial quest POI integration

James Whitehead II [11-24-10 - 21:42]
Initial quest POI integration
Filename
TomTomLite.lua
diff --git a/TomTomLite.lua b/TomTomLite.lua
index 3f858dc..52f2edf 100644
--- a/TomTomLite.lua
+++ b/TomTomLite.lua
@@ -16,6 +16,8 @@ function addon:Initialize()
     self.arrow:SetPoint("CENTER", 0, 0)
     self.arrow:Hide()

+    self:RegisterEvent("QUEST_POI_UPDATE", "ObjectivesChanged")
+    self:RegisterEvent("QUEST_LOG_UPDATE", "ObjectivesChanged")
     self:RegisterMessage("TOMTOMLITE_WAYPOINT_ADDED")
 end

@@ -38,12 +40,24 @@ function addon:CreateCrazyArrow(name, parent)

     frame:Hide()

+    local PI2 = math.pi * 2
+
     -- Set up the OnUpdate handler
     frame:SetScript("OnUpdate", function(self, elapsed)
+        -- Get the current location
+        local cmap = GetCurrentMapAreaID()
+        local cx, cy = GetPlayerMapPosition("player")
         local map, floor, x, y = unpack(self.waypoint)
-        local distance, angle = addon.mapdata:DistanceAndDirection(map, 0, x, y)
-        local facing = GetPlayerFacing()
+        local distance, xd, yd = addon.mapdata:DistanceWithinContinent(cmap, 0, cx, cy, map, floor, x, y)
+
+        local angle = math.atan2(xd, yd)
+        if angle > 0 then
+            angle = PI2 - angle
+        else
+            angle = -angle
+        end

+        local facing = GetPlayerFacing()
         local faceangle = angle - facing

         local perc = math.abs((math.pi - math.abs(faceangle)) / math.pi)
@@ -97,3 +111,57 @@ function addon:TOMTOMLITE_WAYPOINT_ADDED(msg, waypoint, ...)
     self.arrow.info:SetFormattedText("%.2f, %.2f - %s", x * 100, y * 100, lzone)
     self.arrow:Show()
 end
+
+function addon:ObjectivesChanged(event, ...)
+    self:Scan()
+end
+
+local function POIAnchorToCoord(poiframe)
+    local point, relto, relpoint, x, y = poiframe:GetPoint()
+    local frame = WorldMapDetailFrame
+    local width = frame:GetWidth()
+    local height = frame:GetHeight()
+    local scale = frame:GetScale() / poiframe:GetScale()
+    local cx = (x / scale) / width
+    local cy = (-y / scale) / height
+
+    if cx < 0 or cx > 1 or cy < 0 or cy > 1 then
+        return nil, nil
+    end
+
+    local map, floor = GetCurrentMapAreaID()
+    return map, floor or 0, cx, cy
+end
+
+local function findQuestFrameFromQuestIndex(index)
+    -- Try to find the correct quest frame
+    for i = 1, MAX_NUM_QUESTS do
+        local questFrame = _G["WorldMapQuestFrame" .. i];
+        if not questFrame then
+            break
+        elseif questFrame.questLogIndex == index then
+            return questFrame
+        end
+    end
+end
+
+function addon:Scan()
+    -- Get the first quest item in the watch frame
+    local questIndex = GetQuestIndexForWatch(1)
+    if not questIndex then
+        self.arrow:Hide()
+        return
+    end
+
+    local title = GetQuestLogTitle(questIndex)
+
+    ShowUIPanel(WorldMapFrame)
+    HideUIPanel(WorldMapFrame)
+
+    local frame = findQuestFrameFromQuestIndex(questIndex)
+    if frame then
+        local map, floor, x, y = POIAnchorToCoord(frame.poiIcon)
+        self:AddWaypoint(map, floor, x, y, {title = title})
+    end
+end
+