1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __GAME_H__
30 #define __GAME_H__
31 
32 #include "idlib/BitMsg.h"
33 #include "idlib/Dict.h"
34 #include "framework/UsercmdGen.h"
35 #include "renderer/RenderWorld.h"
36 #include "sound/sound.h"
37 
38 class idAASFileManager;
39 class idCollisionModelManager;
40 class idRenderSystem;
41 class idRenderModelManager;
42 class idUserInterface;
43 class idUserInterfaceManager;
44 class idNetworkSystem;
45 
46 /*
47 ===============================================================================
48 
49 	Public game interface with methods to run the game.
50 
51 ===============================================================================
52 */
53 
54 typedef struct {
55 	char		sessionCommand[MAX_STRING_CHARS];	// "map", "disconnect", "victory", etc
56 	int			consistencyHash;					// used to check for network game divergence
57 	int			health;
58 	int			heartRate;
59 	int			stamina;
60 	int			combat;
61 	bool		syncNextGameFrame;					// used when cinematics are skipped to prevent session from simulating several game frames to
62 													// keep the game time in sync with real time
63 } gameReturn_t;
64 
65 typedef enum {
66 	ALLOW_YES = 0,
67 	ALLOW_BADPASS,	// core will prompt for password and connect again
68 	ALLOW_NOTYET,	// core will wait with transmitted message
69 	ALLOW_NO		// core will abort with transmitted message
70 } allowReply_t;
71 
72 typedef enum {
73 	ESC_IGNORE = 0,	// do nothing
74 	ESC_MAIN,		// start main menu GUI
75 	ESC_GUI			// set an explicit GUI
76 } escReply_t;
77 
78 class idGame {
79 public:
~idGame()80 	virtual						~idGame() {}
81 
82 	// Initialize the game for the first time.
83 	virtual void				Init( void ) = 0;
84 
85 	// Shut down the entire game.
86 	virtual void				Shutdown( void ) = 0;
87 
88 	// Set the local client number. Distinguishes listen ( == 0 ) / dedicated ( == -1 )
89 	virtual void				SetLocalClient( int clientNum ) = 0;
90 
91 	// Sets the user info for a client.
92 	// if canModify is true, the game can modify the user info in the returned dictionary pointer, server will forward the change back
93 	// canModify is never true on network client
94 	virtual const idDict *		SetUserInfo( int clientNum, const idDict &userInfo, bool isClient, bool canModify ) = 0;
95 
96 	// Retrieve the game's userInfo dict for a client.
97 	virtual const idDict *		GetUserInfo( int clientNum ) = 0;
98 
99 	// The game gets a chance to alter userinfo before they are emitted to server.
100 	virtual void				ThrottleUserInfo( void ) = 0;
101 
102 	// Sets the serverinfo at map loads and when it changes.
103 	virtual void				SetServerInfo( const idDict &serverInfo ) = 0;
104 
105 	// The session calls this before moving the single player game to a new level.
106 	virtual const idDict &		GetPersistentPlayerInfo( int clientNum ) = 0;
107 
108 	// The session calls this right before a new level is loaded.
109 	virtual void				SetPersistentPlayerInfo( int clientNum, const idDict &playerInfo ) = 0;
110 
111 	// Loads a map and spawns all the entities.
112 	virtual void				InitFromNewMap( const char *mapName, idRenderWorld *renderWorld, idSoundWorld *soundWorld, bool isServer, bool isClient, int randseed ) = 0;
113 
114 	// Loads a map from a savegame file.
115 	virtual bool				InitFromSaveGame( const char *mapName, idRenderWorld *renderWorld, idSoundWorld *soundWorld, idFile *saveGameFile ) = 0;
116 
117 	// Saves the current game state, the session may have written some data to the file already.
118 	virtual void				SaveGame( idFile *saveGameFile ) = 0;
119 
120 	// Shut down the current map.
121 	virtual void				MapShutdown( void ) = 0;
122 
123 	// Caches media referenced from in key/value pairs in the given dictionary.
124 	virtual void				CacheDictionaryMedia( const idDict *dict ) = 0;
125 
126 	// Spawns the player entity to be used by the client.
127 	virtual void				SpawnPlayer( int clientNum ) = 0;
128 
129 	// Runs a game frame, may return a session command for level changing, etc
130 	virtual gameReturn_t		RunFrame( const usercmd_t *clientCmds ) = 0;
131 
132 	// Makes rendering and sound system calls to display for a given clientNum.
133 	virtual bool				Draw( int clientNum ) = 0;
134 
135 	// Let the game do it's own UI when ESCAPE is used
136 	virtual escReply_t			HandleESC( idUserInterface **gui ) = 0;
137 
138 	// get the games menu if appropriate ( multiplayer )
139 	virtual idUserInterface *	StartMenu() = 0;
140 
141 	// When the game is running it's own UI fullscreen, GUI commands are passed through here
142 	// return NULL once the fullscreen UI mode should stop, or "main" to go to main menu
143 	virtual const char *		HandleGuiCommands( const char *menuCommand ) = 0;
144 
145 	// main menu commands not caught in the engine are passed here
146 	virtual void				HandleMainMenuCommands( const char *menuCommand, idUserInterface *gui ) = 0;
147 
148 	// Early check to deny connect.
149 	virtual allowReply_t		ServerAllowClient( int numClients, const char *IP, const char *guid, const char *password, char reason[MAX_STRING_CHARS] ) = 0;
150 
151 	// Connects a client.
152 	virtual void				ServerClientConnect( int clientNum, const char *guid ) = 0;
153 
154 	// Spawns the player entity to be used by the client.
155 	virtual void				ServerClientBegin( int clientNum ) = 0;
156 
157 	// Disconnects a client and removes the player entity from the game.
158 	virtual void				ServerClientDisconnect( int clientNum ) = 0;
159 
160 	// Writes initial reliable messages a client needs to recieve when first joining the game.
161 	virtual void				ServerWriteInitialReliableMessages( int clientNum ) = 0;
162 
163 	// Writes a snapshot of the server game state for the given client.
164 	virtual void				ServerWriteSnapshot( int clientNum, int sequence, idBitMsg &msg, byte *clientInPVS, int numPVSClients ) = 0;
165 
166 	// Patches the network entity states at the server with a snapshot for the given client.
167 	virtual bool				ServerApplySnapshot( int clientNum, int sequence ) = 0;
168 
169 	// Processes a reliable message from a client.
170 	virtual void				ServerProcessReliableMessage( int clientNum, const idBitMsg &msg ) = 0;
171 
172 	// Reads a snapshot and updates the client game state.
173 	virtual void				ClientReadSnapshot( int clientNum, int sequence, const int gameFrame, const int gameTime, const int dupeUsercmds, const int aheadOfServer, const idBitMsg &msg ) = 0;
174 
175 	// Patches the network entity states at the client with a snapshot.
176 	virtual bool				ClientApplySnapshot( int clientNum, int sequence ) = 0;
177 
178 	// Processes a reliable message from the server.
179 	virtual void				ClientProcessReliableMessage( int clientNum, const idBitMsg &msg ) = 0;
180 
181 	// Runs prediction on entities at the client.
182 	virtual gameReturn_t		ClientPrediction( int clientNum, const usercmd_t *clientCmds, bool lastPredictFrame ) = 0;
183 
184 	// Used to manage divergent time-lines
185 	virtual void				SelectTimeGroup( int timeGroup ) = 0;
186 	virtual int					GetTimeGroupTime( int timeGroup ) = 0;
187 
188 	virtual void				GetBestGameType( const char* map, const char* gametype, char buf[ MAX_STRING_CHARS ] ) = 0;
189 
190 	// Returns a summary of stats for a given client
191 	virtual void				GetClientStats( int clientNum, char *data, const int len ) = 0;
192 
193 	// Switch a player to a particular team
194 	virtual void				SwitchTeam( int clientNum, int team ) = 0;
195 
196 	virtual bool				DownloadRequest( const char *IP, const char *guid, const char *paks, char urls[ MAX_STRING_CHARS ] ) = 0;
197 
198 	virtual void				GetMapLoadingGUI( char gui[ MAX_STRING_CHARS ] ) = 0;
199 };
200 
201 extern idGame *					game;
202 
203 
204 /*
205 ===============================================================================
206 
207 	Public game interface with methods for in-game editing.
208 
209 ===============================================================================
210 */
211 
212 typedef struct {
213 	idSoundEmitter *			referenceSound;	// this is the interface to the sound system, created
214 												// with idSoundWorld::AllocSoundEmitter() when needed
215 	idVec3						origin;
216 	int							listenerId;		// SSF_PRIVATE_SOUND only plays if == listenerId from PlaceListener
217 												// no spatialization will be performed if == listenerID
218 	const idSoundShader *		shader;			// this really shouldn't be here, it is a holdover from single channel behavior
219 	float						diversity;		// 0.0 to 1.0 value used to select which
220 												// samples in a multi-sample list from the shader are used
221 	bool						waitfortrigger;	// don't start it at spawn time
222 	soundShaderParms_t			parms;			// override volume, flags, etc
223 } refSound_t;
224 
225 enum {
226 	TEST_PARTICLE_MODEL = 0,
227 	TEST_PARTICLE_IMPACT,
228 	TEST_PARTICLE_MUZZLE,
229 	TEST_PARTICLE_FLIGHT,
230 	TEST_PARTICLE_SELECTED
231 };
232 
233 class idEntity;
234 class idMD5Anim;
235 
236 // FIXME: this interface needs to be reworked but it properly separates code for the time being
237 class idGameEdit {
238 public:
~idGameEdit(void)239 	virtual						~idGameEdit( void ) {}
240 
241 	// These are the canonical idDict to parameter parsing routines used by both the game and tools.
242 	virtual void				ParseSpawnArgsToRenderLight( const idDict *args, renderLight_t *renderLight );
243 	virtual void				ParseSpawnArgsToRenderEntity( const idDict *args, renderEntity_t *renderEntity );
244 	virtual void				ParseSpawnArgsToRefSound( const idDict *args, refSound_t *refSound );
245 
246 	// Animation system calls for non-game based skeletal rendering.
247 	virtual idRenderModel *		ANIM_GetModelFromEntityDef( const char *classname );
248 	virtual const idVec3		&ANIM_GetModelOffsetFromEntityDef( const char *classname );
249 	virtual idRenderModel *		ANIM_GetModelFromEntityDef( const idDict *args );
250 	virtual idRenderModel *		ANIM_GetModelFromName( const char *modelName );
251 	virtual const idMD5Anim *	ANIM_GetAnimFromEntityDef( const char *classname, const char *animname );
252 	virtual int					ANIM_GetNumAnimsFromEntityDef( const idDict *args );
253 	virtual const char *		ANIM_GetAnimNameFromEntityDef( const idDict *args, int animNum );
254 	virtual const idMD5Anim *	ANIM_GetAnim( const char *fileName );
255 	virtual int					ANIM_GetLength( const idMD5Anim *anim );
256 	virtual int					ANIM_GetNumFrames( const idMD5Anim *anim );
257 	virtual void				ANIM_CreateAnimFrame( const idRenderModel *model, const idMD5Anim *anim, int numJoints, idJointMat *frame, int time, const idVec3 &offset, bool remove_origin_offset );
258 	virtual idRenderModel *		ANIM_CreateMeshForAnim( idRenderModel *model, const char *classname, const char *animname, int frame, bool remove_origin_offset );
259 
260 	// Articulated Figure calls for AF editor and Radiant.
261 	virtual bool				AF_SpawnEntity( const char *fileName );
262 	virtual void				AF_UpdateEntities( const char *fileName );
263 	virtual void				AF_UndoChanges( void );
264 	virtual idRenderModel *		AF_CreateMesh( const idDict &args, idVec3 &meshOrigin, idMat3 &meshAxis, bool &poseIsSet );
265 
266 
267 	// Entity selection.
268 	virtual void				ClearEntitySelection( void );
269 	virtual int					GetSelectedEntities( idEntity *list[], int max );
270 	virtual void				AddSelectedEntity( idEntity *ent );
271 
272 	// Selection methods
273 	virtual void				TriggerSelected();
274 
275 	// Entity defs and spawning.
276 	virtual const idDict *		FindEntityDefDict( const char *name, bool makeDefault = true ) const;
277 	virtual void				SpawnEntityDef( const idDict &args, idEntity **ent );
278 	virtual idEntity *			FindEntity( const char *name ) const;
279 	virtual const char *		GetUniqueEntityName( const char *classname ) const;
280 
281 	// Entity methods.
282 	virtual void				EntityGetOrigin( idEntity *ent, idVec3 &org ) const;
283 	virtual void				EntityGetAxis( idEntity *ent, idMat3 &axis ) const;
284 	virtual void				EntitySetOrigin( idEntity *ent, const idVec3 &org );
285 	virtual void				EntitySetAxis( idEntity *ent, const idMat3 &axis );
286 	virtual void				EntityTranslate( idEntity *ent, const idVec3 &org );
287 	virtual const idDict *		EntityGetSpawnArgs( idEntity *ent ) const;
288 	virtual void				EntityUpdateChangeableSpawnArgs( idEntity *ent, const idDict *dict );
289 	virtual void				EntityChangeSpawnArgs( idEntity *ent, const idDict *newArgs );
290 	virtual void				EntityUpdateVisuals( idEntity *ent );
291 	virtual void				EntitySetModel( idEntity *ent, const char *val );
292 	virtual void				EntityStopSound( idEntity *ent );
293 	virtual void				EntityDelete( idEntity *ent );
294 	virtual void				EntitySetColor( idEntity *ent, const idVec3 color );
295 
296 	// Player methods.
297 	virtual bool				PlayerIsValid() const;
298 	virtual void				PlayerGetOrigin( idVec3 &org ) const;
299 	virtual void				PlayerGetAxis( idMat3 &axis ) const;
300 	virtual void				PlayerGetViewAngles( idAngles &angles ) const;
301 	virtual void				PlayerGetEyePosition( idVec3 &org ) const;
302 
303 	// In game map editing support.
304 	virtual const idDict *		MapGetEntityDict( const char *name ) const;
305 	virtual void				MapSave( const char *path = NULL ) const;
306 	virtual void				MapSetEntityKeyVal( const char *name, const char *key, const char *val ) const ;
307 	virtual void				MapCopyDictToEntity( const char *name, const idDict *dict ) const;
308 	virtual int					MapGetUniqueMatchingKeyVals( const char *key, const char *list[], const int max ) const;
309 	virtual void				MapAddEntity( const idDict *dict ) const;
310 	virtual int					MapGetEntitiesMatchingClassWithString( const char *classname, const char *match, const char *list[], const int max ) const;
311 	virtual void				MapRemoveEntity( const char *name ) const;
312 	virtual void				MapEntityTranslate( const char *name, const idVec3 &v ) const;
313 
314 };
315 
316 extern idGameEdit *				gameEdit;
317 
318 
319 /*
320 ===============================================================================
321 
322 	Game API.
323 
324 ===============================================================================
325 */
326 
327 const int GAME_API_VERSION		= 9;
328 
329 typedef struct {
330 
331 	int							version;				// API version
332 	idSys *						sys;					// non-portable system services
333 	idCommon *					common;					// common
334 	idCmdSystem *				cmdSystem;				// console command system
335 	idCVarSystem *				cvarSystem;				// console variable system
336 	idFileSystem *				fileSystem;				// file system
337 	idNetworkSystem *			networkSystem;			// network system
338 	idRenderSystem *			renderSystem;			// render system
339 	idSoundSystem *				soundSystem;			// sound system
340 	idRenderModelManager *		renderModelManager;		// render model manager
341 	idUserInterfaceManager *	uiManager;				// user interface manager
342 	idDeclManager *				declManager;			// declaration manager
343 	idAASFileManager *			AASFileManager;			// AAS file manager
344 	idCollisionModelManager *	collisionModelManager;	// collision model manager
345 
346 } gameImport_t;
347 
348 typedef struct {
349 
350 	int							version;				// API version
351 	idGame *					game;					// interface to run the game
352 	idGameEdit *				gameEdit;				// interface for in-game editing
353 
354 } gameExport_t;
355 
356 extern "C" {
357 typedef gameExport_t * (*GetGameAPI_t)( gameImport_t *import );
358 }
359 
360 #endif /* !__GAME_H__ */
361