GetNeighbours() 공개 메소드

public GetNeighbours ( Node node ) : List
node Node
리턴 List
예제 #1
0
    public List <Node> FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

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

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            Node currentNode = openSet [0];
            for (int i = 1; i < openSet.Count; i++)
            {
                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);

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

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

                int penality = 0;
                if (IsNearToUnwalkable(neighbour))
                {
                    penality = 1000;
                }

//				int newMovementCostToNeighbour = currentNode.gCost + GetDistance (currentNode, neighbour);
                int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + penality;
//				Debug.Log (newMovementCostToNeighbour + "gCost "+neighbour.gCost);
                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);
                    }
                }
            }
        }
        return(null);
    }
예제 #2
0
        public void GetNeighbourForCornerCell()
        {
            const int pointX = 0;
            const int pointY = 0;

            var expectedNeighbourCells = new List <Cell>
            {
                new Cell()
                {
                    PointX = 0, PointY = 1
                },
                new Cell()
                {
                    PointX = 1, PointY = 0
                },
                new Cell()
                {
                    PointX = 1, PointY = 1
                },
            };

            var actual = _grid.GetNeighbours(pointX, pointY);

            Assert.IsTrue(CheckListContainsSameCells(expectedNeighbourCells, actual));
        }
예제 #3
0
    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 || startNode.aiWalkable) && (targetNode.walkable || targetNode.aiWalkable))
        {
            MinHeap <Node> openSet   = new MinHeap <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;
                    break;
                }

                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if ((!neighbour.walkable && !neighbour.aiWalkable) || 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);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
예제 #4
0
            public Grid Update(Grid grid)//Converts one grid to another. The transformation is based on the provided rules.
            {
                newGrid         = grid;
                int[,] newCells = new int[grid.Width, grid.Height]; //All the cells from the provided original grid are saved as an 2-dimensional array.

                foreach (Cell cell in grid.cells)                   //The rules of the game are applied here.
                {                                                   //When a rule is applied, changes are made to the value of the new formed cells.
                    if ((cell.Colour == 0) & ((grid.GetNeighbours(cell) == 3) || (grid.GetNeighbours(cell) == 6)))
                    {
                        newCells[cell.WidthPosition, cell.HeightPosition] = 1;
                    }
                    else if ((cell.Colour == 1) & !((grid.GetNeighbours(cell) == 2) || (grid.GetNeighbours(cell) == 3) || (grid.GetNeighbours(cell) == 6)))
                    {
                        newCells[cell.WidthPosition, cell.HeightPosition] = 0;
                    }
                    else
                    {
                        newCells[cell.WidthPosition, cell.HeightPosition] = grid.cells[cell.WidthPosition, cell.HeightPosition].Colour;
                    }
                }
                //The newly generated cells are replacing the old ones from the grid.
                foreach (Cell cell in newGrid.cells)
                {
                    cell.Colour = newCells[cell.WidthPosition, cell.HeightPosition];
                }
                return(newGrid);
            }
예제 #5
0
    //wavefront algorithm
    private void GenerateCostMap()
    {
        goalCell          = grid.GetCellFromWorld(goal);
        goalCell.distance = 0;
        goalCell.isMarked = true;
        queue.Enqueue(goalCell);

        while (queue.Count > 0)
        {
            current = queue.Dequeue();
            if (current.unpassable)
            {
                continue;
            }
            Cell[] neighbours = grid.GetNeighbours(current);
            for (int i = 0; i < 4; ++i)
            {
                Cell currentNeighbour = neighbours[i];
                if (currentNeighbour == null || currentNeighbour.isMarked)
                {
                    continue;
                }

                currentNeighbour.distance = current.distance + 1;
                currentNeighbour.isMarked = true;
                queue.Enqueue(currentNeighbour);
            }
        }
    }
