diff Main.lua @ 214:9b0333c7513e

Don't create an encounter_data entry for NPCs unless it's required.
author James D. Callahan III <jcallahan@curse.com>
date Mon, 21 Jan 2013 18:01:26 -0600
parents 3c1623ab71db
children 3b04818b4c03
line wrap: on
line diff
--- a/Main.lua	Wed Dec 19 14:57:57 2012 -0600
+++ b/Main.lua	Mon Jan 21 18:01:26 2013 -0600
@@ -325,18 +325,31 @@
     return unit
 end
 
+local NPCEntry
+do
+    local npc_prototype = {}
+    local npc_meta = {
+        __index = npc_prototype
+    }
 
-local function NPCEntry(identifier)
-    local npc = DBEntry("npcs", identifier)
+    function NPCEntry(identifier)
+        local npc = _G.setmetatable(DBEntry("npcs", identifier), npc_meta)
 
-    if not npc then
-        return
+        if not npc then
+            return
+        end
+        return npc
     end
-    local instance_token = InstanceDifficultyToken()
-    npc.encounter_data = npc.encounter_data or {}
-    npc.encounter_data[instance_token] = npc.encounter_data[instance_token] or {}
-    npc.encounter_data[instance_token].stats = npc.encounter_data[instance_token].stats or {}
-    return npc
+
+    function npc_prototype:EncounterData()
+        local instance_token = InstanceDifficultyToken()
+        self.encounter_data = self.encounter_data or {}
+        self.encounter_data[instance_token] = self.encounter_data[instance_token] or {}
+        self.encounter_data[instance_token].stats = self.encounter_data[instance_token].stats or {}
+
+        return self.encounter_data
+    end
+
 end
 
 
@@ -910,7 +923,7 @@
         npc.is_pvp = _G.UnitIsPVP("target") and true or nil
         npc.reaction = ("%s:%s:%s"):format(_G.UnitLevel("player"), _G.UnitFactionGroup("player"), REACTION_NAMES[_G.UnitReaction("player", "target")])
 
-        local encounter_data = npc.encounter_data[InstanceDifficultyToken()].stats
+        local encounter_data = npc:EncounterData()[InstanceDifficultyToken()].stats
         local npc_level = ("level_%d"):format(_G.UnitLevel("target"))
 
         if not encounter_data[npc_level] then
@@ -950,7 +963,7 @@
             return
         end
         local zone_name, area_id, x, y, map_level, difficulty_token = CurrentLocationData()
-        local npc_data = npc.encounter_data[difficulty_token].stats[("level_%d"):format(_G.UnitLevel("target"))]
+        local npc_data = npc:EncounterData()[difficulty_token].stats[("level_%d"):format(_G.UnitLevel("target"))]
         local zone_token = ("%s:%d"):format(zone_name, area_id)
         npc_data.locations = npc_data.locations or {} -- TODO: Fix this. It is broken. Possibly something to do with the timed updates.
 
@@ -1009,7 +1022,7 @@
         return
     end
     local loot_type = "drops"
-    local encounter_data = npc.encounter_data[InstanceDifficultyToken()]
+    local encounter_data = npc:EncounterData()[InstanceDifficultyToken()]
     encounter_data[loot_type] = encounter_data[loot_type] or {}
     encounter_data.loot_counts = encounter_data.loot_counts or {}
     encounter_data.loot_counts[loot_type] = (encounter_data.loot_counts[loot_type] or 0) + 1
@@ -1169,7 +1182,7 @@
         end
 
         if bit.band(FLAGS_NPC_CONTROL, source_flags) == FLAGS_NPC_CONTROL and bit.band(FLAGS_NPC, source_flags) ~= 0 then
-            local encounter_data = NPCEntry(source_id).encounter_data[InstanceDifficultyToken()]
+            local encounter_data = NPCEntry(source_id):EncounterData()[InstanceDifficultyToken()]
             encounter_data.spells = encounter_data.spells or {}
             encounter_data.spells[spell_id] = (encounter_data.spells[spell_id] or 0) + 1
         end
@@ -1446,7 +1459,7 @@
                 local npc = NPCEntry(source_id)
 
                 if npc then
-                    local encounter_data = npc.encounter_data[difficulty_token]
+                    local encounter_data = npc:EncounterData()[difficulty_token]
                     encounter_data[loot_type] = encounter_data[loot_type] or {}
 
                     if not source_list[source_guid] then
@@ -1470,7 +1483,7 @@
             if not npc then
                 return
             end
-            local encounter_data = npc.encounter_data[difficulty_token]
+            local encounter_data = npc:EncounterData()[difficulty_token]
             encounter_data[loot_type] = encounter_data[loot_type] or {}
 
             if not source_list[current_loot.identifier] then