예제 #1
0
    //---- || Breath first search method ||----\\

    public void BreathFirstSearch(Vector3 startPos, Vector3 goalPos)
    {
        Stopwatch bsw = new Stopwatch();

        bsw.Start();

        List <Node> openList = new List <Node>();

        closedList = new HashSet <Node>();

        Node currentNode;
        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(goalPos);

        openList.Add(startNode);

        if (startNode.walkable && targetNode.walkable)
        {
            while (openList.Count > 0)
            {
                currentNode = openList[0];
                openList.Remove(currentNode);
                closedList.Add(currentNode);
                if (currentNode.Equals(targetNode))
                {
                    bsw.Stop();

                    print("Path found in: " + bsw.ElapsedMilliseconds + " ms");
                    BreathfirstMilliseconds = bsw.ElapsedMilliseconds;

                    grid.BFSclosedList = closedList;
                    RetracePath(startNode, targetNode);

                    return;
                }

                foreach (Node i in grid.GetNeighbours(currentNode))
                {
                    if (!i.walkable || closedList.Contains(i))
                    {
                        continue;
                    }


                    if (!openList.Contains(i))
                    {
                        i.parent = currentNode;
                        openList.Add(i);
                    }
                }
            }
        }
    }
예제 #2
0
    //--- || Heap coroutine ||---\\

    IEnumerator FindPathHeap(Vector3 startPos, Vector3 targetPos)
    {
        Stopwatch swHeap = new Stopwatch();

        swHeap.Start();
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;
        Node      startNode   = grid.NodeFromWorldPoint(startPos);
        Node      targetNode  = grid.NodeFromWorldPoint(targetPos);

        if (startNode.walkable && targetNode.walkable)
        {
            openSet   = new Heap <Node> (grid.MaxSize);
            closedSet = new HashSet <Node> ();
            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                Node currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);
                grid.closedSet = closedSet;

                if (currentNode == targetNode)
                {
                    swHeap.Stop();
                    print("Path found in: " + swHeap.ElapsedMilliseconds + " ms");
                    AStarMilliseconds = swHeap.ElapsedMilliseconds;
                    pathSuccess       = true;
                    break;
                }

                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if (!neighbour.walkable || closedSet.Contains(neighbour))
                    {
                        continue;
                    }

                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);

                    if (newMovementCostToNeighbour < currentNode.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = newMovementCostToNeighbour;
                        neighbour.hCost  = GetDistance(neighbour, targetNode);
                        neighbour.parent = currentNode;
                        openSet.Add(neighbour);

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbour);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
    }