예제 #6
0
 //找出第一個轉彎點
 Vector3 redo(Node nowNode, Node lastNode, float angel)
 {
     foreach (Node neighbour in grid.GetNeighbours(nowNode))
     {
         if (grid.path.Contains(neighbour))
         {
             Vector2 nowVec   = new Vector2(nowNode.gridX, nowNode.gridY) - new Vector2(neighbour.gridX, neighbour.gridY);
             float   nowAngel = Mathf.Atan2(nowVec.y, nowVec.x);
             //如果方向一致,遞迴函數找下一個
             if (nowAngel == angel)
             {
                 return(redo(neighbour, nowNode, nowAngel));
             }
             else if
             //如果現在這個節點就是上一個節點
             (neighbour == lastNode)
             {
                 continue;
             }
             else
             //如果方向不同,結束遞迴狀態
             {
                 return(nowNode.worldPosition);
             }
         }
     }
     return(new Vector3(0, 0, 0));
 }
예제 #7
0
    void InitializeMap()
    {
        Node startNode = grid.NodeFromWorldPoint(seeker.position);

        // Debug.Log(seeker.position);
        startNode.yDistance = (int)seeker.position.y;
        // Debug.Log(startNode.worldPosition);
        // set of nodes to be evaluated
        Heap <Node> openSet = new Heap <Node>(grid.MaxSize);
        // set of nodes already evaluated
        HashSet <Node> closedSet = new HashSet <Node>();

        openSet.Add(startNode);

        Node maxNode = startNode;

        // Checking all the walkable nodes
        while (openSet.Count > 0)
        {
            Node currentNode = openSet.RemoveFirst();
            closedSet.Add(currentNode);
            // Update the currentNode start block to walkable
            RaycastHit2D initialHit = Physics2D.Raycast(currentNode.worldPosition, Vector2.zero, 0, layerMask);
            if (initialHit)
            {
                if (initialHit.transform.tag == "FixedBlock")
                {
                    initialHit.transform.tag = "WalkableBlock";
                }
            }

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

                // Updating every fixed block to walkableBlock to allow snapping to them
                RaycastHit2D hit = Physics2D.Raycast(neighbour.worldPosition, Vector2.zero, 0, layerMask);
                if (hit)
                {
                    if (hit.transform.tag == "FixedBlock")
                    {
                        hit.transform.tag = "WalkableBlock";
                    }
                }

                neighbour.parent = currentNode;
                if (!openSet.Contains(neighbour))
                {
                    openSet.Add(neighbour);
                }
            }
        }
    }
예제 #8
0
    public List <Node> FindPath(Node startNode, Node targetNode)
    {
        List <Node>    openSet   = new List <Node> ();
        HashSet <Node> closedSet = new HashSet <Node> ();

        openSet.Add(startNode);
        while (openSet.Count > 0)
        {
            Node currentNode = openSet [0];
            for (int i = 1; i < openSet.Count; i++)
            {
                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);

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

            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);
                    }
                }
            }
        }
        return(null);
    }
예제 #9
0
    IEnumerator FindTarget(Vector3 startPos, List <Node> available, HashSet <Node> dynamicBlocked)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();
        Node        target         = null;
        bool        targetSuccess  = false;
        List <Node> visitedNodes   = new List <Node>();
        List <Node> lastAddedNodes = new List <Node>();
        List <Node> nowAddedNodes  = new List <Node>();
        Node        center         = grid.NodeFromWorldPoint(startPos);

        if (center != null && available != null)
        {
            if (available.Count > 0)
            {
                visitedNodes.Add(center);
                lastAddedNodes.Add(center);
                int addedCount = 1;
                while (addedCount > 0)
                {
                    addedCount = 0;
                    foreach (Node n in lastAddedNodes)
                    {
                        List <Node> neighbours = grid.GetNeighbours(n);

                        foreach (Node node in neighbours)
                        {
                            if (node.danger == 0 && !node.visited && !visitedNodes.Contains(node) && !dynamicBlocked.Contains(node))
                            {
                                if (available.Contains(node))
                                {
                                    target        = node;
                                    targetSuccess = true;
                                    sw.Stop();
                                    break;
                                }
                                visitedNodes.Add(node);
                                nowAddedNodes.Add(node);
                                addedCount++;
                            }
                        }
                        if (targetSuccess)
                        {
                            break;
                        }
                    }
                    lastAddedNodes = nowAddedNodes;
                    nowAddedNodes  = new List <Node>();
                    if (targetSuccess)
                    {
                        break;
                    }
                }
            }
        }
        yield return(null);

        requestManager.FinishedProcessingTarget(target, targetSuccess);
    }
