view 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
line wrap: on
line source
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 not UnitIsDead(unit) then
			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
	end
	if tank and tank ~= '' then
		return tank
	end

	tank = nil
	for i = 1, GetNumPartyMembers() do
		local unit = 'party'..i
		if not UnitIsDead(unit) then
			if tankGUID and UnitGUID(unit) == tankGUID then
				return unit
			end

			if UnitGroupRolesAssigned(unit) == 'TANK' then
				tank = unit
			end
		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')

-- If your tank dies, this will trigger a new tank to be found as soon as combat is over.
TankSpotter:RegisterEvent("PLAYER_DEAD", 'UpdateAllMacros')
TankSpotter:RegisterEvent("PLAYER_UNGHOST", 'UpdateAllMacros')



SLASH_TANKSPOTTER1, SLASH_TANKSPOTTER2  = '/tankspotter', '/ts'
SlashCmdList["TANKSPOTTER"] = function(msg, editBox)
	TankSpotter:Slash(msg, editBox)
end