FinishedProcessingPath() public method

public FinishedProcessingPath ( Vector2 path, bool success ) : void
path Vector2
success bool
return void
    public IEnumerator FindPath(Vector2 startPos, Vector2 targetPos)
    {
        Vector2[] waypoints   = new Vector2[0];
        bool      pathSuccess = false;
        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)
            {
                pathSuccess = true;
                waypoints   = RetracePath(startNode, targetNode);
                break;
            }
            foreach (Node neighbor in grid.GetNeighbors(currentNode))
            {
                if (!neighbor.walkable || closedSet.Contains(neighbor))
                {
                    continue;
                }

                int newMovementCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbor);

                if (newMovementCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor))
                {
                    neighbor.gCost  = newMovementCostToNeighbor;
                    neighbor.hCost  = GetDistance(neighbor, targetNode);
                    neighbor.parent = currentNode;

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

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

        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 2
0
    void CreatePath(Node startNode, Node targetNode)
    {
        bool pathSuccess = false;
        int  traffic;
        int  trafficCap;

        openSet   = new Heap <Node>(grid.RoadSet.Count);
        closedSet = new HashSet <Node>();
        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            // remove the smallest fCost from Heap
            Node currentNode = openSet.RemoveFirst();
            closedSet.Add(currentNode);

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

            foreach (Node neighbour in grid.PathGetRoadNeighbours(currentNode))
            {
                if (closedSet.Contains(neighbour))
                {
                    continue;
                }

                lock (currentNode)
                    traffic = currentNode.trafficTimer.TrafficIntensity;
                trafficCap = currentNode.trafficTimer.InitialTrafficIntensity;
                int penalty = 17 - 17 * traffic / (trafficCap + 17 / trafficCap);

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

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


                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                    else
                    {
                        openSet.UpdateItem(neighbour);
                    }
                }
            }
        }
        requestManager.FinishedProcessingPath(ReverseParents(startNode, targetNode), pathSuccess);
    }
Esempio n. 3
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Vector3[] wayppoints  = new Vector3[0];
        bool      pathSuccess = false;

        PathfindingGrid.Node startNode  = grid.NodeFromWorldPoint(startPos);
        PathfindingGrid.Node targetNode = grid.NodeFromWorldPoint(targetPos);

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

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

                closedSet.Add(currentNode);

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

                foreach (PathfindingGrid.Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if (!neighbour.walkable || closedSet.Contains(neighbour))
                    {
                        continue;
                    }
                    int newMovementCostToNeighbour = currentNode.gCost + Distance(currentNode, neighbour);

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

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

        if (pathSuccess)
        {
            wayppoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(wayppoints, pathSuccess);
    }
Esempio n. 4
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

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

        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        if (startNode.isWalkable && targetNode.isWalkable)
        {
            Queue <Node>   openSet   = new Queue <Node>();
            HashSet <Node> closedSet = new HashSet <Node>();
            openSet.Enqueue(startNode);

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

                if (currentNode == targetNode)
                {
                    sw.Stop();
                    print("path found in " + sw.ElapsedMilliseconds + "ms");
                    pathSuccess = true;
                    break;
                }

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

                    neighbour.parent = currentNode;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Enqueue(neighbour);
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
    void FindPath(Tile startPos, Tile targetPos)
    {
        Tile[]         waypoints   = new Tile[0];
        bool           pathSuccess = false;
        Heap <Tile>    openSet     = new Heap <Tile>(maxSize);
        HashSet <Tile> closeSet    = new HashSet <Tile>();

        openSet.Add(startPos);
        while (openSet.Count > 0)
        {
            Tile currentTile = openSet.RemoveHead();
            closeSet.Add(currentTile);

            if (currentTile == targetPos)
            {
                pathSuccess = true;
                break;
            }

            foreach (Tile neighbor in currentTile.neighbors)
            {
                if (!closeSet.Contains(neighbor))
                {
                    int newMovementCostToNeighbor = currentTile.gCost + GetDistance(currentTile, neighbor) + neighbor.movementPenalty;

                    if (newMovementCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor))
                    {
                        neighbor.gCost = newMovementCostToNeighbor;
                        neighbor.hCost = GetDistance(neighbor, targetPos);

                        neighbor.Parent = currentTile;

                        if (!openSet.Contains(neighbor))
                        {
                            openSet.Add(neighbor);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbor);
                        }
                    }
                }
            }
        }

        if (pathSuccess)
        {
            waypoints = RetracePath(startPos, targetPos);
        }

        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 6
0
    IEnumerator FindPathRoutine(NodeSideInfo start, NodeSideInfo end)
    {
        bool findSuccess = CalculatePath(start, end);

        NodeSideInfo[] wayPoints = new NodeSideInfo[0];
        {
            yield return(null);

            if (findSuccess)
            {
                wayPoints = RetracePath(start, end);
            }
            _requestManager.FinishedProcessingPath(wayPoints, findSuccess);
        }
    }
Esempio n. 7
0
    public IEnumerator GenerateMovementPath(Vector3 startPos, Vector3 targetPos)
    {
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        Node startNode  = grid.NodeFromWorldPosition(startPos);
        Node targetNode = grid.NodeFromWorldPosition(targetPos);

        if (startNode.walkable && targetNode.walkable && startNode != targetNode)
        {
            pathSuccess = aStar.PathFindingLogic(pathSuccess, startNode, targetNode, unit.currentMovementPoints);
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        else
        {
            Debug.LogError("Path requested was not valid.");
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess, this);
    }
Esempio n. 8
0
    IEnumerator FindingPath(Vector3 startPos, Vector3 targetPos)
    {
        //Stopwatch sw = new Stopwatch();
        //sw.Start();

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

        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        if (startNode.walkable && targetNode.walkable)
        {
            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();
                currentNode.movementPenalty = 0;
                closedSet.Add(currentNode);

                if (currentNode == targetNode)
                {
                    //sw.Stop();
                    //print("Path found:" + sw.ElapsedMilliseconds + " ms");
                    pathSuccess = true;
                    RetracePath(startNode, targetNode);
                    currentNode.movementPenalty = 0;
                    break;
                }

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

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

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbour);
                        }
                    }
                }
            }
            yield return(null);
        }
        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 9
0
    //also takes in the calling unit's PID
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos, bool canFly, int unitPlayerID, HeuristicFunction heuristicFunc)
    {
        waypoints = new Node[0];
        bool pathSuccess = false;

        Node startNode  = gridRef.NodeFromWorldPoint(startPos);
        Node targetNode = gridRef.NodeFromWorldPoint(targetPos);

        Unit currentUnit = startNode.GetUnit();


        HashSet <Node> nodesInBfsRange = GetNodesMinMaxRange(startPos, canFly, minDepthLimit, maxDepthLimit);

        if (startNode.canWalkHere && targetNode.canWalkHere && nodesInBfsRange.Contains(targetNode))
        {
            Heap <Node>    openSet   = new Heap <Node>(gridRef.MaxSize);
            HashSet <Node> closedSet = new HashSet <Node>();
            openSet.Add(startNode);

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

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

                foreach (Node neighbour in gridRef.GetNeighbours(currentNode))
                {
                    bool checkHostile = false;

                    if (neighbour.GetUnit() != null)
                    {
                        if (neighbour.GetUnit().GetUnitPlayerID() != unitPlayerID)
                        {
                            checkHostile = true;
                        }
                    }

                    if ((!canFly && !neighbour.canWalkHere) || closedSet.Contains(neighbour) || checkHostile)
                    {
                        continue;                         //Skips unwalkable nodes when unit cannot fly, or if any node in closed set or if the unit in the node is hostile
                        //considers unwalkable nodes if unit can fly, and ignores any node if in closed set
                    }

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

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                    }
                }
            }
        }

        yield return(null);


        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
            currentUnit.SetMovementSpeedLeft(currentUnit.GetMovementSpeedLeft() - (waypoints.Length - 1));
        }

        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 10
0
    IEnumerator FindPath(Vector3 startPosition, Vector3 targetPosition)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

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

        MapNode startNode  = grid.NodeFromWorldPoint(startPosition);
        MapNode targetNode = grid.NodeFromWorldPoint(targetPosition);

        if (startNode.walkable && targetNode.walkable)
        {
            BinaryHeap <MapNode> openSet   = new BinaryHeap <MapNode>(grid.MaxSize);
            HashSet <MapNode>    closedSet = new HashSet <MapNode>();

            openSet.Add(startNode);
            while (openSet.Count > 0)
            {
                MapNode current = openSet.Pop();
                if (current.parent != null)
                {
                    previousDirection = new Vector2(current.gridX - current.parent.gridX, current.gridY - current.parent.gridY);
                }

                closedSet.Add(current);
                if (current == targetNode)
                {
                    pathSuccess = true;
                    sw.Stop();
                    break;
                }

                foreach (MapNode neighbor in grid.GetNeighbors(current))
                {
                    if (!neighbor.walkable || closedSet.Contains(neighbor))
                    {
                        continue;
                    }

                    Vector2 newDirection = new Vector2(neighbor.gridX - current.gridX, neighbor.gridY - current.gridY);

                    int directionChangePenalty;
                    if (newDirection == previousDirection)
                    {
                        directionChangePenalty = 0;
                    }

                    else if (newDirection.x == previousDirection.x || newDirection.y == previousDirection.y)
                    {
                        directionChangePenalty = lightTurnPenalty;
                    }

                    else
                    {
                        directionChangePenalty = heavyTurnPenalty;
                    }

                    int newMoveCostToNeighbor = current.G + GetDistance(current, neighbor) + neighbor.movementPenalty + directionChangePenalty;
                    if (!openSet.Contains(neighbor) || newMoveCostToNeighbor < neighbor.G)
                    {
                        neighbor.G      = newMoveCostToNeighbor;
                        neighbor.H      = GetHeuristicValue(neighbor, targetNode);
                        neighbor.parent = current;
                        if (!openSet.Contains(neighbor))
                        {
                            openSet.Add(neighbor);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbor);
                        }
                    }
                }
            }
        }

        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
        //print("path found: " + sw.ElapsedMilliseconds);
    }