예제 #10
0
    /**
     * Find the path from starting position to target position.
     */
    public Vector3[] PathFind(Vector3 start, Vector3 target)
    {
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

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

        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;
                    break;
                }

                foreach (Node neighbour in grid.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);
                        }
                    }
                }
            }
        }

        if (pathSuccess)
        {
            waypoints   = RetracePath(startNode, targetNode);
            pathSuccess = waypoints.Length > 0;
        }

        return(waypoints);
    }
예제 #11
0
    public Node GetWalkableNode()
    {
        Grid grid = GameObject.Find("A*").GetComponent <Grid>();
        Node n    = grid.NodeFromWorldPoint(area.position);

        if (n.walkable && !grid.IsNodeOccupied(n))
        {
            return(n);
        }
        List <Node> list       = new List <Node>();
        List <Node> closedList = new List <Node>();

        list.Add(n);
        while (list.Count > 0)
        {
            n = list[0];
            foreach (Node neigh in grid.GetNeighbours(n))
            {
                if (IsInRoom(neigh))
                {
                    if (neigh.walkable && !grid.IsNodeOccupied(neigh))
                    {
                        return(neigh);
                    }
                    else if (!list.Contains(neigh) && !closedList.Contains(neigh))
                    {
                        list.Add(neigh);
                    }
                }
            }
            list.Remove(n);
            closedList.Add(n);
        }
        return(null);
    }
예제 #12
0
    public void 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)
        {
            Heap <Node>    openSet   = new Heap <Node> (grid.MaxSize);
            HashSet <Node> closedSet = new HashSet <Node> ();
            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                Node currNode = openSet.RemoveFirstItem();

                closedSet.Add(currNode);

                if (currNode == targetNode)
                {
                    pathSuccess = true;
                    break;                     //to exit the loop
                }

                foreach (Node neighbour in grid.GetNeighbours(currNode))
                {
                    if (!neighbour.walkable || closedSet.Contains(neighbour))                        //|| !pm.MapLimits(neighbour.worldPosition
                    {
                        continue;
                    }

                    int nwMCostToN = currNode.gCost + GetDistance(currNode, neighbour);

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

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

        if (pathSuccess)
        {
            waypoints = RetraceP(startNode, targetNode);
        }
        rqManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
예제 #13
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);

        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)
                {
                    Debug.Log("Pathfinding 1: " + pathSuccess);
                    pathSuccess = true;
                    Debug.Log("Pathfinding 2: " + pathSuccess);
                    break;
                }

                foreach (Node neighbour in grid.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);
                        }
                    }
                }
            }
        }
        if (pathSuccess)
        {
            waypoints   = backtrack(startNode, targetNode);
            pathSuccess = waypoints.Length > 0;
        }
        callback(new PathResult(waypoints, pathSuccess, request.callback));
    }
예제 #14
0
    public override void OnTurnChange(Node node)
    {
        if (grid == null)
        {
            grid = CombatManager.instance.GetComponent <Grid>();
        }
        base.OnTurnChange(node);
        if (!CombatManager.instance.IsThisUnitsTurn(node.Unit))
        {
            return;
        }
        Unit        thisUnit             = node.Unit;
        Unit        target               = CombatManager.instance.ClosestUnit(thisUnit);
        List <Node> validMovementTargets = CombatManager.instance.ValidMovementTargets(node);

        Node[] neighbours = grid.GetNeighbours(grid.UnitsNode(target));
        for (int i = 0; i < neighbours.Length; ++i)
        {
            if (validMovementTargets.Contains(neighbours[i]))
            {
                Debug.Log(node.Unit);
                CombatManager.instance.MoveUnit(node, neighbours[i]);
                CombatManager.instance.Attack(thisUnit, target);
                break;
            }
        }
        thisUnit.actionPoints = 0;
    }
    void  FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();
        Node           startNode  = grid.NodeFromWorldPoint(startPos);
        Node           targetNode = grid.NodeFromWorldPoint(targetPos);
        List <Node>    openSet    = new List <Node>();
        HashSet <Node> closedSet  = new HashSet <Node>();

        openSet.Add(startNode);

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

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


            if (currentNode == targetNode)
            {
                sw.Stop();
                print("Path found: " + sw.ElapsedMilliseconds + " ms");
                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);
                    }
                }
            }
        }
    }
