1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 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 this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 // client.h -- primary header for client
21 
22 //define	PARANOID			// speed sapping error checking
23 
24 #ifdef __x86_64__
25 #define INT long int
26 #else
27 #define INT int
28 #endif
29 
30 #include <math.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 
37 #include "ref.h"
38 
39 #include "vid.h"
40 #include "screen.h"
41 #include "sound.h"
42 #include "input.h"
43 #include "keys.h"
44 #include "console.h"
45 #include "cdaudio.h"
46 
47 //=============================================================================
48 
49 #ifdef QMAX
50 #define random()	((rand () & 0x7fff) / ((float)0x7fff))
51 #define crandom()	(2.0 * (random() - 0.5))
52 vec3_t clientOrg; //lerped org of client for server->client side effects
53 
54 void vectoangles2 (vec3_t value1, vec3_t angles);
55 
56 #include "particles.h"
57 
58 int	color8red (int color8);
59 int	color8green (int color8);
60 int	color8blue (int color8);
61 #endif
62 
63 typedef struct
64 {
65 	qboolean		valid;			// cleared if delta parsing was invalid
66 	int				serverframe;
67 	int				servertime;		// server time the message is valid for (in msec)
68 	int				deltaframe;
69 	byte			areabits[MAX_MAP_AREAS/8];		// portalarea visibility bits
70 	player_state_t	playerstate;
71 	int				num_entities;
72 	int				parse_entities;	// non-masked index into cl_parse_entities array
73 } frame_t;
74 
75 typedef struct
76 {
77 	entity_state_t	baseline;		// delta from this if not from a previous frame
78 	entity_state_t	current;
79 	entity_state_t	prev;			// will always be valid, but might just be a copy of current
80 
81 	int			serverframe;		// if not current, this ent isn't in the frame
82 
83 	int			trailcount;			// for diminishing grenade trails
84 	vec3_t		lerp_origin;		// for trails (variable hz)
85 
86 	int			fly_stoptime;
87 } centity_t;
88 
89 #define MAX_CLIENTWEAPONMODELS		20		// PGM -- upped from 16 to fit the chainfist vwep
90 
91 typedef struct
92 {
93 	char	name[MAX_QPATH];
94 	char	cinfo[MAX_QPATH];
95 	struct image_s	*skin;
96 	struct image_s	*icon;
97 	char	iconname[MAX_QPATH];
98 	struct model_s	*model;
99 	struct model_s	*weaponmodel[MAX_CLIENTWEAPONMODELS];
100 } clientinfo_t;
101 
102 extern char cl_weaponmodels[MAX_CLIENTWEAPONMODELS][MAX_QPATH];
103 extern int num_cl_weaponmodels;
104 
105 #define	CMD_BACKUP		64	// allow a lot of command backups for very fast systems
106 
107 //
108 // the client_state_t structure is wiped completely at every
109 // server map change
110 //
111 typedef struct
112 {
113 	int			timeoutcount;
114 
115 	int			timedemo_frames;
116 	int			timedemo_start;
117 
118 	qboolean	refresh_prepped;	// false if on new level or new ref dll
119 	qboolean	sound_prepped;		// ambient sounds can start
120 	qboolean	force_refdef;		// vid has changed, so we can't use a paused refdef
121 
122 	int			parse_entities;		// index (not anded off) into cl_parse_entities[]
123 
124 	usercmd_t	cmd;
125 	usercmd_t	cmds[CMD_BACKUP];	// each mesage will send several old cmds
126 	int			cmd_time[CMD_BACKUP];	// time sent, for calculating pings
127 	short		predicted_origins[CMD_BACKUP][3];	// for debug comparing against server
128 
129 	float		predicted_step;				// for stair up smoothing
130 	unsigned	predicted_step_time;
131 
132 	vec3_t		predicted_origin;	// generated by CL_PredictMovement
133 	vec3_t		predicted_angles;
134 	vec3_t		prediction_error;
135 
136 	frame_t		frame;				// received from server
137 	int			surpressCount;		// number of messages rate supressed
138 	frame_t		frames[UPDATE_BACKUP];
139 
140 	// the client maintains its own idea of view angles, which are
141 	// sent to the server each frame.  It is cleared to 0 upon entering each level.
142 	// the server sends a delta each frame which is added to the locally
143 	// tracked view angles to account for standing on rotating objects,
144 	// and teleport direction changes
145 	vec3_t		viewangles;
146 
147 	int			time;			// this is the time value that the client
148 								// is rendering at.  always <= cls.realtime
149 	float		lerpfrac;		// between oldframe and frame
150 
151 	refdef_t	refdef;
152 
153 	vec3_t		v_forward, v_right, v_up;	// set when refdef.angles is set
154 
155 	//
156 	// transient data from server
157 	//
158 	char		layout[1024];		// general 2D overlay
159 	int			inventory[MAX_ITEMS];
160 
161 	//
162 	// non-gameserver infornamtion
163 	// FIXME: move this cinematic stuff into the cin_t structure
164 	FILE		*cinematic_file;
165 	int			cinematictime;		// cls.realtime for first cinematic frame
166 	int			cinematicframe;
167 	unsigned char	cinematicpalette[768];
168 	qboolean	cinematicpalette_active;
169 
170 	//
171 	// server state information
172 	//
173 	qboolean	attractloop;		// running the attract loop, any key will menu
174 	int			servercount;	// server identification for prespawns
175 	char		gamedir[MAX_QPATH];
176 	int			playernum;
177 
178 	char		configstrings[MAX_CONFIGSTRINGS][MAX_QPATH];
179 
180 	//
181 	// locally derived information from server state
182 	//
183 	struct model_s	*model_draw[MAX_MODELS];
184 	struct cmodel_s	*model_clip[MAX_MODELS];
185 
186 	struct sfx_s	*sound_precache[MAX_SOUNDS];
187 	struct image_s	*image_precache[MAX_IMAGES];
188 
189 	clientinfo_t	clientinfo[MAX_CLIENTS];
190 	clientinfo_t	baseclientinfo;
191 } client_state_t;
192 
193 extern	client_state_t	cl;
194 
195 /*
196 ==================================================================
197 
198 the client_static_t structure is persistant through an arbitrary number
199 of server connections
200 
201 ==================================================================
202 */
203 
204 typedef enum {
205 	ca_uninitialized,
206 	ca_disconnected, 	// not talking to a server
207 	ca_connecting,		// sending request packets to the server
208 	ca_connected,		// netchan_t established, waiting for svc_serverdata
209 	ca_active			// game views should be displayed
210 } connstate_t;
211 
212 typedef enum {
213 	dl_none,
214 	dl_model,
215 	dl_sound,
216 	dl_skin,
217 	dl_single
218 } dltype_t;		// download type
219 
220 typedef enum {key_game, key_console, key_message, key_menu} keydest_t;
221 
222 typedef struct
223 {
224 	connstate_t	state;
225 	keydest_t	key_dest;
226 
227 	int			framecount;
228 	int			realtime;			// always increasing, no clamping, etc
229 	float		frametime;			// seconds since last frame
230 
231 // screen rendering information
232 	float		disable_screen;		// showing loading plaque between levels
233 									// or changing rendering dlls
234 									// if time gets > 30 seconds ahead, break it
235 	int			disable_servercount;	// when we receive a frame and cl.servercount
236 									// > cls.disable_servercount, clear disable_screen
237 
238 // connection information
239 	char		servername[MAX_OSPATH];	// name of server from original connect
240 	float		connect_time;		// for connection retransmits
241 
242 	int			quakePort;			// a 16 bit value that allows quake servers
243 									// to work around address translating routers
244 	netchan_t	netchan;
245 	int			serverProtocol;		// in case we are doing some kind of version hack
246 
247 	int			challenge;			// from the server to use for connecting
248 
249 	FILE		*download;			// file transfer from server
250 	char		downloadtempname[MAX_OSPATH];
251 	char		downloadname[MAX_OSPATH];
252 	int			downloadnumber;
253 	dltype_t	downloadtype;
254 	int			downloadpercent;
255 
256 // demo recording info must be here, so it isn't cleared on level change
257 	qboolean	demorecording;
258 	qboolean	demowaiting;	// don't record until a non-delta message is received
259 	FILE		*demofile;
260 } client_static_t;
261 
262 extern client_static_t	cls;
263 
264 //=============================================================================
265 
266 //
267 // cvars
268 //
269 extern	cvar_t	*cl_stereo_separation;
270 extern	cvar_t	*cl_stereo;
271 
272 extern	cvar_t	*cl_gun;
273 extern	cvar_t	*cl_add_blend;
274 extern	cvar_t	*cl_add_lights;
275 extern	cvar_t	*cl_add_particles;
276 extern	cvar_t	*cl_add_entities;
277 extern	cvar_t	*cl_predict;
278 extern	cvar_t	*cl_footsteps;
279 extern	cvar_t	*cl_noskins;
280 extern	cvar_t	*cl_autoskins;
281 
282 #ifdef QMAX
283 //psychospaz stuff
284 //railgun
285 extern	cvar_t	*cl_railred;
286 extern	cvar_t	*cl_railgreen;
287 extern	cvar_t	*cl_railblue;
288 extern	cvar_t	*cl_railtype;
289 //3dcam
290 extern	cvar_t	*cl_3dcam;
291 extern	cvar_t	*cl_3dcam_angle;
292 extern	cvar_t	*cl_3dcam_chase;
293 extern	cvar_t	*cl_3dcam_dist;
294 extern	cvar_t	*cl_3dcam_alpha;
295 extern	cvar_t	*cl_3dcam_adjust;
296 
297 extern	cvar_t	*cl_blood;
298 
299 #endif
300 
301 extern	cvar_t	*cl_upspeed;
302 extern	cvar_t	*cl_forwardspeed;
303 extern	cvar_t	*cl_sidespeed;
304 
305 extern	cvar_t	*cl_yawspeed;
306 extern	cvar_t	*cl_pitchspeed;
307 
308 extern	cvar_t	*cl_run;
309 
310 extern	cvar_t	*cl_anglespeedkey;
311 
312 extern	cvar_t	*cl_shownet;
313 extern	cvar_t	*cl_showmiss;
314 extern	cvar_t	*cl_showclamp;
315 
316 extern	cvar_t	*lookspring;
317 extern	cvar_t	*lookstrafe;
318 extern	cvar_t	*sensitivity;
319 
320 extern	cvar_t	*m_pitch;
321 extern	cvar_t	*m_yaw;
322 extern	cvar_t	*m_forward;
323 extern	cvar_t	*m_side;
324 
325 extern	cvar_t	*freelook;
326 
327 extern	cvar_t	*cl_lightlevel;	// FIXME HACK
328 
329 extern	cvar_t	*cl_paused;
330 extern	cvar_t	*cl_timedemo;
331 
332 extern	cvar_t	*cl_vwep;
333 
334 typedef struct
335 {
336 	int		key;				// so entities can reuse same entry
337 	vec3_t	color;
338 	vec3_t	origin;
339 	float	radius;
340 	float	die;				// stop lighting after this time
341 	float	decay;				// drop this each second
342 	float	minlight;			// don't add when contributing less
343 } cdlight_t;
344 
345 extern	centity_t	cl_entities[MAX_EDICTS];
346 extern	cdlight_t	cl_dlights[MAX_DLIGHTS];
347 
348 // the cl_parse_entities must be large enough to hold UPDATE_BACKUP frames of
349 // entities, so that when a delta compressed message arives from the server
350 // it can be un-deltad from the original
351 #define	MAX_PARSE_ENTITIES	1024
352 extern	entity_state_t	cl_parse_entities[MAX_PARSE_ENTITIES];
353 
354 //=============================================================================
355 
356 extern	netadr_t	net_from;
357 extern	sizebuf_t	net_message;
358 
359 void DrawString (int x, int y, char *s);
360 void DrawAltString (int x, int y, char *s);	// toggle high bit
361 qboolean	CL_CheckOrDownloadFile (char *filename);
362 
363 void CL_AddNetgraph (void);
364 
365 //ROGUE
366 typedef struct cl_sustain
367 {
368 	int			id;
369 	int			type;
370 	int			endtime;
371 	int			nextthink;
372 	int			thinkinterval;
373 	vec3_t		org;
374 	vec3_t		dir;
375 	int			color;
376 	int			count;
377 	int			magnitude;
378 	void		(*think)(struct cl_sustain *self);
379 } cl_sustain_t;
380 
381 #define MAX_SUSTAINS		32
382 void CL_ParticleSteamEffect2(cl_sustain_t *self);
383 
384 void CL_TeleporterParticles (entity_state_t *ent);
385 void CL_ParticleEffect (vec3_t org, vec3_t dir, int color, int count);
386 void CL_ParticleEffect2 (vec3_t org, vec3_t dir, int color, int count);
387 
388 // RAFAEL
389 void CL_ParticleEffect3 (vec3_t org, vec3_t dir, int color, int count);
390 
391 
392 //=================================================
393 
394 #ifdef QMAX
395 
396 typedef struct
397 {
398 	qboolean	isactive;
399 
400 	vec3_t		lightcol;
401 	float		light;
402 	float		lightvel;
403 } cplight_t;
404 #define P_LIGHTS_MAX 8
405 #endif
406 
407 // ========
408 // PGM
409 typedef struct particle_s
410 {
411 	struct particle_s	*next;
412 
413 	float		time;
414 
415 	vec3_t		org;
416 	vec3_t		vel;
417 	vec3_t		accel;
418 #ifdef QMAX
419   vec3_t color;
420   vec3_t colorvel;
421 #else
422   float		color;
423   float		colorvel;
424 #endif
425 	float		alpha;
426 	float		alphavel;
427 
428 #ifdef QMAX
429 	cplight_t	lights[P_LIGHTS_MAX];
430 
431 	float		start;
432 	float		size;
433 	float		sizevel;
434 
435 	vec3_t		angle;
436 
437 	int			image;
438 	int			flags;
439 
440 	vec3_t		oldorg;
441 	float		temp;
442 	int			src_ent;
443 	int			dst_ent;
444 
445 	struct particle_s	*link;
446 
447   void		(*think)(struct particle_s *p, vec3_t org, vec3_t angle, float *alpha, float *size, int *image, float *time);
448   qboolean	thinknext;
449 #endif
450 } cparticle_t;
451 
452 
453 #define	PARTICLE_GRAVITY	40
454 #define BLASTER_PARTICLE_COLOR		0xe0
455 // PMM
456 #define INSTANT_PARTICLE	-10000.0
457 // PGM
458 // ========
459 
460 void CL_ClearEffects (void);
461 void CL_ClearTEnts (void);
462 void CL_BlasterTrail (vec3_t start, vec3_t end);
463 void CL_QuadTrail (vec3_t start, vec3_t end);
464 void CL_RailTrail (vec3_t start, vec3_t end);
465 void CL_BubbleTrail (vec3_t start, vec3_t end);
466 #ifdef QMAX
467 void CL_FlagTrail (vec3_t start, vec3_t end, qboolean isred);
468 #else
469 void CL_FlagTrail (vec3_t start, vec3_t end, float color);
470 #endif
471 
472 // RAFAEL
473 void CL_IonripperTrail (vec3_t start, vec3_t end);
474 
475 // ========
476 // PGM
477 void CL_BlasterParticles2 (vec3_t org, vec3_t dir, unsigned int color);
478 void CL_BlasterTrail2 (vec3_t start, vec3_t end);
479 void CL_DebugTrail (vec3_t start, vec3_t end);
480 void CL_SmokeTrail (vec3_t start, vec3_t end, int colorStart, int colorRun, int spacing);
481 void CL_Flashlight (int ent, vec3_t pos);
482 void CL_ForceWall (vec3_t start, vec3_t end, int color);
483 void CL_FlameEffects (centity_t *ent, vec3_t origin);
484 void CL_GenericParticleEffect (vec3_t org, vec3_t dir, int color, int count, int numcolors, int dirspread, float alphavel);
485 void CL_BubbleTrail2 (vec3_t start, vec3_t end, int dist);
486 void CL_Heatbeam (vec3_t start, vec3_t end);
487 void CL_ParticleSteamEffect (vec3_t org, vec3_t dir, int color, int count, int magnitude);
488 void CL_TrackerTrail (vec3_t start, vec3_t end, int particleColor);
489 void CL_Tracker_Explode(vec3_t origin);
490 void CL_TagTrail (vec3_t start, vec3_t end, float color);
491 void CL_ColorFlash (vec3_t pos, int ent, int intensity, float r, float g, float b);
492 void CL_Tracker_Shell(vec3_t origin);
493 void CL_MonsterPlasma_Shell(vec3_t origin);
494 void CL_ColorExplosionParticles (vec3_t org, int color, int run);
495 void CL_ParticleSmokeEffect (vec3_t org, vec3_t dir, int color, int count, int magnitude);
496 void CL_Widowbeamout (cl_sustain_t *self);
497 void CL_Nukeblast (cl_sustain_t *self);
498 void CL_WidowSplash (vec3_t org);
499 // PGM
500 // ========
501 
502 int CL_ParseEntityBits (unsigned *bits);
503 void CL_ParseDelta (entity_state_t *from, entity_state_t *to, int number, int bits);
504 void CL_ParseFrame (void);
505 
506 void CL_ParseTEnt (void);
507 void CL_ParseConfigString (void);
508 void CL_ParseMuzzleFlash (void);
509 void CL_ParseMuzzleFlash2 (void);
510 void SmokeAndFlash(vec3_t origin);
511 
512 void CL_SetLightstyle (int i);
513 
514 void CL_RunParticles (void);
515 void CL_RunDLights (void);
516 void CL_RunLightStyles (void);
517 
518 void CL_AddEntities (void);
519 void CL_AddDLights (void);
520 void CL_AddTEnts (void);
521 void CL_AddLightStyles (void);
522 
523 //=================================================
524 
525 void CL_PrepRefresh (void);
526 void CL_RegisterSounds (void);
527 
528 void CL_Quit_f (void);
529 
530 void IN_Accumulate (void);
531 
532 void CL_ParseLayout (void);
533 
534 
535 //
536 // cl_main
537 //
538 extern	refexport_t	re;		// interface to refresh .dll
539 
540 void CL_Init (void);
541 
542 void CL_FixUpGender(void);
543 void CL_Disconnect (void);
544 void CL_Disconnect_f (void);
545 void CL_GetChallengePacket (void);
546 void CL_PingServers_f (void);
547 void CL_Snd_Restart_f (void);
548 void CL_RequestNextDownload (void);
549 
550 //
551 // cl_input
552 //
553 typedef struct
554 {
555 	int			down[2];		// key nums holding it down
556 	unsigned	downtime;		// msec timestamp
557 	unsigned	msec;			// msec down this frame
558 	int			state;
559 } kbutton_t;
560 
561 extern	kbutton_t	in_mlook, in_klook;
562 extern 	kbutton_t 	in_strafe;
563 extern 	kbutton_t 	in_speed;
564 
565 void CL_InitInput (void);
566 void CL_SendCmd (void);
567 void CL_SendMove (usercmd_t *cmd);
568 
569 void CL_ClearState (void);
570 
571 void CL_ReadPackets (void);
572 
573 int  CL_ReadFromServer (void);
574 void CL_WriteToServer (usercmd_t *cmd);
575 void CL_BaseMove (usercmd_t *cmd);
576 
577 void IN_CenterView (void);
578 
579 float CL_KeyState (kbutton_t *key);
580 char *Key_KeynumToString (int keynum);
581 
582 //
583 // cl_demo.c
584 //
585 void CL_WriteDemoMessage (void);
586 void CL_Stop_f (void);
587 void CL_Record_f (void);
588 
589 //
590 // cl_parse.c
591 //
592 extern	char *svc_strings[256];
593 
594 void CL_ParseServerMessage (void);
595 void CL_LoadClientinfo (clientinfo_t *ci, char *s);
596 void SHOWNET(char *s);
597 void CL_ParseClientinfo (int player);
598 void CL_Download_f (void);
599 
600 //
601 // cl_view.c
602 //
603 extern	int			gun_frame;
604 extern	struct model_s	*gun_model;
605 
606 void V_Init (void);
607 void V_RenderView( float stereo_separation );
608 void V_AddEntity (entity_t *ent);
609 #ifdef QMAX
610 void V_AddParticle (vec3_t org, vec3_t angle, vec3_t color, float alpha, float size, int image, int flags);
611 #else
612 void V_AddParticle (vec3_t org, int color, float alpha);
613 #endif
614 void V_AddLight (vec3_t org, float intensity, float r, float g, float b);
615 void V_AddLightStyle (int style, float r, float g, float b);
616 
617 //
618 // cl_tent.c
619 //
620 void CL_RegisterTEntSounds (void);
621 void CL_RegisterTEntModels (void);
622 void CL_SmokeAndFlash(vec3_t origin);
623 
624 
625 //
626 // cl_pred.c
627 //
628 void CL_InitPrediction (void);
629 void CL_PredictMove (void);
630 void CL_CheckPredictionError (void);
631 
632 //
633 // cl_fx.c
634 //
635 cdlight_t *CL_AllocDlight (int key);
636 void CL_BigTeleportParticles (vec3_t org);
637 void CL_RocketTrail (vec3_t start, vec3_t end, centity_t *old);
638 void CL_DiminishingTrail (vec3_t start, vec3_t end, centity_t *old, int flags);
639 void CL_FlyEffect (centity_t *ent, vec3_t origin);
640 void CL_BfgParticles (entity_t *ent);
641 void CL_AddParticles (void);
642 void CL_EntityEvent (entity_state_t *ent);
643 // RAFAEL
644 void CL_TrapParticles (entity_t *ent);
645 
646 //
647 // menus
648 //
649 void M_Init (void);
650 void M_Keydown (int key);
651 void M_Draw (void);
652 void M_Menu_Main_f (void);
653 void M_ForceMenuOff (void);
654 void M_AddToServerList (netadr_t adr, char *info);
655 
656 //
657 // cl_inv.c
658 //
659 void CL_ParseInventory (void);
660 void CL_KeyInventory (int key);
661 void CL_DrawInventory (void);
662 
663 //
664 // cl_pred.c
665 //
666 void CL_PredictMovement (void);
667 
668 #if id386
669 void x86_TimerStart( void );
670 void x86_TimerStop( void );
671 void x86_TimerInit( unsigned long smallest, unsigned longest );
672 unsigned long *x86_TimerGetHistogram( void );
673 #endif
674 
675 
676 #ifdef QMAX
677 void SetParticleImages (void);
678 #endif
679