| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
#ifndef FIFE_PATHFINDER_ROUTEPATHER |
| 23 |
#define FIFE_PATHFINDER_ROUTEPATHER |
| 24 |
|
| 25 |
// Standard C++ library includes |
| 26 |
#include <map> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
// 3rd party library includes |
| 30 |
|
| 31 |
// FIFE includes |
| 32 |
// These includes are split up in two parts, separated by one empty line |
| 33 |
// First block: files included from the FIFE root src directory |
| 34 |
// Second block: files included from the same folder |
| 35 |
#include "model/metamodel/ipather.h" |
| 36 |
#include "model/structures/location.h" |
| 37 |
#include "util/structures/priorityqueue.h" |
| 38 |
|
| 39 |
namespace FIFE { |
| 40 |
|
| 41 |
class CellCache; |
| 42 |
class RoutePatherSearch; |
| 43 |
class Route; |
| 44 |
|
| 45 |
class RoutePather : public IPather { |
| 46 |
public: |
| 47 |
/** Constructor. |
| 48 |
* |
| 49 |
*/ |
| 50 |
RoutePather() : m_nextFreeSessionId(0), m_maxTicks(1000) { |
| 51 |
} |
| 52 |
|
| 53 |
/** Creates a route between the start and end location that needs be solved. |
| 54 |
* |
| 55 |
* @param start A const reference to the start location. |
| 56 |
* @param end A const reference to the target location. |
| 57 |
* @param immediate A optional boolean, if true the route bypass the max. ticks limit and solves the path immediately, otherwise false. |
| 58 |
* @param costId A const reference to the string that holds the cost identifier. You can use it optional then this cost id is used instead of the default cost. |
| 59 |
*/ |
| 60 |
Route* createRoute(const Location& start, const Location& end, bool immediate = false, const std::string& costId = ""); |
| 61 |
|
| 62 |
/** Solves the route to create a path. |
| 63 |
* |
| 64 |
* @param route A pointer to the route which should be solved. |
| 65 |
* @param priority The priority to assign to search (high are pushed to the front of the queue). @see PriorityType |
| 66 |
* @param immediate A optional boolean, if true the route bypass the max. ticks limit and solves the path immediately, otherwise false. |
| 67 |
* @return A boolean, if true the route could be solved, otherwise false. |
| 68 |
*/ |
| 69 |
bool solveRoute(Route* route, int32_t priority = MEDIUM_PRIORITY, bool immediate = false); |
| 70 |
|
| 71 |
/** Follows the path of the route. |
| 72 |
* |
| 73 |
* @param current A const reference to the current location. |
| 74 |
* @param route A pointer to the route which should be followed. |
| 75 |
* @param speed A double which holds the speed. |
| 76 |
* @param nextLocation A reference to the next location returned by the pather. |
| 77 |
* @return A boolean, if true the route could be followed, otherwise false. |
| 78 |
*/ |
| 79 |
bool followRoute(const Location& current, Route* route, double speed, Location& nextLocation); |
| 80 |
|
| 81 |
/** Updates the route pather. |
| 82 |
* |
| 83 |
* Advances the active search by so many time steps. If the search |
| 84 |
* completes then this function pops it from the active session list and |
| 85 |
* continues updating the next session until it runs out of time. |
| 86 |
* @see setMaxTicks() |
| 87 |
*/ |
| 88 |
void update(); |
| 89 |
|
| 90 |
/** Cancels a session. |
| 91 |
* |
| 92 |
* Cancels a route pather session. Determines if it is an active session |
| 93 |
* or not and acts accordingly. |
| 94 |
* |
| 95 |
* @param sessionId The id of the session to cancel. |
| 96 |
* @return True if the session could be canceled false otherwise. |
| 97 |
*/ |
| 98 |
bool cancelSession(const int32_t sessionId); |
| 99 |
|
| 100 |
/** Sets maximal ticks (update steps) to solve routes. @see update() |
| 101 |
* @param ticks A integer which holds the steps. default is 1000 |
| 102 |
*/ |
| 103 |
void setMaxTicks(int32_t ticks); |
| 104 |
|
| 105 |
/** Returns maximal ticks (update steps) to solve routes. @see update() |
| 106 |
* @return A integer which holds the steps. default is 1000 |
| 107 |
*/ |
| 108 |
int32_t getMaxTicks(); |
| 109 |
|
| 110 |
/** Returns name of the pathfinder. |
| 111 |
* @return A string that contains the name of the pathfinder. |
| 112 |
*/ |
| 113 |
std::string getName() const; |
| 114 |
|
| 115 |
private: |
| 116 |
//! A path is a list with locations. Each location holds the coordinate for one cell. |
| 117 |
typedef std::list<Location> Path; |
| 118 |
|
| 119 |
//! Holds the searches and their priority. |
| 120 |
typedef PriorityQueue<RoutePatherSearch*, int32_t> SessionQueue; |
| 121 |
|
| 122 |
//! Holds the sessions. |
| 123 |
typedef std::list<int32_t> SessionList; |
| 124 |
|
| 125 |
/** Adds a session id to the session map. |
| 126 |
* |
| 127 |
* Stores the given session id in the session map. |
| 128 |
* |
| 129 |
* @param sessionId The session id to store. |
| 130 |
*/ |
| 131 |
void addSessionId(const int32_t sessionId); |
| 132 |
|
| 133 |
/** Makes a new session id. |
| 134 |
* |
| 135 |
* @return The new session id. |
| 136 |
*/ |
| 137 |
int32_t makeSessionId(); |
| 138 |
|
| 139 |
/** Are two locations equivalent from the perspective of pathing (same layer coordinates and layer). |
| 140 |
* |
| 141 |
* @param a A const reference to the first location to check. |
| 142 |
* @param b A const reference to the second location to check. |
| 143 |
* @return A boolean, true if the locations are equal, false otherwise. |
| 144 |
*/ |
| 145 |
bool locationsEqual(const Location& a, const Location& b); |
| 146 |
|
| 147 |
/** Determines if the given session Id is valid. |
| 148 |
* |
| 149 |
* Searches the session list to determine if a search with the given session id |
| 150 |
* has been registered. |
| 151 |
* @param sessionId The session id to check. |
| 152 |
* @return true if one has, false otherwise. |
| 153 |
*/ |
| 154 |
bool sessionIdValid(const int32_t sessionId); |
| 155 |
|
| 156 |
/** Removes a session id from the session map. |
| 157 |
* |
| 158 |
* @param sessionId The session id to remove. |
| 159 |
* @return True if the sessionId could be removed, false otherwise. |
| 160 |
*/ |
| 161 |
bool invalidateSessionId(const int32_t sessionId); |
| 162 |
|
| 163 |
//! A map of currently running sessions (searches). |
| 164 |
SessionQueue m_sessions; |
| 165 |
|
| 166 |
//! A list of session ids that have been registered. |
| 167 |
SessionList m_registeredSessionIds; |
| 168 |
|
| 169 |
//! The next free session id. |
| 170 |
int32_t m_nextFreeSessionId; |
| 171 |
|
| 172 |
//! The maximum number of ticks allowed. |
| 173 |
int32_t m_maxTicks; |
| 174 |
}; |
| 175 |
} |
| 176 |
#endif |