예제 #16
0
    public void FindPath(Vector3 startPos, Vector3 destinationPos)
    {
        var startNode       = grid.NodeFromWorldPoint(startPos);
        var destinationNode = grid.NodeFromWorldPoint(destinationPos);

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

        openSet.Add(startNode);

        while (openSet.Count != 0)
        {
            Node currentNode = openSet[0];
            for (var i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].FCost < currentNode.FCost)
                {
                    currentNode = openSet[i];
                }
                else if (openSet[i].FCost == currentNode.FCost)
                {
                    if (openSet[i].hCost < currentNode.hCost)
                    {
                        currentNode = openSet[i];
                    }
                }

                openSet.Remove(currentNode);
                closedSet.Add(currentNode);
                if (currentNode == destinationNode)
                {
                    RetracePath(startNode, destinationNode);
                    pathFound = true;
                    return;
                }

                var neighbours = grid.GetNeighbours(currentNode);
                var length     = neighbours.Count;
                for (var index = 0; index < length; index++)
                {
                    var neighbour = neighbours[index];
                    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, destinationNode);
                        neighbour.parent = currentNode;
                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                    }
                }
            }
        }
    }
예제 #17
0
    public void FindPath(PathRequest request, Action <PathResult> callback)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

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

        Node startNode  = _grid.NodeFromWorldPoint(request.PathStart);
        Node targetNode = _grid.NodeFromWorldPoint(request.PathEnd);


        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)
                {
                    sw.Stop();
                    Debug.Log("Path Found in " + sw.ElapsedMilliseconds + "ms");
                    pathSuccess = true;
                    break;
                }
                foreach (var neighbour in _grid.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);
                        }
                    }
                }
            }
        }
        if (pathSuccess)
        {
            wayPoints   = RetracePath(startNode, targetNode);
            pathSuccess = wayPoints.Length > 0;
        }
        callback(new PathResult(wayPoints, pathSuccess, request.Callback));
    }
예제 #18
0
    /// <summary>
    /// Set current node to unwalkable.
    /// </summary>
    public void UpdateNodePosition()
    {
        Node node = m_grid.NodeFromWorldPoint(transform.position);

        if (isMoving == false)
        {
            lastPositionNeighbors = m_grid.GetNeighbours(node);
            foreach (Node n in lastPositionNeighbors)
            {
                if (n.walkable != Walkable.Impassable)
                {
                    n.walkable = Walkable.Blocked;
                }
            }
            node.walkable    = Walkable.Blocked;
            lastNodePosition = node;
            currentPosition  = new Vector2(node.gridX, node.gridY);
            return;
        }

        if (lastNodePosition != null && isMoving)
        {
            preventExtraNodeUpdate    = false;
            lastPositionNeighbors     = m_grid.GetNeighbours(node);
            lastNodePosition.walkable = Walkable.Passable;
            if (lastPositionNeighbors != null)
            {
                foreach (Node n in lastPositionNeighbors)
                {
                    if (n.walkable != Walkable.Impassable)
                    {
                        n.walkable = Walkable.Passable;
                    }
                }
            }
            if (!node.Equals(lastNodePosition))
            {
                spacesMoved++;
            }
        }
        else
        {
            node.walkable    = Walkable.Blocked;
            lastNodePosition = node;
            currentPosition  = new Vector2(node.gridX, node.gridY);
        }
    }
예제 #19
0
    private IEnumerator FindPath(Vector2 startPos, Vector2 targetPos)
    {
        var startNode  = grid.NodeFromWorldPoint(startPos);
        var targetNode = grid.NodeFromWorldPoint(targetPos);

        var waypoints = new Vector2[0];
        var success   = false;

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

            openSet.Add(startNode);

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

                //if target node found, exit from loop
                if (currentNode == targetNode)
                {
                    success = true;
                    RetracePath(startNode, targetNode);
                    break;
                }

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

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

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

        yield return(null);

        if (success)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        PathRequestHandler.FinishedProcessingPath(waypoints, success);
    }
