changeset 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
files Core.lua TankSpotter.toc
diffstat 2 files changed, 135 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core.lua	Sun Nov 14 17:03:15 2010 -0800
@@ -0,0 +1,128 @@
+TankSpotter = select(2, ...)
+TankSpotter = LibStub("AceAddon-3.0"):NewAddon(TankSpotter, "TankSpotter", "AceEvent-3.0")
+
+local tankGUID = nil
+
+function TankSpotter:Slash(msg, editBox)
+	msg = strlower(msg)
+	if msg == 'set' then
+		if UnitName("target") and UnitIsFriend("player", "target") then
+			tankGUID = UnitGUID("target")
+			if UnitGUID("pet") == tankGUID then
+				-- pets change GUIDs when spawned, so this GUID can't be used
+				tankGUID = 'pet'
+			end
+		else
+			print('You must select a friendly target to set it as your tank.')
+		end
+	elseif msg == 'clear' then
+		tankGUID = nil
+	elseif msg == 'update' then
+		-- UpdateAllMacros is called at the end of the function.
+	else
+		print('Usage: TODO: Implement instructions')
+	end
+	TankSpotter:UpdateAllMacros(msg)
+end
+
+function TankSpotter:findTank()
+	-- the guid is simply to make sure the pet still exists.
+	if tankGUID == 'pet' and UnitGUID("pet") then
+		return 'pet'
+	end
+
+	local tank = nil
+	for i = 1, GetNumRaidMembers()-1 do
+		local unit = 'raid'..i
+		if tankGUID and UnitGUID(unit) == tankGUID then
+			return unit
+		end
+		if UnitGroupRolesAssigned(unit) == 'TANK' then
+			if tank == nil then
+				tank = unit
+			else
+				-- Too many tanks.
+				tank = ''
+			end
+		end
+	end
+	if tank and tank ~= '' then
+		return tank
+	end
+
+	tank = nil
+	for i = 1, GetNumPartyMembers() do
+		local unit = 'party'..i
+		if tankGUID and UnitGUID(unit) == tankGUID then
+			return unit
+		end
+		
+		if UnitGroupRolesAssigned(unit) == 'TANK' then
+			tank = unit
+		end
+	end
+	
+	return tank or 'pet'
+end
+
+TankSpotter.former_tank = ''
+function TankSpotter:updateMacro(index)
+	if index > 0 then
+		name, iconTexture, body, isLocal = GetMacroInfo(index);
+		local former_tank = body:match('#tank=(%w+)')
+		local new_tank = self:findTank()
+		if former_tank and new_tank ~= former_tank then
+			if self.former_tank ~= new_tank then
+				print(format('Updating tank macros from %s to %s', former_tank, new_tank))
+
+			end
+			self.former_tank = new_tank
+			local new_body = body:gsub(former_tank, new_tank)
+			name = name:gsub(former_tank, new_tank)
+			-- print('y', index, name, iconTexture, body, isLocal)
+			if new_body ~= body then
+				EditMacro(index, name, 0, new_body, nil)
+			end
+		end
+	end
+end
+
+local updateAfterCombat = false
+function TankSpotter:UpdateAllMacros(event)
+	if InCombatLockdown() then
+		updateAfterCombat = true
+		TankSpotter:RegisterEvent("PLAYER_LEAVE_COMBAT", 'UpdateAllMacros')
+		return
+	end
+
+	if updateAfterCombat then
+		TankSpotter:UnregisterEvent("PLAYER_LEAVE_COMBAT")
+	end
+	
+	local endGlobal, endChar = GetNumMacros()
+	endChar = endChar + 36 -- character specific macros start at 37
+	local i = 1
+
+	while i <= endChar do
+		self:updateMacro(i)
+		if i == endGlobal then
+			i = 36
+		end
+		i = i + 1
+	end
+end
+
+
+TankSpotter:RegisterEvent("PLAYER_ROLES_ASSIGNED", 'UpdateAllMacros')
+TankSpotter:RegisterEvent("PLAYER_ENTERING_WORLD", 'UpdateAllMacros')
+TankSpotter:RegisterEvent("PARTY_MEMBERS_CHANGED", 'UpdateAllMacros')
+-- This is used to determine when a pet is summoned or dismissed
+TankSpotter:RegisterEvent("UNIT_PET", 'UpdateAllMacros')
+
+SLASH_TANKSPOTTER1, SLASH_TANKSPOTTER2  = '/tankspotter', '/ts'
+SlashCmdList["TANKSPOTTER"] = function(msg, editBox)
+	TankSpotter:Slash(msg, editBox)
+end
+
+
+-- TankSpotter:UpdateAllMacros()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TankSpotter.toc	Sun Nov 14 17:03:15 2010 -0800
@@ -0,0 +1,7 @@
+## Interface: 40000
+## Title: TankSpotter
+## Notes: "This addon will update your macros to always point at your tank"
+## Author: Asa Ayers <Asa.Ayers@Gmail.com>
+## Version: @project-version@
+
+Core.lua