Ejemplo n.º 1
0
    public void FindPath(PathRequest request, Action <PathResult> callback)
    {
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        Node startNode  = grid.NodeFromWorldPoint(request.pathStart);
        Node targetNode = grid.NodeFromWorldPoint(request.pathEnd);

        startNode.parent = startNode;         // NEW!!!!!

        //if (startNode == null || targetNode == null) Debug.LogWarning("Node does not exist");

        Heap <Node>    openSet   = new Heap <Node>(grid.MaxSize); // Set to be evaluated
        HashSet <Node> closedSet = new HashSet <Node>();          // Set already evaluated

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            Node current = openSet.RemoveFirst();
            closedSet.Add(current);
            if (current == targetNode)
            {
                pathSuccess = true;
                break;                 // We finish
            }
            List <Node> neighbours = grid.GetNeighbours(current);
            foreach (Node neighbour in neighbours)
            {
                if (closedSet.Contains(neighbour))
                {
                    continue;
                }
                // If new path to neighbour is shorter OR neighbour not in openSet
                int newDist = current.gCost + GetDistance(current, neighbour);
                if (newDist < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newDist;
                    neighbour.hCost  = GetDistance(neighbour, targetNode);
                    neighbour.parent = current;
                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                    else
                    {
                        openSet.UpdateItem(neighbour);                      // NEW!!!!!
                    }
                }
            }
        }
        if (pathSuccess)
        {
            waypoints   = RetracePath(startNode, targetNode);
            pathSuccess = waypoints.Length > 0;
        }
        callback(new PathResult(waypoints, pathSuccess, request.callback));
    }
Ejemplo n.º 2
0
    void FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        Heap <Node>    openSet   = new Heap <Node>(grid.MaxSize);
        HashSet <Node> closedSet = new HashSet <Node>();

        openSet.Add(startNode);

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

            if (currentNode == targetNode)
            {
                RetracePath(startNode, targetNode);
                return;
            }

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

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

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                    else
                    {
                        openSet.UpdateItem(neighbour);
                    }
                }
            }
        }
    }
Ejemplo n.º 3
0
    IEnumerator FindPath(Vector3 _start, Vector3 _end)
    {
        //if they're on the same node, then return the nodes position

        Vector3[] path        = new Vector3[0];
        bool      pathSuccess = false;

        PathNode start  = tileGrid.GetTileAtPos(_start).GetComponent <PathNode>();
        PathNode target = tileGrid.GetTileAtPos(_end).GetComponent <PathNode>();

        if (!start || !target)
        {
            yield return(null);
        }

        if (start != target)
        {
            //list of open and closed nodes
            openNodes   = new Heap <PathNode>(tileGrid.xSize * tileGrid.ySize);
            closedNodes = new HashSet <PathNode>();

            openNodes.Add(start);

            int x = 0;
            while (openNodes.Count > 0)
            {
                currentNode = openNodes.RemoveFirst();
                closedNodes.Add(currentNode);

                if (currentNode == target)
                {
                    pathSuccess = true;
                    break;
                }

                foreach (PathNode n in tileGrid.GetNeighbours(currentNode))
                {
                    if (closedNodes.Contains(n))
                    {
                        continue;
                    }
                    if (!n.walkable)
                    {
                        continue;
                    }

                    int f = currentNode.g + GetDistance(currentNode, n) + n.weight;
                    if (f < n.g || !openNodes.Contains(n))
                    {
                        n.g          = f;
                        n.h          = GetDistance(n, target);
                        n.ParentNode = currentNode;

                        if (!openNodes.Contains(n))
                        {
                            openNodes.Add(n);
                        }
                    }
                    x++;
                }

                if (closedNodes.Count >= maxSize - 1)
                {
                    currentNode = null;
                    break;
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            path = RetracePath(start, target);
        }
        requestManager.FinishedProcessing(path, pathSuccess);
    }