예제 #20
0
    public void FindPathWithPriorityQueue(Vector3 startPos, Vector3 targetPos)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

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

        pTempNode[1] = grid.NodeFromWorldPosition(GameObject.FindGameObjectWithTag("Player").GetComponent <Transform>().position);
        pTempNode[0] = grid.NodeFromWorldPosition(targetPos);
        PriorityQueue <Node> openSet   = new PriorityQueue <Node>(1000);
        HashSet <Node>       closedSet = new HashSet <Node>();

        openSet.Enqueue(startNode);
        fTotalDistance = GetDistance(startNode, targetNode);
        while (openSet.Count() > 0)
        {
            Node currentNode = openSet.Dequeue();
            if (currentNode == targetNode)
            {
                RetracePath(startNode, targetNode);

                sw.Stop();
                return;
            }

            closedSet.Add(currentNode);

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

                int g = currentNode.gCost + GetDistance(currentNode, n);
                int h = GetDistance(n, targetNode);

                int f = g + h;

                if (!openSet.Contains(n))
                {
                    n.gCost  = g;
                    n.hCost  = h;
                    n.parent = currentNode;
                    openSet.Enqueue(n);
                }
                else
                {
                    if (n.fCost > f || (n.fCost == f && n.gCost > g))
                    {
                        n.gCost  = g;
                        n.parent = currentNode;
                    }
                }
            }
        }
    }
예제 #21
0
    public List <Node> FindPath(Cell startCell, Cell endCell)
    {
        Node startNode = _grid.GetNode(startCell.Position);
        Node endNode   = _grid.GetNode(endCell.Position);

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

        openSet.Add(startNode);

        //Loop through entire openset
        while (openSet.Count > 0)
        {
            var currentNode = openSet[0];

            //Get the node with the smallest FCost.
            for (var i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].FCost < currentNode.FCost ||
                    openSet[i].FCost == currentNode.FCost && openSet[i].HCost < currentNode.HCost)
                {
                    currentNode = openSet[i];
                }
            }

            //Remove current from open and add to closed
            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            //If we are at the target, we're done
            if (currentNode == endNode)
            {
                return(RetracePath(startNode, endNode));
            }

            //Loop through all neighbours
            foreach (var neighbour in _grid.GetNeighbours(currentNode))
            {
                //Skip if you can't walk on it or if we already checked it
                if (!neighbour.Walkable || closedSet.Contains(neighbour))
                {
                    continue;
                }

                //Calculate costs
                var newGCostToNeighbour = GuessDistance(currentNode, neighbour);
                if (newGCostToNeighbour < neighbour.GCost || !openSet.Contains(neighbour))
                {
                    neighbour.GCost  = newGCostToNeighbour;
                    neighbour.HCost  = GuessDistance(neighbour, endNode);
                    neighbour.Parent = currentNode;

                    openSet.Add(neighbour);
                }
            }
        }

        return(null);
    }
예제 #22
0
    Vector2[] FindPath(Vector2 from, Vector2 to)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

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

        _Node startNode  = grid.NodeFromWorldPoint(from);
        _Node targetNode = grid.NodeFromWorldPoint(to);

        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);

            if (currentNode == targetNode)
            {
                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, targetNode);
                    neighbour.parent = currentNode;

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

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

        return(waypoints);
    }
예제 #23
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Vector3[] waypoints   = new Vector3[0];               //An array of waypoints that the unit will travel across
        bool      pathSuccess = false;                        //A bool to check whether or not the path was successfully found.

        Node startNode  = grid.NodeFromWorldPoint(startPos);  //The node that the pathfinder starts at
        Node targetNode = grid.NodeFromWorldPoint(targetPos); //The node that the pathfinder needs to get to

        if (startNode.walkable && targetNode.walkable)
        {
            Heap <Node>    openSet   = new Heap <Node> (grid.MaxSize); //An open list of nodes to check which nodes are able to be used
            HashSet <Node> closedSet = new HashSet <Node> ();          //A closed list of nodes to indicate that they've already been used
            openSet.Add(startNode);                                    //Add the starting node to the open set

            while (openSet.Count > 0)
            {
                Node currentNode = openSet.RemoveFirst();   //The current node will always start at the first node in the list
                closedSet.Add(currentNode);                 //Add the current node to the closed set as it should always be the first node to travel to

                if (currentNode == targetNode)              //If the current node has reached the target node, the path has been successfully found
                {
                    pathSuccess = true;
                    break;
                }

                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if (!neighbour.walkable || closedSet.Contains(neighbour))                      //checks if the neighbouring node is traversable or hasn't already been checked
                    {
                        continue;
                    }

                    int newMoveCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); //New cost to move to a neighbour is gcost plus the distance between current and neighbour nodes
                    if (newMoveCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))         //If neighbour cost is less than neighbour's gcost or neighbour node is not in the openset
                    {
                        neighbour.gCost  = newMoveCostToNeighbour;                                        //neighbour gcost equals new neighbour cost
                        neighbour.hCost  = GetDistance(neighbour, targetNode);                            //neighbour hcost equals the distance from the neighbour to the target node
                        neighbour.parent = currentNode;

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);                             //Add the neighbour to the openset if it isn't already
                        }
                        else
                        {
                            openSet.UpdateItem(neighbour);                             //Update the heap to find the neibour with highest priority
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);            //Get each node used between the start and end nodes
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess); // Call the method that tells the request manager that the path has finished processing
    }
예제 #24
0
    public void FindPath(Vector3 startPos, Vector3 endPos)
    {
        var start = grid.NodeFromWorldPoint(startPos);
        var end   = grid.NodeFromWorldPoint(endPos);

        //closedList.Clear();
        //openList.Clear();
        //List<Node> openList = new List<Node>();
        //List<Node> closedList = new List<Node>();

        openList.Add(start);

        while (openList.Count > 0)
        {
            Node current = openList[0];
            for (int i = 1; i < openList.Count; i++)
            {
                if (openList[i].fCost < current.fCost || openList[i].fCost == current.fCost && openList[i].hCost < current.hCost)
                {
                    current = openList[i];
                }
            }

            openList.Remove(current);
            closedList.Add(current);

            if (current == end)
            {
                //retrace
                Retrace(start, end);
                //Debug.Log("Done");
                return;
            }


            foreach (var neighbour in grid.GetNeighbours(current))
            {
                if (closedList.Contains(neighbour) || !neighbour.walkable)
                {
                    continue;
                }

                int newG = current.gCost + Distance(current, neighbour);
                if (newG < neighbour.gCost || !openList.Contains(neighbour))
                {
                    neighbour.gCost  = newG;
                    neighbour.hCost  = Distance(neighbour, end);
                    neighbour.parent = current;
                    if (!openList.Contains(neighbour))
                    {
                        openList.Add(neighbour);
                    }
                }
            }
        }

        //Retrace(start, end);
        //Debug.Log("No Solution");
    }
