/// <summary>   Heading to radians. </summary>
 ///
 ///
 /// <param name="x">                        The x coordinate. </param>
 /// <param name="y">                        The y coordinate. </param>
 /// <param name="to" type="D3DXVECTOR2">    to. </param>
 ///
 /// <returns>   A float. </returns>
 private float HeadingToRad(float x, float y, D3DXVECTOR2 to)
 {
     return (float)Math.Atan2((to.x - x), (to.y - y));
 }
 private void RotateCharacter(D3DXVECTOR2 newPoint)
 {
     var dx2DPoint = new Player2DPos {x = newPoint.x, y = newPoint.y};
     Funcs.ModelRotation(ref dx2DPoint);
 }
        /// <summary>   Gets closest index. </summary>
        ///
        /// <remarks>   Bunny 2, 3/14/2014. </remarks>
        ///
        /// <param name="PList">    The list. </param>
        /// <param name="curPos">   The current position. </param>
        ///
        /// <returns>   The closest index. </returns>
        private int GetClosestIndex(List<D3DXVECTOR2> PList, D3DXVECTOR2 curPos)
        {
            var n = 0;

            PList.ForEach(p => n = Distance(curPos, p) < Distance(curPos, PList[n]) ? PList.IndexOf(p) : n);

            return n;
        }
 /// <summary>   Distances. </summary>
 ///
 ///
 /// <param name="p1" type="D3DXVECTOR2">    The first D3DXVECTOR2. </param>
 /// <param name="p2" type="D3DXVECTOR2">    The second D3DXVECTOR2. </param>
 ///
 /// <returns>   A float. </returns>
 private float Distance(D3DXVECTOR2 p1, D3DXVECTOR2 p2)
 {
     return Distance(p1.y, p1.x, p2.y, p2.x);
 }
 /// <summary>   Distances. </summary>
 ///
 ///
 /// <param name="p1" type="Player2DPos">    . </param>
 /// <param name="p2" type="D3DXVECTOR2">    The second D3DXVECTOR2. </param>
 ///
 /// <returns>   A float. </returns>
 ///
 /// ### <param name="p1" type="D3DXVECTOR2">    The first D3DXVECTOR2. </param>
 private float Distance(Player2DPos p1, D3DXVECTOR2 p2)
 {
     return Distance(p1.y, p1.x, p2.y, p2.x);
 }
        public void Walk(float x, float y, bool KeepWalking)
        {
            if (!_mut2.WaitOne(1000))
            {
                Console.WriteLine("Asynchronous action taken on synchronic function.");
                goto CleanUp;
            }

            HaltFlag = false;
            float heading;
            float tobeHeading;
            var waypoint = new D3DXVECTOR2(x, y);
            if (CorDelay)
                Bolter.GlobalInterface.GlobalMovement.Status = WalkingStatus.Standing;

            RotateCharacter(waypoint);

            if (CamReset)
                Bolter.GlobalInterface.GlobalInput.SendKeyPress(KeyStates.Toggled, Key.End);

            if (CorDelay)
                Thread.Sleep(HeadToll);

            var decX = (Funcs.GetPOS(EntityType.PCMob, Axis.X, 0) > x);

            Bolter.GlobalInterface.GlobalMovement.Status = WalkingStatus.Autorun | WalkingStatus.Running;

            if (decX)
            {
                while (Funcs.GetPOS(EntityType.PCMob, Axis.X, 0) > x)
                {
                    if (HaltFlag)
                    {
                        Bolter.GlobalInterface.GlobalMovement.Status = WalkingStatus.Standing;
                        goto CleanUp;
                    }
                    if (AICorrection)
                    {
                        heading = Funcs.GetHeading(EntityType.PCMob, 0);

                        tobeHeading = HeadingToRad(Funcs.GetPOS(EntityType.PCMob, Axis.X, 0), Funcs.GetPOS(EntityType.PCMob, Axis.Y, 0), waypoint);

                        // Check if our heading is within our tolerance.
                        if (tobeHeading - heading < 0 ? tobeHeading - heading < -0.1f : tobeHeading - heading > 0.1f)
                            RotateCharacter(waypoint);
                    }
                    Thread.Sleep(10);
                }
            }
            else
            {
                while (Funcs.GetPOS(EntityType.PCMob, Axis.X, 0) < x)
                {
                    if (HaltFlag)
                    {
                        Bolter.GlobalInterface.GlobalMovement.Status = WalkingStatus.Standing;
                        goto CleanUp;
                    }
                    if (AICorrection)
                    {
                        heading = Funcs.GetHeading(EntityType.PCMob, 0);
                        tobeHeading = HeadingToRad(Funcs.GetPOS(EntityType.PCMob, Axis.X, 0), Funcs.GetPOS(EntityType.PCMob, Axis.Y, 0), waypoint);

                        // Check if our heading is within our tolerance.
                        if (tobeHeading - heading < 0 ? tobeHeading - heading < -0.1f : tobeHeading - heading > 0.1f)
                            RotateCharacter(waypoint);
                    }
                    Thread.Sleep(10);
                }
            }
            if (!KeepWalking)
                Bolter.GlobalInterface.GlobalMovement.Status = WalkingStatus.Standing;
            CleanUp:
            _mut2.ReleaseMutex();
        }
        public IList FindBestPath(IList pathNames, D3DXVECTOR2 desiredPOS, out string name)
        {
            var changethis = Funcs.Get2DPos();
            var currentPOS = new D3DXVECTOR2(changethis.x,changethis.y);
            var zoneName = Funcs.GetZoneName();
            var walkablePaths = (from path in pathNames as List<string>
                                 where !string.IsNullOrEmpty(path)
                                 select _Waypoints.Zone.First(p => p.Name == zoneName).Path.First(i => i.Name == path)).ToList();

            var shortist = float.MaxValue;
            var returnPath = walkablePaths.First().Point;
            var retName = "";
            foreach (var path in walkablePaths)
            {
                var MyPOSIndex = GetClosestIndex(path.Point, currentPOS);
                var DesiredPOSIndex = GetClosestIndex(path.Point, desiredPOS);
                var overallDistance = 0f;
                if (MyPOSIndex < DesiredPOSIndex)
                {
                    var truncedPath = path.Point.Skip(MyPOSIndex).Take(DesiredPOSIndex - MyPOSIndex).ToList();
                    var counter1 = 0;
                    foreach (var dxvector2 in truncedPath)
                    {
                        counter1++;
                        if (counter1 == truncedPath.Count) break;
                        overallDistance += Distance(dxvector2, truncedPath.ElementAt(counter1));
                    }
                }
                else
                {
                    var truncedPath = path.Point.Skip(0).Reverse().Skip(MyPOSIndex).Take(MyPOSIndex - DesiredPOSIndex).ToList();
                    var counter1 = 0;
                    foreach (var dxvector2 in truncedPath)
                    {
                        counter1++;
                        if (counter1 == truncedPath.Count) break;
                        overallDistance += Distance(dxvector2, truncedPath.ElementAt(counter1));
                    }
                }
                if (!(overallDistance < shortist)) continue;
                returnPath = path.Point;
                shortist = overallDistance;
                retName = path.Name;
            }
            name = retName;
            return returnPath;
        }