local n=game:GetService("\080\097\116\104\102\105\110\100\105\110\103\083\101\114\118\105\099\101")local t=game:GetService("\082\117\110\083\101\114\118\105\099\101")local H=script.Parent local B=require(H.CoreModule)local S=require(H.Status)local Z=require(H.Troubleshoot)local I=script.Parent.Parent.Parent local O=I.Humanoid local G=I.HumanoidRootPart local i=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")function movement.moveToFinished()local n=true local t=false B.spawn(function()wait(1)if not t then n=false i:Fire()end end)B.spawn(function()O.MoveToFinished:Wait()i:Fire()end)i.Event:Wait()t=true return n end local o=n:CreatePath()function movement.pathToLocation(n)if not n or not n.Parent then return end o:ComputeAsync(G.Position,n.Position)if o.Status==Enum.PathStatus.NoPath then return end local H=o:GetWaypoints()if H[1]and B.checkDist(H[1],G)>10 then t.Heartbeat:Wait()movement.pathToLocation(n)end Z.visualizePath(o)for t,Z in ipairs(H)do if Z.Action==Enum.PathWaypointAction.Jump then O.Jump=true end O:MoveTo(Z.Position)B.spawn(function()while O.WalkToPoint.Y>G.Position.Y-1 do wait(.1)O.Jump=true end end)local I=movement.moveToFinished()local i=B.checkDist(G,n)local o=B.checkSight(n)if not I or not n.Parent or i<30 and o or S:get("\099\117\114\114\101\110\116\084\097\114\103\101\116")~=n then break elseif B.checkSight(n)and n.Position.Y<=G.Position.Y-5 then O:MoveTo(G.Position)break end if B.checkDist(n,H[#H])>30 then break end end end function movement.walkRandom()local t=math.random(-100,100)local H=math.random(-100,100)local Z=G.Position+Vector3.new(t,0,H)local I=n:CreatePath()I:ComputeAsync(G.Position,Z)local i=I:GetWaypoints()if I.Status==Enum.PathStatus.Success then for n,t in ipairs(i)do if t.Action==Enum.PathWaypointAction.Jump then O.Jump=true end O:MoveTo(t.Position)B.spawn(function()wait(.5)if O.WalkToPoint.Y>G.Position.Y then O.Jump=true end end)local H=O.MoveToFinished:Wait()if not H or S:get("\099\117\114\114\101\110\116\084\097\114\103\101\116")then break end end else wait(2)end end

local pathfindingService = game:GetService("PathfindingService")
local runService = game:GetService("RunService")

local modules = script.Parent
local core = require(modules.CoreModule)
local status = require(modules.Status)
local troubleshoot = require(modules.Troubleshoot)

local marine = script.Parent.Parent.Parent
local myHuman = marine.Humanoid
local myRoot = marine.HumanoidRootPart


local moveEvent = Instance.new("BindableEvent")
function movement.moveToFinished()
	local success = true
	local completed = false
    
    core.spawn(function() 
        wait(1)
		if not completed then
	        success = false
	        moveEvent:Fire()   
		end
    end)
    
    core.spawn(function()
        myHuman.MoveToFinished:Wait()
        moveEvent:Fire()
    end)
    
	moveEvent.Event:Wait()

	completed = true

    return success
end

local path = pathfindingService:CreatePath()
function movement.pathToLocation(target)
	if not target or not target.Parent then return end
	
	path:ComputeAsync(myRoot.Position, target.Position)
	
	if path.Status == Enum.PathStatus.NoPath then
		return
	end
	
	local waypoints = path:GetWaypoints()
	
	--Make sure that path is actually valid
	if waypoints[1] and core.checkDist(waypoints[1],myRoot) > 10 then
		runService.Heartbeat:Wait()
		movement.pathToLocation(target)
	end
	
	troubleshoot.visualizePath(path)
	
	for _,waypoint in ipairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			myHuman.Jump = true
		end
		myHuman:MoveTo(waypoint.Position)
		core.spawn(function()
			while myHuman.WalkToPoint.Y > myRoot.Position.Y - 1 do
				wait(0.1)
				myHuman.Jump = true
			end
		end)
		local moveSuccess = movement.moveToFinished()
		local dist = core.checkDist(myRoot,target)
		local canSee = core.checkSight(target)
		if not moveSuccess or not target.Parent or (dist < 30 and canSee) or status:get("currentTarget") ~= target then
			break
		elseif (core.checkSight(target) and target.Position.Y <= myRoot.Position.Y-5) then
			myHuman:MoveTo(myRoot.Position)
			break
		end
		if core.checkDist(target,waypoints[#waypoints]) > 30 then
			--movement.pathToLocation(target)
			break
		end
	end
end

function movement.walkRandom()
	local randX = math.random(-100,100)
	local randZ = math.random(-100,100)
	local goal = myRoot.Position + Vector3.new(randX, 0, randZ)
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(myRoot.Position, goal)
	local waypoints = path:GetWaypoints()
	
	if path.Status == Enum.PathStatus.Success then
		for i,waypoint in ipairs(waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			myHuman:MoveTo(waypoint.Position)
			core.spawn(function()
				wait(0.5)
				if myHuman.WalkToPoint.Y > myRoot.Position.Y then
					myHuman.Jump = true
				end
			end)
			local moveSuccess = myHuman.MoveToFinished:Wait()
			if not moveSuccess or status:get("currentTarget") then
				break
			end
		end
	else
		wait(2)
	end
end
