Cafu Engine
BaseEntity.hpp
1 /*
2 =================================================================================
3 This file is part of Cafu, the open-source game engine and graphics engine
4 for multiplayer, cross-platform, real-time 3D action.
5 Copyright (C) 2002-2013 Carsten Fuchs Software.
6 
7 Cafu is free software: you can redistribute it and/or modify it under the terms
8 of the GNU General Public License as published by the Free Software Foundation,
9 either version 3 of the License, or (at your option) any later version.
10 
11 Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with Cafu. If not, see <http://www.gnu.org/licenses/>.
17 
18 For support and more information about Cafu, visit us at <http://www.cafu.de>.
19 =================================================================================
20 */
21 
22 #ifndef CAFU_BASE_ENTITY_HPP_INCLUDED
23 #define CAFU_BASE_ENTITY_HPP_INCLUDED
24 
25 #include "../../GameEntity.hpp"
26 #include "ClipSys/ClipModel.hpp"
27 #include "GameSys/Entity.hpp"
28 
29 #include <map>
30 
31 #if defined(_WIN32) && _MSC_VER<1600
32 #include "pstdint.h" // Paul Hsieh's portable implementation of the stdint.h header.
33 #else
34 #include <stdint.h>
35 #endif
36 
37 
38 class PhysicsWorldT;
39 struct lua_State;
40 namespace cf { namespace ClipSys { class CollisionModelT; } }
41 namespace cf { namespace TypeSys { class TypeInfoManT; } }
42 namespace cf { namespace TypeSys { class CreateParamsT; } }
43 
44 
45 namespace GAME_NAME
46 {
47  class EntityCreateParamsT;
48  class ApproxBaseT;
49 
50 
51  /// The TypeInfoTs of all BaseEntityT derived classes must register with this TypeInfoManT instance.
52  cf::TypeSys::TypeInfoManT& GetBaseEntTIM();
53 
54 
55  // This class describes "base entities", the most central component in game<-->engine communication.
56  class BaseEntityT : public GameEntityI
57  {
58  public:
59 
60  const std::map<std::string, std::string> Properties; ///< The properties of this entities from the map file.
61 
62  IntrusivePtrT<cf::GameSys::EntityT> m_Entity; ///< The associated entity in the cf::GameSys::WorldT.
63 
64 
65  /// The destructor.
66  virtual ~BaseEntityT();
67 
68  /// The implementation calls DoSerialize(), that derived classes override to add their own data.
69  void Serialize(cf::Network::OutStreamT& Stream) const /*final override*/;
70 
71  /// The implementation calls DoDeserialize(), that derived classes override to read their own data.
72  /// It also calls ProcessEvent() (overridden by derived classes) for any received events.
73  void Deserialize(cf::Network::InStreamT& Stream, bool IsIniting) /*final override*/;
74 
75  // Implement GameEntityI base class methods.
76  virtual void NotifyLeaveMap() { }
77  virtual const BoundingBox3dT& GetDimensions() const { return m_Dimensions; }
78  virtual void GetBodyOrientation(unsigned short& h, unsigned short& p, unsigned short& b) const { h=0; p=0; b=0; /*TODO: this method is called from obsolete methods only...*/ }
79 
80 
81  /// This SERVER-SIDE function is used to notify this entity that it was touched by another entity.
82  /// 'Entity' is the entity that touches this one, and is usually the entity from which the call is made.
83  /// Calls are only made from within other entities 'Think()' functions.
84  /// (Unfortunately, this function cannot be declared as "protected": see "C++ FAQs, 2nd edition" by Cline, Lomow, Girou, pages 249f.)
85  virtual void NotifyTouchedBy(BaseEntityT* Entity);
86 
87  /// This SERVER-SIDE method is called whenever another entity walked into one of our trigger volumes (trigger brushes).
88  /// It is called once per frame as long as the other entity stays within our trigger volume.
89  ///
90  /// @param Activator The entity that walked into our trigger volume and thus caused the call of this method.
91  ///
92  /// Note that most entity classes just do nothing in their implementation of this method, usually because they never have trigger brushes anyway.
93  /// Entity classes that are very likely to provide an implementation though are EntTriggerT (of course!) and the items like weapons,
94  /// so they can e.g. detect being picked up.
95  /// Calls to this method normally come from within the Activator->Think() method, because it are the activators themselves that detect
96  /// that they entered a trigger volume (it are not the entities with the triggers that detect that something entered their volume).
97  /// (Unfortunately, this function cannot be declared as "protected": see "C++ FAQs, 2nd edition" by Cline, Lomow, Girou, pages 249f.)
98  virtual void OnTrigger(BaseEntityT* Activator);
99 
100  /// This SERVER-SIDE function is used to have this entity take damage.
101  /// 'Entity' is the entitiy that causes the damage (i.e., who fired a shot). It is usually the entity from which the call is made.
102  /// 'Amount' is the amount of damage that was caused, and is usually subtracted from this entitys health.
103  /// 'ImpactDir' is the direction from which we were hit.
104  /// Calls are only made from within other entities 'Think()' functions.
105  /// (Unfortunately, this function cannot be declared as "protected": see "C++ FAQs, 2nd edition" by Cline, Lomow, Girou, pages 249f.)
106  virtual void TakeDamage(BaseEntityT* Entity, char Amount, const VectorT& ImpactDir);
107 
108  /// This SERVER-SIDE function is used for posting an event of the given type.
109  /// The event is automatically sent from the entity instance on the server to the entity instances
110  /// on the clients, and causes a matching call to ProcessEvent() there.
111  /// The meaning of the event type is specific to the concrete entity class.
112  /// Note that events are fully predictable: they work well even in the presence of client prediction.
113  void PostEvent(unsigned int EventType) { m_EventsCount[EventType]++; }
114 
115 
116  /// This CLIENT-SIDE function is called to process events on the client.
117  /// Events that have been posted via PostEvent() on the server (or in client prediction) are eventually
118  /// received in Deserialize(), which automatically calls this method.
119  /// In the call, Deserialize() indicates the number of new events since the last call (at least one,
120  /// or it wouldn't call ProcessEvent() at all). This way, each event can be processed exactly once.
121  ///
122  /// TODO: Move into private section below "DoDeserialize()" ??
123  virtual void ProcessEvent(unsigned int EventType, unsigned int NumEvents);
124 
125  // Implement CLIENT-SIDE GameEntityI base class methods.
126  virtual bool GetLightSourceInfo(unsigned long& DiffuseColor, unsigned long& SpecularColor, VectorT& Position, float& Radius, bool& CastsShadows) const /*override*/;
127  virtual void Draw(bool FirstPersonView, float LodDist) const /*override*/;
128  virtual void Interpolate(float FrameTime) /*final override*/;
129  virtual void PostDraw(float FrameTime, bool FirstPersonView) /*override*/;
130 
131 
132  /// Returns the proper type info for this entity.
133  virtual const cf::TypeSys::TypeInfoT* GetType() const;
134  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
135  static const cf::TypeSys::TypeInfoT TypeInfo; ///< The type info object for (objects/instances of) this class.
136 
137 
138  // Methods provided to be called from the map/entity Lua scripts.
139  static int GetName(lua_State* L);
140 
141 
142  protected:
143 
144  /// Protected constructor such that only concrete entities can call this for creating a BaseEntityT, but nobody else.
145  /// Concrete entities are created in the GameI::CreateGameEntity() method for both the server- and client-side.
146  BaseEntityT(const EntityCreateParamsT& Params, const BoundingBox3dT& Dimensions, const unsigned int NUM_EVENT_TYPES);
147 
148  /// Concrete entities call this method in their constructors in order to have us automatically interpolate
149  /// the value that has been specified with the Interp instance.
150  /// Interpolation occurs on the client only: it is advanced in the video frames between server updates.
151  /// Each update received from the server (applied in DoDeserialize()) automatically updates the reference value
152  /// for the interpolation in the next video frames.
153  void Register(ApproxBaseT* Interp);
154 
155  BoundingBox3dT m_Dimensions; ///< The bounding box of this entity (relative to the origin).
156 
157 
158  private:
159 
160  BaseEntityT(const BaseEntityT&); ///< Use of the Copy Constructor is not allowed.
161  void operator = (const BaseEntityT&); ///< Use of the Assignment Operator is not allowed.
162 
163  /// Derived classes override this method in order to write their state into the given stream.
164  /// The method itself is automatically called from Serialize(), see Serialize() for more details.
165  ///
166  /// (This follows the "Non-Virtual Interface Idiom" as described by Scott Meyers in
167  /// "Effective C++, 3rd Edition", item 35 ("Consider alternatives to virtual functions.").)
168  virtual void DoSerialize(cf::Network::OutStreamT& Stream) const { }
169 
170  /// Derived classes override this method in order to read their state from the given stream.
171  /// The method itself is automatically called from Deserialize(), see Deserialize() for more details.
172  ///
173  /// (This follows the "Non-Virtual Interface Idiom" as described by Scott Meyers in
174  /// "Effective C++, 3rd Edition", item 35 ("Consider alternatives to virtual functions.").)
175  virtual void DoDeserialize(cf::Network::InStreamT& Stream) { }
176 
177  ArrayT<uint32_t> m_EventsCount; ///< A counter for each event type for the number of its occurrences. Serialized (and deserialized) normally along with the entity state.
178  ArrayT<uint32_t> m_EventsRef; ///< A reference counter for each event type for the number of processed occurrences. Never serialized (or deserialized), never reset, strictly growing.
179 
180  ArrayT<ApproxBaseT*> m_Interpolators; ///< The interpolators for advancing values in the client video frames between server updates. The values that are interpolated are specified by calls to Register() by the derived entity class.
181  };
182 }
183 
184 #endif
Definition: PhysicsWorld.hpp:173
This is the interface that the client and server use to access and work with game entities...
Definition: GameEntity.hpp:40
const std::map< std::string, std::string > Properties
The properties of this entities from the map file.
Definition: BaseEntity.hpp:60
This class is used for reading data from a StateT instance (deserialization).
Definition: State.hpp:222
Definition: EntityCreateParams.hpp:37
Definition: BaseEntity.hpp:56
This class manages the type infos.
Definition: TypeSys.hpp:159
virtual const BoundingBox3dT & GetDimensions() const
Returns the dimensions of this entity.
Definition: BaseEntity.hpp:77
This class is used for writing data into a StateT instance (serialization).
Definition: State.hpp:96
A common base class for "approximators" (interpolators and extrapolators), so that approximators of d...
Definition: Interpolator.hpp:30
IntrusivePtrT< cf::GameSys::EntityT > m_Entity
The associated entity in the cf::GameSys::WorldT.
Definition: BaseEntity.hpp:62
BoundingBox3dT m_Dimensions
The bounding box of this entity (relative to the origin).
Definition: BaseEntity.hpp:155
virtual void NotifyLeaveMap()
Let the entity know that it is about to be removed from the map.
Definition: BaseEntity.hpp:76
static const cf::TypeSys::TypeInfoT TypeInfo
The type info object for (objects/instances of) this class.
Definition: BaseEntity.hpp:135
void PostEvent(unsigned int EventType)
This SERVER-SIDE function is used for posting an event of the given type.
Definition: BaseEntity.hpp:113
virtual void GetBodyOrientation(unsigned short &h, unsigned short &p, unsigned short &b) const
Returns the orientation angles of the entity itself.
Definition: BaseEntity.hpp:78
Definition: TypeSys.hpp:67
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:94