Guest User

Untitled

a guest
Jan 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. std::vector<Node*> m_openList;
  2. m_openList.push_back(startingNode);
  3.  
  4.  
  5. int iter = 0;
  6. while (iter < maxIter && !m_openList->empty())
  7. {
  8.     iter++;
  9.    
  10.     // Remove node from open list and put it in closed list.
  11.     Node* bestNode = m_openList->pop();
  12.     bestNode->flags &= ~NODE_OPEN;
  13.     bestNode->flags |= NODE_CLOSED;
  14.    
  15.     // Reached the goal, stop searching.
  16.     if (bestNode->id == endNodeId) {
  17.         lastBestNode = bestNode;
  18.         return true;
  19.     }
  20.    
  21.     // iterate over all neighbor nodes
  22.     for (unsigned int i = 0; i <= bestNode.neighbors.size(); ++i) {    
  23.         Node* neighbourNode = bestNode.neighbors[i];
  24.         if (!neighbourNode) {
  25.             continue;
  26.         }
  27.        
  28.         // Calculate cost and heuristic.
  29.         float cost = 0;
  30.         float heuristic = 0;
  31.        
  32.         // Special case for last node.
  33.         if (neighbour == endNode) {
  34.             // Cost
  35.             const float curCost = GetCurrentNodeCost();
  36.             const float endCost = GetEndNodeCost();
  37.            
  38.             cost = bestNode->cost + curCost + endCost;
  39.             heuristic = 0;
  40.         }
  41.         else {
  42.             // Cost
  43.             const float curCost = GetCurrentNodeCost();
  44.            
  45.             cost = bestNode->cost + curCost;
  46.             heuristic = Vdist(neighbourNode->pos, endNodePosition)*H_SCALE;
  47.         }
  48.        
  49.         const float total = cost + heuristic;
  50.        
  51.         // The node is already in open list and the new result is worse, skip.
  52.         if ((neighbourNode->flags & NODE_OPEN) && total >= neighbourNode->total)
  53.             continue;
  54.         // The node is already visited and process, and the new result is worse, skip.
  55.         if ((neighbourNode->flags & NODE_CLOSED) && total >= neighbourNode->total)
  56.             continue;
  57.        
  58.         // Add or update the node.
  59.         neighbourNode->id = neighbourRef;
  60.         neighbourNode->flags &= ~NODE_CLOSED;
  61.         neighbourNode->cost = cost;
  62.         neighbourNode->total = total;
  63.        
  64.         if (neighbourNode->flags & NODE_OPEN) {
  65.             // Already in open, update node location.
  66.             m_openList->modify(neighbourNode);
  67.         }
  68.         else {
  69.             // Put the node in open list.
  70.             neighbourNode->flags |= NODE_OPEN;
  71.             m_openList->push(neighbourNode);
  72.         }
  73.        
  74.         // Update nearest node to target so far.
  75.         if (heuristic < lastBestNodeCost) {
  76.             lastBestNodeCost = heuristic;
  77.             lastBestNode = neighbourNode;
  78.         }
  79.     }
  80. }
Add Comment
Please, Sign In to add comment