1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 Copyright (C) 2000-2006 Tim Angus
5 
6 This file is part of Tremulous.
7 
8 Tremulous is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12 
13 Tremulous is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Tremulous; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 ===========================================================================
22 */
23 // client.h -- primary header for client
24 
25 #include "../qcommon/q_shared.h"
26 #include "../qcommon/qcommon.h"
27 #include "../renderer/tr_public.h"
28 #include "../ui/ui_public.h"
29 #include "keys.h"
30 #include "snd_public.h"
31 #include "../cgame/cg_public.h"
32 #include "../game/bg_public.h"
33 
34 #define	RETRANSMIT_TIMEOUT	3000	// time between connection packet retransmits
35 
36 
37 // snapshots are a view of the server at a given time
38 typedef struct {
39 	qboolean		valid;			// cleared if delta parsing was invalid
40 	int				snapFlags;		// rate delayed and dropped commands
41 
42 	int				serverTime;		// server time the message is valid for (in msec)
43 
44 	int				messageNum;		// copied from netchan->incoming_sequence
45 	int				deltaNum;		// messageNum the delta is from
46 	int				ping;			// time from when cmdNum-1 was sent to time packet was reeceived
47 	byte			areamask[MAX_MAP_AREA_BYTES];		// portalarea visibility bits
48 
49 	int				cmdNum;			// the next cmdNum the server is expecting
50 	playerState_t	ps;						// complete information about the current player at this time
51 
52 	int				numEntities;			// all of the entities that need to be presented
53 	int				parseEntitiesNum;		// at the time of this snapshot
54 
55 	int				serverCommandNum;		// execute all commands up to this before
56 											// making the snapshot current
57 } clSnapshot_t;
58 
59 
60 
61 /*
62 =============================================================================
63 
64 the clientActive_t structure is wiped completely at every
65 new gamestate_t, potentially several times during an established connection
66 
67 =============================================================================
68 */
69 
70 typedef struct {
71 	int		p_cmdNumber;		// cl.cmdNumber when packet was sent
72 	int		p_serverTime;		// usercmd->serverTime when packet was sent
73 	int		p_realtime;			// cls.realtime when packet was sent
74 } outPacket_t;
75 
76 // the parseEntities array must be large enough to hold PACKET_BACKUP frames of
77 // entities, so that when a delta compressed message arives from the server
78 // it can be un-deltad from the original
79 #define	MAX_PARSE_ENTITIES	2048
80 
81 extern int g_console_field_width;
82 
83 typedef struct {
84 	int			timeoutcount;		// it requres several frames in a timeout condition
85 									// to disconnect, preventing debugging breaks from
86 									// causing immediate disconnects on continue
87 	clSnapshot_t	snap;			// latest received from server
88 
89 	int			serverTime;			// may be paused during play
90 	int			oldServerTime;		// to prevent time from flowing bakcwards
91 	int			oldFrameServerTime;	// to check tournament restarts
92 	int			serverTimeDelta;	// cl.serverTime = cls.realtime + cl.serverTimeDelta
93 									// this value changes as net lag varies
94 	qboolean	extrapolatedSnapshot;	// set if any cgame frame has been forced to extrapolate
95 									// cleared when CL_AdjustTimeDelta looks at it
96 	qboolean	newSnapshots;		// set on parse of any valid packet
97 
98 	gameState_t	gameState;			// configstrings
99 	char		mapname[MAX_QPATH];	// extracted from CS_SERVERINFO
100 
101 	int			parseEntitiesNum;	// index (not anded off) into cl_parse_entities[]
102 
103 	int			mouseDx[2], mouseDy[2];	// added to by mouse events
104 	int			mouseIndex;
105 	int			joystickAxis[MAX_JOYSTICK_AXIS];	// set by joystick events
106 
107 	// cgame communicates a few values to the client system
108 	int			cgameUserCmdValue;	// current weapon to add to usercmd_t
109 	float		cgameSensitivity;
110 
111 	// cmds[cmdNumber] is the predicted command, [cmdNumber-1] is the last
112 	// properly generated command
113 	usercmd_t	cmds[CMD_BACKUP];	// each mesage will send several old cmds
114 	int			cmdNumber;			// incremented each frame, because multiple
115 									// frames may need to be packed into a single packet
116 
117 	outPacket_t	outPackets[PACKET_BACKUP];	// information about each packet we have sent out
118 
119 	// the client maintains its own idea of view angles, which are
120 	// sent to the server each frame.  It is cleared to 0 upon entering each level.
121 	// the server sends a delta each frame which is added to the locally
122 	// tracked view angles to account for standing on rotating objects,
123 	// and teleport direction changes
124 	vec3_t		viewangles;
125 
126 	int			serverId;			// included in each client message so the server
127 												// can tell if it is for a prior map_restart
128 	// big stuff at end of structure so most offsets are 15 bits or less
129 	clSnapshot_t	snapshots[PACKET_BACKUP];
130 
131 	entityState_t	entityBaselines[MAX_GENTITIES];	// for delta compression when not in previous frame
132 
133 	entityState_t	parseEntities[MAX_PARSE_ENTITIES];
134 } clientActive_t;
135 
136 extern	clientActive_t		cl;
137 
138 /*
139 =============================================================================
140 
141 the clientConnection_t structure is wiped when disconnecting from a server,
142 either to go to a full screen console, play a demo, or connect to a different server
143 
144 A connection can be to either a server through the network layer or a
145 demo through a file.
146 
147 =============================================================================
148 */
149 
150 
151 typedef struct {
152 
153 	int			clientNum;
154 	int			lastPacketSentTime;			// for retransmits during connection
155 	int			lastPacketTime;				// for timeouts
156 
157 	netadr_t	serverAddress;
158 	int			connectTime;				// for connection retransmits
159 	int			connectPacketCount;			// for display on connection dialog
160 	char		serverMessage[MAX_STRING_TOKENS];	// for display on connection dialog
161 
162 	int			challenge;					// from the server to use for connecting
163 	int			checksumFeed;				// from the server for checksum calculations
164 
165 	// these are our reliable messages that go to the server
166 	int			reliableSequence;
167 	int			reliableAcknowledge;		// the last one the server has executed
168 	char		reliableCommands[MAX_RELIABLE_COMMANDS][MAX_STRING_CHARS];
169 
170 	// server message (unreliable) and command (reliable) sequence
171 	// numbers are NOT cleared at level changes, but continue to
172 	// increase as long as the connection is valid
173 
174 	// message sequence is used by both the network layer and the
175 	// delta compression layer
176 	int			serverMessageSequence;
177 
178 	// reliable messages received from server
179 	int			serverCommandSequence;
180 	int			lastExecutedServerCommand;		// last server command grabbed or executed with CL_GetServerCommand
181 	char		serverCommands[MAX_RELIABLE_COMMANDS][MAX_STRING_CHARS];
182 
183 	// file transfer from server
184 	fileHandle_t download;
185 	char		downloadTempName[MAX_OSPATH];
186 	char		downloadName[MAX_OSPATH];
187 	int			downloadNumber;
188 	int			downloadBlock;	// block we are waiting for
189 	int			downloadCount;	// how many bytes we got
190 	int			downloadSize;	// how many bytes we got
191 	char		downloadList[MAX_INFO_STRING]; // list of paks we need to download
192 	qboolean	downloadRestart;	// if true, we need to do another FS_Restart because we downloaded a pak
193 
194 	// demo information
195 	char		demoName[MAX_QPATH];
196 	qboolean	spDemoRecording;
197 	qboolean	demorecording;
198 	qboolean	demoplaying;
199 	qboolean	demowaiting;	// don't record until a non-delta message is received
200 	qboolean	firstDemoFrameSkipped;
201 	fileHandle_t	demofile;
202 
203 	int			timeDemoFrames;		// counter of rendered frames
204 	int			timeDemoStart;		// cls.realtime before first frame
205 	int			timeDemoBaseTime;	// each frame will be at this time + frameNum * 50
206 
207 	// big stuff at end of structure so most offsets are 15 bits or less
208 	netchan_t	netchan;
209 } clientConnection_t;
210 
211 extern	clientConnection_t clc;
212 
213 /*
214 ==================================================================
215 
216 the clientStatic_t structure is never wiped, and is used even when
217 no client connection is active at all
218 
219 ==================================================================
220 */
221 
222 typedef struct {
223 	netadr_t	adr;
224 	int			start;
225 	int			time;
226 	char		info[MAX_INFO_STRING];
227 } ping_t;
228 
229 typedef struct {
230 	netadr_t	adr;
231 	char	  	hostName[MAX_NAME_LENGTH];
232 	char	  	mapName[MAX_NAME_LENGTH];
233 	char	  	game[MAX_NAME_LENGTH];
234 	int			netType;
235 	int			gameType;
236 	int		  	clients;
237 	int		  	maxClients;
238 	int			minPing;
239 	int			maxPing;
240 	int			ping;
241 	qboolean	visible;
242 } serverInfo_t;
243 
244 typedef struct {
245 	byte	ip[4];
246 	unsigned short	port;
247 } serverAddress_t;
248 
249 typedef struct {
250 	connstate_t	state;				// connection status
251 	int			keyCatchers;		// bit flags
252 
253 	qboolean	cddialog;			// bring up the cd needed dialog next frame
254 
255 	char		servername[MAX_OSPATH];		// name of server from original connect (used by reconnect)
256 
257 	// when the server clears the hunk, all of these must be restarted
258 	qboolean	rendererStarted;
259 	qboolean	soundStarted;
260 	qboolean	soundRegistered;
261 	qboolean	uiStarted;
262 	qboolean	cgameStarted;
263 
264 	int			framecount;
265 	int			frametime;			// msec since last frame
266 
267 	int			realtime;			// ignores pause
268 	int			realFrametime;		// ignoring pause, so console always works
269 
270 	int			numlocalservers;
271 	serverInfo_t	localServers[MAX_OTHER_SERVERS];
272 
273 	int			numglobalservers;
274 	serverInfo_t  globalServers[MAX_GLOBAL_SERVERS];
275 	// additional global servers
276 	int			numGlobalServerAddresses;
277 	serverAddress_t		globalServerAddresses[MAX_GLOBAL_SERVERS];
278 
279 	int			numfavoriteservers;
280 	serverInfo_t	favoriteServers[MAX_OTHER_SERVERS];
281 
282 	int			nummplayerservers;
283 	serverInfo_t	mplayerServers[MAX_OTHER_SERVERS];
284 
285 	int pingUpdateSource;		// source currently pinging or updating
286 
287 	int masterNum;
288 
289 	// update server info
290 	netadr_t	updateServer;
291 	char		updateChallenge[MAX_TOKEN_CHARS];
292 	char		updateInfoString[MAX_INFO_STRING];
293 
294 	netadr_t	authorizeServer;
295 
296 	// rendering info
297 	glconfig_t	glconfig;
298 	qhandle_t	charSetShader;
299 	qhandle_t	whiteShader;
300 	qhandle_t	consoleShader;
301 } clientStatic_t;
302 
303 extern	clientStatic_t		cls;
304 
305 //=============================================================================
306 
307 extern	vm_t			*cgvm;	// interface to cgame dll or vm
308 extern	vm_t			*uivm;	// interface to ui dll or vm
309 extern	refexport_t		re;		// interface to refresh .dll
310 
311 
312 //
313 // cvars
314 //
315 extern	cvar_t	*cl_nodelta;
316 extern	cvar_t	*cl_debugMove;
317 extern	cvar_t	*cl_noprint;
318 extern	cvar_t	*cl_timegraph;
319 extern	cvar_t	*cl_maxpackets;
320 extern	cvar_t	*cl_packetdup;
321 extern	cvar_t	*cl_shownet;
322 extern	cvar_t	*cl_showSend;
323 extern	cvar_t	*cl_timeNudge;
324 extern	cvar_t	*cl_showTimeDelta;
325 extern	cvar_t	*cl_freezeDemo;
326 
327 extern	cvar_t	*cl_yawspeed;
328 extern	cvar_t	*cl_pitchspeed;
329 extern	cvar_t	*cl_run;
330 extern	cvar_t	*cl_anglespeedkey;
331 
332 extern	cvar_t	*cl_sensitivity;
333 extern	cvar_t	*cl_platformSensitivity;
334 extern	cvar_t	*cl_freelook;
335 
336 extern	cvar_t	*cl_mouseAccel;
337 extern	cvar_t	*cl_showMouseRate;
338 
339 extern	cvar_t	*m_pitch;
340 extern	cvar_t	*m_yaw;
341 extern	cvar_t	*m_forward;
342 extern	cvar_t	*m_side;
343 extern	cvar_t	*m_filter;
344 
345 extern	cvar_t	*cl_timedemo;
346 extern	cvar_t	*cl_aviFrameRate;
347 extern	cvar_t	*cl_aviMotionJpeg;
348 
349 extern	cvar_t	*cl_activeAction;
350 
351 extern	cvar_t	*cl_allowDownload;
352 extern	cvar_t	*cl_conXOffset;
353 extern	cvar_t	*cl_inGameVideo;
354 
355 //=================================================
356 
357 //
358 // cl_main
359 //
360 
361 void CL_Init (void);
362 void CL_FlushMemory(void);
363 void CL_ShutdownAll(void);
364 void CL_AddReliableCommand( const char *cmd );
365 
366 void CL_StartHunkUsers( void );
367 
368 void CL_Disconnect_f (void);
369 void CL_GetChallengePacket (void);
370 void CL_Vid_Restart_f( void );
371 void CL_Snd_Restart_f (void);
372 void CL_StartDemoLoop( void );
373 void CL_NextDemo( void );
374 void CL_ReadDemoMessage( void );
375 demoState_t CL_DemoState( void );
376 int CL_DemoPos( void );
377 void CL_DemoName( char *buffer, int size );
378 
379 void CL_InitDownloads(void);
380 void CL_NextDownload(void);
381 
382 void CL_GetPing( int n, char *buf, int buflen, int *pingtime );
383 void CL_GetPingInfo( int n, char *buf, int buflen );
384 void CL_ClearPing( int n );
385 int CL_GetPingQueueCount( void );
386 
387 void CL_ShutdownRef( void );
388 void CL_InitRef( void );
389 int CL_ServerStatus( char *serverAddress, char *serverStatusString, int maxLen );
390 
391 
392 //
393 // cl_input
394 //
395 typedef struct {
396 	int			down[2];		// key nums holding it down
397 	unsigned	downtime;		// msec timestamp
398 	unsigned	msec;			// msec down this frame if both a down and up happened
399 	qboolean	active;			// current state
400 	qboolean	wasPressed;		// set when down, not cleared when up
401 } kbutton_t;
402 
403 extern	kbutton_t	in_mlook, in_klook;
404 extern 	kbutton_t 	in_strafe;
405 extern 	kbutton_t 	in_speed;
406 
407 void CL_InitInput (void);
408 void CL_SendCmd (void);
409 void CL_ClearState (void);
410 void CL_ReadPackets (void);
411 
412 void CL_WritePacket( void );
413 void IN_CenterView (void);
414 
415 void CL_VerifyCode( void );
416 
417 float CL_KeyState (kbutton_t *key);
418 char *Key_KeynumToString (int keynum);
419 
420 //
421 // cl_parse.c
422 //
423 extern int cl_connectedToPureServer;
424 
425 void CL_SystemInfoChanged( void );
426 void CL_ParseServerMessage( msg_t *msg );
427 
428 //====================================================================
429 
430 void	CL_ServerInfoPacket( netadr_t from, msg_t *msg );
431 void	CL_LocalServers_f( void );
432 void	CL_GlobalServers_f( void );
433 void	CL_FavoriteServers_f( void );
434 void	CL_Ping_f( void );
435 qboolean CL_UpdateVisiblePings_f( int source );
436 
437 
438 //
439 // console
440 //
441 void Con_DrawCharacter (int cx, int line, int num);
442 
443 void Con_CheckResize (void);
444 void Con_Init (void);
445 void Con_Clear_f (void);
446 void Con_ToggleConsole_f (void);
447 void Con_DrawNotify (void);
448 void Con_ClearNotify (void);
449 void Con_RunConsole (void);
450 void Con_DrawConsole (void);
451 void Con_PageUp( void );
452 void Con_PageDown( void );
453 void Con_Top( void );
454 void Con_Bottom( void );
455 void Con_Close( void );
456 
457 void CL_LoadConsoleHistory( void );
458 void CL_SaveConsoleHistory( void );
459 
460 //
461 // cl_scrn.c
462 //
463 void	SCR_Init (void);
464 void	SCR_UpdateScreen (void);
465 
466 void	SCR_DebugGraph (float value, int color);
467 
468 int		SCR_GetBigStringWidth( const char *str );	// returns in virtual 640x480 coordinates
469 
470 void	SCR_AdjustFrom640( float *x, float *y, float *w, float *h );
471 void	SCR_FillRect( float x, float y, float width, float height,
472 					 const float *color );
473 void	SCR_DrawPic( float x, float y, float width, float height, qhandle_t hShader );
474 void	SCR_DrawNamedPic( float x, float y, float width, float height, const char *picname );
475 
476 void	SCR_DrawBigString( int x, int y, const char *s, float alpha );			// draws a string with embedded color control characters with fade
477 void	SCR_DrawBigStringColor( int x, int y, const char *s, vec4_t color );	// ignores embedded color control characters
478 void	SCR_DrawSmallStringExt( int x, int y, const char *string, float *setColor, qboolean forceColor );
479 void	SCR_DrawSmallChar( int x, int y, int ch );
480 
481 
482 //
483 // cl_cin.c
484 //
485 
486 void CL_PlayCinematic_f( void );
487 void SCR_DrawCinematic (void);
488 void SCR_RunCinematic (void);
489 void SCR_StopCinematic (void);
490 int CIN_PlayCinematic( const char *arg0, int xpos, int ypos, int width, int height, int bits);
491 e_status CIN_StopCinematic(int handle);
492 e_status CIN_RunCinematic (int handle);
493 void CIN_DrawCinematic (int handle);
494 void CIN_SetExtents (int handle, int x, int y, int w, int h);
495 void CIN_SetLooping (int handle, qboolean loop);
496 void CIN_UploadCinematic(int handle);
497 void CIN_CloseAllVideos(void);
498 
499 //
500 // cl_cgame.c
501 //
502 void CL_InitCGame( void );
503 void CL_ShutdownCGame( void );
504 qboolean CL_GameCommand( void );
505 void CL_GameConsoleText( void );
506 void CL_CGameRendering( stereoFrame_t stereo );
507 void CL_SetCGameTime( void );
508 void CL_FirstSnapshot( void );
509 void CL_ShaderStateChanged(void);
510 
511 //
512 // cl_ui.c
513 //
514 void CL_InitUI( void );
515 void CL_ShutdownUI( void );
516 int Key_GetCatcher( void );
517 void Key_SetCatcher( int catcher );
518 void LAN_LoadCachedServers( void );
519 void LAN_SaveServersToCache( void );
520 
521 
522 //
523 // cl_net_chan.c
524 //
525 void CL_Netchan_Transmit( netchan_t *chan, msg_t* msg);	//int length, const byte *data );
526 void CL_Netchan_TransmitNextFragment( netchan_t *chan );
527 qboolean CL_Netchan_Process( netchan_t *chan, msg_t *msg );
528 
529 //
530 // cl_avi.c
531 //
532 qboolean CL_OpenAVIForWriting( const char *filename );
533 void CL_TakeVideoFrame( void );
534 void CL_WriteAVIVideoFrame( const byte *imageBuffer, int size );
535 void CL_WriteAVIAudioFrame( const byte *pcmBuffer, int size );
536 qboolean CL_CloseAVI( void );
537 qboolean CL_VideoRecording( void );
538