Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if( useAStar )
- {
- if (newRequest)
- {
- if (m_straightline)
- {
- int curR, curC; //current row and column
- D3DXVECTOR3 cur = m_owner->GetBody().GetPos(); //get position of pathfinder
- g_terrain.GetRowColumn( &cur, &curR, &curC ); //translate pos to row/col
- //optimize for straight path
- if (StraightLineTest(curR, curC, r, c))
- {
- m_waypointList.clear();
- D3DXVECTOR3 spot = g_terrain.GetCoordinates( r, c );
- m_waypointList.push_back( spot );
- return true;
- }
- }
- //reset existing search
- astar->OpenList.clear();
- astar->nodeDataArray.resize(g_terrain.GetWidth() * g_terrain.GetWidth());
- astar->InitNodeArray(g_terrain.GetWidth());
- int curR, curC; //current row and column
- D3DXVECTOR3 cur = m_owner->GetBody().GetPos(); //get position of pathfinder
- g_terrain.GetRowColumn( &cur, &curR, &curC ); //translate pos to row/col
- //Push Start Node onto the Open List
- //set up starting node
- astar->nodeDataArray[curR * g_terrain.GetWidth() + curC].InitNode(curR, curC, -1, -1, 0, GetHeuristicCalc(), GetHeuristicWeight(), r, c);
- //this starting node is definitely going on the OpenList
- astar->nodeDataArray[curR * g_terrain.GetWidth() + curC].listFlag = OnOpen;
- //put the starting node on the OpenList
- astar->OpenList.push_front(astar->nodeDataArray[curR * g_terrain.GetWidth() + curC]);
- }
- //While (Open List is not empty)
- while (astar->OpenList.size() > 0)
- {
- //Pop cheapest node off Open List (parent node)
- std::list<Node>::iterator it = astar->FindCheapestNodeOnOpenList();
- //If node is the Goal Node, then path found (RETURN “found”)
- if (it->row == r && it->col == c)
- {
- DrawGridColors(); //debug draw path
- //put final node on "closed list"
- astar->nodeDataArray[it->row * g_terrain.GetWidth() + it->col].listFlag = OnClosed;
- //create waypoint list
- CreateWaypoints(astar->nodeDataArray[it->row * g_terrain.GetWidth() + it->col]);
- //rubberbanding smoothing
- if (m_rubberband)
- RubberbandTest();
- //catmull-rom smoothing
- if (m_smooth)
- ApplyCatmullRom();
- return true;
- }
- //For (all neighboring child nodes)
- for (int pos = 0; pos < 8; ++pos)
- {
- unsigned int neighborRow;
- unsigned int neighborCol;
- float g;
- //skip out of bounds, walls, check corners, and get neightbor coords, and get g
- if (!ValidLocation(pos, it->row, it->col, neighborRow, neighborCol, g))
- continue;
- //skip parent
- if (neighborRow == it->parentRow && neighborCol == it->parentCol)
- continue;
- //Compute its cost, f(x) = g(x) + h(x)
- Node node(neighborRow, neighborCol, it->row, it->col, it->cost + g, GetHeuristicCalc(), GetHeuristicWeight(), r, c);
- //creating a new node calculates the cost in the total member
- //If child node isn’t on Open or Closed list, put it on Open List.
- if (astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].listFlag == OnNone)
- {
- //update the node data and put on open list
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].cost = node.cost;
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].total = node.total;
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].parentRow = node.parentRow;
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].parentCol = node.parentCol;
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].listFlag = OnOpen;
- astar->OpenList.push_back(astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol]);
- }
- //If neighbor node is on Open or Closed List, AND this new one is cheaper,
- //(the if check above guarantees that it is on one of the lists, so now we //just check for being cheaper)
- //(on open AND cheaper) OR (on closed AND cheaper)
- else if ( astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].listFlag != OnNone && node.total <
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].total )
- {
- //"remove from closed list" is just changing the listFlag, done when updating //the nodeDataArray for this node in the next lines
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].cost = node.cost;
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].total = node.total;
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].parentRow = node.parentRow;
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].parentCol = node.parentCol;
- astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol].listFlag = OnOpen;
- astar->OpenList.push_back(astar->nodeDataArray[neighborRow * g_terrain.GetWidth() + neighborCol]);
- }
- }
- //Place parent node on the Closed List (we’re done with it)
- astar->nodeDataArray[it->row * g_terrain.GetWidth() + it->col].listFlag = OnClosed;
- astar->OpenList.erase(it); //cheapest node on open list was "popped", so remove it
- //If taken too much time this frame (or in single step mode), abort search for now and //resume next frame (RETURN “working”)
- if (!m_singleStep)
- {
- DrawGridColors(); //debug draw this frame
- return false;
- }
- }
- //Open List empty, thus no path possible (RETURN “fail”)
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement