Quantcast

Add a way to lookup waypoints by map id

James Whitehead II [12-02-10 - 13:18]
Add a way to lookup waypoints by map id
Filename
TomTomLite.lua
diff --git a/TomTomLite.lua b/TomTomLite.lua
index ab27226..1b51a4c 100644
--- a/TomTomLite.lua
+++ b/TomTomLite.lua
@@ -8,7 +8,48 @@ local L = addon.L
 addon.callbacks = LibStub("CallbackHandler-1.0"):New(addon)
 addon.mapdata = LibStub("LibMapData-1.0")
 addon.libwindow = LibStub("LibWindow-1.1")
-addon.waypoints = {}
+
+-- Set up some tables to track waypoints
+do
+    local cache = {}
+    addon.waypoints = setmetatable({}, {
+        __newindex = function(t, k, v)
+            if type(v) == "nil" then
+                -- A waypoint is being removed
+                local oldwaypoint = rawget(t, k)
+                rawset(t, k, nil)
+                local map = oldwaypoint[1]
+                if map then
+                    cache[map] = nil
+                end
+            else
+                -- A waypoint is being added
+                rawset(t, k, v)
+                local map = v[1]
+                if map then
+                    cache[map] = nil
+                end
+            end
+        end,
+    })
+    addon.waypointsByMap = setmetatable({}, {
+        __index = function(t, k)
+            local cached = cache[k]
+            if cached then
+                return cached
+            end
+            -- Look up all the waypoints for a given mapId
+            local set = {}
+            for idx, waypoint in ipairs(addon.waypoints) do
+                if waypoint[1] == k then
+                    table.insert(set, waypoint)
+                end
+            end
+            rawset(cache, k, set)
+            return set
+        end
+    })
+end

 function addon:Initialize()
     self.db = LibStub("AceDB-3.0"):New("TomTomLiteDB", self.defaults)