Beispiel #1
0
        public PathFinder(IMap map, double diagonalCost)
        {
            _map   = map ?? throw new ArgumentNullException("map", "Map cannot be null");
            _graph = new EdgeWeightedDigraph(_map.Width * _map.Height);

            foreach (Cell cell in _map.GetAllCells())
            {
                if (cell.IsWalkable)
                {
                    int v = IndexFor(cell.Point);
                    foreach (Cell neighbor in _map.GetBorderCellsInSquare(cell.Point, 1))
                    {
                        if (neighbor.IsWalkable)
                        {
                            int w = IndexFor(neighbor.Point);
                            if (neighbor.Point.X != cell.Point.X && neighbor.Point.Y != cell.Point.Y)
                            {
                                _graph.AddEdge(new DirectedEdge(v, w, diagonalCost));
                                _graph.AddEdge(new DirectedEdge(w, v, diagonalCost));
                            }
                            else
                            {
                                _graph.AddEdge(new DirectedEdge(v, w, 1.0));
                                _graph.AddEdge(new DirectedEdge(w, v, 1.0));
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public PathFinder(IMap map)
        {
            _map   = map ?? throw new ArgumentNullException("map", "Map cannot be null");
            _graph = new EdgeWeightedDigraph(_map.Width * _map.Height);

            foreach (Cell cell in _map.GetAllCells())
            {
                if (cell.IsWalkable)
                {
                    int v = IndexFor(cell.Point);
                    foreach (Cell neighbor in _map.GetBorderCellsInDiamond(cell.Point, 1))
                    {
                        if (neighbor.IsWalkable)
                        {
                            int w = IndexFor(neighbor.Point);
                            _graph.AddEdge(new DirectedEdge(v, w, 1.0));
                            _graph.AddEdge(new DirectedEdge(w, v, 1.0));
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public PathFinder(IMap map, double cardinalCost, double diagonalCost)
        {
            _map   = map ?? throw new ArgumentNullException("map");
            _graph = new EdgeWeightedDigraph(_map.Width * _map.Height);

            foreach (Cell cell in _map.GetAllCells())
            {
                if (!cell.IsWalkable)
                {
                    continue;
                }

                int cellIndex      = IndexFor(cell.Point);
                var neighborCoords = new List <(Point, double)>
                {
                    (new Point(cell.Point.X + 0, cell.Point.Y - 1), cardinalCost),
                    (new Point(cell.Point.X + 0, cell.Point.Y + 1), cardinalCost),
                    (new Point(cell.Point.X + 1, cell.Point.Y + 0), cardinalCost),
                    (new Point(cell.Point.X - 1, cell.Point.Y + 0), cardinalCost),
                    (new Point(cell.Point.X - 1, cell.Point.Y - 1), diagonalCost),
                    (new Point(cell.Point.X - 1, cell.Point.Y + 1), diagonalCost),
                    (new Point(cell.Point.X + 1, cell.Point.Y - 1), diagonalCost),
                    (new Point(cell.Point.X + 1, cell.Point.Y + 1), diagonalCost),
                };

                foreach (var(coord, cost) in neighborCoords)
                {
                    if (!map.IsWithinMap(coord))
                    {
                        continue;
                    }
                    if (!map.IsWalkable(coord))
                    {
                        continue;
                    }

                    int neighborIndex = IndexFor(coord);
                    _graph.AddEdge(new DirectedEdge(cellIndex, neighborIndex, cost));
                }
            }
        }