FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
route.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 // Standard C++ library includes
23 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "util/log/logger.h"
31 #include "model/metamodel/object.h"
33 
34 #include "route.h"
35 
36 namespace FIFE {
37 
38  static Logger _log(LM_STRUCTURES);
39 
40  Route::Route(const Location& start, const Location& end):
41  m_status(ROUTE_CREATED),
42  m_startNode(start),
43  m_endNode(end),
44  m_walked(0),
45  m_sessionId(-1),
46  m_rotation(0),
47  m_replanned(false),
48  m_costId(""),
49  m_object(NULL) {
50  }
51 
53  }
54 
56  if (m_status != status) {
57  m_status = status;
58  }
59  }
60 
62  return m_status;
63  }
64 
65  void Route::setStartNode(const Location& node) {
66  m_startNode = node;
67  if (m_status != ROUTE_CREATED) {
69  if (!m_path.empty()) {
70  m_path.clear();
71  }
72  m_walked = 1;
73  }
74  }
75 
77  return m_startNode;
78  }
79 
80  void Route::setEndNode(const Location& node) {
81  if (m_status != ROUTE_CREATED) {
83  if (!m_path.empty()) {
85  m_path.clear();
86  }
87  m_walked = 1;
88  }
89  m_endNode = node;
90  }
91 
93  return m_endNode;
94  }
95 
97  if (m_path.empty()) {
98  return m_startNode;
99  }
100  if (m_current == m_path.end()) {
101  return m_path.back();
102  }
103  return *m_current;
104  }
105 
107  if (m_path.empty()) {
108  return m_startNode;
109  }
110  if (m_current != m_path.begin()) {
111  --m_current;
112  const Location& loc = *m_current;
113  ++m_current;
114  return loc;
115  }
116  return *m_current;
117  }
118 
120  if (m_path.empty()) {
121  return m_startNode;
122  }
123  if (m_current != m_path.end()) {
124  ++m_current;
125  if (m_current != m_path.end()) {
126  const Location& loc = *m_current;
127  --m_current;
128  return loc;
129  }
130  --m_current;
131  }
132  return *m_current;
133  }
134 
135  bool Route::walkToNextNode(int32_t step) {
136  if (m_path.empty() || step == 0) {
137  return false;
138  }
139 
140  int32_t pos = static_cast<int32_t>(m_walked) + step;
141  if (pos > static_cast<int32_t>(m_path.size()) || pos < 0) {
142  return false;
143  }
144  if (step > 0) {
145  for (int32_t i = 0; i < step; ++i, ++m_current);
146  } else {
147  for (int32_t i = 0; i > step; --i, --m_current);
148  }
149  m_walked += step;
150 
151  return true;
152  }
153 
155  if (m_path.empty()) {
156  return true;
157  }
158  return m_current == m_path.end();
159  }
160 
161  void Route::setPath(const Path& path) {
162  m_path = path;
163  if (!m_path.empty()) {
165  m_current = m_path.begin();
166  m_startNode = m_path.front();
167  m_endNode = m_path.back();
168  }
169  if (!isMultiCell()) {
170  m_replanned = false;
171  }
172  m_walked = 1;
173  }
174 
176  return m_path;
177  }
178 
179  void Route::cutPath(uint32_t length) {
180  if (length == 0) {
181  if (!m_path.empty()) {
183  m_endNode = *m_current;
184  m_path.clear();
185  m_current = m_path.end();
186  }
188  m_walked = 1;
189  m_replanned = true;
190  return;
191  } else if (length >= m_path.size()) {
192  return;
193  }
194 
195  uint32_t newend = m_walked + length - 1;
196  if (newend > m_path.size()) {
197  return;
198  }
199 
200  m_path.resize(newend);
201  m_endNode = m_path.back();
202  m_replanned = true;
203  }
204 
205  void Route::setReplanned(bool replanned) {
206  m_replanned = replanned;
207  }
208 
210  return m_replanned;
211  }
212 
214  return m_path.size();
215  }
216 
218  return m_walked;
219  }
220 
221  void Route::setSessionId(int32_t id) {
222  m_sessionId = id;
223  }
224 
226  return m_sessionId;
227  }
228 
229  void Route::setRotation(int32_t rotation) {
230  m_rotation = rotation;
231  }
232 
233  int32_t Route::getRotation() {
234  return m_rotation;
235  }
236 
237  void Route::setCostId(const std::string& cost) {
238  m_costId = cost;
239  }
240 
241  const std::string& Route::getCostId() {
242  return m_costId;
243  }
244 
246  if (m_object) {
247  return m_object->isMultiObject();
248  }
249  return false;
250  }
251 
252  void Route::setOccupiedArea(const std::vector<ModelCoordinate>& area) {
253  m_area = area;
254  }
255 
256  const std::vector<ModelCoordinate>& Route::getOccupiedArea() {
257  return m_area;
258  }
259 
260  std::vector<ModelCoordinate> Route::getOccupiedCells(int32_t rotation) {
261  if (m_object) {
262  return m_object->getMultiObjectCoordinates(rotation);
263  }
264  std::vector<ModelCoordinate> coords;
265  return coords;
266  }
267 
269  if (!m_object) {
270  return -1;
271  }
272  return m_object->getZStepRange();
273  }
274 
276  if (m_object) {
277  if (!m_object->getWalkableAreas().empty()) {
278  return true;
279  }
280  }
281  return false;
282  }
283 
284  const std::list<std::string> Route::getLimitedAreas() {
285  std::list<std::string> areas;
286  if (m_object) {
287  areas = m_object->getWalkableAreas();
288  }
289  return areas;
290  }
291 
293  m_object = obj;
294  }
295 
297  return m_object;
298  }
299 } // FIFE
void setOccupiedArea(const std::vector< ModelCoordinate > &area)
Sets occupied coordinates for multi cell object.
Definition: route.cpp:252
void cutPath(uint32_t length=1)
Cuts path after the given length.
Definition: route.cpp:179
Object * m_object
pointer to multi object
Definition: route.h:275
Location m_endNode
end location
Definition: route.h:248
void setObject(Object *obj)
Sets the object, needed for multi cell and z-step range.
Definition: route.cpp:292
RouteStatusInfo m_status
search status
Definition: route.h:242
std::list< Location > Path
A path is a list with locations. Each location holds the coordinate for one cell. ...
Definition: ipather.h:38
int32_t getZStepRange()
Returns z-step range from object.
Definition: route.cpp:268
const Location & getPreviousNode()
Returns previous location.
Definition: route.cpp:106
const Location & getStartNode()
Returns the start location.
Definition: route.cpp:76
Object class.
Definition: object.h:51
bool walkToNextNode(int32_t step=1)
Changes the position on the path.
Definition: route.cpp:135
Object * getObject()
Returns the object, needed for multi cell and z-step range.
Definition: route.cpp:296
void setRouteStatus(RouteStatusInfo status)
Sets route status.
Definition: route.cpp:55
int32_t m_sessionId
session id of the search
Definition: route.h:260
bool isMultiObject() const
Gets if object uses special cost.
Definition: object.cpp:197
const Location & getNextNode()
Returns next location.
Definition: route.cpp:119
const Location & getEndNode()
Returns the target location.
Definition: route.cpp:92
uint32_t getPathLength()
Returns the length of the path.
Definition: route.cpp:213
static Logger _log(LM_AUDIO)
uint32_t m_walked
walked steps on the path
Definition: route.h:257
PathIterator m_current
current position on the path
Definition: route.h:254
bool reachedEnd()
Gets if the end of the path was achieved.
Definition: route.cpp:154
int32_t m_rotation
current rotation
Definition: route.h:263
const std::list< std::string > getLimitedAreas()
Definition: route.cpp:284
void setPath(const Path &path)
Sets the path for the route.
Definition: route.cpp:161
void setRotation(int32_t rotation)
Sets the current rotation.
Definition: route.cpp:229
void setStartNode(const Location &node)
Sets the start location.
Definition: route.cpp:65
int32_t getSessionId()
Returns the session identifier.
Definition: route.cpp:225
void setCostId(const std::string &cost)
Sets cost identifier which should be used for pathfinding.
Definition: route.cpp:237
bool isAreaLimited()
Definition: route.cpp:275
RouteStatusInfo getRouteStatus()
Returns route status.
Definition: route.cpp:61
void setReplanned(bool replanned)
Sets the route to replanned.
Definition: route.cpp:205
Path m_path
path
Definition: route.h:251
std::vector< ModelCoordinate > m_area
occupied cells by multicell object
Definition: route.h:272
const std::list< std::string > & getWalkableAreas() const
Returns a list that contains all walkable area ids.
Definition: object.cpp:348
~Route()
Destructor.
Definition: route.cpp:52
Path getPath()
Returns the path.
Definition: route.cpp:175
uint32_t getWalkedLength()
Returns the walked steps.
Definition: route.cpp:217
const std::vector< ModelCoordinate > & getOccupiedArea()
Returns occupied coordinates for multi cell object.
Definition: route.cpp:256
std::string m_costId
used cost identifier
Definition: route.h:269
std::vector< ModelCoordinate > getMultiObjectCoordinates(int32_t rotation)
Returns all multi object coordinates for the given rotation.
Definition: object.cpp:270
const std::string & getCostId()
Returns cost identifier which is used for pathfinding.
Definition: route.cpp:241
std::vector< ModelCoordinate > getOccupiedCells(int32_t rotation)
Returns relative coordinates for multi cell object based on rotation.
Definition: route.cpp:260
const Location & getCurrentNode()
Returns current location.
Definition: route.cpp:96
bool isMultiCell()
Gets if path is for a multi cell object.
Definition: route.cpp:245
void setEndNode(const Location &node)
Sets the target location.
Definition: route.cpp:80
int32_t getZStepRange() const
Returns z-step range from object.
Definition: object.cpp:326
int32_t getRotation()
Returns the current rotation.
Definition: route.cpp:233
unsigned int uint32_t
Definition: core.h:40
bool m_replanned
is path replanned
Definition: route.h:266
Location m_startNode
start location
Definition: route.h:245
bool isReplanned()
Gets if the route is replanned.
Definition: route.cpp:209
Route(const Location &start, const Location &end)
Constructor.
Definition: route.cpp:40
uint8_t RouteStatusInfo
Definition: route.h:56
void setSessionId(int32_t id)
Sets the session identifier.
Definition: route.cpp:221