/// <summary>
        /// Run a given path, represented as list of Tile (see PathFindig.GetPath).
        /// </summary>
        /// <param name="path">List of coordinates as Tile objects.</param>
        /// <param name="timeout">Maximum amount of time to run the path. (default: -1, no limit)</param>
        /// <param name="debugMessage">Outputs a debug message.</param>
        /// <param name="useResync">ReSyncs the path calculation.</param>
        /// <returns>True: if it finish the path in time. False: otherwise</returns>
        public static bool RunPath(List <Tile> path, float timeout = -1, bool debugMessage = false, bool useResync = true)
        {
            if (path == null)
            {
                return(false);
            }
            DateTime timeStart, timeEnd;

            timeStart = DateTime.Now;
            timeEnd   = (timeout < 0) ? timeStart.AddDays(1) : timeStart.AddSeconds(timeout);

            Tile dst = path.Last();

            foreach (Tile step in path)
            {
                if (Player.Position.X == dst.X && Player.Position.Y == dst.Y)
                {
                    Misc.SendMessage("PathFind: Destination reached", 66);
                    return(true);
                }
                bool walkok = false;
                if (step.X > Player.Position.X && step.Y == Player.Position.Y) //East
                {
                    Rotate(Direction.east, debugMessage);
                    walkok = Run(Direction.east, debugMessage);
                }
                else if (step.X < Player.Position.X && step.Y == Player.Position.Y) // West
                {
                    Rotate(Direction.west, debugMessage);
                    walkok = Run(Direction.west, debugMessage);
                }
                else if (step.X == Player.Position.X && step.Y < Player.Position.Y) //North
                {
                    Rotate(Direction.north, debugMessage);
                    walkok = Run(Direction.north, debugMessage);
                }
                else if (step.X == Player.Position.X && step.Y > Player.Position.Y) //South
                {
                    Rotate(Direction.south, debugMessage);
                    walkok = Run(Direction.south, debugMessage);
                }
                else if (step.X > Player.Position.X && step.Y > Player.Position.Y) //Down
                {
                    Rotate(Direction.down, debugMessage);
                    walkok = Run(Direction.down, debugMessage);
                }
                else if (step.X < Player.Position.X && step.Y < Player.Position.Y) //UP
                {
                    Rotate(Direction.up, debugMessage);
                    walkok = Run(Direction.up, debugMessage);
                }
                else if (step.X > Player.Position.X && step.Y < Player.Position.Y) //Right
                {
                    Rotate(Direction.right, debugMessage);
                    walkok = Run(Direction.right, debugMessage);
                }
                else if (step.X < Player.Position.X && step.Y > Player.Position.Y) //Left
                {
                    Rotate(Direction.left, debugMessage);
                    walkok = Run(Direction.left, debugMessage);
                }
                else if (Player.Position.X == step.X && Player.Position.Y == step.Y) // no action
                {
                    walkok = true;
                }

                if (timeout >= 0 && DateTime.Now.CompareTo(timeEnd) > 0)
                {
                    if (debugMessage)
                    {
                        Misc.SendMessage("PathFind: RunPath run TIMEOUT", 33);
                    }
                    return(false);
                }

                if (!walkok)
                {
                    if (debugMessage)
                    {
                        Misc.SendMessage("PathFind: Move action FAIL", 33);
                    }

                    if (useResync)
                    {
                        Misc.Resync();
                        Misc.Pause(200);
                    }

                    return(false);
                }
                else
                {
                    if (debugMessage)
                    {
                        Misc.SendMessage("PathFind: Move action OK", 66);
                    }
                }
            }

            if (Player.Position.X == dst.X && Player.Position.Y == dst.Y)
            {
                Misc.SendMessage("PathFind: Destination reached", 66);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 2
0
        private static bool Engine(Route r)
        {
            List <Tile> road = PathMove.GetPath(r.X, r.Y, r.IgnoreMobile);

            if (road == null) // No way to destination
            {
                Misc.SendMessage("PathFind: Destination not valid", 33);
                return(false);
            }

            foreach (Tile step in road)
            {
                if (Player.Position.X == r.X && Player.Position.Y == r.Y)
                {
                    Misc.SendMessage("PathFind: Destination reached", 66);
                    return(true);
                }
                bool walkok = false;
                if (step.X > Player.Position.X && step.Y == Player.Position.Y) //East
                {
                    Rotate(Direction.East, r.DebugMessage);
                    walkok = Run(Direction.East, r.DebugMessage);
                }
                else if (step.X < Player.Position.X && step.Y == Player.Position.Y) // West
                {
                    Rotate(Direction.West, r.DebugMessage);
                    walkok = Run(Direction.West, r.DebugMessage);
                }
                else if (step.X == Player.Position.X && step.Y < Player.Position.Y) //North
                {
                    Rotate(Direction.North, r.DebugMessage);
                    walkok = Run(Direction.North, r.DebugMessage);
                }
                else if (step.X == Player.Position.X && step.Y > Player.Position.Y) //South
                {
                    Rotate(Direction.South, r.DebugMessage);
                    walkok = Run(Direction.South, r.DebugMessage);
                }
                else if (step.X > Player.Position.X && step.Y > Player.Position.Y) //Down
                {
                    Rotate(Direction.Down, r.DebugMessage);
                    walkok = Run(Direction.Down, r.DebugMessage);
                }
                else if (step.X < Player.Position.X && step.Y < Player.Position.Y) //UP
                {
                    Rotate(Direction.Up, r.DebugMessage);
                    walkok = Run(Direction.Up, r.DebugMessage);
                }
                else if (step.X > Player.Position.X && step.Y < Player.Position.Y) //Right
                {
                    Rotate(Direction.Right, r.DebugMessage);
                    walkok = Run(Direction.Right, r.DebugMessage);
                }
                else if (step.X < Player.Position.X && step.Y > Player.Position.Y) //Left
                {
                    Rotate(Direction.Left, r.DebugMessage);
                    walkok = Run(Direction.Left, r.DebugMessage);
                }
                else if (Player.Position.X == step.X && Player.Position.Y == step.Y) // no action
                {
                    walkok = true;
                }

                if (!walkok)
                {
                    if (r.DebugMessage)
                    {
                        Misc.SendMessage("PathFind: Move action FAIL", 33);
                    }

                    if (r.UseResync)
                    {
                        Misc.Resync();
                        Misc.Pause(200);
                    }

                    return(false);
                }
                else
                {
                    if (r.DebugMessage)
                    {
                        Misc.SendMessage("PathFind: Move action OK", 66);
                    }
                }
            }

            if (Player.Position.X == r.X && Player.Position.Y == r.Y)
            {
                Misc.SendMessage("PathFind: Destination reached", 66);
                return(true);
            }
            else
            {
                Go(r);
                return(false);
            }
        }