Esempio n. 11
0
    private IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        //Debug.Log($"START: {startPos}, END: {targetPos}");

        // Stopwatch to see the performance gain through heap optimization
        Stopwatch sw = new Stopwatch();

        // Start stopwatch
        sw.Start();

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

        Node startNode  = graph.NodeFromWorldPoint(startPos);
        Node targetNode = graph.NodeFromWorldPoint(targetPos);

        // If the start or end nodes are not walkable do not bother finding a path
        if (startNode.walkable && targetNode.walkable)
        {
            // Implementing the Heap Optimization into the former lists.
            Heap <Node>    openSet   = new Heap <Node>(graph.MaxSize);
            HashSet <Node> closedSet = new HashSet <Node>();
            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                // This is equal to the first node in the open set
                Node currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);

                if (currentNode == targetNode)
                {
                    // Stop stopwatch
                    sw.Stop();
                    print("Path found: " + sw.ElapsedMilliseconds + " ms");
                    pathSuccess = true;
                    break;
                }

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

                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour)
                                                     + neighbour.movementPenalty;
                    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);
                        }
                    }
                }
            }
        }
        // wait one frame before returning
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        if (startNode.walkable && targetNode.walkable)
        {
        }
        List <Node>    openSet   = new List <Node>();
        HashSet <Node> closedSet = new HashSet <Node>();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            Node node = openSet[0];
            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].fCost < node.fCost || openSet[i].fCost == node.fCost)
                {
                    if (openSet[i].fCost < node.fCost)
                    {
                        node = openSet[i];
                    }
                }
            }

            openSet.Remove(node);
            closedSet.Add(node);

            if (node == targetNode)
            {
                pathSuccess = true;
                break;
            }

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

                int newCostToNeightbour = node.gCost + GetDistance(node, neighbour);
                if (newCostToNeightbour < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newCostToNeightbour;
                    neighbour.hCost  = GetDistance(neighbour, targetNode);
                    neighbour.parent = node;
                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
        private IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
        {
            //debug timer
            var sw = new Stopwatch();

            sw.Start();

            bool pathSuccess = false;

            var startNode  = _grid.NodeFromWorldPoint(startPos);
            var targetNode = _grid.NodeFromWorldPoint(targetPos);

            if (startNode.Walkable && targetNode.Walkable)
            {
                // var openSet = new List<ANode>();
                var openSet = new Heap <Node>(_grid.MaxSize);

                var closeSet = new HashSet <Node>();

                openSet.Add(startNode);

                while (openSet.Count > 0)
                {
                    /*
                     * using List deal with openSet
                     */
                    // var currentNode = openSet[0];
                    // foreach (var n in openSet.Where(n =>
                    //     n.FCost < currentNode.FCost || (n.FCost == currentNode.FCost && n.HCost < currentNode.HCost)))
                    // {
                    //     currentNode = n;
                    // }
                    //
                    // openSet.Remove(currentNode);

                    /*
                     * using AHeap
                     */
                    var currentNode = openSet.RemoveFirst();

                    closeSet.Add(currentNode);

                    if (currentNode == targetNode)
                    {
                        pathSuccess = true;
                        sw.Stop();
                        print("Path found in " + sw.ElapsedMilliseconds + " ms");

                        break;
                    }

                    foreach (var neighbour in _grid.GetNeighbours(currentNode))
                    {
                        if (!neighbour.Walkable || closeSet.Contains(neighbour))
                        {
                            continue;
                        }

                        var newMovementCostToNeighbour = currentNode.GCost + GetDistance(currentNode, neighbour) + neighbour.MovementPenalty;
                        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);
                            }
                        }
                    }
                }
            }


            yield return(null);

            if (pathSuccess)
            {
                //record the path
                var waypoints = RetracePath(startNode, targetNode);
                _pathRequestManager.FinishedProcessingPath(waypoints, true);
            }
        }