예제 #25
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)
        {
            Stack <Node>   openSet   = new Stack <Node>(grid.MaxSize);
            HashSet <Node> closedSet = new HashSet <Node>();
            openSet.Push(startNode);

            while (openSet.Count > 0)
            {
                Node currentNode = openSet.Pop();
                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 (!closedSet.Contains(neighbour))
                    {
                        closedSet.Add(neighbour);

                        openSet.Push(neighbour);
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
예제 #26
0
    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)
        {
            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;
                    break;
                }

                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if (!neighbour.walkable || closedSet.Contains(neighbour) /* || !neighbour.discovered*/)
                    {
                        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);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
            DateTime after = DateTime.Now;
            //TimeSpan duration = after-before;
            Debug.Log("path found time taken = " /* (+after - before)*/);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
예제 #27
0
    public void FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Node startNode  = grid.GetNodeFromWorldPos(startPos);
        Node targetNode = grid.GetNodeFromWorldPos(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();

            /*
             * for(int i = 1; i < openSet.Count; i++)
             * {
             *  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);

            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);
                    }
                }
            }
        }
    }
예제 #28
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

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

        //neste if, o codigo faz o rastreamento dos nodes e o calculo dos vizinhos, para calcular o custo de g, h e f (f = g + h)
        //esta parte teorica estara explicada no relatorio
        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);
                requestManager = GetComponent <PathRequestManager>();

                if (currentNode == targetNode)
                {
                    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 < 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);
    }
예제 #29
0
    private void SpawnWave()
    {
        if (_genticAlgorithm.bestFitness == 1.0f)
        {
            Debug.Log("!!!!!BEST DNA REACHED!!!!!!!");
        }
        else
        {
            // generate a new generation of DNA
            _genticAlgorithm.NewGeneration();

            currentGeneration++;

            // update debug representation
            debugPopulation.Clear();
            for (int i = 0; i < _genticAlgorithm.population.Count; i++)
            {
                debugPopulation.Add(_genticAlgorithm.population[i].genes[0]);
            }
        }

        // clear the population ready to spawn in new enemies
        _AIPopulation.Clear();

        // keep track of spawn in locations so that there is no spawning
        // on a node that already contains an enemy
        List <Node> spawnedInNodes = new List <Node> ();

        for (int i = 0; i < _genticAlgorithm.population.Count; i++)
        {
            // get the traits from the population
            AIDNA <EvolutionTraits> traits = _genticAlgorithm.population [i];

            // spawn in the enemy using the prefab
            GameObject newAI = Instantiate(_enemyPrefab);

            // apply to speed and damage from the traits/genes
            newAI.GetComponent <SimpleEnemyState> ().damage = Mathf.RoundToInt(traits.genes [0].muscleArm);
            newAI.GetComponent <SimpleEnemyState> ().speed  = traits.genes [0].muscleLeg;


            // spawn enemy at an adjacent tile from the Boss (this.transform)
            List <Node> neighbours = _Grid.GetNeighbours(_Grid.NodeFromWorldPoint(this.transform.position));
            foreach (Node node in neighbours)
            {
                // node must be walkable and not have been spawned on by another enemy
                if (node.walkable == true && spawnedInNodes.Contains(node) == false)
                {
                    // update position
                    newAI.transform.position = new Vector3(node.worldPosition.x, 1.0f, node.worldPosition.z);

                    _AIPopulation.Add(newAI);
                    spawnedInNodes.Add(node);
                    break;
                }
            }
        }
    }
예제 #30
0
    //The algorithm to find the path via A* algorithm
    private void FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        /*
         * Make two lists of nodes where openSet are the one being tested while the closedSet is the ones added to the path
         *
         */
        List <Node> openSet   = new List <Node>();
        List <Node> closedSet = new List <Node>();

        openSet.Add(startNode);

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

            for (int i = 1; i < openSet.Count; i++)
            {
                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);

            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);
                    }
                }
            }
        }
    }
예제 #31
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        grid = GetComponent<Grid>();
        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) {
                    sw.Stop();
                    print ("Path found: " + sw.ElapsedMilliseconds + " ms");
                    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 < 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);
    }
예제 #32
0
파일: BFSSearch.cs 프로젝트: zmedgyes/Unity
    public Node searchNearestNode(Vector3 position, List<Node> goals,Grid grid)
    {
        List<Node> visitedNodes = new List<Node>();
        List<Node> lastAddedNodes = new List<Node>();
        List<Node> nowAddedNodes = new List<Node>();
        Node center = grid.NodeFromWorldPoint(position);
        bool run = true;
        if(center!=null && goals != null)
        {
            if (goals.Count > 0)
            {
                visitedNodes.Add(center);
                lastAddedNodes.Add(center);
                int addedCount = 0;
                while (run) {
                    foreach (Node n in lastAddedNodes) {
                        List<Node> neighbours = grid.GetNeighbours(n);
                        foreach(Node node in neighbours)
                        {
                            if(node.danger==0 && !node.visited && !visitedNodes.Contains(node))
                            {
                                if (goals.Contains(node))
                                {
                                    return node;

                                }
                                visitedNodes.Add(node);
                                nowAddedNodes.Add(node);
                                addedCount++;

                            }
                        }

                    }
                    lastAddedNodes = nowAddedNodes;
                    nowAddedNodes = new List<Node>();
                    if (addedCount == 0) { run = false; }
                    addedCount = 0;
                }

            }

        }
        return null;
    }