comparison Main.lua @ 13:39972835bdc9

Added proper support for Herbalism and Mining from NPC corpses. Wipe action_data when LOOT_CLOSED fires.
author James D. Callahan III <jcallahan@curse.com>
date Tue, 01 May 2012 16:07:13 -0500
parents be3d67c28a27
children 27c153c3a1ed
comparison
equal deleted inserted replaced
12:be3d67c28a27 13:39972835bdc9
35 } 35 }
36 } 36 }
37 37
38 38
39 local EVENT_MAPPING = { 39 local EVENT_MAPPING = {
40 LOOT_CLOSED = true,
40 LOOT_OPENED = true, 41 LOOT_OPENED = true,
41 MERCHANT_SHOW = "UpdateMerchantItems", 42 MERCHANT_SHOW = "UpdateMerchantItems",
42 MERCHANT_UPDATE = "UpdateMerchantItems", 43 MERCHANT_UPDATE = "UpdateMerchantItems",
43 PLAYER_TARGET_CHANGED = true, 44 PLAYER_TARGET_CHANGED = true,
44 QUEST_COMPLETE = true, 45 QUEST_COMPLETE = true,
233 234
234 235
235 ----------------------------------------------------------------------- 236 -----------------------------------------------------------------------
236 -- Event handlers. 237 -- Event handlers.
237 ----------------------------------------------------------------------- 238 -----------------------------------------------------------------------
238 local re_gold = _G.GOLD_AMOUNT:gsub("%%d", "(%%d+)") 239 function WDP:LOOT_CLOSED()
239 local re_silver = _G.SILVER_AMOUNT:gsub("%%d", "(%%d+)") 240 table.wipe(action_data)
240 local re_copper = _G.COPPER_AMOUNT:gsub("%%d", "(%%d+)") 241 end
241 242
242 243
243 local function _moneyMatch(money, re) 244 do
244 return money:match(re) or 0 245 local re_gold = _G.GOLD_AMOUNT:gsub("%%d", "(%%d+)")
245 end 246 local re_silver = _G.SILVER_AMOUNT:gsub("%%d", "(%%d+)")
246 247 local re_copper = _G.COPPER_AMOUNT:gsub("%%d", "(%%d+)")
247 248
248 local function _toCopper(money) 249
249 if not money then 250 local function _moneyMatch(money, re)
250 return 0 251 return money:match(re) or 0
251 end 252 end
252 253
253 return _moneyMatch(money, re_gold) * 10000 + _moneyMatch(money, re_silver) * 100 + _moneyMatch(money, re_copper) 254
254 end 255 local function _toCopper(money)
255 256 if not money then
256 257 return 0
257 local LOOT_VERIFY_FUNCS = { 258 end
258 [AF.NPC] = function() 259
259 local fishing_loot = _G.IsFishingLoot() 260 return _moneyMatch(money, re_gold) * 10000 + _moneyMatch(money, re_silver) * 100 + _moneyMatch(money, re_copper)
260 261 end
261 if not fishing_loot and _G.UnitExists("target") and not _G.UnitIsFriend("player", "target") and _G.UnitIsDead("target") then 262
262 if _G.UnitIsPlayer("target") or _G.UnitPlayerControlled("target") then 263
263 return false 264 local LOOT_VERIFY_FUNCS = {
264 end 265 [AF.NPC] = function()
265 local unit_type, id_num = WDP:ParseGUID(_G.UnitGUID("target")) 266 local fishing_loot = _G.IsFishingLoot()
266 action_data.id_num = id_num 267
267 end 268 if not fishing_loot and _G.UnitExists("target") and not _G.UnitIsFriend("player", "target") and _G.UnitIsDead("target") then
268 return true 269 if _G.UnitIsPlayer("target") or _G.UnitPlayerControlled("target") then
269 end, 270 return false
270 [AF.OBJECT] = function() 271 end
271 return true 272 local unit_type, id_num = WDP:ParseGUID(_G.UnitGUID("target"))
272 end, 273 action_data.id_num = id_num
273 } 274 end
274 275 return true
275 276 end,
276 local LOOT_UPDATE_FUNCS = { 277 [AF.OBJECT] = function()
277 [AF.NPC] = function() 278 return true
278 local npc = UnitEntry("npcs", action_data.id_num) 279 end,
279 npc.drops = npc.drops or {} 280 }
280 281
281 for index = 1, #action_data.drops do 282
282 table.insert(npc.drops, action_data.drops[index]) 283 local LOOT_UPDATE_FUNCS = {
283 end 284 [AF.NPC] = function()
284 end, 285 local npc = UnitEntry("npcs", action_data.id_num)
285 [AF.OBJECT] = function() 286 local loot_type = action_data.loot_type or "drops"
286 local object = UnitEntry("objects", action_data.identifier) 287 npc[loot_type] = npc[loot_type] or {}
287 object.drops = object.drops or {} 288
288 289 for index = 1, #action_data.drops do
289 for index = 1, #action_data.drops do 290 table.insert(npc[loot_type], action_data.drops[index])
290 table.insert(object.drops, action_data.drops[index]) 291 end
291 end 292 end,
292 end, 293 [AF.OBJECT] = function()
293 } 294 local object = UnitEntry("objects", action_data.identifier)
294 295 object.drops = object.drops or {}
295 296
296 function WDP:LOOT_OPENED() 297 for index = 1, #action_data.drops do
297 if not action_data.type then 298 table.insert(object.drops, action_data.drops[index])
298 action_data.type = AF.NPC 299 end
299 end 300 end,
300 local verify_func = LOOT_VERIFY_FUNCS[action_data.type] 301 }
301 local update_func = LOOT_UPDATE_FUNCS[action_data.type] 302
302 303
303 if not verify_func or not update_func or not verify_func() then 304 function WDP:LOOT_OPENED()
304 return 305 if not action_data.type then
305 end 306 action_data.type = AF.NPC
306 307 end
307 local loot_registry = {} 308 local verify_func = LOOT_VERIFY_FUNCS[action_data.type]
308 action_data.drops = {} 309 local update_func = LOOT_UPDATE_FUNCS[action_data.type]
309 310
310 for loot_slot = 1, _G.GetNumLootItems() do 311 if not verify_func or not update_func or not verify_func() then
311 local icon_texture, item_text, quantity, quality, locked = _G.GetLootSlotInfo(loot_slot) 312 return
312 313 end
313 if _G.LootSlotIsItem(loot_slot) then 314
314 local item_id = ItemLinkToID(_G.GetLootSlotLink(loot_slot)) 315 local loot_registry = {}
315 loot_registry[item_id] = (loot_registry[item_id]) or 0 + quantity 316 action_data.drops = {}
316 elseif _G.LootSlotIsCoin(loot_slot) then 317
317 table.insert(action_data.drops, ("money:%d"):format(_toCopper(item_text))) 318 for loot_slot = 1, _G.GetNumLootItems() do
318 elseif _G.LootSlotIsCurrency(loot_slot) then 319 local icon_texture, item_text, quantity, quality, locked = _G.GetLootSlotInfo(loot_slot)
319 table.insert(action_data.drops, ("currency:%d:%s"):format(quantity, icon_texture:match("[^\\]+$"):lower())) 320
320 end 321 if _G.LootSlotIsItem(loot_slot) then
321 end 322 local item_id = ItemLinkToID(_G.GetLootSlotLink(loot_slot))
322 323 loot_registry[item_id] = (loot_registry[item_id]) or 0 + quantity
323 for item_id, quantity in pairs(loot_registry) do 324 elseif _G.LootSlotIsCoin(loot_slot) then
324 table.insert(action_data.drops, ("%d:%d"):format(item_id, quantity)) 325 table.insert(action_data.drops, ("money:%d"):format(_toCopper(item_text)))
325 end 326 elseif _G.LootSlotIsCurrency(loot_slot) then
326 update_func() 327 table.insert(action_data.drops, ("currency:%d:%s"):format(quantity, icon_texture:match("[^\\]+$"):lower()))
327 end 328 end
329 end
330
331 for item_id, quantity in pairs(loot_registry) do
332 table.insert(action_data.drops, ("%d:%d"):format(item_id, quantity))
333 end
334 update_func()
335 end
336 end -- do-block
328 337
329 338
330 local POINT_MATCH_PATTERNS = { 339 local POINT_MATCH_PATTERNS = {
331 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING:gsub("%%d", "(%%d+)")), -- May no longer be necessary 340 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING:gsub("%%d", "(%%d+)")), -- May no longer be necessary
332 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_3V3:gsub("%%d", "(%%d+)")), -- May no longer be necessary 341 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_3V3:gsub("%%d", "(%%d+)")), -- May no longer be necessary
467 if max_power > 0 then 476 if max_power > 0 then
468 local power_type = _G.UnitPowerType("target") 477 local power_type = _G.UnitPowerType("target")
469 npc.stats[npc_level].power = ("%s:%d"):format(POWER_TYPE_NAMES[_G.tostring(power_type)] or power_type, max_power) 478 npc.stats[npc_level].power = ("%s:%d"):format(POWER_TYPE_NAMES[_G.tostring(power_type)] or power_type, max_power)
470 end 479 end
471 end 480 end
472 action_data.type = AF.NPC -- This will be set as appropriate below
473 end 481 end
474 482
475 483
476 do 484 do
477 local function UpdateQuestJuncture(point) 485 local function UpdateQuestJuncture(point)
555 action_data.type = AF.OBJECT 563 action_data.type = AF.OBJECT
556 action_data.x = x 564 action_data.x = x
557 action_data.y = y 565 action_data.y = y
558 action_data.zone = zone_name 566 action_data.zone = zone_name
559 action_data.identifier = identifier 567 action_data.identifier = identifier
560 print(("Found spell flagged for OBJECT: %s (%s, %s)"):format(zone_name, x, y))
561 elseif bit.band(spell_flags, AF.ZONE) == AF.ZONE then 568 elseif bit.band(spell_flags, AF.ZONE) == AF.ZONE then
562 print("Found spell flagged for ZONE") 569 print("Found spell flagged for ZONE")
563 end 570 end
564 elseif tt_unit_name and not tt_item_name then 571 elseif tt_unit_name and not tt_item_name then
565 if bit.band(spell_flags, AF.NPC) == AF.NPC then 572 if bit.band(spell_flags, AF.NPC) == AF.NPC then
566 print("Found spell flagged for NPC") 573 if not tt_unit_id or tt_unit_name ~= target_name then
574 return
575 end
576 action_data.type = AF.NPC
577 action_data.loot_type = spell_label:lower()
567 end 578 end
568 elseif bit.band(spell_flags, AF.ITEM) == AF.ITEM then 579 elseif bit.band(spell_flags, AF.ITEM) == AF.ITEM then
569 print("Found spell flagged for ITEM") 580 print("Found spell flagged for ITEM")
570 else 581 else
571 print(("%s: We have an issue with types and flags."), event_name) 582 print(("%s: We have an issue with types and flags."), event_name)
579 function WDP:UNIT_SPELLCAST_SUCCEEDED(event_name, unit_id, spell_name, spell_rank, spell_line, spell_id) 590 function WDP:UNIT_SPELLCAST_SUCCEEDED(event_name, unit_id, spell_name, spell_rank, spell_line, spell_id)
580 if unit_id ~= "player" then 591 if unit_id ~= "player" then
581 return 592 return
582 end 593 end
583 594
584 if action_data.type == AF.OBJECT then
585 end
586
587 if private.SPELL_LABELS_BY_NAME[spell_name] then 595 if private.SPELL_LABELS_BY_NAME[spell_name] then
588 print(("%s: '%s', '%s', '%s', '%s', '%s'"):format(event_name, unit_id, spell_name, spell_rank, spell_line, spell_id)) 596 print(("%s: '%s', '%s', '%s', '%s', '%s'"):format(event_name, unit_id, spell_name, spell_rank, spell_line, spell_id))
589 end 597 end
590 private.tracked_line = nil 598 private.tracked_line = nil
591 end 599 end