comparison Main.lua @ 29:5802628b2bf0

Cleanup for code dealing with NPC entries.
author James D. Callahan III <jcallahan@curse.com>
date Tue, 22 May 2012 12:38:35 -0500
parents 11afd2cc0b83
children bdb3d8a2c3a7
comparison
equal deleted inserted replaced
28:11afd2cc0b83 29:5802628b2bf0
75 75
76 76
77 ----------------------------------------------------------------------- 77 -----------------------------------------------------------------------
78 -- Helper Functions. 78 -- Helper Functions.
79 ----------------------------------------------------------------------- 79 -----------------------------------------------------------------------
80 local function DBEntry(data_type, unit_id)
81 if not data_type or not unit_id then
82 return
83 end
84 local unit = db[data_type][unit_id]
85
86 if not unit then
87 db[data_type][unit_id] = {}
88 unit = db[data_type][unit_id]
89 end
90 return unit
91 end
92
93
94 local function InstanceDifficultyToken() 80 local function InstanceDifficultyToken()
95 local _, instance_type, instance_difficulty, difficulty_name, _, _, is_dynamic = _G.GetInstanceInfo() 81 local _, instance_type, instance_difficulty, difficulty_name, _, _, is_dynamic = _G.GetInstanceInfo()
96 if difficulty_name == "" then 82 if difficulty_name == "" then
97 difficulty_name = "NONE" 83 difficulty_name = "NONE"
98 end 84 end
99 return ("%s:%s:%s"):format(instance_type:upper(), difficulty_name:upper():gsub(" ", "_"), _G.tostring(is_dynamic)) 85 return ("%s:%s:%s"):format(instance_type:upper(), difficulty_name:upper():gsub(" ", "_"), _G.tostring(is_dynamic))
86 end
87
88
89 local function DBEntry(data_type, unit_id)
90 if not data_type or not unit_id then
91 return
92 end
93 local unit = db[data_type][unit_id]
94
95 if not unit then
96 db[data_type][unit_id] = {}
97 unit = db[data_type][unit_id]
98 end
99 return unit
100 end
101
102
103 local function NPCEntry(identifier)
104 local npc = DBEntry("npcs", identifier)
105
106 if not npc then
107 return
108 end
109 local instance_token = InstanceDifficultyToken()
110 npc.encounter_data = npc.encounter_data or {}
111 npc.encounter_data[instance_token] = npc.encounter_data[instance_token] or {}
112 return npc
100 end 113 end
101 114
102 115
103 local function CurrentLocationData() 116 local function CurrentLocationData()
104 local map_level = _G.GetCurrentMapDungeonLevel() or 0 117 local map_level = _G.GetCurrentMapDungeonLevel() or 0
341 354
342 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then 355 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
343 return 356 return
344 end 357 end
345 local zone_name, area_id, x, y, map_level, instance_token = CurrentLocationData() 358 local zone_name, area_id, x, y, map_level, instance_token = CurrentLocationData()
346 local npc_data = DBEntry("npcs", unit_idnum).encounter_data[instance_token][("level_%d"):format(_G.UnitLevel("target"))] 359 local npc_data = NPCEntry(unit_idnum).encounter_data[instance_token][("level_%d"):format(_G.UnitLevel("target"))]
360 local location_token = ("%s:%d"):format(zone_name, area_id)
347 npc_data.locations = npc_data.locations or {} 361 npc_data.locations = npc_data.locations or {}
348 362 npc_data.locations[location_token] = npc_data.locations[location_token] or {}
349 local location_token = ("%s:%d"):format(zone_name, area_id)
350
351 if not npc_data.locations[location_token] then
352 npc_data.locations[location_token] = {}
353 end
354 363
355 -- Only record corpse location if there is no entry for this GUID. 364 -- Only record corpse location if there is no entry for this GUID.
356 if is_dead and npc_data.locations[location_token][target_guid] then 365 if is_dead and npc_data.locations[location_token][target_guid] then
357 return 366 return
358 end 367 end
378 if not source_id or source_type ~= private.UNIT_TYPES.NPC then 387 if not source_id or source_type ~= private.UNIT_TYPES.NPC then
379 return 388 return
380 end 389 end
381 390
382 if bit.band(FLAGS_NPC_CONTROL, source_flags) == FLAGS_NPC_CONTROL and bit.band(FLAGS_NPC, source_flags) ~= 0 then 391 if bit.band(FLAGS_NPC_CONTROL, source_flags) == FLAGS_NPC_CONTROL and bit.band(FLAGS_NPC, source_flags) ~= 0 then
383 local npc = DBEntry("npcs", source_id) 392 local encounter_data = NPCEntry(source_id).encounter_data[InstanceDifficultyToken()]
384 local instance_token = InstanceDifficultyToken()
385 npc.encounter_data = npc.encounter_data or {}
386
387 if not npc.encounter_data[instance_token] then
388 npc.encounter_data[instance_token] = {}
389 end
390 local encounter_data = npc.encounter_data[instance_token]
391
392 encounter_data.spells = encounter_data.spells or {} 393 encounter_data.spells = encounter_data.spells or {}
393 encounter_data.spells[spell_id] = (encounter_data.spells[spell_id] or 0) + 1 394 encounter_data.spells[spell_id] = (encounter_data.spells[spell_id] or 0) + 1
394 end 395 end
395 end 396 end
396 397
415 end 416 end
416 end -- do-block 417 end -- do-block
417 418
418 419
419 function WDP:COMBAT_TEXT_UPDATE(event, message_type, faction_name, amount) 420 function WDP:COMBAT_TEXT_UPDATE(event, message_type, faction_name, amount)
420 local npc = DBEntry("npcs", action_data.identifier) 421 local npc = NPCEntry(action_data.identifier)
421 422
422 if not npc then 423 if not npc then
423 return 424 return
424 end 425 end
425 local instance_token = InstanceDifficultyToken() 426 local encounter_data = npc.encounter_data[InstanceDifficultyToken()]
426 427 encounter_data[action_data.npc_level].reputations = encounter_data[action_data.npc_level].reputations or {}
427 if not npc.encounter_data[instance_token][action_data.npc_level].reputations then 428 encounter_data[action_data.npc_level].reputations[faction_name] = amount
428 npc.encounter_data[instance_token][action_data.npc_level].reputations = {}
429 end
430 npc.encounter_data[instance_token][action_data.npc_level].reputations[faction_name] = amount
431 end 429 end
432 430
433 431
434 do 432 do
435 local re_gold = _G.GOLD_AMOUNT:gsub("%%d", "(%%d+)") 433 local re_gold = _G.GOLD_AMOUNT:gsub("%%d", "(%%d+)")
515 local LOOT_UPDATE_FUNCS = { 513 local LOOT_UPDATE_FUNCS = {
516 [AF.ITEM] = function() 514 [AF.ITEM] = function()
517 GenericLootUpdate("items") 515 GenericLootUpdate("items")
518 end, 516 end,
519 [AF.NPC] = function() 517 [AF.NPC] = function()
520 local npc = DBEntry("npcs", action_data.identifier) 518 local npc = NPCEntry(action_data.identifier)
521 519
522 if not npc then 520 if not npc then
523 return 521 return
524 end 522 end
525 local instance_token = InstanceDifficultyToken() 523 local encounter_data = npc.encounter_data[InstanceDifficultyToken()]
526
527 if not npc.encounter_data[instance_token] then
528 npc.encounter_data[instance_token] = {}
529 end
530 local loot_type = action_data.label or "drops" 524 local loot_type = action_data.label or "drops"
531 npc.encounter_data[instance_token][loot_type] = npc.encounter_data[instance_token][loot_type] or {} 525 encounter_data[loot_type] = encounter_data[loot_type] or {}
532
533 local loot_data = npc.encounter_data[instance_token][loot_type]
534 526
535 for index = 1, #action_data.loot_list do 527 for index = 1, #action_data.loot_list do
536 table.insert(loot_data, action_data.loot_list[index]) 528 table.insert(encounter_data[loot_type], action_data.loot_list[index])
537 end 529 end
538 end, 530 end,
539 [AF.OBJECT] = function() 531 [AF.OBJECT] = function()
540 GenericLootUpdate("objects", InstanceDifficultyToken()) 532 GenericLootUpdate("objects", InstanceDifficultyToken())
541 end, 533 end,
542 [AF.ZONE] = function() 534 [AF.ZONE] = function()
543 local loot_type = action_data.label or "drops" 535 local loot_type = action_data.label or "drops"
544 local zone = DBEntry("zones", action_data.zone) 536 local zone = DBEntry("zones", action_data.zone)
545 zone[action_data.instance_token] = zone[action_data.instance_token] or {} 537 zone[action_data.instance_token] = zone[action_data.instance_token] or {}
546 zone[action_data.instance_token][loot_type] = zone[loot_type][action_data.instance_token] or {} 538 zone[action_data.instance_token][loot_type] = zone[action_data.instance_token][loot_type] or {}
547 539
548 local location_data = ("%s:%s:%s"):format(action_data.map_level, action_data.x, action_data.y) 540 local location_data = ("%s:%s:%s"):format(action_data.map_level, action_data.x, action_data.y)
549 local loot_data = zone[action_data.instance_token][loot_type][location_data] 541 local loot_data = zone[action_data.instance_token][loot_type][location_data]
550 542
551 if not loot_data then 543 if not loot_data then
634 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target")) 626 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target"))
635 627
636 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then 628 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
637 return 629 return
638 end 630 end
639 local merchant = DBEntry("npcs", unit_idnum) 631 local merchant = NPCEntry(unit_idnum)
640 merchant.sells = merchant.sells or {} 632 merchant.sells = merchant.sells or {}
641 633
642 for item_index = 1, _G.GetMerchantNumItems() do 634 for item_index = 1, _G.GetMerchantNumItems() do
643 local _, _, copper_price, stack_size, num_available, _, extended_cost = _G.GetMerchantItemInfo(item_index) 635 local _, _, copper_price, stack_size, num_available, _, extended_cost = _G.GetMerchantItemInfo(item_index)
644 local item_id = ItemLinkToID(_G.GetMerchantItemLink(item_index)) 636 local item_id = ItemLinkToID(_G.GetMerchantItemLink(item_index))
714 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("pet")) 706 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("pet"))
715 707
716 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then 708 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
717 return 709 return
718 end 710 end
719 DBEntry("npcs", unit_idnum).mind_control = true 711 NPCEntry(unit_idnum).mind_control = true
720 table.wipe(action_data) 712 table.wipe(action_data)
721 end 713 end
722 714
723 715
724 do 716 do
757 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target")) 749 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target"))
758 750
759 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then 751 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
760 return 752 return
761 end 753 end
762 local npc = DBEntry("npcs", unit_idnum) 754 local npc = NPCEntry(unit_idnum)
763 local _, class_token = _G.UnitClass("target") 755 local _, class_token = _G.UnitClass("target")
764 npc.class = class_token 756 npc.class = class_token
765 757
766 UpdateFactionNames() 758 UpdateFactionNames()
767 DatamineTT:ClearLines() 759 DatamineTT:ClearLines()
782 end 774 end
783 npc.genders = npc.genders or {} 775 npc.genders = npc.genders or {}
784 npc.genders[GENDER_NAMES[_G.UnitSex("target")] or "UNDEFINED"] = true 776 npc.genders[GENDER_NAMES[_G.UnitSex("target")] or "UNDEFINED"] = true
785 npc.is_pvp = _G.UnitIsPVP("target") and true or nil 777 npc.is_pvp = _G.UnitIsPVP("target") and true or nil
786 npc.reaction = ("%s:%s:%s"):format(_G.UnitLevel("player"), _G.UnitFactionGroup("player"), REACTION_NAMES[_G.UnitReaction("player", "target")]) 778 npc.reaction = ("%s:%s:%s"):format(_G.UnitLevel("player"), _G.UnitFactionGroup("player"), REACTION_NAMES[_G.UnitReaction("player", "target")])
787 npc.encounter_data = npc.encounter_data or {} 779
788 780 local encounter_data = npc.encounter_data[InstanceDifficultyToken()]
789 local instance_token = InstanceDifficultyToken()
790
791 if not npc.encounter_data[instance_token] then
792 npc.encounter_data[instance_token] = {}
793 end
794 local encounter_data = npc.encounter_data[instance_token]
795 local npc_level = ("level_%d"):format(_G.UnitLevel("target")) 781 local npc_level = ("level_%d"):format(_G.UnitLevel("target"))
796 782
797 if not encounter_data[npc_level] then 783 if not encounter_data[npc_level] then
798 encounter_data[npc_level] = { 784 encounter_data[npc_level] = {
799 max_health = _G.UnitHealthMax("target"), 785 max_health = _G.UnitHealthMax("target"),
867 function WDP:TRAINER_SHOW() 853 function WDP:TRAINER_SHOW()
868 if not _G.IsTradeskillTrainer() then 854 if not _G.IsTradeskillTrainer() then
869 return 855 return
870 end 856 end
871 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target")) 857 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target"))
872 local npc = DBEntry("npcs", unit_idnum) 858 local npc = NPCEntry(unit_idnum)
873 npc.teaches = npc.teaches or {} 859 npc.teaches = npc.teaches or {}
874 860
875 -- Get the initial trainer filters 861 -- Get the initial trainer filters
876 local available = _G.GetTrainerServiceTypeFilter("available") 862 local available = _G.GetTrainerServiceTypeFilter("available")
877 local unavailable = _G.GetTrainerServiceTypeFilter("unavailable") 863 local unavailable = _G.GetTrainerServiceTypeFilter("unavailable")