Changeset 3849
- Timestamp:
- 02/22/12 17:59:24 (16 months ago)
- Location:
- branches/active/cell_pathfinding/engine/core/pathfinder/routepather
- Files:
-
- 4 edited
-
routepather.cpp (modified) (6 diffs)
-
routepather.h (modified) (5 diffs)
-
routepathersearch.cpp (modified) (4 diffs)
-
routepathersearch.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/active/cell_pathfinding/engine/core/pathfinder/routepather/routepather.cpp
r3621 r3849 32 32 #include "model/structures/instance.h" 33 33 #include "model/structures/layer.h" 34 35 #include "pathfinder/searchspace.h" 34 #include "model/structures/cellcache.h" 36 35 37 36 #include "routepather.h" … … 39 38 40 39 namespace FIFE { 41 void RoutePather::setMap(Map* map) {42 if(!map) {43 return;44 }45 m_map = map;46 }47 40 48 41 int32_t RoutePather::makeSessionId() { … … 51 44 52 45 void RoutePather::makePlan(const Instance *instance, const Location& target, int32_t session_id, int32_t priority) { 53 SearchSpace* searchspace = getSearchSpace(target.getLayer()); 54 if(!searchspace) { 55 searchspace = new SearchSpace(target.getLayer()); 56 addSearchSpace(searchspace); 57 } 58 if(searchspace->isInSearchSpace(target)) { 59 RoutePatherSearch* newSearch = new RoutePatherSearch(session_id, instance->getLocation(), target, searchspace); 46 Cellcache* cache = instance->getLocation().getLayer()->getCellcache(); 47 if (!cache) { 48 return; 49 } 50 if (cache->isInCellcache(target)) { 51 RoutePatherSearch* newSearch = new RoutePatherSearch(session_id, instance->getLocation(), target, cache); 60 52 m_sessions.pushElement(SessionQueue::value_type(newSearch, priority)); 61 53 addSessionId(session_id); … … 65 57 66 58 bool RoutePather::locationsEqual(const Location &a, const Location &b) { 67 68 59 const ModelCoordinate a_coord = a.getLayerCoordinates(); 69 60 const ModelCoordinate b_coord = b.getLayerCoordinates(); … … 74 65 bool RoutePather::testStep(const Instance *instance, Path& path) { 75 66 Location instanceLoc = instance->getLocation(); 76 if(!path.empty() && 77 !locationsEqual(path.front(), instanceLoc) && 78 instanceLoc.getLayer()->cellContainsBlockingInstance(path.front().getLayerCoordinates())) { 67 if (!path.empty() && 68 !locationsEqual(path.front(), instanceLoc) && 69 instanceLoc.getLayer()->cellContainsBlockingInstance(path.front().getLayerCoordinates())) { 70 79 71 const bool last_step = path.front() == path.back(); 80 72 path.clear(); … … 247 239 return false; 248 240 } 249 250 bool RoutePather::addSearchSpace(SearchSpace* search_space) {251 std::pair<SearchSpaceMap::iterator, bool> res = m_searchspaces.insert(SearchSpaceMap::value_type(search_space->getLayer(), search_space));252 253 return res.second;254 }255 256 SearchSpace* RoutePather::getSearchSpace(Layer * const layer) {257 SearchSpaceMap::iterator i = m_searchspaces.find(layer);258 if(i == m_searchspaces.end()) {259 return 0;260 }261 return i->second;262 }263 241 } -
branches/active/cell_pathfinding/engine/core/pathfinder/routepather/routepather.h
r3689 r3849 40 40 namespace FIFE { 41 41 42 class Search; 43 class SearchSpace; 42 class Cellcache; 44 43 class RoutePatherSearch; 45 44 … … 52 51 } 53 52 54 void setMap(Map* map);55 53 int32_t getNextLocation(const Instance* instance, const Location& target, 56 54 double distance_to_travel, Location& nextLocation, … … 76 74 bool cancelSession(const int32_t session_id); 77 75 78 /** Adds a search space to the route pather.79 *80 * @param search_space A pointer to the search space to add.81 * @return a boolean signifying whether the search space was correctly added or not.82 */83 bool addSearchSpace(SearchSpace* search_space);84 85 /** Retrieves a searchspace associated with the given layer.86 *87 * @param A pointer to the search space88 * @return A pointer to the search space if it could be found, 0 otherwise.89 */90 SearchSpace* getSearchSpace(Layer * const layer);91 92 76 std::string getName() const { return "RoutePather"; }; 93 77 private: … … 96 80 typedef std::list<int32_t> SessionList; 97 81 typedef std::map<int32_t, Path> PathMap; 98 typedef std::map<Layer*, SearchSpace*> SearchSpaceMap;99 82 typedef std::map<int32_t, Location> LocationMap; 100 83 /** Makes the instance follow the given path. … … 161 144 162 145 //The map the search is running on. 163 Map* m_map;146 Map* m_map; 164 147 165 148 //A map of currently running sessions (searches). 166 SessionQueue m_sessions;149 SessionQueue m_sessions; 167 150 168 151 //A list of session ids that have been registered. 169 SessionList m_registeredSessionIds;152 SessionList m_registeredSessionIds; 170 153 171 154 //Calculated paths for the movement phase. 172 PathMap m_paths;155 PathMap m_paths; 173 156 174 157 //The endpoints for which those paths were calculated 175 LocationMap m_path_targets; 176 177 //A map of searchspaces. 178 SearchSpaceMap m_searchspaces; 158 LocationMap m_path_targets; 179 159 180 160 //The next free session id. 181 int32_t m_nextFreeSessionId;161 int32_t m_nextFreeSessionId; 182 162 183 163 //The maximum number of ticks allowed. 184 int32_t m_maxticks;164 int32_t m_maxticks; 185 165 }; 186 166 } -
branches/active/cell_pathfinding/engine/core/pathfinder/routepather/routepathersearch.cpp
r3624 r3849 32 32 #include "model/structures/layer.h" 33 33 #include "model/structures/instancetree.h" 34 #include "model/structures/cellcache.h" 34 35 #include "model/metamodel/object.h" 35 #include "pathfinder/searchspace.h"36 36 #include "pathfinder/heuristic.h" 37 37 #include "util/math/fife_math.h" … … 40 40 41 41 namespace FIFE { 42 RoutePatherSearch::RoutePatherSearch(const int32_t session_id, const Location& from, const Location& to, SearchSpace* searchSpace)43 :m_to(to),44 m_from(from),45 m_sessionId(session_id),46 m_searchspace(searchSpace),47 m_status(search_status_incomplete),48 m_startCoordInt(searchSpace->convertCoordToInt(from.getLayerCoordinates())),49 m_destCoordInt(searchSpace->convertCoordToInt(to.getLayerCoordinates())),50 m_next(0),51 m_heuristic(Heuristic::getHeuristic(searchSpace->getLayer()->getCellGrid()->getType()))52 { 42 RoutePatherSearch::RoutePatherSearch(const int32_t session_id, const Location& from, const Location& to, Cellcache* cache): 43 m_to(to), 44 m_from(from), 45 m_sessionId(session_id), 46 m_cellcache(cache), 47 m_status(search_status_incomplete), 48 m_startCoordInt(cache->convertCoordToInt(from.getLayerCoordinates())), 49 m_destCoordInt(cache->convertCoordToInt(to.getLayerCoordinates())), 50 m_next(0), 51 m_heuristic(Heuristic::getHeuristic(cache->getLayer()->getCellGrid()->getType())) { 52 53 53 m_sortedfrontier.pushElement(PriorityQueue<int32_t, double>::value_type(m_startCoordInt, 0.0)); 54 int32_t max_index = m_ searchspace->getMaxIndex();55 m_spt.resize(max_index + 1, -1);56 m_sf.resize(max_index + 1, -1);57 m_gCosts.resize(max_index + 1, 0.0f);;54 int32_t max_index = m_cellcache->getMaxIndex(); 55 m_spt.resize(max_index, -1); 56 m_sf.resize(max_index, -1); 57 m_gCosts.resize(max_index, 0.0f);; 58 58 } 59 59 … … 75 75 //use destination layer for getting the cell coordinates for now, this should be moved 76 76 //into search space. 77 ModelCoordinate nextCoord = m_ searchspace->convertIntToCoord(m_next);77 ModelCoordinate nextCoord = m_cellcache->convertIntToCoord(m_next); 78 78 std::vector<ModelCoordinate> adjacents; 79 m_searchspace->getLayer()->getCellGrid()->getAccessibleCoordinates(nextCoord, adjacents); 79 m_cellcache->getLayer()->getCellGrid()->getAccessibleCoordinates(nextCoord, adjacents); 80 Location loc(m_cellcache->getLayer()); 80 81 for(std::vector<ModelCoordinate>::iterator i = adjacents.begin(); i != adjacents.end(); ++i) { 81 82 //first determine if coordinate is in search space. 82 Location loc;83 loc.setLayer(m_searchspace->getLayer());84 83 loc.setLayerCoordinates((*i)); 85 int32_t adjacentInt = m_ searchspace->convertCoordToInt((*i));86 if (m_searchspace->isInSearchSpace(loc)) {84 int32_t adjacentInt = m_cellcache->convertCoordToInt((*i)); 85 if (m_cellcache->isInCellcache(loc)) { 87 86 if((adjacentInt == m_next || loc.getLayer()->cellContainsBlockingInstance(loc.getLayerCoordinates())) && 88 87 adjacentInt != m_destCoordInt) { 89 88 continue; 90 89 } 91 double hCost = m_heuristic->calculate((*i), destCoord);90 double hCost = m_heuristic->calculate((*i), destCoord); 92 91 //float hCost = Heuristic::getHeuristic(m_searchspace->getLayer()->getCellGrid()->getType())->calculate((*i), destCoord); 93 92 double gCost = m_gCosts[m_next] + loc.getLayer()->getCellGrid()->getAdjacentCost(nextCoord, (*i)); … … 114 113 to.setExactLayerCoordinates(FIFE::intPt2doublePt(to.getLayerCoordinates())); 115 114 path.push_back(to); 115 Location newnode(m_cellcache->getLayer()); 116 116 while(current != end) { 117 if(m_spt[current] < 0 ) { 118 // This is when the size of m_spt can not handle the distance of the location 119 setSearchStatus(search_status_failed); 120 break; 121 } 122 current = m_spt[current]; 123 Location newnode; 124 newnode.setLayer(m_searchspace->getLayer()); 125 ModelCoordinate currentCoord = m_searchspace->convertIntToCoord(current); 117 if(m_spt[current] < 0 ) { 118 // This is when the size of m_spt can not handle the distance of the location 119 setSearchStatus(search_status_failed); 120 break; 121 } 122 current = m_spt[current]; 123 ModelCoordinate currentCoord = m_cellcache->convertIntToCoord(current); 126 124 newnode.setLayerCoordinates(currentCoord); 127 125 path.push_front(newnode); 128 }126 } 129 127 path.front().setExactLayerCoordinates(m_from.getExactLayerCoordinates()); 130 128 return path; -
branches/active/cell_pathfinding/engine/core/pathfinder/routepather/routepathersearch.h
r3624 r3849 35 35 namespace FIFE { 36 36 37 class Map; 38 class SearchSpace; 37 class Cellcache; 39 38 class Heuristic; 40 39 … … 45 44 class RoutePatherSearch { 46 45 public: 47 RoutePatherSearch(const int32_t session_id, const Location& from, const Location& to, SearchSpace* searchSpace);46 RoutePatherSearch(const int32_t session_id, const Location& from, const Location& to, Cellcache* cache); 48 47 49 typedef std::list<Location> Path; 50 /** An enumeration of the different status the search can be in. 51 * 52 */ 53 enum SearchStatus { 54 search_status_failed, 55 search_status_complete, 56 search_status_incomplete 57 }; 48 typedef std::list<Location> Path; 49 50 /** An enumeration of the different status the search can be in. 51 * 52 */ 53 enum SearchStatus { 54 search_status_failed, 55 search_status_complete, 56 search_status_incomplete 57 }; 58 58 59 59 virtual void updateSearch(); … … 61 61 virtual Path calcPath(); 62 62 63 /** Retrieves the session id. 64 * 65 * @return The searches session id in the pather. 66 */ 67 int32_t getSessionId() const { 68 return m_sessionId; 69 } 63 /** Retrieves the session id. 64 * 65 * @return The searches session id in the pather. 66 */ 67 int32_t getSessionId() const { return m_sessionId; } 70 68 71 /** Retrieves the pather. 72 * 73 * @return A pointer to the abstract pather which 74 */ 75 SearchSpace* getSearchSpace() const { 76 return m_searchspace; 77 } 69 /** A small function which returns the current status of the search. 70 * 71 * @return An integer value representing the status, which is enumerated by this class. 72 */ 73 int32_t getSearchStatus() const { return m_status; } 78 74 79 /** A small function which returns the current status of the search. 80 * 81 * @return An integer value representing the status, which is enumerated by this class. 82 */ 83 int32_t getSearchStatus() const { 84 return m_status; 85 } 75 protected: 76 /** Sets the current status of the search. 77 * 78 * @param status The status to set. 79 */ 80 void setSearchStatus(const SearchStatus status) { m_status = status; } 86 81 87 protected: 88 /** Sets the current status of the search. 89 * 90 * @param status The status to set. 91 */ 92 void setSearchStatus(const SearchStatus status) { 93 m_status = status; 94 } 82 private: 83 // A location object representing where the search started. 84 Location m_to; 95 85 96 private: 97 //A location object representing where the search started. 98 Location m_to; 86 // A location object representing where the search ended. 87 Location m_from; 99 88 100 //A location object representing where the search ended.101 Location m_from;89 // An integer containing the session id for this search. 90 int32_t m_sessionId; 102 91 103 //An integer containing the session id for this search.104 int32_t m_sessionId;92 // A pointer to the Cellcache. 93 Cellcache* m_cellcache; 105 94 106 //A pointer to the pather that owns this search.107 SearchSpace* m_searchspace;95 // An enumeration of the searches current status. 96 SearchStatus m_status; 108 97 109 //An enumeration of the searches current status. 110 SearchStatus m_status; 111 112 //The start coordinate as an int32_t. 113 int32_t m_startCoordInt; 114 115 //The destination coordinate as an int32_t. 116 int32_t m_destCoordInt; 117 118 //The next coordinate to check out. 119 int32_t m_next; 98 // The start coordinate as an int32_t. 99 int32_t m_startCoordInt; 120 100 121 //The class to use to calculate the heuristic value.122 Heuristic* m_heuristic;101 // The destination coordinate as an int32_t. 102 int32_t m_destCoordInt; 123 103 124 // The shortest path tree.125 std::vector<int32_t> m_spt;104 // The next coordinate to check out. 105 int32_t m_next; 126 106 127 // The search frontier.128 std::vector<int32_t> m_sf;107 // The class to use to calculate the heuristic value. 108 Heuristic* m_heuristic; 129 109 130 // A table to hold the costs.131 std::vector< double> m_gCosts;110 // The shortest path tree. 111 std::vector<int32_t> m_spt; 132 112 133 //priority queue to hold nodes on the sf in order. 113 // The search frontier. 114 std::vector<int32_t> m_sf; 115 116 // A table to hold the costs. 117 std::vector<double> m_gCosts; 118 119 // Priority queue to hold nodes on the sf in order. 134 120 PriorityQueue<int32_t, double> m_sortedfrontier; 135 121 };
Note: See TracChangeset
for help on using the changeset viewer.
