FindPath() public method

public FindPath ( Cell, startCell, Cell, goalCell, List map, bool targetCellMustBeFree ) : void
startCell Cell,
goalCell Cell,
map List
targetCellMustBeFree bool
return void
Example #1
0
        public void MoveToNode(IGraphNode node, int movementLimit = -1)
        {
            // Find the shortest route to the destination node, and start moving towards it.
            var pathfinder = new AStarPathfinder();

            path = pathfinder.FindPath(GetTravelingNode(), node);

            if (OnStartMove != null)
            {
                OnStartMove.Invoke();
            }

            if (movementLimit > -1 && path.nodes.Count > movementLimit)
            {
                path.nodes.RemoveRange(movementLimit, path.nodes.Count - movementLimit);
            }


            if (path != null)
            {
                pathEnumerator = path.nodes.GetEnumerator();
                pathEnumerator.MoveNext();
                SetTargetNode(pathEnumerator.Current, false);
            }
        }
Example #2
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            MapCreationScript.Instance.ClearWorld();

            Vector2Int startPos  = new Vector2Int((int)Random.Range(0, 49), (int)Random.Range(0, 49));
            Vector2Int targetPos = new Vector2Int((int)Random.Range(0, 49), (int)Random.Range(0, 49));

            Cell startCell  = world.map.GetCellAt(startPos);
            Cell targetCell = world.map.GetCellAt(targetPos);

            AStarPathfinder aPathfinder = new AStarPathfinder(world.map);

            bool result = aPathfinder.FindPath(startCell, targetCell);

            foreach (ANode node in aPathfinder.visited)
            {
                world.map.GetCellAt(new Vector2Int(node.x, node.y)).GetGameObject().GetComponent <SpriteRenderer>().color = Color.black;
            }

            foreach (ANode node in aPathfinder.path)
            {
                world.map.GetCellAt(new Vector2Int(node.x, node.y)).GetGameObject().GetComponent <SpriteRenderer>().color = Color.red;
            }
        }
    }
Example #3
0
    public static List <Cell> PathToTarget(this GameMap gm, Cell origin, Cell goal)
    {
        var pathFinder = new AStarPathfinder();

        pathFinder.FindPath(origin, goal, gm.CellGameMap, false);
        return(pathFinder.CellsFromPath());
    }
Example #4
0
    // Use this for initialization
    private void Start()
    {
        var a = new Node("A");
        var b = new Node("B");
        var c = new Node("C");
        var d = new Node("D");
        var e = new Node("E");
        var f = new Node("F");

        a.AddOutgoingEdge(new Edge(a, b, 4));
        a.AddOutgoingEdge(new Edge(a, c, 2));
        a.AddOutgoingEdge(new Edge(a, f, 50));
        b.AddOutgoingEdge(new Edge(b, c, 5));
        b.AddOutgoingEdge(new Edge(b, d, 10));
        c.AddOutgoingEdge(new Edge(c, e, 3));
        e.AddOutgoingEdge(new Edge(e, d, 4));
        d.AddOutgoingEdge(new Edge(d, f, 11));

        var pathfinder = new AStarPathfinder <Node>(Heuristic);
        var timer      = new StopwatchExecutionTimer();
        var path       = pathfinder.FindPath(a, f);

        print(string.Format("In {1} seconds found the following path with cost {0} from A to F:", path.Cost, timer.ElapsedSeconds));
        print(path.Edges.Aggregate("", (soFar, edge) => soFar + (soFar.Length > 0 ? " -> " : "") + edge));
    }
Example #5
0
        private static void DifficultyExample()
        {
            Console.Clear();
            Console.Out.WriteLine("Example with movement penalties in map.");
            WriteLegend();

            var map = new MapSample();

            for (var y = 0; y < map.Height / 3; y++)
            {
                map[map.Width / 3, y].Difficulty = 9;
                map[map.Width / 3, map.Height - 1 - y].Difficulty = 9;
                map[2 * map.Width / 3, y].Difficulty = 3;
                map[2 * map.Width / 3, map.Height - 1 - y].Difficulty = 3;
            }
            Console.Out.Write(map.ToString());
            var pathFinder = new AStarPathfinder(map, 1000, false, new ManhattanDistance());
            var from       = new PointInt32(0, 0);
            var to         = new PointInt32(map.Width - 1, map.Height - 1);

            Console.Out.WriteLine("Path from {0} to {1}.", from, to);
            var path = pathFinder.FindPath(null, from, to);

            Console.Out.Write(map.ToString(path, true));
            Console.Out.WriteLine("Press any key to continue ...");
            Console.In.ReadLine();
        }
Example #6
0
        public IPath FindPath(Location goal)
        {
            AbstractPathfinder algorithm = new AStarPathfinder();

            _path = algorithm.FindPath(_grid, _position, goal);

            return(_path);
        }
Example #7
0
        public void Test1()
        {
            var grid       = new AStarGrid(CreateMap(5, 5));
            var pathfinder = new AStarPathfinder(grid);

            List <Vector2> path = null;

            path = pathfinder.FindPath(0, 0, 1, 1);
            Assert.AreEqual(1, path.Count);

            path = pathfinder.FindPath(0, 0, 2, 2);
            Assert.AreEqual(2, path.Count);

            path = pathfinder.FindPath(0, 0, 2, 1);
            Assert.AreEqual(2, path.Count);

            path = pathfinder.FindPath(0, 0, 3, 2);
            Assert.AreEqual(3, path.Count);
        }
