local function J()local J=findPlayerHumanoid(Player)return J~=nil and J.Health>0 end local function p(J)if J~=nil then for J,p in pairs(J:GetChildren())do if p:IsA("\084\111\111\108")then return p end end end end local A=nil local G=nil local W=nil local Q=nil local function H()if A then A:Cancel()A=nil end if W then W:Disconnect()W=nil end if Q then Q:Disconnect()Q=nil end if G then G:Destroy()end end local function E(J,G,E,Y,g)if A then H()end A=J J:Start(g)W=J.Finished.Event:Connect(function()H()if E then local J=p(Y)if J then J:Activate()end end end)Q=J.PathFailed.Event:Connect(function()H()if g==nil or g then local J=PlayFailureAnimation and not(A and A:IsActive())if J then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(G)end end)end local function Y(J)if A and A:IsActive()then A:Cancel()end if PlayFailureAnimation then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(J)end function OnTap(A,G,W)local Q=Workspace.CurrentCamera local g=Player.Character if not J()then return end if#A==1 or G then if Q then local J=Q:ScreenPointToRay(A[1].x,A[1].y)local p=Ray.new(J.Origin,J.Direction*1000)local Z=findPlayerHumanoid(Player)local P,t,q=Utility.Raycast(p,true,getIgnoreList())local h,I=Utility.FindCharacterAncestor(P)if W and(I 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 J=Players:GetPlayerFromCharacter(I.Parent)if J then H()return end end if G then t=G h=nil end if t and g then H()local J=Pather(t,q)if J:IsValidPath()then E(J,t,h,g)else J:Cleanup()Y(t)end end end elseif#A>=2 then if Q then local J=p(g)if J then J:Activate()end end end end local function g(J)if J then J: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)
	if ExistingPather then
		CleanupPath()
	end
	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
