Quantcast

Fix for proximity sort not working

James Whitehead II [10-08-12 - 14:02]
Fix for proximity sort not working

This required removing priority from quest objectives and implementing a
distance sort as secondary key for any waypoints with the same priority.
Filename
DatabaseDefaults.lua
TomTomLite.lua
sources/QuestObjectives.lua
diff --git a/DatabaseDefaults.lua b/DatabaseDefaults.lua
index c8dd3d9..710b815 100644
--- a/DatabaseDefaults.lua
+++ b/DatabaseDefaults.lua
@@ -17,6 +17,8 @@ addon.defaults = {

         corpseArrow = true,

+		forceUpdateThrottle = 2.0,
+
         goodcolor = {0, 1, 0},
         badcolor = {1, 0, 0},
         middlecolor = {1, 1, 0},
diff --git a/TomTomLite.lua b/TomTomLite.lua
index 7a66a06..ae5f574 100644
--- a/TomTomLite.lua
+++ b/TomTomLite.lua
@@ -106,9 +106,17 @@ function addon:CreateCrazyArrow(name, parent)
     frame:Hide()

     local PI2 = math.pi * 2
+	local forceUpdateCounter = 0

     -- Set up the OnUpdate handler
     frame:SetScript("OnUpdate", function(self, elapsed)
+		-- Update the arrow (sort) every updateThrottle seconds
+		forceUpdateCounter = forceUpdateCounter + elapsed
+		if forceUpdateCounter >= addon.db.profile.forceUpdateThrottle then
+			addon:UpdateArrow()
+			forceUpdateCounter = 0
+		end
+
         local map, floor, x, y = unpack(self.waypoint)
         local distance, angle = addon:GetVectorFromCurrent(map, floor, x, y)

@@ -299,16 +307,29 @@ function addon:TOMTOMLITE_WAYPOINTS_CHANGED(msg, ...)
     self:UpdateArrow()
 end

+-- stores the current position
+local current = {}
+local sortPriThenDistance = function(a, b)
+	local m, f, x, y = current.m, current.f, current.x, current.y
+	local apri = a.priority or 0
+	local bpri = b.priority or 0
+
+	if apri == bpri then
+		-- priorities differ, so sort by distance instead
+		local adist = addon:GetVector(m, f, x, y, unpack(a))
+		local bdist = addon:GetVector(m, f, x, y, unpack(b))
+		return adist < bdist
+	end
+
+	return bpri < apri
+end
+
 function addon:UpdateArrow()
-    -- This naive sort function will sort all waypoints so the highest
-    -- priority waypoint is first. This is the waypoint that will be
-    -- displayed on the arrow.
-
-    table.sort(self.waypoints, function(a, b)
-        local apri = a.priority or 0
-        local bpri = b.priority or 0
-        return bpri < apri
-    end)
+	current.m, current.f, current.x, current.y = self:GetPlayerPosition()
+
+	-- Sort by priority, then by distance to waypoint. All sort functions
+	-- return true when a is less than b.
+    table.sort(self.waypoints, sortPriThenDistance)

 	local active = false
 	for idx, waypoint in ipairs(self.waypoints) do
@@ -327,6 +348,10 @@ function addon:UpdateArrow()
 			self.arrow:Show()
 			active = true
 		end
+
+		if active then
+			break
+		end
 	end

 	if not active then
diff --git a/sources/QuestObjectives.lua b/sources/QuestObjectives.lua
index 543a602..6b7034b 100644
--- a/sources/QuestObjectives.lua
+++ b/sources/QuestObjectives.lua
@@ -93,19 +93,22 @@ function addon:OBJECTIVES_CHANGED()
                 -- This waypoint already exists, no need to do anything, except
                 -- possibly change the priority

+				-- NOTE: Priority broken right now, so skip this
                 local waypoint = waypoints[key]
-                local newpri = key == first and addon.PRI_ACTIVE or addon.PRI_NORMAL
-                if waypoint.priority ~= newpri then
-                    changed = true
-                    waypoint.priority = newpri
-                end
+                -- local newpri = key == first and addon.PRI_ACTIVE or addon.PRI_NORMAL
+                -- if waypoint.priority ~= newpri then
+                --     changed = true
+                --     waypoint.priority = newpri
+                -- end

                 newWaypoints[key] = waypoint
             else
                 -- Create the waypoint, setting priority
                 local waypoint = self:AddWaypoint(map, floor, x, y, {
                     title = title,
-                    priority = key == first and addon.PRI_ACTIVE or addon.PRI_NORMAL,
+					priority = addon.PRI_NORMAL,
+					-- Proximity sory appears to be broken right now
+                    -- priority = key == first and addon.PRI_ACTIVE or addon.PRI_NORMAL,
 					source = "questobj",
                 })