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