| 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-2012 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_GAME_GAMEINTERFACE_HPP_INCLUDED |
|---|
| 23 | #define CAFU_GAME_GAMEINTERFACE_HPP_INCLUDED |
|---|
| 24 | |
|---|
| 25 | #include <map> |
|---|
| 26 | #include <string> |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | // Forward declarations. |
|---|
| 30 | class BaseEntityT; |
|---|
| 31 | template<class T> class Vector3T; |
|---|
| 32 | class ModelManagerT; |
|---|
| 33 | namespace cf { namespace ClipSys { class CollisionModelT; } } |
|---|
| 34 | namespace cf { namespace GameSys { class GameWorldI; } } |
|---|
| 35 | namespace cf { namespace SceneGraph { class GenericNodeT; } } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | namespace cf |
|---|
| 39 | { |
|---|
| 40 | namespace GameSys |
|---|
| 41 | { |
|---|
| 42 | /// The game interface, specified as an ABC so that is can be used without linked (module-local) implementation. |
|---|
| 43 | /// This interface is implemented by the game DLL, and thus defines the essence of a game/MOD. |
|---|
| 44 | /// It is used (only) by the engine to run the game. |
|---|
| 45 | class GameI |
|---|
| 46 | { |
|---|
| 47 | public: |
|---|
| 48 | |
|---|
| 49 | /// Called to initialize the game. |
|---|
| 50 | /// This function is called exactly once as the very first function after the game has been loaded, |
|---|
| 51 | /// not each time a new world is about to be loaded. |
|---|
| 52 | /// @param AsClient Tells whether we're running as client. |
|---|
| 53 | /// @param AsServer Tells whether we're running as server. |
|---|
| 54 | /// @param ModelMan The manager for all models that are used in this game. |
|---|
| 55 | virtual void Initialize(bool AsClient, bool AsServer, ModelManagerT& ModelMan)=0; |
|---|
| 56 | |
|---|
| 57 | /// Called to shutdown the game. |
|---|
| 58 | /// This function is called exactly once as the very last function before the game is released, |
|---|
| 59 | /// not each time a world is being freed. |
|---|
| 60 | virtual void Release()=0; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /// Called after the server loaded a world but before any calls to CreateBaseEntityFromMapFile(), so that the game can init a new script state. |
|---|
| 64 | virtual void Sv_PrepareNewWorld(const char* WorldFileName, const cf::ClipSys::CollisionModelT* WorldCollMdl)=0; |
|---|
| 65 | |
|---|
| 66 | /// Called when the server finished calling CreateBaseEntityFromMapFile() for all entities in the world file. |
|---|
| 67 | /// The game can now load/insert the user provided map script (e.g. "TechDemo.lua") to the script state. |
|---|
| 68 | virtual void Sv_FinishNewWorld(const char* WorldFileName)=0; |
|---|
| 69 | |
|---|
| 70 | /// Called by the server when it begins thinking, i.e. before it calls the Think() method of all the entities. |
|---|
| 71 | /// @param FrameTime The time this frame is worth, in seconds. |
|---|
| 72 | virtual void Sv_BeginThinking(float FrameTime)=0; |
|---|
| 73 | |
|---|
| 74 | /// Called by the server when it finished thinking, i.e. after it called the Think() method of all the entities. |
|---|
| 75 | virtual void Sv_EndThinking()=0; |
|---|
| 76 | |
|---|
| 77 | /// Called before the server unloads the world but after it freed all its base entities, so that the game can destroy the script state. |
|---|
| 78 | virtual void Sv_UnloadWorld()=0; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /// Called when the client loads a new world, before any calls to CreateBaseEntityFromTypeNr(). |
|---|
| 82 | virtual void Cl_LoadWorld(const char* WorldFileName, const cf::ClipSys::CollisionModelT* WorldCollMdl)=0; |
|---|
| 83 | |
|---|
| 84 | /// Called when the client unloads the world but after it freed all its base entities. |
|---|
| 85 | virtual void Cl_UnloadWorld()=0; |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | /// Creates a new entity of the given type (entity class name is given by the Properties parameter). |
|---|
| 89 | /// @param Properties The properties dictionary in the map file of this entity (empty if the entity is created "dynamically"). Contains especially the "classname" key, whose value has the entity class name for which an instance is to be created. |
|---|
| 90 | /// @param RootNode The root node of the scene graph of this entity as defined in the map file. NULL if the entity is created "dynamically". |
|---|
| 91 | /// @param CollisionModel The collision model of this entity as defined by map primitives. NULL if no collision model was defined by map primitives (the entity may still have a collision model based on the Properties). |
|---|
| 92 | /// @param ID The global unique ID of this entity (in the current map). |
|---|
| 93 | /// @param WorldFileIndex The index number of the entity in the world file. |
|---|
| 94 | /// @param MapFileIndex The index number of the entity in the map file, for obtaining the information in the map file about it later. |
|---|
| 95 | /// @param GameWorld Pointer to the game world implementation. |
|---|
| 96 | /// @param Origin Where the new entity is supposed to be instantiated. |
|---|
| 97 | /// @returns a base class pointer to the newly created entity instance. |
|---|
| 98 | virtual BaseEntityT* CreateBaseEntityFromMapFile(const std::map<std::string, std::string>& Properties, |
|---|
| 99 | const cf::SceneGraph::GenericNodeT* RootNode, const cf::ClipSys::CollisionModelT* CollisionModel, unsigned long ID, |
|---|
| 100 | unsigned long WorldFileIndex, unsigned long MapFileIndex, cf::GameSys::GameWorldI* GameWorld, const Vector3T<double>& Origin)=0; |
|---|
| 101 | |
|---|
| 102 | /// Creates a new entity of the given type (specified by TypeNr), and returns a pointer to the base class. |
|---|
| 103 | /// This method is used by the client after it received a related network message (which also contains the TypeNr). |
|---|
| 104 | /// @param TypeNr The (number of the) type of the entity to be created. |
|---|
| 105 | /// @param Properties The properties dictionary in the map file of this entity (empty if the entity is created "dynamically"). |
|---|
| 106 | /// @param RootNode The root node of the scene graph of this entity as defined in the map file. NULL if the entity is created "dynamically". |
|---|
| 107 | /// @param CollisionModel The collision model of this entity as defined by map primitives. NULL if no collision model was defined by map primitives (the entity may still have a collision model based on the Properties). |
|---|
| 108 | /// @param ID The global unique ID of this entity (in the current map). |
|---|
| 109 | /// @param WorldFileIndex The index number of the entity in the world file. |
|---|
| 110 | /// @param MapFileIndex The index number of the entity in the map file, for obtaining the information in the map file about it later. |
|---|
| 111 | /// @param GameWorld Pointer to the game world implementation. |
|---|
| 112 | /// @returns a base class pointer to the newly created entity instance. |
|---|
| 113 | virtual BaseEntityT* CreateBaseEntityFromTypeNr(unsigned long TypeNr, const std::map<std::string, std::string>& Properties, |
|---|
| 114 | const cf::SceneGraph::GenericNodeT* RootNode, const cf::ClipSys::CollisionModelT* CollisionModel, unsigned long ID, |
|---|
| 115 | unsigned long WorldFileIndex, unsigned long MapFileIndex, cf::GameSys::GameWorldI* GameWorld)=0; |
|---|
| 116 | |
|---|
| 117 | /// Called to delete the given base entity. |
|---|
| 118 | /// Note that the caller (the engine) cannot call "delete BaseEntity;" directly, because of the "exe/dll boundary". |
|---|
| 119 | /// @param BaseEntity Pointer to the entity instance to be freed. |
|---|
| 120 | virtual void FreeBaseEntity(BaseEntityT* BaseEntity)=0; |
|---|
| 121 | |
|---|
| 122 | /// The virtual destructor, so that derived classes can safely be deleted via a GameI (base class) pointer. |
|---|
| 123 | /// However, with GameIs that's never supposed to happen, so this destructor only exists to silence the g++ compiler warning. |
|---|
| 124 | virtual ~GameI() { } |
|---|
| 125 | }; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | /// A global pointer to an implementation of the GameI interface. |
|---|
| 129 | /// Each module (exe or dll) that uses this pointer must somewhere provide exactly one definition for it (none is provided by the GameSys library). |
|---|
| 130 | /// For more details, see the analogous comment in Libs/FileSys/FileMan.hpp, but note the with the GameSys, the roles of the exe and dll are reversed |
|---|
| 131 | /// (not the exe provides an interface for use by the dll, but the dll provides an interface for use by the exe). |
|---|
| 132 | extern GameI* Game; |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | #endif |
|---|