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