Esempio n. 14
0
    IEnumerator FindPath(Vector3 startPos, Vector3 endPos)
    {
        System.Diagnostics.Stopwatch sw = new Stopwatch();
        sw.Start();

        Node[] waypoints   = new Node[0];
        bool   pathSuccess = false;

        Debug.Log("StartPos: " + startPos);
        Debug.Log("EndPos: " + endPos);

        Node startNode  = nodeManager.NodeFromWorldspace(startPos);
        Node targetNode = nodeManager.NodeFromWorldspace(endPos);

        yield return(null);

        //Debug.Log(startNode.neighbours.Count);

        if (startNode.neighbours.Count > 0 && targetNode != null)
        {
            //Debug.Log("startNode neigbours count is greater than 0 and target node is not null");
            //openList is the neighbors remaining
            //closedList is the nodes visited
            Heap <Node>    openSet   = new Heap <Node>(nodeManager.nodes.Count);
            HashSet <Node> closedSet = new HashSet <Node>();

            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                //Debug.Log("Open Set Count is Greater than 0");
                Node currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);

                if (currentNode == targetNode)
                {
                    sw.Stop();
                    Debug.Log("Completed in: " + sw.ElapsedMilliseconds + "ms");
                    pathSuccess = true;
                    break;
                }

                foreach (Node neighbour in currentNode.neighbours)
                {
                    if (neighbour.isLocked || closedSet.Contains(neighbour))
                    {
                        //Debug.Log("Neighbour is locked or in closed set");
                        continue;
                    }

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

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

        yield return(null);

        if (pathSuccess)
        {
            print("Path Success");
            waypoints = MakePath(startNode, targetNode);
        }

        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 15
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

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

        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        if (startNode.walkable && targetNode.walkable)
        {
            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)                   // If target found
                {
                    sw.Stop();
                    print("Path found: " + sw.ElapsedMilliseconds + " ms");
                    pathSuccess = true;
                    break;
                }

                List <int> BadXDirs = new List <int>();
                List <int> BadYDirs = new List <int>();

                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if (closedSet.Contains(neighbour))
                    {
                        continue;
                    }
                    if (!neighbour.walkable)
                    {
                        // If the unwalkable neighbour is a cardinal direction, block off the two diagonals on it's side as well
                        int[] diff = currentNode.NeighbourDir(neighbour);

                        // If either number = 0, then it's a cardinal direction
                        if (diff[0] == 0)   // If X=0 then it's a y direction
                        {
                            BadYDirs.Add(diff[1]);
                        }
                        if (diff[1] == 0)
                        {
                            BadXDirs.Add(diff[0]);
                        }
                        continue;
                    }
                    int[] neighbourDiff = currentNode.NeighbourDir(neighbour);
                    if (BadXDirs.Contains(neighbourDiff[0]) || BadYDirs.Contains(neighbourDiff[1]))
                    {
                        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);
                            openSet.UpdateItem(neighbour);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 16
0
    IEnumerator FindPath(Int2 startPos, Int2 targetPos, bool walkableOverRide)
    {
        int  _cellCounter  = 0;
        bool _breakme      = false;
        int  _clickcount   = 0;
        bool multiZoneWalk = true;

        Stopwatch sw = new Stopwatch();

        sw.Start();

        Int2[] waypoints   = new Int2[0];
        bool   pathSuccess = false;

        Node startNode  = NodeFromWorldPoint(startPos);
        Node targetNode = NodeFromWorldPoint(targetPos);

        startNode.parent = startNode;

        if (PathFindingDebug)
        {
            Debug.Log(string.Format("Start Node {0},{1} | StartPos {2} Target Node {3},{4} | TargetPos {5}", startNode.gridX, startNode.gridY, startPos, targetNode.gridX, targetNode.gridY, targetPos));
            Debug.Log(string.Format("Start Node Walkable{0}, TargetNode Walkable {1} Multizonewalk {2} Walkableoverride {3}", startNode.walkable, targetNode.walkable, multiZoneWalk, walkableOverRide));
        }

        //I remembered the startnode check because wherever you are you should be able to get out.
        if ((targetNode.walkable && multiZoneWalk) || walkableOverRide == true)
        {
            //Heap<Node> openSet = new Heap<Node>(grid.MaxSize);
            //TODO fix this we added placeholder
            Heap <Node> openSet = new Heap <Node>(100);

            HashSet <Node> closedSet = new HashSet <Node>();
            openSet.Add(startNode);

            while (openSet.Count > 0 && !_breakme)
            {
                //Debug.Log( string.Format( "OpenSet has {0} items", openSet.Count ) );
                Node currentNode = openSet.RemoveFirst();                                       //Pull out the first step and put it into the next set
                closedSet.Add(currentNode);                                                     //Add current node into the closed set

                if (PathFindingDebug)
                {
                    //Debug.Log( string.Format( "Current HEX {0},{1}", currentNode.gridX, currentNode.gridY ) );
                }

                if (currentNode == targetNode)
                {
                    sw.Stop();
                    print("Path found: " + sw.ElapsedMilliseconds + " ms");
                    pathSuccess = true;
                    break;
                }
                else if (_cellCounter > 10000)
                {
                    _cellCounter++;
                    _breakme = true;
                }
                else if (_cellCounter < 90000)
                {
                    _cellCounter++;
                    _clickcount++;
                    if (_clickcount == 1000)
                    {
                        //Debug.Log( string.Format( "Counter {0}", _cellCounter ) );
                        _clickcount = 0;
                    }
                }

                //This one should only run if the path is not walkable and we get really close?
                if (!targetNode.walkable && (currentNode.hCost == 14 || currentNode.hCost == 10) && currentNode.walkable)
                {
                    if (PathFindingDebug)
                    {
                        //Debug.Log( String.Format( "GCost {0} HCost {1} FCost {2} - Current Node {3}", currentNode.gCost, currentNode.hCost, currentNode.fCost, currentNode.worldPosition ) );
                    }
                    sw.Stop();
                    if (PathFindingDebug)
                    {
                        //Debug.Log( string.Format( "Closest Path found: {0} ms", sw.ElapsedMilliseconds ) );
                    }
                    pathSuccess = true;
                    targetNode  = currentNode;
                    break;
                }

                //TODO fix this LOOP
                //foreach (Node neighbour in grid.GetNeighbors(currentNode))
                //{
                //    if (PathFindingDebug)
                //    {
                //        //Debug.Log( string.Format( "Get Neighbors - Current Neighbor X{0}|Y{1}", neighbour.gridX, neighbour.gridY ) );
                //    }

                //    if (!neighbour.walkable || closedSet.Contains(neighbour))
                //    {
                //        if (PathFindingDebug)
                //        {
                //            //Debug.Log( string.Format( "Neighbor Not Walkable or ClosedSet Contains neighbor X{0}|Y{1}!", neighbour.gridX, neighbour.gridY ) );
                //        }
                //        continue;
                //    }

                //    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + TurningCost(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);
                //    }
                //}
            }
            yield return(null);
        }
        else
        {
            Debug.Log("Path not walkable");
        }

        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        else
        {
            Debug.Log("Path Not found!");
        }

        if (PathFindingDebug)
        {
            //Debug.Log( "WayPoint Array Print out " );
            //foreach ( Int2 myint in waypoints ) {
            //    Debug.Log( string.Format( "Array {0}", myint ) );
            //}
            //Debug.Log( "End PRINTOUT" );
        }

        requestManager.FinishedProcessingPath(waypoints, pathSuccess, walkableOverRide);
    }
Esempio n. 17
0
    IEnumerator FindPath(Vector3 startPos, Vector3 endPos)
    {
        Vector3[] wayPoints = new Vector3[0];
        bool      success   = false;
        Node      startNode = grid.NodeFromWorldPoint(startPos);
        Node      endNode   = grid.NodeFromWorldPoint(endPos);

        if (startNode == endNode)
        {
            requestManager.FinishedProcessingPath(wayPoints, success);
            yield return(null);
        }
        else
        {
            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.gridX == endNode.gridX && currentNode.gridY == endNode.gridY)
                {
                    success = true;
                    break;
                }

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

                    int newMovementCost = currentNode.gCost + 5 + n.penalty;
                    if (newMovementCost < n.gCost || !openSet.Contains(n))
                    {
                        n.gCost  = newMovementCost;
                        n.hCost  = getDistance(n, endNode);
                        n.parent = currentNode;
                        if (!openSet.Contains(n))
                        {
                            openSet.Add(n);
                        }
                        else
                        {
                            openSet.UpdateItem(n);
                        }
                    }
                }
            }
            yield return(null);

            if (success)
            {
                wayPoints = RetracePath(startNode, endNode);
            }
            requestManager.FinishedProcessingPath(wayPoints, success);
        }
    }
