/usr/include/Eris-1.3/Eris/Entity.h is in liberis-1.3-dev 1.3.23-6ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | #ifndef ERIS_ENTITY_H
#define ERIS_ENTITY_H
#include <Eris/Types.h>
#include <Atlas/Objects/ObjectsFwd.h>
#include <wfmath/point.h>
#include <wfmath/vector.h>
#include <wfmath/axisbox.h>
#include <wfmath/quaternion.h>
#include <wfmath/timestamp.h>
#include <sigc++/trackable.h>
#include <sigc++/slot.h>
#include <sigc++/signal.h>
#include <sigc++/connection.h>
#include <map>
#include <vector>
namespace Atlas {
namespace Message {
class Element;
typedef std::map<std::string, Element> MapType;
}
}
namespace Eris {
// Forward Declerations
class Entity;
class TypeInfo;
class View;
class EntityRouter;
class Task;
typedef std::vector<Entity*> EntityArray;
typedef std::vector<Task*> TaskArray;
typedef std::vector<TypeInfoPtr> TypeInfoArray;
/**
@brief Entity is a concrete (instantiable) class representing one game entity
Entity encapsulates the state and tracking of one game entity; this includes
it's location in the containership tree (graph?), it's name and unique and id,
and so on.
This class may be sub-classed by users (and those sub-classes built via
a Factory), to allow specific functionality. This means there are two
integration strategies; either subclassing and over-riding virtual functions,
or creating peer clases and attaching them to the signals.
@note If you handle entities manually, you must make sure to call shutdown() before the instance is deleted.
*/
class Entity : virtual public sigc::trackable
{
public:
typedef std::map<std::string, Atlas::Message::Element> AttrMap;
explicit Entity(const std::string& id, TypeInfo* ty);
virtual ~Entity();
/**
* @brief Shuts down the entity. A call to this must be made before the entity is deleted.
* In normal operations, where Eris itself takes care of the entities, it will be called automatically.
* If you however manually handle instance of this in your code you must call it yourself.
*/
virtual void shutdown();
// heirarchy interface
/**
* @brief Gets the number of contained entities, i.e. entities that are direct children of this.
* The number returned is only for direct children, so the number of nested entities can be larger.
* @return
*/
unsigned int numContained() const;
/**
* @brief Gets the child entity at the specified index.
* @param index An index for the collection of child entities. This must be a valid index as no bounds checking will happen.
* @return A pointer to a child entity.
*/
Entity* getContained(unsigned int index) const;
/**
* @brief Gets the value of a named attribute.
* If no attribute by the specified name can be found an InvalidOperation exception will be thrown. Therefore always first call hasAttr to make sure that the attribute exists.
* @param attr The attribute name.
* @return A reference to the attribute by the specified name.
* @throws InvalidOperation If no attribute by the specified name can be found.
*/
const Atlas::Message::Element& valueOfAttr(const std::string& attr) const;
/**
* @brief Checks whether an attribute exists.
* @param p The name of the attribute.
* @return True if the attribute exists.
*/
bool hasAttr(const std::string &p) const;
/**
* @brief A slot which can be used for receiving attribute update signals.
*/
typedef sigc::slot<void, const Atlas::Message::Element&> AttrChangedSlot;
/**
* @brief Setup an observer so that the specified slot is fired when the named attribue's value changes
*
* @param attr The name of the attribute to observe.
* @param aslot The slot which will be fired when the attribute changes.
* @return The connection created.
*/
sigc::connection observe(const std::string& attr, const AttrChangedSlot& aslot);
// accessors
/**
* @brief Retrieve the unique entity ID.
* @return The unique id of the entity.
*/
const std::string& getId() const;
/**
* @brief Gets the name of the entity.
* In contrast to getId() this is not unique, and doesn't even have to be set.
* @return The name of the entity.
*/
const std::string& getName() const;
/**
* @brief Access the current time-stamp of the entity.
* @return The current time stamp.
*/
float getStamp() const;
/**
* @brief Gets the type of this entity.
* @return The type of this entity. This can be null.
*/
TypeInfo* getType() const;
/**
* @brief The containing entity, or null if this is a top-level visible entity.
* @return The containing entity, or null.
*/
Entity* getLocation() const;
/**
* @brief Returns the Entity's position inside it's parent in the parent's local system coordinates.
* @return The position of the entity in parent relative coords.
*/
WFMath::Point<3> getPosition() const;
/**
* @brief Gets all attributes defined for this entity.
* The collection of entities returned will include both local attributes as well as the defaults set in the TypeInfo (and all of its parents) of this entity.
* @note This is a rather expensive operation since it needs to iterate over all parent TypeInfo instances and build up a map, which is then returned by value. If you only want to get a single attribute you should instead use the valueOfAttr method.
* @see getInstanceAttributes() for a similiar method which only returns those attributes that are local to this entity.
* @return A map of the combined attributes of both this entity and all of it's TypeInfo parents.
*/
const AttrMap getAttributes() const;
/**
* @brief Gets all locally defined attributes.
* This will only return those attributes that are locally defined for this entity. In practice it will in most cases mean those attributes that have been changed by the defaults as defined in the TypeInfo instance.
* @note This will only return a subset of all attributes. If you need to iterate over all attributes you should instead use the getAttributes() method. If you only want the value of a specific attribute you should use the valueOfAttr method.
* @see getAttributes
* @return The locally defined attributes for the entity.
*/
const AttrMap& getInstanceAttributes() const;
/**
* @brief Test if this entity has a non-zero velocity vector.
* @return True if the entity has a non-zero velocity.
*/
bool isMoving() const;
/**
* @brief Retrieve the predicted position of this entity, based on it's velocity and acceleration.
* If the entity is not moving, this is the same as calling getPosition().
* @return The predicted position of the entity.
*/
WFMath::Point<3> getPredictedPos() const;
/**
* @brief Retrieve the current predicted velocity of an entity. If the entity is not moving, this is an <em>invalid</em> Vector.
* @return The predicted velocity of the entity.
*/
WFMath::Vector<3> getPredictedVelocity() const;
/** retreive this Entity's position in view coordinates. */
WFMath::Point<3> getViewPosition() const;
/** retreive this Entity's orientation in view coordinates. */
WFMath::Quaternion getViewOrientation() const;
/** Returns the entity's velocity as last set explicitely. **/
const WFMath::Vector< 3 > & getVelocity(void) const;
/** Returns the entity's orientation as last set explicitely. **/
const WFMath::Quaternion & getOrientation(void) const;
/** Returns the entity's bounding box in the entity's local system coordinates. **/
const WFMath::AxisBox< 3 > & getBBox(void) const;
/**
* @brief Returns true if the entity has a bounding box.
* Not all entities have bounding boxes, but those that are represented as physical objects in the world usually do.
* @return True if the entity has a bounding box.
*/
bool hasBBox() const;
/**
* @brief Gets the tasks associated with this entity.
* @return The tasks associated with this entity.
*/
const TaskArray& getTasks() const;
/**
@brief Get a list of operations supported by this entity (tool)
If the entity does no provide an operations list, this will return an empty
array.
*/
TypeInfoArray getUseOperations() const;
bool hasChild(const std::string& eid) const;
/** determine if this entity is visible. */
bool isVisible() const;
/**
* @brief Extracts an entity id from the supplied element.
*
* There are two ways to express entity ids; either as a string,
* or as a map with one entry named "$eid" (where the string is the value).
* The function parses the element for these two cases, if found fills the
* "id" parameter and then returns true.
*
* @param element The element which we want to extract from.
* @param id The string where the id, if found, will be placed.
* @return True if an id could be found.
*/
static bool extractEntityId(const Atlas::Message::Element& element, std::string& id);
// coordinate transformations
template<class C>
C toLocationCoords(const C& c) const;
template<class C>
C fromLocationCoords(const C& c) const;
// A vector (e.g., the distance between two points, or
// a velocity) gets rotated by a coordinate transformation,
// but doesn't get shifted by the change in the position
// of the origin, so we handle it separately. We also
// need to copy the vector before rotating, because
// Vector::rotate() rotates it in place.
WFMath::Vector<3> toLocationCoords(const WFMath::Vector<3>& v) const;
WFMath::Vector<3> fromLocationCoords(const WFMath::Vector<3>& v) const;
// Signals
sigc::signal<void, Entity*> ChildAdded;
sigc::signal<void, Entity*> ChildRemoved;
/// Signal that the entity's container changed
/** emitted when our location changes. First argument is the old location.
The new location can be found via getLocation.
Note either the old or new location might be NULL.
*/
sigc::signal<void, Entity*> LocationChanged;
/** Emitted when one or more attributes change. The arguments is a set
of attribute IDs which were modified. */
sigc::signal<void, const StringSet&> Changed;
/** Emitted when then entity's position, orientation or velocity change.*/
sigc::signal<void> Moved;
/** Emitted when an entity starts or stops moving (as determined by the
'inMotion' method. */
sigc::signal<void, bool> Moving;
/**
* @brief Emitted with the entity speaks.
*
* The argument contains attributes that make up the content of the Say
* operation.
* - Attribute "say" which is of type string and contains the text that
* this entity said.
* - Attrubute "responses" which is a list of strings. When conversing
* with NPCs this list is used to give the client a clue about
* possible answers the NPCs understand.
* - Attribute "address" which is optional, and is a list of strings,
* which are entity id values specifying those entities that were
* addressed. Note that all entities, even those not addressed, can
* still receive such Say operations.
**/
sigc::signal< void, const Atlas::Objects::Root & > Say;
/**
Emitted when this entity emits an imaginary operation (also known as
an emote. This is used for debugging, but not much else.
*/
sigc::signal<void, const std::string&> Emote;
/**
Emitted when this entity performs an action. The argument to the
action is passed as the signal argument. For examples of action
arguments, see some documentation that probably isn't written yet.
*/
sigc::signal<void, const Atlas::Objects::Operation::RootOperation&> Acted;
/**
Emitted when this entity performs an action which causes a noise. This
may happen alongside the sight of the action, or not, depending on the
distance to the entity and so on.
*/
sigc::signal<void, const Atlas::Objects::Root&> Noise;
/**
Emitted when the visibility of the entity changes. Often this happens
because it has moved in or out of the sight range of the avatar.
*/
sigc::signal<void, bool> VisibilityChanged;
/**
Emitted prior to deletion. Note that entity instances may be deleted for
different reasons - passing out of the view, being deleted on the server,
or during disconnection. This signal is emitted regardless.
*/
sigc::signal<void> BeingDeleted;
/**
Emitted when a task has been added to the entity. Argument is the task.
*/
sigc::signal<void, Task*> TaskAdded;
/**
Emitted when a task has been removed from the entity. Argument is the task.
*/
sigc::signal<void, Task*> TaskRemoved;
protected:
/** over-rideable initialisation helper. When subclassing, if you
over-ride this method, take care to call the base implementation, or
unfortunate things will happen. */
virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp);
/** process TALK data - default implementation emits the Say signal.
@param talk The TALK operation
*/
virtual void onTalk(const Atlas::Objects::Operation::RootOperation& talk);
virtual void onAttrChanged(const std::string& attr, const Atlas::Message::Element &v);
virtual void onLocationChanged(Entity* oldLoc);
/** over-rideable hook method when then Entity position, orientation or
velocity change. The default implementation emits the Moved signal. */
virtual void onMoved();
/** over-rideable hook when the actual (computed) visiblity of this
entity changed. The default implementation emits the VisiblityChanged
signal. */
virtual void onVisibilityChanged(bool vis);
/**
Over-rideable hook when this entity is seen to perform an action.
Default implementation emits the Action signal.
*/
virtual void onAction(const Atlas::Objects::Operation::RootOperation& act);
/**
Over-rideable hook when this entity is heard performing an action.
Default implementation emits the Noise signal.
*/
virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation& op);
/**
Over-rideable hook when this entity is seen to emit an imginary op.
Default implementation emits the Emote signal.
*/
virtual void onImaginary(const Atlas::Objects::Root& act);
/** over-rideable hook for when the entity changes from stationary to
moving or vice-versa. This hook exists so a client can treat moving objects
differently (eg, placing them in a different part of the scene graph).
If you over-ride this, you <em>must</em> call the base version, or motion prediction
will stop working for the entity. */
virtual void setMoving(bool moving);
/**
Over-rideable hook when child entities are added. The default implementation
emits the ChildAdded signal.
*/
virtual void onChildAdded(Entity* child);
/**
Over-rideable hook when child entities are removed. The default implementation
emits the Childremoved signal.
*/
virtual void onChildRemoved(Entity* child);
/**
* @brief Over-rideable hook for when tasks are added.
* @param task The new task.
*/
virtual void onTaskAdded(Task* task);
friend class IGRouter;
friend class View;
friend class Task;
/**
Fully initialise all entity state based on a RootEntity, including
location and contents.
*/
void sight(const Atlas::Objects::Entity::RootEntity& gent);
/**
* @brief Initialise all simple state from a Root. This excludes location and contents, and may optionally exclude all attributes related to motion.
* @param obj The atlas object containing the data.
* @param allowMotion If false, motion elements (position, velocity etc.) will be filtered out.
* @param includeTypeInfoAttributes If true, the default attributes of the type info will be used too. This is normally only desired when the entity is initially set up.
*/
void setFromRoot(const Atlas::Objects::Root& obj, bool allowMotion, bool includeTypeInfoAttributes = false);
/** the View calls this to change local entity visibility. No one else
should be calling it!*/
void setVisible(bool vis);
void setAttr(const std::string &p, const Atlas::Message::Element &v);
/**
Map Atlas attributes to natively stored properties. Should be changed to
use an integer hash in the future, since this called frequently.
*/
bool nativeAttrChanged(const std::string &p, const Atlas::Message::Element &v);
/**
* @brief Connected to the TypeInfo::AttributeChanges event.
* This will in turn call the attrChangedFromTypeInfo, which is overridable in a subclass if so desired.
* @param attributeName The name of the attribute which is to be changed.
* @param element The new element data.
*/
void typeInfo_AttributeChanges(const std::string& attributeName, const Atlas::Message::Element& element);
/**
* @brief Called when an attribute has been changed in the TypeInfo for this entity.
* If the attribute doesn't have an instance value local to this entity the event will be processed just like a call to setAttr but without the attribute being saved in the map of instance attributes.
* @param attributeName The name of the attribute which is to be changed.
* @param element The new element data.
*/
virtual void attrChangedFromTypeInfo(const std::string& attributeName, const Atlas::Message::Element& element);
/**
* @brief Utility method for recursively filling a map of attributes from a TypeInfo instance.
* The method will recursively call itself to make sure that the topmost TypeInfo is used first. This makes sure that attributes are overwritten by newer values, if duplicates exists.
* @param attributes The map of attributes to fill.
* @param typeInfo The type info from which we will copy values, as well as its parents.
*/
void fillAttributesFromType(Entity::AttrMap& attributes, TypeInfo* typeInfo) const;
void beginUpdate();
void addToUpdate(const std::string& attr);
void endUpdate();
/** update the entity's location based on Atlas data. This is used by
the MOVE handler to update the location information. */
void setLocationFromAtlas(const std::string& locId);
/** setLocation is the core of the entity hierarchy maintenance logic.
We make setting location the 'fixup' action; addChild / removeChild are
correspondingly simple. */
void setLocation(Entity* newLocation);
/// wrapper for setLocation with additional code the retrive the
/// location if it's not available right now
void setContentsFromAtlas(const StringList& contents);
/**
Remove from a map all items whose key is a movement related attribute,
eg position or velocity
*/
void filterMoveAttrs(Atlas::Message::MapType& attrs) const;
typedef std::map<std::string, Entity*> IdEntityMap;
void buildEntityDictFromContents(IdEntityMap& dict);
void addChild(Entity* e);
void removeChild(Entity* e);
void addToLocation();
void removeFromLocation();
void updateTasks(const Atlas::Message::Element& e);
void removeTask(Task* t);
/** recursively update the real visiblity of this entity, and fire
appropriate signals. */
void updateCalculatedVisibility(bool wasVisible);
class DynamicState
{
public:
WFMath::Point<3> position;
WFMath::Vector<3> velocity;
};
void updatePredictedState(const WFMath::TimeStamp& t);
void createAlarmExpired();
/**
* @brief Gets the typeservice used throughout the Eris system.
* @returns A type service instance.
*/
virtual TypeService* getTypeService() const = 0;
/**
* @brief Removes the entity from any movement prediction service.
* This is called when movement has stopped.
*/
virtual void removeFromMovementPrediction() = 0;
/**
* @brief Adds the entity to any movement prediction service.
* This is called when movement has started.
*/
virtual void addToMovementPredition() = 0;
/**
* @brief Gets an entity with the supplied id from the system.
* @param id The id of the entity to get.
*/
virtual Entity* getEntity(const std::string& id) = 0;
AttrMap m_attrs;
TypeInfo* m_type;
// primary state, in native form
Entity* m_location;
EntityArray m_contents;
const std::string m_id; ///< the Atlas object ID
std::string m_name; ///< a human readable name
float m_stamp; ///< last modification time (in seconds)
std::string m_description;
bool m_visible;
bool m_limbo; ///< waiting for parent bind
WFMath::AxisBox<3> m_bbox;
WFMath::Point<3> m_position;
WFMath::Vector<3> m_velocity;
WFMath::Quaternion m_orientation;
WFMath::Vector<3> m_acc;
DynamicState m_predicted;
// extra state and state tracking things
/** If greater than zero, we are doing a batched update. This supresses emission
of the Changed signal until endUpdate is called, so that a number of
attributes may be updated en-masse, generating just one signal. */
int m_updateLevel;
/** When a batched property update is in progress, the set tracks the names
of each modified property. This set is passed as a parameter of the Changed
callback when endUpdate is called, to allow clients to determine what
was changed. */
StringSet m_modifiedAttrs;
typedef sigc::signal<void, const Atlas::Message::Element&> AttrChangedSignal;
typedef std::map<std::string, AttrChangedSignal> ObserverMap;
ObserverMap m_observers;
/** This flag should be set when the server notifies that this entity
has a bounding box. If this flag is not true, the contents of the
BBox attribute are undefined. */
bool m_hasBBox;
WFMath::TimeStamp m_lastMoveTime;
bool m_moving; ///< flag recording if this entity is current considered in-motion
bool m_recentlyCreated; ///< flag set if this entity was the subject of a sight(create)
TaskArray m_tasks;
bool m_initialised;
};
inline unsigned int Entity::numContained() const {
return m_contents.size();
}
inline Entity* Entity::getContained(unsigned int index) const {
return m_contents[index];
}
inline const std::string& Entity::getId() const
{
return m_id;
}
inline const std::string& Entity::getName() const
{
return m_name;
}
inline float Entity::getStamp() const
{
return m_stamp;
}
inline TypeInfo* Entity::getType() const
{
return m_type;
}
/** the containing entity, or null if this is a top-level visible entity. */
inline Entity* Entity::getLocation() const
{
return m_location;
}
/** Returns the Entity's position inside it's parent in the parent's local system coordinates. **/
inline WFMath::Point<3> Entity::getPosition() const
{
return m_position;
}
/** Returns the entity's velocity as last set explicitely. **/
inline const WFMath::Vector< 3 > & Entity::getVelocity(void) const
{
return m_velocity;
}
/** Returns the entity's orientation as last set explicitely. **/
inline const WFMath::Quaternion & Entity::getOrientation(void) const
{
return m_orientation;
}
/** Returns the entity's bounding box in the entity's local system coordinates. **/
inline const WFMath::AxisBox< 3 > & Entity::getBBox(void) const
{
return m_bbox;
}
inline bool Entity::hasBBox() const
{
return m_hasBBox;
}
inline const TaskArray& Entity::getTasks() const
{
return m_tasks;
}
template<class C>
inline C Entity::toLocationCoords(const C& c) const
{
return c.toParentCoords(getPredictedPos(), m_orientation);
}
template<class C>
inline C Entity::fromLocationCoords(const C& c) const
{
return c.toLocalCoords(getPredictedPos(), m_orientation);
}
inline WFMath::Vector<3> Entity::toLocationCoords(const WFMath::Vector<3>& v) const
{
return WFMath::Vector<3>(v).rotate(m_orientation);
}
inline WFMath::Vector<3> Entity::fromLocationCoords(const WFMath::Vector<3>& v) const
{
return WFMath::Vector<3>(v).rotate(m_orientation.inverse());
}
} // of namespace
#endif
|