comparison 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
comparison
equal deleted inserted replaced
213:52d1e3f75eaf 214:9b0333c7513e
323 global_db[data_type][unit_id] = unit 323 global_db[data_type][unit_id] = unit
324 end 324 end
325 return unit 325 return unit
326 end 326 end
327 327
328 328 local NPCEntry
329 local function NPCEntry(identifier) 329 do
330 local npc = DBEntry("npcs", identifier) 330 local npc_prototype = {}
331 331 local npc_meta = {
332 if not npc then 332 __index = npc_prototype
333 return 333 }
334 end 334
335 local instance_token = InstanceDifficultyToken() 335 function NPCEntry(identifier)
336 npc.encounter_data = npc.encounter_data or {} 336 local npc = _G.setmetatable(DBEntry("npcs", identifier), npc_meta)
337 npc.encounter_data[instance_token] = npc.encounter_data[instance_token] or {} 337
338 npc.encounter_data[instance_token].stats = npc.encounter_data[instance_token].stats or {} 338 if not npc then
339 return npc 339 return
340 end
341 return npc
342 end
343
344 function npc_prototype:EncounterData()
345 local instance_token = InstanceDifficultyToken()
346 self.encounter_data = self.encounter_data or {}
347 self.encounter_data[instance_token] = self.encounter_data[instance_token] or {}
348 self.encounter_data[instance_token].stats = self.encounter_data[instance_token].stats or {}
349
350 return self.encounter_data
351 end
352
340 end 353 end
341 354
342 355
343 local function CurrentLocationData() 356 local function CurrentLocationData()
344 if _G.GetCurrentMapAreaID() ~= current_area_id then 357 if _G.GetCurrentMapAreaID() ~= current_area_id then
908 npc.genders = npc.genders or {} 921 npc.genders = npc.genders or {}
909 npc.genders[GENDER_NAMES[_G.UnitSex("target")] or "UNDEFINED"] = true 922 npc.genders[GENDER_NAMES[_G.UnitSex("target")] or "UNDEFINED"] = true
910 npc.is_pvp = _G.UnitIsPVP("target") and true or nil 923 npc.is_pvp = _G.UnitIsPVP("target") and true or nil
911 npc.reaction = ("%s:%s:%s"):format(_G.UnitLevel("player"), _G.UnitFactionGroup("player"), REACTION_NAMES[_G.UnitReaction("player", "target")]) 924 npc.reaction = ("%s:%s:%s"):format(_G.UnitLevel("player"), _G.UnitFactionGroup("player"), REACTION_NAMES[_G.UnitReaction("player", "target")])
912 925
913 local encounter_data = npc.encounter_data[InstanceDifficultyToken()].stats 926 local encounter_data = npc:EncounterData()[InstanceDifficultyToken()].stats
914 local npc_level = ("level_%d"):format(_G.UnitLevel("target")) 927 local npc_level = ("level_%d"):format(_G.UnitLevel("target"))
915 928
916 if not encounter_data[npc_level] then 929 if not encounter_data[npc_level] then
917 encounter_data[npc_level] = { 930 encounter_data[npc_level] = {
918 max_health = _G.UnitHealthMax("target"), 931 max_health = _G.UnitHealthMax("target"),
948 961
949 if not npc then 962 if not npc then
950 return 963 return
951 end 964 end
952 local zone_name, area_id, x, y, map_level, difficulty_token = CurrentLocationData() 965 local zone_name, area_id, x, y, map_level, difficulty_token = CurrentLocationData()
953 local npc_data = npc.encounter_data[difficulty_token].stats[("level_%d"):format(_G.UnitLevel("target"))] 966 local npc_data = npc:EncounterData()[difficulty_token].stats[("level_%d"):format(_G.UnitLevel("target"))]
954 local zone_token = ("%s:%d"):format(zone_name, area_id) 967 local zone_token = ("%s:%d"):format(zone_name, area_id)
955 npc_data.locations = npc_data.locations or {} -- TODO: Fix this. It is broken. Possibly something to do with the timed updates. 968 npc_data.locations = npc_data.locations or {} -- TODO: Fix this. It is broken. Possibly something to do with the timed updates.
956 969
957 local zone_data = npc_data.locations[zone_token] 970 local zone_data = npc_data.locations[zone_token]
958 971
1007 if not item_id then 1020 if not item_id then
1008 Debug(("%s: ItemID is nil, from item link %s"):format(event_name, item_link)) 1021 Debug(("%s: ItemID is nil, from item link %s"):format(event_name, item_link))
1009 return 1022 return
1010 end 1023 end
1011 local loot_type = "drops" 1024 local loot_type = "drops"
1012 local encounter_data = npc.encounter_data[InstanceDifficultyToken()] 1025 local encounter_data = npc:EncounterData()[InstanceDifficultyToken()]
1013 encounter_data[loot_type] = encounter_data[loot_type] or {} 1026 encounter_data[loot_type] = encounter_data[loot_type] or {}
1014 encounter_data.loot_counts = encounter_data.loot_counts or {} 1027 encounter_data.loot_counts = encounter_data.loot_counts or {}
1015 encounter_data.loot_counts[loot_type] = (encounter_data.loot_counts[loot_type] or 0) + 1 1028 encounter_data.loot_counts[loot_type] = (encounter_data.loot_counts[loot_type] or 0) + 1
1016 1029
1017 table.insert(encounter_data[loot_type], ("%d:%d"):format(item_id, quantity)) 1030 table.insert(encounter_data[loot_type], ("%d:%d"):format(item_id, quantity))
1167 if not source_id or not UnitTypeIsNPC(source_type) then 1180 if not source_id or not UnitTypeIsNPC(source_type) then
1168 return 1181 return
1169 end 1182 end
1170 1183
1171 if bit.band(FLAGS_NPC_CONTROL, source_flags) == FLAGS_NPC_CONTROL and bit.band(FLAGS_NPC, source_flags) ~= 0 then 1184 if bit.band(FLAGS_NPC_CONTROL, source_flags) == FLAGS_NPC_CONTROL and bit.band(FLAGS_NPC, source_flags) ~= 0 then
1172 local encounter_data = NPCEntry(source_id).encounter_data[InstanceDifficultyToken()] 1185 local encounter_data = NPCEntry(source_id):EncounterData()[InstanceDifficultyToken()]
1173 encounter_data.spells = encounter_data.spells or {} 1186 encounter_data.spells = encounter_data.spells or {}
1174 encounter_data.spells[spell_id] = (encounter_data.spells[spell_id] or 0) + 1 1187 encounter_data.spells[spell_id] = (encounter_data.spells[spell_id] or 0) + 1
1175 end 1188 end
1176 end 1189 end
1177 1190
1444 for source_guid, loot_data in pairs(current_loot.sources) do 1457 for source_guid, loot_data in pairs(current_loot.sources) do
1445 local source_id = select(2, ParseGUID(source_guid)) 1458 local source_id = select(2, ParseGUID(source_guid))
1446 local npc = NPCEntry(source_id) 1459 local npc = NPCEntry(source_id)
1447 1460
1448 if npc then 1461 if npc then
1449 local encounter_data = npc.encounter_data[difficulty_token] 1462 local encounter_data = npc:EncounterData()[difficulty_token]
1450 encounter_data[loot_type] = encounter_data[loot_type] or {} 1463 encounter_data[loot_type] = encounter_data[loot_type] or {}
1451 1464
1452 if not source_list[source_guid] then 1465 if not source_list[source_guid] then
1453 encounter_data.loot_counts = encounter_data.loot_counts or {} 1466 encounter_data.loot_counts = encounter_data.loot_counts or {}
1454 encounter_data.loot_counts[loot_type] = (encounter_data.loot_counts[loot_type] or 0) + 1 1467 encounter_data.loot_counts[loot_type] = (encounter_data.loot_counts[loot_type] or 0) + 1
1468 local npc = NPCEntry(current_loot.identifier) 1481 local npc = NPCEntry(current_loot.identifier)
1469 1482
1470 if not npc then 1483 if not npc then
1471 return 1484 return
1472 end 1485 end
1473 local encounter_data = npc.encounter_data[difficulty_token] 1486 local encounter_data = npc:EncounterData()[difficulty_token]
1474 encounter_data[loot_type] = encounter_data[loot_type] or {} 1487 encounter_data[loot_type] = encounter_data[loot_type] or {}
1475 1488
1476 if not source_list[current_loot.identifier] then 1489 if not source_list[current_loot.identifier] then
1477 encounter_data.loot_counts = encounter_data.loot_counts or {} 1490 encounter_data.loot_counts = encounter_data.loot_counts or {}
1478 encounter_data.loot_counts[loot_type] = (encounter_data.loot_counts[loot_type] or 0) + 1 1491 encounter_data.loot_counts[loot_type] = (encounter_data.loot_counts[loot_type] or 0) + 1