Esempio n. 18
0
    IEnumerator FindPath(Vector3 startPosition, Vector3 targetPosition)
    {
        Vector3[] waypoints      = new Vector3[0];
        Vector3[] riskyWaypoints = new Vector3[0];

        bool pathSuccess = false;

        Node startNode  = grid.NodeFromWorldPoint(startPosition);
        Node targetNode = grid.NodeFromWorldPoint(targetPosition);

        if (startNode.walkable && targetNode.walkable)
        {
            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)
                {
                    pathSuccess = true;
                    waypoints   = RetracePath(startNode, targetNode);
                    break;
                }

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

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

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

                        if (!openSet.Contains(neighbourNode))
                        {
                            openSet.Add(neighbourNode);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbourNode);
                        }
                    }
                }
            }

            Heap <Node>    riskyOpenSet   = new Heap <Node>(grid.MaxSize);
            HashSet <Node> riskyClosedSet = new HashSet <Node>();

            riskyOpenSet.Add(startNode);

            while (riskyOpenSet.Count > 0)
            {
                Node currentNode = riskyOpenSet.RemoveFirst();
                riskyClosedSet.Add(currentNode);

                if (currentNode == targetNode)
                {
                    pathSuccess    = true;
                    riskyWaypoints = RetracePath(startNode, targetNode);
                    break;
                }

                foreach (Node neighbourNode in grid.GetNeighbourNodes(currentNode))
                {
                    if (!neighbourNode.walkable || riskyClosedSet.Contains(neighbourNode))
                    {
                        continue;
                    }

                    if (neighbourNode.weight >= 1000)
                    {
                        neighbourNode.weight -= 1000;
                    }

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

                    if (newMovementCostToNeighbour < neighbourNode.gCost || !riskyOpenSet.Contains(neighbourNode))
                    {
                        neighbourNode.gCost  = newMovementCostToNeighbour;
                        neighbourNode.hCost  = GetDistance(neighbourNode, targetNode);
                        neighbourNode.parent = currentNode;

                        if (!riskyOpenSet.Contains(neighbourNode))
                        {
                            riskyOpenSet.Add(neighbourNode);
                        }
                        else
                        {
                            riskyOpenSet.UpdateItem(neighbourNode);
                        }
                    }
                }
            }
        }
        yield return(null);

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

            //Debug Code for displaying path per frame

            /*
             * for (int i = 0; i < waypoints.Length; i++) {
             *  if (i == 0) {
             *      Debug.Log("-----------------------------------------------");
             *  }
             *  Debug.Log(waypoints[i]);
             * } */
        }
        pathRequestManager.FinishedProcessingPath(waypoints, riskyWaypoints, pathSuccess);
    }
Esempio n. 19
0
    IEnumerator FindPath(Vector3 srcPos, Vector3 dstPos)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

        Vector3[] waypoints = new Vector3[0];
        bool      success   = false;

        StartNode = grid.NodeFromWoldPos(srcPos);
        EndNode   = grid.NodeFromWoldPos(dstPos);

        if (StartNode == null || EndNode == null)
        {
            yield return(null);
        }

        openSet   = new List <Node>();
        closedSet = new HashSet <Node>();

        openSet.Add(StartNode);
        openSet[0].gCost = 0;
        openSet[0].hCost = movementCost(StartNode, EndNode);


        while (openSet.Count > 0)
        {
            currentNode = openSet[0];

            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].fCost <= currentNode.fCost)
                {
                    currentNode = openSet[i];
                }
            }

            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            if (currentNode == EndNode)
            {
                sw.Stop();
                success = true;
                break;
            }

            foreach (Node n in grid.GenerateNeighBors(currentNode))
            {
                if (!n.mIsWalkable)
                {
                    continue;
                }

                int cost = currentNode.gCost + movementCost(currentNode, n);
                if (openSet.Contains(n) && cost < n.gCost)
                {
                    openSet.Remove(n);
                }
                if (closedSet.Contains(n) && cost < n.gCost)
                {
                    closedSet.Remove(n);
                }
                if (!closedSet.Contains(n) && !openSet.Contains(n))
                {
                    n.gCost  = cost;
                    n.hCost  = movementCost(n, EndNode);
                    n.Parent = currentNode;
                    openSet.Add(n);
                }
            }
        }
        yield return(null);

        if (success)
        {
            waypoints = RetracePath();
        }
        requestManager.FinishedProcessingPath(waypoints, success);
    }
Esempio n. 20
0
    private IEnumerator FindPath(Vector3 start, Vector3 target, PathParameters parameters)
    {
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        var startNode  = grid.NodeFromWorldPoint(start);
        var targetNode = grid.NodeFromWorldPoint(target);

        if (startNode == null || targetNode == null)
        {
            requestManager.FinishedProcessingPath(waypoints, pathSuccess, parameters);
            yield break;
        }

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

        openSet.Add(startNode);

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

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

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

                if (Diagonal(currentNode, neighbour))
                {
                    var n1 = grid.GetNode(currentNode.xGrid, neighbour.yGrid);
                    var n2 = grid.GetNode(neighbour.xGrid, currentNode.yGrid);
                    if (!n1.Walkable || !n2.Walkable)
                    {
                        continue;
                    }
                }

                int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + neighbour.MovementPenalty;
                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);
                    }
                }
            }
        }
        if (pathSuccess)
        {
            waypoints = RetracePath(start, startNode, targetNode, parameters.AllAngles);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess, parameters);
    }
