Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.90 KB | None | 0 0
  1. local onlyBossAlert = true;
  2. local deadGUIDs, pulledGUIDs, petOwners = {}, {}, {};
  3. local msgColor, npcColor = "FF6600", "B30000";
  4. local classColors = {
  5.     ["DEATHKNIGHT"] = "C41F3B",
  6.     ["DEMONHUNTER"] = "A330C9",
  7.     ["DRUID"] = "FF7D0A",
  8.     ["HUNTER"] = "ABD473",
  9.     ["MAGE"] = "69CCF0",
  10.     ["MONK"] = "00FF96",
  11.     ["PALADIN"] = "F58CBA",
  12.     ["PRIEST"] = "FFFFFF",
  13.     ["ROGUE"] = "FFF569",
  14.     ["SHAMAN"] = "0070DE",
  15.     ["WARLOCK"] = "9482C9",
  16.     ["WARRIOR"] = "C79C6E"
  17. };
  18. local ignoredSpells = {
  19.     "Death Grip",
  20.     "Imprison",
  21.     "Hunter's Mark",
  22.     "Beast Lore",
  23.     "Flare",
  24.     "Soothe",
  25.     "Marked for Death",
  26.     "Shadowstep",
  27.     "Blind",
  28.     "Sap",
  29.     "Hex"
  30. };
  31.  
  32. local function isIgnoredSpell(spellName)
  33.     for _, ignoredSpellName in pairs(ignoredSpells) do
  34.         if ignoredSpellName == spellName then
  35.             return true;
  36.         end
  37.     end
  38.     return false;
  39. end
  40.  
  41. local function getClassColor(guid)
  42.     local _, engClass = GetPlayerInfoByGUID(guid);
  43.     if engClass then
  44.         return classColors[engClass];
  45.     end
  46.     return nil;
  47. end
  48.  
  49. local function isDead(guid)
  50.     if deadGUIDs[guid] then
  51.         return true;
  52.     end
  53.     return false;
  54. end
  55.  
  56. local function isAlreadyPulled(guid)
  57.     if pulledGUIDs[guid] then
  58.         return true;
  59.     end
  60.     return false;
  61. end
  62.  
  63. local function isBoss(guid)
  64.     local _, _, _, _, _, mobID = strsplit("-", guid)
  65.     if LibStub("LibBossIDs-1.0").BossIDs[tonumber(mobID)] then
  66.         return true;
  67.     end
  68.     return false;
  69. end
  70.  
  71. local function isInPVEInstance()
  72.     local inInstance, instanceType = IsInInstance();
  73.     if inInstance and (instanceType == "party" or instanceType == "raid") then
  74.         return true;
  75.     end
  76.     return false;
  77. end
  78.  
  79. local function isInPartyOrRaid()
  80.     if GetNumGroupMembers(LE_PARTY_CATEGORY_HOME) > 0 or GetNumGroupMembers(LE_PARTY_CATEGORY_INSTANCE) > 0 then
  81.         return true;
  82.     end
  83.     return false;
  84. end
  85.  
  86. local function getPetOwner(petGUID)
  87.     if petGUID then
  88.         if petOwners[petGUID] then
  89.             return petOwners[petGUID][1], petOwners[petGUID][2];
  90.         end
  91.         if UnitInRaid("player") then
  92.             for i = 1, 40 do
  93.                 if UnitGUID("raidpet" .. i) == petGUID then
  94.                     return UnitGUID("raid" .. i), UnitName("raid" .. i);
  95.                 end
  96.             end
  97.         else  -- player is alone or in party.
  98.             if UnitGUID("pet") == petGUID then  -- access to player's pet if alone or in a party the same way.
  99.                 return UnitGUID("player"), UnitName("player");
  100.             end
  101.             for i = 1, 4 do  -- 4 members of the group.
  102.                 if UnitGUID("partypet" .. i) == petGUID then
  103.                     return UnitGUID("party" .. i), UnitName("party" .. i);
  104.                 end
  105.             end
  106.         end
  107.     end
  108.     return nil;
  109. end
  110.  
  111. local function setColor(msg, color)
  112.     return string.format("|cFF" .. color .. "%s|r", msg);
  113. end
  114.  
  115. local function buildPullAlertMessage(event, isBodyPull, isPet, unitGUID, unitName, npcGUID, npcName, spellID, spellName)
  116.     if (onlyBossAlert and not isBoss(npcGUID)) or isDead(npcGUID) then
  117.         return nil;
  118.     end
  119.     local msg, classColor;
  120.     npcName = setColor(npcName, npcColor);
  121.     if string.find(event, "SWING") then
  122.         spellLink = GetSpellLink("Auto Attack");
  123.     else
  124.         spellLink = GetSpellLink(spellID) or spellName;
  125.     end
  126.     if isPet then  -- unitName == petName
  127.         local petOwnerGUID, petOwnerName = getPetOwner(unitGUID);
  128.         if petOwnerName then
  129.             classColor = getClassColor(petOwnerGUID);
  130.             petOwnerName = setColor(petOwnerName, classColor);
  131.             if isBodyPull then
  132.                 msg = string.format("%s's %s body pulled %s.", petOwnerName, unitName, npcName);
  133.             else
  134.                 msg = string.format("%s's %s pulled %s with " .. spellLink .. ".", petOwnerName, unitName, npcName);
  135.             end
  136.         else
  137.             if isBodyPull then
  138.                 msg = string.format("%s (pet) body pulled %s.", unitName, npcName);
  139.             else
  140.                 msg = string.format("%s (pet) pulled %s with " .. spellLink .. ".", unitName, npcName);
  141.             end
  142.         end
  143.     else  -- unitName == playerName
  144.         classColor = getClassColor(unitGUID);
  145.         unitName = setColor(unitName, classColor);
  146.         if isBodyPull then
  147.             msg = string.format("%s body pulled %s.", unitName, npcName);
  148.         else
  149.             msg = string.format("%s pulled %s with " .. spellLink .. ".", unitName, npcName);
  150.         end
  151.     end
  152.     return msg;
  153. end
  154.  
  155. function convertHexToRgb(hex)
  156.     local hex = hex:gsub("#", "");
  157.     if hex:len() == 3 then
  158.         return (tonumber("0x" .. hex:sub(1, 1)) * 17) / 255, (tonumber("0x" .. hex:sub(2, 2)) * 17) / 255, (tonumber("0x" .. hex:sub(3, 3)) * 17) / 255;
  159.     else
  160.         return tonumber("0x" .. hex:sub(1, 2)) / 255, tonumber("0x" .. hex:sub(3, 4)) / 255, tonumber("0x" .. hex:sub(5, 6)) / 255;
  161.     end
  162. end
  163.  
  164. local function printAllChatFrames(msg, color)
  165.     if msg then
  166.         local r, g, b = convertHexToRgb(color);
  167.         for i = 1, 20 do
  168.             _G["ChatFrame" .. i]:AddMessage(msg, r, g, b);
  169.         end
  170.     end
  171. end
  172.  
  173. local function whoTFPulled(...)
  174.     local _, event, _, srcGUID, srcName, srcFlags, _, destGUID, destName, destFlags, _, spellID, spellName = select(1, ...);
  175.     if event == "UNIT_DIED" then
  176.         deadGUIDs[destGUID] = true;
  177.         return;
  178.     end
  179.     if destName and srcName and destName ~= srcName and not string.find(event, "_RESURRECT") and not string.find(event, "_CREATE") and (string.find(event, "SWING") or string.find(event, "RANGE") or string.find(event, "SPELL")) then
  180.         -- Register owners of special pets like Army of the Dead or Totems.
  181.         if string.find(event, "_SUMMON") then
  182.             petOwners[destGUID] = {srcGUID, srcName};
  183.             return;
  184.         end
  185.         local isBodyPull, isPet;
  186.         -- A player is attacking a mob.
  187.         if bit.band(srcFlags, COMBATLOG_OBJECT_TYPE_PLAYER) ~= 0 and bit.band(destFlags, COMBATLOG_OBJECT_TYPE_NPC) ~= 0 then
  188.             if not isAlreadyPulled(destGUID) and not isIgnoredSpell(spellName) then
  189.                 pulledGUIDs[destGUID] = true;
  190.                 isBodyPull = false;
  191.                 isPet = false;
  192.                 printAllChatFrames(buildPullAlertMessage(event, isBodyPull, isPet, srcGUID, srcName, destGUID, destName, spellID, spellName), msgColor);
  193.             end
  194.         -- A mob is attacking a player.
  195.         elseif bit.band(destFlags, COMBATLOG_OBJECT_TYPE_PLAYER) ~= 0 and bit.band(srcFlags, COMBATLOG_OBJECT_TYPE_NPC) ~= 0 then
  196.             if not isAlreadyPulled(srcGUID) then
  197.                 pulledGUIDs[srcGUID] = true;
  198.                 isBodyPull = true;
  199.                 isPet = false;
  200.                 printAllChatFrames(buildPullAlertMessage(event, isBodyPull, isPet, destGUID, destName, srcGUID, srcName, spellID, spellName), msgColor);
  201.             end
  202.         -- A player's pet attacks a mob.
  203.         elseif bit.band(srcFlags, COMBATLOG_OBJECT_CONTROL_PLAYER) ~= 0 and bit.band(destFlags, COMBATLOG_OBJECT_TYPE_NPC) ~= 0 then
  204.             if not isAlreadyPulled(destGUID) then
  205.                 pulledGUIDs[destGUID] = true;
  206.                 isBodyPull = false;
  207.                 isPet = true;
  208.                 printAllChatFrames(buildPullAlertMessage(event, isBodyPull, isPet, srcGUID, srcName, destGUID, destName, spellID, spellName), msgColor);
  209.             end
  210.         -- A mob attacks a player's pet.
  211.         elseif bit.band(destFlags, COMBATLOG_OBJECT_CONTROL_PLAYER) ~= 0 and bit.band(srcFlags, COMBATLOG_OBJECT_TYPE_NPC) ~= 0 then
  212.             if not isAlreadyPulled(srcGUID) then
  213.                 pulledGUIDs[srcGUID] = true;
  214.                 isBodyPull = true;
  215.                 isPet = true;
  216.                 printAllChatFrames(buildPullAlertMessage(event, isBodyPull, isPet, destGUID, destName, srcGUID, srcName, spellID, spellName), msgColor);
  217.             end
  218.         end
  219.     end
  220. end
  221.  
  222. local function eventHandler(self, event, ...)
  223.     if event == "COMBAT_LOG_EVENT_UNFILTERED" then
  224.         if isInPVEInstance() and isInPartyOrRaid() then
  225.             whoTFPulled(...);
  226.         else
  227.             wipe(deadGUIDs);
  228.         end
  229.     elseif event == "PLAYER_REGEN_ENABLED" then
  230.         -- print("PLAYER_REGEN_ENABLED");
  231.         if not UnitIsDeadOrGhost("player") and not UnitIsFeignDeath("player") and not UnitAffectingCombat("pet") then
  232.             -- print("PLAYER_REGEN_ENABLED: wipe pulledGUIDs");
  233.             wipe(pulledGUIDs);
  234.             wipe(petOwners);
  235.         end
  236.     elseif event == "PLAYER_ALIVE" then
  237.         -- print("PLAYER_ALIVE");
  238.         if not IsEncounterInProgress() and not UnitAffectingCombat("player") then
  239.             -- print("PLAYER_ALIVE: wipe pulledGUIDs");
  240.             wipe(pulledGUIDs);
  241.             wipe(petOwners);
  242.         end
  243.     end
  244. end
  245.  
  246. local frame = CreateFrame("Frame");
  247. frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
  248. frame:RegisterEvent("PLAYER_REGEN_ENABLED");
  249. frame:RegisterEvent("PLAYER_ALIVE");
  250. frame:SetScript("OnEvent", eventHandler);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement