00001 #ifndef __ASTAR_SEARCH_MANAGER_H__
00002 #define __ASTAR_SEARCH_MANAGER_H__
00003
00004 #pragma once
00005
00006 #include "AStarSearch.h"
00007
00008 #include <string>
00009 #include <map>
00010
00011 #include "OgreException.h"
00012 using Ogre::Exception;
00013 using std::string;
00014 using std::map;
00015
00016
00026 template<typename UserState>
00027 class AStarSearchManager
00028 {
00029
00030
00031 private:
00032 typedef map< string, AStarSearch<UserState>* > SearchMap;
00033 SearchMap mSearchList;
00034
00035
00036
00037
00038 public:
00039 AStarSearchManager ()
00040 {
00041 mSearchList.clear ();
00042 }
00043
00044 ~AStarSearchManager ()
00045 {
00046 destroyAllSearchs ();
00047 }
00048
00049
00050
00051 public:
00052 AStarSearch<UserState>* createSearch ( string name, UserState* startState, UserState* goalState )
00053 {
00054 SearchMap::iterator iter = mSearchList.find ( name );
00055 if ( iter != mSearchList.end() )
00056 throw Exception ( Exception::ERR_ITEM_NOT_FOUND, "Search already exists", "AStarSearchManager::createSearch" );
00057
00058 AStarSearch<UserState>* newSearch = new AStarSearch<UserState> ();
00059 newSearch->setStartState( startState, goalState );
00060 mSearchList[name] = newSearch;
00061
00062 return newSearch;
00063 }
00064
00065
00066
00067 void destroySearch ( string name )
00068 {
00069 SearchMap::iterator iter = mSearchList.find ( name );
00070 if ( iter == mSearchList.end() )
00071 throw Exception ( Exception::ERR_ITEM_NOT_FOUND, "Search doesn't exists", "AStarSearchManager::destroySearch" );
00072
00073 delete (*iter).second;
00074 mSearchList.erase ( iter );
00075 }
00076
00077
00078
00079 void destroyAllSearchs ()
00080 {
00081 SearchMap::iterator iter = mSearchList.begin ();
00082 while ( iter != mSearchList.end() )
00083 {
00084 delete (*iter).second;
00085 iter++;
00086 }
00087 mSearchList.clear ();
00088 }
00089
00090
00091
00092 bool isSearchSolved ( string name )
00093 {
00094 return ( _getSearch(name)->getSearchState() == AStarSearch<UserState>::SS_PATH_FOUND );
00095 }
00096
00097
00098
00099 bool isSearchUnsolvable ( string name )
00100 {
00101 return ( _getSearch(name)->getSearchState() == AStarSearch<UserState>::SS_PATH_NOT_FOUND );
00102 }
00103
00104
00105
00106 void advanceSearch ( string searchName, int numSteps = 1 )
00107 {
00108 _advanceSearch ( _getSearch(searchName), numSteps );
00109 }
00110
00111
00112
00113 void advanceAllSearchs ( int numSteps = 1 )
00114 {
00115 SearchMap::iterator iter = mSearchList.begin ();
00116
00117 while ( iter != mSearchList.end() )
00118 {
00119 _advanceSearch ( (*iter).second, numSteps );
00120 iter++;
00121 }
00122 }
00123
00124
00125
00126 const WaypointList<UserState>* getSolutionPath ( string name )
00127 {
00128 return _getSearch(name)->getSolutionPath();
00129 }
00130
00131
00132 AStarSearch<UserState>* getSearch ( string name )
00133 {
00134 SearchMap::iterator iter = mSearchList.find ( name );
00135 if ( iter == mSearchList.end() )
00136 throw Exception ( Exception::ERR_ITEM_NOT_FOUND, "Search not found", "AStarSearchManager::getSearch" );
00137
00138 return (*iter).second;
00139 }
00140
00141
00142
00143
00144 private:
00145 void _advanceSearch ( AStarSearch<UserState>* search, int numSteps )
00146 {
00147 for ( int i = 0; i < numSteps; i++ )
00148 {
00149
00150
00151 if ( search->isRunnable() )
00152 search->advanceSearch ();
00153 else
00154 break;
00155 }
00156 }
00157 };
00158
00159 #endif