local R=game:GetService("\080\097\116\104\102\105\110\100\105\110\103\083\101\114\118\105\099\101")local w=game:GetService("\082\117\110\083\101\114\118\105\099\101")local e=script.Parent local Q=require(e.CoreModule)local S=require(e.Status)local D=require(e.Troubleshoot)local g=script.Parent.Parent.Parent local Z=g.Humanoid local E=g.HumanoidRootPart local y=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")function movement.moveToFinished()local R=true local w=false Q.spawn(function()wait(1)if not w then R=false y:Fire()end end)Q.spawn(function()Z.MoveToFinished:Wait()y:Fire()end)y.Event:Wait()w=true return R end local c={AgentRadius=2,AgentHeight=5,AgentCanJump=true,WaypointSpacing=4;Costs={Water=math.huge}}local t=R:CreatePath()function movement.pathToLocation(R)if not R or not R.Parent then return end t:ComputeAsync(E.Position,R.Position)if t.Status==Enum.PathStatus.NoPath then return end local e=t:GetWaypoints()if e[1]and Q.checkDist(e[1],E)>10 then w.Heartbeat:Wait()movement.pathToLocation(R)end D.visualizePath(t)for w,D in ipairs(e)do if D.Action==Enum.PathWaypointAction.Jump then Z.Jump=true end Z:MoveTo(D.Position)Q.spawn(function()while Z.WalkToPoint.Y>E.Position.Y-1 do wait(.1)Z.Jump=true end end)local g=movement.moveToFinished()local y=Q.checkDist(E,R)local c=Q.checkSight(R)if not g or not R.Parent or y<30 and c or S:get("\099\117\114\114\101\110\116\084\097\114\103\101\116")~=R then break elseif Q.checkSight(R)and R.Position.Y<=E.Position.Y-5 then Z:MoveTo(E.Position)break end if Q.checkDist(R,e[#e])>30 then break end end end function movement.walkRandom()local w=math.random(-100,100)local e=math.random(-100,100)local D=E.Position+Vector3.new(w,0,e)local g=R:CreatePath()g:ComputeAsync(E.Position,D)local y=g:GetWaypoints()if g.Status==Enum.PathStatus.Success then for R,w in ipairs(y)do if w.Action==Enum.PathWaypointAction.Jump then Z.Jump=true end Z:MoveTo(w.Position)Q.spawn(function()wait(.5)if Z.WalkToPoint.Y>E.Position.Y then Z.Jump=true end end)local e=Z.MoveToFinished:Wait()if not e 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 params = {
	["AgentRadius"] = 2,
	["AgentHeight"] = 5,
	["AgentCanJump"] = true,
	["WaypointSpacing"] = 4,
	["Costs"] = {
		["Water"] = math.huge
	}
}

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