Esempio n. 21
0
    /* This function finds a path between 2 nodes
     *
     * @param startPos - the starting node
     * @param targetPos - the target node
     *
     */
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

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

        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        if (startNode.walkable && targetNode.walkable)
        {
            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();

                /* This part of the algorithm is costly when using a list.
                 * Comparing the fCost, or hCost as a backup, to get the least cost node before proceeding with the algorithm
                 * By using a heap instead, finding a path takes less than one fourth of the time.
                 *
                 * Node currentNode = openSet[0];
                 * for (int i = 1; i< openSet.Count; i++)      // Most costly part of algorithm
                 * {
                 *  if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
                 *  {
                 *      currentNode = openSet[i];
                 *  }
                 * }
                 * openSet.Remove(currentNode);
                 */

                closedSet.Add(currentNode);

                // We have reached the targetNode (found the path)
                if (currentNode == targetNode)
                {
                    sw.Stop();
                    print("Path found: " + sw.ElapsedMilliseconds + "ms");
                    pathSuccess = true;
                    RetracePath(startNode, targetNode);
                    break;  // Coroutine requires this instead of return to exit pathfinding loop
                }

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

                    int newMovementCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbor) + neighbor.movementPenalty; //movementPenalty for weighted cost
                    if (newMovementCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor))
                    {
                        neighbor.gCost  = newMovementCostToNeighbor;
                        neighbor.hCost  = GetDistance(neighbor, targetNode);
                        neighbor.parent = currentNode;

                        if (!openSet.Contains(neighbor))
                        {
                            openSet.Add(neighbor);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbor);
                        }
                    }
                }
            }
        }
        // wait for one frame before returning
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 22
0
    IEnumerator FindPath(Vector3 beginPos, Vector3 targetPos)
    {
        Node startNode = grid.GetNodeFromWorldPoint(beginPos);
        Node endNode   = grid.GetNodeFromWorldPoint(targetPos);

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

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

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

                closedSet.Add(currentNode);

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

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

                    int costToNeighbour = currentNode.gCost + grid.GetDistanceBetweenNodes(currentNode, neighbour);

                    if (costToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = costToNeighbour;
                        neighbour.hCost  = grid.GetDistanceBetweenNodes(neighbour, endNode);
                        neighbour.parent = currentNode;

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

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

        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 23
0
    public IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        // Calculate nodes from positions
        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        if (startNode.walkable && targetNode.walkable)
        {
            List <Node>    openNodes   = new List <Node>();    // the set of nodes to be evaluated
            HashSet <Node> closedNodes = new HashSet <Node>(); // the set of nodes already evaluated
            openNodes.Add(startNode);

            while (openNodes.Count > 0)
            {
                Node currentNode = openNodes[0];
                for (int i = 0; i < openNodes.Count; i++)
                {
                    // current node = node in Open Nodes with the lowest F_cost, or (when the highest open node has the same F_cost as the current node) get the node with the highest H_cost
                    if (openNodes[i].fCost < currentNode.fCost || openNodes[i].fCost == currentNode.fCost && openNodes[i].hCost < currentNode.hCost)
                    {
                        currentNode = openNodes[i];
                    }
                }

                // Remove current from openNodes and add to closedNodes
                openNodes.Remove(currentNode);
                closedNodes.Add(currentNode);

                if (currentNode == targetNode)
                {
                    // Found path
                    pathSuccess = true;
                    break;
                }

                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if (!neighbour.walkable || closedNodes.Contains(neighbour))
                    {
                        // Skip to the next neighbour
                        continue;
                    }

                    // Check if the new path to the neighbour is shorter or if the neighbour is not in Open nodes
                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
                    if (newMovementCostToNeighbour < neighbour.gCost || !openNodes.Contains(neighbour))
                    {
                        neighbour.gCost  = newMovementCostToNeighbour;
                        neighbour.hCost  = GetDistance(neighbour, targetNode);
                        neighbour.parent = currentNode;

                        if (!openNodes.Contains(neighbour))
                        {
                            openNodes.Add(neighbour);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 24
0
    //since the execution of this function takes long, let other functions in different thread
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        //start timer
        Stopwatch sw = new Stopwatch();

        sw.Start();

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

        //get position of start and target node in world space
        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        startNode.parent = startNode;

        //if both nodes are walkable
        if (startNode.walkable && targetNode.walkable)
        {
            //heap is template cladss we defined
            Heap <Node> openSet = new Heap <Node>(grid.MaxSize);
            //hashset holds set of nodes
            HashSet <Node> closedSet = new HashSet <Node>();
            openSet.Add(startNode);

            //heap is not empty,
            while (openSet.Count > 0)
            {
                //pop off heap, and add node
                Node currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);

                //if node = finish, we are at the target, return out of function
                if (currentNode == targetNode)
                {
                    sw.Stop();
                    //print ("Path found: " + sw.ElapsedMilliseconds + " ms");
                    pathSuccess = true;
                    break;
                }

                //for all neighbours of currentnode,
                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    //skip iteration if neighbour is unwalkable
                    if (!neighbour.walkable || closedSet.Contains(neighbour))
                    {
                        continue;
                    }

                    //calculate weight, using gcost
                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + neighbour.movementPenalty;
                    //if the current node is closer, than the neightbour is, update values
                    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);
                        }
                    }
                }
            }
        }
        yield return(null);

        //if bool true, retrace path
        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 25
0
    //경로 찾기
    IEnumerator  FindPath(Vector3 startPos, Vector3 endPos)
    {
        //heap 구조 시간
        Stopwatch sw = new Stopwatch();

        sw.Start();


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

        //시작 노드와 도착 노드 를 그리드 좌표로
        Node startNode = grid.NodeFromWorldPoint(startPos);
        Node endNode   = grid.NodeFromWorldPoint(endPos);

        if (startNode.walkable && endNode.walkable)
        {
            Heap <Node> openSet = new Heap <Node>(grid.MaxSize);
            //중복되지 않은 데이터 사용 할때 사용 값을 저장
            HashSet <Node> closeSet = new HashSet <Node>();

            //시작 노드 부터 시작
            openSet.Add(startNode);


            while (openSet.Count > 0)
            {
                //첫번째 노드
                Node currentNode = openSet.RemoveFirst();


                //클로즈리스트에 노드를 더해준다
                closeSet.Add(currentNode);

                //최근 노드와 도착노드가 같다면 끝낸다(while문 탈출)
                if (currentNode == endNode)
                {
                    sw.Stop();
                    //print("Path found : " + sw.ElapsedMilliseconds + "ms");
                    pathSuccess = true;

                    break;
                }
                //반복 주위에 있는 노드
                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    //걸을수 없는 위치거나 클로즈노드가 추가되어잇다면 건너뛰기
                    if (!neighbour.walkable || closeSet.Contains(neighbour))
                    {
                        continue;
                    }

                    //주위에 이웃되어있는 노드들에 경로 값을 더하고 최솟값 경로를 추가해준다
                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
                    if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = newMovementCostToNeighbour;
                        neighbour.hCost  = GetDistance(neighbour, endNode);
                        neighbour.parent = currentNode;

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                    }
                    //이부분은 아직 도착노드에 하지 않은 상태이므로 다시 while 문 실행
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, endNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Esempio n. 26
0
    void CalculatePath(Vector3 cp_start, Vector3 cp_target)
    {
        grid.Reset();

        Tile startTile  = grid.TileFromWorldPosition(cp_start);  // translate the start position to a grid tile.
        Tile targetTile = grid.TileFromWorldPosition(cp_target); // translate the target position to a grid tile.

        List <Tile>    openList   = new List <Tile>();           // List to check for neighbours.
        HashSet <Tile> closedList = new HashSet <Tile>();        // HashSet for checked neighbours.

        // (HashSet for better performance).
        Vector3[] waypoints  = null;
        bool      pathSucces = false;

        if (!startTile.isWall && !targetTile.isWall)    // Only if the start and end tile are walkable initiate the pathfinding.
        {
            openList.Add(startTile);                    // Start with the first tile (Start Position).
            while (openList.Count > 0)
            {
                Tile currentTile = openList[0];
                for (int i = 0; i < openList.Count; i++)
                {
                    // Check if the current node's f-cost is lower or the same as
                    if (openList[i].f < currentTile.f || (openList[i].f == currentTile.f && openList[i].h < currentTile.h))
                    {
                        currentTile = openList[i];
                    }
                }

                openList.Remove(currentTile);   // If the tile has been checked, remove it from the openList.
                closedList.Add(currentTile);    // And add it to the closedList, so it won't be checked again.

                if (currentTile == targetTile)  // If the current tile is the target tile
                {
                    pathSucces = true;
                    break;
                }

                foreach (Tile neighbour in grid.GetNeighbourTiles(currentTile))
                {
                    if (neighbour.isWall || closedList.Contains(neighbour))
                    {
                        continue;   // Ignore the neighbour if it's a wall.
                    }

                    int moveCost = currentTile.g + GetManhattenDistance(currentTile, neighbour);
                    if (!openList.Contains(neighbour) || moveCost < neighbour.g)
                    {
                        neighbour.g      = moveCost;                                    // Assigns the move cost (g);
                        neighbour.h      = GetManhattenDistance(neighbour, targetTile); // the manhatten distance to target (h);
                        neighbour.parent = currentTile;                                 // and sets the current tile to the last tile's parent.

                        if (!openList.Contains(neighbour))
                        {
                            openList.Add(neighbour);    // Adds the neighbours to the openList.
                        }
                    }
                }
            }
        }
        //yield return null;  // Waits for one frame.
        if (pathSucces)
        {
            waypoints = GetFinalPath(startTile, targetTile);  // Backtrace the parents to calculate the actual path.
        }
        requestManager.FinishedProcessingPath(waypoints, pathSucces);
    }
Esempio n. 27
0
    private IEnumerator FindPath(Vector3 _startPosition, Vector3 _targetPosition)
    {
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();

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

        Node startNode  = AStarGrid.g.GetNodeFromWorldPosition(_startPosition);
        Node targetNode = AStarGrid.g.GetNodeFromWorldPosition(_targetPosition);

        //Debug for draw gizmos
        AStarGrid.g.targetNode = targetNode;

        //if the nodes are not obstacles
        if (startNode.walkable && targetNode.walkable)
        {
            //Nodes to evaluate
            Heap <Node> openList = new Heap <Node>((int)(AStarGrid.g.gridSize.x * AStarGrid.g.gridSize.y));
            //Already evaluated nodes
            List <Node> closedList = new List <Node>();

            startNode.gCost = 0;
            openList.Add(startNode);

            int safetyCheck = 0;
            //Loop through open list
            while (openList.Count > 0)
            {
                //Debug.Log("Looping through the open list...");
                safetyCheck++;
                //Can only search in a 20 by 20 space so max 400
                if (safetyCheck > 400)
                {
                    break;
                }

                //Remove current node from the open list because it is being evaluated
                Node currentNode = openList.RemoveFirstItem();

                // -- Very slow, optimised using the heap -- //

                /*Node lowestFCostNode = null;
                 * foreach(Node n in openList)
                 * {
                 *  if ((lowestFCostNode != null && lowestFCostNode.fCost > n.fCost)
                 || (n.fCost == lowestFCostNode.fCost && n.hCost < lowestFCostNode.hCost))
                 ||     lowestFCostNode = n;
                 ||}
                 ||Node currentNode = lowestFCostNode;*/

                //Add the current node to the closed (evaluated) list
                closedList.Add(currentNode);

                //Path found
                if (currentNode == targetNode)
                {
                    sw.Stop();
                    pathSuccess = true;
                    Statistics.instance.SavePathfindingTime(sw.ElapsedMilliseconds);
                    //Debug.Log("Path found in " + sw.ElapsedMilliseconds + " ms");
                    break;
                }

                foreach (Node neighbour in AStarGrid.g.GetNodeNeighbours(currentNode))
                {
                    //if the neighbour is an obstacle or has already been evaluated
                    //or is in a building
                    if (!neighbour.walkable || closedList.Contains(neighbour))
                    {
                        continue;
                    }

                    int movementCost = currentNode.gCost + GetDistanceBetweenNodes(currentNode, neighbour);
                    if (movementCost < neighbour.gCost || !openList.Contains(neighbour))
                    {
                        //set f cost of neighbour (g cost + h cost)
                        neighbour.gCost = movementCost;
                        neighbour.hCost = GetDistanceBetweenNodes(neighbour, targetNode);
                        //set parent of neighbour to current
                        neighbour.parentNode = currentNode;

                        if (!openList.Contains(neighbour))
                        {
                            openList.Add(neighbour);
                        }
                        else
                        {
                            openList.UpdateItem(neighbour);
                        }
                    }
                }
                //yield return null;
            }
        }
        if (pathSuccess)
        {
            waypoints = DefinePath(startNode, targetNode);
        }
        //else
        //{
        //    Debug.LogWarning("Path not found!");
        //}
        prm.FinishedProcessingPath(waypoints, pathSuccess);
        yield return(null);
    }
Esempio n. 28
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Vector3[] wayPoints   = new Vector3[0];
        bool      pathSuccess = false;                              // A bool to check if path is possible
        Node      startNode   = grid.NodeFromWorldPoint(startPos);  // The algorithm starts at this point
        Node      targetNode  = grid.NodeFromWorldPoint(targetPos); // The algorithm ends at this point

        if (startNode.walkable && targetNode.walkable)              // If start and end are walkable
        {
            List <Node>    openSet   = new List <Node>();           // The set of node to be evaluated
            HashSet <Node> closedSet = new HashSet <Node>();        // The set of nodes already evaluated
            openSet.Add(startNode);                                 // Add the first node to the openSet

            while (openSet.Count > 0)
            {
                Node currentNode = openSet[0]; // The currentNode is the startNode
                for (int i = 1; i < openSet.Count; i++)
                {
                    // If a node in the openSet has a fCost smaller than the fCost from the startNode, than this is the new currentNode
                    // On top of this, if the fCost of a node is equal to the fCost of the currentNode, than we look at the hCost
                    // If the hCost is smaller, than this is the new CurrentNode
                    if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
                    {
                        currentNode = openSet[i];
                    }
                }

                // We need to remove the newest currentNode from the openSet and put it into closedSet
                openSet.Remove(currentNode);
                closedSet.Add(currentNode);

                // If the currentNode is the targetNode
                if (currentNode == targetNode)
                {
                    pathSuccess = true;
                    break; // Get out of the while
                }

                // We need to check for each neighbour if it is walkable and not already evaluated
                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if (!neighbour.walkable || closedSet.Contains(neighbour))
                    {
                        continue;
                    }

                    // If the new path to a neighbour is shorter or when the neighbour is not in the openSet, we:
                    // Set an fCost to the neighbour
                    // Set the parent of this neighbour to current, which is important for backtracking
                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); // Cost = gcost + hcost
                    if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = newMovementCostToNeighbour;         // Set G cost
                        neighbour.hCost  = GetDistance(neighbour, targetNode); // Distance to end (H cost)
                        neighbour.parent = currentNode;                        // Set the parent of the neighbour

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour); // Add neighbour if it is not in the open set
                        }
                    }
                }
            }
            yield return(null); // Wait for one frame and then return

            if (pathSuccess)
            {
                wayPoints = RetracePath(startNode, targetNode);
            }
            pathRequestManager.FinishedProcessingPath(wayPoints, pathSuccess); // Send the manager that we finished the path and its waypoints
        }
    }
