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