int Ghost::SetTarget(){ // std::cout<<"target set"<<std::endl; AStarSearch<MapSearchNode> astarsearch; unsigned int SearchCount = 0; const unsigned int NumSearches = 1; while(SearchCount < NumSearches) { // Create a start state MapSearchNode nodeStart; nodeStart.x = m_X/30; nodeStart.y = m_Y/30; // Define the goal state MapSearchNode nodeEnd; if(pill==0&&!Dead){ nodeEnd.x = targetFx; nodeEnd.y = targetFy; } if(pill==1&&!Dead){ nodeEnd.x = targetFy; nodeEnd.y = targetFx; } if(Dead){ nodeEnd.x = 12; // targetFx=12; // targetFy=9; nodeEnd.y = 10; } // Set Start and goal states astarsearch.SetStartAndGoalStates( nodeStart, nodeEnd ); unsigned int SearchState; unsigned int SearchSteps = 0; do { SearchState = astarsearch.SearchStep(); SearchSteps++; #if DEBUG_LISTS cout << "Steps:" << SearchSteps << "\n"; int len = 0; cout << "Open:\n"; MapSearchNode *p = astarsearch.GetOpenListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY // ((MapSearchNode *)p)->PrintNodeInfo(); #endif p = astarsearch.GetOpenListNext(); } cout << "Open list has " << len << " nodes\n"; len = 0; cout << "Closed:\n"; p = astarsearch.GetClosedListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY // p->PrintNodeInfo(); #endif p = astarsearch.GetClosedListNext(); } cout << "Closed list has " << len << " nodes\n"; #endif } while( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING ); if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED ) { // cout << "Search found goal state\n"; MapSearchNode *node = astarsearch.GetSolutionStart(); #if DISPLAY_SOLUTION cout << "Displaying solution\n"; #endif // node->PrintNodeInfo(); node = astarsearch.GetSolutionNext(); if(!node){ return 1; } if(node){ targetNx=node->RetX(); targetNy=node->RetY(); // node->PrintNodeInfo(); if(Dead) if(targetNx==12&&targetNy==10) Reset(); } /* cout << "for reference \n"; for( ;; ) { node = astarsearch.GetSolutionNext(); if( !node ) { break; } node->PrintNodeInfo(); }; */ // Once you're done with the solution you can free the nodes up astarsearch.FreeSolutionNodes(); } else if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED ) { cout << "Search terminated. Did not find goal state\n"; } // Display the number of loops the search went through // cout << "SearchSteps : " << SearchSteps << "\n"; SearchCount ++; astarsearch.EnsureMemoryFreed(); } return 0; }
//------------------------------------------------------------------------------------- int NavTileHandle::findStraightPath(int layer, const Position3D& start, const Position3D& end, std::vector<Position3D>& paths) { setMapLayer(layer); pCurrNavTileHandle = this; if(pCurrNavTileHandle->pTilemap->GetNumLayers() < layer + 1) { ERROR_MSG(fmt::format("NavTileHandle::findStraightPath: not found layer({})\n", layer)); return NAV_ERROR; } // Create a start state nodeStart.x = int(start.x / pTilemap->GetTileWidth()); nodeStart.y = int(start.z / pTilemap->GetTileHeight()); // Define the goal state nodeGoal.x = int(end.x / pTilemap->GetTileWidth()); nodeGoal.y = int(end.z / pTilemap->GetTileHeight()); //DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: start({}, {}), end({}, {})\n", // nodeStart.x, nodeStart.y, nodeGoal.x, nodeGoal.y)); // Set Start and goal states astarsearch.SetStartAndGoalStates(nodeStart, nodeGoal); unsigned int SearchState; unsigned int SearchSteps = 0; do { SearchState = astarsearch.SearchStep(); SearchSteps++; #if DEBUG_LISTS DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: Steps: {}\n", SearchSteps)); int len = 0; DEBUG_MSG("NavTileHandle::findStraightPath: Open:\n"); MapSearchNode *p = astarsearch.GetOpenListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY ((MapSearchNode *)p)->printNodeInfo(); #endif p = astarsearch.GetOpenListNext(); } DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: Open list has {} nodes\n", len)); len = 0; DEBUG_MSG("NavTileHandle::findStraightPath: Closed:\n"); p = astarsearch.GetClosedListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY p->printNodeInfo(); #endif p = astarsearch.GetClosedListNext(); } DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: Closed list has {} nodes\n", len)); #endif } while( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING ); if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED ) { //DEBUG_MSG("NavTileHandle::findStraightPath: Search found goal state\n"); MapSearchNode *node = astarsearch.GetSolutionStart(); int steps = 0; //node->PrintNodeInfo(); for( ;; ) { node = astarsearch.GetSolutionNext(); if( !node ) { break; } //node->PrintNodeInfo(); steps ++; paths.push_back(Position3D((float)node->x * pTilemap->GetTileWidth(), 0, (float)node->y * pTilemap->GetTileWidth())); }; // DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: Solution steps {}\n", steps)); // Once you're done with the solution you can free the nodes up astarsearch.FreeSolutionNodes(); } else if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED ) { ERROR_MSG("NavTileHandle::findStraightPath: Search terminated. Did not find goal state\n"); } // Display the number of loops the search went through // DEBUG_MSG(fmt::format("NavTileHandle::findStraightPath: SearchSteps: {}\n", SearchSteps)); astarsearch.EnsureMemoryFreed(); return 0; }
int PathSearch(void* data) { std::vector<Vector2D> result; auto enemy = static_cast<Enemy*>(data); auto params = enemy->GetPathSearchParams(); auto mapData = enemy->GetMap()->GetMapForAStarSearch(); unsigned int SearchCount = 0; const unsigned int NumSearches = 1; AStarSearch<MapSearchNode> m_astarsearch; /////////////////////////////////////////// /*ofstream outputFile; outputFile.open("mapData.txt"); int idx = 0; for (int i=0; i<params.mapWidth*params.mapHeight; i++) { outputFile << mapData[i]; if((i+1)%params.mapWidth == 0) { outputFile << endl; } } outputFile.close();*/ /////////////////////////////////////////// while(SearchCount < NumSearches) { // Create a start state MapSearchNode nodeStart(params.start.x, params.start.y, params.mapWidth, params.mapHeight, mapData); //nodeStart.SetMap(params.mapWidth, params.mapHeight, mapData); //nodeStart.x = params.start.x; //nodeStart.y = params.start.y; // Define the goal state MapSearchNode nodeEnd(params.end.x, params.end.y, params.mapWidth, params.mapHeight, mapData); //nodeEnd.SetMap(params.mapWidth, params.mapHeight, mapData); //nodeEnd.x = params.end.x; //nodeEnd.y = params.end.y; // Set Start and goal states m_astarsearch.SetStartAndGoalStates(nodeStart, nodeEnd); unsigned int SearchState; unsigned int SearchSteps = 0; do { SearchState = m_astarsearch.SearchStep(); SearchSteps++; } while(SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING); if(SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED) { MapSearchNode* node = m_astarsearch.GetSolutionStart(); int steps = 0; //node->PrintNodeInfo(); result.push_back(node->GetNodeInfo()); for(;;) { node = m_astarsearch.GetSolutionNext(); if(!node) { break; } //node->PrintNodeInfo(); result.push_back(node->GetNodeInfo()); steps ++; }; // Once you're done with the solution you can free the nodes up m_astarsearch.FreeSolutionNodes(); } SearchCount++; m_astarsearch.EnsureMemoryFreed(); } enemy->SetPathSearchResult(result); return 0; }
bool ast::astar( uint8_t* map, uint32_t width, uint32_t height, const point_t start, const point_t goal, std::vector<point_t>& path ) { //cout << "STL A* Search implementation\n(C)2001 Justin Heyes-Jones\n"; // set the static vars _map = map; _map_width = width; _map_height = height; // Our sample problem defines the world as a 2d array representing a terrain // Each element contains an integer from 0 to 5 which indicates the cost // of travel across the terrain. Zero means the least possible difficulty // in travelling (think ice rink if you can skate) whilst 5 represents the // most difficult. 9 indicates that we cannot pass. // Create an instance of the search class... AStarSearch<MapSearchNode> astarsearch; unsigned int SearchCount = 0; const unsigned int NumSearches = 1; bool path_found = false; while(SearchCount < NumSearches) { // Create a start state MapSearchNode nodeStart; nodeStart.x = start.x; nodeStart.y = start.y; // Define the goal state MapSearchNode nodeEnd; nodeEnd.x = goal.x; nodeEnd.y = goal.y; // Set Start and goal states astarsearch.SetStartAndGoalStates( nodeStart, nodeEnd ); unsigned int SearchState; unsigned int SearchSteps = 0; do { SearchState = astarsearch.SearchStep(); SearchSteps++; #if DEBUG_LISTS cout << "Steps:" << SearchSteps << "\n"; int len = 0; cout << "Open:\n"; MapSearchNode *p = astarsearch.GetOpenListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY ((MapSearchNode *)p)->PrintNodeInfo(); #endif p = astarsearch.GetOpenListNext(); } cout << "Open list has " << len << " nodes\n"; len = 0; cout << "Closed:\n"; p = astarsearch.GetClosedListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY p->PrintNodeInfo(); #endif p = astarsearch.GetClosedListNext(); } cout << "Closed list has " << len << " nodes\n"; #endif } while( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING ); if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED ) { //cout << "Search found goal state\n"; MapSearchNode *node = astarsearch.GetSolutionStart(); #if DISPLAY_SOLUTION cout << "Displaying solution\n"; #endif int steps = 0; //node->PrintNodeInfo(); path.push_back( point_t( node->x, node->y ) ); for( ;; ) { node = astarsearch.GetSolutionNext(); if( !node ) { break; } //node->PrintNodeInfo(); path.push_back( point_t( node->x, node->y ) ); steps ++; }; //cout << "Solution steps " << steps << endl; // Once you're done with the solution you can free the nodes up astarsearch.FreeSolutionNodes(); path_found = true; } else if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED ) { cout << "Search terminated. Did not find goal state\n"; } // Display the number of loops the search went through //cout << "SearchSteps : " << SearchSteps << "\n"; SearchCount ++; astarsearch.EnsureMemoryFreed(); } return path_found; }
int main( int argc, char *argv[] ) { cout << "STL A* Search implementation\n(C)2001 Justin Heyes-Jones\n"; // Our sample problem defines the world as a 2d array representing a terrain // Each element contains an integer from 0 to 5 which indicates the cost // of travel across the terrain. Zero means the least possible difficulty // in travelling (think ice rink if you can skate) whilst 5 represents the // most difficult. 9 indicates that we cannot pass. // Create an instance of the search class... AStarSearch<MapSearchNode> astarsearch; unsigned int SearchCount = 0; const unsigned int NumSearches = 1; while(SearchCount < NumSearches) { // Create a start state MapSearchNode nodeStart; nodeStart.x = rand()%MAP_WIDTH; nodeStart.y = rand()%MAP_HEIGHT; // Define the goal state MapSearchNode nodeEnd; nodeEnd.x = rand()%MAP_WIDTH; nodeEnd.y = rand()%MAP_HEIGHT; // Set Start and goal states astarsearch.SetStartAndGoalStates( nodeStart, nodeEnd ); unsigned int SearchState; unsigned int SearchSteps = 0; do { SearchState = astarsearch.SearchStep(); SearchSteps++; #if DEBUG_LISTS cout << "Steps:" << SearchSteps << "\n"; int len = 0; cout << "Open:\n"; MapSearchNode *p = astarsearch.GetOpenListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY ((MapSearchNode *)p)->PrintNodeInfo(); #endif p = astarsearch.GetOpenListNext(); } cout << "Open list has " << len << " nodes\n"; len = 0; cout << "Closed:\n"; p = astarsearch.GetClosedListStart(); while( p ) { len++; #if !DEBUG_LIST_LENGTHS_ONLY p->PrintNodeInfo(); #endif p = astarsearch.GetClosedListNext(); } cout << "Closed list has " << len << " nodes\n"; #endif } while( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING ); if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED ) { cout << "Search found goal state\n"; MapSearchNode *node = astarsearch.GetSolutionStart(); #if DISPLAY_SOLUTION cout << "Displaying solution\n"; #endif int steps = 0; node->PrintNodeInfo(); for( ;; ) { node = astarsearch.GetSolutionNext(); if( !node ) { break; } node->PrintNodeInfo(); steps ++; }; cout << "Solution steps " << steps << endl; // Once you're done with the solution you can free the nodes up astarsearch.FreeSolutionNodes(); } else if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED ) { cout << "Search terminated. Did not find goal state\n"; } // Display the number of loops the search went through cout << "SearchSteps : " << SearchSteps << "\n"; SearchCount ++; astarsearch.EnsureMemoryFreed(); } return 0; }
GMEXPORT double CalculatePathFromXYtoXY(double x1, double y1, double x2, double y2) { if (x1 != x2 || y1 != y2) { m_PathList.clear(); std::map<int, std::map<int, MapSearchNode*> > grid; for (int i = 0; i < 42; i++) { for (int j = 0; j < 34; j++) { grid[i][j] = new MapSearchNode(); grid[i][j]->x = i; grid[i][j]->y = j; } } ofstream fileStream; fileStream.open("level_paths.txt"); // define the new nodes MapSearchNode* start = new MapSearchNode(); start->x = x1; start->y = y1; fileStream << "START"; fileStream << " START X: "; fileStream << start->x; fileStream << " START Y: "; fileStream << start->y; MapSearchNode* end = new MapSearchNode(); end->x = x2; end->y = y2; fileStream << "END"; fileStream << " END X: "; fileStream << end->x; fileStream << " END Y: "; fileStream << end->y; MapSearchNode* current = new MapSearchNode(); MapSearchNode* child = new MapSearchNode(); std::list<MapSearchNode*> openList; std::list<MapSearchNode*> closedList; list<MapSearchNode*>::iterator i; unsigned int n = 0; openList.push_back(start); start->opened = true; while (n == 0 || (current != end && n < 50)) { // Look for the smallest f value in the openList for (i = openList.begin(); i != openList.end(); i++) { if (i == openList.begin() || (*i)->GetFScore() <= current->GetFScore()) { current = (*i); } } fileStream << "searching"; fileStream << " Current X: "; fileStream << current->x; fileStream << " Current Y: "; fileStream << current->y; // Stop if we've reached the end if (current->x == end->x && current->y == end->y) { fileStream << "end reached"; break; } // Remove the current point from the open list openList.remove(current); current->opened = false; // Add the current point from the open list closedList.push_back(current); current->closed = true; // Get all the current adjacent walkable points for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { if (x == 0 && y == 0) { // ignore current node, pass continue; } if (x == 0 || y == 0) { child = grid[current->x + x][current->y + y]; // if it's closed or not walkable then pass if (child->closed || (spmap[child->x][child->y] != 0 && spmap[child->x][child->y] != 3 && spmap[child->x][child->y] != 4 && spmap[child->x][child->y] != 2 && spmap[child->x][child->y] != 9)) { fileStream << "\n"; fileStream << "closed or not walkable"; continue; } // IF AT A CORNER? // if it's already in the opened list if (child->opened) { if (child->gScore > child->GetGScore(current)) { child->parent = current; child->ComputeScores(end); } } else { openList.push_back(child); child->opened = true; // COMPUTE THE G child->parent = current; child->ComputeScores(end); } } } } n++; fileStream << "\n"; } // Reset for (i = openList.begin(); i != openList.end(); i++) { (*i)->opened = false; } for (i = closedList.begin(); i != closedList.end(); i++) { (*i)->closed = false; } fileStream.close(); fileStream.open("level_path.txt"); // resolve the path starting from the end point while (current->parent && current != start) { fileStream << "X "; fileStream << current->x; fileStream << " Y "; fileStream << current->y; fileStream << "\n"; m_PathList.push_back(current); current = current->parent; n ++; } fileStream.close(); return 0; } return 0; }