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 
21 #include "g_local.h"
22 
23 game_locals_t	game;
24 level_locals_t	level;
25 game_import_t	gi;
26 game_export_t	globals;
27 spawn_temp_t	st;
28 
29 int	sm_meat_index;
30 int	snd_fry;
31 int meansOfDeath;
32 
33 edict_t		*g_edicts;
34 
35 cvar_t	*deathmatch;
36 cvar_t	*coop;
37 cvar_t	*dmflags;
38 cvar_t	*skill;
39 cvar_t	*fraglimit;
40 cvar_t	*timelimit;
41 cvar_t	*password;
42 cvar_t	*spectator_password;
43 cvar_t	*needpass;
44 cvar_t	*maxclients;
45 cvar_t	*maxspectators;
46 cvar_t	*maxentities;
47 cvar_t	*g_select_empty;
48 cvar_t	*dedicated;
49 
50 cvar_t	*filterban;
51 
52 cvar_t	*sv_maxvelocity;
53 cvar_t	*sv_gravity;
54 
55 cvar_t	*sv_rollspeed;
56 cvar_t	*sv_rollangle;
57 cvar_t	*gun_x;
58 cvar_t	*gun_y;
59 cvar_t	*gun_z;
60 
61 cvar_t	*run_pitch;
62 cvar_t	*run_roll;
63 cvar_t	*bob_up;
64 cvar_t	*bob_pitch;
65 cvar_t	*bob_roll;
66 
67 cvar_t	*sv_cheats;
68 
69 cvar_t	*flood_msgs;
70 cvar_t	*flood_persecond;
71 cvar_t	*flood_waitdelay;
72 
73 cvar_t	*sv_maplist;
74 
75 void SpawnEntities (char *mapname, char *entities, char *spawnpoint);
76 void ClientThink (edict_t *ent, usercmd_t *cmd);
77 qboolean ClientConnect (edict_t *ent, char *userinfo);
78 void ClientUserinfoChanged (edict_t *ent, char *userinfo);
79 void ClientDisconnect (edict_t *ent);
80 void ClientBegin (edict_t *ent);
81 void ClientCommand (edict_t *ent);
82 void RunEntity (edict_t *ent);
83 void WriteGame (char *filename, qboolean autosave);
84 void ReadGame (char *filename);
85 void WriteLevel (char *filename);
86 void ReadLevel (char *filename);
87 void InitGame (void);
88 void G_RunFrame (void);
89 
90 
91 //===================================================================
92 
93 
ShutdownGame(void)94 void ShutdownGame (void)
95 {
96 	gi.dprintf ("==== ShutdownGame ====\n");
97 
98 	gi.FreeTags (TAG_LEVEL);
99 	gi.FreeTags (TAG_GAME);
100 }
101 
102 
103 /*
104 =================
105 GetGameAPI
106 
107 Returns a pointer to the structure with all entry points
108 and global variables
109 =================
110 */
GetGameAPI(game_import_t * import)111 game_export_t *GetGameAPI (game_import_t *import)
112 {
113 	gi = *import;
114 
115 	globals.apiversion = GAME_API_VERSION;
116 	globals.Init = InitGame;
117 	globals.Shutdown = ShutdownGame;
118 	globals.SpawnEntities = SpawnEntities;
119 
120 	globals.WriteGame = WriteGame;
121 	globals.ReadGame = ReadGame;
122 	globals.WriteLevel = WriteLevel;
123 	globals.ReadLevel = ReadLevel;
124 
125 	globals.ClientThink = ClientThink;
126 	globals.ClientConnect = ClientConnect;
127 	globals.ClientUserinfoChanged = ClientUserinfoChanged;
128 	globals.ClientDisconnect = ClientDisconnect;
129 	globals.ClientBegin = ClientBegin;
130 	globals.ClientCommand = ClientCommand;
131 
132 	globals.RunFrame = G_RunFrame;
133 
134 	globals.ServerCommand = ServerCommand;
135 
136 	globals.edict_size = sizeof(edict_t);
137 
138 	return &globals;
139 }
140 
141 #ifndef GAME_HARD_LINKED
142 
143 // this is only here so the functions in q_shared.c can link
Com_Printf(const char * fmt,...)144 void Com_Printf( const char *fmt, ... ) {
145 	va_list		argptr;
146 	char		text[MAX_STRING_CHARS];
147 
148 	va_start( argptr, fmt );
149 	Q_vsnprintf( text, sizeof( text ), fmt, argptr );
150 	va_end( argptr );
151 
152 	gi.dprintf( "%s", text );
153 }
154 
Com_DPrintf(const char * fmt,...)155 void Com_DPrintf( const char *fmt, ... ) {
156 }
157 
Com_WPrintf(const char * fmt,...)158 void Com_WPrintf( const char *fmt, ... ) {
159 	va_list		argptr;
160 	char		text[MAX_STRING_CHARS];
161 
162 	va_start( argptr, fmt );
163 	Q_vsnprintf( text, sizeof( text ), fmt, argptr );
164 	va_end( argptr );
165 
166 	gi.dprintf( "WARNING: %s", text );
167 }
168 
Com_EPrintf(const char * fmt,...)169 void Com_EPrintf( const char *fmt, ... ) {
170 	va_list		argptr;
171 	char		text[MAX_STRING_CHARS];
172 
173 	va_start( argptr, fmt );
174 	Q_vsnprintf( text, sizeof( text ), fmt, argptr );
175 	va_end( argptr );
176 
177 	gi.dprintf( "ERROR: %s", text );
178 }
179 
Com_Error(comErrorType_t err_level,const char * error,...)180 void Com_Error( comErrorType_t err_level, const char *error, ... ) {
181 	va_list		argptr;
182 	char		text[MAX_STRING_CHARS];
183 
184 	va_start( argptr, error );
185 	Q_vsnprintf( text, sizeof( text ), error, argptr );
186 	va_end( argptr );
187 
188 	gi.error( "%s", text );
189 }
190 
191 #endif
192 
193 //======================================================================
194 
195 
196 /*
197 =================
198 ClientEndServerFrames
199 =================
200 */
ClientEndServerFrames(void)201 void ClientEndServerFrames (void)
202 {
203 	int		i;
204 	edict_t	*ent;
205 
206 	// calc the player views now that all pushing
207 	// and damage has been added
208 	for (i=0 ; i<maxclients->value ; i++)
209 	{
210 		ent = g_edicts + 1 + i;
211 		if (!ent->inuse || !ent->client)
212 			continue;
213 		ClientEndServerFrame (ent);
214 	}
215 
216 }
217 
218 /*
219 =================
220 CreateTargetChangeLevel
221 
222 Returns the created target changelevel
223 =================
224 */
CreateTargetChangeLevel(char * map)225 edict_t *CreateTargetChangeLevel(char *map)
226 {
227 	edict_t *ent;
228 
229 	ent = G_Spawn ();
230 	ent->classname = "target_changelevel";
231 	Com_sprintf(level.nextmap, sizeof(level.nextmap), "%s", map);
232 	ent->map = level.nextmap;
233 	return ent;
234 }
235 
CopyString(const char * in)236 char *CopyString( const char *in ) {
237 	char	*out;
238 	int length;
239 
240 	if( !in ) {
241 		return NULL;
242 	}
243 
244 	length = strlen( in ) + 1;
245 
246 	out = gi.TagMalloc( TAG_GAME, length );
247 	strcpy( out, in );
248 
249 	return out;
250 }
251 
252 /*
253 =================
254 EndDMLevel
255 
256 The timelimit or fraglimit has been exceeded
257 =================
258 */
EndDMLevel(void)259 void EndDMLevel (void)
260 {
261 	edict_t		*ent;
262 	char *s, *t, *f;
263 	static const char *seps = " ,\n\r";
264 
265 	// stay on same level flag
266 	if ((int)dmflags->value & DF_SAME_LEVEL)
267 	{
268 		BeginIntermission (CreateTargetChangeLevel (level.mapname) );
269 		return;
270 	}
271 
272 	// see if it's in the map list
273 	if (*sv_maplist->string) {
274 		s = CopyString(sv_maplist->string);
275 		f = NULL;
276 		t = strtok(s, seps);
277 		while (t != NULL) {
278 			if (Q_stricmp(t, level.mapname) == 0) {
279 				// it's in the list, go to the next one
280 				t = strtok(NULL, seps);
281 				if (t == NULL) { // end of list, go to first one
282 					if (f == NULL) // there isn't a first one, same level
283 						BeginIntermission (CreateTargetChangeLevel (level.mapname) );
284 					else
285 						BeginIntermission (CreateTargetChangeLevel (f) );
286 				} else
287 					BeginIntermission (CreateTargetChangeLevel (t) );
288 				gi.TagFree(s);
289 				return;
290 			}
291 			if (!f)
292 				f = t;
293 			t = strtok(NULL, seps);
294 		}
295 		gi.TagFree(s);
296 	}
297 
298 	if (level.nextmap[0]) // go to a specific map
299 		BeginIntermission (CreateTargetChangeLevel (level.nextmap) );
300 	else {	// search for a changelevel
301 		ent = G_Find (NULL, FOFS(classname), "target_changelevel");
302 		if (!ent)
303 		{	// the map designer didn't include a changelevel,
304 			// so create a fake ent that goes back to the same level
305 			BeginIntermission (CreateTargetChangeLevel (level.mapname) );
306 			return;
307 		}
308 		BeginIntermission (ent);
309 	}
310 }
311 
312 
313 /*
314 =================
315 CheckNeedPass
316 =================
317 */
CheckNeedPass(void)318 void CheckNeedPass (void)
319 {
320 	int need;
321 
322 	// if password or spectator_password has changed, update needpass
323 	// as needed
324 	if (password->modified || spectator_password->modified)
325 	{
326 		password->modified = spectator_password->modified = false;
327 
328 		need = 0;
329 
330 		if (*password->string && Q_stricmp(password->string, "none"))
331 			need |= 1;
332 		if (*spectator_password->string && Q_stricmp(spectator_password->string, "none"))
333 			need |= 2;
334 
335 		gi.cvar_set("needpass", va("%d", need));
336 	}
337 }
338 
339 /*
340 =================
341 CheckDMRules
342 =================
343 */
CheckDMRules(void)344 void CheckDMRules (void)
345 {
346 	int			i;
347 	gclient_t	*cl;
348 
349 	if (level.intermissiontime)
350 		return;
351 
352 	if (!deathmatch->value)
353 		return;
354 
355 	if (timelimit->value)
356 	{
357 		if (level.time >= timelimit->value*60)
358 		{
359 			gi.bprintf (PRINT_HIGH, "Timelimit hit.\n");
360 			EndDMLevel ();
361 			return;
362 		}
363 	}
364 
365 	if (fraglimit->value)
366 	{
367 		for (i=0 ; i<maxclients->value ; i++)
368 		{
369 			cl = game.clients + i;
370 			if (!g_edicts[i+1].inuse)
371 				continue;
372 
373 			if (cl->resp.score >= fraglimit->value)
374 			{
375 				gi.bprintf (PRINT_HIGH, "Fraglimit hit.\n");
376 				EndDMLevel ();
377 				return;
378 			}
379 		}
380 	}
381 }
382 
383 
384 /*
385 =============
386 ExitLevel
387 =============
388 */
ExitLevel(void)389 void ExitLevel (void)
390 {
391 	int		i;
392 	edict_t	*ent;
393 	char	command [256];
394 
395 	Com_sprintf (command, sizeof(command), "gamemap \"%s\"\n", level.changemap);
396 	gi.AddCommandString (command);
397 	level.changemap = NULL;
398 	level.exitintermission = 0;
399 	level.intermissiontime = 0;
400 	ClientEndServerFrames ();
401 
402 	// clear some things before going to next level
403 	for (i=0 ; i<maxclients->value ; i++)
404 	{
405 		ent = g_edicts + 1 + i;
406 		if (!ent->inuse)
407 			continue;
408 		if (ent->health > ent->client->pers.max_health)
409 			ent->health = ent->client->pers.max_health;
410 	}
411 
412 }
413 
414 /*
415 ================
416 G_RunFrame
417 
418 Advances the world by 0.1 seconds
419 ================
420 */
G_RunFrame(void)421 void G_RunFrame (void)
422 {
423 	int		i;
424 	edict_t	*ent;
425 
426 	level.framenum++;
427 	level.time = level.framenum*FRAMETIME;
428 
429 	// choose a client for monsters to target this frame
430 	AI_SetSightClient ();
431 
432 	// exit intermissions
433 
434 	if (level.exitintermission)
435 	{
436 		ExitLevel ();
437 		return;
438 	}
439 
440 	//
441 	// treat each object in turn
442 	// even the world gets a chance to think
443 	//
444 	ent = &g_edicts[0];
445 	for (i=0 ; i<globals.num_edicts ; i++, ent++)
446 	{
447 		if (!ent->inuse)
448 			continue;
449 
450 		level.current_entity = ent;
451 
452 		VectorCopy (ent->s.origin, ent->s.old_origin);
453 
454 		// if the ground entity moved, make sure we are still on it
455 		if ((ent->groundentity) && (ent->groundentity->linkcount != ent->groundentity_linkcount))
456 		{
457 			ent->groundentity = NULL;
458 			if ( !(ent->flags & (FL_SWIM|FL_FLY)) && (ent->svflags & SVF_MONSTER) )
459 			{
460 				M_CheckGround (ent);
461 			}
462 		}
463 
464 		if (i > 0 && i <= maxclients->value)
465 		{
466 			ClientBeginServerFrame (ent);
467 			continue;
468 		}
469 
470 		G_RunEntity (ent);
471 	}
472 
473 	// see if it is time to end a deathmatch
474 	CheckDMRules ();
475 
476 	// see if needpass needs updated
477 	CheckNeedPass ();
478 
479 	// build the playerstate_t structures for all players
480 	ClientEndServerFrames ();
481 }
482 
483