1 /*
2 	server.h
3 
4 	(description)
5 
6 	Copyright (C) 1996-1997  Id Software, Inc.
7 
8 	This program is free software; you can redistribute it and/or
9 	modify it under the terms of the GNU General Public License
10 	as published by the Free Software Foundation; either version 2
11 	of the License, or (at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 	See the 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, write to:
21 
22 		Free Software Foundation, Inc.
23 		59 Temple Place - Suite 330
24 		Boston, MA  02111-1307, USA
25 
26 */
27 // server.h
28 
29 #ifndef _SERVER_H
30 #define _SERVER_H
31 
32 #include <stdarg.h>
33 
34 #include "QF/info.h"
35 #include "QF/model.h"
36 #include "QF/quakeio.h"
37 #include "QF/sizebuf.h"
38 
39 #include "world.h"
40 
41 #include "host.h"
42 #include "netchan.h"
43 #include "qw/bothdefs.h"
44 #include "qw/msg_backbuf.h"
45 #include "qw/protocol.h"
46 
47 #define	QW_SERVER
48 
49 #define	MAX_MASTERS	32				// max recipients for heartbeat packets
50 
51 #define	MAX_SIGNON_BUFFERS	8
52 
53 typedef enum {
54 	ss_dead,			// no map loaded
55 	ss_loading,			// spawning level edicts
56 	ss_active			// actively running
57 } server_state_t;
58 // some qc commands are valid only before the server has finished
59 // initializing (precache commands, static sounds / objects, etc)
60 
61 typedef struct {
62 	qboolean	active;				// false when server is going down
63 	server_state_t	state;			// precache commands are valid only during load
64 
65 	double		time;
66 
67 	int			lastcheck;			// used by PF_checkclient
68 	double		lastchecktime;		// for monster ai
69 
70 	qboolean	paused;				// are we paused?
71 
72 	//check player/eyes models for hacks
73 	unsigned int	model_player_checksum;
74 	unsigned int	eyes_player_checksum;
75 
76 	char		name[64];			// map name
77 	char		modelname[MAX_QPATH];		// maps/<name>.bsp, for model_precache[0]
78 	struct model_s 	*worldmodel;
79 	const char	*model_precache[MAX_MODELS];	// NULL terminated
80 	const char	*sound_precache[MAX_SOUNDS];	// NULL terminated
81 	const char	*lightstyles[MAX_LIGHTSTYLES];
82 	struct model_s		*models[MAX_MODELS];
83 
84 	int			num_edicts;			// increases towards MAX_EDICTS
85 	struct edict_s		*edicts;			// can NOT be array indexed, because
86 									// struct edict_s is variable sized, but can
87 									// be used to reference the world ent
88 
89 	byte		*pvs, *phs;			// fully expanded and decompressed
90 
91 	//antilag
92 	float       lagentsfrac;
93 	laggedentinfo_t *lagents;
94 	unsigned    maxlagents;
95 
96 	// added to every client's unreliable buffer each frame, then cleared
97 	sizebuf_t	datagram;
98 	byte		datagram_buf[MAX_DATAGRAM];
99 
100 	// added to every client's reliable buffer each frame, then cleared
101 	sizebuf_t	reliable_datagram;
102 	byte		reliable_datagram_buf[MAX_MSGLEN];
103 
104 	// the multicast buffer is used to send a message to a set of clients
105 	sizebuf_t	multicast;
106 	byte		multicast_buf[MAX_MSGLEN];
107 
108 	// the master buffer is used for building log packets
109 	sizebuf_t	master;
110 	byte		master_buf[MAX_DATAGRAM];
111 
112 	// the signon buffer will be sent to each client as they connect
113 	// includes the entity baselines, the static entities, etc
114 	// large levels will have >MAX_DATAGRAM sized signons, so
115 	// multiple signon messages are kept
116 	sizebuf_t	signon;
117 	int			num_signon_buffers;
118 	int         max_signon_buffers;	// grows;
119 	int        *signon_buffer_size;
120 	byte      (*signon_buffers)[MAX_DATAGRAM];
121 
122 	// demo stuff
123 	int         recording_demo;
124 	struct recorder_s *recorders;
125 } server_t;
126 
127 #define	NUM_SPAWN_PARMS			16
128 
129 typedef enum {
130 	cs_free,		// can be reused for a new connection
131 	cs_server,		// client is grabbed by the server for its own purposes
132 	cs_zombie,		// client has been disconnected, but don't reuse
133 					// connection for a couple seconds
134 	cs_connected,	// has been assigned to a client_t, but not in game yet
135 	cs_spawned		// client is fully in game
136 } sv_client_state_t;
137 
138 typedef struct {
139 	// received from client
140 
141 	// reply
142 	double				senttime;
143 	float				ping_time;
144 	vec3_t              playerpositions[MAX_CLIENTS];
145 	qboolean            playerpresent[MAX_CLIENTS];
146 	packet_entities_t	entities;
147 	packet_players_t	players;
148 } client_frame_t;
149 
150 typedef enum {
151 	dt_tp_normal,
152 	dt_tp_demo,
153 	dt_tp_qtv,
154 } delta_type_t;
155 
156 typedef enum {
157 	dt_pvs_normal,
158 	dt_pvs_fat,
159 	dt_pvs_none,
160 } delta_pvs_t;
161 
162 typedef struct {
163 	delta_type_t type;
164 	delta_pvs_t pvs;
165 	int         delta_sequence;
166 	int         cur_frame;
167 	int			out_frame;
168 	int			in_frame;
169 	struct client_s *client;
170 	client_frame_t frames[UPDATE_BACKUP];	// updates can be deltad from here
171 } delta_t;
172 
173 #define MAX_BACK_BUFFERS	8
174 #define MAX_STUFFTEXT		512
175 #define MAX_NAME			32
176 
177 typedef enum {
178 	ft_ban,
179 	ft_mute,		// mute penalty save over disconnect
180 	ft_cuff,		// cuff penatly save over disconnect
181 } filtertype_t;
182 
183 typedef struct client_s {
184 	sv_client_state_t	state;
185 	int				ping;				// fake ping for server clients
186 	qboolean		prespawned;
187 	qboolean		spawned;
188 
189 	int				spectator;			// non-interactive
190 
191 	qboolean		sendinfo;			// at end of frame, send info to all
192 										// this prevents malicious multiple
193 										// broadcasts
194 	float			lastnametime;		// time of last name change
195 	int				lastnamecount;		// time of last name change
196 	qboolean		drop;				// lose this guy next opportunity
197 	int				lossage;			// loss percentage
198 
199 	int				userid;				// identifying number
200 	struct info_s   *userinfo;			// infostring
201 
202 	usercmd_t		lastcmd;			// for filling in big drops and partial predictions
203 	double			localtime;			// of last message
204 	int				oldbuttons;
205 	int				oldonground;
206 
207 	float			maxspeed;			// localized maxspeed
208 	float			entgravity;			// localized ent gravity
209 
210 	struct edict_s			*edict;		// EDICT_NUM(clientnum+1)
211 	char			name[MAX_NAME];		// for printing to other people
212 										// extracted from userinfo
213 	int				messagelevel;		// for filtering printed messages
214 
215 	// the datagram is written to after every frame, but cleared only
216 	// when it is sent out to the client.  overflow is tolerated.
217 	sizebuf_t		datagram;
218 	byte			datagram_buf[MAX_DATAGRAM];
219 
220 	// back buffers for client reliable data
221 	backbuf_t       backbuf;
222 
223 	char			stufftext_buf[MAX_STUFFTEXT];
224 
225 	double			connection_started;	// or time of disconnect for zombies
226 	qboolean		send_message;		// set on frames a datagram arived on
227 
228 	//antilag stuff
229 	laggedentinfo_t laggedents[MAX_CLIENTS];
230 	unsigned        laggedents_count;
231 	float           laggedents_frac;
232 
233 // spawn parms are carried from level to level
234 	float			spawn_parms[NUM_SPAWN_PARMS];
235 
236 // client known data for deltas
237 	int				old_frags;
238 
239 	int				stats[MAX_CL_STATS];
240 
241 	delta_t			delta;
242 
243 	QFile			*download;			// file being downloaded
244 	int				downloadsize;		// total bytes
245 	int				downloadcount;		// bytes sent
246 
247 	int				spec_track;			// entnum of player tracking
248 
249 	double			whensaid[10];       // JACK: For floodprots
250  	int				whensaidhead;       // Head value for floodprots
251  	double			lockedtill;
252 
253 	QFile			*upload;
254 	struct dstring_s *uploadfn;
255 	netadr_t		snap_from;
256 	qboolean		remote_snap;
257 
258 //===== NETWORK ============
259 	int				chokecount;
260 	netchan_t		netchan;
261 	int				msecs, msec_cheating;
262 	double			last_check;
263 	double			cuff_time;
264 	float			stdver;
265 } client_t;
266 
267 // a client can leave the server in one of four ways:
268 // dropping properly by quiting or disconnecting
269 // timing out if no valid messages are received for timeout->value seconds
270 // getting kicked off by the server operator
271 // a program error, like an overflowed reliable buffer
272 
273 extern qboolean rcon_from_user;			// current command is a from a user
274 
275 //============================================================================
276 
277 
278 #define	STATFRAMES	100
279 typedef struct {
280 	double	active;
281 	double	idle;
282 	double  demo;
283 	int		count;
284 	int		packets;
285 
286 	double	latched_active;
287 	double	latched_idle;
288 	double  latched_demo;
289 	int		latched_packets;
290 } svstats_t;
291 
292 // MAX_CHALLENGES is made large to prevent a denial
293 // of service attack that could cycle all of them
294 // out before legitimate users connected
295 #define	MAX_CHALLENGES	1024
296 
297 typedef struct {
298 	netadr_t	adr;
299 	int			challenge;
300 	int			time;
301 } challenge_t;
302 
303 typedef struct {
304 	int			spawncount;			// number of servers spawned since start,
305 									// used to check late spawns
306 	client_t	clients[MAX_CLIENTS];
307 	int			maxclients;
308 	int			num_clients;
309 	int			serverflags;		// episode completion information
310 	void		(*phys_client) (struct edict_s *ent, int num);
311 
312 	double		last_heartbeat;
313 	int			heartbeat_sequence;
314 	svstats_t	stats;
315 
316 	info_t		*info;
317 
318 	// log messages are used so that fraglog processes can get stats
319 	int			logsequence;	// the message currently being filled
320 	double		logtime;		// time of last swap
321 	sizebuf_t	log[2];
322 	byte		log_buf[2][MAX_DATAGRAM];
323 
324 	challenge_t	challenges[MAX_CHALLENGES];	// to prevent invalid IPs from connecting
325 
326 	// demo stuff
327 	byte        *demomem;
328 	int         demomemsize;
329 } server_static_t;
330 
331 //=============================================================================
332 // DoSflood protection
333 //=============================================================================
334 typedef struct {
335 	netadr_t	adr;
336 	double		issued;
337 	int			floodcount;
338 	int			cmdcount;
339 	double		firstseen;
340 } flood_t;
341 
342 typedef enum {
343 	FLOOD_PING,
344 	FLOOD_LOG,
345 	FLOOD_CONNECT,
346 	FLOOD_STATUS,
347 	FLOOD_RCON,
348 	FLOOD_BAN
349 } flood_enum_t;
350 
351 #define DOSFLOODCMDS 6
352 #define DOSFLOODIP 64   // remember latest 64 IP's for each cmd.
353 
354 //=============================================================================
355 
356 // edict->movetype values
357 #define	MOVETYPE_NONE			0		// never moves
358 #define	MOVETYPE_ANGLENOCLIP	1
359 #define	MOVETYPE_ANGLECLIP		2
360 #define	MOVETYPE_WALK			3		// gravity
361 #define	MOVETYPE_STEP			4		// gravity, special edge handling
362 #define	MOVETYPE_FLY			5
363 #define	MOVETYPE_TOSS			6		// gravity
364 #define	MOVETYPE_PUSH			7		// no clip to world, push and crush
365 #define	MOVETYPE_NOCLIP			8
366 #define	MOVETYPE_FLYMISSILE		9		// extra size to monsters
367 #define	MOVETYPE_BOUNCE			10
368 #define MOVETYPE_PPUSH                  13               // no clip to world, push and crush
369 
370 // edict->solid values
371 #define	SOLID_NOT				0		// no interaction with other objects
372 #define	SOLID_TRIGGER			1		// touch on edge, but not blocking
373 #define	SOLID_BBOX				2		// touch on edge, block
374 #define	SOLID_SLIDEBOX			3		// touch on edge, but not an onground
375 #define	SOLID_BSP				4		// bsp clip, touch on edge, block
376 
377 // edict->deadflag values
378 #define	DEAD_NO					0
379 #define	DEAD_DYING				1
380 #define	DEAD_DEAD				2
381 
382 #define	DAMAGE_NO				0
383 #define	DAMAGE_YES				1
384 #define	DAMAGE_AIM				2
385 
386 // edict->flags
387 #define	FL_FLY					(1<<0)
388 #define	FL_SWIM					(1<<1)
389 #define	FL_GLIMPSE				(1<<2)
390 #define	FL_CLIENT				(1<<3)
391 #define	FL_INWATER				(1<<4)
392 #define	FL_MONSTER				(1<<5)
393 #define	FL_GODMODE				(1<<6)
394 #define	FL_NOTARGET				(1<<7)
395 #define	FL_ITEM					(1<<8)
396 #define	FL_ONGROUND				(1<<9)
397 #define	FL_PARTIALGROUND		(1<<10)	// not all corners are valid
398 #define	FL_WATERJUMP			(1<<11)	// player jumping out of water
399 // 4096 used by quakec
400 #define FL_FINALIZED			(1<<13)
401 #define FL_FINDABLE_NONSOLID	(1<<14)
402 #define FLQW_LAGGEDMOVE			(1<<16)
403 
404 // entity effects
405 
406 //define	EF_BRIGHTFIELD			1
407 //define	EF_MUZZLEFLASH 			2
408 #define	EF_BRIGHTLIGHT 			4
409 #define	EF_DIMLIGHT 			8
410 
411 
412 #define	SPAWNFLAG_NOT_EASY			256
413 #define	SPAWNFLAG_NOT_MEDIUM		512
414 #define	SPAWNFLAG_NOT_HARD			1024
415 #define	SPAWNFLAG_NOT_DEATHMATCH	2048
416 
417 #define	MULTICAST_ALL			0
418 #define	MULTICAST_PHS			1
419 #define	MULTICAST_PVS			2
420 
421 #define	MULTICAST_ALL_R			3
422 #define	MULTICAST_PHS_R			4
423 #define	MULTICAST_PVS_R			5
424 
425 //============================================================================
426 // FIXME: declare exported variables in their own relevant .h
427 
428 extern	struct cvar_s	*sv_hide_version_info;
429 extern	struct cvar_s	*sv_highchars;
430 
431 extern	struct cvar_s	*sv_mintic, *sv_maxtic;
432 extern	struct cvar_s	*sv_maxspeed;
433 
434 extern	struct cvar_s	*sv_timeout;
435 
436 extern	netadr_t	master_adr[MAX_MASTERS];	// address of the master server
437 
438 extern	struct cvar_s	*spawn;
439 extern	struct cvar_s	*teamplay;
440 extern	struct cvar_s	*deathmatch;
441 extern	struct cvar_s	*fraglimit;
442 extern	struct cvar_s	*timelimit;
443 
444 extern	server_static_t	svs;				// persistant server info
445 extern	server_t		sv;					// local server
446 
447 extern	client_t	*host_client;
448 
449 extern	struct edict_s		*sv_player;
450 
451 extern	char		localmodels[MAX_MODELS][5];	// inline model names for precache
452 
453 extern	struct info_s	*localinfo;
454 
455 extern	int			host_hunklevel;
456 extern	QFile		*sv_logfile;
457 extern	QFile		*sv_fraglogfile;
458 
459 extern	double		sv_frametime;
460 extern	double		realtime;
461 
462 extern	const char *client_info_filters[];
463 
464 extern struct cbuf_s *sv_cbuf;
465 extern struct cbuf_args_s *sv_args;
466 
467 //===========================================================
468 // FIXME: declare exported functions in their own relevant .h
469 
470 void SV_Init (void);
471 void SV_Sbar_Init (void);
472 void SV_Progs_Init (void);
473 void SV_Progs_Init_Cvars (void);
474 void SV_PR_Cmds_Init (void);
475 void SV_LoadProgs (void);
476 
477 void Con_Printf (const char *fmt, ...) __attribute__((format(printf,1,2)));
478 void Con_DPrintf (const char *fmt, ...) __attribute__((format(printf,1,2)));
479 
480 extern struct clip_hull_s *pf_hull_list[];
481 
482 //
483 // sv_main.c
484 //
485 
486 client_t *SV_AllocClient (int spectator, int server);
487 
488 void SV_SavePenaltyFilter (client_t *cl, filtertype_t type, double pentime);
489 double SV_RestorePenaltyFilter (client_t *cl, filtertype_t type);
490 
491 void SV_Shutdown (void);
492 void SV_Frame (float time);
493 void SV_FinalMessage (const char *message);
494 void SV_DropClient (client_t *drop);
495 int SV_CalcPing (client_t *cl);
496 void SV_FullClientUpdate (client_t *client, sizebuf_t *buf);
497 void SV_FullClientUpdateToClient (client_t *client, backbuf_t *backbuf);
498 
499 int SV_ModelIndex (const char *name);
500 
501 qboolean SV_CheckBottom (struct edict_s *ent);
502 qboolean SV_movestep (struct edict_s *ent, const vec3_t move,
503 					  qboolean relink);
504 
505 void SV_WriteClientdataToMessage (client_t *client, sizebuf_t *msg);
506 
507 struct progs_s;
508 void SV_MoveToGoal (struct progs_s *pr);
509 
510 void SV_SaveSpawnparms (void);
511 
512 void SV_Physics_Client (struct edict_s	*ent);
513 
514 void SV_PreRunCmd (void);
515 void SV_RunCmd (usercmd_t *ucmd, qboolean inside);
516 void SV_PostRunCmd (void);
517 void SV_SetupUserCommands (void);
518 void SV_ExecuteUserCommand (const char *s);
519 void SV_InitOperatorCommands (void);
520 void SV_GIB_Init (void);
521 
522 void SV_SendServerinfo (client_t *client);
523 void SV_ExtractFromUserinfo (client_t *cl);
524 
525 
526 void Master_Heartbeat (void);
527 void Master_Packet (void);
528 
529 //
530 // sv_init.c
531 //
532 void SV_SpawnServer (const char *server);
533 void SV_FlushSignon (void);
534 
535 
536 //
537 // sv_phys.c
538 //
539 void SV_ProgStartFrame (void);
540 void SV_Physics (void);
541 void SV_CheckVelocity (struct edict_s *ent);
542 void SV_AddGravity (struct edict_s *ent);
543 void SV_FinishGravity (struct edict_s *ent, vec3_t move);
544 qboolean SV_RunThink (struct edict_s *ent);
545 void SV_Physics_Toss (struct edict_s *ent);
546 void SV_RunNewmis (void);
547 void SV_SetMoveVars(void);
548 struct trace_s;
549 int SV_FlyMove (struct edict_s *ent, float time, struct trace_s *steptrace);
550 struct trace_s SV_PushEntity (struct edict_s *ent, vec3_t push,
551 							  unsigned traceflags);
552 int SV_EntCanSupportJump (struct edict_s *ent);
553 
554 //
555 // sv_send.c
556 //
557 void SV_Print (const char *fmt, va_list args);
558 void SV_Printf (const char *fmt, ...) __attribute__((format(printf,1,2)));
559 void SV_SendClientMessages (void);
560 void SV_GetStats (struct edict_s *ent, int spectator, int stats[]);
561 
562 void SV_Multicast (const vec3_t origin, int to);
563 void SV_StartSound (struct edict_s *entity, int channel, const char *sample,
564 					int volume, float attenuation);
565 void SV_ClientPrintf (int recorder, client_t *cl, int level, const char *fmt, ...) __attribute__((format(printf,4,5)));
566 void SV_BroadcastPrintf (int level, const char *fmt, ...) __attribute__((format(printf,2,3)));
567 void SV_BroadcastCommand (const char *fmt, ...) __attribute__((format(printf,1,2)));
568 void SV_SendMessagesToAll (void);
569 void SV_FindModelNumbers (void);
570 
571 //
572 // sv_user.c
573 //
574 
575 #define UCMD_NO_REDIRECT 1
576 #define UCMD_OVERRIDABLE 2
577 
578 void SV_WriteWorldVars (netchan_t *netchan);
579 void SV_WriteSoundlist (netchan_t *netchan, int n);
580 void SV_WriteModellist (netchan_t *netchan, int n);
581 void SV_WriteSpawn1 (backbuf_t *backbuf, int n);
582 void SV_WriteSpawn2 (backbuf_t *backbuf);
583 void SV_ExecuteClientMessage (client_t *cl);
584 void SV_UserInit (void);
585 void SV_TogglePause (const char *msg);
586 void *SV_AddUserCommand (const char *name, void (*func) (void *userdata),
587 				         int flags,
588 				         void *userdata,
589 				         void (*on_free) (void *userdata));
590 int SV_RemoveUserCommand (void *cmd);
591 void SV_Spawn (client_t *client);
592 void SV_SetUserinfo (client_t *client, const char *key, const char *value);
593 extern int (*ucmd_unknown)(void);
594 
595 //
596 // svonly.c
597 //
598 typedef enum {
599 	RD_NONE,
600 	RD_CLIENT,
601 	RD_PACKET,
602 	RD_MOD,
603 } redirect_t;
604 
605 void SV_BeginRedirect (redirect_t rd);
606 void SV_EndRedirect (void);
607 extern redirect_t sv_redirected;
608 extern struct dstring_s outputbuf;
609 
610 //
611 // sv_ccmds.c
612 //
613 void SV_Status_f (void);
614 const char *SV_Current_Map (void);
615 void SV_SetLocalinfo (const char *key, const char *value);
616 
617 
618 //
619 // sv_ents.c
620 //
621 void SV_WriteEntitiesToClient (delta_t *delta, sizebuf_t *msg);
622 
623 //
624 // sv_nchan.c
625 //
626 
627 void Cvar_Info (struct cvar_s *var);
628 
629 extern struct cvar_s *sv_antilag;
630 extern struct cvar_s *sv_antilag_frac;
631 extern struct cvar_s *sv_timecheck_fuzz;
632 extern struct cvar_s *sv_timecheck_decay;
633 extern struct cvar_s *sv_maxrate;
634 extern struct cvar_s *sv_timestamps;
635 extern struct cvar_s *sv_timefmt;
636 extern struct cvar_s *sv_phs;
637 extern struct cvar_s *sv_maxvelocity;
638 extern struct cvar_s *sv_gravity;
639 extern struct cvar_s *sv_jump_any;
640 extern struct cvar_s *sv_aim;
641 extern struct cvar_s *sv_stopspeed;
642 extern struct cvar_s *sv_spectatormaxspeed;
643 extern struct cvar_s *sv_accelerate;
644 extern struct cvar_s *sv_airaccelerate;
645 extern struct cvar_s *sv_wateraccelerate;
646 extern struct cvar_s *sv_friction;
647 extern struct cvar_s *sv_waterfriction;
648 extern struct cvar_s *pr_double_remove;
649 extern struct cvar_s *allow_download;
650 extern struct cvar_s *allow_download_skins;
651 extern struct cvar_s *allow_download_models;
652 extern struct cvar_s *allow_download_sounds;
653 extern struct cvar_s *allow_download_maps;
654 
655 extern int fp_messages;
656 extern int fp_persecond;
657 extern int fp_secondsdead;
658 extern struct cvar_s *pausable;
659 extern qboolean nouse;
660 
661 extern char fp_msg[255];
662 
663 extern int sv_nailmodel, sv_supernailmodel, sv_playermodel;
664 
665 extern int con_printf_no_log;
666 
667 //FIXME location
668 #define STOP_EPSILON 0.1
669 
670 #endif // _SERVER_H
671