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