local function u()local u=findPlayerHumanoid(Player)return u~=nil and u.Health>0 end local function m(u)if u~=nil then for u,m in pairs(u:GetChildren())do if m:IsA("\084\111\111\108")then return m end end end end local x=nil local d=nil local c=nil local I=nil local function g()if x then x:Cancel()x=nil end if c then c:Disconnect()c=nil end if I then I:Disconnect()I=nil end if d then d:Destroy()end end local function A(u,d,A,h,b)x=u u:Start(b)c=u.Finished.Event:Connect(function()g()if A then local u=m(h)if u then u:Activate()end end end)I=u.PathFailed.Event:Connect(function()g()if b==nil or b then local u=PlayFailureAnimation and not(x and x:IsActive())if u then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(d)end end)end local function h(u)if x and x:IsActive()then x:Cancel()end if PlayFailureAnimation then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(u)end function OnTap(x,d,c)local I=Workspace.CurrentCamera local b=Player.Character if not u()then return end if#x==1 or d then if I then local u=I:ScreenPointToRay(x[1].x,x[1].y)local m=Ray.new(u.Origin,u.Direction*1000)local R=findPlayerHumanoid(Player)local n,y,s=Utility.Raycast(m,true,getIgnoreList())local C,D=Utility.FindCharacterAncestor(n)if c and(D and StarterGui:GetCore("\065\118\097\116\097\114\067\111\110\116\101\120\116\077\101\110\117\069\110\097\098\108\101\100"))then local u=Players:GetPlayerFromCharacter(D.Parent)if u then g()return end end if d then y=d C=nil end if y and b then g()local u=Pather(y,s)if u:IsValidPath()then A(u,y,C,b)else u:Cleanup()h(y)end end end elseif#x>=2 then if I then local u=m(b)if u then u:Activate()end end end end local function b(u)if u then u:Disconnect()end end
-------------------------------------------------------------------------

local function CheckAlive()
	local humanoid = findPlayerHumanoid(Player)
	return humanoid ~= nil and humanoid.Health > 0
end

local function GetEquippedTool(character)
	if character ~= nil then
		for _, child in pairs(character:GetChildren()) do
			if child:IsA("Tool") then
				return child
			end
		end
	end
end

local ExistingPather = nil
local ExistingIndicator = nil
local PathCompleteListener = nil
local PathFailedListener = nil

local function CleanupPath()
	if ExistingPather then
		ExistingPather:Cancel()
		ExistingPather = nil
	end
	if PathCompleteListener then
		PathCompleteListener:Disconnect()
		PathCompleteListener = nil
	end
	if PathFailedListener then
		PathFailedListener:Disconnect()
		PathFailedListener = nil
	end
	if ExistingIndicator then
		ExistingIndicator:Destroy()
	end
end

local function HandleMoveTo(thisPather, hitPt, hitChar, character, overrideShowPath)
	ExistingPather = thisPather
	thisPather:Start(overrideShowPath)

	PathCompleteListener = thisPather.Finished.Event:Connect(function()
		CleanupPath()
		if hitChar then
			local currentWeapon = GetEquippedTool(character)
			if currentWeapon then
				currentWeapon:Activate()
			end
		end
	end)
	PathFailedListener = thisPather.PathFailed.Event:Connect(function()
		CleanupPath()
		if overrideShowPath == nil or overrideShowPath then
			local shouldPlayFailureAnim = PlayFailureAnimation and not (ExistingPather and ExistingPather:IsActive())
			if shouldPlayFailureAnim then
				ClickToMoveDisplay.PlayFailureAnimation()
			end
			ClickToMoveDisplay.DisplayFailureWaypoint(hitPt)
		end
	end)
end

local function ShowPathFailedFeedback(hitPt)
	if ExistingPather and ExistingPather:IsActive() then
		ExistingPather:Cancel()
	end
	if PlayFailureAnimation then
		ClickToMoveDisplay.PlayFailureAnimation()
	end
	ClickToMoveDisplay.DisplayFailureWaypoint(hitPt)
end

function OnTap(tapPositions, goToPoint, wasTouchTap)
	-- Good to remember if this is the latest tap event
	local camera = Workspace.CurrentCamera
	local character = Player.Character

	if not CheckAlive() then
		return
	end

	-- This is a path tap position
	if #tapPositions == 1 or goToPoint then
		if camera then
			local unitRay = camera:ScreenPointToRay(tapPositions[1].x, tapPositions[1].y)
			local ray = Ray.new(unitRay.Origin, unitRay.Direction * 1000)

			local myHumanoid = findPlayerHumanoid(Player)
			local hitPart, hitPt, hitNormal = Utility.Raycast(ray, true, getIgnoreList())

			local hitChar, hitHumanoid = Utility.FindCharacterAncestor(hitPart)
			if wasTouchTap and hitHumanoid and StarterGui:GetCore("AvatarContextMenuEnabled") then
				local clickedPlayer = Players:GetPlayerFromCharacter(hitHumanoid.Parent)
				if clickedPlayer then
					CleanupPath()
					return
				end
			end
			if goToPoint then
				hitPt = goToPoint
				hitChar = nil
			end
			if hitPt and character then
				-- Clean up current path
				CleanupPath()
				local thisPather = Pather(hitPt, hitNormal)
				if thisPather:IsValidPath() then
					HandleMoveTo(thisPather, hitPt, hitChar, character)
				else
					-- Clean up
					thisPather:Cleanup()
					-- Feedback here for when we don't have a good path
					ShowPathFailedFeedback(hitPt)
				end
			end
		end
	elseif #tapPositions >= 2 then
		if camera then
			-- Do shoot
			local currentWeapon = GetEquippedTool(character)
			if currentWeapon then
				currentWeapon:Activate()
			end
		end
	end
end

local function DisconnectEvent(event)
	if event then
		event:Disconnect()
	end
end
