]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_world.qc
developer 1 -> try to find that LMS bug... switch developer to 1 when it happens...
[divverent/nexuiz.git] / data / qcsrc / server / g_world.qc
1 string GetMapname();
2 void GotoNextMap();
3
4 void SetDefaultAlpha()
5 {
6         if(cvar("g_cloaked"))
7         {
8                 default_player_alpha = cvar("g_balance_cloaked_alpha");
9         }
10         else
11         {
12                 default_player_alpha = cvar("g_player_alpha");
13                 if(default_player_alpha <= 0)
14                         default_player_alpha = 1;
15         }
16 }
17
18 void GotoFirstMap()
19 {
20         if(cvar("_sv_init"))
21         {
22                 cvar_set("_sv_init", "0");
23                 tokenize(cvar_string("g_maplist"));
24                 if(argv(0) != GetMapname())
25                 {
26                         cvar_set("nextmap", argv(0));
27                         GotoNextMap();
28                 }
29         }
30 }
31
32 float world_already_spawned;
33 void worldspawn (void)
34 {
35         if(world_already_spawned)
36                 error("world already spawned - you may have EXACTLY ONE worldspawn!");
37         world_already_spawned = TRUE;
38         // Precache all player models
39         // Workaround for "invisible players"
40         precache_model("models/player/carni.zym");
41         precache_model("models/player/crash.zym");
42         precache_model("models/player/grunt.zym");
43         precache_model("models/player/headhunter.zym");
44         precache_model("models/player/insurrectionist.zym");
45         precache_model("models/player/jeandarc.zym");
46         precache_model("models/player/lurk.zym");
47         precache_model("models/player/lycanthrope.zym");
48         precache_model("models/player/marine.zym");
49         precache_model("models/player/nexus.zym");
50         precache_model("models/player/pyria.zym");
51         precache_model("models/player/shock.zym");
52         precache_model("models/player/skadi.zym");
53         precache_model("models/player/specop.zym");
54         precache_model("models/player/visitant.zym");
55
56         //precache_model ("progs/beam.mdl");
57         precache_model ("models/bullet.mdl");
58         precache_model ("models/casing_bronze.mdl");
59         precache_model ("models/casing_shell.mdl");
60         precache_model ("models/casing_steel.mdl");
61         precache_model ("models/ebomb.mdl");
62         precache_model ("models/elaser.mdl");
63         precache_model ("models/flash.md3");
64         precache_model ("models/gibs/bloodyskull.md3");
65         precache_model ("models/gibs/chunk.mdl");
66         precache_model ("models/gibs/eye.md3");
67         precache_model ("models/gibs/gib1.md3");
68         //precache_model ("models/gibs/gib2.md3");
69         //precache_model ("models/gibs/gib3.md3");
70         //precache_model ("models/gibs/gib4.md3");
71         precache_model ("models/gibs/gib5.md3");
72         //precache_model ("models/gibs/gib6.md3");
73         precache_model ("models/gibs/gib1.mdl");
74         precache_model ("models/gibs/gib2.mdl");
75         precache_model ("models/gibs/gib3.mdl");
76         precache_model ("models/grenademodel.md3");
77         precache_model ("models/hagarmissile.mdl");
78         precache_model ("models/items/a_bullets.mdl");
79         precache_model ("models/items/a_cells.md3");
80         precache_model ("models/items/a_rockets.md3");
81         precache_model ("models/items/a_shells.md3");
82         precache_model ("models/items/g_a1.md3");
83         precache_model ("models/items/g_a25.md3");
84         precache_model ("models/items/g_h1.md3");
85         precache_model ("models/items/g_h25.md3");
86         precache_model ("models/items/g_h100.md3");
87         precache_model ("models/items/g_invincible.md3");
88         precache_model ("models/items/g_strength.md3");
89         precache_model ("models/laser.mdl");
90         precache_model ("models/misc/chatbubble.spr");
91         precache_model ("models/misc/teambubble.spr");
92         precache_model ("models/nexflash.md3");
93         precache_model ("models/plasma.mdl");
94         precache_model ("models/plasmatrail.mdl");
95         precache_model ("models/rocket.md3");
96         //precache_model ("models/sprites/grenexpl.spr");
97         precache_model ("models/runematch/rune.mdl");
98         precache_model ("models/runematch/curse.mdl");
99         //precache_model ("models/sprites/hagar.spr");
100         //precache_model ("models/sprites/muzzleflash.spr32");
101         //precache_model ("models/sprites/electrocombo.spr32");
102         //precache_model ("models/sprites/plasmahitwall.spr32");
103         //precache_model ("models/sprites/plasmashot.spr32");
104         //precache_model ("models/sprites/rockexpl.spr");
105         precache_model ("models/tracer.mdl");
106         precache_model ("models/uziflash.md3");
107         precache_model ("models/weapons/g_crylink.md3");
108         precache_model ("models/weapons/g_electro.md3");
109         precache_model ("models/weapons/g_gl.md3");
110         precache_model ("models/weapons/g_hagar.md3");
111         precache_model ("models/weapons/g_nex.md3");
112         precache_model ("models/weapons/g_rl.md3");
113         precache_model ("models/weapons/g_shotgun.md3");
114         precache_model ("models/weapons/g_uzi.md3");
115         precache_model ("models/weapons/v_crylink.md3");
116         precache_model ("models/weapons/v_electro.md3");
117         precache_model ("models/weapons/v_gl.md3");
118         precache_model ("models/weapons/v_hagar.md3");
119         precache_model ("models/weapons/v_laser.md3");
120         precache_model ("models/weapons/v_nex.md3");
121         precache_model ("models/weapons/v_rl.md3");
122         precache_model ("models/weapons/v_shotgun.md3");
123         precache_model ("models/weapons/v_uzi.md3");
124         precache_model ("models/weapons/w_crylink.zym");
125         precache_model ("models/weapons/w_electro.zym");
126         precache_model ("models/weapons/w_gl.zym");
127         precache_model ("models/weapons/w_hagar.zym");
128         precache_model ("models/weapons/w_laser.zym");
129         precache_model ("models/weapons/w_nex.zym");
130         precache_model ("models/weapons/w_rl.zym");
131         precache_model ("models/weapons/w_shotgun.zym");
132         precache_model ("models/weapons/w_uzi.zym");
133
134         // laser for laser-guided weapons
135         precache_model ("models/laser_dot.mdl");
136
137         precache_sound ("misc/null.wav");
138         precache_sound ("misc/armor1.wav");
139         precache_sound ("misc/armor25.wav");
140         precache_sound ("misc/armorimpact.wav");
141         precache_sound ("misc/bodyimpact1.wav");
142         precache_sound ("misc/bodyimpact2.wav");
143         precache_sound ("misc/gib.wav");
144         precache_sound ("misc/gib_splat01.wav");
145         precache_sound ("misc/gib_splat02.wav");
146         precache_sound ("misc/gib_splat03.wav");
147         precache_sound ("misc/gib_splat04.wav");
148         //precache_sound ("misc/h2ohit.wav");
149         precache_sound ("misc/hit.wav");
150         precache_sound ("misc/footstep01.wav");
151         precache_sound ("misc/footstep02.wav");
152         precache_sound ("misc/footstep03.wav");
153         precache_sound ("misc/footstep04.wav");
154         precache_sound ("misc/footstep05.wav");
155         precache_sound ("misc/footstep06.wav");
156         precache_sound ("misc/hitground1.ogg");
157         precache_sound ("misc/hitground2.ogg");
158         precache_sound ("misc/hitground3.ogg");
159         precache_sound ("misc/hitground4.ogg");
160         precache_sound ("misc/itempickup.ogg");
161         precache_sound ("misc/itemrespawn.ogg");
162         precache_sound ("misc/jumppad.ogg");
163         precache_sound ("misc/mediumhealth.ogg");
164         precache_sound ("misc/megahealth.ogg");
165         precache_sound ("misc/minihealth.ogg");
166         precache_sound ("misc/powerup.ogg");
167         precache_sound ("misc/powerup_shield.ogg");
168         precache_sound ("misc/talk.wav");
169         precache_sound ("misc/teleport.ogg");
170         precache_sound ("plats/medplat1.wav");
171         precache_sound ("plats/medplat2.wav");
172         precache_sound ("player/lava.wav");
173         precache_sound ("player/slime.wav");
174         precache_sound ("weapons/crylink_fire.ogg");
175         precache_sound ("weapons/electro_bounce.ogg");
176         precache_sound ("weapons/electro_fire.ogg");
177         precache_sound ("weapons/electro_fire2.ogg");
178         precache_sound ("weapons/electro_fly.wav");
179         precache_sound ("weapons/electro_impact.ogg");
180         precache_sound ("weapons/electro_impact_combo.ogg");
181         //precache_sound ("weapons/grenade_bounce.ogg");
182         precache_sound ("weapons/grenade_bounce1.ogg");
183         precache_sound ("weapons/grenade_bounce2.ogg");
184         precache_sound ("weapons/grenade_bounce3.ogg");
185         precache_sound ("weapons/grenade_fire.ogg");
186         precache_sound ("weapons/grenade_impact.ogg");
187         precache_sound ("weapons/hagar_fire.ogg");
188         precache_sound ("weapons/hagexp1.ogg");
189         precache_sound ("weapons/hagexp2.ogg");
190         precache_sound ("weapons/hagexp3.ogg");
191         precache_sound ("weapons/hook_fire.ogg");
192         precache_sound ("weapons/hook_impact.ogg");
193         precache_sound ("weapons/lasergun_fire.ogg");
194         precache_sound ("weapons/laserimpact.ogg");
195         precache_sound ("weapons/nexfire.ogg");
196         precache_sound ("weapons/neximpact.ogg");
197         precache_sound ("weapons/ric1.ogg");
198         precache_sound ("weapons/ric2.ogg");
199         precache_sound ("weapons/ric3.ogg");
200         precache_sound ("weapons/rocket_fire.ogg");
201         precache_sound ("weapons/rocket_fly.wav");
202         precache_sound ("weapons/rocket_impact.ogg");
203         precache_sound ("weapons/rocket_det.ogg");
204         precache_sound ("weapons/shotgun_fire.ogg");
205         precache_sound ("weapons/tink1.ogg");
206         precache_sound ("weapons/uzi_fire.ogg");
207         precache_sound ("weapons/weapon_switch.ogg");
208         precache_sound ("weapons/weaponpickup.ogg");
209         precache_sound ("weapons/strength_fire.ogg");
210
211         //precache_sound ("announce/male/kill10.ogg");
212         //precache_sound ("announce/male/kill15.ogg");
213         //precache_sound ("announce/male/kill20.ogg");
214         //precache_sound ("announce/male/kill25.ogg");
215         //precache_sound ("announce/male/kill3.ogg");
216         //precache_sound ("announce/male/kill30.ogg");
217         //precache_sound ("announce/male/kill4.ogg");
218         //precache_sound ("announce/male/kill5.ogg");
219         //precache_sound ("announce/male/kill6.ogg");
220         //precache_sound ("announce/male/mapkill1.ogg");
221         //precache_sound ("announce/robotic/last_second_save.ogg");
222         //precache_sound ("announce/robotic/narrowly_averted.ogg");
223         //precache_sound ("minstagib/mockery.ogg");
224
225         // announcer sounds - male
226         precache_sound ("announcer/male/03kills.ogg");
227         precache_sound ("announcer/male/05kills.ogg");
228         precache_sound ("announcer/male/10kills.ogg");
229         precache_sound ("announcer/male/15kills.ogg");
230         precache_sound ("announcer/male/20kills.ogg");
231         precache_sound ("announcer/male/25kills.ogg");
232         precache_sound ("announcer/male/30kills.ogg");
233         precache_sound ("announcer/male/botlike.ogg");
234         precache_sound ("announcer/male/electrobitch.ogg");
235         precache_sound ("announcer/male/welcome.ogg");
236         precache_sound ("announcer/male/yoda.ogg");
237
238         // announcer sounds - robotic
239         precache_sound ("announcer/robotic/1fragleft.ogg");
240         precache_sound ("announcer/robotic/1minuteremains.ogg");
241         precache_sound ("announcer/robotic/2fragsleft.ogg");
242         precache_sound ("announcer/robotic/3fragsleft.ogg");
243         precache_sound ("announcer/robotic/lastsecond.ogg");
244         precache_sound ("announcer/robotic/narrowly.ogg");
245
246         // plays music for the level if there is any
247         if (self.noise)
248         {
249                 precache_sound (self.noise);
250                 ambientsound ('0 0 0', self.noise, 1.00, ATTN_NONE);
251         }
252
253                 // 0 normal
254         lightstyle(0, "m");
255
256         // 1 FLICKER (first variety)
257         lightstyle(1, "mmnmmommommnonmmonqnmmo");
258
259         // 2 SLOW STRONG PULSE
260         lightstyle(2, "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba");
261
262         // 3 CANDLE (first variety)
263         lightstyle(3, "mmmmmaaaaammmmmaaaaaabcdefgabcdefg");
264
265         // 4 FAST STROBE
266         lightstyle(4, "mamamamamama");
267
268         // 5 GENTLE PULSE 1
269         lightstyle(5,"jklmnopqrstuvwxyzyxwvutsrqponmlkj");
270
271         // 6 FLICKER (second variety)
272         lightstyle(6, "nmonqnmomnmomomno");
273
274         // 7 CANDLE (second variety)
275         lightstyle(7, "mmmaaaabcdefgmmmmaaaammmaamm");
276
277         // 8 CANDLE (third variety)
278         lightstyle(8, "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa");
279
280         // 9 SLOW STROBE (fourth variety)
281         lightstyle(9, "aaaaaaaazzzzzzzz");
282
283         // 10 FLUORESCENT FLICKER
284         lightstyle(10, "mmamammmmammamamaaamammma");
285
286         // 11 SLOW PULSE NOT FADE TO BLACK
287         lightstyle(11, "abcdefghijklmnopqrrqponmlkjihgfedcba");
288
289         // styles 32-62 are assigned by the light program for switchable lights
290
291         // 63 testing
292         lightstyle(63, "a");
293
294         player_count = 0;
295         lms_dead_count = 0;
296         lms_lowest_lives = 0;
297         lms_next_place = 0;
298
299         GotoFirstMap();
300
301         if(cvar("g_campaign"))
302                 CampaignPreInit();
303
304         InitGameplayMode();
305         //if (cvar("g_domination"))
306         //      dom_init();
307
308         local entity head;
309         head = nextent(world);
310         maxclients = 0;
311         while(head)
312         {
313                 maxclients++;
314                 head = nextent(head);
315         }
316
317         GameLogInit(); // prepare everything
318         if(cvar("sv_eventlog"))
319         {
320                 local string s;
321                 GameLogEcho(":logversion:2", FALSE);
322                 s = strcat(cvar_string("sv_eventlog_files_counter"), ".");
323                 s = strcat(s, ftos(random()));
324                 GameLogEcho(strcat(":gamestart:", GetMapname(), ":", s), FALSE);
325                 s = ":gameinfo:mutators:LIST";
326                 if(cvar("g_grappling_hook"))
327                         s = strcat(s, ":grappling_hook");
328                 if(!cvar("g_use_ammunition"))
329                         s = strcat(s, ":no_use_ammunition");
330                 if(!cvar("g_pickup_items"))
331                         s = strcat(s, ":no_pickup_items");
332                 if(cvar("g_instagib"))
333                         s = strcat(s, ":instagib");
334                 if(cvar("g_rocketarena"))
335                         s = strcat(s, ":rockerarena");
336                 if(cvar("g_nixnex"))
337                         s = strcat(s, ":nixnex");
338                 if(cvar("g_vampire"))
339                         s = strcat(s, ":vampire");
340                 if(cvar("g_laserguided_missile"))
341                         s = strcat(s, ":laserguided_missile");
342                 if(cvar("g_norecoil"))
343                         s = strcat(s, ":norecoil");
344                 if(cvar("g_midair"))
345                         s = strcat(s, ":midair");
346                 if(cvar("g_minstagib"))
347                         s = strcat(s, ":minstagib");
348                 GameLogEcho(s, FALSE);
349                 GameLogEcho(":gameinfo:end", FALSE);
350         }
351
352         cvar_set("nextmap", "");
353
354         SetDefaultAlpha();
355
356         if(cvar("g_campaign"))
357                 CampaignPostInit();
358 }
359
360 void light (void)
361 {
362         makestatic (self);
363 }
364
365 float( string pFilename ) TryFile =
366 {
367         local float lHandle;
368         dprint("TryFile(\"", pFilename, "\")\n");
369         lHandle = fopen( pFilename, FILE_READ );
370         if( lHandle != -1 ) {
371                 fclose( lHandle );
372                 return TRUE;
373         } else {
374                 return FALSE;
375         }
376 };
377
378 string GetMapname()
379 {
380         if (game == GAME_DEATHMATCH)
381                 return strcat("dm_", mapname);
382         else if (game == GAME_TEAM_DEATHMATCH)
383                 return strcat("tdm_", mapname);
384         else if (game == GAME_DOMINATION)
385                 return strcat("dom_", mapname);
386         else if (game == GAME_CTF)
387                 return strcat("ctf_", mapname);
388         else if (game == GAME_RUNEMATCH)
389                 return strcat("rune_", mapname);
390         else if (game == GAME_LMS)
391                 return strcat("lms_", mapname);
392         return strcat("dm_", mapname);
393 }
394
395 float Map_Count, Map_Current;
396 string Map_Current_Name;
397
398 // NOTE: this now expects the map list to be already tokenize()d and the count in Map_Count
399 float GetMaplistPosition()
400 {
401         float pos;
402         string map;
403
404         map = GetMapname();
405         for(pos = 0; pos < Map_Count; ++pos)
406                 if(map == argv(pos))
407                         return pos;
408
409         // resume normal maplist rotation if current map is not in g_maplist
410         return cvar("g_maplist_index");
411 }
412
413 float MapHasRightSize(string map)
414 {
415         // open map size restriction file
416         float fh;
417         dprint("opensize "); dprint(map);
418         fh = fopen(strcat("maps/", map, ".sizes"), FILE_READ);
419         if(fh >= 0)
420         {
421                 float mapmin, mapmax;
422                 dprint(": ok, ");
423                 mapmin = stof(fgets(fh));
424                 mapmax = stof(fgets(fh));
425                 fclose(fh);
426                 if(player_count < mapmin)
427                 {
428                         dprint("not enough\n");
429                         return FALSE;
430                 }
431                 if(player_count > mapmax)
432                 {
433                         dprint("too many\n");
434                         return FALSE;
435                 }
436                 dprint("right size\n");
437                 return TRUE;
438         }
439         dprint(": not found\n");
440         return TRUE;
441 }
442
443 string Map_Filename(float position)
444 {
445         return strcat("maps/", argv(position), ".mapcfg");
446 }
447
448 float(float position, float pass) Map_Check =
449 {
450         string filename;
451         string map_next;
452         map_next = argv(position);
453         if(pass <= 1)
454                 if(map_next == Map_Current_Name) // same map again in first pass?
455                         return 0;
456         filename = Map_Filename(position);
457         if(TryFile(filename))
458         {
459                 if(pass == 2)
460                         return 1;
461                 if(MapHasRightSize(argv(position)))
462                         return 1;
463                 return 0;
464         }
465         else
466                 dprint( "Couldn't find '", filename, "'..\n" );
467
468         return 0;
469 }
470
471 void(float position) Map_Goto =
472 {
473         cvar_set("g_maplist_index", ftos(position));
474         localcmd(strcat("exec \"", Map_Filename(position) ,"\"\n"));
475 }
476
477 // return codes of map selectors:
478 //   -1 = temporary failure (that is, try some method that is guaranteed to succeed)
479 //   -2 = permanent failure
480 float() MaplistMethod_Iterate = // usual method
481 {
482         float pass, i;
483
484         for(pass = 1; pass <= 2; ++pass)
485         {
486                 for(i = 1; i < Map_Count; ++i)
487                 {
488                         float mapindex;
489                         mapindex = math_mod(i + Map_Current, Map_Count);
490                         if(Map_Check(mapindex, pass))
491                                 return mapindex;
492                 }
493         }
494         return -1;
495 }
496
497 float() MaplistMethod_Repeat = // fallback method
498 {
499         if(Map_Check(Map_Current, 2))
500                 return Map_Current;
501         return -2;
502 }
503
504 float() MaplistMethod_Random = // random map selection
505 {
506         float i, imax;
507
508         imax = 42;
509
510         for(i = 0; i <= imax; ++i)
511         {
512                 float mapindex;
513                 mapindex = math_mod(Map_Current + ceil(random() * (Map_Count - 1)), Map_Count); // any OTHER map
514                 if(Map_Check(mapindex, 1))
515                         return mapindex;
516         }
517         return -1;
518 }
519
520 float(float exponent) MaplistMethod_Shuffle = // more clever shuffling
521 // the exponent sets a bias on the map selection:
522 // the higher the exponent, the 
523 {
524         float i, j, imax, insertpos;
525
526         imax = 42;
527
528         if(Map_Count <= 1)
529                 return 0; // only one map, then always play this one
530
531         for(i = 0; i <= imax; ++i)
532         {
533                 string newlist;
534
535                 // now reinsert this at another position
536                 insertpos = pow(random(), 1 / exponent);       // ]0, 1]
537                 insertpos = insertpos * (Map_Count - 1);       // ]0, Map_Count - 1]
538                 insertpos = ceil(insertpos) + 1;               // {2, 3, 4, ..., Map_Count}
539                 dprint("SHUFFLE: insert pos = ", ftos(insertpos), "\n");
540
541                 // insert the current map there
542                 newlist = "";
543                 for(j = 1; j < insertpos; ++j)                 // i == 1: no loop, will be inserted as first; however, i == 1 has been excluded above
544                         newlist = strcat(newlist, "'", argv(j), "'");
545                 newlist = strcat(newlist, "'", argv(0), "'");  // now insert the just selected map
546                 for(j = insertpos; j < Map_Count; ++j)         // i == Map_Count: no loop, has just been inserted as last
547                         newlist = strcat(newlist, "'", argv(j), "'");
548                 cvar_set("g_maplist", newlist);
549                 Map_Count = tokenize(newlist);
550
551                 // NOTE: the selected map has just been inserted at (insertpos-1)th position
552                 Map_Current = insertpos - 1; // this is not really valid, but this way the fallback has a chance of working
553                 if(Map_Check(Map_Current, 1))
554                         return Map_Current;
555         }
556         return -1;
557 }
558
559 void() Maplist_Init =
560 {
561         string temp;
562         temp = cvar_string("g_maplist");
563         Map_Count = tokenize(temp);
564         if(Map_Count == 0)
565         {
566                 bprint( "Maplist is empty!  Resetting it to default map list.\n" );
567                 cvar_set("g_maplist", temp = cvar_string("g_maplist_defaultlist"));
568                 Map_Count = tokenize(temp);
569         }
570         if(Map_Count == 0)
571                 error("empty maplist, cannot select a new map");
572         Map_Current = bound(0, GetMaplistPosition(), Map_Count - 1);
573
574         Map_Current_Name = strzone(argv(Map_Current)); // will be automatically freed on exit thanks to DP
575         // this may or may not be correct, but who cares, in the worst case a map
576         // isn't chosen in the first pass that should have been
577 }
578
579 void() GotoNextMap =
580 {
581         //local string nextmap;
582         //local float n, nummaps;
583         //local string s;
584         string exit_cfg;
585         if (alreadychangedlevel)
586                 return;
587         alreadychangedlevel = TRUE;
588
589         if(cvar("g_campaign"))
590         {
591                 CampaignPostIntermission();
592                 return;
593         }
594
595         if (cvar("samelevel")) // if samelevel is set, stay on same level
596         {
597                 // 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)
598                 //localcmd(strcat("exec \"maps/", mapname, ".mapcfg\"\n"));
599                 // so instead just restart the current map using the restart command (DOES NOT WORK PROPERLY WITH exit_cfg STUFF)
600                 localcmd("restart\n");
601                 //changelevel (mapname);
602                 return;
603         }
604
605         // if an exit cfg is defined by exiting map, exec it.
606         exit_cfg = cvar_string("exit_cfg");
607         if(exit_cfg != "")
608                 localcmd(strcat("exec \"", exit_cfg, "\"\n"));
609
610         localcmd("exec game_reset.cfg\n");
611
612
613         if (cvar("lastlevel"))
614         {
615                 localcmd(strcat("set lastlevel 0\n"));
616                 localcmd(strcat("togglemenu\n"));
617         }
618         else
619         {
620                 float nextMap;
621                 float allowReset;
622
623                 // cvar "nextmap" always gets priority
624                 if(TryFile(strcat("maps/", cvar_string("nextmap"), ".mapcfg")))
625                 {
626                         localcmd(strcat("exec \"maps/", cvar_string("nextmap"), ".mapcfg\"\n"));
627                         return;
628                 }
629
630                 for(allowReset = 1; allowReset >= 0; --allowReset)
631                 {
632                         Maplist_Init();
633                         nextMap = -1;
634
635                         if(nextMap == -1)
636                                 if(cvar("g_maplist_shuffle") > 0)
637                                         nextMap = MaplistMethod_Shuffle(cvar("g_maplist_shuffle") + 1);
638
639                         if(nextMap == -1)
640                                 if(cvar("g_maplist_selectrandom"))
641                                         nextMap = MaplistMethod_Random();
642
643                         if(nextMap == -1)
644                                 nextMap = MaplistMethod_Iterate();
645
646                         if(nextMap == -1)
647                                 nextMap = MaplistMethod_Repeat();
648
649                         if(nextMap >= 0)
650                         {
651                                 Map_Goto(nextMap);
652                                 break;
653                         }
654                         else // PERMANENT FAILURE
655                         {
656                                 if(allowReset)
657                                 {
658                                         bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );
659                                         cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
660                                 }
661                                 else
662                                 {
663                                         error("Everything is broken - not even the default map list works. Please report this to the developers.");
664                                 }
665                         }
666                 }
667         }
668 };
669
670
671 /*
672 ============
673 IntermissionThink
674
675 When the player presses attack or jump, change to the next level
676 ============
677 */
678 void() IntermissionThink =
679 {
680         if(cvar("sv_autoscreenshot"))
681         if(self.cnt > time)
682         {
683                 self.cnt = FALSE;
684                 if(clienttype(self) == CLIENTTYPE_REAL)
685                         stuffcmd(self, "screenshot\necho \"^5A screenshot has been taken at request of the server.\"\n");
686                 return;
687         }
688
689         if (time < intermission_exittime)
690                 return;
691
692         if (time < intermission_exittime + 10 && !self.button0 && !self.button1 && !self.button2 && !self.button3)
693                 return;
694
695         GotoNextMap ();
696 };
697
698 /*
699 ============
700 FindIntermission
701
702 Returns the entity to view from
703 ============
704 */
705 /*
706 entity() FindIntermission =
707 {
708         local   entity spot;
709         local   float cyc;
710
711 // look for info_intermission first
712         spot = find (world, classname, "info_intermission");
713         if (spot)
714         {       // pick a random one
715                 cyc = random() * 4;
716                 while (cyc > 1)
717                 {
718                         spot = find (spot, classname, "info_intermission");
719                         if (!spot)
720                                 spot = find (spot, classname, "info_intermission");
721                         cyc = cyc - 1;
722                 }
723                 return spot;
724         }
725
726 // then look for the start position
727         spot = find (world, classname, "info_player_start");
728         if (spot)
729                 return spot;
730
731 // testinfo_player_start is only found in regioned levels
732         spot = find (world, classname, "testplayerstart");
733         if (spot)
734                 return spot;
735
736 // then look for the start position
737         spot = find (world, classname, "info_player_deathmatch");
738         if (spot)
739                 return spot;
740
741         //objerror ("FindIntermission: no spot");
742         return world;
743 };
744 */
745
746 /*
747 ===============================================================================
748
749 RULES
750
751 ===============================================================================
752 */
753
754 void() DumpStats =
755 {
756         local float file;
757         local string s;
758
759         if(cvar("_printstats"))
760                 cvar_set("_printstats", "0");
761         else if(!gameover)
762                 return;
763
764         if(gameover)
765                 s = ":scores:";
766         else
767                 s = ":status:";
768
769         s = strcat(s, GetMapname(), ":", ftos(rint(time)));
770
771         if(cvar("sv_eventlog") && gameover)
772                 GameLogEcho(s, FALSE);
773         else if(cvar("sv_logscores_console"))
774                 ServerConsoleEcho(s, FALSE);
775         if(cvar("sv_logscores_file"))
776         {
777                 file = fopen(cvar_string("sv_logscores_filename"), FILE_APPEND);
778                 fputs(file, strcat(s, "\n"));
779         }
780
781         other = findchainflags(flags, FL_CLIENT);
782         while (other)
783         {
784                 if ((clienttype(other) == CLIENTTYPE_REAL) || (clienttype(other) == CLIENTTYPE_BOT && cvar("sv_logscores_bots")))
785                 {
786                         s = strcat(":player:", ftos(other.frags), ":");
787                         s = strcat(s, ftos(other.deaths), ":");
788                         s = strcat(s, ftos(rint(time - other.jointime)), ":");
789                         s = strcat(s, ftos(other.team), ":");
790
791                         if(cvar("sv_logscores_file"))
792                                 fputs(file, strcat(s, other.netname, "\n"));
793                         if(cvar("sv_eventlog") && gameover)
794                                 GameLogEcho(strcat(s, ftos(other.playerid), ":", other.netname), TRUE);
795                         else if(cvar("sv_logscores_console"))
796                                 ServerConsoleEcho(strcat(s, other.netname), TRUE);
797                 }
798                 other = other.chain;
799         }
800
801         if(cvar("sv_eventlog") && gameover)
802                 GameLogEcho(":end", FALSE);
803         else if(cvar("sv_logscores_console"))
804                 ServerConsoleEcho(":end", FALSE);
805         if(cvar("sv_logscores_file"))
806         {
807                 fputs(file, ":end\n");
808                 fclose(file);
809         }
810 }
811
812
813 /*
814 go to the next level for deathmatch
815 only called if a time or frag limit has expired
816 */
817 void() NextLevel =
818 {
819         gameover = TRUE;
820
821         intermission_running = 1;
822
823 // enforce a wait time before allowing changelevel
824         if(player_count > 0)
825                 intermission_exittime = time + cvar("sv_mapchange_delay");
826         else
827                 intermission_exittime = -60;
828
829         WriteByte (MSG_ALL, SVC_CDTRACK);
830         WriteByte (MSG_ALL, 3);
831         WriteByte (MSG_ALL, 3);
832
833         //pos = FindIntermission ();
834
835         VoteReset();
836
837         DumpStats();
838
839         if(cvar("sv_eventlog"))
840                 GameLogEcho(":gameover", FALSE);
841
842         GameLogClose();
843
844         other = findchainflags(flags, FL_CLIENT);
845         while (other != world)
846         {
847                 //other.nextthink = time + 0.5;
848                 other.takedamage = DAMAGE_NO;
849                 other.solid = SOLID_NOT;
850                 other.movetype = MOVETYPE_NONE;
851                 other.angles = other.v_angle;
852                 other.angles_x = other.angles_x * -1;
853                 other.cnt = time + 0.5; // used for autoscreenshot
854
855                 self = other;
856
857                 if(other.winning)
858                         bprint(strcat(other.netname, " ^7wins.\n"));
859
860                 /*
861                 if (pos != world);
862                 {
863                         other.modelindex = 0;
864                         other.weaponentity = world; // remove weapon model
865                         other.view_ofs = '0 0 0';
866                         other.angles = other.v_angle = pos.mangle;
867                         if (!other.angles)
868                         {
869                                 other.angles = other.v_angle = pos.angles;
870                                 other.v_angle_x = other.v_angle_x * -1;
871                         }
872                         other.fixangle = TRUE;          // turn this way immediately
873                         setorigin (other, pos.origin);
874                 }
875                 */
876                 other = other.chain;
877         }
878
879         if(cvar("g_campaign"))
880                 CampaignPreIntermission();
881
882         WriteByte (MSG_ALL, SVC_INTERMISSION);
883 };
884
885 /*
886 ============
887 CheckRules_Player
888
889 Exit deathmatch games upon conditions
890 ============
891 */
892 void() CheckRules_Player =
893 {
894         if (gameover)   // someone else quit the game already
895                 return;
896
897         // fixme: don't check players; instead check dom_team and ctf_team entities
898         //   (div0: and that in CheckRules_World please)
899 };
900
901 float checkrules_oneminutewarning;
902 float checkrules_leaderfrags;
903 float tdm_max_score, tdm_old_score;
904
905 float checkrules_equality;
906 float checkrules_overtimewarning;
907 float checkrules_overtimeend;
908
909 void() InitiateOvertime =
910 {
911         if(!checkrules_overtimeend)
912                 checkrules_overtimeend = time + 60 * cvar("timelimit_maxovertime");
913 }
914
915 float WINNING_NO = 0; // no winner, but time limits may terminate the game
916 float WINNING_YES = 1; // winner found
917 float WINNING_NEVER = 2; // no winner, enter overtime if time limit is reached
918 float WINNING_STARTOVERTIME = 3; // no winner, enter overtime NOW
919
920 float(float fraglimitreached, float equality) GetWinningCode =
921 {
922         if(equality)
923                 if(fraglimitreached)
924                         return WINNING_STARTOVERTIME;
925                 else
926                         return WINNING_NEVER;
927         else
928                 if(fraglimitreached)
929                         return WINNING_YES;
930                 else
931                         return WINNING_NO;
932 }
933
934 // set the .winning flag for exactly those players with a given field value
935 void(.float field, float value) SetWinners =
936 {
937         entity head;
938         head = findchain(classname, "player");
939         while (head)
940         {
941                 head.winning = (head.field == value);
942                 head = head.chain;
943         }
944 }
945
946 // set the .winning flag for those players with a given field value
947 void(.float field, float value) AddWinners =
948 {
949         entity head;
950         head = findchain(classname, "player");
951         while (head)
952         {
953                 if(head.field == value)
954                         head.winning = 1;
955                 head = head.chain;
956         }
957 }
958
959 // clear the .winning flags
960 void(void) ClearWinners =
961 {
962         entity head;
963         head = findchain(classname, "player");
964         while (head)
965         {
966                 head.winning = 0;
967                 head = head.chain;
968         }
969 }
970
971 // LMS winning condition: game terminates if and only if there's at most one
972 // one player who's living lives. Top two scores being equal cancels the time
973 // limit.
974 float() WinningCondition_LMS =
975 {
976         entity head;
977
978         if(cvar("developer"))
979         {
980                 float real_dead_count;
981                 float real_player_count;
982                 for(head = world; (head = find(head, classname, "player")); )
983                 {
984                         if(head.frags < 1)
985                                 real_dead_count += 1;
986                         real_player_count += 1;
987                 }
988                 for(head = world; (head = find(head, classname, "observer")); )
989                 {
990                         if(head.frags < 1)
991                                 real_dead_count += 1;
992                         real_player_count += 1;
993                 }
994                 for(head = world; (head = find(head, classname, "spectator")); )
995                 {
996                         if(head.frags < 1)
997                                 real_dead_count += 1;
998                         real_player_count += 1;
999                 }
1000
1001                 if(player_count != real_player_count)
1002                 {
1003                         dprint("ERROR: ", ftos(player_count), " is not ");
1004                         dprint(ftos(real_player_count), " -> player count mismatch\n");
1005                 }
1006
1007                 if(lms_dead_count != real_dead_count)
1008                 {
1009                         dprint("ERROR: ", ftos(lms_dead_count), " is not ");
1010                         dprint(ftos(real_dead_count), " -> dead count mismatch\n");
1011                 }
1012         }
1013
1014         if(player_count > 1 && lms_dead_count >= player_count - 1)
1015                 return WINNING_YES; // He's the last man standing!
1016
1017         if((player_count == 1 && lms_dead_count == 1))
1018                 return WINNING_YES; // All dead... (n:n is handled by the test above)
1019
1020         // dprint("player count = "); dprint(ftos(player_count));
1021         // dprint(", dead count = "); dprint(ftos(lms_dead_count));
1022         // dprint("\n");
1023
1024         // When we get here, we have at least two players who are actually LIVING,
1025         // or one player who is still waiting for a victim to join the server. Now
1026         // check if the top two players have equal score.
1027
1028         checkrules_leaderfrags = 0;
1029         head = findchain(classname, "player");
1030         checkrules_equality = FALSE;
1031         while (head)
1032         {
1033                 if(head.frags > checkrules_leaderfrags)
1034                 {
1035                         checkrules_leaderfrags = head.frags;
1036                         checkrules_equality = FALSE;
1037                 }
1038                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
1039                         checkrules_equality = TRUE;
1040                 head = head.chain;
1041         }
1042
1043         SetWinners(frags, checkrules_leaderfrags);
1044
1045         // The top two players have the same amount of lives? No timelimit then,
1046         // enter overtime...
1047
1048         if(checkrules_equality)
1049                 return WINNING_NEVER;
1050
1051         // Top two have different scores? Way to go for our beloved TIMELIMIT!
1052         return WINNING_NO;
1053 }
1054
1055 // DM winning condition: game terminates if a player reached the fraglimit,
1056 // unless the first two players have the same score. The latter case also
1057 // breaks the time limit.
1058 float(float fraglimit) WinningCondition_MaxIndividualScore =
1059 {
1060         float checkrules_oldleaderfrags;
1061         entity head;
1062
1063         checkrules_oldleaderfrags = checkrules_leaderfrags;
1064         checkrules_leaderfrags = 0;
1065         head = findchain(classname, "player");
1066         checkrules_equality = FALSE;
1067         while (head)
1068         {
1069                 if(head.frags > checkrules_leaderfrags)
1070                 {
1071                         checkrules_leaderfrags = head.frags;
1072                         checkrules_equality = FALSE;
1073                 }
1074                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
1075                         checkrules_equality = TRUE;
1076                 head = head.chain;
1077         }
1078
1079         if(checkrules_leaderfrags > 0)
1080                 SetWinners(frags, checkrules_leaderfrags);
1081         else
1082                 ClearWinners();
1083
1084         if (!cvar("g_runematch"))
1085                 if (checkrules_leaderfrags != checkrules_oldleaderfrags)
1086                 {
1087                         if (checkrules_leaderfrags == fraglimit - 1)
1088                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
1089                         else if (checkrules_leaderfrags == fraglimit - 2)
1090                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
1091                         else if (checkrules_leaderfrags == fraglimit - 3)
1092                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
1093                 }
1094
1095         return GetWinningCode(fraglimit && checkrules_leaderfrags >= fraglimit, checkrules_equality);
1096 }
1097
1098 float(float fraglimit) WinningConditionBase_Teamplay =
1099 {
1100         tdm_old_score = tdm_max_score;
1101         tdm_max_score = max(team1_score, team2_score, team3_score, team4_score);
1102
1103         checkrules_equality =
1104         (
1105                 (tdm_max_score > 0)
1106                 &&
1107                 (
1108                           (team1_score == tdm_max_score)
1109                         + (team2_score == tdm_max_score)
1110                         + (team3_score == tdm_max_score)
1111                         + (team4_score == tdm_max_score)
1112                         >= 2));
1113
1114         ClearWinners();
1115         if(tdm_max_score > 0)
1116         {
1117                 if(team1_score == tdm_max_score)
1118                         AddWinners(team, COLOR_TEAM1);
1119                 if(team2_score == tdm_max_score)
1120                         AddWinners(team, COLOR_TEAM2);
1121                 if(team3_score == tdm_max_score)
1122                         AddWinners(team, COLOR_TEAM3);
1123                 if(team4_score == tdm_max_score)
1124                         AddWinners(team, COLOR_TEAM4);
1125         }
1126
1127         if(!cvar("g_runematch") && !cvar("g_domination"))
1128                 if(tdm_max_score != tdm_old_score)
1129                 {
1130                         if(tdm_max_score == fraglimit - 1)
1131                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
1132                         else if(tdm_max_score == fraglimit - 2)
1133                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
1134                         else if(tdm_max_score == fraglimit - 3)
1135                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
1136                 }
1137
1138         return GetWinningCode(fraglimit && tdm_max_score >= fraglimit, checkrules_equality);
1139 }
1140
1141 // TDM winning condition: game terminates if a team's score sum reached the
1142 // fraglimit, unless the first two teams have the same total score. The latter
1143 // case also breaks the time limit.
1144 float(float fraglimit) WinningCondition_MaxTeamSum =
1145 {
1146         entity head;
1147
1148         team1_score = team2_score = team3_score = team4_score = 0;
1149
1150         head = findchain(classname, "player");
1151         while (head)
1152         {
1153                 if(head.team == COLOR_TEAM1)
1154                         team1_score += head.frags;
1155                 else if(head.team == COLOR_TEAM2)
1156                         team2_score += head.frags;
1157                 else if(head.team == COLOR_TEAM3)
1158                         team3_score += head.frags;
1159                 else if(head.team == COLOR_TEAM4)
1160                         team4_score += head.frags;
1161                 head = head.chain;
1162         }
1163
1164         return WinningConditionBase_Teamplay(fraglimit);
1165 }
1166
1167 // DOM/CTF winning condition: game terminates if the max of a team's players'
1168 // score reached the fraglimit, unless the first two teams have the same
1169 // maximum score. The latter case also breaks the time limit.
1170 float(float fraglimit) WinningCondition_MaxTeamMax =
1171 {
1172         entity head;
1173
1174         team1_score = team2_score = team3_score = team4_score = 0;
1175
1176         head = findchain(classname, "player");
1177         while (head)
1178         {
1179                 if(head.team == COLOR_TEAM1)
1180                 {
1181                         if(head.frags > team1_score)
1182                                 team1_score = head.frags;
1183                 }
1184                 else if(head.team == COLOR_TEAM2)
1185                 {
1186                         if(head.frags > team2_score)
1187                                 team2_score = head.frags;
1188                 }
1189                 else if(head.team == COLOR_TEAM3)
1190                 {
1191                         if(head.frags > team3_score)
1192                                 team3_score = head.frags;
1193                 }
1194                 else if(head.team == COLOR_TEAM4)
1195                 {
1196                         if(head.frags > team4_score)
1197                                 team4_score = head.frags;
1198                 }
1199                 head = head.chain;
1200         }
1201
1202         return WinningConditionBase_Teamplay(fraglimit);
1203 }
1204
1205 void PrintScoreboardFor(string name, string colorcode, float whichteam)
1206 {
1207         entity head;
1208         float fragtotal;
1209         string s;
1210         float found;
1211         found = FALSE;
1212         head = find(world, classname, "player");
1213         while(head)
1214         {
1215                 if(!whichteam || head.team == whichteam)
1216                 {
1217                         if(name != "")
1218                                 if(!found)
1219                                         ServerConsoleEcho(strcat(" ", colorcode, name, ":"), FALSE);
1220                         found = TRUE;
1221                         fragtotal = fragtotal + head.frags;
1222                         s = ftos(head.frags);
1223                         s = strcat(s, "/", ftos(head.deaths));
1224                         s = strcat(s, " @ ", ftos(head.ping));
1225                         if(clienttype(head) == CLIENTTYPE_BOT)
1226                                 s = strcat(s, "botms");
1227                         else
1228                                 s = strcat(s, "ms");
1229                         ServerConsoleEcho(strcat("  ", colorcode, head.netname, colorcode, " (", s, ")"), TRUE);
1230                 }
1231                 head = find(head, classname, "player");
1232         }
1233         if(whichteam && found)
1234                 ServerConsoleEcho(strcat(colorcode, "  (total: ", ftos(fragtotal), ")"), FALSE);
1235 }
1236
1237 void PrintScoreboard()
1238 {
1239         ServerConsoleEcho("Scoreboard:", FALSE);
1240         if(teams_matter)
1241         {
1242                 PrintScoreboardFor("Red", "^1", COLOR_TEAM1);
1243                 PrintScoreboardFor("Blue", "^4", COLOR_TEAM2);
1244                 PrintScoreboardFor("Pink", "^6", COLOR_TEAM3);
1245                 PrintScoreboardFor("Yellow", "^3", COLOR_TEAM4);
1246         }
1247         else
1248         {
1249                 PrintScoreboardFor("", "^7", 0);
1250         }
1251         ServerConsoleEcho(".", FALSE);
1252 }
1253
1254 void RemoveFromMaplist(string m)
1255 {
1256         string result;
1257         float litems;
1258         float i;
1259         float found;
1260
1261         litems = tokenize(cvar_string("g_maplist"));
1262         found = 0;
1263         result = "";
1264         for(i = 0; i < litems; ++i)
1265         {
1266                 m = strcat(m);
1267                 result = strcat(result);
1268                 if(argv(i) == m)
1269                         found += 1;
1270                 else
1271                         result = strcat(result, "'", argv(i), "'");
1272         }
1273         if(found)
1274                 cvar_set("g_maplist", result);
1275         ServerConsoleEcho(strcat("Removed ", ftos(found), " items."), FALSE);
1276 }
1277
1278 void AddToMaplist(string m)
1279 {
1280         string result;
1281         float found;
1282         float litems;
1283         float i;
1284         float ipos;
1285         float inserted;
1286
1287         if(!TryFile(strcat("maps/", m, ".mapcfg")))
1288         {
1289                 ServerConsoleEcho("Map not found.", FALSE);
1290                 return;
1291         }
1292
1293         litems = tokenize(cvar_string("g_maplist"));
1294         if(cvar("g_maplist_shuffle"))
1295                 ipos = ceil(random() * (litems + 1)) - 1;
1296         else
1297                 ipos = litems;
1298         found = 0;
1299         inserted = 0;
1300         for(i = 0; i < litems; ++i)
1301         {
1302                 m = strcat(m);
1303                 if(i == ipos)
1304                 {
1305                         result = strcat(result, "'", m, "'");
1306                         inserted = 1;
1307                 }
1308                 result = strcat(result, "'", argv(i), "'");
1309                 if(argv(i) == m)
1310                         found += 1;
1311         }
1312         if(!inserted)
1313                 result = strcat(result, "'", m, "'");
1314         if(!found)
1315         {
1316                 cvar_set("g_maplist", result);
1317                 ServerConsoleEcho("Map added.", FALSE);
1318         }
1319         else
1320                 ServerConsoleEcho("Map already in list.", FALSE);
1321 }
1322
1323 void ShuffleMaplist()
1324 {
1325         string result;
1326         float start;
1327         float litems;
1328         float selected;
1329         float i;
1330
1331         result = cvar_string("g_maplist");
1332         litems = tokenize(result);
1333
1334         for(start = 0; start < litems - 1; ++start)
1335         {
1336                 result = "";
1337
1338                 // select a random item
1339                 selected = ceil(random() * (litems - start) + start) - 1;
1340
1341                 // shift this item to the place start
1342                 for(i = 0; i < start; ++i)
1343                         result = strcat(result, "'", argv(i), "'");
1344                 result = strcat(result, "'", argv(selected), "'");
1345                 for(i = start; i < litems; ++i)
1346                         if(i != selected)
1347                                 result = strcat(result, "'", argv(i), "'");
1348
1349                 litems = tokenize(result);
1350
1351                 //dprint(result, "\n");
1352         }
1353
1354         cvar_set("g_maplist", result);
1355 }
1356
1357 /*
1358 ============
1359 CheckRules_World
1360
1361 Exit deathmatch games upon conditions
1362 ============
1363 */
1364 void() CheckRules_World =
1365 {
1366         local float status;
1367         local float timelimit;
1368         local float fraglimit;
1369
1370         VoteThink();
1371
1372         SetDefaultAlpha();
1373
1374         if (intermission_running)
1375                 if (time >= intermission_exittime + 60)
1376                 {
1377                         GotoNextMap();
1378                         return;
1379                 }
1380
1381         if (gameover)   // someone else quit the game already
1382                 return;
1383
1384         DumpStats();
1385
1386         if(cvar("_scoreboard"))
1387         {
1388                 cvar_set("_scoreboard", "0");
1389                 PrintScoreboard();
1390         }
1391
1392         // automatically shuffle when setting g_maplist_shuffle
1393         if(cvar_string("_g_maplist_add") != "")
1394         {
1395                 AddToMaplist(cvar_string("_g_maplist_add"));
1396                 cvar_set("_g_maplist_add", "");
1397         }
1398         if(cvar_string("_g_maplist_remove") != "")
1399         {
1400                 RemoveFromMaplist(cvar_string("_g_maplist_remove"));
1401                 cvar_set("_g_maplist_remove", "");
1402         }
1403         if(cvar("_g_maplist_shufflenow") || (cvar("g_maplist_shuffle") && !cvar("_g_maplist_have_shuffled")))
1404         {
1405                 ShuffleMaplist();
1406                 localcmd("set _g_maplist_shufflenow 0\nset _g_maplist_have_shuffled 1\necho Shuffled map list.\n");
1407         }
1408         if(cvar("_g_maplist_have_shuffled"))
1409                 if(!cvar("g_maplist_shuffle"))
1410                         localcmd("set _g_maplist_have_shuffled 0\n");
1411
1412         timelimit = cvar("timelimit") * 60;
1413         fraglimit = cvar("fraglimit");
1414
1415         if (timelimit && time >= timelimit)
1416                 InitiateOvertime();
1417
1418         if (checkrules_overtimeend && time >= checkrules_overtimeend)
1419         {
1420                 NextLevel();
1421                 return;
1422         }
1423
1424         if(!checkrules_overtimewarning && checkrules_overtimeend)
1425         {
1426                 checkrules_overtimewarning = TRUE;
1427                 //sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1428                 bcenterprint("^3Now playing ^1OVERTIME^3!\n\n^3Keep fragging until we have a ^1winner^3!");
1429         }
1430
1431         if (!checkrules_oneminutewarning && timelimit > 0 && time > timelimit - 60)
1432         {
1433                 checkrules_oneminutewarning = TRUE;
1434                 sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1435         }
1436
1437         status = WINNING_NO;
1438         if(cvar("g_lms"))
1439         {
1440                 status = WinningCondition_LMS();
1441         }
1442         else
1443         {
1444                 if(teams_matter)
1445                 {
1446                         if(cvar("g_tdm") || cvar("g_runematch") || cvar("g_ctf") || cvar("g_domination"))
1447                                 status = WinningCondition_MaxTeamSum(fraglimit);
1448                         //else if()
1449                         //      status = WinningCondition_MaxTeamMax(fraglimit);
1450                         else
1451                         {
1452                                 dprint("div0: How can this happen?\n");
1453                                 status = WinningCondition_MaxTeamMax(fraglimit);
1454                         }
1455                 }
1456                 else
1457                         status = WinningCondition_MaxIndividualScore(fraglimit);
1458         }
1459
1460         if(status == WINNING_STARTOVERTIME)
1461         {
1462                 status = WINNING_NEVER;
1463                 InitiateOvertime();
1464         }
1465
1466         if(status == WINNING_NEVER)
1467                 // equality cases! Nobody wins if the overtime ends in a draw.
1468                 ClearWinners();
1469
1470         if(checkrules_overtimeend)
1471                 if(status != WINNING_NEVER)
1472                         status = WINNING_YES;
1473
1474         if(status == WINNING_YES)
1475                 NextLevel();
1476 };