Debian Codegraph @

code/game/g_main.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23
24 #include "g_local.h"
25
26 level_locals_t level;
27
28 typedef struct {
29 vmCvar_t *vmCvar;
30 char *cvarName;
31 char *defaultString;
32 int cvarFlags;
33 int modificationCount; // for tracking changes
34 qboolean trackChange; // track this variable, and announce if changed
35 qboolean teamShader; // track and if changed, update shader state
36 } cvarTable_t;
37
38 gentity_t g_entities[MAX_GENTITIES];
39 gclient_t g_clients[MAX_CLIENTS];
40
41 vmCvar_t g_gametype;
42 vmCvar_t g_dmflags;
43 vmCvar_t g_fraglimit;
44 vmCvar_t g_timelimit;
45 vmCvar_t g_capturelimit;
46 vmCvar_t g_friendlyFire;
47 vmCvar_t g_password;
48 vmCvar_t g_needpass;
49 vmCvar_t g_maxclients;
50 vmCvar_t g_maxGameClients;
51 vmCvar_t g_dedicated;
52 vmCvar_t g_speed;
53 vmCvar_t g_gravity;
54 vmCvar_t g_cheats;
55 vmCvar_t g_knockback;
56 vmCvar_t g_quadfactor;
57 vmCvar_t g_forcerespawn;
58 vmCvar_t g_inactivity;
59 vmCvar_t g_debugMove;
60 vmCvar_t g_debugDamage;
61 vmCvar_t g_debugAlloc;
62 vmCvar_t g_weaponRespawn;
63 vmCvar_t g_weaponTeamRespawn;
64 vmCvar_t g_motd;
65 vmCvar_t g_synchronousClients;
66 vmCvar_t g_warmup;
67 vmCvar_t g_doWarmup;
68 vmCvar_t g_restarted;
69 vmCvar_t g_logfile;
70 vmCvar_t g_logfileSync;
71 vmCvar_t g_blood;
72 vmCvar_t g_podiumDist;
73 vmCvar_t g_podiumDrop;
74 vmCvar_t g_allowVote;
75 vmCvar_t g_teamAutoJoin;
76 vmCvar_t g_teamForceBalance;
77 vmCvar_t g_banIPs;
78 vmCvar_t g_filterBan;
79 vmCvar_t g_smoothClients;
80 vmCvar_t pmove_fixed;
81 vmCvar_t pmove_msec;
82 vmCvar_t g_rankings;
83 vmCvar_t g_listEntity;
84 vmCvar_t g_localTeamPref;
85 #ifdef MISSIONPACK
86 vmCvar_t g_obeliskHealth;
87 vmCvar_t g_obeliskRegenPeriod;
88 vmCvar_t g_obeliskRegenAmount;
89 vmCvar_t g_obeliskRespawnDelay;
90 vmCvar_t g_cubeTimeout;
91 vmCvar_t g_redteam;
92 vmCvar_t g_blueteam;
93 vmCvar_t g_singlePlayer;
94 vmCvar_t g_enableDust;
95 vmCvar_t g_enableBreath;
96 vmCvar_t g_proxMineTimeout;
97 #endif
98
99 static cvarTable_t gameCvarTable[] = {
100 // don't override the cheat state set by the system
101 { &g_cheats, "sv_cheats", "", 0, 0, qfalse },
102
103 // noset vars
104 { NULL, "gamename", GAMEVERSION , CVAR_SERVERINFO | CVAR_ROM, 0, qfalse },
105 { NULL, "gamedate", PRODUCT_DATE , CVAR_ROM, 0, qfalse },
106 { &g_restarted, "g_restarted", "0", CVAR_ROM, 0, qfalse },
107
108 // latched vars
109 { &g_gametype, "g_gametype", "0", CVAR_SERVERINFO | CVAR_USERINFO | CVAR_LATCH, 0, qfalse },
110
111 { &g_maxclients, "sv_maxclients", "8", CVAR_SERVERINFO | CVAR_LATCH | CVAR_ARCHIVE, 0, qfalse },
112 { &g_maxGameClients, "g_maxGameClients", "0", CVAR_SERVERINFO | CVAR_LATCH | CVAR_ARCHIVE, 0, qfalse },
113
114 // change anytime vars
115 { &g_dmflags, "dmflags", "0", CVAR_SERVERINFO | CVAR_ARCHIVE, 0, qtrue },
116 { &g_fraglimit, "fraglimit", "20", CVAR_SERVERINFO | CVAR_ARCHIVE | CVAR_NORESTART, 0, qtrue },
117 { &g_timelimit, "timelimit", "0", CVAR_SERVERINFO | CVAR_ARCHIVE | CVAR_NORESTART, 0, qtrue },
118 { &g_capturelimit, "capturelimit", "8", CVAR_SERVERINFO | CVAR_ARCHIVE | CVAR_NORESTART, 0, qtrue },
119
120 { &g_synchronousClients, "g_synchronousClients", "0", CVAR_SYSTEMINFO, 0, qfalse },
121
122 { &g_friendlyFire, "g_friendlyFire", "0", CVAR_ARCHIVE, 0, qtrue },
123
124 { &g_teamAutoJoin, "g_teamAutoJoin", "0", CVAR_ARCHIVE },
125 { &g_teamForceBalance, "g_teamForceBalance", "0", CVAR_ARCHIVE },
126
127 { &g_warmup, "g_warmup", "20", CVAR_ARCHIVE, 0, qtrue },
128 { &g_doWarmup, "g_doWarmup", "0", CVAR_ARCHIVE, 0, qtrue },
129 { &g_logfile, "g_log", "games.log", CVAR_ARCHIVE, 0, qfalse },
130 { &g_logfileSync, "g_logsync", "0", CVAR_ARCHIVE, 0, qfalse },
131
132 { &g_password, "g_password", "", CVAR_USERINFO, 0, qfalse },
133
134 { &g_banIPs, "g_banIPs", "", CVAR_ARCHIVE, 0, qfalse },
135 { &g_filterBan, "g_filterBan", "1", CVAR_ARCHIVE, 0, qfalse },
136
137 { &g_needpass, "g_needpass", "0", CVAR_SERVERINFO | CVAR_ROM, 0, qfalse },
138
139 { &g_dedicated, "dedicated", "0", 0, 0, qfalse },
140
141 { &g_speed, "g_speed", "320", 0, 0, qtrue },
142 { &g_gravity, "g_gravity", "800", 0, 0, qtrue },
143 { &g_knockback, "g_knockback", "1000", 0, 0, qtrue },
144 { &g_quadfactor, "g_quadfactor", "3", 0, 0, qtrue },
145 { &g_weaponRespawn, "g_weaponrespawn", "5", 0, 0, qtrue },
146 { &g_weaponTeamRespawn, "g_weaponTeamRespawn", "30", 0, 0, qtrue },
147 { &g_forcerespawn, "g_forcerespawn", "20", 0, 0, qtrue },
148 { &g_inactivity, "g_inactivity", "0", 0, 0, qtrue },
149 { &g_debugMove, "g_debugMove", "0", 0, 0, qfalse },
150 { &g_debugDamage, "g_debugDamage", "0", 0, 0, qfalse },
151 { &g_debugAlloc, "g_debugAlloc", "0", 0, 0, qfalse },
152 { &g_motd, "g_motd", "", 0, 0, qfalse },
153 { &g_blood, "com_blood", "1", 0, 0, qfalse },
154
155 { &g_podiumDist, "g_podiumDist", "80", 0, 0, qfalse },
156 { &g_podiumDrop, "g_podiumDrop", "70", 0, 0, qfalse },
157
158 { &g_allowVote, "g_allowVote", "1", CVAR_ARCHIVE, 0, qfalse },
159 { &g_listEntity, "g_listEntity", "0", 0, 0, qfalse },
160
161 #ifdef MISSIONPACK
162 { &g_obeliskHealth, "g_obeliskHealth", "2500", 0, 0, qfalse },
163 { &g_obeliskRegenPeriod, "g_obeliskRegenPeriod", "1", 0, 0, qfalse },
164 { &g_obeliskRegenAmount, "g_obeliskRegenAmount", "15", 0, 0, qfalse },
165 { &g_obeliskRespawnDelay, "g_obeliskRespawnDelay", "10", CVAR_SERVERINFO, 0, qfalse },
166
167 { &g_cubeTimeout, "g_cubeTimeout", "30", 0, 0, qfalse },
168 { &g_redteam, "g_redteam", "Stroggs", CVAR_ARCHIVE | CVAR_SERVERINFO | CVAR_USERINFO , 0, qtrue, qtrue },
169 { &g_blueteam, "g_blueteam", "Pagans", CVAR_ARCHIVE | CVAR_SERVERINFO | CVAR_USERINFO , 0, qtrue, qtrue },
170 { &g_singlePlayer, "ui_singlePlayerActive", "", 0, 0, qfalse, qfalse },
171
172 { &g_enableDust, "g_enableDust", "0", CVAR_SERVERINFO, 0, qtrue, qfalse },
173 { &g_enableBreath, "g_enableBreath", "0", CVAR_SERVERINFO, 0, qtrue, qfalse },
174 { &g_proxMineTimeout, "g_proxMineTimeout", "20000", 0, 0, qfalse },
175 #endif
176 { &g_smoothClients, "g_smoothClients", "1", 0, 0, qfalse},
177 { &pmove_fixed, "pmove_fixed", "0", CVAR_SYSTEMINFO, 0, qfalse},
178 { &pmove_msec, "pmove_msec", "8", CVAR_SYSTEMINFO, 0, qfalse},
179
180 { &g_rankings, "g_rankings", "0", 0, 0, qfalse},
181 { &g_localTeamPref, "g_localTeamPref", "", 0, 0, qfalse }
182
183 };
184
185 static int gameCvarTableSize = ARRAY_LEN( gameCvarTable );
186
187
188 void G_InitGame( int levelTime, int randomSeed, int restart );
189 void G_RunFrame( int levelTime );
190 void G_ShutdownGame( int restart );
191 void CheckExitRules( void );
192
193
194 /*
195 ================
196 vmMain
197
198 This is the only way control passes into the module.
199 This must be the very first function compiled into the .q3vm file
200 ================
201 */
202 Q_EXPORT intptr_t vmMain( int command, int arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8, int arg9, int arg10, int arg11 ) {
203 switch ( command ) {
204 case GAME_INIT:
205 G_InitGame( arg0, arg1, arg2 );
206 return 0;
207 case GAME_SHUTDOWN:
208 G_ShutdownGame( arg0 );
209 return 0;
210 case GAME_CLIENT_CONNECT:
211 return (intptr_t)ClientConnect( arg0, arg1, arg2 );
212 case GAME_CLIENT_THINK:
213 ClientThink( arg0 );
214 return 0;
215 case GAME_CLIENT_USERINFO_CHANGED:
216 ClientUserinfoChanged( arg0 );
217 return 0;
218 case GAME_CLIENT_DISCONNECT:
219 ClientDisconnect( arg0 );
220 return 0;
221 case GAME_CLIENT_BEGIN:
222 ClientBegin( arg0 );
223 return 0;
224 case GAME_CLIENT_COMMAND:
225 ClientCommand( arg0 );
226 return 0;
227 case GAME_RUN_FRAME:
228 G_RunFrame( arg0 );
229 return 0;
230 case GAME_CONSOLE_COMMAND:
231 return ConsoleCommand();
232 case BOTAI_START_FRAME:
233 return BotAIStartFrame( arg0 );
234 }
235
236 return -1;
237 }
238
239
240 void QDECL G_Printf( const char *fmt, ... ) {
241 va_list argptr;
242 char text[1024];
243
244 va_start (argptr, fmt);
245 Q_vsnprintf (text, sizeof(text), fmt, argptr);
246 va_end (argptr);
247
248 trap_Print( text );
249 }
250
251 void QDECL G_Error( const char *fmt, ... ) {
252 va_list argptr;
253 char text[1024];
254
255 va_start (argptr, fmt);
256 Q_vsnprintf (text, sizeof(text), fmt, argptr);
257 va_end (argptr);
258
259 trap_Error( text );
260 }
261
262 /*
263 ================
264 G_FindTeams
265
266 Chain together all entities with a matching team field.
267 Entity teams are used for item groups and multi-entity mover groups.
268
269 All but the first will have the FL_TEAMMEMBER flag set and teammaster field set
270 All but the last will have the teamchain field set to the next one
271 ================
272 */
273 void G_FindTeams( void ) {
274 gentity_t *e, *e2;
275 int i, j;
276 int c, c2;
277
278 c = 0;
279 c2 = 0;
280 for ( i=MAX_CLIENTS, e=g_entities+i ; i < level.num_entities ; i++,e++ ) {
281 if (!e->inuse)
282 continue;
283 if (!e->team)
284 continue;
285 if (e->flags & FL_TEAMMEMBER)
286 continue;
287 e->teammaster = e;
288 c++;
289 c2++;
290 for (j=i+1, e2=e+1 ; j < level.num_entities ; j++,e2++)
291 {
292 if (!e2->inuse)
293 continue;
294 if (!e2->team)
295 continue;
296 if (e2->flags & FL_TEAMMEMBER)
297 continue;
298 if (!strcmp(e->team, e2->team))
299 {
300 c2++;
301 e2->teamchain = e->teamchain;
302 e->teamchain = e2;
303 e2->teammaster = e;
304 e2->flags |= FL_TEAMMEMBER;
305
306 // make sure that targets only point at the master
307 if ( e2->targetname ) {
308 e->targetname = e2->targetname;
309 e2->targetname = NULL;
310 }
311 }
312 }
313 }
314
315 G_Printf ("%i teams with %i entities\n", c, c2);
316 }
317
318 void G_RemapTeamShaders( void ) {
319 #ifdef MISSIONPACK
320 char string[1024];
321 float f = level.time * 0.001;
322 Com_sprintf( string, sizeof(string), "team_icon/%s_red", g_redteam.string );
323 AddRemap("textures/ctf2/redteam01", string, f);
324 AddRemap("textures/ctf2/redteam02", string, f);
325 Com_sprintf( string, sizeof(string), "team_icon/%s_blue", g_blueteam.string );
326 AddRemap("textures/ctf2/blueteam01", string, f);
327 AddRemap("textures/ctf2/blueteam02", string, f);
328 trap_SetConfigstring(CS_SHADERSTATE, BuildShaderStateConfig());
329 #endif
330 }
331
332
333 /*
334 =================
335 G_RegisterCvars
336 =================
337 */
338 void G_RegisterCvars( void ) {
339 int i;
340 cvarTable_t *cv;
341 qboolean remapped = qfalse;
342
343 for ( i = 0, cv = gameCvarTable ; i < gameCvarTableSize ; i++, cv++ ) {
344 trap_Cvar_Register( cv->vmCvar, cv->cvarName,
345 cv->defaultString, cv->cvarFlags );
346 if ( cv->vmCvar )
347 cv->modificationCount = cv->vmCvar->modificationCount;
348
349 if (cv->teamShader) {
350 remapped = qtrue;
351 }
352 }
353
354 if (remapped) {
355 G_RemapTeamShaders();
356 }
357
358 // check some things
359 if ( g_gametype.integer < 0 || g_gametype.integer >= GT_MAX_GAME_TYPE ) {
360 G_Printf( "g_gametype %i is out of range, defaulting to 0\n", g_gametype.integer );
361 trap_Cvar_Set( "g_gametype", "0" );
362 trap_Cvar_Update( &g_gametype );
363 }
364
365 level.warmupModificationCount = g_warmup.modificationCount;
366 }
367
368 /*
369 =================
370 G_UpdateCvars
371 =================
372 */
373 void G_UpdateCvars( void ) {
374 int i;
375 cvarTable_t *cv;
376 qboolean remapped = qfalse;
377
378 for ( i = 0, cv = gameCvarTable ; i < gameCvarTableSize ; i++, cv++ ) {
379 if ( cv->vmCvar ) {
380 trap_Cvar_Update( cv->vmCvar );
381
382 if ( cv->modificationCount != cv->vmCvar->modificationCount ) {
383 cv->modificationCount = cv->vmCvar->modificationCount;
384
385 if ( cv->trackChange ) {
386 trap_SendServerCommand( -1, va("print \"Server: %s changed to %s\n\"",
387 cv->cvarName, cv->vmCvar->string ) );
388 }
389
390 if (cv->teamShader) {
391 remapped = qtrue;
392 }
393 }
394 }
395 }
396
397 if (remapped) {
398 G_RemapTeamShaders();
399 }
400 }
401
402 /*
403 ============
404 G_InitGame
405
406 ============
407 */
408 void G_InitGame( int levelTime, int randomSeed, int restart ) {
409 int i;
410
411 G_Printf ("------- Game Initialization -------\n");
412 G_Printf ("gamename: %s\n", GAMEVERSION);
413 G_Printf ("gamedate: %s\n", PRODUCT_DATE);
414
415 srand( randomSeed );
416
417 G_RegisterCvars();
418
419 G_ProcessIPBans();
420
421 G_InitMemory();
422
423 // set some level globals
424 memset( &level, 0, sizeof( level ) );
425 level.time = levelTime;
426 level.startTime = levelTime;
427
428 level.snd_fry = G_SoundIndex("sound/player/fry.wav"); // FIXME standing in lava / slime
429
430 if ( g_gametype.integer != GT_SINGLE_PLAYER && g_logfile.string[0] ) {
431 if ( g_logfileSync.integer ) {
432 trap_FS_FOpenFile( g_logfile.string, &level.logFile, FS_APPEND_SYNC );
433 } else {
434 trap_FS_FOpenFile( g_logfile.string, &level.logFile, FS_APPEND );
435 }
436 if ( !level.logFile ) {
437 G_Printf( "WARNING: Couldn't open logfile: %s\n", g_logfile.string );
438 } else {
439 char serverinfo[MAX_INFO_STRING];
440
441 trap_GetServerinfo( serverinfo, sizeof( serverinfo ) );
442
443 G_LogPrintf("------------------------------------------------------------\n" );
444 G_LogPrintf("InitGame: %s\n", serverinfo );
445 }
446 } else {
447 G_Printf( "Not logging to disk.\n" );
448 }
449
450 G_InitWorldSession();
451
452 // initialize all entities for this game
453 memset( g_entities, 0, MAX_GENTITIES * sizeof(g_entities[0]) );
454 level.gentities = g_entities;
455
456 // initialize all clients for this game
457 level.maxclients = g_maxclients.integer;
458 memset( g_clients, 0, MAX_CLIENTS * sizeof(g_clients[0]) );
459 level.clients = g_clients;
460
461 // set client fields on player ents
462 for ( i=0 ; i<level.maxclients ; i++ ) {
463 g_entities[i].client = level.clients + i;
464 }
465
466 // always leave room for the max number of clients,
467 // even if they aren't all used, so numbers inside that
468 // range are NEVER anything but clients
469 level.num_entities = MAX_CLIENTS;
470
471 for ( i=0 ; i<MAX_CLIENTS ; i++ ) {
472 g_entities[i].classname = "clientslot";
473 }
474
475 // let the server system know where the entites are
476 trap_LocateGameData( level.gentities, level.num_entities, sizeof( gentity_t ),
477 &level.clients[0].ps, sizeof( level.clients[0] ) );
478
479 // reserve some spots for dead player bodies
480 InitBodyQue();
481
482 ClearRegisteredItems();
483
484 // parse the key/value pairs and spawn gentities
485 G_SpawnEntitiesFromString();
486
487 // general initialization
488 G_FindTeams();
489
490 // make sure we have flags for CTF, etc
491 if( g_gametype.integer >= GT_TEAM ) {
492 G_CheckTeamItems();
493 }
494
495 SaveRegisteredItems();
496
497 G_Printf ("-----------------------------------\n");
498
499 if( g_gametype.integer == GT_SINGLE_PLAYER || trap_Cvar_VariableIntegerValue( "com_buildScript" ) ) {
500 G_ModelIndex( SP_PODIUM_MODEL );
501 }
502
503 if ( trap_Cvar_VariableIntegerValue( "bot_enable" ) ) {
504 BotAISetup( restart );
505 BotAILoadMap( restart );
506 G_InitBots( restart );
507 }
508
509 G_RemapTeamShaders();
510
511 trap_SetConfigstring( CS_INTERMISSION, "" );
512 }
513
514
515
516 /*
517 =================
518 G_ShutdownGame
519 =================
520 */
521 void G_ShutdownGame( int restart ) {
522 G_Printf ("==== ShutdownGame ====\n");
523
524 if ( level.logFile ) {
525 G_LogPrintf("ShutdownGame:\n" );
526 G_LogPrintf("------------------------------------------------------------\n" );
527 trap_FS_FCloseFile( level.logFile );
528 level.logFile = 0;
529 }
530
531 // write all the client session data so we can get it back
532 G_WriteSessionData();
533
534 if ( trap_Cvar_VariableIntegerValue( "bot_enable" ) ) {
535 BotAIShutdown( restart );
536 }
537 }
538
539
540
541 //===================================================================
542
543 void QDECL Com_Error ( int logLevel, const char *error, ... ) {
544 va_list argptr;
545 char text[1024];
546
547 va_start (argptr, error);
548 Q_vsnprintf (text, sizeof(text), error, argptr);
549 va_end (argptr);
550
551 trap_Error( text );
552 }
553
554 void QDECL Com_Printf( const char *msg, ... ) {
555 va_list argptr;
556 char text[1024];
557
558 va_start (argptr, msg);
559 Q_vsnprintf (text, sizeof(text), msg, argptr);
560 va_end (argptr);
561
562 trap_Print( text );
563 }
564
565 /*
566 ========================================================================
567
568 PLAYER COUNTING / SCORE SORTING
569
570 ========================================================================
571 */
572
573 /*
574 =============
575 AddTournamentPlayer
576
577 If there are less than two tournament players, put a
578 spectator in the game and restart
579 =============
580 */
581 void AddTournamentPlayer( void ) {
582 int i;
583 gclient_t *client;
584 gclient_t *nextInLine;
585
586 if ( level.numPlayingClients >= 2 ) {
587 return;
588 }
589
590 // never change during intermission
591 if ( level.intermissiontime ) {
592 return;
593 }
594
595 nextInLine = NULL;
596
597 for ( i = 0 ; i < level.maxclients ; i++ ) {
598 client = &level.clients[i];
599 if ( client->pers.connected != CON_CONNECTED ) {
600 continue;
601 }
602 if ( client->sess.sessionTeam != TEAM_SPECTATOR ) {
603 continue;
604 }
605 // never select the dedicated follow or scoreboard clients
606 if ( client->sess.spectatorState == SPECTATOR_SCOREBOARD ||
607 client->sess.spectatorClient < 0 ) {
608 continue;
609 }
610
611 if(!nextInLine || client->sess.spectatorNum > nextInLine->sess.spectatorNum)
612 nextInLine = client;
613 }
614
615 if ( !nextInLine ) {
616 return;
617 }
618
619 level.warmupTime = -1;
620
621 // set them to free-for-all team
622 SetTeam( &g_entities[ nextInLine - level.clients ], "f" );
623 }
624
625 /*
626 =======================
627 AddTournamentQueue
628
629 Add client to end of tournament queue
630 =======================
631 */
632
633 void AddTournamentQueue(gclient_t *client)
634 {
635 int index;
636 gclient_t *curclient;
637
638 for(index = 0; index < level.maxclients; index++)
639 {
640 curclient = &level.clients[index];
641
642 if(curclient->pers.connected != CON_DISCONNECTED)
643 {
644 if(curclient == client)
645 curclient->sess.spectatorNum = 0;
646 else if(curclient->sess.sessionTeam == TEAM_SPECTATOR)
647 curclient->sess.spectatorNum++;
648 }
649 }
650 }
651
652 /*
653 =======================
654 RemoveTournamentLoser
655
656 Make the loser a spectator at the back of the line
657 =======================
658 */
659 void RemoveTournamentLoser( void ) {
660 int clientNum;
661
662 if ( level.numPlayingClients != 2 ) {
663 return;
664 }
665
666 clientNum = level.sortedClients[1];
667
668 if ( level.clients[ clientNum ].pers.connected != CON_CONNECTED ) {
669 return;
670 }
671
672 // make them a spectator
673 SetTeam( &g_entities[ clientNum ], "s" );
674 }
675
676 /*
677 =======================
678 RemoveTournamentWinner
679 =======================
680 */
681 void RemoveTournamentWinner( void ) {
682 int clientNum;
683
684 if ( level.numPlayingClients != 2 ) {
685 return;
686 }
687
688 clientNum = level.sortedClients[0];
689
690 if ( level.clients[ clientNum ].pers.connected != CON_CONNECTED ) {
691 return;
692 }
693
694 // make them a spectator
695 SetTeam( &g_entities[ clientNum ], "s" );
696 }
697
698 /*
699 =======================
700 AdjustTournamentScores
701 =======================
702 */
703 void AdjustTournamentScores( void ) {
704 int clientNum;
705
706 clientNum = level.sortedClients[0];
707 if ( level.clients[ clientNum ].pers.connected == CON_CONNECTED ) {
708 level.clients[ clientNum ].sess.wins++;
709 ClientUserinfoChanged( clientNum );
710 }
711
712 clientNum = level.sortedClients[1];
713 if ( level.clients[ clientNum ].pers.connected == CON_CONNECTED ) {
714 level.clients[ clientNum ].sess.losses++;
715 ClientUserinfoChanged( clientNum );
716 }
717
718 }
719
720 /*
721 =============
722 SortRanks
723
724 =============
725 */
726 int QDECL SortRanks( const void *a, const void *b ) {
727 gclient_t *ca, *cb;
728
729 ca = &level.clients[*(int *)a];
730 cb = &level.clients[*(int *)b];
731
732 // sort special clients last
733 if ( ca->sess.spectatorState == SPECTATOR_SCOREBOARD || ca->sess.spectatorClient < 0 ) {
734 return 1;
735 }
736 if ( cb->sess.spectatorState == SPECTATOR_SCOREBOARD || cb->sess.spectatorClient < 0 ) {
737 return -1;
738 }
739
740 // then connecting clients
741 if ( ca->pers.connected == CON_CONNECTING ) {
742 return 1;
743 }
744 if ( cb->pers.connected == CON_CONNECTING ) {
745 return -1;
746 }
747
748
749 // then spectators
750 if ( ca->sess.sessionTeam == TEAM_SPECTATOR && cb->sess.sessionTeam == TEAM_SPECTATOR ) {
751 if ( ca->sess.spectatorNum > cb->sess.spectatorNum ) {
752 return -1;
753 }
754 if ( ca->sess.spectatorNum < cb->sess.spectatorNum ) {
755 return 1;
756 }
757 return 0;
758 }
759 if ( ca->sess.sessionTeam == TEAM_SPECTATOR ) {
760 return 1;
761 }
762 if ( cb->sess.sessionTeam == TEAM_SPECTATOR ) {
763 return -1;
764 }
765
766 // then sort by score
767 if ( ca->ps.persistant[PERS_SCORE]
768 > cb->ps.persistant[PERS_SCORE] ) {
769 return -1;
770 }
771 if ( ca->ps.persistant[PERS_SCORE]
772 < cb->ps.persistant[PERS_SCORE] ) {
773 return 1;
774 }
775 return 0;
776 }
777
778 /*
779 ============
780 CalculateRanks
781
782 Recalculates the score ranks of all players
783 This will be called on every client connect, begin, disconnect, death,
784 and team change.
785 ============
786 */
787 void CalculateRanks( void ) {
788 int i;
789 int rank;
790 int score;
791 int newScore;
792 gclient_t *cl;
793
794 level.follow1 = -1;
795 level.follow2 = -1;
796 level.numConnectedClients = 0;
797 level.numNonSpectatorClients = 0;
798 level.numPlayingClients = 0;
799 level.numVotingClients = 0; // don't count bots
800
801 for (i = 0; i < ARRAY_LEN(level.numteamVotingClients); i++)
802 level.numteamVotingClients[i] = 0;
803
804 for ( i = 0 ; i < level.maxclients ; i++ ) {
805 if ( level.clients[i].pers.connected != CON_DISCONNECTED ) {
806 level.sortedClients[level.numConnectedClients] = i;
807 level.numConnectedClients++;
808
809 if ( level.clients[i].sess.sessionTeam != TEAM_SPECTATOR ) {
810 level.numNonSpectatorClients++;
811
812 // decide if this should be auto-followed
813 if ( level.clients[i].pers.connected == CON_CONNECTED ) {
814 level.numPlayingClients++;
815 if ( !(g_entities[i].r.svFlags & SVF_BOT) ) {
816 level.numVotingClients++;
817 if ( level.clients[i].sess.sessionTeam == TEAM_RED )
818 level.numteamVotingClients[0]++;
819 else if ( level.clients[i].sess.sessionTeam == TEAM_BLUE )
820 level.numteamVotingClients[1]++;
821 }
822 if ( level.follow1 == -1 ) {
823 level.follow1 = i;
824 } else if ( level.follow2 == -1 ) {
825 level.follow2 = i;
826 }
827 }
828 }
829 }
830 }
831
832 qsort( level.sortedClients, level.numConnectedClients,
833 sizeof(level.sortedClients[0]), SortRanks );
834
835 // set the rank value for all clients that are connected and not spectators
836 if ( g_gametype.integer >= GT_TEAM ) {
837 // in team games, rank is just the order of the teams, 0=red, 1=blue, 2=tied
838 for ( i = 0; i < level.numConnectedClients; i++ ) {
839 cl = &level.clients[ level.sortedClients[i] ];
840 if ( level.teamScores[TEAM_RED] == level.teamScores[TEAM_BLUE] ) {
841 cl->ps.persistant[PERS_RANK] = 2;
842 } else if ( level.teamScores[TEAM_RED] > level.teamScores[TEAM_BLUE] ) {
843 cl->ps.persistant[PERS_RANK] = 0;
844 } else {
845 cl->ps.persistant[PERS_RANK] = 1;
846 }
847 }
848 } else {
849 rank = -1;
850 score = 0;
851 for ( i = 0; i < level.numPlayingClients; i++ ) {
852 cl = &level.clients[ level.sortedClients[i] ];
853 newScore = cl->ps.persistant[PERS_SCORE];
854 if ( i == 0 || newScore != score ) {
855 rank = i;
856 // assume we aren't tied until the next client is checked
857 level.clients[ level.sortedClients[i] ].ps.persistant[PERS_RANK] = rank;
858 } else {
859 // we are tied with the previous client
860 level.clients[ level.sortedClients[i-1] ].ps.persistant[PERS_RANK] = rank | RANK_TIED_FLAG;
861 level.clients[ level.sortedClients[i] ].ps.persistant[PERS_RANK] = rank | RANK_TIED_FLAG;
862 }
863 score = newScore;
864 if ( g_gametype.integer == GT_SINGLE_PLAYER && level.numPlayingClients == 1 ) {
865 level.clients[ level.sortedClients[i] ].ps.persistant[PERS_RANK] = rank | RANK_TIED_FLAG;
866 }
867 }
868 }
869
870 // set the CS_SCORES1/2 configstrings, which will be visible to everyone
871 if ( g_gametype.integer >= GT_TEAM ) {
872 trap_SetConfigstring( CS_SCORES1, va("%i", level.teamScores[TEAM_RED] ) );
873 trap_SetConfigstring( CS_SCORES2, va("%i", level.teamScores[TEAM_BLUE] ) );
874 } else {
875 if ( level.numConnectedClients == 0 ) {
876 trap_SetConfigstring( CS_SCORES1, va("%i", SCORE_NOT_PRESENT) );
877 trap_SetConfigstring( CS_SCORES2, va("%i", SCORE_NOT_PRESENT) );
878 } else if ( level.numConnectedClients == 1 ) {
879 trap_SetConfigstring( CS_SCORES1, va("%i", level.clients[ level.sortedClients[0] ].ps.persistant[PERS_SCORE] ) );
880 trap_SetConfigstring( CS_SCORES2, va("%i", SCORE_NOT_PRESENT) );
881 } else {
882 trap_SetConfigstring( CS_SCORES1, va("%i", level.clients[ level.sortedClients[0] ].ps.persistant[PERS_SCORE] ) );
883 trap_SetConfigstring( CS_SCORES2, va("%i", level.clients[ level.sortedClients[1] ].ps.persistant[PERS_SCORE] ) );
884 }
885 }
886
887 // see if it is time to end the level
888 CheckExitRules();
889
890 // if we are at the intermission, send the new info to everyone
891 if ( level.intermissiontime ) {
892 SendScoreboardMessageToAllClients();
893 }
894 }
895
896
897 /*
898 ========================================================================
899
900 MAP CHANGING
901
902 ========================================================================
903 */
904
905 /*
906 ========================
907 SendScoreboardMessageToAllClients
908
909 Do this at BeginIntermission time and whenever ranks are recalculated
910 due to enters/exits/forced team changes
911 ========================
912 */
913 void SendScoreboardMessageToAllClients( void ) {
914 int i;
915
916 for ( i = 0 ; i < level.maxclients ; i++ ) {
917 if ( level.clients[ i ].pers.connected == CON_CONNECTED ) {
918 DeathmatchScoreboardMessage( g_entities + i );
919 }
920 }
921 }
922
923 /*
924 ========================
925 MoveClientToIntermission
926
927 When the intermission starts, this will be called for all players.
928 If a new client connects, this will be called after the spawn function.
929 ========================
930 */
931 void MoveClientToIntermission( gentity_t *ent ) {
932 // take out of follow mode if needed
933 if ( ent->client->sess.spectatorState == SPECTATOR_FOLLOW ) {
934 StopFollowing( ent );
935 }
936
937 FindIntermissionPoint();
938 // move to the spot
939 VectorCopy( level.intermission_origin, ent->s.origin );
940 VectorCopy( level.intermission_origin, ent->client->ps.origin );
941 VectorCopy (level.intermission_angle, ent->client->ps.viewangles);
942 ent->client->ps.pm_type = PM_INTERMISSION;
943
944 // clean up powerup info
945 memset( ent->client->ps.powerups, 0, sizeof(ent->client->ps.powerups) );
946
947 ent->client->ps.eFlags = 0;
948 ent->s.eFlags = 0;
949 ent->s.eType = ET_GENERAL;
950 ent->s.modelindex = 0;
951 ent->s.loopSound = 0;
952 ent->s.event = 0;
953 ent->r.contents = 0;
954 }
955
956 /*
957 ==================
958 FindIntermissionPoint
959
960 This is also used for spectator spawns
961 ==================
962 */
963 void FindIntermissionPoint( void ) {
964 gentity_t *ent, *target;
965 vec3_t dir;
966
967 // find the intermission spot
968 ent = G_Find (NULL, FOFS(classname), "info_player_intermission");
969 if ( !ent ) { // the map creator forgot to put in an intermission point...
970 SelectSpawnPoint ( vec3_origin, level.intermission_origin, level.intermission_angle, qfalse );
971 } else {
972 VectorCopy (ent->s.origin, level.intermission_origin);
973 VectorCopy (ent->s.angles, level.intermission_angle);
974 // if it has a target, look towards it
975 if ( ent->target ) {
976 target = G_PickTarget( ent->target );
977 if ( target ) {
978 VectorSubtract( target->s.origin, level.intermission_origin, dir );
979 vectoangles( dir, level.intermission_angle );
980 }
981 }
982 }
983
984 }
985
986 /*
987 ==================
988 BeginIntermission
989 ==================
990 */
991 void BeginIntermission( void ) {
992 int i;
993 gentity_t *client;
994
995 if ( level.intermissiontime ) {
996 return; // already active
997 }
998
999 // if in tournement mode, change the wins / losses
1000 if ( g_gametype.integer == GT_TOURNAMENT ) {
1001 AdjustTournamentScores();
1002 }
1003
1004 level.intermissiontime = level.time;
1005 // move all clients to the intermission point
1006 for (i=0 ; i< level.maxclients ; i++) {
1007 client = g_entities + i;
1008 if (!client->inuse)
1009 continue;
1010 // respawn if dead
1011 if (client->health <= 0) {
1012 ClientRespawn(client);
1013 }
1014 MoveClientToIntermission( client );
1015 }
1016 #ifdef MISSIONPACK
1017 if (g_singlePlayer.integer) {
1018 trap_Cvar_Set("ui_singlePlayerActive", "0");
1019 UpdateTournamentInfo();
1020 }
1021 #else
1022 // if single player game
1023 if ( g_gametype.integer == GT_SINGLE_PLAYER ) {
1024 UpdateTournamentInfo();
1025 SpawnModelsOnVictoryPads();
1026 }
1027 #endif
1028 // send the current scoring to all clients
1029 SendScoreboardMessageToAllClients();
1030
1031 }
1032
1033
1034 /*
1035 =============
1036 ExitLevel
1037
1038 When the intermission has been exited, the server is either killed
1039 or moved to a new level based on the "nextmap" cvar
1040
1041 =============
1042 */
1043 void ExitLevel (void) {
1044 int i;
1045 gclient_t *cl;
1046 char nextmap[MAX_STRING_CHARS];
1047 char d1[MAX_STRING_CHARS];
1048
1049 //bot interbreeding
1050 BotInterbreedEndMatch();
1051
1052 // if we are running a tournement map, kick the loser to spectator status,
1053 // which will automatically grab the next spectator and restart
1054 if ( g_gametype.integer == GT_TOURNAMENT ) {
1055 if ( !level.restarted ) {
1056 RemoveTournamentLoser();
1057 trap_SendConsoleCommand( EXEC_APPEND, "map_restart 0\n" );
1058 level.restarted = qtrue;
1059 level.changemap = NULL;
1060 level.intermissiontime = 0;
1061 }
1062 return;
1063 }
1064
1065 trap_Cvar_VariableStringBuffer( "nextmap", nextmap, sizeof(nextmap) );
1066 trap_Cvar_VariableStringBuffer( "d1", d1, sizeof(d1) );
1067
1068 if( !Q_stricmp( nextmap, "map_restart 0" ) && Q_stricmp( d1, "" ) ) {
1069 trap_Cvar_Set( "nextmap", "vstr d2" );
1070 trap_SendConsoleCommand( EXEC_APPEND, "vstr d1\n" );
1071 } else {
1072 trap_SendConsoleCommand( EXEC_APPEND, "vstr nextmap\n" );
1073 }
1074
1075 level.changemap = NULL;
1076 level.intermissiontime = 0;
1077
1078 // reset all the scores so we don't enter the intermission again
1079 level.teamScores[TEAM_RED] = 0;
1080 level.teamScores[TEAM_BLUE] = 0;
1081 for ( i=0 ; i< g_maxclients.integer ; i++ ) {
1082 cl = level.clients + i;
1083 if ( cl->pers.connected != CON_CONNECTED ) {
1084 continue;
1085 }
1086 cl->ps.persistant[PERS_SCORE] = 0;
1087 }
1088
1089 // we need to do this here before changing to CON_CONNECTING
1090 G_WriteSessionData();
1091
1092 // change all client states to connecting, so the early players into the
1093 // next level will know the others aren't done reconnecting
1094 for (i=0 ; i< g_maxclients.integer ; i++) {
1095 if ( level.clients[i].pers.connected == CON_CONNECTED ) {
1096 level.clients[i].pers.connected = CON_CONNECTING;
1097 }
1098 }
1099
1100 }
1101
1102 /*
1103 =================
1104 G_LogPrintf
1105
1106 Print to the logfile with a time stamp if it is open
1107 =================
1108 */
1109 void QDECL G_LogPrintf( const char *fmt, ... ) {
1110 va_list argptr;
1111 char string[1024];
1112 int min, tens, sec;
1113
1114 sec = ( level.time - level.startTime ) / 1000;
1115
1116 min = sec / 60;
1117 sec -= min * 60;
1118 tens = sec / 10;
1119 sec -= tens * 10;
1120
1121 Com_sprintf( string, sizeof(string), "%3i:%i%i ", min, tens, sec );
1122
1123 va_start( argptr, fmt );
1124 Q_vsnprintf(string + 7, sizeof(string) - 7, fmt, argptr);
1125 va_end( argptr );
1126
1127 if ( g_dedicated.integer ) {
1128 G_Printf( "%s", string + 7 );
1129 }
1130
1131 if ( !level.logFile ) {
1132 return;
1133 }
1134
1135 trap_FS_Write( string, strlen( string ), level.logFile );
1136 }
1137
1138 /*
1139 ================
1140 LogExit
1141
1142 Append information about this game to the log file
1143 ================
1144 */
1145 void LogExit( const char *string ) {
1146 int i, numSorted;
1147 gclient_t *cl;
1148 #ifdef MISSIONPACK
1149 qboolean won = qtrue;
1150 team_t team = TEAM_RED;
1151 #endif
1152 G_LogPrintf( "Exit: %s\n", string );
1153
1154 level.intermissionQueued = level.time;
1155
1156 // this will keep the clients from playing any voice sounds
1157 // that will get cut off when the queued intermission starts
1158 trap_SetConfigstring( CS_INTERMISSION, "1" );
1159
1160 // don't send more than 32 scores (FIXME?)
1161 numSorted = level.numConnectedClients;
1162 if ( numSorted > 32 ) {
1163 numSorted = 32;
1164 }
1165
1166 if ( g_gametype.integer >= GT_TEAM ) {
1167 G_LogPrintf( "red:%i blue:%i\n",
1168 level.teamScores[TEAM_RED], level.teamScores[TEAM_BLUE] );
1169 }
1170
1171 for (i=0 ; i < numSorted ; i++) {
1172 int ping;
1173
1174 cl = &level.clients[level.sortedClients[i]];
1175
1176 if ( cl->sess.sessionTeam == TEAM_SPECTATOR ) {
1177 continue;
1178 }
1179 if ( cl->pers.connected == CON_CONNECTING ) {
1180 continue;
1181 }
1182
1183 ping = cl->ps.ping < 999 ? cl->ps.ping : 999;
1184
1185 G_LogPrintf( "score: %i ping: %i client: %i %s\n", cl->ps.persistant[PERS_SCORE], ping, level.sortedClients[i], cl->pers.netname );
1186 #ifdef MISSIONPACK
1187 if (g_singlePlayer.integer && !(g_entities[cl - level.clients].r.svFlags & SVF_BOT)) {
1188 team = cl->sess.sessionTeam;
1189 }
1190 if (g_singlePlayer.integer && g_gametype.integer < GT_TEAM) {
1191 if (g_entities[cl - level.clients].r.svFlags & SVF_BOT && cl->ps.persistant[PERS_RANK] == 0) {
1192 won = qfalse;
1193 }
1194 }
1195 #endif
1196
1197 }
1198
1199 #ifdef MISSIONPACK
1200 if (g_singlePlayer.integer) {
1201 if (g_gametype.integer >= GT_TEAM) {
1202 if (team == TEAM_BLUE) {
1203 won = level.teamScores[TEAM_BLUE] > level.teamScores[TEAM_RED];
1204 } else {
1205 won = level.teamScores[TEAM_RED] > level.teamScores[TEAM_BLUE];
1206 }
1207 }
1208 trap_SendConsoleCommand( EXEC_APPEND, (won) ? "spWin\n" : "spLose\n" );
1209 }
1210 #endif
1211
1212
1213 }
1214
1215
1216 /*
1217 =================
1218 CheckIntermissionExit
1219
1220 The level will stay at the intermission for a minimum of 5 seconds
1221 If all players wish to continue, the level will then exit.
1222 If one or more players have not acknowledged the continue, the game will
1223 wait 10 seconds before going on.
1224 =================
1225 */
1226 void CheckIntermissionExit( void ) {
1227 int ready, notReady, playerCount;
1228 int i;
1229 gclient_t *cl;
1230 int readyMask;
1231
1232 if ( g_gametype.integer == GT_SINGLE_PLAYER ) {
1233 return;
1234 }
1235
1236 // see which players are ready
1237 ready = 0;
1238 notReady = 0;
1239 readyMask = 0;
1240 playerCount = 0;
1241 for (i=0 ; i< g_maxclients.integer ; i++) {
1242 cl = level.clients + i;
1243 if ( cl->pers.connected != CON_CONNECTED ) {
1244 continue;
1245 }
1246 if ( g_entities[i].r.svFlags & SVF_BOT ) {
1247 continue;
1248 }
1249
1250 playerCount++;
1251 if ( cl->readyToExit ) {
1252 ready++;
1253 if ( i < 16 ) {
1254 readyMask |= 1 << i;
1255 }
1256 } else {
1257 notReady++;
1258 }
1259 }
1260
1261 // copy the readyMask to each player's stats so
1262 // it can be displayed on the scoreboard
1263 for (i=0 ; i< g_maxclients.integer ; i++) {
1264 cl = level.clients + i;
1265 if ( cl->pers.connected != CON_CONNECTED ) {
1266 continue;
1267 }
1268 cl->ps.stats[STAT_CLIENTS_READY] = readyMask;
1269 }
1270
1271 // never exit in less than five seconds
1272 if ( level.time < level.intermissiontime + 5000 ) {
1273 return;
1274 }
1275
1276 // only test ready status when there are real players present
1277 if ( playerCount > 0 ) {
1278 // if nobody wants to go, clear timer
1279 if ( !ready ) {
1280 level.readyToExit = qfalse;
1281 return;
1282 }
1283
1284 // if everyone wants to go, go now
1285 if ( !notReady ) {
1286 ExitLevel();
1287 return;
1288 }
1289 }
1290
1291 // the first person to ready starts the ten second timeout
1292 if ( !level.readyToExit ) {
1293 level.readyToExit = qtrue;
1294 level.exitTime = level.time;
1295 }
1296
1297 // if we have waited ten seconds since at least one player
1298 // wanted to exit, go ahead
1299 if ( level.time < level.exitTime + 10000 ) {
1300 return;
1301 }
1302
1303 ExitLevel();
1304 }
1305
1306 /*
1307 =============
1308 ScoreIsTied
1309 =============
1310 */
1311 qboolean ScoreIsTied( void ) {
1312 int a, b;
1313
1314 if ( level.numPlayingClients < 2 ) {
1315 return qfalse;
1316 }
1317
1318 if ( g_gametype.integer >= GT_TEAM ) {
1319 return level.teamScores[TEAM_RED] == level.teamScores[TEAM_BLUE];
1320 }
1321
1322 a = level.clients[level.sortedClients[0]].ps.persistant[PERS_SCORE];
1323 b = level.clients[level.sortedClients[1]].ps.persistant[PERS_SCORE];
1324
1325 return a == b;
1326 }
1327
1328 /*
1329 =================
1330 CheckExitRules
1331
1332 There will be a delay between the time the exit is qualified for
1333 and the time everyone is moved to the intermission spot, so you
1334 can see the last frag.
1335 =================
1336 */
1337 void CheckExitRules( void ) {
1338 int i;
1339 gclient_t *cl;
1340 // if at the intermission, wait for all non-bots to
1341 // signal ready, then go to next level
1342 if ( level.intermissiontime ) {
1343 CheckIntermissionExit ();
1344 return;
1345 }
1346
1347 if ( level.intermissionQueued ) {
1348 #ifdef MISSIONPACK
1349 int time = (g_singlePlayer.integer) ? SP_INTERMISSION_DELAY_TIME : INTERMISSION_DELAY_TIME;
1350 if ( level.time - level.intermissionQueued >= time ) {
1351 level.intermissionQueued = 0;
1352 BeginIntermission();
1353 }
1354 #else
1355 if ( level.time - level.intermissionQueued >= INTERMISSION_DELAY_TIME ) {
1356 level.intermissionQueued = 0;
1357 BeginIntermission();
1358 }
1359 #endif
1360 return;
1361 }
1362
1363 // check for sudden death
1364 if ( ScoreIsTied() ) {
1365 // always wait for sudden death
1366 return;
1367 }
1368
1369 if ( g_timelimit.integer < 0 || g_timelimit.integer > INT_MAX / 60000 ) {
1370 G_Printf( "timelimit %i is out of range, defaulting to 0\n", g_timelimit.integer );
1371 trap_Cvar_Set( "timelimit", "0" );
1372 trap_Cvar_Update( &g_timelimit );
1373 }
1374
1375 if ( g_timelimit.integer && !level.warmupTime ) {
1376 if ( level.time - level.startTime >= g_timelimit.integer*60000 ) {
1377 trap_SendServerCommand( -1, "print \"Timelimit hit.\n\"");
1378 LogExit( "Timelimit hit." );
1379 return;
1380 }
1381 }
1382
1383 if ( g_fraglimit.integer < 0 ) {
1384 G_Printf( "fraglimit %i is out of range, defaulting to 0\n", g_fraglimit.integer );
1385 trap_Cvar_Set( "fraglimit", "0" );
1386 trap_Cvar_Update( &g_fraglimit );
1387 }
1388
1389 if ( g_gametype.integer < GT_CTF && g_fraglimit.integer ) {
1390 if ( level.teamScores[TEAM_RED] >= g_fraglimit.integer ) {
1391 trap_SendServerCommand( -1, "print \"Red hit the fraglimit.\n\"" );
1392 LogExit( "Fraglimit hit." );
1393 return;
1394 }
1395
1396 if ( level.teamScores[TEAM_BLUE] >= g_fraglimit.integer ) {
1397 trap_SendServerCommand( -1, "print \"Blue hit the fraglimit.\n\"" );
1398 LogExit( "Fraglimit hit." );
1399 return;
1400 }
1401
1402 for ( i=0 ; i< g_maxclients.integer ; i++ ) {
1403 cl = level.clients + i;
1404 if ( cl->pers.connected != CON_CONNECTED ) {
1405 continue;
1406 }
1407 if ( cl->sess.sessionTeam != TEAM_FREE ) {
1408 continue;
1409 }
1410
1411 if ( cl->ps.persistant[PERS_SCORE] >= g_fraglimit.integer ) {
1412 LogExit( "Fraglimit hit." );
1413 trap_SendServerCommand( -1, va("print \"%s" S_COLOR_WHITE " hit the fraglimit.\n\"",
1414 cl->pers.netname ) );
1415 return;
1416 }
1417 }
1418 }
1419
1420 if ( g_capturelimit.integer < 0 ) {
1421 G_Printf( "capturelimit %i is out of range, defaulting to 0\n", g_capturelimit.integer );
1422 trap_Cvar_Set( "capturelimit", "0" );
1423 trap_Cvar_Update( &g_capturelimit );
1424 }
1425
1426 if ( g_gametype.integer >= GT_CTF && g_capturelimit.integer ) {
1427
1428 if ( level.teamScores[TEAM_RED] >= g_capturelimit.integer ) {
1429 trap_SendServerCommand( -1, "print \"Red hit the capturelimit.\n\"" );
1430 LogExit( "Capturelimit hit." );
1431 return;
1432 }
1433
1434 if ( level.teamScores[TEAM_BLUE] >= g_capturelimit.integer ) {
1435 trap_SendServerCommand( -1, "print \"Blue hit the capturelimit.\n\"" );
1436 LogExit( "Capturelimit hit." );
1437 return;
1438 }
1439 }
1440 }
1441
1442
1443
1444 /*
1445 ========================================================================
1446
1447 FUNCTIONS CALLED EVERY FRAME
1448
1449 ========================================================================
1450 */
1451
1452
1453 /*
1454 =============
1455 CheckTournament
1456
1457 Once a frame, check for changes in tournement player state
1458 =============
1459 */
1460 void CheckTournament( void ) {
1461 // check because we run 3 game frames before calling Connect and/or ClientBegin
1462 // for clients on a map_restart
1463 if ( level.numPlayingClients == 0 ) {
1464 return;
1465 }
1466
1467 if ( g_gametype.integer == GT_TOURNAMENT ) {
1468
1469 // pull in a spectator if needed
1470 if ( level.numPlayingClients < 2 ) {
1471 AddTournamentPlayer();
1472 }
1473
1474 // if we don't have two players, go back to "waiting for players"
1475 if ( level.numPlayingClients != 2 ) {
1476 if ( level.warmupTime != -1 ) {
1477 level.warmupTime = -1;
1478 trap_SetConfigstring( CS_WARMUP, va("%i", level.warmupTime) );
1479 G_LogPrintf( "Warmup:\n" );
1480 }
1481 return;
1482 }
1483
1484 if ( level.warmupTime == 0 ) {
1485 return;
1486 }
1487
1488 // if the warmup is changed at the console, restart it
1489 if ( g_warmup.modificationCount != level.warmupModificationCount ) {
1490 level.warmupModificationCount = g_warmup.modificationCount;
1491 level.warmupTime = -1;
1492 }
1493
1494 // if all players have arrived, start the countdown
1495 if ( level.warmupTime < 0 ) {
1496 if ( level.numPlayingClients == 2 ) {
1497 // fudge by -1 to account for extra delays
1498 if ( g_warmup.integer > 1 ) {
1499 level.warmupTime = level.time + ( g_warmup.integer - 1 ) * 1000;
1500 } else {
1501 level.warmupTime = 0;
1502 }
1503
1504 trap_SetConfigstring( CS_WARMUP, va("%i", level.warmupTime) );
1505 }
1506 return;
1507 }
1508
1509 // if the warmup time has counted down, restart
1510 if ( level.time > level.warmupTime ) {
1511 level.warmupTime += 10000;
1512 trap_Cvar_Set( "g_restarted", "1" );
1513 trap_SendConsoleCommand( EXEC_APPEND, "map_restart 0\n" );
1514 level.restarted = qtrue;
1515 return;
1516 }
1517 } else if ( g_gametype.integer != GT_SINGLE_PLAYER && level.warmupTime != 0 ) {
1518 int counts[TEAM_NUM_TEAMS];
1519 qboolean notEnough = qfalse;
1520
1521 if ( g_gametype.integer >= GT_TEAM ) {
1522 counts[TEAM_BLUE] = TeamCount( -1, TEAM_BLUE );
1523 counts[TEAM_RED] = TeamCount( -1, TEAM_RED );
1524
1525 if (counts[TEAM_RED] < 1 || counts[TEAM_BLUE] < 1) {
1526 notEnough = qtrue;
1527 }
1528 } else if ( level.numPlayingClients < 2 ) {
1529 notEnough = qtrue;
1530 }
1531
1532 if ( notEnough ) {
1533 if ( level.warmupTime != -1 ) {
1534 level.warmupTime = -1;
1535 trap_SetConfigstring( CS_WARMUP, va("%i", level.warmupTime) );
1536 G_LogPrintf( "Warmup:\n" );
1537 }
1538 return; // still waiting for team members
1539 }
1540
1541 if ( level.warmupTime == 0 ) {
1542 return;
1543 }
1544
1545 // if the warmup is changed at the console, restart it
1546 if ( g_warmup.modificationCount != level.warmupModificationCount ) {
1547 level.warmupModificationCount = g_warmup.modificationCount;
1548 level.warmupTime = -1;
1549 }
1550
1551 // if all players have arrived, start the countdown
1552 if ( level.warmupTime < 0 ) {
1553 // fudge by -1 to account for extra delays
1554 if ( g_warmup.integer > 1 ) {
1555 level.warmupTime = level.time + ( g_warmup.integer - 1 ) * 1000;
1556 } else {
1557 level.warmupTime = 0;
1558 }
1559
1560 trap_SetConfigstring( CS_WARMUP, va("%i", level.warmupTime) );
1561 return;
1562 }
1563
1564 // if the warmup time has counted down, restart
1565 if ( level.time > level.warmupTime ) {
1566 level.warmupTime += 10000;
1567 trap_Cvar_Set( "g_restarted", "1" );
1568 trap_SendConsoleCommand( EXEC_APPEND, "map_restart 0\n" );
1569 level.restarted = qtrue;
1570 return;
1571 }
1572 }
1573 }
1574
1575
1576 /*
1577 ==================
1578 CheckVote
1579 ==================
1580 */
1581 void CheckVote( void ) {
1582 if ( level.voteExecuteTime && level.voteExecuteTime < level.time ) {
1583 level.voteExecuteTime = 0;
1584 trap_SendConsoleCommand( EXEC_APPEND, va("%s\n", level.voteString ) );
1585 }
1586 if ( !level.voteTime ) {
1587 return;
1588 }
1589 if ( level.time - level.voteTime >= VOTE_TIME ) {
1590 trap_SendServerCommand( -1, "print \"Vote failed.\n\"" );
1591 } else {
1592 // ATVI Q3 1.32 Patch #9, WNF
1593 if ( level.voteYes > level.numVotingClients/2 ) {
1594 // execute the command, then remove the vote
1595 trap_SendServerCommand( -1, "print \"Vote passed.\n\"" );
1596 level.voteExecuteTime = level.time + 3000;
1597 } else if ( level.voteNo >= level.numVotingClients/2 ) {
1598 // same behavior as a timeout
1599 trap_SendServerCommand( -1, "print \"Vote failed.\n\"" );
1600 } else {
1601 // still waiting for a majority
1602 return;
1603 }
1604 }
1605 level.voteTime = 0;
1606 trap_SetConfigstring( CS_VOTE_TIME, "" );
1607
1608 }
1609
1610 /*
1611 ==================
1612 PrintTeam
1613 ==================
1614 */
1615 void PrintTeam(int team, char *message) {
1616 int i;
1617
1618 for ( i = 0 ; i < level.maxclients ; i++ ) {
1619 if (level.clients[i].sess.sessionTeam != team)
1620 continue;
1621 trap_SendServerCommand( i, message );
1622 }
1623 }
1624
1625 /*
1626 ==================
1627 SetLeader
1628 ==================
1629 */
1630 void SetLeader(int team, int client) {
1631 int i;
1632
1633 if ( level.clients[client].pers.connected == CON_DISCONNECTED ) {
1634 PrintTeam(team, va("print \"%s is not connected\n\"", level.clients[client].pers.netname) );
1635 return;
1636 }
1637 if (level.clients[client].sess.sessionTeam != team) {
1638 PrintTeam(team, va("print \"%s is not on the team anymore\n\"", level.clients[client].pers.netname) );
1639 return;
1640 }
1641 for ( i = 0 ; i < level.maxclients ; i++ ) {
1642 if (level.clients[i].sess.sessionTeam != team)
1643 continue;
1644 if (level.clients[i].sess.teamLeader) {
1645 level.clients[i].sess.teamLeader = qfalse;
1646 ClientUserinfoChanged(i);
1647 }
1648 }
1649 level.clients[client].sess.teamLeader = qtrue;
1650 ClientUserinfoChanged( client );
1651 PrintTeam(team, va("print \"%s is the new team leader\n\"", level.clients[client].pers.netname) );
1652 }
1653
1654 /*
1655 ==================
1656 CheckTeamLeader
1657 ==================
1658 */
1659 void CheckTeamLeader( int team ) {
1660 int i;
1661
1662 for ( i = 0 ; i < level.maxclients ; i++ ) {
1663 if (level.clients[i].sess.sessionTeam != team)
1664 continue;
1665 if (level.clients[i].sess.teamLeader)
1666 break;
1667 }
1668 if (i >= level.maxclients) {
1669 for ( i = 0 ; i < level.maxclients ; i++ ) {
1670 if (level.clients[i].sess.sessionTeam != team)
1671 continue;
1672 if (!(g_entities[i].r.svFlags & SVF_BOT)) {
1673 level.clients[i].sess.teamLeader = qtrue;
1674 break;
1675 }
1676 }
1677
1678 if (i >= level.maxclients) {
1679 for ( i = 0 ; i < level.maxclients ; i++ ) {
1680 if (level.clients[i].sess.sessionTeam != team)
1681 continue;
1682 level.clients[i].sess.teamLeader = qtrue;
1683 break;
1684 }
1685 }
1686 }
1687 }
1688
1689 /*
1690 ==================
1691 CheckTeamVote
1692 ==================
1693 */
1694 void CheckTeamVote( int team ) {
1695 int cs_offset;
1696
1697 if ( team == TEAM_RED )
1698 cs_offset = 0;
1699 else if ( team == TEAM_BLUE )
1700 cs_offset = 1;
1701 else
1702 return;
1703
1704 if ( !level.teamVoteTime[cs_offset] ) {
1705 return;
1706 }
1707 if ( level.time - level.teamVoteTime[cs_offset] >= VOTE_TIME ) {
1708 trap_SendServerCommand( -1, "print \"Team vote failed.\n\"" );
1709 } else {
1710 if ( level.teamVoteYes[cs_offset] > level.numteamVotingClients[cs_offset]/2 ) {
1711 // execute the command, then remove the vote
1712 trap_SendServerCommand( -1, "print \"Team vote passed.\n\"" );
1713 //
1714 if ( !Q_strncmp( "leader", level.teamVoteString[cs_offset], 6) ) {
1715 //set the team leader
1716 SetLeader(team, atoi(level.teamVoteString[cs_offset] + 7));
1717 }
1718 else {
1719 trap_SendConsoleCommand( EXEC_APPEND, va("%s\n", level.teamVoteString[cs_offset] ) );
1720 }
1721 } else if ( level.teamVoteNo[cs_offset] >= level.numteamVotingClients[cs_offset]/2 ) {
1722 // same behavior as a timeout
1723 trap_SendServerCommand( -1, "print \"Team vote failed.\n\"" );
1724 } else {
1725 // still waiting for a majority
1726 return;
1727 }
1728 }
1729 level.teamVoteTime[cs_offset] = 0;
1730 trap_SetConfigstring( CS_TEAMVOTE_TIME + cs_offset, "" );
1731
1732 }
1733
1734
1735 /*
1736 ==================
1737 CheckCvars
1738 ==================
1739 */
1740 void CheckCvars( void ) {
1741 static int lastMod = -1;
1742
1743 if ( g_password.modificationCount != lastMod ) {
1744 lastMod = g_password.modificationCount;
1745 if ( *g_password.string && Q_stricmp( g_password.string, "none" ) ) {
1746 trap_Cvar_Set( "g_needpass", "1" );
1747 } else {
1748 trap_Cvar_Set( "g_needpass", "0" );
1749 }
1750 }
1751 }
1752
1753 /*
1754 =============
1755 G_RunThink
1756
1757 Runs thinking code for this frame if necessary
1758 =============
1759 */
1760 void G_RunThink (gentity_t *ent) {
1761 int thinktime;
1762
1763 thinktime = ent->nextthink;
1764 if (thinktime <= 0) {
1765 return;
1766 }
1767 if (thinktime > level.time) {
1768 return;
1769 }
1770
1771 ent->nextthink = 0;
1772 if (!ent->think) {
1773 G_Error ( "NULL ent->think");
1774 }
1775 ent->think (ent);
1776 }
1777
1778 /*
1779 ================
1780 G_RunFrame
1781
1782 Advances the non-player objects in the world
1783 ================
1784 */
1785 void G_RunFrame( int levelTime ) {
1786 int i;
1787 gentity_t *ent;
1788
1789 // if we are waiting for the level to restart, do nothing
1790 if ( level.restarted ) {
1791 return;
1792 }
1793
1794 level.framenum++;
1795 level.previousTime = level.time;
1796 level.time = levelTime;
1797
1798 // get any cvar changes
1799 G_UpdateCvars();
1800
1801 //
1802 // go through all allocated objects
1803 //
1804 ent = &g_entities[0];
1805 for (i=0 ; i<level.num_entities ; i++, ent++) {
1806 if ( !ent->inuse ) {
1807 continue;
1808 }
1809
1810 // clear events that are too old
1811 if ( level.time - ent->eventTime > EVENT_VALID_MSEC ) {
1812 if ( ent->s.event ) {
1813 ent->s.event = 0; // &= EV_EVENT_BITS;
1814 if ( ent->client ) {
1815 ent->client->ps.externalEvent = 0;
1816 // predicted events should never be set to zero
1817 //ent->client->ps.events[0] = 0;
1818 //ent->client->ps.events[1] = 0;
1819 }
1820 }
1821 if ( ent->freeAfterEvent ) {
1822 // tempEntities or dropped items completely go away after their event
1823 G_FreeEntity( ent );
1824 continue;
1825 } else if ( ent->unlinkAfterEvent ) {
1826 // items that will respawn will hide themselves after their pickup event
1827 ent->unlinkAfterEvent = qfalse;
1828 trap_UnlinkEntity( ent );
1829 }
1830 }
1831
1832 // temporary entities don't think
1833 if ( ent->freeAfterEvent ) {
1834 continue;
1835 }
1836
1837 if ( !ent->r.linked && ent->neverFree ) {
1838 continue;
1839 }
1840
1841 if ( ent->s.eType == ET_MISSILE ) {
1842 G_RunMissile( ent );
1843 continue;
1844 }
1845
1846 if ( ent->s.eType == ET_ITEM || ent->physicsObject ) {
1847 G_RunItem( ent );
1848 continue;
1849 }
1850
1851 if ( ent->s.eType == ET_MOVER ) {
1852 G_RunMover( ent );
1853 continue;
1854 }
1855
1856 if ( i < MAX_CLIENTS ) {
1857 G_RunClient( ent );
1858 continue;
1859 }
1860
1861 G_RunThink( ent );
1862 }
1863
1864 // perform final fixups on the players
1865 ent = &g_entities[0];
1866 for (i=0 ; i < level.maxclients ; i++, ent++ ) {
1867 if ( ent->inuse ) {
1868 ClientEndFrame( ent );
1869 }
1870 }
1871
1872 // see if it is time to do a tournement restart
1873 CheckTournament();
1874
1875 // see if it is time to end the level
1876 CheckExitRules();
1877
1878 // update to team status?
1879 CheckTeamStatus();
1880
1881 // cancel vote if timed out
1882 CheckVote();
1883
1884 // check team votes
1885 CheckTeamVote( TEAM_RED );
1886 CheckTeamVote( TEAM_BLUE );
1887
1888 // for tracking changes
1889 CheckCvars();
1890
1891 if (g_listEntity.integer) {
1892 for (i = 0; i < MAX_GENTITIES; i++) {
1893 G_Printf("%4i: %s\n", i, g_entities[i].classname);
1894 }
1895 trap_Cvar_Set("g_listEntity", "0");
1896 }
1897 }