Cafu Engine
World.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_GAMESYS_WORLD_HPP_INCLUDED
8 #define CAFU_GAMESYS_WORLD_HPP_INCLUDED
9 
10 #include "UniScriptState.hpp"
11 
12 #include <stdexcept>
13 
14 
15 namespace cf { namespace ClipSys { class ClipWorldT; } }
16 namespace cf { namespace ClipSys { class CollModelManI; } }
17 namespace cf { namespace GuiSys { class GuiResourcesT; } }
18 class ModelManagerT;
19 class PhysicsWorldT;
20 struct CaKeyboardEventT;
21 struct CaMouseEventT;
22 
23 
24 namespace cf
25 {
26  namespace GameSys
27  {
28  /// The TypeInfoTs of all WorldT-derived classes must register with this TypeInfoManT instance.
29  cf::TypeSys::TypeInfoManT& GetWorldTIM();
30 
31 
32  class EntityT;
33 
34 
35  /// This class holds the hierarchy of game entities that populate a game world.
36  /// The root of the hierarchy is the map entity, all other entities are direct or indirect children of it.
37  /// The world also holds shared resources that all entities commonly use, such as the script state and the
38  /// model manager.
39  class WorldT : public RefCountedT
40  {
41  public:
42 
43  class InitErrorT;
44 
45  /// A value that indicates where and to which purpose a game world is instantiated.
46  /// The details of a world sometimes depend on its realm:
47  /// - prefabs can only be loaded into worlds in the Map Editor,
48  /// - client interpolations only need to be accounted for on the clients,
49  /// - the human player's "think" code sometimes must know whether it is running
50  /// on the server or in a prediction step on the client.
51  enum RealmT
52  {
53  RealmServer,
54  RealmClient,
55  RealmMapEditor,
56  RealmOther
57  };
58 
59  /// Flags for initializing a world from a map script.
61  {
62  InitFlag_InlineCode = 1, ///< Normally, the `ScriptName` parameter to the WorldT ctor is a filename. If this is set, it is treated as inline script code.
63  InitFlag_OnlyStatic = 2, ///< If set, only the static data will be loaded. User-defined scripts with custom, initial behaviour are *not* run.
64  InitFlag_AsPrefab = 4 ///< This must be set if the map script is loaded for use as a prefab. Can only be used if the world is in realm `RealmMapEditor`.
65  };
66 
67  /// Initializes the given script state for use with WorldT instances.
68  static void InitScriptState(UniScriptStateT& ScriptState);
69 
70  /// Assigns the given world to the global "world" and loads the given script in order to initialize it.
71  /// @param World The world to init.
72  /// @param ScriptName The file name of the script to load.
73  /// @param Flags A combination of the flags in InitFlagsT.
74  /// @throws Throws an InitErrorT object on problems loading the script.
75  /// @returns The root entity as loaded from the given script.
76  static IntrusivePtrT<EntityT> LoadScript(IntrusivePtrT<WorldT> World, const std::string& ScriptName, int Flags = 0);
77 
78 
79  /// Constructor for creating an entity hierarchy (== "a world") from the given script file.
80  /// @param Realm The realm of this world, indicating where and to which purpose the world is instantiated.
81  /// @param ScriptState The caller will use this world with this script state (binds the world to it).
82  /// @param ModelMan The manager for all models that are used in this world.
83  /// @param GuiRes The provider for resources (fonts and models) for all GUIs in this world.
84  /// @param CollModelMan The manager for all collision models that are used in this world.
85  /// @param ClipWorld The clip world, where entities can register their collision models and run collision detection queries. Can be `NULL`, e.g. in CaWE or the map compile tools.
86  /// @param PhysicsWorld The physics world, where entities can register their rigid bodies and run collision detection queries. Can be `NULL`, e.g. in CaWE or the map compile tools.
87  WorldT(RealmT Realm, UniScriptStateT& ScriptState, ModelManagerT& ModelMan, cf::GuiSys::GuiResourcesT& GuiRes,
88  cf::ClipSys::CollModelManI& CollModelMan, cf::ClipSys::ClipWorldT* ClipWorld, PhysicsWorldT* PhysicsWorld);
89 
90  /// Returns the realm of this world, indicating where and to which purpose the
91  /// world has been instantiated.
92  RealmT GetRealm() const { return m_Realm; }
93 
94  /// Returns the script state of this world.
95  UniScriptStateT& GetScriptState() { return m_ScriptState; }
96 
97  /// Returns how many millimeters one world unit is large.
98  /// Used whenever we have to deal with concrete units of measurement such as millimeters or meters
99  /// (e.g. for physics computations, radiometric units (e.g. W/m^2), the acoustic Doppler effect, etc.).
100  /// FIXME: The same constant is currently also defined (as `const double METERS_PER_WORLD_UNIT = 0.0254`)
101  /// in CollisionModelStaticT::BulletAdapterT, PhysicsWorldT::TraceBoundingBox(), and the Sound Systems.
102  float GetMillimetersPerWorldUnit() const { return 25.4f; }
103 
104  /// Returns the root entity of this world.
105  IntrusivePtrT<EntityT> GetRootEntity() const { return m_RootEntity; }
106 
107  /// Returns the ID that the next newly created entity should get.
108  unsigned int GetNextEntityID(unsigned int ForcedID = UINT_MAX);
109 
110  /// Returns the manager for all models that are used in this world.
111  ModelManagerT& GetModelMan() const { return m_ModelMan; }
112 
113  /// Returns the resource provider for commonly used GUI fonts and models.
114  /// All GUIs that are created in this world share their font and model resources via the returned GuiResourcesT instance.
115  cf::GuiSys::GuiResourcesT& GetGuiResources() const { return m_GuiResources; }
116 
117  /// Returns the manager for all collision models that are used in this world.
118  cf::ClipSys::CollModelManI& GetCollModelMan() const { return m_CollModelMan; }
119 
120  /// The clip world, where entities can register their collision models and run collision detection queries.
121  /// Can be `NULL`, e.g. in CaWE or the map compile tools.
122  cf::ClipSys::ClipWorldT* GetClipWorld() const { return m_ClipWorld; }
123 
124  /// The physics world, where entities can register their rigid bodies and run collision detection queries.
125  /// Can be `NULL`, e.g. in CaWE or the map compile tools.
126  PhysicsWorldT* GetPhysicsWorld() const { return m_PhysicsWorld; }
127 
128  /// Renders this world.
129  /// Note that this method does *not* setup any of the MatSys's model, view or projection matrices:
130  /// it's up to the caller to do that.
131  void Render() const;
132 
133  /// Processes a keyboard event by forwarding it to the entity that currently has the input focus.
134  /// @param KE The keyboard event to process.
135  /// @returns `true` if the device has been successfully processed, `false` otherwise.
136  bool ProcessDeviceEvent(const CaKeyboardEventT& KE);
137 
138  /// Processes a mouse event by forwarding it to the entity that currently has the input focus.
139  /// @param ME The mouse event to process.
140  /// @returns `true` if the device has been successfully processed, `false` otherwise.
141  bool ProcessDeviceEvent(const CaMouseEventT& ME);
142 
143  // /// Advances the world one frame (one "clock-tick") on the server.
144  // /// It typically updates all game-relevant state that is sync'ed over the network to all
145  // /// connected game clients.
146  // /// EntityT::OnServerFrame() is called by this method for each entity in this world.
147  // ///
148  // /// @param t The time in seconds since the last server frame.
149  // void OnServerFrame(float t);
150 
151  /// Advances the entity one frame (one "clock-tick") on the client.
152  /// It typically updates eye-candy that is *not* sync'ed over the network.
153  /// EntityT::OnClientFrame() is called by this method for each entity in this world.
154  ///
155  /// @param t The time in seconds since the last client frame.
156  void OnClientFrame(float t);
157 
158 
159  // The TypeSys related declarations for this class.
160  /*virtual*/ const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
161  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
162  static const cf::TypeSys::TypeInfoT TypeInfo;
163 
164 
165  private:
166 
167  WorldT(const WorldT&); ///< Use of the Copy Constructor is not allowed.
168  void operator = (const WorldT&); ///< Use of the Assignment Operator is not allowed.
169 
170  void Init(); ///< Calls the OnInit() script methods of all entities.
171 
172 
173  const RealmT m_Realm; ///< The realm of this world, indicating where and to which purpose the world has been instantiated.
174  UniScriptStateT& m_ScriptState; ///< The script state that this world is bound to.
175  IntrusivePtrT<EntityT> m_RootEntity; ///< The root of the entity hierarchy that forms this world.
176  unsigned int m_NextEntID; ///< The ID that the next newly created entity should get.
177  ModelManagerT& m_ModelMan; ///< The manager for all models that are used in this world.
178  cf::GuiSys::GuiResourcesT& m_GuiResources; ///< The provider for resources (fonts and models) for all GUIs in this world.
179  cf::ClipSys::CollModelManI& m_CollModelMan; ///< The manager for all collision models that are used in this world.
180  cf::ClipSys::ClipWorldT* m_ClipWorld; ///< The clip world, where entities can register their collision models and run collision detection queries. Can be `NULL`, e.g. in CaWE or the map compile tools.
181  PhysicsWorldT* m_PhysicsWorld; ///< The physics world, where entities can register their rigid bodies and run collision detection queries. Can be `NULL`, e.g. in CaWE or the map compile tools.
182 
183 
184  // Methods called from Lua scripts on cf::GameSys::WorldT instances.
185  static int CreateNew(lua_State* LuaState); ///< Creates and returns a new entity or component.
186  static int GetRootEntity(lua_State* LuaState); ///< Returns the root entity of this world.
187  static int SetRootEntity(lua_State* LuaState); ///< Sets the root entity for this world.
188  static int TraceRay(lua_State* LuaState); ///< Employs m_ClipWorld->TraceRay() to trace a ray through the (clip) world.
189  static int Phys_TraceBB(lua_State* LuaState); ///< Employs m_PhysicsWorld->TraceBoundingBox() to trace a bounding-box through the (physics) world.
190  static int toString(lua_State* LuaState); ///< Returns a short string description of this world.
191 
192  static const luaL_Reg MethodsList[]; ///< List of methods registered with Lua.
193  static const char* DocClass;
194  static const cf::TypeSys::MethsDocT DocMethods[];
195  };
196  }
197 }
198 
199 
200 /// A class that is thrown on WorldT initialization errors.
201 class cf::GameSys::WorldT::InitErrorT : public std::runtime_error
202 {
203  public:
204 
205  InitErrorT(const std::string& Message);
206 };
207 
208 #endif
A class that is thrown on WorldT initialization errors.
Definition: World.hpp:201
Definition: PhysicsWorld.hpp:158
float GetMillimetersPerWorldUnit() const
Returns how many millimeters one world unit is large.
Definition: World.hpp:102
This class implements smart (reference-counted) pointers.
Definition: Pointer.hpp:43
cf::ClipSys::CollModelManI & GetCollModelMan() const
Returns the manager for all collision models that are used in this world.
Definition: World.hpp:118
This class provides (an interface to) the creation, management and destruction of collision models...
Definition: CollisionModelMan.hpp:38
ModelManagerT & GetModelMan() const
Returns the manager for all models that are used in this world.
Definition: World.hpp:111
Normally, the ScriptName parameter to the WorldT ctor is a filename. If this is set, it is treated as inline script code.
Definition: World.hpp:62
RealmT GetRealm() const
Returns the realm of this world, indicating where and to which purpose the world has been instantiate...
Definition: World.hpp:92
unsigned int GetNextEntityID(unsigned int ForcedID=UINT_MAX)
Returns the ID that the next newly created entity should get.
Definition: World.cpp:208
bool ProcessDeviceEvent(const CaKeyboardEventT &KE)
Processes a keyboard event by forwarding it to the entity that currently has the input focus...
Definition: World.cpp:236
cf::GuiSys::GuiResourcesT & GetGuiResources() const
Returns the resource provider for commonly used GUI fonts and models.
Definition: World.hpp:115
This struct describes a mouse event.
Definition: OpenGLWindow.hpp:185
static IntrusivePtrT< EntityT > LoadScript(IntrusivePtrT< WorldT > World, const std::string &ScriptName, int Flags=0)
Assigns the given world to the global "world" and loads the given script in order to initialize it...
Definition: World.cpp:68
This must be set if the map script is loaded for use as a prefab. Can only be used if the world is in...
Definition: World.hpp:64
PhysicsWorldT * GetPhysicsWorld() const
The physics world, where entities can register their rigid bodies and run collision detection queries...
Definition: World.hpp:126
static void InitScriptState(UniScriptStateT &ScriptState)
Initializes the given script state for use with WorldT instances.
Definition: World.cpp:47
UniScriptStateT & GetScriptState()
Returns the script state of this world.
Definition: World.hpp:95
void Render() const
Renders this world.
Definition: World.cpp:225
Definition: World.hpp:85
If set, only the static data will be loaded. User-defined scripts with custom, initial behaviour are ...
Definition: World.hpp:63
This class manages the type infos.
Definition: TypeSys.hpp:145
void OnClientFrame(float t)
Advances the entity one frame (one "clock-tick") on the client.
Definition: World.cpp:256
This struct describes a keyboard event.
Definition: OpenGLWindow.hpp:20
IntrusivePtrT< EntityT > GetRootEntity() const
Returns the root entity of this world.
Definition: World.hpp:105
This class holds the hierarchy of game entities that populate a game world.
Definition: World.hpp:39
This class is used for managing model instances.
Definition: ModelManager.hpp:31
The clip world manages all the clip models that exist in a world (their "union"). ...
Definition: ClipWorld.hpp:27
cf::ClipSys::ClipWorldT * GetClipWorld() const
The clip world, where entities can register their collision models and run collision detection querie...
Definition: World.hpp:122
This class represents the state of a script: the underlying Lua state, pending coroutines, metatables for C++ class hierarchies, etc.
Definition: UniScriptState.hpp:214
InitFlagsT
Flags for initializing a world from a map script.
Definition: World.hpp:60
WorldT(RealmT Realm, UniScriptStateT &ScriptState, ModelManagerT &ModelMan, cf::GuiSys::GuiResourcesT &GuiRes, cf::ClipSys::CollModelManI &CollModelMan, cf::ClipSys::ClipWorldT *ClipWorld, PhysicsWorldT *PhysicsWorld)
Constructor for creating an entity hierarchy (== "a world") from the given script file...
Definition: World.cpp:193
RealmT
A value that indicates where and to which purpose a game world is instantiated.
Definition: World.hpp:51
Definition: TypeSys.hpp:52
Definition: TypeSys.hpp:57
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
A base class for objects that are reference-counted with IntrusivePtrTs.
Definition: Pointer.hpp:13
This class manages and provides resources (fonts and models) for GuiImplT instances.
Definition: GuiResources.hpp:26