comparison Main.lua @ 19:6a0c96063003

Handle looting items from items. Renamed UnitEntry() to DBEntry() for clarity.
author James D. Callahan III <jcallahan@curse.com>
date Tue, 08 May 2012 12:59:23 -0500
parents 86f02232a9e5
children dae28edbbd61
comparison
equal deleted inserted replaced
18:86f02232a9e5 19:6a0c96063003
69 69
70 70
71 ----------------------------------------------------------------------- 71 -----------------------------------------------------------------------
72 -- Helper Functions. 72 -- Helper Functions.
73 ----------------------------------------------------------------------- 73 -----------------------------------------------------------------------
74 local function UnitEntry(unit_type, unit_id) 74 local function DBEntry(data_type, unit_id)
75 if not unit_type or not unit_id then 75 if not data_type or not unit_id then
76 return 76 return
77 end 77 end
78 local unit = db[unit_type][unit_id] 78 local unit = db[data_type][unit_id]
79 79
80 if not unit then 80 if not unit then
81 db[unit_type][unit_id] = {} 81 db[data_type][unit_id] = {}
82 unit = db[unit_type][unit_id] 82 unit = db[data_type][unit_id]
83 end 83 end
84 return unit 84 return unit
85 end 85 end
86 86
87 87
143 local function UpdateObjectLocation(identifier) 143 local function UpdateObjectLocation(identifier)
144 if not identifier then 144 if not identifier then
145 return 145 return
146 end 146 end
147 local zone_name, x, y, map_level, instance_type = CurrentLocationData() 147 local zone_name, x, y, map_level, instance_type = CurrentLocationData()
148 local object = UnitEntry("objects", identifier) 148 local object = DBEntry("objects", identifier)
149 object.locations = object.locations or {} 149 object.locations = object.locations or {}
150 150
151 if not object.locations[zone_name] then 151 if not object.locations[zone_name] then
152 object.locations[zone_name] = {} 152 object.locations[zone_name] = {}
153 end 153 end
154 object.locations[zone_name][("%s:%s:%s:%s"):format(instance_type, map_level, x, y)] = true 154 object.locations[zone_name][("%s:%s:%s:%s"):format(instance_type, map_level, x, y)] = true
155 end
156
157
158 local function HandleItemUse(item_link, bag_index, slot_index)
159 if not item_link then
160 return
161 end
162 local item_id = ItemLinkToID(item_link)
163
164 if not bag_index or not slot_index then
165 for new_bag_index = 0, _G.NUM_BAG_FRAMES do
166 for new_slot_index = 1, _G.GetContainerNumSlots(new_bag_index) do
167 if item_id == ItemLinkToID(_G.GetContainerItemLink(new_bag_index, new_slot_index)) then
168 bag_index = new_bag_index
169 slot_index = new_slot_index
170 break
171 end
172 end
173 end
174 end
175
176 if not bag_index or not slot_index then
177 return
178 end
179 local _, _, _, _, _, is_lootable = _G.GetContainerItemInfo(bag_index, slot_index)
180
181 if not is_lootable then
182 return
183 end
184 DatamineTT:ClearLines()
185 DatamineTT:SetBagItem(bag_index, slot_index)
186
187 for line_index = 1, DatamineTT:NumLines() do
188 local current_line = _G["WDPDatamineTTTextLeft" .. line_index]
189
190 if not current_line then
191 break
192 end
193
194 if current_line:GetText() == _G.ITEM_OPENABLE then
195 table.wipe(action_data)
196 action_data.type = AF.ITEM
197 action_data.item_id = item_id
198 action_data.loot_type = "contains"
199 break
200 end
201 end
155 end 202 end
156 203
157 204
158 ----------------------------------------------------------------------- 205 -----------------------------------------------------------------------
159 -- Methods. 206 -- Methods.
180 for event_name, mapping in pairs(EVENT_MAPPING) do 227 for event_name, mapping in pairs(EVENT_MAPPING) do
181 self:RegisterEvent(event_name, (_G.type(mapping) ~= "boolean") and mapping or nil) 228 self:RegisterEvent(event_name, (_G.type(mapping) ~= "boolean") and mapping or nil)
182 end 229 end
183 durability_timer_handle = self:ScheduleRepeatingTimer("ProcessDurability", 30) 230 durability_timer_handle = self:ScheduleRepeatingTimer("ProcessDurability", 30)
184 target_location_timer_handle = self:ScheduleRepeatingTimer("UpdateTargetLocation", 0.2) 231 target_location_timer_handle = self:ScheduleRepeatingTimer("UpdateTargetLocation", 0.2)
232
233 _G.hooksecurefunc("UseContainerItem", function(bag_index, slot_index, target_unit)
234 if target_unit then
235 return
236 end
237 HandleItemUse(_G.GetContainerItemLink(bag_index, slot_index), bag_index, slot_index)
238
239 end)
240
241 _G.hooksecurefunc("UseItemByName", function(identifier, target_unit)
242 if target_unit then
243 return
244 end
245 local _, item_link = _G.GetItemInfo(identifier)
246 HandleItemUse(item_link)
247 end)
185 end 248 end
186 249
187 250
188 local function RecordDurability(item_id, durability) 251 local function RecordDurability(item_id, durability)
189 if not durability or durability <= 0 then 252 if not durability or durability <= 0 then
235 298
236 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then 299 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
237 return 300 return
238 end 301 end
239 local zone_name, x, y, map_level, instance_type = CurrentLocationData() 302 local zone_name, x, y, map_level, instance_type = CurrentLocationData()
240 local npc_data = UnitEntry("npcs", unit_idnum).stats[("level_%d"):format(_G.UnitLevel("target"))] 303 local npc_data = DBEntry("npcs", unit_idnum).stats[("level_%d"):format(_G.UnitLevel("target"))]
241 npc_data.locations = npc_data.locations or {} 304 npc_data.locations = npc_data.locations or {}
242 305
243 if not npc_data.locations[zone_name] then 306 if not npc_data.locations[zone_name] then
244 npc_data.locations[zone_name] = {} 307 npc_data.locations[zone_name] = {}
245 end 308 end
249 312
250 ----------------------------------------------------------------------- 313 -----------------------------------------------------------------------
251 -- Event handlers. 314 -- Event handlers.
252 ----------------------------------------------------------------------- 315 -----------------------------------------------------------------------
253 function WDP:COMBAT_TEXT_UPDATE(event, message_type, faction_name, amount) 316 function WDP:COMBAT_TEXT_UPDATE(event, message_type, faction_name, amount)
254 -- if message_type ~= "FACTION" or _G.UnitIsUnit("target", "questnpc") then 317 -- if message_type ~= "FACTION" or _G.UnitIsUnit("target", "questnpc") then
255 -- return 318 -- return
256 -- end 319 -- end
257 -- local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target")) 320 -- local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target"))
258 -- local npc = UnitEntry("npcs", unit_idnum) 321 -- local npc = DBEntry("npcs", unit_idnum)
259 -- 322 --
260 -- if not npc then 323 -- if not npc then
261 -- return 324 -- return
262 -- end 325 -- end
263 -- npc.reputations = npc.reputations or {} 326 -- npc.reputations = npc.reputations or {}
264 -- npc.reputations[faction_name] = amount 327 -- npc.reputations[faction_name] = amount
265 -- 328 --
266 -- print(("%s: %s, %s, %s"):format(event, message_type, faction_name, amount)) 329 -- print(("%s: %s, %s, %s"):format(event, message_type, faction_name, amount))
267 end 330 end
268 331
269 332
270 function WDP:LOOT_CLOSED() 333 function WDP:LOOT_CLOSED()
327 } 390 }
328 391
329 392
330 local LOOT_UPDATE_FUNCS = { 393 local LOOT_UPDATE_FUNCS = {
331 [AF.ITEM] = function() 394 [AF.ITEM] = function()
332 local item = UnitEntry("items", action_data.item_id) 395 local item = DBEntry("items", action_data.item_id)
333 local loot_type = action_data.loot_type 396 local loot_type = action_data.loot_type or "drops"
334 item[loot_type] = item[loot_type] or {} 397 item[loot_type] = item[loot_type] or {}
335 398
336 for index = 1, #action_data.loot_list do 399 for index = 1, #action_data.loot_list do
337 table.insert(item[loot_type], action_data.loot_list[index]) 400 table.insert(item[loot_type], action_data.loot_list[index])
338 end 401 end
339 end, 402 end,
340 [AF.NPC] = function() 403 [AF.NPC] = function()
341 local npc = UnitEntry("npcs", action_data.id_num) 404 local npc = DBEntry("npcs", action_data.id_num)
342 405
343 if not npc then 406 if not npc then
344 return 407 return
345 end 408 end
346 local loot_type = action_data.loot_type or "drops" 409 local loot_type = action_data.loot_type or "drops"
349 for index = 1, #action_data.loot_list do 412 for index = 1, #action_data.loot_list do
350 table.insert(npc[loot_type], action_data.loot_list[index]) 413 table.insert(npc[loot_type], action_data.loot_list[index])
351 end 414 end
352 end, 415 end,
353 [AF.OBJECT] = function() 416 [AF.OBJECT] = function()
354 local object = UnitEntry("objects", action_data.identifier) 417 local object = DBEntry("objects", action_data.identifier)
355 object.drops = object.drops or {} 418 object.drops = object.drops or {}
356 419
357 for index = 1, #action_data.loot_list do 420 for index = 1, #action_data.loot_list do
358 table.insert(object.drops, action_data.loot_list[index]) 421 table.insert(object.drops, action_data.loot_list[index])
359 end 422 end
360 end, 423 end,
361 [AF.ZONE] = function() 424 [AF.ZONE] = function()
362 local loot_type = action_data.loot_type or "drops" 425 local loot_type = action_data.loot_type or "drops"
363 local zone = UnitEntry("zones", action_data.zone) 426 local zone = DBEntry("zones", action_data.zone)
364 zone[loot_type] = zone[loot_type] or {} 427 zone[loot_type] = zone[loot_type] or {}
365 428
366 local location_data = ("%s:%s:%s:%s"):format(action_data.instance_type, action_data.map_level, action_data.x, action_data.y) 429 local location_data = ("%s:%s:%s:%s"):format(action_data.instance_type, action_data.map_level, action_data.x, action_data.y)
367 local loot_data = zone[loot_type][location_data] 430 local loot_data = zone[loot_type][location_data]
368 431
452 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target")) 515 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target"))
453 516
454 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then 517 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
455 return 518 return
456 end 519 end
457 local merchant = UnitEntry("npcs", unit_idnum) 520 local merchant = DBEntry("npcs", unit_idnum)
458 merchant.sells = merchant.sells or {} 521 merchant.sells = merchant.sells or {}
459 522
460 for item_index = 1, _G.GetMerchantNumItems() do 523 for item_index = 1, _G.GetMerchantNumItems() do
461 local _, _, copper_price, stack_size, num_available, _, extended_cost = _G.GetMerchantItemInfo(item_index) 524 local _, _, copper_price, stack_size, num_available, _, extended_cost = _G.GetMerchantItemInfo(item_index)
462 local item_id = ItemLinkToID(_G.GetMerchantItemLink(item_index)) 525 local item_id = ItemLinkToID(_G.GetMerchantItemLink(item_index))
563 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then 626 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
564 return 627 return
565 end 628 end
566 table.wipe(action_data) 629 table.wipe(action_data)
567 630
568 local npc = UnitEntry("npcs", unit_idnum) 631 local npc = DBEntry("npcs", unit_idnum)
569 local _, class_token = _G.UnitClass("target") 632 local _, class_token = _G.UnitClass("target")
570 npc.class = class_token 633 npc.class = class_token
571 -- TODO: Add faction here 634 -- TODO: Add faction here
572 npc.gender = GENDER_NAMES[_G.UnitSex("target")] or "UNDEFINED" 635 npc.gender = GENDER_NAMES[_G.UnitSex("target")] or "UNDEFINED"
573 npc.is_pvp = _G.UnitIsPVP("target") and true or nil 636 npc.is_pvp = _G.UnitIsPVP("target") and true or nil
601 local unit_type, unit_id = WDP:ParseGUID(_G.UnitGUID("questnpc")) 664 local unit_type, unit_id = WDP:ParseGUID(_G.UnitGUID("questnpc"))
602 665
603 if unit_type == private.UNIT_TYPES.OBJECT then 666 if unit_type == private.UNIT_TYPES.OBJECT then
604 UpdateObjectLocation(unit_id) 667 UpdateObjectLocation(unit_id)
605 end 668 end
606 local quest = UnitEntry("quests", _G.GetQuestID()) 669 local quest = DBEntry("quests", _G.GetQuestID())
607 quest[point] = quest[point] or {} 670 quest[point] = quest[point] or {}
608 quest[point][("%s:%d"):format(private.UNIT_TYPE_NAMES[unit_type + 1], unit_id)] = true 671 quest[point][("%s:%d"):format(private.UNIT_TYPE_NAMES[unit_type + 1], unit_id)] = true
609 end 672 end
610 673
611 674