Example #8
0
    public void StartAStarPathfinder()
    {
        StopAllCoroutines();
        grid.StopAllCoroutines();


        StartCoroutine
            (aStar.FindPath(grid.WorldPointToNode(pathfinderObject.transform.position),
                            grid.WorldPointToNode(targetNode.transform.position), s.value));
    }
        public void Day15Part1()
        {
            var repair           = new OxygenRepairDroid(UserMode.LeastExplored);
            var result           = repair.Repair();
            var pathfinder       = new AStarPathfinder();
            var path             = pathfinder.FindPath(result);
            var numberOfCommands = path.Count + 1;

            numberOfCommands.Should().Be(238);
        }
Example #10
0
 public void SetPath(GameObject target)
 {
     if (target != m_target)
     {
         m_target = target;
         List <AstarNode> pathlist = m_finder.FindPath(transform.position, m_target.transform.position);
         foreach (AstarNode node in pathlist)
         {
             m_path.Enqueue(node);
         }
         currentTarget = GetNextNode();
     }
 }
Example #11
0
        private static void SimpleExample()
        {
            Console.Clear();
            Console.Out.WriteLine("Simple example - empty map.");
            WriteLegend();

            var map = new MapSample();

            Console.Out.Write(map.ToString());
            var pathFinder = new AStarPathfinder(map, 1000, false, new ManhattanDistance());
            var from       = new PointInt32(0, 0);
            var to         = new PointInt32(map.Width - 1, map.Height - 1);

            Console.Out.WriteLine("Path from {0} to {1}.", from, to);
            var path = pathFinder.FindPath(null, from, to);

            Console.Out.Write(map.ToString(path, true));
            Console.Out.WriteLine("Press any key to continue ...");
            Console.In.ReadLine();
        }
Example #12
0
 public List <NavTile> FindPath(NavTile start, NavTile goal)
 {
     return(pathfinder.FindPath(start, goal));
 }
    public void CreatePathways(List <RectRoom> rooms)
    {
        if (rooms.Count <= 1)
        {
            return;
        }

        for (int i = 0; i < rooms.Count - 1; i++)
        {
            AStarPathfinder pathfinder = new AStarPathfinder(GameManagerScript.Instance.world.map);


            RectRoom startRoom = rooms[i];
            RectRoom nextRoom  = rooms[i + 1];

            List <Cell> listOfRoomCellsStart = startRoom.GetListOfRoomCells();
            Cell        startCell            = listOfRoomCellsStart[Random.Range(0, listOfRoomCellsStart.Count - 1)];

            List <Cell> listOfRoomCellsNext = nextRoom.GetListOfRoomCells();
            Cell        nextCell            = listOfRoomCellsNext[Random.Range(0, listOfRoomCellsNext.Count - 1)];

            bool b = pathfinder.FindPath(startCell, nextCell);

            if (!b)
            {
                continue;
            }

            for (int j = 0; j < pathfinder.path.Count - 1; j++)
            {
                Cell node     = GameManagerScript.Instance.world.map.GetCellAt(new Vector2Int(pathfinder.path[j].x, pathfinder.path[j].y));
                Cell nextNode = GameManagerScript.Instance.world.map.GetCellAt(new Vector2Int(pathfinder.path[j + 1].x, pathfinder.path[j + 1].y));

                Vector2Int position1 = node.GetPosition();
                Vector2Int position2 = nextNode.GetPosition();

                if (listOfRoomCellsStart.Contains(node) || listOfRoomCellsNext.Contains(node))
                {
                    continue;
                }

                GameManagerScript.Instance.world.map.GetCellAt(position1).GetGameObject().GetComponent <SpriteRenderer>().color = floorColor;

                Vector2Int dif = position1 - position2;

                if (dif == new Vector2Int(0, 1) || dif == new Vector2Int(0, -1))
                {
                    Vector2Int leftNode  = new Vector2Int(position1.x - 1, position1.y);
                    Vector2Int rightNode = new Vector2Int(position1.x + 1, position1.y);

                    if (GameManagerScript.Instance.world.map.IsPositionViable(leftNode))
                    {
                        GameManagerScript.Instance.world.map.GetCellAt(new Vector2Int(leftNode.x, leftNode.y)).GetGameObject().GetComponent <SpriteRenderer>().color = wallColor;
                        GameManagerScript.Instance.world.map.GetCellAt(new Vector2Int(leftNode.x, leftNode.y)).SetTerrainType(Cell.TERRAIN_TYPE.wall);
                    }

                    if (GameManagerScript.Instance.world.map.IsPositionViable(rightNode))
                    {
                        GameManagerScript.Instance.world.map.GetCellAt(new Vector2Int(rightNode.x, rightNode.y)).GetGameObject().GetComponent <SpriteRenderer>().color = wallColor;
                        GameManagerScript.Instance.world.map.GetCellAt(new Vector2Int(rightNode.x, rightNode.y)).SetTerrainType(Cell.TERRAIN_TYPE.wall);
                    }
                }
            }
        }
    }
 public static List<Cell> PathToTarget(this GameMap gm, Cell origin, Cell goal)
 {
     var pathFinder = new AStarPathfinder();
     pathFinder.FindPath(origin, goal, gm.CellGameMap, false);
     return pathFinder.CellsFromPath();
 }