FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
object.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006-2011 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/base/exception.h"
31 
32 #include "object.h"
33 #include "action.h"
34 #include "ipather.h"
35 
36 namespace FIFE {
37  Object::Object(const std::string& identifier, const std::string& name_space, Object* inherited):
38  m_id(identifier),
39  m_namespace(name_space),
40  m_filename(""),
41  m_inherited(inherited),
42  m_actions(NULL),
43  m_blocking(false),
44  m_static(false),
45  m_pather(NULL),
46  m_visual(NULL),
47  m_defaultAction(NULL),
48  m_cellStack(0),
49  m_costId(""),
50  m_cost(1.0),
51  m_multiPart(false),
52  m_restrictedRotation(false),
53  m_zRange(-1),
54  m_area("") {
55  }
56 
58  if (m_actions) {
59  std::map<std::string, Action*>::const_iterator i(m_actions->begin());
60  while (i != m_actions->end()) {
61  delete i->second;
62  ++i;
63  }
64  delete m_actions;
65  }
66  delete m_visual;
67  }
68 
69  Action* Object::createAction(const std::string& identifier, bool is_default) {
70  if (!m_actions) {
71  m_actions = new std::map<std::string, Action*>;
72  }
73 
74  std::map<std::string, Action*>::const_iterator it = m_actions->begin();
75  for(; it != m_actions->end(); ++it) {
76  if(identifier == it->second->getId()) {
77  throw NameClash(identifier);
78  }
79  }
80 
81  Action* a = getAction(identifier);
82  if (!a) {
83  a = new Action(identifier);
84  (*m_actions)[identifier] = a;
85  if (is_default || (!m_defaultAction)) {
86  m_defaultAction = a;
87  }
88  }
89  return a;
90  }
91 
92  Action* Object::getAction(const std::string& identifier) const {
93  std::map<std::string, Action*>::const_iterator i;
94  if (m_actions) {
95  i = m_actions->find(identifier);
96  }
97  if ((!m_actions) || (i == m_actions->end())) {
98  if (m_inherited) {
99  return m_inherited->getAction(identifier);
100  }
101  return NULL;
102  }
103  return i->second;
104  }
105 
106  std::list<std::string> Object::getActionIds() const {
107  std::list<std::string> action_ids;
108  action_ids.clear();
109  if (m_actions) {
110  std::map<std::string, Action*>::const_iterator actions_it = m_actions->begin();
111  for(; actions_it != m_actions->end(); ++actions_it) {
112  action_ids.push_back(actions_it->first);
113  }
114  }
115  return action_ids;
116  }
117 
118  void Object::setDefaultAction(const std::string& identifier) {
119  std::map<std::string, Action*>::const_iterator i;
120  Action* action = NULL;
121  if (m_actions) {
122  i = m_actions->find(identifier);
123  }
124  if ((!m_actions) || (i == m_actions->end())) {
125  if (m_inherited) {
126  action = m_inherited->getAction(identifier);
127  }
128  } else {
129  action = i->second;
130  }
131 
132  if (action) {
133  m_defaultAction = action;
134  }
135  }
136 
137  void Object::setPather(IPather* pather) {
138  m_pather = pather;
139  }
140 
141  bool Object::isBlocking() const {
142  if (m_blocking) {
143  return true;
144  }
145  if (m_inherited) {
146  return m_inherited->isBlocking();
147  }
148  return false;
149  }
150 
151  bool Object::isStatic() const {
152  if (m_static) {
153  return true;
154  }
155  if (m_inherited) {
156  return m_inherited->isStatic();
157  }
158  return false;
159  }
160 
161  void Object::setFilename(const std::string& file) {
162  m_filename = file;
163  }
164 
165  const std::string& Object::getFilename() const {
166  return m_filename;
167  }
168 
170  m_cellStack = position;
171  }
172 
174  return m_cellStack;
175  }
176 
177  bool Object::isSpecialCost() const {
178  return m_costId != "";
179  }
180 
181  void Object::setCostId(const std::string& cost) {
182  m_costId = cost;
183  }
184 
185  const std::string& Object::getCostId() const {
186  return m_costId;
187  }
188 
189  void Object::setCost(double cost) {
190  m_cost = cost;
191  }
192 
193  double Object::getCost() const {
194  return m_cost;
195  }
196 
197  bool Object::isMultiObject() const {
198  return !m_multiPartIds.empty();
199  }
200 
201  void Object::addMultiPartId(const std::string& partId) {
202  m_multiPartIds.push_back(partId);
203  }
204 
205  const std::list<std::string>& Object::getMultiPartIds() const {
206  return m_multiPartIds;
207  }
208 
209  void Object::removeMultiPartId(const std::string& partId) {
210  std::list<std::string>::iterator it = m_multiPartIds.begin();
211  for (; it != m_multiPartIds.end(); ++it) {
212  if (*it == partId) {
213  m_multiPartIds.erase(it);
214  break;
215  }
216  }
217  }
218 
220  m_multiPartIds.clear();
221  }
222 
223  bool Object::isMultiPart() const {
224  return m_multiPart;
225  }
226 
227  void Object::setMultiPart(bool part) {
228  m_multiPart = part;
229  }
230 
232  m_multiParts.insert(obj);
233  }
234 
235  const std::set<Object*>& Object::getMultiParts() const {
236  return m_multiParts;
237  }
238 
240  m_multiParts.erase(obj);
241 
242  }
243 
245  m_multiParts.clear();
246  }
247 
248  void Object::addMultiPartCoordinate(int32_t rotation, ModelCoordinate coord) {
249  m_multiPartCoordinates.insert(std::pair<int32_t, ModelCoordinate>(rotation, coord));
250  m_partAngleMap[rotation] = rotation;
251  }
252 
253  const std::multimap<int32_t, ModelCoordinate>& Object::getMultiPartCoordinates() const {
254  return m_multiPartCoordinates;
255  }
256 
257  std::vector<ModelCoordinate> Object::getMultiPartCoordinates(int32_t rotation) {
258  std::vector<ModelCoordinate> coordinates;
259  int32_t closest = 0;
260  getIndexByAngle(rotation, m_partAngleMap, closest);
261  std::pair<std::multimap<int32_t, ModelCoordinate>::iterator,
262  std::multimap<int32_t, ModelCoordinate>::iterator> result = m_multiPartCoordinates.equal_range(closest);
263  std::multimap<int32_t, ModelCoordinate>::iterator it = result.first;
264  for (; it != result.second; ++it) {
265  coordinates.push_back((*it).second);
266  }
267  return coordinates;
268  }
269 
270  std::vector<ModelCoordinate> Object::getMultiObjectCoordinates(int32_t rotation) {
271  if (m_multiObjectCoordinates.empty()) {
272  std::set<Object*>::iterator subit = m_multiParts.begin();
273  for (; subit != m_multiParts.end(); ++subit) {
274  const std::multimap<int32_t, ModelCoordinate>& subcoords = (*subit)->getMultiPartCoordinates();
275  m_multiObjectCoordinates.insert(subcoords.begin(), subcoords.end());
276  }
277  std::multimap<int32_t, ModelCoordinate>::iterator it = m_multiObjectCoordinates.begin();
278  for (; it != m_multiObjectCoordinates.end(); ++it) {
279  m_multiAngleMap[(*it).first] = (*it).first;
280  }
281  }
282  int32_t closest = 0;
283  getIndexByAngle(rotation, m_multiAngleMap, closest);
284  std::vector<ModelCoordinate> coordinates;
285  std::pair<std::multimap<int32_t, ModelCoordinate>::iterator,
286  std::multimap<int32_t, ModelCoordinate>::iterator> result = m_multiObjectCoordinates.equal_range(closest);
287  std::multimap<int32_t, ModelCoordinate>::iterator it = result.first;
288  ModelCoordinate parent(0,0);
289  coordinates.push_back(parent);
290  for (; it != result.second; ++it) {
291  coordinates.push_back((*it).second);
292  }
293  return coordinates;
294  }
295 
297  m_rotationAnchor = anchor;
298  }
299 
301  return m_rotationAnchor;
302  }
303 
304  void Object::setRestrictedRotation(bool restrict) {
305  m_restrictedRotation = restrict;
306  }
307 
309  return m_restrictedRotation;
310  }
311 
312  int32_t Object::getRestrictedRotation(int32_t rotation) {
313  int32_t closest = 0;
314  if (!m_multiAngleMap.empty()) {
315  getIndexByAngle(rotation, m_multiAngleMap, closest);
316  } else if (!m_partAngleMap.empty()) {
317  getIndexByAngle(rotation, m_partAngleMap, closest);
318  }
319  return closest;
320  }
321 
322  void Object::setZStepRange(int32_t zRange) {
323  m_zRange = zRange;
324  }
325 
326  int32_t Object::getZStepRange() const {
327  return m_zRange;
328  }
329 
330  void Object::setArea(const std::string& id) {
331  m_area = id;
332  }
333 
334  const std::string& Object::getArea() const {
335  return m_area;
336  }
337 
338  void Object::addWalkableArea(const std::string& id) {
339  m_walkableAreas.push_back(id);
340  m_walkableAreas.sort();
341  m_walkableAreas.unique();
342  }
343 
344  void Object::removeWalkableArea(const std::string& id) {
345  m_walkableAreas.remove(id);
346  }
347 
348  const std::list<std::string>& Object::getWalkableAreas() const {
349  return m_walkableAreas;
350  }
351 
352  bool Object::operator==(const Object& obj) const {
353  return m_id == obj.getId() && m_namespace == obj.getNamespace();
354  }
355 
356  bool Object::operator!=(const Object& obj) const {
357  return m_id != obj.getId() || m_namespace != obj.getNamespace();
358  }
359 
360 }
double m_cost
cost value, default 1.0
Definition: object.h:367
void setDefaultAction(const std::string &identifier)
Sets default action assigned to this object.
Definition: object.cpp:118
double getCost() const
Returns the cost.
Definition: object.cpp:193
void removeMultiPart(Object *obj)
Removes a multi part object.
Definition: object.cpp:239
~Object()
Destructor.
Definition: object.cpp:57
void removeWalkableArea(const std::string &id)
Removes an area id from walkable areas.
Definition: object.cpp:344
void setArea(const std::string &id)
Sets the area id that the instances of this object adds to their cells.
Definition: object.cpp:330
const std::string & getNamespace() const
Definition: object.h:69
bool m_restrictedRotation
indicates if object uses only restricted rotations
Definition: object.h:373
Object class.
Definition: object.h:51
void setRotationAnchor(const ExactModelCoordinate &anchor)
Sets the rotation anchor for this multi object.
Definition: object.cpp:296
const std::string & getId() const
Definition: object.h:68
type_angle2id m_partAngleMap
part object angles
Definition: object.h:388
bool isMultiObject() const
Gets if object uses special cost.
Definition: object.cpp:197
bool isStatic() const
Gets if object moves.
Definition: object.cpp:151
void setCellStackPosition(uint8_t position)
Sets the cell stack position.
Definition: object.cpp:169
const std::list< std::string > & getMultiPartIds() const
Returns all multi part identifiers.
Definition: object.cpp:205
void setZStepRange(int32_t zRange)
Sets z-step range for object.
Definition: object.cpp:322
std::list< std::string > m_multiPartIds
list with part identifiers
Definition: object.h:379
const std::set< Object * > & getMultiParts() const
Returns all multi part objects.
Definition: object.cpp:235
std::list< std::string > m_walkableAreas
Definition: object.h:401
Object(const std::string &identifier, const std::string &name_space, Object *inherited=NULL)
Constructor An object may optionally inherit default attributes from another object.
Definition: object.cpp:37
void setFilename(const std::string &file)
Definition: object.cpp:161
bool operator!=(const Object &obj) const
Compares unequality of two objects.
Definition: object.cpp:356
bool isMultiPart() const
Gets if object is a part of a multi object.
Definition: object.cpp:223
std::string m_namespace
namespace
Definition: object.h:334
const std::string & getFilename() const
Definition: object.cpp:165
std::multimap< int32_t, ModelCoordinate > m_multiPartCoordinates
part object coordinates
Definition: object.h:394
void removeAllMultiPartIds()
Removes all multi part identifiers.
Definition: object.cpp:219
void removeMultiPartId(const std::string &partId)
Removes a multi part identifier.
Definition: object.cpp:209
unsigned char uint8_t
Definition: core.h:38
std::multimap< int32_t, ModelCoordinate > m_multiObjectCoordinates
multi object coordinates
Definition: object.h:397
void setMultiPart(bool part)
Sets the object as a part of a multi object.
Definition: object.cpp:227
std::string m_id
identifier
Definition: object.h:331
const std::string & getCostId() const
Returns the cost id.
Definition: object.cpp:185
const std::multimap< int32_t, ModelCoordinate > & getMultiPartCoordinates() const
Returns all rotationally dependent coordinates from this object part.
Definition: object.cpp:253
void setRestrictedRotation(bool restrict)
Sets the rotation to restricted.
Definition: object.cpp:304
void addMultiPartCoordinate(int32_t rotation, ModelCoordinate coord)
Adds rotationally dependent coordinates for this object part.
Definition: object.cpp:248
ExactModelCoordinate m_rotationAnchor
rotation anchor
Definition: object.h:382
bool isRestrictedRotation() const
Gets if object uses restricted rotations.
Definition: object.cpp:308
Object * m_inherited
pointer to inherited object
Definition: object.h:340
void setCost(double cost)
Sets the cost.
Definition: object.cpp:189
void removeMultiParts()
Removes all multi part objects.
Definition: object.cpp:244
int32_t m_zRange
z range value
Definition: object.h:376
Action * createAction(const std::string &identifier, bool is_default=false)
Adds new action with given id.
Definition: object.cpp:69
bool m_multiPart
indicates if object is part of multi object
Definition: object.h:370
std::string m_area
Definition: object.h:399
const std::list< std::string > & getWalkableAreas() const
Returns a list that contains all walkable area ids.
Definition: object.cpp:348
std::string m_filename
filename
Definition: object.h:337
void addMultiPart(Object *obj)
Adds a object as a part of a multi object.
Definition: object.cpp:231
void addMultiPartId(const std::string &partId)
Adds a multi part identifier.
Definition: object.cpp:201
uint8_t m_cellStack
position on cellstack
Definition: object.h:361
IPather * m_pather
pointer to pathfinder
Definition: object.h:352
uint8_t getCellStackPosition() const
Returns cell stack position.
Definition: object.cpp:173
std::vector< ModelCoordinate > getMultiObjectCoordinates(int32_t rotation)
Returns all multi object coordinates for the given rotation.
Definition: object.cpp:270
A 3D Point.
Definition: point.h:202
Action * getAction(const std::string &identifier) const
Gets action with given id.
Definition: object.cpp:92
bool m_blocking
indicates if object blocks
Definition: object.h:346
int32_t getIndexByAngle(int32_t angle, const type_angle2id &angle2id, int32_t &closestMatchingAngle)
Returns id for given angle from angle2id map in case there are no elements in the map...
Definition: angles.cpp:37
const std::string & getArea() const
Gets the area id that the instances of this object adds to their cells.
Definition: object.cpp:334
std::set< Object * > m_multiParts
set with part objects
Definition: object.h:385
bool isSpecialCost() const
Gets if object uses special cost.
Definition: object.cpp:177
bool operator==(const Object &obj) const
Compares equality of two objects.
Definition: object.cpp:352
Action * m_defaultAction
pointer to default action
Definition: object.h:358
bool m_static
indicates if object is static
Definition: object.h:349
void addWalkableArea(const std::string &id)
Adds an area id to walkable area.
Definition: object.cpp:338
int32_t getZStepRange() const
Returns z-step range from object.
Definition: object.cpp:326
type_angle2id m_multiAngleMap
multi object angles
Definition: object.h:391
void setPather(IPather *pather)
Sets pather used by instances created out of this object.
Definition: object.cpp:137
std::list< std::string > getActionIds() const
Gets all available action ids of the object and packs them into a list.
Definition: object.cpp:106
const ExactModelCoordinate & getRotationAnchor() const
Returns the rotation anchor for this multi object.
Definition: object.cpp:300
int32_t getRestrictedRotation(int32_t rotation)
Returns the most obvious rotation, based on multi coordinates.
Definition: object.cpp:312
bool isBlocking() const
Gets if object blocks movement.
Definition: object.cpp:141
void setCostId(const std::string &cost)
Sets the cost id.
Definition: object.cpp:181
std::map< std::string, Action * > * m_actions
holds action ids and assigned actions
Definition: object.h:343
IVisual * m_visual
pointer to object visual
Definition: object.h:355
std::string m_costId
cost identifier
Definition: object.h:364