Eris  1.3.19
Entity.h
00001 #ifndef ERIS_ENTITY_H
00002 #define ERIS_ENTITY_H
00003 
00004 #include <Eris/Types.h>
00005 
00006 #include <Atlas/Objects/ObjectsFwd.h>
00007 
00008 #include <wfmath/point.h>
00009 #include <wfmath/vector.h>
00010 #include <wfmath/axisbox.h>
00011 #include <wfmath/quaternion.h>
00012 #include <wfmath/timestamp.h>
00013 
00014 #include <sigc++/trackable.h>
00015 #include <sigc++/slot.h>
00016 #include <sigc++/signal.h>
00017 #include <sigc++/connection.h>
00018 
00019 #include <map>
00020 #include <vector>
00021 
00022 namespace Atlas {
00023     namespace Message {
00024         class Element;
00025         typedef std::map<std::string, Element> MapType;
00026     }
00027 }
00028 
00029 namespace Eris {
00030 
00031 // Forward Declerations 
00032 class Entity;
00033 class TypeInfo;
00034 class View;
00035 class EntityRouter;
00036 class Task;
00037 
00038 typedef std::vector<Entity*> EntityArray;
00039 typedef std::vector<Task*> TaskArray;
00040 typedef std::vector<TypeInfoPtr> TypeInfoArray;
00041 
00057 class Entity : virtual public sigc::trackable
00058 {
00059 public: 
00060     typedef std::map<std::string, Atlas::Message::Element> AttrMap;
00061     
00062     explicit Entity(const std::string& id, TypeInfo* ty);
00063     virtual ~Entity();
00064 
00070     virtual void shutdown();
00071 
00072 // heirarchy interface    
00078     unsigned int numContained() const;
00079     
00085     Entity* getContained(unsigned int index) const;
00086 
00094     const Atlas::Message::Element& valueOfAttr(const std::string& attr) const;
00095         
00101     bool hasAttr(const std::string &p) const;
00102 
00106     typedef sigc::slot<void, const Atlas::Message::Element&> AttrChangedSlot;
00107 
00115     sigc::connection observe(const std::string& attr, const AttrChangedSlot& aslot);
00116 
00117 // accessors
00122     const std::string& getId() const;
00123     
00129     const std::string& getName() const;
00130         
00135     float getStamp() const;
00136 
00141     TypeInfo* getType() const;
00142     
00147     Entity* getLocation() const;
00148         
00153     WFMath::Point<3> getPosition() const;
00154     
00162     const AttrMap getAttributes() const;
00163     
00171     const AttrMap& getInstanceAttributes() const;
00172     
00177     bool isMoving() const;
00178         
00184     WFMath::Point<3> getPredictedPos() const;
00185     
00190     WFMath::Vector<3> getPredictedVelocity() const;
00191     
00193     WFMath::Point<3> getViewPosition() const;
00194 
00196     WFMath::Quaternion getViewOrientation() const;
00197     
00199     const WFMath::Vector< 3 > & getVelocity(void) const;
00200     
00202     const WFMath::Quaternion & getOrientation(void) const;
00203     
00205     const WFMath::AxisBox< 3 > & getBBox(void) const;
00206 
00212     bool hasBBox() const;
00213     
00218     const TaskArray& getTasks() const;
00219     
00225     TypeInfoArray getUseOperations() const;
00226     
00227     bool hasChild(const std::string& eid) const;
00228     
00230     bool isVisible() const;
00231 
00232 // coordinate transformations
00233     template<class C>
00234     C toLocationCoords(const C& c) const;
00235     
00236     template<class C>
00237     C fromLocationCoords(const C& c) const;
00238     
00239     // A vector (e.g., the distance between two points, or
00240     // a velocity) gets rotated by a coordinate transformation,
00241     // but doesn't get shifted by the change in the position
00242     // of the origin, so we handle it separately. We also
00243     // need to copy the vector before rotating, because
00244     // Vector::rotate() rotates it in place.
00245     WFMath::Vector<3> toLocationCoords(const WFMath::Vector<3>& v) const;
00246     
00247     WFMath::Vector<3> fromLocationCoords(const WFMath::Vector<3>& v) const;
00248         
00249 // Signals
00250     sigc::signal<void, Entity*> ChildAdded;
00251     sigc::signal<void, Entity*> ChildRemoved;
00252     
00254 
00258     sigc::signal<void, Entity*> LocationChanged;
00259 
00262     sigc::signal<void, const StringSet&> Changed;
00263 
00265     sigc::signal<void> Moved;
00266 
00269     sigc::signal<void, bool> Moving;
00270 
00286     sigc::signal< void, const Atlas::Objects::Root & > Say;
00287         
00292     sigc::signal<void, const std::string&> Emote;
00293     
00299     sigc::signal<void, const Atlas::Objects::Operation::RootOperation&> Acted;
00300     
00306     sigc::signal<void, const Atlas::Objects::Root&> Noise;
00307 
00312     sigc::signal<void, bool> VisibilityChanged;
00313     
00319     sigc::signal<void> BeingDeleted;
00320     
00324     sigc::signal<void, Task*> TaskAdded;
00328     sigc::signal<void, Task*> TaskRemoved;
00329 protected:              
00333     virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp);
00334     
00338     virtual void onTalk(const Atlas::Objects::Operation::RootOperation& talk);
00339 
00340     virtual void onAttrChanged(const std::string& attr, const Atlas::Message::Element &v);
00341         
00342     virtual void onLocationChanged(Entity* oldLoc);
00343     
00346     virtual void onMoved();
00347     
00351     virtual void onVisibilityChanged(bool vis);
00352 
00357     virtual void onAction(const Atlas::Objects::Operation::RootOperation& act);
00358 
00363     virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation& op);
00364 
00369     virtual void onImaginary(const Atlas::Objects::Root& act);
00370 
00376     virtual void setMoving(bool moving);
00377     
00382     virtual void onChildAdded(Entity* child);
00383     
00388     virtual void onChildRemoved(Entity* child);
00389 
00394     virtual void onTaskAdded(Task* task);
00395 
00396     friend class IGRouter;
00397     friend class View;
00398     friend class Task;
00399 
00404     void sight(const Atlas::Objects::Entity::RootEntity& gent);
00405     
00406 
00413     void setFromRoot(const Atlas::Objects::Root& obj, bool allowMotion, bool includeTypeInfoAttributes = false);
00414     
00417     void setVisible(bool vis);
00418     
00419     void setAttr(const std::string &p, const Atlas::Message::Element &v);       
00420         
00425     bool nativeAttrChanged(const std::string &p, const Atlas::Message::Element &v);
00426     
00433     void typeInfo_AttributeChanges(const std::string& attributeName, const Atlas::Message::Element& element);
00434     
00441     virtual void attrChangedFromTypeInfo(const std::string& attributeName, const Atlas::Message::Element& element);
00442     
00443     
00450     void fillAttributesFromType(Entity::AttrMap& attributes, TypeInfo* typeInfo) const;
00451     
00452     void beginUpdate();
00453     void addToUpdate(const std::string& attr);
00454     void endUpdate();
00455 
00458     void setLocationFromAtlas(const std::string& locId);
00459     
00463     void setLocation(Entity* newLocation);
00464     
00467     void setContentsFromAtlas(const StringList& contents);
00468     
00473     void filterMoveAttrs(Atlas::Message::MapType& attrs) const;
00474 
00475     typedef std::map<std::string, Entity*> IdEntityMap;
00476     void buildEntityDictFromContents(IdEntityMap& dict);
00477     
00478     void addChild(Entity* e);
00479     void removeChild(Entity* e);
00480 
00481     void addToLocation();
00482     void removeFromLocation();
00483 
00484     void updateTasks(const Atlas::Message::Element& e);
00485     void removeTask(Task* t);
00486 
00489     void updateCalculatedVisibility(bool wasVisible);
00490         
00491     class DynamicState
00492     {
00493     public:
00494         WFMath::Point<3> position;
00495         WFMath::Vector<3> velocity;
00496     };
00497     
00498     void updatePredictedState(const WFMath::TimeStamp& t);
00499     
00500     void createAlarmExpired();
00501     
00506     virtual TypeService* getTypeService() const = 0;
00507 
00512     virtual void removeFromMovementPrediction() = 0;
00513 
00518     virtual void addToMovementPredition() = 0;
00519 
00524     virtual Entity* getEntity(const std::string& id) = 0;
00525 
00526 
00527     AttrMap m_attrs;
00528     
00529     TypeInfo* m_type;
00530     
00531 // primary state, in native form
00532     Entity* m_location; 
00533     EntityArray m_contents;
00534     
00535     const std::string m_id;     
00536     std::string m_name;         
00537     float m_stamp;              
00538     std::string m_description;
00539     bool m_visible;
00540     bool m_limbo;   
00541     
00542     WFMath::AxisBox<3> m_bbox;
00543     WFMath::Point<3> m_position;
00544     WFMath::Vector<3> m_velocity;
00545     WFMath::Quaternion m_orientation;    
00546     WFMath::Vector<3> m_acc;
00547     
00548     DynamicState m_predicted;
00549     
00550 // extra state and state tracking things
00554     int m_updateLevel;
00555 
00560     StringSet m_modifiedAttrs;
00561         
00562     typedef sigc::signal<void, const Atlas::Message::Element&> AttrChangedSignal;
00563         
00564     typedef std::map<std::string, AttrChangedSignal> ObserverMap;
00565     ObserverMap m_observers;
00566 
00570     bool m_hasBBox;
00571     
00572     WFMath::TimeStamp m_lastMoveTime;
00573     bool m_moving; 
00574     
00575     bool m_recentlyCreated; 
00576     
00577     TaskArray m_tasks;
00578 
00579     bool m_initialised;
00580 };
00581 
00582 inline unsigned int Entity::numContained() const {
00583     return m_contents.size();
00584 }
00585 
00586 inline Entity* Entity::getContained(unsigned int index) const {
00587     return m_contents[index];
00588 }
00589 
00590 inline const std::string& Entity::getId() const
00591 {
00592     return m_id;
00593 }
00594 
00595 inline const std::string& Entity::getName() const
00596 {
00597     return m_name;
00598 }
00599 
00600 inline float Entity::getStamp() const
00601 {
00602     return m_stamp;
00603 }
00604 
00605 inline TypeInfo* Entity::getType() const
00606 {
00607     return m_type;
00608 }
00609 
00611 inline Entity* Entity::getLocation() const
00612 {
00613     return m_location;
00614 }
00615 
00617 inline WFMath::Point<3> Entity::getPosition() const
00618 {
00619     return m_position;
00620 }
00622 inline const WFMath::Vector< 3 > & Entity::getVelocity(void) const
00623 {
00624     return m_velocity;
00625 }
00626 
00628 inline const WFMath::Quaternion & Entity::getOrientation(void) const
00629 {
00630     return m_orientation;
00631 }
00632 
00634 inline const WFMath::AxisBox< 3 > & Entity::getBBox(void) const
00635 {
00636     return m_bbox;
00637 }
00638 
00639 inline bool Entity::hasBBox() const
00640 {
00641     return m_hasBBox;
00642 }
00643 
00644 inline const TaskArray& Entity::getTasks() const
00645 {
00646     return m_tasks; 
00647 }
00648 
00649 template<class C>
00650 inline C Entity::toLocationCoords(const C& c) const
00651 {
00652     return c.toParentCoords(getPredictedPos(), m_orientation);
00653 }
00654 
00655 template<class C>
00656 inline C Entity::fromLocationCoords(const C& c) const
00657 {
00658     return c.toLocalCoords(getPredictedPos(), m_orientation);
00659 }
00660 
00661 inline WFMath::Vector<3> Entity::toLocationCoords(const WFMath::Vector<3>& v) const
00662 {
00663     return WFMath::Vector<3>(v).rotate(m_orientation);
00664 }
00665 
00666 inline WFMath::Vector<3> Entity::fromLocationCoords(const WFMath::Vector<3>& v) const
00667 {
00668     return WFMath::Vector<3>(v).rotate(m_orientation.inverse());
00669 }
00670 
00671 } // of namespace
00672 
00673 #endif