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