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