FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
route.cpp
Go to the documentation of this file.
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 // 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"
32 #include "model/structures/layer.h"
34 
35 #include "route.h"
36 
37 namespace FIFE {
38 
39  static Logger _log(LM_STRUCTURES);
40 
41  Route::Route(const Location& start, const Location& end):
42  m_status(ROUTE_CREATED),
43  m_startNode(start),
44  m_endNode(end),
45  m_walked(0),
46  m_sessionId(-1),
47  m_rotation(0),
48  m_replanned(false),
49  m_ignoresBlocker(false),
50  m_costId(""),
51  m_object(NULL) {
52  }
53 
55  }
56 
58  if (m_status != status) {
59  m_status = status;
60  }
61  }
62 
64  return m_status;
65  }
66 
67  void Route::setStartNode(const Location& node) {
68  m_startNode = node;
69  if (m_status != ROUTE_CREATED) {
71  if (!m_path.empty()) {
72  m_path.clear();
73  }
74  m_walked = 1;
75  }
76  }
77 
79  return m_startNode;
80  }
81 
82  void Route::setEndNode(const Location& node) {
83  if (m_status != ROUTE_CREATED) {
85  if (!m_path.empty()) {
87  m_path.clear();
88  }
89  m_walked = 1;
90  }
91  m_endNode = node;
92  }
93 
95  return m_endNode;
96  }
97 
99  if (m_path.empty()) {
100  return m_startNode;
101  }
102  if (m_current == m_path.end()) {
103  return m_path.back();
104  }
105  return *m_current;
106  }
107 
109  if (m_path.empty()) {
110  return m_startNode;
111  }
112  if (m_current != m_path.begin()) {
113  --m_current;
114  const Location& loc = *m_current;
115  ++m_current;
116  return loc;
117  }
118  return *m_current;
119  }
120 
122  if (m_path.empty()) {
123  return m_startNode;
124  }
125  if (m_current != m_path.end()) {
126  ++m_current;
127  if (m_current != m_path.end()) {
128  const Location& loc = *m_current;
129  --m_current;
130  return loc;
131  }
132  --m_current;
133  }
134  return *m_current;
135  }
136 
137  bool Route::walkToNextNode(int32_t step) {
138  if (m_path.empty() || step == 0) {
139  return false;
140  }
141 
142  int32_t pos = static_cast<int32_t>(m_walked) + step;
143  if (pos > static_cast<int32_t>(m_path.size()) || pos < 0) {
144  return false;
145  }
146  if (step > 0) {
147  for (int32_t i = 0; i < step; ++i, ++m_current);
148  } else {
149  for (int32_t i = 0; i > step; --i, --m_current);
150  }
151  m_walked += step;
152 
153  return true;
154  }
155 
157  if (m_path.empty()) {
158  return true;
159  }
160  return m_current == m_path.end();
161  }
162 
163  void Route::setPath(const Path& path) {
164  m_path = path;
165  if (!m_path.empty()) {
167  m_current = m_path.begin();
168  m_startNode = m_path.front();
169  m_endNode = m_path.back();
170  }
171  if (!isMultiCell()) {
172  m_replanned = false;
173  }
174  m_walked = 1;
175  }
176 
178  return m_path;
179  }
180 
181  void Route::cutPath(uint32_t length) {
182  if (length == 0) {
183  if (!m_path.empty()) {
185  m_endNode = *m_current;
186  m_path.clear();
187  m_current = m_path.end();
188  }
190  m_walked = 1;
191  m_replanned = true;
192  return;
193  } else if (length >= m_path.size()) {
194  return;
195  }
196 
197  uint32_t newend = m_walked + length - 1;
198  if (newend > m_path.size()) {
199  return;
200  }
201 
202  m_path.resize(newend);
203  m_endNode = m_path.back();
204  m_replanned = true;
205  }
206 
207  void Route::setReplanned(bool replanned) {
208  m_replanned = replanned;
209  }
210 
212  return m_replanned;
213  }
214 
216  return m_path.size();
217  }
218 
220  return m_walked;
221  }
222 
223  void Route::setSessionId(int32_t id) {
224  m_sessionId = id;
225  }
226 
228  return m_sessionId;
229  }
230 
231  void Route::setRotation(int32_t rotation) {
232  m_rotation = rotation;
233  }
234 
235  int32_t Route::getRotation() {
236  return m_rotation;
237  }
238 
239  void Route::setCostId(const std::string& cost) {
240  m_costId = cost;
241  }
242 
243  const std::string& Route::getCostId() {
244  return m_costId;
245  }
246 
248  if (m_object) {
249  return m_object->isMultiObject();
250  }
251  return false;
252  }
253 
254  void Route::setOccupiedArea(const std::vector<ModelCoordinate>& area) {
255  m_area = area;
256  }
257 
258  const std::vector<ModelCoordinate>& Route::getOccupiedArea() {
259  return m_area;
260  }
261 
262  std::vector<ModelCoordinate> Route::getOccupiedCells(int32_t rotation) {
263  if (m_object) {
264  return m_object->getMultiObjectCoordinates(rotation);
265  }
266  std::vector<ModelCoordinate> coords;
267  return coords;
268  }
269 
271  if (!m_object) {
272  return -1;
273  }
274  return m_object->getZStepRange();
275  }
276 
278  if (m_object) {
279  if (!m_object->getWalkableAreas().empty()) {
280  return true;
281  }
282  }
283  return false;
284  }
285 
286  const std::list<std::string> Route::getLimitedAreas() {
287  std::list<std::string> areas;
288  if (m_object) {
289  areas = m_object->getWalkableAreas();
290  }
291  return areas;
292  }
293 
295  m_ignoresBlocker = ignore;
296  }
297 
299  return m_ignoresBlocker;
300  }
301 
303  Path p;
304  if (!m_path.empty()) {
305  for (PathIterator it = m_path.begin(); it != m_path.end(); ++it) {
306  Layer* layer = (*it).getLayer();
307  if (layer->cellContainsBlockingInstance((*it).getLayerCoordinates())) {
308  p.push_back(*it);
309  }
310  }
311  }
312  return p;
313  }
314 
316  m_object = obj;
317  }
318 
320  return m_object;
321  }
322 } // FIFE
void setOccupiedArea(const std::vector< ModelCoordinate > &area)
Sets occupied coordinates for multi cell object.
Definition: route.cpp:254
std::list< std::string > getWalkableAreas() const
Returns a list that contains all walkable area ids.
Definition: object.cpp:606
void cutPath(uint32_t length=1)
Cuts path after the given length.
Definition: route.cpp:181
Object * m_object
pointer to multi object
Definition: route.h:294
Location m_endNode
end location
Definition: route.h:264
void setObject(Object *obj)
Sets the object, needed for multi cell and z-step range.
Definition: route.cpp:315
std::vector< ModelCoordinate > getMultiObjectCoordinates(int32_t rotation) const
Returns all multi object coordinates for the given rotation.
Definition: object.cpp:478
RouteStatusInfo m_status
search status
Definition: route.h:258
std::list< Location > Path
A path is a list with locations. Each location holds the coordinate for one cell. ...
Definition: ipather.h:38
void setDynamicBlockerIgnored(bool ignore)
Sets the route to ignore dynamic blocker.
Definition: route.cpp:294
int32_t getZStepRange()
Returns z-step range from object.
Definition: route.cpp:270
const Location & getPreviousNode()
Returns previous location.
Definition: route.cpp:108
const Location & getStartNode()
Returns the start location.
Definition: route.cpp:78
Object class.
Definition: object.h:51
bool walkToNextNode(int32_t step=1)
Changes the position on the path.
Definition: route.cpp:137
Object * getObject()
Returns the object, needed for multi cell and z-step range.
Definition: route.cpp:319
void setRouteStatus(RouteStatusInfo status)
Sets route status.
Definition: route.cpp:57
int32_t m_sessionId
session id of the search
Definition: route.h:276
const Location & getNextNode()
Returns next location.
Definition: route.cpp:121
const Location & getEndNode()
Returns the target location.
Definition: route.cpp:94
uint32_t getPathLength()
Returns the length of the path.
Definition: route.cpp:215
static Logger _log(LM_AUDIO)
bool m_ignoresBlocker
ignores dynamic blocker
Definition: route.h:285
uint32_t m_walked
walked steps on the path
Definition: route.h:273
Path getBlockingPathLocations()
Returns the blocking locations of the path.
Definition: route.cpp:302
PathIterator m_current
current position on the path
Definition: route.h:270
bool reachedEnd()
Gets if the end of the path was achieved.
Definition: route.cpp:156
int32_t m_rotation
current rotation
Definition: route.h:279
const std::list< std::string > getLimitedAreas()
Definition: route.cpp:286
void setPath(const Path &path)
Sets the path for the route.
Definition: route.cpp:163
void setRotation(int32_t rotation)
Sets the current rotation.
Definition: route.cpp:231
void setStartNode(const Location &node)
Sets the start location.
Definition: route.cpp:67
int32_t getSessionId()
Returns the session identifier.
Definition: route.cpp:227
A basic layer on a map.
Definition: layer.h:99
void setCostId(const std::string &cost)
Sets cost identifier which should be used for pathfinding.
Definition: route.cpp:239
bool isAreaLimited()
Definition: route.cpp:277
bool isDynamicBlockerIgnored()
Gets if the route ignores dynamic blocker.
Definition: route.cpp:298
RouteStatusInfo getRouteStatus()
Returns route status.
Definition: route.cpp:63
Path::iterator PathIterator
path iterator
Definition: route.h:255
void setReplanned(bool replanned)
Sets the route to replanned.
Definition: route.cpp:207
bool isMultiObject() const
Gets if object uses special cost.
Definition: object.cpp:346
Path m_path
path
Definition: route.h:267
std::vector< ModelCoordinate > m_area
occupied cells by multicell object
Definition: route.h:291
~Route()
Destructor.
Definition: route.cpp:54
Path getPath()
Returns the path.
Definition: route.cpp:177
uint32_t getWalkedLength()
Returns the walked steps.
Definition: route.cpp:219
const std::vector< ModelCoordinate > & getOccupiedArea()
Returns occupied coordinates for multi cell object.
Definition: route.cpp:258
std::string m_costId
used cost identifier
Definition: route.h:288
int32_t getZStepRange() const
Returns z-step range from object.
Definition: object.cpp:563
const std::string & getCostId()
Returns cost identifier which is used for pathfinding.
Definition: route.cpp:243
std::vector< ModelCoordinate > getOccupiedCells(int32_t rotation)
Returns relative coordinates for multi cell object based on rotation.
Definition: route.cpp:262
bool cellContainsBlockingInstance(const ModelCoordinate &cellCoordinate)
Determines if a given cell on the layer contains a blocking instance.
Definition: layer.cpp:462
const Location & getCurrentNode()
Returns current location.
Definition: route.cpp:98
bool isMultiCell()
Gets if path is for a multi cell object.
Definition: route.cpp:247
void setEndNode(const Location &node)
Sets the target location.
Definition: route.cpp:82
int32_t getRotation()
Returns the current rotation.
Definition: route.cpp:235
unsigned int uint32_t
Definition: core.h:40
bool m_replanned
is path replanned
Definition: route.h:282
Location m_startNode
start location
Definition: route.h:261
bool isReplanned()
Gets if the route is replanned.
Definition: route.cpp:211
Route(const Location &start, const Location &end)
Constructor.
Definition: route.cpp:41
uint8_t RouteStatusInfo
Definition: route.h:56
void setSessionId(int32_t id)
Sets the session identifier.
Definition: route.cpp:223