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