]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/gamec/g_world.c
Now overtime also gets initiated by equal frags and reaching the fraglimit.
[divverent/nexuiz.git] / data / qcsrc / server / gamec / g_world.c
1 string GetMapname();
2
3 void worldspawn (void)
4 {
5
6         // Precache all player models
7         // Workaround for "invisible players"
8         precache_model("models/player/carni.zym");
9         precache_model("models/player/crash.zym");
10         precache_model("models/player/grunt.zym");
11         precache_model("models/player/headhunter.zym");
12         precache_model("models/player/insurrectionist.zym");
13         precache_model("models/player/jeandarc.zym");
14         precache_model("models/player/lurk.zym");
15         precache_model("models/player/lycanthrope.zym");
16         precache_model("models/player/marine.zym");
17         precache_model("models/player/nexus.zym");
18         precache_model("models/player/pyria.zym");
19         precache_model("models/player/shock.zym");
20         precache_model("models/player/skadi.zym");
21         precache_model("models/player/specop.zym");
22         precache_model("models/player/visitant.zym");
23
24         //precache_model ("progs/beam.mdl");
25         precache_model ("models/bullet.mdl");
26         precache_model ("models/casing_bronze.mdl");
27         precache_model ("models/casing_shell.mdl");
28         precache_model ("models/casing_steel.mdl");
29         precache_model ("models/ebomb.mdl");
30         precache_model ("models/elaser.mdl");
31         precache_model ("models/flash.md3");
32         precache_model ("models/gibs/bloodyskull.md3");
33         precache_model ("models/gibs/chunk.mdl");
34         precache_model ("models/gibs/eye.md3");
35         precache_model ("models/gibs/gib1.md3");
36         //precache_model ("models/gibs/gib2.md3");
37         //precache_model ("models/gibs/gib3.md3");
38         //precache_model ("models/gibs/gib4.md3");
39         precache_model ("models/gibs/gib5.md3");
40         //precache_model ("models/gibs/gib6.md3");
41         precache_model ("models/gibs/gib1.mdl");
42         precache_model ("models/gibs/gib2.mdl");
43         precache_model ("models/gibs/gib3.mdl");
44         precache_model ("models/grenademodel.md3");
45         precache_model ("models/hagarmissile.mdl");
46         precache_model ("models/items/a_bullets.mdl");
47         precache_model ("models/items/a_cells.md3");
48         precache_model ("models/items/a_rockets.md3");
49         precache_model ("models/items/a_shells.md3");
50         precache_model ("models/items/g_a1.md3");
51         precache_model ("models/items/g_a25.md3");
52         precache_model ("models/items/g_h1.md3");
53         precache_model ("models/items/g_h25.md3");
54         precache_model ("models/items/g_h100.md3");
55         precache_model ("models/items/g_invincible.md3");
56         precache_model ("models/items/g_strength.md3");
57         precache_model ("models/laser.mdl");
58         precache_model ("models/misc/chatbubble.spr");
59         precache_model ("models/misc/teambubble.spr");
60         precache_model ("models/nexflash.md3");
61         precache_model ("models/plasma.mdl");
62         precache_model ("models/plasmatrail.mdl");
63         precache_model ("models/rocket.md3");
64         precache_model ("models/sprites/grenexpl.spr");
65         precache_model ("models/sprites/hagar.spr");
66         precache_model ("models/sprites/muzzleflash.spr32");
67         precache_model ("models/sprites/electrocombo.spr32");
68         //precache_model ("models/sprites/plasmahitwall.spr32");
69         //precache_model ("models/sprites/plasmashot.spr32");
70         precache_model ("models/sprites/rockexpl.spr");
71         precache_model ("models/tracer.mdl");
72         precache_model ("models/uziflash.md3");
73         precache_model ("models/weapons/g_crylink.md3");
74         precache_model ("models/weapons/g_electro.md3");
75         precache_model ("models/weapons/g_gl.md3");
76         precache_model ("models/weapons/g_hagar.md3");
77         precache_model ("models/weapons/g_nex.md3");
78         precache_model ("models/weapons/g_rl.md3");
79         precache_model ("models/weapons/g_shotgun.md3");
80         precache_model ("models/weapons/g_uzi.md3");
81         precache_model ("models/weapons/v_crylink.md3");
82         precache_model ("models/weapons/v_electro.md3");
83         precache_model ("models/weapons/v_gl.md3");
84         precache_model ("models/weapons/v_hagar.md3");
85         precache_model ("models/weapons/v_laser.md3");
86         precache_model ("models/weapons/v_nex.md3");
87         precache_model ("models/weapons/v_rl.md3");
88         precache_model ("models/weapons/v_shotgun.md3");
89         precache_model ("models/weapons/v_uzi.md3");
90         precache_model ("models/weapons/w_crylink.zym");
91         precache_model ("models/weapons/w_electro.zym");
92         precache_model ("models/weapons/w_gl.zym");
93         precache_model ("models/weapons/w_hagar.zym");
94         precache_model ("models/weapons/w_laser.zym");
95         precache_model ("models/weapons/w_nex.zym");
96         precache_model ("models/weapons/w_rl.zym");
97         precache_model ("models/weapons/w_shotgun.zym");
98         precache_model ("models/weapons/w_uzi.zym");
99
100         // laser for laser-guided weapons
101         precache_model ("models/laser_dot.mdl");
102
103         precache_sound ("misc/null.wav");
104         precache_sound ("misc/armor1.wav");
105         precache_sound ("misc/armor25.wav");
106         precache_sound ("misc/armorimpact.wav");
107         precache_sound ("misc/bodyimpact1.wav");
108         precache_sound ("misc/bodyimpact2.wav");
109         precache_sound ("misc/gib.wav");
110         precache_sound ("misc/gib_splat01.wav");
111         precache_sound ("misc/gib_splat02.wav");
112         precache_sound ("misc/gib_splat03.wav");
113         precache_sound ("misc/gib_splat04.wav");
114         //precache_sound ("misc/h2ohit.wav");
115         precache_sound ("misc/hit.wav");
116         precache_sound ("misc/footstep01.wav");
117         precache_sound ("misc/footstep02.wav");
118         precache_sound ("misc/footstep03.wav");
119         precache_sound ("misc/footstep04.wav");
120         precache_sound ("misc/footstep05.wav");
121         precache_sound ("misc/footstep06.wav");
122         precache_sound ("misc/hitground1.ogg");
123         precache_sound ("misc/hitground2.ogg");
124         precache_sound ("misc/hitground3.ogg");
125         precache_sound ("misc/hitground4.ogg");
126         precache_sound ("misc/itempickup.ogg");
127         precache_sound ("misc/itemrespawn.ogg");
128         precache_sound ("misc/jumppad.ogg");
129         precache_sound ("misc/mediumhealth.ogg");
130         precache_sound ("misc/megahealth.ogg");
131         precache_sound ("misc/minihealth.ogg");
132         precache_sound ("misc/powerup.ogg");
133         precache_sound ("misc/powerup_shield.ogg");
134         precache_sound ("misc/talk.wav");
135         precache_sound ("misc/teleport.ogg");
136         precache_sound ("plats/medplat1.wav");
137         precache_sound ("plats/medplat2.wav");
138         precache_sound ("player/lava.wav");
139         precache_sound ("player/slime.wav");
140         precache_sound ("weapons/crylink_fire.ogg");
141         precache_sound ("weapons/electro_bounce.ogg");
142         precache_sound ("weapons/electro_fire.ogg");
143         precache_sound ("weapons/electro_fire2.ogg");
144         precache_sound ("weapons/electro_fly.wav");
145         precache_sound ("weapons/electro_impact.ogg");
146         precache_sound ("weapons/electro_impact_combo.ogg");
147         //precache_sound ("weapons/grenade_bounce.ogg");
148         precache_sound ("weapons/grenade_bounce1.ogg");
149         precache_sound ("weapons/grenade_bounce2.ogg");
150         precache_sound ("weapons/grenade_bounce3.ogg");
151         precache_sound ("weapons/grenade_fire.ogg");
152         precache_sound ("weapons/grenade_impact.ogg");
153         precache_sound ("weapons/hagar_fire.ogg");
154         precache_sound ("weapons/hagexp1.ogg");
155         precache_sound ("weapons/hagexp2.ogg");
156         precache_sound ("weapons/hagexp3.ogg");
157         precache_sound ("weapons/hook_fire.ogg");
158         precache_sound ("weapons/hook_impact.ogg");
159         precache_sound ("weapons/lasergun_fire.ogg");
160         precache_sound ("weapons/laserimpact.ogg");
161         precache_sound ("weapons/nexfire.ogg");
162         precache_sound ("weapons/neximpact.ogg");
163         precache_sound ("weapons/ric1.ogg");
164         precache_sound ("weapons/ric2.ogg");
165         precache_sound ("weapons/ric3.ogg");
166         precache_sound ("weapons/rocket_fire.ogg");
167         precache_sound ("weapons/rocket_fly.wav");
168         precache_sound ("weapons/rocket_impact.ogg");
169         precache_sound ("weapons/rocket_det.ogg");
170         precache_sound ("weapons/shotgun_fire.ogg");
171         precache_sound ("weapons/tink1.ogg");
172         precache_sound ("weapons/uzi_fire.ogg");
173         precache_sound ("weapons/weapon_switch.ogg");
174         precache_sound ("weapons/weaponpickup.ogg");
175         precache_sound ("weapons/strength_fire.ogg");
176
177         //precache_sound ("announce/male/kill10.ogg");
178         //precache_sound ("announce/male/kill15.ogg");
179         //precache_sound ("announce/male/kill20.ogg");
180         //precache_sound ("announce/male/kill25.ogg");
181         //precache_sound ("announce/male/kill3.ogg");
182         //precache_sound ("announce/male/kill30.ogg");
183         //precache_sound ("announce/male/kill4.ogg");
184         //precache_sound ("announce/male/kill5.ogg");
185         //precache_sound ("announce/male/kill6.ogg");
186         //precache_sound ("announce/male/mapkill1.ogg");
187         //precache_sound ("announce/robotic/last_second_save.ogg");
188         //precache_sound ("announce/robotic/narrowly_averted.ogg");
189         //precache_sound ("minstagib/mockery.ogg");
190
191         // announcer sounds - male
192         precache_sound ("announcer/male/03kills.ogg");
193         precache_sound ("announcer/male/05kills.ogg");
194         precache_sound ("announcer/male/10kills.ogg");
195         precache_sound ("announcer/male/15kills.ogg");
196         precache_sound ("announcer/male/20kills.ogg");
197         precache_sound ("announcer/male/25kills.ogg");
198         precache_sound ("announcer/male/30kills.ogg");
199         precache_sound ("announcer/male/botlike.ogg");
200         precache_sound ("announcer/male/electrobitch.ogg");
201         precache_sound ("announcer/male/welcome.ogg");
202         precache_sound ("announcer/male/yoda.ogg");
203
204         // announcer sounds - robotic
205         precache_sound ("announcer/robotic/1fragleft.ogg");
206         precache_sound ("announcer/robotic/1minuteremains.ogg");
207         precache_sound ("announcer/robotic/2fragsleft.ogg");
208         precache_sound ("announcer/robotic/3fragsleft.ogg");
209         precache_sound ("announcer/robotic/lastsecond.ogg");
210         precache_sound ("announcer/robotic/narrowly.ogg");
211
212         // plays music for the level if there is any
213         if (self.noise)
214         {
215                 precache_sound (self.noise);
216                 ambientsound ('0 0 0', self.noise, 1.00, ATTN_NONE);
217         }
218
219                 // 0 normal
220         lightstyle(0, "m");
221
222         // 1 FLICKER (first variety)
223         lightstyle(1, "mmnmmommommnonmmonqnmmo");
224
225         // 2 SLOW STRONG PULSE
226         lightstyle(2, "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba");
227
228         // 3 CANDLE (first variety)
229         lightstyle(3, "mmmmmaaaaammmmmaaaaaabcdefgabcdefg");
230
231         // 4 FAST STROBE
232         lightstyle(4, "mamamamamama");
233
234         // 5 GENTLE PULSE 1
235         lightstyle(5,"jklmnopqrstuvwxyzyxwvutsrqponmlkj");
236
237         // 6 FLICKER (second variety)
238         lightstyle(6, "nmonqnmomnmomomno");
239
240         // 7 CANDLE (second variety)
241         lightstyle(7, "mmmaaaabcdefgmmmmaaaammmaamm");
242
243         // 8 CANDLE (third variety)
244         lightstyle(8, "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa");
245
246         // 9 SLOW STROBE (fourth variety)
247         lightstyle(9, "aaaaaaaazzzzzzzz");
248
249         // 10 FLUORESCENT FLICKER
250         lightstyle(10, "mmamammmmammamamaaamammma");
251
252         // 11 SLOW PULSE NOT FADE TO BLACK
253         lightstyle(11, "abcdefghijklmnopqrrqponmlkjihgfedcba");
254
255         // styles 32-62 are assigned by the light program for switchable lights
256
257         // 63 testing
258         lightstyle(63, "a");
259
260         player_count = 0;
261         lms_dead_count = 0;
262         lms_lowest_lives = 0;
263         lms_next_place = 0;
264
265         InitGameplayMode();
266         //if (cvar("g_domination"))
267         //      dom_init();
268
269         // Urrebot setting
270         urrebots_navopt = cvar("urrebots_navopt");
271
272         local entity head;
273         head = nextent(world);
274         maxclients = 0;
275         while(head)
276         {
277                 maxclients++;
278                 head = nextent(head);
279         }
280
281         GameLogInit(); // prepare everything
282         if(cvar("sv_eventlog"))
283         {
284                 local string s;
285                 GameLogEcho(":logversion:2", FALSE);
286                 s = strcat(cvar_string("sv_eventlog_files_counter"), ".");
287                 s = strcat(s, ftos(random()));
288                 GameLogEcho(strcat(":gamestart:", GetMapname(), ":", s), FALSE);
289                 s = ":gameinfo:mutators:LIST";
290                 if(cvar("g_grappling_hook"))
291                         s = strcat(s, ":grappling_hook");
292                 if(!cvar("g_use_ammunition"))
293                         s = strcat(s, ":no_use_ammunition");
294                 if(!cvar("g_pickup_items"))
295                         s = strcat(s, ":no_pickup_items");
296                 if(cvar("g_instagib"))
297                         s = strcat(s, ":instagib");
298                 if(cvar("g_rocketarena"))
299                         s = strcat(s, ":rockerarena");
300                 if(cvar("g_nixnex"))
301                         s = strcat(s, ":nixnex");
302                 if(cvar("g_vampire"))
303                         s = strcat(s, ":vampire");
304                 if(cvar("g_laserguided_missile"))
305                         s = strcat(s, ":laserguided_missile");
306                 if(cvar("g_norecoil"))
307                         s = strcat(s, ":norecoil");
308                 if(cvar("g_midair"))
309                         s = strcat(s, ":midair");
310                 if(cvar("g_minstagib"))
311                         s = strcat(s, ":minstagib");
312                 GameLogEcho(s, FALSE);
313                 GameLogEcho(":gameinfo:end", FALSE);
314         }
315
316         cvar_set("nextmap", "");
317 }
318
319 void light (void)
320 {
321         makestatic (self);
322 }
323
324 float( string pFilename ) TryFile =
325 {
326         local float lHandle;
327         dprint("TryFile(\"", pFilename, "\")\n");
328         lHandle = fopen( pFilename, FILE_READ );
329         if( lHandle != -1 ) {
330                 fclose( lHandle );
331                 return TRUE;
332         } else {
333                 return FALSE;
334         }
335 };
336
337 string GetMapname()
338 {
339         if (game == GAME_DEATHMATCH)
340                 return strcat("dm_", mapname);
341         else if (game == GAME_TEAM_DEATHMATCH)
342                 return strcat("tdm_", mapname);
343         else if (game == GAME_DOMINATION)
344                 return strcat("dom_", mapname);
345         else if (game == GAME_CTF)
346                 return strcat("ctf_", mapname);
347         else if (game == GAME_RUNEMATCH)
348                 return strcat("rune_", mapname);
349         else if (game == GAME_LMS)
350                 return strcat("lms_", mapname);
351         return strcat("dm_", mapname);
352 }
353
354 float GetMaplistPosition()
355 {
356         float pos, f;
357         string map, s;
358
359         map = strzone(GetMapname());
360         pos = 0;
361         f = tokenize(cvar_string("g_maplist"));
362
363         while(pos < f)
364         {
365                 s = strzone(argv(pos));
366                 if(s == map)
367                 {
368                         strunzone(s);
369                         strunzone(map);
370                         return pos;
371                 }
372                 strunzone(s);
373                 pos++;
374         }
375
376         strunzone(map);
377         // resume normal maplist rotation if current map is not in g_maplist
378         return cvar("g_maplist_index");          
379 }
380
381 void() GotoNextMap =
382 {
383         //local string nextmap;
384         //local float n, nummaps;
385         //local string s;
386         string exit_cfg;
387         if (alreadychangedlevel)
388                 return;
389         alreadychangedlevel = TRUE;
390         if (cvar("samelevel")) // if samelevel is set, stay on same level
391         {
392                 // this does not work because it tries to exec maps/nexdm01.mapcfg (which doesn't exist, it should be trying maps/dm_nexdm01.mapcfg for example)
393                 //localcmd(strcat("exec \"maps/", mapname, ".mapcfg\"\n"));
394                 // so instead just restart the current map using the restart command (DOES NOT WORK PROPERLY WITH exit_cfg STUFF)
395                 localcmd("restart\n");
396                 //changelevel (mapname);
397                 return;
398         }
399
400         // if an exit cfg is defined by exiting map, exec it.
401         exit_cfg = cvar_string("exit_cfg");
402         if(exit_cfg != "")
403                 localcmd(strcat("exec \"", exit_cfg, "\"\n"));
404
405         ResetGameCvars();
406
407
408         if (cvar("lastlevel"))
409         {
410                 localcmd(strcat("set lastlevel 0\n"));
411                 localcmd(strcat("togglemenu\n"));
412         }
413         else
414         {
415                 // method 0
416                 local float lCurrent;
417                 local float lSize;
418                 local float lOldCurrent;
419                 local float lBeforeCurrent;
420                 local float pass;
421
422                 if(TryFile(strcat("maps/", cvar_string("nextmap"), ".mapcfg")))
423                 {
424                         localcmd(strcat("exec \"maps/", cvar_string("nextmap"), ".mapcfg\"\n"));
425                         return;
426                 }
427
428                 pass = 0;
429                 while (pass < 2)
430                 {
431                         pass = pass + 1;
432                         local string temp;
433                         temp = cvar_string( "g_maplist" );
434                         if(temp == "")
435                         {
436                                 bprint( "Maplist is empty!  Resetting it to default map list.\n" );
437                                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
438                                 temp = cvar_string( "g_maplist" );
439                         }
440                         temp = strzone(temp);
441                         dprint("g_maplist is ", temp, "\n");
442                         lCurrent = max(0, GetMaplistPosition());
443                         lSize = tokenize( temp );
444                         lOldCurrent = lCurrent;
445                         dprint(ftos(lOldCurrent), " / ", ftos(lSize), " (start)\n");
446
447                         // if we want a random map selection...
448                         if(cvar("g_maplist_selectrandom") && lSize > 1)
449                         {
450                                 lBeforeCurrent = lCurrent - 1;
451                                 if(lBeforeCurrent < 0)
452                                         lBeforeCurrent = lSize - 1;
453                                 lCurrent = ceil(random() * (lSize - 1)) - 1; // random in 0..lsize-2
454                                 if(lCurrent >= lBeforeCurrent)
455                                         lCurrent += 1;
456                                 // choose any map except for the current one
457                         }
458
459                         while( 1 ) {
460                                 local string lFilename;
461
462                                 lCurrent = lCurrent + 1;
463                                 dprint(ftos(lCurrent), " / ", ftos(lSize), "\n");
464                                 if( lCurrent >= lSize ) {
465                                         lCurrent = 0;
466                                 }
467                                 if( lOldCurrent == lCurrent ) {
468                                         // we couldn't find a valid map at all
469                                         if (pass == 1)
470                                         {
471                                                 bprint( "Maplist is bad/messed up. Not one good mapcfg can be found in it!  Resetting it to default map list.\n" );
472                                                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
473                                                 // let the loop restart with the default list now
474                                         }
475                                         else
476                                         {
477                                                 bprint( "Both g_maplist and g_maplist_defaultlist are messed up!  Complain to developers!\n" );
478                                                 localcmd( "disconnect\n" );
479                                         }
480                                         break;
481                                 }
482
483                                 cvar_set( "g_maplist_index", ftos( lCurrent ) );
484
485                                 lFilename = strcat( "maps/", argv( lCurrent ), ".mapcfg" );
486                                 if( TryFile( lFilename ) ) {
487                                         localcmd(strcat("exec \"", lFilename ,"\"\n"));
488                                         pass = 2; // exit the outer loop
489                                         break;
490                                 } else {
491                                         dprint( "Couldn't find '", lFilename, "'..\n" );
492                                 }
493                                 //changelevel( argv( lCurrent ) );
494                         }
495                         strunzone(temp);
496                 }
497
498                 /*
499                 // method 1
500
501                 //local entity pos;
502                 local float fh;
503                 local string line;
504
505                 // restart current map if no cycle is found
506                 nextmap = strzone(mapname);
507                 fh = fopen("maplist.cfg", FILE_READ);
508                 if (fh >= 0)
509                 {
510                         while (1)
511                         {
512                                 line = fgets(fh);
513                                 if (nextmap == mapname)
514                                 {
515                                         strunzone(nextmap);
516                                         nextmap = strzone(line);
517                                 }
518                                 if (!line)
519                                         break;
520                                 if (line == mapname)
521                                 {
522                                         line = fgets(fh);
523                                         if (!line)
524                                                 break;
525                                         strunzone(nextmap);
526                                         nextmap = strzone(line);
527                                         break;
528                                 }
529                         }
530                         fclose(fh);
531                 }
532                 changelevel (nextmap);
533                 strunzone(nextmap);*/
534
535                 // method 3
536                 /*
537                 s = cvar_string("g_maplist");
538                 nummaps = tokenize(s);
539                 // if no map list, restart current one
540                 nextmap = mapname;
541                 if (nummaps >= 1)
542                 {
543                         n = 0;
544                         while (n < nummaps)
545                         {
546                                 if (argv(n) == mapname)
547                                         break;
548                                 n = n + 1;
549                         }
550                         n = n + 1;
551                         if (n >= nummaps)
552                                 n = 0;
553                         nextmap = argv(n);
554                 }
555                 changelevel (nextmap);
556                 */
557         }
558 };
559
560
561 /*
562 ============
563 IntermissionThink
564
565 When the player presses attack or jump, change to the next level
566 ============
567 */
568 void() IntermissionThink =
569 {
570         if (time < intermission_exittime)
571                 return;
572
573         if (time < intermission_exittime + 10 && !self.button0 && !self.button1 && !self.button2 && !self.button3)
574                 return;
575
576         GotoNextMap ();
577 };
578
579 /*
580 ============
581 FindIntermission
582
583 Returns the entity to view from
584 ============
585 */
586 /*
587 entity() FindIntermission =
588 {
589         local   entity spot;
590         local   float cyc;
591
592 // look for info_intermission first
593         spot = find (world, classname, "info_intermission");
594         if (spot)
595         {       // pick a random one
596                 cyc = random() * 4;
597                 while (cyc > 1)
598                 {
599                         spot = find (spot, classname, "info_intermission");
600                         if (!spot)
601                                 spot = find (spot, classname, "info_intermission");
602                         cyc = cyc - 1;
603                 }
604                 return spot;
605         }
606
607 // then look for the start position
608         spot = find (world, classname, "info_player_start");
609         if (spot)
610                 return spot;
611
612 // testinfo_player_start is only found in regioned levels
613         spot = find (world, classname, "testplayerstart");
614         if (spot)
615                 return spot;
616
617 // then look for the start position
618         spot = find (world, classname, "info_player_deathmatch");
619         if (spot)
620                 return spot;
621
622         //objerror ("FindIntermission: no spot");
623         return world;
624 };
625 */
626
627 /*
628 ===============================================================================
629
630 RULES
631
632 ===============================================================================
633 */
634
635 void() DumpStats =
636 {
637         local float file;
638         local string s;
639
640         if(cvar("_printstats"))
641                 cvar_set("_printstats", "0");
642         else if(!gameover)
643                 return;
644
645         if(gameover)
646                 s = ":scores:";
647         else
648                 s = ":status:";
649
650         s = strcat(s, GetMapname(), ":", ftos(rint(time)));
651
652         if(cvar("sv_eventlog") && gameover)
653                 GameLogEcho(s, FALSE);
654         else if(cvar("sv_logscores_console"))
655                 ServerConsoleEcho(s, FALSE);
656         if(cvar("sv_logscores_file"))
657         {
658                 file = fopen(cvar_string("sv_logscores_filename"), FILE_APPEND);
659                 fputs(file, strcat(s, "\n"));
660         }
661
662         other = findchainflags(flags, FL_CLIENT);
663         while (other)
664         {
665                 if ((clienttype(other) == CLIENTTYPE_REAL) || (clienttype(other) == CLIENTTYPE_BOT && cvar("sv_logscores_bots")))
666                 {
667                         s = strcat(":player:", ftos(other.frags), ":");
668                         s = strcat(s, ftos(other.deaths), ":");
669                         s = strcat(s, ftos(rint(time - other.jointime)), ":");
670                         s = strcat(s, ftos(other.team), ":");
671
672                         if(cvar("sv_logscores_file"))
673                                 fputs(file, strcat(s, other.netname, "\n"));
674                         if(cvar("sv_eventlog") && gameover)
675                                 GameLogEcho(strcat(s, ftos(other.playerid), ":", other.netname), TRUE);
676                         else if(cvar("sv_logscores_console"))
677                                 ServerConsoleEcho(strcat(s, other.netname), TRUE);
678                 }
679                 other = other.chain;
680         }
681
682         if(cvar("sv_eventlog") && gameover)
683                 GameLogEcho(":end", FALSE);
684         else if(cvar("sv_logscores_console"))
685                 ServerConsoleEcho(":end", FALSE);
686         if(cvar("sv_logscores_file"))
687         {
688                 fputs(file, ":end\n");
689                 fclose(file);
690         }
691 }
692
693
694 /*
695 go to the next level for deathmatch
696 only called if a time or frag limit has expired
697 */
698 void() NextLevel =
699 {
700         gameover = TRUE;
701
702         intermission_running = 1;
703
704 // enforce a wait time before allowing changelevel
705         intermission_exittime = time + cvar("sv_mapchange_delay");
706
707         WriteByte (MSG_ALL, SVC_CDTRACK);
708         WriteByte (MSG_ALL, 3);
709         WriteByte (MSG_ALL, 3);
710
711         //pos = FindIntermission ();
712
713         VoteReset();
714
715         DumpStats();
716
717         if(cvar("sv_eventlog"))
718                 GameLogEcho(":gameover", FALSE);
719
720         GameLogClose();
721
722         other = findchainflags(flags, FL_CLIENT);
723         while (other != world)
724         {
725                 //other.nextthink = time + 0.5;
726                 other.takedamage = DAMAGE_NO;
727                 other.solid = SOLID_NOT;
728                 other.movetype = MOVETYPE_NONE;
729                 other.angles = other.v_angle;
730                 other.angles_x = other.angles_x * -1;
731
732                 self = other;
733                 weapon_action(other.weapon, WR_IDLE);
734
735                 if(clienttype(other) == CLIENTTYPE_REAL)
736                 if(cvar("sv_autoscreenshot"))
737                         stuffcmd(other, "screenshot\necho \"^5A screenshot has been taken at request of the server.\"\n");
738
739                 /*
740                 if (pos != world);
741                 {
742                         other.modelindex = 0;
743                         other.weaponentity = world; // remove weapon model
744                         other.view_ofs = '0 0 0';
745                         other.angles = other.v_angle = pos.mangle;
746                         if (!other.angles)
747                         {
748                                 other.angles = other.v_angle = pos.angles;
749                                 other.v_angle_x = other.v_angle_x * -1;
750                         }
751                         other.fixangle = TRUE;          // turn this way immediately
752                         setorigin (other, pos.origin);
753                 }
754                 */
755                 other = other.chain;
756         }
757
758         WriteByte (MSG_ALL, SVC_INTERMISSION);
759 };
760
761 /*
762 ============
763 CheckRules_Player
764
765 Exit deathmatch games upon conditions
766 ============
767 */
768 void() CheckRules_Player =
769 {
770         if (gameover)   // someone else quit the game already
771                 return;
772
773         // fixme: don't check players; instead check dom_team and ctf_team entities
774         //   (div0: and that in CheckRules_World please)
775 };
776
777 float checkrules_oneminutewarning;
778 float checkrules_leaderfrags;
779 float tdm_max_score, tdm_old_score;
780
781 float checkrules_equality;
782 float checkrules_overtimewarning;
783 float checkrules_overtimeend;
784
785 void() InitiateOvertime =
786 {
787         if(!checkrules_overtimeend)
788                 checkrules_overtimeend = time + 60 * cvar("timelimit_maxovertime");
789 }
790
791 float WINNING_NO = 0; // no winner, but time limits may terminate the game
792 float WINNING_YES = 1; // winner found
793 float WINNING_NEVER = 2; // no winner, enter overtime if time limit is reached
794 float WINNING_STARTOVERTIME = 3; // no winner, enter overtime NOW
795
796 float(float fraglimitreached, float equality) GetWinningCode =
797 {
798         if(equality)
799                 if(fraglimitreached)
800                         return WINNING_STARTOVERTIME;
801                 else
802                         return WINNING_NEVER;
803         else
804                 if(fraglimitreached)
805                         return WINNING_YES;
806                 else
807                         return WINNING_NO;
808 }
809
810 // LMS winning condition: game terminates if and only if there's at most one
811 // one player who's living lives. Top two scores being equal cancels the time
812 // limit.
813 float() WinningCondition_LMS =
814 {
815         entity head;
816
817         if(lms_dead_count < 0)
818                 lms_dead_count = 0;
819
820         if(player_count > 1 && lms_dead_count >= player_count - 1)
821                 return WINNING_YES; // He's the last man standing!
822
823         if((player_count == 1 && lms_dead_count == 1))
824                 return WINNING_YES; // All dead... (n:n is handled by the test above)
825
826         // When we get here, we have at least two players who are actually LIVING,
827         // or one player who is still waiting for a victim to join the server. Now
828         // check if the top two players have equal score.
829
830         checkrules_leaderfrags = 0;
831         head = findchain(classname, "player");
832         checkrules_equality = FALSE;
833         while (head)
834         {
835                 if(head.frags > checkrules_leaderfrags)
836                         checkrules_leaderfrags = head.frags;
837                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
838                         checkrules_equality = TRUE;
839                 head = head.chain;
840         }
841
842         // The top two players have the same amount of lives? No timelimit then,
843         // enter overtime...
844
845         if(checkrules_equality)
846                 return WINNING_NEVER;
847
848         // Top two have different scores? Way to go for our beloved TIMELIMIT!
849         return WINNING_NO;
850 }
851
852 // DM winning condition: game terminates if a player reached the fraglimit,
853 // unless the first two players have the same score. The latter case also
854 // breaks the time limit.
855 float(float fraglimit) WinningCondition_MaxIndividualScore =
856 {
857         float checkrules_oldleaderfrags;
858         entity head;
859
860         checkrules_oldleaderfrags = checkrules_leaderfrags;
861         checkrules_leaderfrags = 0;
862         head = findchain(classname, "player");
863         checkrules_equality = FALSE;
864         while (head)
865         {
866                 if(head.frags > checkrules_leaderfrags)
867                         checkrules_leaderfrags = head.frags;
868                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
869                         checkrules_equality = TRUE;
870                 head = head.chain;
871         }
872
873         if (!cvar("g_runematch"))
874                 if (checkrules_leaderfrags != checkrules_oldleaderfrags)
875                 {
876                         if (checkrules_leaderfrags == fraglimit - 1)
877                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
878                         else if (checkrules_leaderfrags == fraglimit - 2)
879                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
880                         else if (checkrules_leaderfrags == fraglimit - 3)
881                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
882                 }
883
884         return GetWinningCode(fraglimit && checkrules_leaderfrags >= fraglimit, checkrules_equality);
885 }
886
887 float(float fraglimit) WinningConditionBase_Teamplay =
888 {
889         tdm_old_score = tdm_max_score;
890         tdm_max_score = max(team1_score, team2_score, team3_score, team4_score);
891
892         checkrules_equality =
893                 (tdm_max_score > 0)
894                 &&
895                 (
896                           (team1_score == tdm_max_score)
897                         + (team2_score == tdm_max_score)
898                         + (team3_score == tdm_max_score)
899                         + (team4_score == tdm_max_score)
900                         >= 2);
901
902         if(!cvar("g_runematch") && !cvar("g_domination"))
903                 if(tdm_max_score != tdm_old_score)
904                 {
905                         if(tdm_max_score == fraglimit - 1)
906                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
907                         else if(tdm_max_score == fraglimit - 2)
908                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
909                         else if(tdm_max_score == fraglimit - 3)
910                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
911                 }
912
913         return GetWinningCode(fraglimit && tdm_max_score >= fraglimit, checkrules_equality);
914 }
915
916 // TDM winning condition: game terminates if a team's score sum reached the
917 // fraglimit, unless the first two teams have the same total score. The latter
918 // case also breaks the time limit.
919 float(float fraglimit) WinningCondition_MaxTeamSum =
920 {
921         entity head;
922
923         team1_score = team2_score = team3_score = team4_score = 0;
924
925         head = findchain(classname, "player");
926         while (head)
927         {
928                 if(head.team == COLOR_TEAM1)
929                         team1_score += head.frags;
930                 else if(head.team == COLOR_TEAM2)
931                         team2_score += head.frags;
932                 else if(head.team == COLOR_TEAM3)
933                         team3_score += head.frags;
934                 else if(head.team == COLOR_TEAM4)
935                         team4_score += head.frags;
936                 head = head.chain;
937         }
938
939         return WinningConditionBase_Teamplay(fraglimit);
940 }
941
942 // DOM/CTF winning condition: game terminates if the max of a team's players'
943 // score reached the fraglimit, unless the first two teams have the same
944 // maximum score. The latter case also breaks the time limit.
945 float(float fraglimit) WinningCondition_MaxTeamMax =
946 {
947         entity head;
948
949         team1_score = team2_score = team3_score = team4_score = 0;
950
951         head = findchain(classname, "player");
952         while (head)
953         {
954                 if(head.team == COLOR_TEAM1)
955                 {
956                         if(head.frags > team1_score)
957                                 team1_score = head.frags;
958                 }
959                 else if(head.team == COLOR_TEAM2)
960                 {
961                         if(head.frags > team2_score)
962                                 team2_score = head.frags;
963                 }
964                 else if(head.team == COLOR_TEAM3)
965                 {
966                         if(head.frags > team3_score)
967                                 team3_score = head.frags;
968                 }
969                 else if(head.team == COLOR_TEAM4)
970                 {
971                         if(head.frags > team4_score)
972                                 team4_score = head.frags;
973                 }
974                 head = head.chain;
975         }
976
977         return WinningConditionBase_Teamplay(fraglimit);
978 }
979
980
981 // Add/remove bots if needed
982 void() CheckRules_Minplayers =
983 {
984         float f;
985
986         if(cvar("minplayers") >= maxclients)
987                 cvar_set("minplayers", ftos(maxclients - 1));
988
989         f = cvar("minplayers") - (player_count - bot_number);
990         if((player_count - bot_number) < 1)
991                 f = 0;
992
993         if(cvar("bot_number") != f)
994         {
995                 if(cvar("minplayers") != f)
996                         cvar_set("bot_number", ftos(f));
997                 else
998                         cvar_set("bot_number", "0");
999         }
1000 }
1001
1002
1003 /*
1004 ============
1005 CheckRules_World
1006
1007 Exit deathmatch games upon conditions
1008 ============
1009 */
1010 void() CheckRules_World =
1011 {
1012         local float status;
1013         local float timelimit;
1014         local float fraglimit;
1015
1016         VoteThink();
1017
1018         if (intermission_running)
1019                 if (time >= intermission_exittime + 60)
1020                 {
1021                         GotoNextMap();
1022                         return;
1023                 }
1024
1025         if (gameover)   // someone else quit the game already
1026                 return;
1027
1028         DumpStats();
1029
1030         timelimit = cvar("timelimit") * 60;
1031         fraglimit = cvar("fraglimit");
1032
1033         if (timelimit && time >= timelimit)
1034                 InitiateOvertime();
1035
1036         if (checkrules_overtimeend && time >= checkrules_overtimeend)
1037         {
1038                 NextLevel();
1039                 return;
1040         }
1041
1042         if(!checkrules_overtimewarning && checkrules_overtimeend)
1043         {
1044                 checkrules_overtimewarning = TRUE;
1045                 //sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1046                 bcenterprint("^3Now playing ^1OVERTIME^3!\n\n^3Keep fragging until we have a ^1winner^3!");
1047         }
1048
1049         if (!checkrules_oneminutewarning && timelimit && time > timelimit - 60)
1050         {
1051                 checkrules_oneminutewarning = TRUE;
1052                 sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1053         }
1054
1055         if(cvar("minplayers"))
1056                 CheckRules_Minplayers();
1057
1058         status = WINNING_NO;
1059         if(cvar("g_lms"))
1060         {
1061                 status = WinningCondition_LMS();
1062         }
1063         else
1064         {
1065                 if(teams_matter)
1066                 {
1067                         if(cvar("g_tdm") || cvar("g_runematch"))
1068                                 status = WinningCondition_MaxTeamSum(fraglimit);
1069                         else if(cvar("g_ctf") || cvar("g_domination"))
1070                                 status = WinningCondition_MaxTeamMax(fraglimit);
1071                         else
1072                         {
1073                                 dprint("div0: How can this happen?\n");
1074                                 status = WinningCondition_MaxTeamMax(fraglimit);
1075                         }
1076                 }
1077                 else
1078                         status = WinningCondition_MaxIndividualScore(fraglimit);
1079         }
1080
1081         if(status == WINNING_STARTOVERTIME)
1082         {
1083                 status = WINNING_NEVER;
1084                 InitiateOvertime();
1085         }
1086
1087         if(checkrules_overtimeend)
1088                 if(status != WINNING_NEVER)
1089                         status = WINNING_YES;
1090
1091         if(status == WINNING_YES)
1092                 NextLevel();
1093 };