1 /*
2  * Copyright (C) 1997-2001 Id Software, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * 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 along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59
17  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  */
20 /* server.h */
21 
22 #include "../qcommon/qcommon.h"
23 #include "../game/game.h"
24 
25 /*
26  * ===========================================================================
27  * ==
28  */
29 
30 #define	MAX_MASTERS	8	/* max recipients for heartbeat packets */
31 
32 typedef enum {
33 	ss_dead,		/* no map loaded */
34 	ss_loading,		/* spawning level edicts */
35 	ss_game,		/* actively running */
36 	ss_cinematic,
37 	ss_demo,
38 	ss_pic
39 } server_state_t;
40 
41 /* some qc commands are only valid before the server has finished */
42 /* initializing (precache commands, static sounds / objects, etc) */
43 
44 typedef struct {
45 	server_state_t	state;	/* precache commands are only valid during load */
46 
47 	qboolean	attractloop;	/* running cinematics and demos for the local system only */
48 	qboolean	loadgame;	/* client begins should reuse existing entity */
49 
50 	unsigned	time;	/* always sv.framenum * 100 msec */
51 	int		framenum;
52 
53 	char		name[MAX_QPATH];	/* map name, or cinematic name */
54 	struct cmodel_s *models[MAX_MODELS];
55 
56 	char		configstrings[MAX_CONFIGSTRINGS][MAX_QPATH];
57 	entity_state_t	baselines[MAX_EDICTS];
58 
59 	/* the multicast buffer is used to send a message to a set of clients */
60 	/* it is only used to marshall data until SV_Multicast is called */
61 	sizebuf_t	multicast;
62 	byte		multicast_buf[MAX_MSGLEN];
63 
64 	/* demo server information */
65 	fileHandle_t	demofile;
66 	qboolean	timedemo;	/* don't time sync */
67 } server_t;
68 
69 #define EDICT_NUM(n) ((edict_t *)((byte *)ge->edicts + ge->edict_size*(n)))
70 #define NUM_FOR_EDICT(e) ( ((byte *)(e)-(byte *)ge->edicts ) / ge->edict_size)
71 
72 
73 typedef enum {
74 	cs_free,		/* can be reused for a new connection */
75 	cs_zombie,		/* client has been disconnected, but don't reuse */
76 				/* connection for a couple seconds */
77 	cs_connected,		/* has been assigned to a client_t, but not in game yet */
78 	cs_spawned		/* client is fully in game */
79 } client_state_t;
80 
81 typedef struct {
82 	int		areabytes;
83 	byte		areabits  [MAX_MAP_AREAS / 8];	/* portalarea visibility bits */
84 	player_state_t	ps;
85 	int		num_entities;
86 	int		first_entity;	/* into the circular sv_packet_entities[] */
87 	int		senttime;	/* for ping calculations */
88 } client_frame_t;
89 
90 #define	LATENCY_COUNTS	16
91 #define	RATE_MESSAGES	10
92 
93 typedef struct client_s {
94 	client_state_t	state;
95 
96 	char		userinfo  [MAX_INFO_STRING];	/* name, etc */
97 
98 	int		lastframe;	/* for delta compression */
99 	usercmd_t	lastcmd;/* for filling in big drops */
100 
101 	int		commandMsec;	/* every seconds this is reset, if user */
102 	/* commands exhaust it, assume time cheating */
103 
104 	int		frame_latency[LATENCY_COUNTS];
105 	int		ping;
106 
107 	int		message_size[RATE_MESSAGES];	/* used to rate drop packets */
108 	int		rate;
109 	int		surpressCount;	/* number of messages rate supressed */
110 
111 	edict_t        *edict;		/* EDICT_NUM(clientnum+1) */
112 	char		name      [32];	/* extracted from userinfo, high bits masked */
113 	int		messagelevel;	/* for filtering printed messages */
114 
115 	/* The datagram is written to by sound calls, prints, temp ents, etc. */
116 	/* It can be harmlessly overflowed. */
117 	sizebuf_t	datagram;
118 	byte		datagram_buf[MAX_MSGLEN];
119 
120 	client_frame_t	frames[UPDATE_BACKUP];	/* updates can be delta'd from here */
121 
122 	byte           *download;	/* file being downloaded */
123 	int		downloadsize;	/* total bytes (can't use EOF because of paks) */
124 	int		downloadcount;	/* bytes sent */
125 
126 	int		lastmessage;	/* sv.framenum when packet was last received */
127 	int		lastconnect;
128 
129 	int		challenge;	/* challenge of this user, randomly generated */
130 
131 	netchan_t	netchan;
132 } client_t;
133 
134 /* a client can leave the server in one of four ways: */
135 /* dropping properly by quiting or disconnecting */
136 /* timing out if no valid messages are received for timeout.value seconds */
137 /* getting kicked off by the server operator */
138 /* a program error, like an overflowed reliable buffer */
139 
140 /*
141  * ===========================================================================
142  */
143 
144 /* MAX_CHALLENGES is made large to prevent a denial */
145 /* of service attack that could cycle all of them */
146 /* out before legitimate users connected */
147 #define	MAX_CHALLENGES	1024
148 
149 typedef struct {
150 	netadr_t	adr;
151 	int		challenge;
152 	int		time;
153 } challenge_t;
154 
155 
156 typedef struct {
157 	qboolean	initialized;	/* sv_init has completed */
158 	int		realtime;	/* always increasing, no clamping, etc */
159 
160 	char		mapcmd[MAX_TOKEN_CHARS];	/* ie: *intro.cin+base  */
161 
162 	int		spawncount;	/* incremented each server start */
163 
164 	/* used to check late spawns */
165 	client_t       *clients;		/* [maxclients->value]; */
166 	int		num_client_entities;	/* maxclients->value*UPDATE_BA
167 						 * CKUP*MAX_PACKET_ENTITIES */
168 	int		next_client_entities;	/* next client_entity to use */
169 	entity_state_t *client_entities;	/* [num_client_entities] */
170 
171 	int		last_heartbeat;
172 
173 	challenge_t	challenges[MAX_CHALLENGES];	/* to prevent invalid
174 							 * IPs from connecting */
175 
176 	/* serverrecord values */
177 	FILE           *demofile;
178 	sizebuf_t	demo_multicast;
179 	byte		demo_multicast_buf[MAX_MSGLEN];
180 } server_static_t;
181 
182 /*
183  * ===========================================================================
184  */
185 
186 extern netadr_t	master_adr[MAX_MASTERS];	/* address of the master server */
187 
188 extern server_static_t svs;	/* persistant server info */
189 extern server_t	sv;		/* local server */
190 
191 extern cvar_t  *sv_paused;
192 extern cvar_t  *maxclients;
193 extern cvar_t  *sv_noreload;	/* don't reload level state when reentering */
194 extern cvar_t  *sv_airaccelerate;	/* don't reload level state when reentering */
195 
196 /* development tool */
197 extern cvar_t  *sv_enforcetime;
198 
199 extern client_t *sv_client;
200 extern edict_t *sv_player;
201 
202 /* =========================================================== */
203 
204 //
205 /* sv_main.c */
206 //
207 
208 extern cvar_t	*sv_killserver;
209 
210 void		SV_FinalMessage(char *message, qboolean reconnect);
211 void		SV_DropClient(client_t * drop);
212 
213 int		SV_ModelIndex(char *name);
214 int		SV_SoundIndex(char *name);
215 int		SV_ImageIndex(char *name);
216 
217 void		SV_WriteClientdataToMessage(client_t * client, sizebuf_t * msg);
218 
219 void		SV_ExecuteUserCommand(char *s);
220 void		SV_InitOperatorCommands(void);
221 
222 void		SV_SendServerinfo(client_t * client);
223 void		SV_UserinfoChanged(client_t * cl);
224 
225 
226 void		Master_Heartbeat(void);
227 void		Master_Packet(void);
228 
229 //
230 /* sv_init.c */
231 //
232 void		SV_InitGame(void);
233 void		SV_Map    (qboolean attractloop, char *levelstring, qboolean loadgame);
234 
235 
236 //
237 /* sv_phys.c */
238 //
239 void		SV_PrepWorldFrame(void);
240 
241 //
242 /* sv_send.c */
243 //
244 typedef enum {
245 	RD_NONE, RD_CLIENT, RD_PACKET
246 } redirect_t;
247 
248 #define	SV_OUTPUTBUF_LENGTH	(MAX_MSGLEN - 16)
249 
250 extern char	sv_outputbuf[SV_OUTPUTBUF_LENGTH];
251 
252 void		SV_FlushRedirect(int sv_redirected, char *outputbuf);
253 
254 void		SV_DemoCompleted(void);
255 void		SV_SendClientMessages(void);
256 
257 void		SV_Multicast(vec3_t origin, multicast_t to);
258 void
259 SV_StartSound(vec3_t origin, edict_t * entity, int channel,
260     int soundindex, float volume,
261     float attenuation, float timeofs);
262 void		SV_ClientPrintf(client_t * cl, int level, char *fmt,...);
263 void		SV_BroadcastPrintf(int level, char *fmt,...);
264 void		SV_BroadcastCommand(char *fmt,...);
265 
266 /* sv_user.c */
267 extern cvar_t  *allow_download;
268 extern cvar_t  *allow_download_players;
269 extern cvar_t  *allow_download_models;
270 extern cvar_t  *allow_download_sounds;
271 extern cvar_t  *allow_download_maps;
272 
273 void		SV_Nextserver(void);
274 void		SV_ExecuteClientMessage(client_t * cl);
275 
276 /* sv_ccmds.c */
277 void		SV_ReadLevelFile(void);
278 void		SV_Status_f(void);
279 
280 /* sv_ents.c */
281 void		SV_WriteFrameToClient(client_t * client, sizebuf_t * msg);
282 void		SV_RecordDemoMessage(void);
283 void		SV_BuildClientFrame(client_t * client);
284 void		SV_Error  (char *error,...);
285 
286 /* sv_game.c */
287 extern game_export_t *ge;
288 
289 void		SV_InitGameProgs(void);
290 void		SV_ShutdownGameProgs(void);
291 void		SV_InitEdict(edict_t * e);
292 
293 
294 
295 /* ============================================================ */
296 
297 //
298 /* high level object sorting to reduce interaction tests */
299 //
300 
301 void		SV_ClearWorld(void);
302 
303 /* called after the world model has been loaded, before linking any entities */
304 
305 void		SV_UnlinkEdict(edict_t * ent);
306 
307 /* call before removing an entity, and before trying to move one, */
308 /* so it doesn't clip against itself */
309 
310 void		SV_LinkEdict(edict_t * ent);
311 
312 /* Needs to be called any time an entity changes origin, mins, maxs, */
313 /* or solid.  Automatically unlinks if needed. */
314 /* sets ent->v.absmin and ent->v.absmax */
315 /* sets ent->leafnums[] for pvs determination even if the entity */
316 /* is not solid */
317 
318 int		SV_AreaEdicts(vec3_t mins, vec3_t maxs, edict_t ** list, int maxcount, int areatype);
319 
320 /* fills in a table of edict pointers with edicts that have bounding boxes */
321 /* that intersect the given area.  It is possible for a non-axial bmodel */
322 /* to be returned that doesn't actually intersect the area on an exact */
323 /* test. */
324 /* returns the number of pointers filled in */
325 /* ??? does this always return the world? */
326 
327 /* =================================================================== */
328 
329 //
330 /* functions that interact with everything apropriate */
331 //
332 int		SV_PointContents(vec3_t p);
333 
334 /* returns the CONTENTS_* value from the world at the given point. */
335 /* Quake 2 extends this to also check entities, to allow moving liquids */
336 
337 
338 trace_t		SV_Trace(vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, edict_t * passedict, int contentmask);
339 
340 /* mins and maxs are relative */
341 
342 /* if the entire move stays in a solid volume, trace.allsolid will be set, */
343 /* trace.startsolid will be set, and trace.fraction will be 0 */
344 
345 /* if the starting point is in a solid, it will be allowed to move out */
346 /* to an open area */
347 
348 /* passedict is explicitly excluded from clipping checks (normally NULL) */
349