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