Quantcast

Add a source for archaeology dig sites

James Whitehead II [01-10-11 - 23:19]
Add a source for archaeology dig sites
Filename
sources/ArchaeologyDigSites.lua
diff --git a/sources/ArchaeologyDigSites.lua b/sources/ArchaeologyDigSites.lua
index e69de29..2c7d90a 100644
--- a/sources/ArchaeologyDigSites.lua
+++ b/sources/ArchaeologyDigSites.lua
@@ -0,0 +1,99 @@
+local addonName, addon = ...
+local L = addon.L
+
+--[[-------------------------------------------------------------------
+--  Archaeology Dig Sitemodule for TomTomLite, example reference module
+-------------------------------------------------------------------]]--
+
+do
+    local desc = L["This source provides waypoints for archaeology dig sites"]
+    addon:RegisterSource("archaeodig", L["Archaeology Dig Sites"], desc)
+end
+
+local eventFrame = CreateFrame("Frame")
+eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
+eventFrame:RegisterEvent("ARTIFACT_HISTORY_READY")
+eventFrame:RegisterEvent("ARTIFACT_COMPLETE")
+eventFrame:RegisterEvent("ARTIFACT_DIG_SITE_UPDATED")
+
+-- Creates waypoints for each of the dig sites that can be found
+-- on the current continent
+
+local waypoints = {}
+local function UpdateDigSites()
+    if not addon.db.profile.archaeologyDigSitesSource then
+        return
+    end
+
+    local omap, ofloor = GetCurrentMapAreaID()
+
+    -- Flip the map to our current zone, then zoom out to the continent
+    SetMapToCurrentZone()
+    local map, floor = GetCurrentMapAreaID()
+
+    local continent = addon:GetMapContinentMap(map)
+    if continent then
+        SetMapByID(continent)
+    end
+
+    local sites = {}
+    for idx = 1, GetNumMapLandmarks() do
+        local name, desc, textureIdx, px, py = GetMapLandmarkInfo(idx)
+        if textureIdx == 177 then
+            local key = string.format("%s:%f:%f:", name, px, py)
+            if not waypoints[key] then
+                local waypoint = addon:AddWaypoint(continent, nil, px, py, {
+                    title = string.format(L["Dig site: %s"], name),
+                    priority = 0,
+                })
+                sites[key] = waypoint
+            else
+                sites[key] = waypoints[key]
+            end
+        end
+    end
+
+    for key, waypoint in pairs(sites) do
+        waypoints[key] = nil
+    end
+
+    for key, waypoint in pairs(waypoints) do
+        addon:RemoveWaypoint(waypoint)
+    end
+
+    waypoints = sites
+end
+
+eventFrame:SetScript("OnEvent", function(self, event, ...)
+    UpdateDigSites()
+end)
+
+local throttle = 2.0
+eventFrame:SetScript("OnUpdate", function(self, elapsed)
+    local min = math.huge
+    local closest
+    for key, waypoint in pairs(waypoints) do
+        local distance, angle = addon:GetVectorFromCurrent(unpack(waypoint))
+        if distance and distance < min then
+            min = distance
+            closest = waypoint
+        end
+    end
+
+    if self.closest == closest then
+        -- Nothing changed, return
+        return
+    end
+
+    for key, waypoint in pairs(waypoints) do
+        if waypoint ~= closest then
+            waypoint.priority = 0
+        else
+            waypoint.priority = 5
+        end
+    end
+
+    self.closest = closest
+
+    addon:FireMessage("TOMTOMLITE_WAYPOINTS_CHANGED")
+end)