Esempio n. 29
0
    IEnumerator FindPath(Vector2 startPos, Vector2 targetPos, bool onlyWalkableArea = true)
    {
        Vector2[] waypoints   = new Vector2[0];
        bool      pathSuccess = false;

        //Get the nodes for respective world space positions.
        Node startNode  = Grid.NodeFromWorldPoint(startPos);
        Node targetNode = Grid.NodeFromWorldPoint(targetPos);

        if (startNode.walkable && targetNode.walkable)
        {
            //Create open set and closed set. Open set for nodes that haven't been examined yet, closed set for nodes that have.
            Heap <Node>    openSet   = new Heap <Node>(grid.MaxSize);
            HashSet <Node> closedSet = new HashSet <Node>();

            //Add start node to openSet
            openSet.Add(startNode);

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

                //We've found the target
                if (currentNode == targetNode)
                {
                    pathSuccess = true;
                    break;
                }

                //Find all neighbouring nodes.
                List <Node> neighbours = new List <Node>();
                neighbours = Grid.GetNeighbourNodes(currentNode);
                foreach (Node neighbour in neighbours)
                {
                    //If the node is not walkable and you're looking *only* for walkable path or it is in the closed set, then continue to the next
                    if ((!neighbour.walkable && onlyWalkableArea) || 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);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
    // Başlangıç ve hedef noktalarımıza göre yolumuzu buluyoruz.
    IEnumerator FindPath(Vector2 startPos, Vector2 targetPos)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

        Vector2[] waypoints   = new Vector2[0];
        bool      pathSuccess = false;

        //Node larımızı grid scriptimizde olan Node ların world pozisyonunu bulan scriptimize atarak pozisyonlarını alıyoruz.
        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        if (startNode.walkable && targetNode.walkable)
        {
            // Listelerimizi oluşturuyoruz.
            Heap <Node>    openSet   = new Heap <Node>(grid.MaxSize);
            HashSet <Node> closedSet = new HashSet <Node>();

            //Başlangıç Node'umuzu openSet listemize ilk eleman olarak atıyoruz.
            openSet.Add(startNode);

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

                //Yeni currentNode'umuz hedefimiz mi diye kontrol ediyoruz.
                if (currentNode == targetNode)
                {
                    sw.Stop();
                    print("Yolumuz şu sürede bulundu : " + sw.ElapsedMilliseconds + "ms");
                    pathSuccess = true;
                    break;
                }
                //Tüm komşularımıza bakıp hangisine daha yakın olduğumuzu buluyoruz.
                foreach (Node neighbours in grid.GetNeighbours(currentNode))
                {
                    if (!neighbours.walkable || closedSet.Contains(neighbours))
                    {
                        continue;
                    }

                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbours);
                    if (newMovementCostToNeighbour < neighbours.gCost || !openSet.Contains(neighbours))
                    {
                        neighbours.gCost  = newMovementCostToNeighbour;
                        neighbours.hCost  = GetDistance(neighbours, targetNode);
                        neighbours.parent = currentNode;
                        //En yakın bulduğumuz komşu zaten openSet dizisinde mi diye bakıyoruz.
                        if (!openSet.Contains(neighbours))
                        {
                            openSet.Add(neighbours);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbours);
                        }
                    }
                }
            }
        }
        else
        {
            movementStoperImage.SetActive(false);
        }


        yield return(null);

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