Ejemplo n.º 1
0
    public void FindPath(Vector3 start, Vector3 end)
    {
        Node startNode = m_Grid.NodeFromWorldPos(start);
        Node endNode   = m_Grid.NodeFromWorldPos(end);

        Heap <Node> openSet   = new Heap <Node>(m_Grid.NodeCount);
        List <Node> closedSet = new List <Node>();

        openSet.Add(startNode);

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

            if (current == endNode)
            {
                RetracePath(startNode, endNode);
                return;
            }

            foreach (Node node in m_Grid.GetNeighbours(current))
            {
                if (node.m_bIsblocked || closedSet.Contains(node))
                {
                    continue;
                }

                int newMovementCost = current.m_iGCost + GetDistance(current, node);

                if (newMovementCost < node.m_iGCost || !openSet.Contains(node))
                {
                    node.m_iGCost = newMovementCost;
                    node.m_iHCost = GetDistance(node, endNode);
                    node.m_Parent = current;

                    if (!openSet.Contains(node))
                    {
                        openSet.Add(node);
                    }
                    else
                    {
                        openSet.UpdateItem(node);
                    }
                }
            }
        }
    }
Ejemplo n.º 2
0
    public void FindPath(Vector3 startPoint, Vector3 endPoint)
    {
        Node startNode = m_Grid.NodeFromWorldPos(startPoint);
        Node endNode   = m_Grid.NodeFromWorldPos(endPoint);

        List <Node> m_lOpenSet   = new List <Node>();
        List <Node> m_lClosedSet = new List <Node>();

        //G-Cost
        startNode.m_iGCost = GetDistance(startNode, startNode);
        //H-Cost
        startNode.m_iHCost = GetDistance(startNode, endNode);

        m_lOpenSet.Add(startNode);

        Node        currentNode = null;
        List <Node> neighbours  = null;

        while (m_lOpenSet.Count > 0)
        {
            currentNode = LowestFCostNode(m_lOpenSet);
            m_lOpenSet.Remove(currentNode);
            m_lClosedSet.Add(currentNode);

            if (currentNode == endNode)
            {
                Retrace(startNode, endNode);
                return;
            }

            neighbours = m_Grid.GetNeighbours(currentNode);

            foreach (Node neighbour in neighbours)
            {
                if (neighbour.m_bIsBlocked)
                {
                    continue;
                }
                if (m_lClosedSet.Contains(neighbour))
                {
                    continue;
                }

                int _iNewMovementCost = currentNode.m_iGCost + GetDistance(currentNode, neighbour);

                if (_iNewMovementCost < neighbour.m_iGCost || !m_lOpenSet.Contains(neighbour))
                {
                    neighbour.m_iGCost  = _iNewMovementCost;
                    neighbour.m_iHCost  = GetDistance(neighbour, endNode);
                    neighbour.m_nParent = currentNode;

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

        Debug.Log("No Route!");
    }
Ejemplo n.º 3
0
    public void FindPath(Vector3 start, Vector3 end)
    {
        Node startNode = m_Grid.NodeFromWorldPos(start);
        Node endNode   = m_Grid.NodeFromWorldPos(end);

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

        // G-cost = distance from the starting node:
        startNode.m_iGCost = GetDistance(startNode, startNode);

        // H-cost = distance from the end node:
        startNode.m_iHCost = GetDistance(startNode, endNode);

        openSet.Add(startNode);

        Node        currentNode = null;
        List <Node> neighbours  = null;

        while (openSet.Count > 0)
        {
            currentNode = LowestFCost(openSet);
            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            // Are we there yet?
            if (currentNode == endNode)
            {
                ReTracePath(startNode, endNode);
                return;
            }

            neighbours = m_Grid.GetNeighbours(currentNode);

            foreach (Node neighbour in neighbours)
            {
                if (neighbour.m_isBlocked)
                {
                    continue;
                }

                if (closedSet.Contains(neighbour))
                {
                    continue;
                }

                int newMovementCost = currentNode.m_iGCost + GetDistance(currentNode, neighbour);

                if (newMovementCost < neighbour.m_iGCost ||
                    !openSet.Contains(neighbour))
                {
                    neighbour.m_iGCost = newMovementCost;
                    neighbour.m_iHCost = GetDistance(neighbour, endNode);
                    neighbour.m_Parent = currentNode;

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

        Debug.Log("No route was found!");
    }
Ejemplo n.º 4
0
    void FindPath(Vector3 start, Vector3 end)
    {
        Node startNode = m_Grid.NodeFromWorldPos(start);
        Node endNode   = m_Grid.NodeFromWorldPos(end);

        //List<Node> openSet = new List<Node>();
        MinHeap <Node> openSet   = new MinHeap <Node>(m_Grid.GridSize);
        List <Node>    closedSet = new List <Node>();

        openSet.Add(startNode);
        //int fCost = 0;

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

            //The following was replaced by heap
            //Node _currentNode = openSet[0];
            //for (int i = 1; i < openSet.Count; i++)
            //{
            //    if (openSet[i].m_ifCost < _currentNode.m_ifCost || openSet[i].m_ifCost == _currentNode.m_ifCost)
            //    {
            //        _currentNode = openSet[i];
            //    }
            //}
            //openSet.Remove(_currentNode);

            closedSet.Add(_currentNode);


            if (_currentNode == endNode)
            {
                RetracePath(startNode, endNode);
                return;
            }



            foreach (Node neighbour in m_Grid.GetNeighbour(_currentNode))
            {
                if (neighbour.m_isBlocked || closedSet.Contains(neighbour))
                {
                    continue;
                }
                int NewMovementCost = _currentNode.m_iGCost + GetDistance(_currentNode, neighbour);

                if (NewMovementCost < neighbour.m_iGCost || !openSet.Contains(neighbour))
                {
                    neighbour.m_iGCost = NewMovementCost;

                    neighbour.m_iHCost = GetDistance(neighbour, endNode);

                    neighbour.m_Parent = _currentNode;


                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                    else
                    {
                        // This was added when heap was implemented, Sorts upwards if openset contains neighbour node
                        openSet.UpdateItem(neighbour);
                    }
                }
            }
        }
    }