Advertisement
crackboom

Action List

Nov 24th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.41 KB | None | 0 0
  1. if( useAStar )
  2. {
  3.     if (newRequest)
  4.     {
  5.         if (m_straightline)
  6.         {
  7.             int curR, curC; //current row and column
  8.             D3DXVECTOR3 cur = m_owner->GetBody().GetPos(); //get position of pathfinder
  9.             g_terrain.GetRowColumn( &cur, &curR, &curC );  //translate pos to row/col
  10.             //optimize for straight path
  11.             if (StraightLineTest(curR, curC, r, c))
  12.             {
  13.                 m_waypointList.clear();
  14.                 D3DXVECTOR3 spot = g_terrain.GetCoordinates( r, c );
  15.                 m_waypointList.push_back( spot );
  16.                 return true;
  17.             }
  18.         }
  19.  
  20.         //reset existing search
  21.         astar->OpenList.clear();
  22.         astar->nodeDataArray.resize(g_terrain.GetWidth() * g_terrain.GetWidth());
  23.         astar->InitNodeArray(g_terrain.GetWidth());
  24.  
  25.         int curR, curC; //current row and column
  26.         D3DXVECTOR3 cur = m_owner->GetBody().GetPos(); //get position of pathfinder
  27.         g_terrain.GetRowColumn( &cur, &curR, &curC );  //translate pos to row/col
  28.  
  29.         //Push Start Node onto the Open List
  30.         //set up starting node
  31.         astar->nodeDataArray[curR * g_terrain.GetWidth() + curC].InitNode(curR, curC, -1,               -1, 0, GetHeuristicCalc(), GetHeuristicWeight(), r, c);
  32.         //this starting node is definitely going on the OpenList
  33.         astar->nodeDataArray[curR * g_terrain.GetWidth() + curC].listFlag = OnOpen;
  34.         //put the starting node on the OpenList
  35.         astar->OpenList.push_front(astar->nodeDataArray[curR * g_terrain.GetWidth() + curC]);  
  36.     }
  37.  
  38.     //While (Open List is not empty)
  39.     while (astar->OpenList.size() > 0)
  40.     {
  41.         //Pop cheapest node off Open List (parent node)
  42.         std::list<Node>::iterator it = astar->FindCheapestNodeOnOpenList();
  43.  
  44.         //If node is the Goal Node, then path found (RETURN “found”)
  45.         if (it->row == r && it->col == c)
  46.         {
  47.             DrawGridColors();   //debug draw path
  48.  
  49.                 //put final node on "closed list"
  50.             astar->nodeDataArray[it->row * g_terrain.GetWidth() + it->col].listFlag =                       OnClosed;
  51.  
  52.             //create waypoint list
  53.             CreateWaypoints(astar->nodeDataArray[it->row * g_terrain.GetWidth() + it->col]);  
  54.  
  55.             //rubberbanding smoothing
  56.             if (m_rubberband)
  57.                 RubberbandTest();
  58.  
  59.             //catmull-rom smoothing
  60.             if (m_smooth)
  61.                 ApplyCatmullRom();
  62.  
  63.             return true;
  64.         }
  65.  
  66.         //For (all neighboring child nodes)
  67.         for (int pos = 0; pos < 8; ++pos)
  68.         {
  69.             unsigned int neighborRow;
  70.             unsigned int neighborCol;
  71.             float g;
  72.             //skip out of bounds, walls, check corners, and get neightbor coords, and get g
  73.             if (!ValidLocation(pos, it->row, it->col, neighborRow, neighborCol, g))
  74.                 continue;
  75.             //skip parent
  76.             if (neighborRow == it->parentRow && neighborCol == it->parentCol)
  77.                 continue;
  78.            
  79.             //Compute its cost, f(x) = g(x) + h(x)
  80.             Node node(neighborRow, neighborCol, it->row, it->col, it->cost + g,                         GetHeuristicCalc(), GetHeuristicWeight(), r, c);
  81.             //creating a new node calculates the cost in the total member
  82.  
  83.             //If child node isn’t on Open or Closed list, put it on Open List.
  84.             if (astar->nodeDataArray[neighborRow * g_terrain.GetWidth() +                           neighborCol].listFlag == OnNone)
  85.             {
  86.                 //update the node data and put on open list
  87.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].cost                     = node.cost;
  88.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].total                    = node.total;
  89.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() +                               neighborCol].parentRow = node.parentRow;
  90.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() +                               neighborCol].parentCol = node.parentCol;
  91.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() +                               neighborCol].listFlag = OnOpen;
  92.                 astar->OpenList.push_back(astar->nodeDataArray[neighborRow *                                g_terrain.GetWidth() + neighborCol]);
  93.             }
  94.  
  95.             //If neighbor node is on Open or Closed List, AND this new one is cheaper,
  96.                 //(the if check above guarantees that it is on one of the lists, so now we              //just check for being cheaper)
  97.                 //(on open AND cheaper) OR (on closed AND cheaper)
  98.             else if ( astar->nodeDataArray[neighborRow * g_terrain.GetWidth() +                         neighborCol].listFlag != OnNone && node.total <
  99.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() +                           neighborCol].total )
  100.             {
  101.                 //"remove from closed list" is just changing the listFlag, done when updating               //the nodeDataArray for this node in the next lines
  102.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].cost =                   node.cost;
  103.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].total                    = node.total;
  104.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() +                               neighborCol].parentRow = node.parentRow;
  105.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() +                               neighborCol].parentCol = node.parentCol;
  106.                 astar->nodeDataArray[neighborRow * g_terrain.GetWidth() +                               neighborCol].listFlag = OnOpen;
  107.                 astar->OpenList.push_back(astar->nodeDataArray[neighborRow *                                g_terrain.GetWidth() + neighborCol]);
  108.             }
  109.         }
  110.  
  111.         //Place parent node on the Closed List (we’re done with it)
  112.         astar->nodeDataArray[it->row * g_terrain.GetWidth() + it->col].listFlag = OnClosed;
  113.         astar->OpenList.erase(it);  //cheapest node on open list was "popped", so remove it
  114.  
  115.         //If taken too much time this frame (or in single step mode), abort search for now and          //resume next frame (RETURN “working”)
  116.         if (!m_singleStep)
  117.         {
  118.             DrawGridColors();  //debug draw this frame
  119.             return false;
  120.         }
  121.     }
  122.  
  123.     //Open List empty, thus no path possible (RETURN “fail”)
  124.     return false;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement