Exemple #1
0
        public IEnumerable <MetaCompleteSquare> FullPath()
        {
            for (int i = 0; i < path.Count; ++i)
            {
                CompleteSquare square = path[i];
                yield return(new MetaCompleteSquare(square, square.X, square.Y));
            }

            /*int x = startingX, y = startingY;
             * int lastX = -1, lastY = -1; // for quick checking.
             * int destinationTileX = destinationX;
             * int destinationTileY = destinationY;
             * List<CompleteSquare> closedList = new List<CompleteSquare>();
             *
             * while (x != destinationTileX && y != destinationTileY)
             * {
             *  bool delivered = false;
             *  for (int a = x - 1; a < x + 1; ++a)
             *  {
             *      for (int b = y - 1; b < y + 1; ++b)
             *      {
             *          if (_squares[a, b].IsPath)
             *          {
             *              if (!closedList.Contains(_squares[a, b]))
             *              {
             *                  lastX = a;
             *                  lastY = b;
             *                  closedList.Add(_squares[a, b]);
             *                  delivered = true;
             *
             *                  yield return new MetaCompleteSquare(_squares[a, b], a, b);
             *              }
             *          }
             *      }
             *  }
             *
             *  if (!delivered)
             *  {
             *      throw new Exception("No path to destination.");
             *  }
             *
             *  x = lastX;
             *  y = lastY;
             * }*/
        }
Exemple #2
0
        public void Pathfind(Map map, Player player, int destinationX, int destinationY)
        {
            /*
             *
             * Find path from hero to monster. First, get coordinates
             * of hero.
             *
             * */
            Point startingPoint = new Point((int)player.Position.x / 64, -(int)player.Position.y / 64);

            startingX         = startingPoint.X;
            startingY         = startingPoint.Y;
            this.destinationX = destinationX / 64;
            this.destinationY = (destinationY / 64);

            // Initialize path.
            for (int y = 0; y < map.Height; ++y)
            {
                for (int x = 0; x < map.Width; ++x)
                {
                    SquareContent content = SquareContent.Empty;
                    Tile          tile    = map.GetTile(x, y);
                    if (!tile.IsPassable)
                    {
                        content = SquareContent.Wall;
                    }
                    else if (x == startingX && y == startingY)
                    {
                        content = SquareContent.Starting;
                    }

                    _squares[x, y] = new CompleteSquare()
                    {
                        X             = -1, Y = -1,
                        IsPath        = false,
                        ContentCode   = content,
                        DistanceSteps = 10000
                    };
                }
            }

            /*
             *
             * Hero starts at distance of 0.
             *
             * */
            _squares[startingPoint.X, startingPoint.Y].DistanceSteps = 0;

            while (true)
            {
                bool madeProgress = false;

                /*
                 *
                 * Look at each square on the board.
                 *
                 * */
                foreach (Point mainPoint in AllSquares())
                {
                    int x = mainPoint.X;
                    int y = mainPoint.Y;

                    /*
                     *
                     * If the square is open, look through valid moves given
                     * the coordinates of that square.
                     *
                     * */
                    Tile relevantTile = map.GetTile(x, y);
                    if (relevantTile.IsPassable)
                    {
                        int passHere = _squares[x, y].DistanceSteps;

                        foreach (Point movePoint in ValidMoves(x, y))
                        {
                            int newX    = movePoint.X;
                            int newY    = movePoint.Y;
                            int newPass = passHere + 1;

                            if (_squares[newX, newY].DistanceSteps > newPass)
                            {
                                _squares[newX, newY].DistanceSteps = newPass;
                                madeProgress = true;
                            }
                        }
                    }
                }
                if (!madeProgress)
                {
                    break;
                }
            }

            HighlightPath();
        }
Exemple #3
0
 public MetaCompleteSquare(CompleteSquare _square, int x, int y)
 {
     Square = _square;
     X      = x;
     Y      = y;
 }
Exemple #4
0
 public MetaCompleteSquare(CompleteSquare _square, int x, int y)
 {
     Square = _square;
     X = x;
     Y = y;
 }
        public void Pathfind(Map map, Player player, int destinationX, int destinationY)
        {
            /*
             *
             * Find path from hero to monster. First, get coordinates
             * of hero.
             *
             * */
            Point startingPoint = new Point((int)player.Position.x / 64, -(int)player.Position.y / 64);
            startingX = startingPoint.X;
            startingY = startingPoint.Y;
            this.destinationX = destinationX / 64;
            this.destinationY = (destinationY / 64);

            // Initialize path.
            for (int y = 0; y < map.Height; ++y)
            {
                for (int x = 0; x < map.Width; ++x)
                {
                    SquareContent content = SquareContent.Empty;
                    Tile tile = map.GetTile(x, y);
                    if (!tile.IsPassable)
                    {
                        content = SquareContent.Wall;
                    }
                    else if (x == startingX && y == startingY)
                    {
                        content = SquareContent.Starting;
                    }

                    _squares[x, y] = new CompleteSquare()
                    {
                        X = -1, Y = -1,
                        IsPath = false,
                        ContentCode = content,
                        DistanceSteps = 10000
                    };
                }
            }

            /*
             *
             * Hero starts at distance of 0.
             *
             * */
            _squares[startingPoint.X, startingPoint.Y].DistanceSteps = 0;

            while (true)
            {
                bool madeProgress = false;

                /*
                 *
                 * Look at each square on the board.
                 *
                 * */
                foreach (Point mainPoint in AllSquares())
                {
                    int x = mainPoint.X;
                    int y = mainPoint.Y;

                    /*
                     *
                     * If the square is open, look through valid moves given
                     * the coordinates of that square.
                     *
                     * */
                    Tile relevantTile = map.GetTile(x, y);
                    if (relevantTile.IsPassable)
                    {
                        int passHere = _squares[x, y].DistanceSteps;

                        foreach (Point movePoint in ValidMoves(x, y))
                        {
                            int newX = movePoint.X;
                            int newY = movePoint.Y;
                            int newPass = passHere + 1;

                            if (_squares[newX, newY].DistanceSteps > newPass)
                            {
                                _squares[newX, newY].DistanceSteps = newPass;
                                madeProgress = true;
                            }
                        }
                    }
                }
                if (!madeProgress)
                {
                    break;
                }
            }

            HighlightPath();
        }