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