Eris  1.3.21
Entity.h
1 #ifndef ERIS_ENTITY_H
2 #define ERIS_ENTITY_H
3 
4 #include <Eris/Types.h>
5 
6 #include <Atlas/Objects/ObjectsFwd.h>
7 
8 #include <wfmath/point.h>
9 #include <wfmath/vector.h>
10 #include <wfmath/axisbox.h>
11 #include <wfmath/quaternion.h>
12 #include <wfmath/timestamp.h>
13 
14 #include <sigc++/trackable.h>
15 #include <sigc++/slot.h>
16 #include <sigc++/signal.h>
17 #include <sigc++/connection.h>
18 
19 #include <map>
20 #include <vector>
21 
22 namespace Atlas {
23  namespace Message {
24  class Element;
25  typedef std::map<std::string, Element> MapType;
26  }
27 }
28 
29 namespace Eris {
30 
31 // Forward Declerations
32 class Entity;
33 class TypeInfo;
34 class View;
35 class EntityRouter;
36 class Task;
37 
38 typedef std::vector<Entity*> EntityArray;
39 typedef std::vector<Task*> TaskArray;
40 typedef std::vector<TypeInfoPtr> TypeInfoArray;
41 
57 class Entity : virtual public sigc::trackable
58 {
59 public:
60  typedef std::map<std::string, Atlas::Message::Element> AttrMap;
61 
62  explicit Entity(const std::string& id, TypeInfo* ty);
63  virtual ~Entity();
64 
70  virtual void shutdown();
71 
72 // heirarchy interface
78  unsigned int numContained() const;
79 
85  Entity* getContained(unsigned int index) const;
86 
94  const Atlas::Message::Element& valueOfAttr(const std::string& attr) const;
95 
101  bool hasAttr(const std::string &p) const;
102 
106  typedef sigc::slot<void, const Atlas::Message::Element&> AttrChangedSlot;
107 
115  sigc::connection observe(const std::string& attr, const AttrChangedSlot& aslot);
116 
117 // accessors
122  const std::string& getId() const;
123 
129  const std::string& getName() const;
130 
135  float getStamp() const;
136 
141  TypeInfo* getType() const;
142 
147  Entity* getLocation() const;
148 
153  WFMath::Point<3> getPosition() const;
154 
162  const AttrMap getAttributes() const;
163 
171  const AttrMap& getInstanceAttributes() const;
172 
177  bool isMoving() const;
178 
184  WFMath::Point<3> getPredictedPos() const;
185 
190  WFMath::Vector<3> getPredictedVelocity() const;
191 
193  WFMath::Point<3> getViewPosition() const;
194 
196  WFMath::Quaternion getViewOrientation() const;
197 
199  const WFMath::Vector< 3 > & getVelocity(void) const;
200 
202  const WFMath::Quaternion & getOrientation(void) const;
203 
205  const WFMath::AxisBox< 3 > & getBBox(void) const;
206 
212  bool hasBBox() const;
213 
218  const TaskArray& getTasks() const;
219 
225  TypeInfoArray getUseOperations() const;
226 
227  bool hasChild(const std::string& eid) const;
228 
230  bool isVisible() const;
231 
232 // coordinate transformations
233  template<class C>
234  C toLocationCoords(const C& c) const;
235 
236  template<class C>
237  C fromLocationCoords(const C& c) const;
238 
239  // A vector (e.g., the distance between two points, or
240  // a velocity) gets rotated by a coordinate transformation,
241  // but doesn't get shifted by the change in the position
242  // of the origin, so we handle it separately. We also
243  // need to copy the vector before rotating, because
244  // Vector::rotate() rotates it in place.
245  WFMath::Vector<3> toLocationCoords(const WFMath::Vector<3>& v) const;
246 
247  WFMath::Vector<3> fromLocationCoords(const WFMath::Vector<3>& v) const;
248 
249 // Signals
250  sigc::signal<void, Entity*> ChildAdded;
251  sigc::signal<void, Entity*> ChildRemoved;
252 
254 
258  sigc::signal<void, Entity*> LocationChanged;
259 
262  sigc::signal<void, const StringSet&> Changed;
263 
265  sigc::signal<void> Moved;
266 
269  sigc::signal<void, bool> Moving;
270 
286  sigc::signal< void, const Atlas::Objects::Root & > Say;
287 
292  sigc::signal<void, const std::string&> Emote;
293 
299  sigc::signal<void, const Atlas::Objects::Operation::RootOperation&> Acted;
300 
306  sigc::signal<void, const Atlas::Objects::Root&> Noise;
307 
312  sigc::signal<void, bool> VisibilityChanged;
313 
319  sigc::signal<void> BeingDeleted;
320 
324  sigc::signal<void, Task*> TaskAdded;
328  sigc::signal<void, Task*> TaskRemoved;
329 protected:
333  virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp);
334 
338  virtual void onTalk(const Atlas::Objects::Operation::RootOperation& talk);
339 
340  virtual void onAttrChanged(const std::string& attr, const Atlas::Message::Element &v);
341 
342  virtual void onLocationChanged(Entity* oldLoc);
343 
346  virtual void onMoved();
347 
351  virtual void onVisibilityChanged(bool vis);
352 
357  virtual void onAction(const Atlas::Objects::Operation::RootOperation& act);
358 
363  virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation& op);
364 
369  virtual void onImaginary(const Atlas::Objects::Root& act);
370 
376  virtual void setMoving(bool moving);
377 
382  virtual void onChildAdded(Entity* child);
383 
388  virtual void onChildRemoved(Entity* child);
389 
394  virtual void onTaskAdded(Task* task);
395 
396  friend class IGRouter;
397  friend class View;
398  friend class Task;
399 
404  void sight(const Atlas::Objects::Entity::RootEntity& gent);
405 
406 
413  void setFromRoot(const Atlas::Objects::Root& obj, bool allowMotion, bool includeTypeInfoAttributes = false);
414 
417  void setVisible(bool vis);
418 
419  void setAttr(const std::string &p, const Atlas::Message::Element &v);
420 
425  bool nativeAttrChanged(const std::string &p, const Atlas::Message::Element &v);
426 
433  void typeInfo_AttributeChanges(const std::string& attributeName, const Atlas::Message::Element& element);
434 
441  virtual void attrChangedFromTypeInfo(const std::string& attributeName, const Atlas::Message::Element& element);
442 
443 
450  void fillAttributesFromType(Entity::AttrMap& attributes, TypeInfo* typeInfo) const;
451 
452  void beginUpdate();
453  void addToUpdate(const std::string& attr);
454  void endUpdate();
455 
458  void setLocationFromAtlas(const std::string& locId);
459 
463  void setLocation(Entity* newLocation);
464 
467  void setContentsFromAtlas(const StringList& contents);
468 
473  void filterMoveAttrs(Atlas::Message::MapType& attrs) const;
474 
475  typedef std::map<std::string, Entity*> IdEntityMap;
476  void buildEntityDictFromContents(IdEntityMap& dict);
477 
478  void addChild(Entity* e);
479  void removeChild(Entity* e);
480 
481  void addToLocation();
482  void removeFromLocation();
483 
484  void updateTasks(const Atlas::Message::Element& e);
485  void removeTask(Task* t);
486 
489  void updateCalculatedVisibility(bool wasVisible);
490 
492  {
493  public:
494  WFMath::Point<3> position;
495  WFMath::Vector<3> velocity;
496  };
497 
498  void updatePredictedState(const WFMath::TimeStamp& t);
499 
500  void createAlarmExpired();
501 
506  virtual TypeService* getTypeService() const = 0;
507 
512  virtual void removeFromMovementPrediction() = 0;
513 
518  virtual void addToMovementPredition() = 0;
519 
524  virtual Entity* getEntity(const std::string& id) = 0;
525 
526 
527  AttrMap m_attrs;
528 
529  TypeInfo* m_type;
530 
531 // primary state, in native form
532  Entity* m_location;
533  EntityArray m_contents;
534 
535  const std::string m_id;
536  std::string m_name;
537  float m_stamp;
538  std::string m_description;
539  bool m_visible;
540  bool m_limbo;
541 
542  WFMath::AxisBox<3> m_bbox;
543  WFMath::Point<3> m_position;
544  WFMath::Vector<3> m_velocity;
545  WFMath::Quaternion m_orientation;
546  WFMath::Vector<3> m_acc;
547 
548  DynamicState m_predicted;
549 
550 // extra state and state tracking things
555 
560  StringSet m_modifiedAttrs;
561 
562  typedef sigc::signal<void, const Atlas::Message::Element&> AttrChangedSignal;
563 
564  typedef std::map<std::string, AttrChangedSignal> ObserverMap;
565  ObserverMap m_observers;
566 
570  bool m_hasBBox;
571 
572  WFMath::TimeStamp m_lastMoveTime;
573  bool m_moving;
574 
576 
577  TaskArray m_tasks;
578 
579  bool m_initialised;
580 };
581 
582 inline unsigned int Entity::numContained() const {
583  return m_contents.size();
584 }
585 
586 inline Entity* Entity::getContained(unsigned int index) const {
587  return m_contents[index];
588 }
589 
590 inline const std::string& Entity::getId() const
591 {
592  return m_id;
593 }
594 
595 inline const std::string& Entity::getName() const
596 {
597  return m_name;
598 }
599 
600 inline float Entity::getStamp() const
601 {
602  return m_stamp;
603 }
604 
605 inline TypeInfo* Entity::getType() const
606 {
607  return m_type;
608 }
609 
612 {
613  return m_location;
614 }
615 
617 inline WFMath::Point<3> Entity::getPosition() const
618 {
619  return m_position;
620 }
622 inline const WFMath::Vector< 3 > & Entity::getVelocity(void) const
623 {
624  return m_velocity;
625 }
626 
628 inline const WFMath::Quaternion & Entity::getOrientation(void) const
629 {
630  return m_orientation;
631 }
632 
634 inline const WFMath::AxisBox< 3 > & Entity::getBBox(void) const
635 {
636  return m_bbox;
637 }
638 
639 inline bool Entity::hasBBox() const
640 {
641  return m_hasBBox;
642 }
643 
644 inline const TaskArray& Entity::getTasks() const
645 {
646  return m_tasks;
647 }
648 
649 template<class C>
650 inline C Entity::toLocationCoords(const C& c) const
651 {
652  return c.toParentCoords(getPredictedPos(), m_orientation);
653 }
654 
655 template<class C>
656 inline C Entity::fromLocationCoords(const C& c) const
657 {
658  return c.toLocalCoords(getPredictedPos(), m_orientation);
659 }
660 
661 inline WFMath::Vector<3> Entity::toLocationCoords(const WFMath::Vector<3>& v) const
662 {
663  return WFMath::Vector<3>(v).rotate(m_orientation);
664 }
665 
666 inline WFMath::Vector<3> Entity::fromLocationCoords(const WFMath::Vector<3>& v) const
667 {
668  return WFMath::Vector<3>(v).rotate(m_orientation.inverse());
669 }
670 
671 } // of namespace
672 
673 #endif
void fillAttributesFromType(Entity::AttrMap &attributes, TypeInfo *typeInfo) const
Utility method for recursively filling a map of attributes from a TypeInfo instance.
Definition: Entity.cpp:156
const WFMath::AxisBox< 3 > & getBBox(void) const
Returns the entity's bounding box in the entity's local system coordinates.
Definition: Entity.h:634
void setFromRoot(const Atlas::Objects::Root &obj, bool allowMotion, bool includeTypeInfoAttributes=false)
Initialise all simple state from a Root.
Definition: Entity.cpp:267
virtual void onVisibilityChanged(bool vis)
over-rideable hook when the actual (computed) visiblity of this entity changed.
Definition: Entity.cpp:815
void filterMoveAttrs(Atlas::Message::MapType &attrs) const
Remove from a map all items whose key is a movement related attribute, eg position or velocity...
Definition: Entity.cpp:297
Entity * getContained(unsigned int index) const
Gets the child entity at the specified index.
Definition: Entity.h:586
virtual void shutdown()
Shuts down the entity.
Definition: Entity.cpp:67
void typeInfo_AttributeChanges(const std::string &attributeName, const Atlas::Message::Element &element)
Connected to the TypeInfo::AttributeChanges event.
Definition: Entity.cpp:473
sigc::signal< void, Task * > TaskAdded
Emitted when a task has been added to the entity.
Definition: Entity.h:324
sigc::signal< void > Moved
Emitted when then entity's position, orientation or velocity change.
Definition: Entity.h:265
const WFMath::Vector< 3 > & getVelocity(void) const
Returns the entity's velocity as last set explicitely.
Definition: Entity.h:622
virtual void onMoved()
over-rideable hook method when then Entity position, orientation or velocity change.
Definition: Entity.cpp:324
sigc::signal< void, const Atlas::Objects::Root & > Noise
Emitted when this entity performs an action which causes a noise.
Definition: Entity.h:306
sigc::signal< void, const StringSet & > Changed
Emitted when one or more attributes change.
Definition: Entity.h:262
WFMath::Vector< 3 > getPredictedVelocity() const
Retrieve the current predicted velocity of an entity.
Definition: Entity.cpp:203
bool isMoving() const
Test if this entity has a non-zero velocity vector.
Definition: Entity.cpp:208
Entity * getLocation() const
The containing entity, or null if this is a top-level visible entity.
Definition: Entity.h:611
unsigned int numContained() const
Gets the number of contained entities, i.e.
Definition: Entity.h:582
bool nativeAttrChanged(const std::string &p, const Atlas::Message::Element &v)
Map Atlas attributes to natively stored properties.
Definition: Entity.cpp:426
void setVisible(bool vis)
the View calls this to change local entity visibility.
Definition: Entity.cpp:766
bool hasBBox() const
Returns true if the entity has a bounding box.
Definition: Entity.h:639
const std::string & getId() const
Retrieve the unique entity ID.
Definition: Entity.h:590
std::string m_name
a human readable name
Definition: Entity.h:536
The representation of an Atlas type (i.e a class or operation definition).
Definition: TypeInfo.h:37
const TaskArray & getTasks() const
Gets the tasks associated with this entity.
Definition: Entity.h:644
sigc::slot< void, const Atlas::Message::Element & > AttrChangedSlot
A slot which can be used for receiving attribute update signals.
Definition: Entity.h:106
virtual Entity * getEntity(const std::string &id)=0
Gets an entity with the supplied id from the system.
View encapsulates the set of entities currently visible to an Avatar, as well as those that have rece...
Definition: View.h:33
sigc::signal< void > BeingDeleted
Emitted prior to deletion.
Definition: Entity.h:319
virtual void onChildRemoved(Entity *child)
Over-rideable hook when child entities are removed.
Definition: Entity.cpp:368
void setLocation(Entity *newLocation)
setLocation is the core of the entity hierarchy maintenance logic.
Definition: Entity.cpp:641
void setLocationFromAtlas(const std::string &locId)
update the entity's location based on Atlas data.
Definition: Entity.cpp:619
bool m_limbo
waiting for parent bind
Definition: Entity.h:540
Definition: IGRouter.h:13
const Atlas::Message::Element & valueOfAttr(const std::string &attr) const
Gets the value of a named attribute.
Definition: Entity.cpp:106
const std::string & getName() const
Gets the name of the entity.
Definition: Entity.h:595
sigc::signal< void, Entity * > LocationChanged
Signal that the entity's container changed.
Definition: Entity.h:258
const std::string m_id
the Atlas object ID
Definition: Entity.h:535
sigc::signal< void, const Atlas::Objects::Root & > Say
Emitted with the entity speaks.
Definition: Entity.h:286
float m_stamp
last modification time (in seconds)
Definition: Entity.h:537
void updateCalculatedVisibility(bool wasVisible)
recursively update the real visiblity of this entity, and fire appropriate signals.
Definition: Entity.cpp:788
sigc::connection observe(const std::string &attr, const AttrChangedSlot &aslot)
Setup an observer so that the specified slot is fired when the named attribue's value changes...
Definition: Entity.cpp:165
WFMath::Quaternion getViewOrientation() const
retreive this Entity's orientation in view coordinates.
Definition: Entity.cpp:182
Definition: Account.cpp:35
bool m_moving
flag recording if this entity is current considered in-motion
Definition: Entity.h:573
sigc::signal< void, bool > VisibilityChanged
Emitted when the visibility of the entity changes.
Definition: Entity.h:312
bool m_recentlyCreated
flag set if this entity was the subject of a sight(create)
Definition: Entity.h:575
virtual TypeService * getTypeService() const =0
Gets the typeservice used throughout the Eris system.
sigc::signal< void, bool > Moving
Emitted when an entity starts or stops moving (as determined by the 'inMotion' method.
Definition: Entity.h:269
WFMath::Point< 3 > getPredictedPos() const
Retrieve the predicted position of this entity, based on it's velocity and acceleration.
Definition: Entity.cpp:193
sigc::signal< void, Task * > TaskRemoved
Emitted when a task has been removed from the entity.
Definition: Entity.h:328
StringSet m_modifiedAttrs
When a batched property update is in progress, the set tracks the names of each modified property...
Definition: Entity.h:560
void setAttr(const std::string &p, const Atlas::Message::Element &v)
Definition: Entity.cpp:381
virtual void attrChangedFromTypeInfo(const std::string &attributeName, const Atlas::Message::Element &element)
Called when an attribute has been changed in the TypeInfo for this entity.
Definition: Entity.cpp:478
float getStamp() const
Access the current time-stamp of the entity.
Definition: Entity.h:600
void setContentsFromAtlas(const StringList &contents)
wrapper for setLocation with additional code the retrive the location if it's not available right now...
Definition: Entity.cpp:686
TypeInfo * getType() const
Gets the type of this entity.
Definition: Entity.h:605
Definition: Entity.h:491
sigc::signal< void, const Atlas::Objects::Operation::RootOperation & > Acted
Emitted when this entity performs an action.
Definition: Entity.h:299
A service class querying and caching types.
Definition: TypeService.h:24
WFMath::Point< 3 > getPosition() const
Returns the Entity's position inside it's parent in the parent's local system coordinates.
Definition: Entity.h:617
Entity is a concrete (instantiable) class representing one game entity.
Definition: Entity.h:57
sigc::signal< void, const std::string & > Emote
Emitted when this entity emits an imaginary operation (also known as an emote.
Definition: Entity.h:292
int m_updateLevel
If greater than zero, we are doing a batched update.
Definition: Entity.h:554
bool isVisible() const
determine if this entity is visible.
Definition: Entity.cpp:778
const AttrMap & getInstanceAttributes() const
Gets all locally defined attributes.
Definition: Entity.cpp:151
bool hasAttr(const std::string &p) const
Checks whether an attribute exists.
Definition: Entity.cpp:126
virtual void onImaginary(const Atlas::Objects::Root &act)
Over-rideable hook when this entity is seen to emit an imginary op.
Definition: Entity.cpp:339
Definition: Task.h:25
virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp)
over-rideable initialisation helper.
Definition: Entity.cpp:92
virtual void onTalk(const Atlas::Objects::Operation::RootOperation &talk)
process TALK data - default implementation emits the Say signal.
Definition: Entity.cpp:306
virtual void addToMovementPredition()=0
Adds the entity to any movement prediction service.
virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation &op)
Over-rideable hook when this entity is heard performing an action.
Definition: Entity.cpp:334
virtual void onTaskAdded(Task *task)
Over-rideable hook for when tasks are added.
Definition: Entity.cpp:373
bool m_hasBBox
This flag should be set when the server notifies that this entity has a bounding box.
Definition: Entity.h:570
WFMath::Point< 3 > getViewPosition() const
retreive this Entity's position in view coordinates.
Definition: Entity.cpp:171
virtual void onChildAdded(Entity *child)
Over-rideable hook when child entities are added.
Definition: Entity.cpp:363
Definition: BaseConnection.h:13
TypeInfoArray getUseOperations() const
Get a list of operations supported by this entity (tool) If the entity does no provide an operations ...
Definition: Entity.cpp:228
const WFMath::Quaternion & getOrientation(void) const
Returns the entity's orientation as last set explicitely.
Definition: Entity.h:628
void sight(const Atlas::Objects::Entity::RootEntity &gent)
Fully initialise all entity state based on a RootEntity, including location and contents.
Definition: Entity.cpp:259
virtual void setMoving(bool moving)
over-rideable hook for when the entity changes from stationary to moving or vice-versa.
Definition: Entity.cpp:346
const AttrMap getAttributes() const
Gets all attributes defined for this entity.
Definition: Entity.cpp:140
virtual void removeFromMovementPrediction()=0
Removes the entity from any movement prediction service.
virtual void onAction(const Atlas::Objects::Operation::RootOperation &act)
Over-rideable hook when this entity is seen to perform an action.
Definition: Entity.cpp:329