annotate Core.lua @ 1:1ba103196ede

Added a check to prevent attempting to use a dead tank. If any players die or 'unghost' it will update the macros (after combat).
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sun, 14 Nov 2010 17:27:04 -0800
parents 16396ebfa35f
children
rev   line source
Asa@0 1 TankSpotter = select(2, ...)
Asa@0 2 TankSpotter = LibStub("AceAddon-3.0"):NewAddon(TankSpotter, "TankSpotter", "AceEvent-3.0")
Asa@0 3
Asa@0 4 local tankGUID = nil
Asa@0 5
Asa@0 6 function TankSpotter:Slash(msg, editBox)
Asa@0 7 msg = strlower(msg)
Asa@0 8 if msg == 'set' then
Asa@0 9 if UnitName("target") and UnitIsFriend("player", "target") then
Asa@0 10 tankGUID = UnitGUID("target")
Asa@0 11 if UnitGUID("pet") == tankGUID then
Asa@0 12 -- pets change GUIDs when spawned, so this GUID can't be used
Asa@0 13 tankGUID = 'pet'
Asa@0 14 end
Asa@0 15 else
Asa@0 16 print('You must select a friendly target to set it as your tank.')
Asa@0 17 end
Asa@0 18 elseif msg == 'clear' then
Asa@0 19 tankGUID = nil
Asa@0 20 elseif msg == 'update' then
Asa@0 21 -- UpdateAllMacros is called at the end of the function.
Asa@0 22 else
Asa@0 23 print('Usage: TODO: Implement instructions')
Asa@0 24 end
Asa@0 25 TankSpotter:UpdateAllMacros(msg)
Asa@0 26 end
Asa@0 27
Asa@0 28 function TankSpotter:findTank()
Asa@0 29 -- the guid is simply to make sure the pet still exists.
Asa@0 30 if tankGUID == 'pet' and UnitGUID("pet") then
Asa@0 31 return 'pet'
Asa@0 32 end
Asa@0 33
Asa@0 34 local tank = nil
Asa@0 35 for i = 1, GetNumRaidMembers()-1 do
Asa@0 36 local unit = 'raid'..i
Asa@1 37 if not UnitIsDead(unit) then
Asa@1 38 if tankGUID and UnitGUID(unit) == tankGUID then
Asa@1 39 return unit
Asa@1 40 end
Asa@1 41 if UnitGroupRolesAssigned(unit) == 'TANK' then
Asa@1 42 if tank == nil then
Asa@1 43 tank = unit
Asa@1 44 else
Asa@1 45 -- Too many tanks.
Asa@1 46 tank = ''
Asa@1 47 end
Asa@0 48 end
Asa@0 49 end
Asa@0 50 end
Asa@0 51 if tank and tank ~= '' then
Asa@0 52 return tank
Asa@0 53 end
Asa@0 54
Asa@0 55 tank = nil
Asa@0 56 for i = 1, GetNumPartyMembers() do
Asa@0 57 local unit = 'party'..i
Asa@1 58 if not UnitIsDead(unit) then
Asa@1 59 if tankGUID and UnitGUID(unit) == tankGUID then
Asa@1 60 return unit
Asa@1 61 end
Asa@1 62
Asa@1 63 if UnitGroupRolesAssigned(unit) == 'TANK' then
Asa@1 64 tank = unit
Asa@1 65 end
Asa@0 66 end
Asa@0 67 end
Asa@0 68
Asa@0 69 return tank or 'pet'
Asa@0 70 end
Asa@0 71
Asa@0 72 TankSpotter.former_tank = ''
Asa@0 73 function TankSpotter:updateMacro(index)
Asa@0 74 if index > 0 then
Asa@0 75 name, iconTexture, body, isLocal = GetMacroInfo(index);
Asa@0 76 local former_tank = body:match('#tank=(%w+)')
Asa@0 77 local new_tank = self:findTank()
Asa@0 78 if former_tank and new_tank ~= former_tank then
Asa@0 79 if self.former_tank ~= new_tank then
Asa@0 80 print(format('Updating tank macros from %s to %s', former_tank, new_tank))
Asa@0 81
Asa@0 82 end
Asa@0 83 self.former_tank = new_tank
Asa@0 84 local new_body = body:gsub(former_tank, new_tank)
Asa@0 85 name = name:gsub(former_tank, new_tank)
Asa@0 86 -- print('y', index, name, iconTexture, body, isLocal)
Asa@0 87 if new_body ~= body then
Asa@0 88 EditMacro(index, name, 0, new_body, nil)
Asa@0 89 end
Asa@0 90 end
Asa@0 91 end
Asa@0 92 end
Asa@0 93
Asa@0 94 local updateAfterCombat = false
Asa@0 95 function TankSpotter:UpdateAllMacros(event)
Asa@0 96 if InCombatLockdown() then
Asa@0 97 updateAfterCombat = true
Asa@0 98 TankSpotter:RegisterEvent("PLAYER_LEAVE_COMBAT", 'UpdateAllMacros')
Asa@0 99 return
Asa@0 100 end
Asa@0 101
Asa@0 102 if updateAfterCombat then
Asa@0 103 TankSpotter:UnregisterEvent("PLAYER_LEAVE_COMBAT")
Asa@0 104 end
Asa@0 105
Asa@0 106 local endGlobal, endChar = GetNumMacros()
Asa@0 107 endChar = endChar + 36 -- character specific macros start at 37
Asa@0 108 local i = 1
Asa@0 109
Asa@0 110 while i <= endChar do
Asa@0 111 self:updateMacro(i)
Asa@0 112 if i == endGlobal then
Asa@0 113 i = 36
Asa@0 114 end
Asa@0 115 i = i + 1
Asa@0 116 end
Asa@0 117 end
Asa@0 118
Asa@0 119
Asa@0 120 TankSpotter:RegisterEvent("PLAYER_ROLES_ASSIGNED", 'UpdateAllMacros')
Asa@0 121 TankSpotter:RegisterEvent("PLAYER_ENTERING_WORLD", 'UpdateAllMacros')
Asa@0 122 TankSpotter:RegisterEvent("PARTY_MEMBERS_CHANGED", 'UpdateAllMacros')
Asa@0 123 -- This is used to determine when a pet is summoned or dismissed
Asa@0 124 TankSpotter:RegisterEvent("UNIT_PET", 'UpdateAllMacros')
Asa@0 125
Asa@1 126 -- If your tank dies, this will trigger a new tank to be found as soon as combat is over.
Asa@1 127 TankSpotter:RegisterEvent("PLAYER_DEAD", 'UpdateAllMacros')
Asa@1 128 TankSpotter:RegisterEvent("PLAYER_UNGHOST", 'UpdateAllMacros')
Asa@1 129
Asa@1 130
Asa@1 131
Asa@0 132 SLASH_TANKSPOTTER1, SLASH_TANKSPOTTER2 = '/tankspotter', '/ts'
Asa@0 133 SlashCmdList["TANKSPOTTER"] = function(msg, editBox)
Asa@0 134 TankSpotter:Slash(msg, editBox)
Asa@0 135 end