- now using a fresh dpextensions.qc
[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 float() DoNextMapOverride;
7
8 void SetDefaultAlpha()
9 {
10         if(cvar("g_running_guns"))
11         {
12                 default_player_alpha = -1;
13                 default_weapon_alpha = +1;
14         }
15         else if(cvar("g_cloaked"))
16         {
17                 default_player_alpha = cvar("g_balance_cloaked_alpha");
18                 default_weapon_alpha = default_player_alpha;
19         }
20         else
21         {
22                 default_player_alpha = cvar("g_player_alpha");
23                 if(default_player_alpha <= 0)
24                         default_player_alpha = 1;
25                 default_weapon_alpha = default_player_alpha;
26         }
27 }
28
29 void fteqcc_testbugs()
30 {
31         float a, b;
32
33         if(!cvar("developer_fteqccbugs"))
34                 return;
35
36         dprint("*** fteqcc test: checking for bugs...\n");
37
38         a = 1;
39         b = 5;
40         if(sqrt(a) - sqrt(b - a) == 0)
41                 dprint("*** fteqcc test: found same-function-twice bug\n");
42         else
43                 dprint("*** fteqcc test: same-function-twice bug got FINALLY FIXED! HOORAY!\n");
44
45         world.frags = -10;
46         world.enemy = world;
47         world.enemy.frags += 10;
48         if(world.frags > 0.2 || world.frags < -0.2) // don't error out if it's just roundoff errors
49                 dprint("*** fteqcc test: found += bug\n");
50         else
51                 dprint("*** fteqcc test: += bug got FINALLY FIXED! HOORAY!\n");
52         world.frags = 0;
53 }
54
55 void GotoFirstMap()
56 {
57         if(cvar("_sv_init"))
58         {
59                 cvar_set("_sv_init", "0");
60                 HandleMaplistShuffleCommands();
61                 tokenize(cvar_string("g_maplist"));
62                 if(argv(0) != GetMapname())
63                 {
64                         cvar_set("nextmap", argv(0));
65                         if(!DoNextMapOverride())
66                                 GotoNextMap();
67                 }
68         }
69 }
70
71 float world_already_spawned;
72 void worldspawn (void)
73 {
74         if(world_already_spawned)
75                 error("world already spawned - you may have EXACTLY ONE worldspawn!");
76         world_already_spawned = TRUE;
77         // Precache all player models
78         // Workaround for "invisible players"
79         precache_model("models/player/carni.zym");
80         precache_model("models/player/crash.zym");
81         precache_model("models/player/grunt.zym");
82         precache_model("models/player/headhunter.zym");
83         precache_model("models/player/insurrectionist.zym");
84         precache_model("models/player/jeandarc.zym");
85         precache_model("models/player/lurk.zym");
86         precache_model("models/player/lycanthrope.zym");
87         precache_model("models/player/marine.zym");
88         precache_model("models/player/nexus.zym");
89         precache_model("models/player/pyria.zym");
90         precache_model("models/player/shock.zym");
91         precache_model("models/player/skadi.zym");
92         precache_model("models/player/specop.zym");
93         precache_model("models/player/visitant.zym");
94
95         //precache_model ("progs/beam.mdl");
96         precache_model ("models/bullet.mdl");
97         precache_model ("models/casing_bronze.mdl");
98         precache_model ("models/casing_shell.mdl");
99         precache_model ("models/casing_steel.mdl");
100         precache_model ("models/ebomb.mdl");
101         precache_model ("models/elaser.mdl");
102         precache_model ("models/flash.md3");
103         precache_model ("models/gibs/bloodyskull.md3");
104         precache_model ("models/gibs/chunk.mdl");
105         precache_model ("models/gibs/eye.md3");
106         precache_model ("models/gibs/gib1.md3");
107         //precache_model ("models/gibs/gib2.md3");
108         //precache_model ("models/gibs/gib3.md3");
109         //precache_model ("models/gibs/gib4.md3");
110         precache_model ("models/gibs/gib5.md3");
111         //precache_model ("models/gibs/gib6.md3");
112         precache_model ("models/gibs/gib1.mdl");
113         precache_model ("models/gibs/gib2.mdl");
114         precache_model ("models/gibs/gib3.mdl");
115         precache_model ("models/grenademodel.md3");
116         precache_model ("models/hagarmissile.mdl");
117         precache_model ("models/items/a_bullets.mdl");
118         precache_model ("models/items/a_cells.md3");
119         precache_model ("models/items/a_rockets.md3");
120         precache_model ("models/items/a_shells.md3");
121         precache_model ("models/items/g_a1.md3");
122         precache_model ("models/items/g_a25.md3");
123         precache_model ("models/items/g_h1.md3");
124         precache_model ("models/items/g_h25.md3");
125         precache_model ("models/items/g_h100.md3");
126         precache_model ("models/items/g_invincible.md3");
127         precache_model ("models/items/g_strength.md3");
128         precache_model ("models/laser.mdl");
129         precache_model ("models/misc/chatbubble.spr");
130         precache_model ("models/misc/teambubble.spr");
131         precache_model ("models/nexflash.md3");
132         precache_model ("models/plasma.mdl");
133         precache_model ("models/plasmatrail.mdl");
134         precache_model ("models/rocket.md3");
135         //precache_model ("models/sprites/grenexpl.spr");
136         precache_model ("models/runematch/rune.mdl");
137         precache_model ("models/runematch/curse.mdl");
138         //precache_model ("models/sprites/hagar.spr");
139         //precache_model ("models/sprites/muzzleflash.spr32");
140         //precache_model ("models/sprites/electrocombo.spr32");
141         //precache_model ("models/sprites/plasmahitwall.spr32");
142         //precache_model ("models/sprites/plasmashot.spr32");
143         //precache_model ("models/sprites/rockexpl.spr");
144         precache_model ("models/tracer.mdl");
145         precache_model ("models/uziflash.md3");
146         precache_model ("models/weapons/g_crylink.md3");
147         precache_model ("models/weapons/g_electro.md3");
148         precache_model ("models/weapons/g_gl.md3");
149         precache_model ("models/weapons/g_hagar.md3");
150         precache_model ("models/weapons/g_nex.md3");
151         precache_model ("models/weapons/g_rl.md3");
152         precache_model ("models/weapons/g_shotgun.md3");
153         precache_model ("models/weapons/g_uzi.md3");
154         precache_model ("models/weapons/v_crylink.md3");
155         precache_model ("models/weapons/v_electro.md3");
156         precache_model ("models/weapons/v_gl.md3");
157         precache_model ("models/weapons/v_hagar.md3");
158         precache_model ("models/weapons/v_laser.md3");
159         precache_model ("models/weapons/v_nex.md3");
160         precache_model ("models/weapons/v_rl.md3");
161         precache_model ("models/weapons/v_shotgun.md3");
162         precache_model ("models/weapons/v_uzi.md3");
163         precache_model ("models/weapons/w_crylink.zym");
164         precache_model ("models/weapons/w_electro.zym");
165         precache_model ("models/weapons/w_gl.zym");
166         precache_model ("models/weapons/w_hagar.zym");
167         precache_model ("models/weapons/w_laser.zym");
168         precache_model ("models/weapons/w_nex.zym");
169         precache_model ("models/weapons/w_rl.zym");
170         precache_model ("models/weapons/w_shotgun.zym");
171         precache_model ("models/weapons/w_uzi.zym");
172
173         // laser for laser-guided weapons
174         precache_model ("models/laser_dot.mdl");
175
176         precache_sound ("misc/null.wav");
177         precache_sound ("misc/armor1.wav");
178         precache_sound ("misc/armor25.wav");
179         precache_sound ("misc/armorimpact.wav");
180         precache_sound ("misc/bodyimpact1.wav");
181         precache_sound ("misc/bodyimpact2.wav");
182         precache_sound ("misc/gib.wav");
183         precache_sound ("misc/gib_splat01.wav");
184         precache_sound ("misc/gib_splat02.wav");
185         precache_sound ("misc/gib_splat03.wav");
186         precache_sound ("misc/gib_splat04.wav");
187         //precache_sound ("misc/h2ohit.wav");
188         precache_sound ("misc/hit.wav");
189         precache_sound ("misc/footstep01.wav");
190         precache_sound ("misc/footstep02.wav");
191         precache_sound ("misc/footstep03.wav");
192         precache_sound ("misc/footstep04.wav");
193         precache_sound ("misc/footstep05.wav");
194         precache_sound ("misc/footstep06.wav");
195         precache_sound ("misc/hitground1.ogg");
196         precache_sound ("misc/hitground2.ogg");
197         precache_sound ("misc/hitground3.ogg");
198         precache_sound ("misc/hitground4.ogg");
199         precache_sound ("misc/itempickup.ogg");
200         precache_sound ("misc/itemrespawn.ogg");
201         precache_sound ("misc/jumppad.ogg");
202         precache_sound ("misc/mediumhealth.ogg");
203         precache_sound ("misc/megahealth.ogg");
204         precache_sound ("misc/minihealth.ogg");
205         precache_sound ("misc/powerup.ogg");
206         precache_sound ("misc/powerup_shield.ogg");
207         precache_sound ("misc/talk.wav");
208         precache_sound ("misc/teleport.ogg");
209         precache_sound ("plats/medplat1.wav");
210         precache_sound ("plats/medplat2.wav");
211         precache_sound ("player/lava.wav");
212         precache_sound ("player/slime.wav");
213         precache_sound ("weapons/crylink_fire.ogg");
214         precache_sound ("weapons/electro_bounce.ogg");
215         precache_sound ("weapons/electro_fire.ogg");
216         precache_sound ("weapons/electro_fire2.ogg");
217         precache_sound ("weapons/electro_fly.wav");
218         precache_sound ("weapons/electro_impact.ogg");
219         precache_sound ("weapons/electro_impact_combo.ogg");
220         //precache_sound ("weapons/grenade_bounce.ogg");
221         precache_sound ("weapons/grenade_bounce1.ogg");
222         precache_sound ("weapons/grenade_bounce2.ogg");
223         precache_sound ("weapons/grenade_bounce3.ogg");
224         precache_sound ("weapons/grenade_bounce4.ogg");
225         precache_sound ("weapons/grenade_bounce5.ogg");
226         precache_sound ("weapons/grenade_bounce6.ogg");
227         precache_sound ("weapons/grenade_fire.ogg");
228         precache_sound ("weapons/grenade_impact.ogg");
229         precache_sound ("weapons/hagar_fire.ogg");
230         precache_sound ("weapons/hagexp1.ogg");
231         precache_sound ("weapons/hagexp2.ogg");
232         precache_sound ("weapons/hagexp3.ogg");
233         precache_sound ("weapons/hook_fire.ogg");
234         precache_sound ("weapons/hook_impact.ogg");
235         precache_sound ("weapons/lasergun_fire.ogg");
236         precache_sound ("weapons/laserimpact.ogg");
237         precache_sound ("weapons/nexfire.ogg");
238         precache_sound ("weapons/neximpact.ogg");
239         precache_sound ("weapons/ric1.ogg");
240         precache_sound ("weapons/ric2.ogg");
241         precache_sound ("weapons/ric3.ogg");
242         precache_sound ("weapons/rocket_fire.ogg");
243         precache_sound ("weapons/rocket_fly.wav");
244         precache_sound ("weapons/rocket_impact.ogg");
245         precache_sound ("weapons/rocket_det.ogg");
246         precache_sound ("weapons/shotgun_fire.ogg");
247         precache_sound ("weapons/tink1.ogg");
248         precache_sound ("weapons/uzi_fire.ogg");
249         precache_sound ("weapons/weapon_switch.ogg");
250         precache_sound ("weapons/weaponpickup.ogg");
251         precache_sound ("weapons/strength_fire.ogg");
252
253         //precache_sound ("announce/male/kill10.ogg");
254         //precache_sound ("announce/male/kill15.ogg");
255         //precache_sound ("announce/male/kill20.ogg");
256         //precache_sound ("announce/male/kill25.ogg");
257         //precache_sound ("announce/male/kill3.ogg");
258         //precache_sound ("announce/male/kill30.ogg");
259         //precache_sound ("announce/male/kill4.ogg");
260         //precache_sound ("announce/male/kill5.ogg");
261         //precache_sound ("announce/male/kill6.ogg");
262         //precache_sound ("announce/male/mapkill1.ogg");
263         //precache_sound ("announce/robotic/last_second_save.ogg");
264         //precache_sound ("announce/robotic/narrowly_averted.ogg");
265         //precache_sound ("minstagib/mockery.ogg");
266
267         // announcer sounds - male
268         precache_sound ("announcer/male/03kills.ogg");
269         precache_sound ("announcer/male/05kills.ogg");
270         precache_sound ("announcer/male/10kills.ogg");
271         precache_sound ("announcer/male/15kills.ogg");
272         precache_sound ("announcer/male/20kills.ogg");
273         precache_sound ("announcer/male/25kills.ogg");
274         precache_sound ("announcer/male/30kills.ogg");
275         precache_sound ("announcer/male/botlike.ogg");
276         precache_sound ("announcer/male/electrobitch.ogg");
277         precache_sound ("announcer/male/welcome.ogg");
278         precache_sound ("announcer/male/yoda.ogg");
279
280         // announcer sounds - robotic
281         precache_sound ("announcer/robotic/1fragleft.ogg");
282         precache_sound ("announcer/robotic/1minuteremains.ogg");
283         precache_sound ("announcer/robotic/2fragsleft.ogg");
284         precache_sound ("announcer/robotic/3fragsleft.ogg");
285         precache_sound ("announcer/robotic/lastsecond.ogg");
286         precache_sound ("announcer/robotic/narrowly.ogg");
287         precache_sound ("announcer/robotic/1.ogg");
288         precache_sound ("announcer/robotic/2.ogg");
289         precache_sound ("announcer/robotic/3.ogg");
290         precache_sound ("announcer/robotic/4.ogg");
291         precache_sound ("announcer/robotic/5.ogg");
292         precache_sound ("announcer/robotic/6.ogg");
293         precache_sound ("announcer/robotic/7.ogg");
294         precache_sound ("announcer/robotic/8.ogg");
295         precache_sound ("announcer/robotic/9.ogg");
296         precache_sound ("announcer/robotic/10.ogg");
297
298         // plays music for the level if there is any
299         if (self.noise)
300         {
301                 precache_sound (self.noise);
302                 ambientsound ('0 0 0', self.noise, 1.00, ATTN_NONE);
303         }
304
305         // 0 normal
306         lightstyle(0, "m");
307
308         // 1 FLICKER (first variety)
309         lightstyle(1, "mmnmmommommnonmmonqnmmo");
310
311         // 2 SLOW STRONG PULSE
312         lightstyle(2, "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba");
313
314         // 3 CANDLE (first variety)
315         lightstyle(3, "mmmmmaaaaammmmmaaaaaabcdefgabcdefg");
316
317         // 4 FAST STROBE
318         lightstyle(4, "mamamamamama");
319
320         // 5 GENTLE PULSE 1
321         lightstyle(5,"jklmnopqrstuvwxyzyxwvutsrqponmlkj");
322
323         // 6 FLICKER (second variety)
324         lightstyle(6, "nmonqnmomnmomomno");
325
326         // 7 CANDLE (second variety)
327         lightstyle(7, "mmmaaaabcdefgmmmmaaaammmaamm");
328
329         // 8 CANDLE (third variety)
330         lightstyle(8, "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa");
331
332         // 9 SLOW STROBE (fourth variety)
333         lightstyle(9, "aaaaaaaazzzzzzzz");
334
335         // 10 FLUORESCENT FLICKER
336         lightstyle(10, "mmamammmmammamamaaamammma");
337
338         // 11 SLOW PULSE NOT FADE TO BLACK
339         lightstyle(11, "abcdefghijklmnopqrrqponmlkjihgfedcba");
340
341         // styles 32-62 are assigned by the light program for switchable lights
342
343         // 63 testing
344         lightstyle(63, "a");
345
346         registercvar("_g_maplist_shufflenow", "0");
347         registercvar("_g_maplist_have_shuffled", "0");
348         registercvar("_g_maplist_add", "");
349         registercvar("_g_maplist_remove", "");
350         registercvar("_g_maplist_putfirst", "");
351
352         player_count = 0;
353         lms_lowest_lives = 0;
354         lms_next_place = 0;
355
356         GotoFirstMap();
357
358         bot_waypoints_for_items = cvar("g_waypoints_for_items");
359         if(bot_waypoints_for_items == 1)
360                 if(self.spawnflags & SPAWNFLAG_NO_WAYPOINTS_FOR_ITEMS)
361                         bot_waypoints_for_items = 0;
362
363         if(cvar("g_campaign"))
364                 CampaignPreInit();
365
366         InitGameplayMode();
367         //if (cvar("g_domination"))
368         //      dom_init();
369
370         local entity head;
371         head = nextent(world);
372         maxclients = 0;
373         while(head)
374         {
375                 maxclients++;
376                 head = nextent(head);
377         }
378
379         GameLogInit(); // prepare everything
380         if(cvar("sv_eventlog"))
381         {
382                 local string s;
383                 GameLogEcho(":logversion:2", FALSE);
384                 s = strcat(cvar_string("sv_eventlog_files_counter"), ".");
385                 s = strcat(s, ftos(random()));
386                 GameLogEcho(strcat(":gamestart:", GetMapname(), ":", s), FALSE);
387                 s = ":gameinfo:mutators:LIST";
388                 if(cvar("g_grappling_hook"))
389                         s = strcat(s, ":grappling_hook");
390                 if(!cvar("g_use_ammunition"))
391                         s = strcat(s, ":no_use_ammunition");
392                 if(!cvar("g_pickup_items"))
393                         s = strcat(s, ":no_pickup_items");
394                 if(cvar("g_instagib"))
395                         s = strcat(s, ":instagib");
396                 if(cvar("g_rocketarena"))
397                         s = strcat(s, ":rockerarena");
398                 if(cvar("g_nixnex"))
399                         s = strcat(s, ":nixnex");
400                 if(cvar("g_vampire"))
401                         s = strcat(s, ":vampire");
402                 if(cvar("g_laserguided_missile"))
403                         s = strcat(s, ":laserguided_missile");
404                 if(cvar("g_norecoil"))
405                         s = strcat(s, ":norecoil");
406                 if(cvar("g_midair"))
407                         s = strcat(s, ":midair");
408                 if(cvar("g_minstagib"))
409                         s = strcat(s, ":minstagib");
410                 GameLogEcho(s, FALSE);
411                 GameLogEcho(":gameinfo:end", FALSE);
412         }
413
414         cvar_set("nextmap", "");
415
416         SetDefaultAlpha();
417
418         if(cvar("g_campaign"))
419                 CampaignPostInit();
420
421         fteqcc_testbugs();
422 }
423
424 void light (void)
425 {
426         //makestatic (self); // Who the f___ did that?
427         remove(self);
428 }
429
430 float( string pFilename ) TryFile =
431 {
432         local float lHandle;
433         dprint("TryFile(\"", pFilename, "\")\n");
434         lHandle = fopen( pFilename, FILE_READ );
435         if( lHandle != -1 ) {
436                 fclose( lHandle );
437                 return TRUE;
438         } else {
439                 return FALSE;
440         }
441 };
442
443 string GetGametype()
444 {
445         if (game == GAME_DEATHMATCH)
446                 return "dm";
447         else if (game == GAME_TEAM_DEATHMATCH)
448                 return "tdm";
449         else if (game == GAME_DOMINATION)
450                 return "dom";
451         else if (game == GAME_CTF)
452                 return "ctf";
453         else if (game == GAME_RUNEMATCH)
454                 return "rune";
455         else if (game == GAME_LMS)
456                 return "lms";
457         return "dm";
458 }
459
460 string getmapname_stored;
461 string GetMapname()
462 {
463         if(getmapname_stored == "")
464                 getmapname_stored = strzone(strcat(GetGametype(), "_", mapname));
465         return getmapname_stored;
466 }
467
468 float Map_Count, Map_Current;
469 string Map_Current_Name;
470
471 // NOTE: this now expects the map list to be already tokenize()d and the count in Map_Count
472 float GetMaplistPosition()
473 {
474         float pos, idx;
475         string map;
476
477         map = GetMapname();
478         idx = cvar("g_maplist_index");
479
480         if(idx >= 0)
481                 if(idx < Map_Count)
482                         if(map == argv(idx))
483                                 return idx;
484
485         for(pos = 0; pos < Map_Count; ++pos)
486                 if(map == argv(pos))
487                         return pos;
488
489         // resume normal maplist rotation if current map is not in g_maplist
490         return idx;
491 }
492
493 float MapHasRightSize(string map)
494 {
495         // open map size restriction file
496         float fh;
497         dprint("opensize "); dprint(map);
498         fh = fopen(strcat("maps/", map, ".sizes"), FILE_READ);
499         if(fh >= 0)
500         {
501                 float mapmin, mapmax;
502                 dprint(": ok, ");
503                 mapmin = stof(fgets(fh));
504                 mapmax = stof(fgets(fh));
505                 fclose(fh);
506                 if(player_count < mapmin)
507                 {
508                         dprint("not enough\n");
509                         return FALSE;
510                 }
511                 if(player_count > mapmax)
512                 {
513                         dprint("too many\n");
514                         return FALSE;
515                 }
516                 dprint("right size\n");
517                 return TRUE;
518         }
519         dprint(": not found\n");
520         return TRUE;
521 }
522
523 string Map_Filename(float position)
524 {
525         // FIXME unused
526         return strcat("maps/", argv(position), ".mapcfg");
527 }
528
529 float(float position, float pass) Map_Check =
530 {
531         string filename;
532         string map_next;
533         map_next = argv(position);
534         if(pass <= 1)
535                 if(map_next == Map_Current_Name) // same map again in first pass?
536                         return 0;
537         filename = Map_Filename(position);
538         if(TryFile(filename))
539         {
540                 if(pass == 2)
541                         return 1;
542                 if(MapHasRightSize(argv(position)))
543                         return 1;
544                 return 0;
545         }
546         else
547                 dprint( "Couldn't find '", filename, "'..\n" );
548
549         return 0;
550 }
551
552 void(string nextmapname) Map_Goto_SetStr =
553 {
554         if(getmapname_stored != "")
555                 strunzone(getmapname_stored);
556         if(nextmapname == "")
557                 getmapname_stored = "";
558         else
559                 getmapname_stored = strzone(nextmapname);
560 }
561
562 void(float position) Map_Goto_SetFloat =
563 {
564         cvar_set("g_maplist_index", ftos(position));
565         Map_Goto_SetStr(argv(position));
566 }
567
568 void() GameResetCfg =
569 {
570         // if an exit cfg is defined by exiting map, exec it.
571         string exit_cfg;
572         exit_cfg = cvar_string("exit_cfg");
573         if(exit_cfg != "")
574                 localcmd(strcat("exec \"", exit_cfg, "\"\n"));
575
576         localcmd("exec game_reset.cfg\n");
577 };
578
579 void() Map_Goto =
580 {
581         GameResetCfg();
582         localcmd(strcat("exec \"maps/", getmapname_stored ,".mapcfg\"\n"));
583 }
584
585 // return codes of map selectors:
586 //   -1 = temporary failure (that is, try some method that is guaranteed to succeed)
587 //   -2 = permanent failure
588 float() MaplistMethod_Iterate = // usual method
589 {
590         float pass, i;
591
592         for(pass = 1; pass <= 2; ++pass)
593         {
594                 for(i = 1; i < Map_Count; ++i)
595                 {
596                         float mapindex;
597                         mapindex = math_mod(i + Map_Current, Map_Count);
598                         if(Map_Check(mapindex, pass))
599                                 return mapindex;
600                 }
601         }
602         return -1;
603 }
604
605 float() MaplistMethod_Repeat = // fallback method
606 {
607         if(Map_Check(Map_Current, 2))
608                 return Map_Current;
609         return -2;
610 }
611
612 float() MaplistMethod_Random = // random map selection
613 {
614         float i, imax;
615
616         imax = 42;
617
618         for(i = 0; i <= imax; ++i)
619         {
620                 float mapindex;
621                 mapindex = math_mod(Map_Current + ceil(random() * (Map_Count - 1)), Map_Count); // any OTHER map
622                 if(Map_Check(mapindex, 1))
623                         return mapindex;
624         }
625         return -1;
626 }
627
628 float(float exponent) MaplistMethod_Shuffle = // more clever shuffling
629 // the exponent sets a bias on the map selection:
630 // the higher the exponent, the 
631 {
632         float i, j, imax, insertpos;
633
634         imax = 42;
635
636         if(Map_Count <= 1)
637                 return 0; // only one map, then always play this one
638
639         for(i = 0; i <= imax; ++i)
640         {
641                 string newlist;
642
643                 // now reinsert this at another position
644                 insertpos = pow(random(), 1 / exponent);       // ]0, 1]
645                 insertpos = insertpos * (Map_Count - 1);       // ]0, Map_Count - 1]
646                 insertpos = ceil(insertpos) + 1;               // {2, 3, 4, ..., Map_Count}
647                 dprint("SHUFFLE: insert pos = ", ftos(insertpos), "\n");
648
649                 // insert the current map there
650                 newlist = "";
651                 for(j = 1; j < insertpos; ++j)                 // i == 1: no loop, will be inserted as first; however, i == 1 has been excluded above
652                         newlist = strcat(newlist, "'", argv(j), "'");
653                 newlist = strcat(newlist, "'", argv(0), "'");  // now insert the just selected map
654                 for(j = insertpos; j < Map_Count; ++j)         // i == Map_Count: no loop, has just been inserted as last
655                         newlist = strcat(newlist, "'", argv(j), "'");
656                 cvar_set("g_maplist", newlist);
657                 Map_Count = tokenize(newlist);
658
659                 // NOTE: the selected map has just been inserted at (insertpos-1)th position
660                 Map_Current = insertpos - 1; // this is not really valid, but this way the fallback has a chance of working
661                 if(Map_Check(Map_Current, 1))
662                         return Map_Current;
663         }
664         return -1;
665 }
666
667 void() Maplist_Init =
668 {
669         string temp;
670         temp = cvar_string("g_maplist");
671         Map_Count = tokenize(temp);
672         if(Map_Count == 0)
673         {
674                 bprint( "Maplist is empty!  Resetting it to default map list.\n" );
675                 cvar_set("g_maplist", temp = cvar_string("g_maplist_defaultlist"));
676                 Map_Count = tokenize(temp);
677         }
678         if(Map_Count == 0)
679                 error("empty maplist, cannot select a new map");
680         Map_Current = bound(0, GetMaplistPosition(), Map_Count - 1);
681
682         Map_Current_Name = strzone(argv(Map_Current)); // will be automatically freed on exit thanks to DP
683         // this may or may not be correct, but who cares, in the worst case a map
684         // isn't chosen in the first pass that should have been
685 }
686
687 string() GetNextMap =
688 {
689         float nextMap;
690
691         Maplist_Init();
692         nextMap = -1;
693
694         if(nextMap == -1)
695                 if(cvar("g_maplist_shuffle") > 0)
696                         nextMap = MaplistMethod_Shuffle(cvar("g_maplist_shuffle") + 1);
697
698         if(nextMap == -1)
699                 if(cvar("g_maplist_selectrandom"))
700                         nextMap = MaplistMethod_Random();
701
702         if(nextMap == -1)
703                 nextMap = MaplistMethod_Iterate();
704
705         if(nextMap == -1)
706                 nextMap = MaplistMethod_Repeat();
707
708         if(nextMap >= 0)
709         {
710                 Map_Goto_SetFloat(nextMap);
711                 return getmapname_stored;
712         }
713
714         return "";
715 };
716
717 float() DoNextMapOverride =
718 {
719         if(cvar("g_campaign"))
720         {
721                 CampaignPostIntermission();
722                 return TRUE;
723         }
724         if(cvar("quit_when_empty"))
725         {
726                 if(player_count <= currentbots)
727                 {
728                         localcmd("quit\n");
729                         return TRUE;
730                 }
731         }
732         if (cvar("samelevel")) // if samelevel is set, stay on same level
733         {
734                 // 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)
735                 //localcmd(strcat("exec \"maps/", mapname, ".mapcfg\"\n"));
736                 // so instead just restart the current map using the restart command (DOES NOT WORK PROPERLY WITH exit_cfg STUFF)
737                 localcmd("restart\n");
738                 //changelevel (mapname);
739                 return TRUE;
740         }
741         if(cvar_string("nextmap") != "")
742                 if(TryFile(strcat("maps/", cvar_string("nextmap"), ".mapcfg")))
743                 {
744                         Map_Goto_SetStr(cvar_string("nextmap"));
745                         Map_Goto();
746                         return TRUE;
747                 }
748         if(cvar("lastlevel"))
749         {
750                 GameResetCfg();
751                 localcmd("set lastlevel 0\ntogglemenu\n");
752                 return TRUE;
753         }
754         return FALSE;
755 };
756
757 void() GotoNextMap =
758 {
759         //local string nextmap;
760         //local float n, nummaps;
761         //local string s;
762         if (alreadychangedlevel)
763                 return;
764         alreadychangedlevel = TRUE;
765
766         {
767                 string nextMap;
768                 float allowReset;
769
770                 for(allowReset = 1; allowReset >= 0; --allowReset)
771                 {
772                         nextMap = GetNextMap();
773                         if(nextMap != "")
774                                 break;
775
776                         if(allowReset)
777                         {
778                                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );
779                                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
780                         }
781                         else
782                         {
783                                 error("Everything is broken - not even the default map list works. Please report this to the developers.");
784                         }
785                 }
786                 Map_Goto();
787         }
788 };
789
790
791 /*
792 ============
793 IntermissionThink
794
795 When the player presses attack or jump, change to the next level
796 ============
797 */
798 .float autoscreenshot;
799 void() MapVote_Think;
800 float mapvote_initialized;
801 void() IntermissionThink =
802 {
803         FixIntermissionClient(self);
804
805         if(cvar("sv_autoscreenshot"))
806         if(self.autoscreenshot)
807         if(time > self.autoscreenshot)
808         {
809                 self.autoscreenshot = FALSE;
810                 if(clienttype(self) == CLIENTTYPE_REAL)
811                         stuffcmd(self, "\nscreenshot\necho \"^5A screenshot has been taken at request of the server.\"\n");
812                 return;
813         }
814
815         if (time < intermission_exittime)
816                 return;
817
818         if(!mapvote_initialized)
819                 if (time < intermission_exittime + 10 && !self.button0 && !self.button2 && !self.button3 && !self.button6 && !self.buttonuse)
820                         return;
821
822         if(intermission_exittime >= 0)
823                 MapVote_Think();
824 };
825
826 /*
827 ============
828 FindIntermission
829
830 Returns the entity to view from
831 ============
832 */
833 /*
834 entity() FindIntermission =
835 {
836         local   entity spot;
837         local   float cyc;
838
839 // look for info_intermission first
840         spot = find (world, classname, "info_intermission");
841         if (spot)
842         {       // pick a random one
843                 cyc = random() * 4;
844                 while (cyc > 1)
845                 {
846                         spot = find (spot, classname, "info_intermission");
847                         if (!spot)
848                                 spot = find (spot, classname, "info_intermission");
849                         cyc = cyc - 1;
850                 }
851                 return spot;
852         }
853
854 // then look for the start position
855         spot = find (world, classname, "info_player_start");
856         if (spot)
857                 return spot;
858
859 // testinfo_player_start is only found in regioned levels
860         spot = find (world, classname, "testplayerstart");
861         if (spot)
862                 return spot;
863
864 // then look for the start position
865         spot = find (world, classname, "info_player_deathmatch");
866         if (spot)
867                 return spot;
868
869         //objerror ("FindIntermission: no spot");
870         return world;
871 };
872 */
873
874 /*
875 ===============================================================================
876
877 RULES
878
879 ===============================================================================
880 */
881
882 void() DumpStats =
883 {
884         local float file;
885         local string s;
886
887         if(cvar("_printstats"))
888                 cvar_set("_printstats", "0");
889         else if(!gameover)
890                 return;
891
892         if(gameover)
893                 s = ":scores:";
894         else
895                 s = ":status:";
896
897         s = strcat(s, GetMapname(), ":", ftos(rint(time)));
898
899         if(cvar("sv_eventlog") && gameover)
900                 GameLogEcho(s, FALSE);
901         else if(cvar("sv_logscores_console"))
902                 ServerConsoleEcho(s, FALSE);
903         if(cvar("sv_logscores_file"))
904         {
905                 file = fopen(cvar_string("sv_logscores_filename"), FILE_APPEND);
906                 fputs(file, strcat(s, "\n"));
907         }
908
909         FOR_EACH_CLIENT(other)
910         {
911                 if ((clienttype(other) == CLIENTTYPE_REAL) || (clienttype(other) == CLIENTTYPE_BOT && cvar("sv_logscores_bots")))
912                 {
913                         s = strcat(":player:", ftos(other.frags), ":");
914                         s = strcat(s, ftos(other.deaths), ":");
915                         s = strcat(s, ftos(rint(time - other.jointime)), ":");
916                         s = strcat(s, ftos(other.team), ":");
917
918                         if(cvar("sv_logscores_file"))
919                                 fputs(file, strcat(s, other.netname, "\n"));
920                         if(cvar("sv_eventlog") && gameover)
921                                 GameLogEcho(strcat(s, ftos(other.playerid), ":", other.netname), TRUE);
922                         else if(cvar("sv_logscores_console"))
923                                 ServerConsoleEcho(strcat(s, other.netname), TRUE);
924                 }
925         }
926
927         if(cvar("sv_eventlog") && gameover)
928                 GameLogEcho(":end", FALSE);
929         else if(cvar("sv_logscores_console"))
930                 ServerConsoleEcho(":end", FALSE);
931         if(cvar("sv_logscores_file"))
932         {
933                 fputs(file, ":end\n");
934                 fclose(file);
935         }
936 }
937
938 void FixIntermissionClient(entity e)
939 {
940         if(!e.autoscreenshot) // initial call
941         {
942                 e.angles = e.v_angle;
943                 e.angles_x = -e.angles_x;
944                 e.autoscreenshot = time + 0.8;  // used for autoscreenshot
945                 e.health = -2342;
946                 // first intermission phase; voting phase has positive health (used to decide whether to send SVC_FINALE or not)
947                 e.solid = SOLID_NOT;
948                 e.movetype = MOVETYPE_NONE;
949                 e.takedamage = DAMAGE_NO;
950                 if(e.weaponentity)
951                         e.weaponentity.effects = EF_NODRAW;
952                 stuffcmd(e, "\nscr_printspeed 1000000\n");
953                 if(clienttype(e) == CLIENTTYPE_REAL)
954                 {
955                         msg_entity = e;
956                         WriteByte(MSG_ONE, SVC_INTERMISSION);
957                 }
958         }
959
960         //e.velocity = '0 0 0';
961         //e.fixangle = TRUE;
962
963         // TODO halt weapon animation
964 }
965
966
967 /*
968 go to the next level for deathmatch
969 only called if a time or frag limit has expired
970 */
971 void() NextLevel =
972 {
973         float minTotalFrags;
974         float maxTotalFrags;
975         float score;
976         float f;
977
978         gameover = TRUE;
979
980         intermission_running = 1;
981
982 // enforce a wait time before allowing changelevel
983         if(player_count > 0)
984                 intermission_exittime = time + cvar("sv_mapchange_delay");
985         else
986                 intermission_exittime = -60;
987
988         WriteByte (MSG_ALL, SVC_CDTRACK);
989         WriteByte (MSG_ALL, 3);
990         WriteByte (MSG_ALL, 3);
991
992         //pos = FindIntermission ();
993
994         VoteReset();
995
996         DumpStats();
997
998         if(cvar("sv_eventlog"))
999                 GameLogEcho(":gameover", FALSE);
1000
1001         GameLogClose();
1002
1003         maxTotalFrags = 0;
1004         FOR_EACH_CLIENT(other)
1005         {
1006                 if(maxTotalFrags < other.totalfrags)
1007                         maxTotalFrags = other.totalfrags;
1008                 if(minTotalFrags > other.totalfrags)
1009                         minTotalFrags = other.totalfrags;
1010         }
1011
1012         FOR_EACH_CLIENT(other)
1013         {
1014                 FixIntermissionClient(other);
1015
1016                 self = other;
1017
1018                 if(other.winning)
1019                         bprint(other.netname, " ^7wins.\n");
1020
1021                 if(!currentbots)
1022                 {
1023                         score = (other.frags - minTotalFrags) / max(maxTotalFrags - minTotalFrags, 1);
1024                         f = bound(0, other.play_time / max(time, 1), 1);
1025                         // store some statistics?
1026                 }
1027         }
1028
1029         if(cvar("g_campaign"))
1030                 CampaignPreIntermission();
1031
1032         // WriteByte (MSG_ALL, SVC_INTERMISSION);
1033 };
1034
1035 /*
1036 ============
1037 CheckRules_Player
1038
1039 Exit deathmatch games upon conditions
1040 ============
1041 */
1042 void() CheckRules_Player =
1043 {
1044         if (gameover)   // someone else quit the game already
1045                 return;
1046
1047         if(self.deadflag == DEAD_NO)
1048                 self.play_time += frametime;
1049
1050         // fixme: don't check players; instead check dom_team and ctf_team entities
1051         //   (div0: and that in CheckRules_World please)
1052 };
1053
1054 float checkrules_oneminutewarning;
1055 float checkrules_leaderfrags;
1056 float tdm_max_score, tdm_old_score;
1057
1058 float checkrules_equality;
1059 float checkrules_overtimewarning;
1060 float checkrules_overtimeend;
1061
1062 void() InitiateOvertime =
1063 {
1064         if(!checkrules_overtimeend)
1065                 checkrules_overtimeend = time + 60 * cvar("timelimit_maxovertime");
1066 }
1067
1068 float WINNING_NO = 0; // no winner, but time limits may terminate the game
1069 float WINNING_YES = 1; // winner found
1070 float WINNING_NEVER = 2; // no winner, enter overtime if time limit is reached
1071 float WINNING_STARTOVERTIME = 3; // no winner, enter overtime NOW
1072
1073 float(float fraglimitreached, float equality) GetWinningCode =
1074 {
1075         if(equality)
1076                 if(fraglimitreached)
1077                         return WINNING_STARTOVERTIME;
1078                 else
1079                         return WINNING_NEVER;
1080         else
1081                 if(fraglimitreached)
1082                         return WINNING_YES;
1083                 else
1084                         return WINNING_NO;
1085 }
1086
1087 // set the .winning flag for exactly those players with a given field value
1088 void(.float field, float value) SetWinners =
1089 {
1090         entity head;
1091         FOR_EACH_PLAYER(head)
1092                 head.winning = (head.field == value);
1093 }
1094
1095 // set the .winning flag for those players with a given field value
1096 void(.float field, float value) AddWinners =
1097 {
1098         entity head;
1099         FOR_EACH_PLAYER(head)
1100                 if(head.field == value)
1101                         head.winning = 1;
1102 }
1103
1104 // clear the .winning flags
1105 void(void) ClearWinners =
1106 {
1107         entity head;
1108         FOR_EACH_PLAYER(head)
1109                 head.winning = 0;
1110 }
1111
1112 float() LMS_NewPlayerLives =
1113 {
1114         float fl;
1115         fl = cvar("fraglimit");
1116         if(fl == 0)
1117                 fl = 999;
1118
1119         // first player has left the game for dying too much? Nobody else can get in.
1120         if(lms_lowest_lives < 1)
1121                 return FALSE;
1122
1123         if(!cvar("g_lms_join_anytime"))
1124                 if(lms_lowest_lives < fl - cvar("g_lms_last_join"))
1125                         return FALSE;
1126
1127         return bound(1, lms_lowest_lives, fl);
1128 }
1129
1130 // LMS winning condition: game terminates if and only if there's at most one
1131 // one player who's living lives. Top two scores being equal cancels the time
1132 // limit.
1133 float() WinningCondition_LMS =
1134 {
1135         entity head;
1136         float have_player;
1137         float have_players;
1138         float l;
1139
1140         have_player = FALSE;
1141         have_players = FALSE;
1142         l = LMS_NewPlayerLives();
1143
1144         head = find(world, classname, "player");
1145         if(head)
1146                 have_player = TRUE;
1147         head = find(head, classname, "player");
1148         if(head)
1149                 have_players = TRUE;
1150
1151         if(have_player)
1152         {
1153                 // we have at least one player
1154                 if(have_players)
1155                 {
1156                         // two or more active players - continue with the game
1157                 }
1158                 else
1159                 {
1160                         // exactly one player?
1161                         if(l)
1162                         {
1163                                 // but no game has taken place yet
1164                         }
1165                         else
1166                         {
1167                                 // a winner!
1168                                 ClearWinners(); SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
1169                                 dprint("Have a winner, ending game.\n");
1170                                 return WINNING_YES;
1171                         }
1172                 }
1173         }
1174         else
1175         {
1176                 // nobody is playing at all...
1177                 if(l)
1178                 {
1179                         // wait for players...
1180                 }
1181                 else
1182                 {
1183                         // SNAFU (maybe a draw game?)
1184                         ClearWinners();
1185                         dprint("No players, ending game.\n");
1186                         return WINNING_YES;
1187                 }
1188         }
1189
1190         // When we get here, we have at least two players who are actually LIVING,
1191         // or one player who is still waiting for a victim to join the server. Now
1192         // check if the top two players have equal score.
1193
1194         checkrules_leaderfrags = 0;
1195         checkrules_equality = FALSE;
1196         FOR_EACH_PLAYER(head)
1197         {
1198                 if(head.frags > checkrules_leaderfrags)
1199                 {
1200                         checkrules_leaderfrags = head.frags;
1201                         checkrules_equality = FALSE;
1202                 }
1203                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
1204                         checkrules_equality = TRUE;
1205         }
1206
1207         SetWinners(frags, checkrules_leaderfrags);
1208
1209         // The top two players have the same amount of lives? No timelimit then,
1210         // enter overtime...
1211
1212         if(checkrules_equality)
1213                 return WINNING_NEVER;
1214
1215         // Top two have different scores? Way to go for our beloved TIMELIMIT!
1216         return WINNING_NO;
1217 }
1218
1219 // DM winning condition: game terminates if a player reached the fraglimit,
1220 // unless the first two players have the same score. The latter case also
1221 // breaks the time limit.
1222 float(float fraglimit) WinningCondition_MaxIndividualScore =
1223 {
1224         float checkrules_oldleaderfrags;
1225         entity head;
1226
1227         checkrules_oldleaderfrags = checkrules_leaderfrags;
1228         checkrules_leaderfrags = 0;
1229         checkrules_equality = FALSE;
1230         FOR_EACH_PLAYER(head)
1231         {
1232                 if(head.frags > checkrules_leaderfrags)
1233                 {
1234                         checkrules_leaderfrags = head.frags;
1235                         checkrules_equality = FALSE;
1236                 }
1237                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
1238                         checkrules_equality = TRUE;
1239         }
1240
1241         if(checkrules_leaderfrags > 0)
1242                 SetWinners(frags, checkrules_leaderfrags);
1243         else
1244                 ClearWinners();
1245
1246         if (!cvar("g_runematch"))
1247                 if (checkrules_leaderfrags != checkrules_oldleaderfrags)
1248                 {
1249                         if (checkrules_leaderfrags == fraglimit - 1)
1250                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
1251                         else if (checkrules_leaderfrags == fraglimit - 2)
1252                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
1253                         else if (checkrules_leaderfrags == fraglimit - 3)
1254                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
1255                 }
1256
1257         return GetWinningCode(fraglimit && checkrules_leaderfrags >= fraglimit, checkrules_equality);
1258 }
1259
1260 float(float fraglimit) WinningConditionBase_Teamplay =
1261 {
1262         tdm_old_score = tdm_max_score;
1263         tdm_max_score = max4(team1_score, team2_score, team3_score, team4_score);
1264
1265         checkrules_equality =
1266         (
1267                 (tdm_max_score > 0)
1268                 &&
1269                 (
1270                           (team1_score == tdm_max_score)
1271                         + (team2_score == tdm_max_score)
1272                         + (team3_score == tdm_max_score)
1273                         + (team4_score == tdm_max_score)
1274                         >= 2));
1275
1276         ClearWinners();
1277         if(tdm_max_score > 0)
1278         {
1279                 if(team1_score == tdm_max_score)
1280                         AddWinners(team, COLOR_TEAM1);
1281                 if(team2_score == tdm_max_score)
1282                         AddWinners(team, COLOR_TEAM2);
1283                 if(team3_score == tdm_max_score)
1284                         AddWinners(team, COLOR_TEAM3);
1285                 if(team4_score == tdm_max_score)
1286                         AddWinners(team, COLOR_TEAM4);
1287         }
1288
1289         if(!cvar("g_runematch") && !cvar("g_domination"))
1290                 if(tdm_max_score != tdm_old_score)
1291                 {
1292                         if(tdm_max_score == fraglimit - 1)
1293                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
1294                         else if(tdm_max_score == fraglimit - 2)
1295                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
1296                         else if(tdm_max_score == fraglimit - 3)
1297                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
1298                 }
1299
1300         return GetWinningCode(fraglimit && tdm_max_score >= fraglimit, checkrules_equality);
1301 }
1302
1303 // TDM winning condition: game terminates if a team's score sum reached the
1304 // fraglimit, unless the first two teams have the same total score. The latter
1305 // case also breaks the time limit.
1306 float(float fraglimit) WinningCondition_MaxTeamSum =
1307 {
1308         entity head;
1309
1310         team1_score = team2_score = team3_score = team4_score = 0;
1311
1312         FOR_EACH_PLAYER(head)
1313         {
1314                 if(head.team == COLOR_TEAM1)
1315                         team1_score += head.frags;
1316                 else if(head.team == COLOR_TEAM2)
1317                         team2_score += head.frags;
1318                 else if(head.team == COLOR_TEAM3)
1319                         team3_score += head.frags;
1320                 else if(head.team == COLOR_TEAM4)
1321                         team4_score += head.frags;
1322         }
1323
1324         return WinningConditionBase_Teamplay(fraglimit);
1325 }
1326
1327 // DOM/CTF winning condition: game terminates if the max of a team's players'
1328 // score reached the fraglimit, unless the first two teams have the same
1329 // maximum score. The latter case also breaks the time limit.
1330 float(float fraglimit) WinningCondition_MaxTeamMax =
1331 {
1332         entity head;
1333
1334         team1_score = team2_score = team3_score = team4_score = 0;
1335
1336         FOR_EACH_PLAYER(head)
1337         {
1338                 if(head.team == COLOR_TEAM1)
1339                 {
1340                         if(head.frags > team1_score)
1341                                 team1_score = head.frags;
1342                 }
1343                 else if(head.team == COLOR_TEAM2)
1344                 {
1345                         if(head.frags > team2_score)
1346                                 team2_score = head.frags;
1347                 }
1348                 else if(head.team == COLOR_TEAM3)
1349                 {
1350                         if(head.frags > team3_score)
1351                                 team3_score = head.frags;
1352                 }
1353                 else if(head.team == COLOR_TEAM4)
1354                 {
1355                         if(head.frags > team4_score)
1356                                 team4_score = head.frags;
1357                 }
1358         }
1359
1360         return WinningConditionBase_Teamplay(fraglimit);
1361 }
1362
1363 void print_to(entity e, string s)
1364 {
1365         if(e)
1366                 sprint(e, strcat(s, "\n"));
1367         else
1368                 ServerConsoleEcho(s, TRUE);
1369 }
1370
1371 void PrintScoreboardFor(entity e, string name, string colorcode, float whichteam)
1372 {
1373         entity head;
1374         float v;
1375         float teamvalue;
1376         float fragtotal;
1377         string s;
1378         float found;
1379         found = FALSE;
1380         teamvalue = 0;
1381         FOR_EACH_PLAYER(head)
1382         {
1383                 if(!whichteam || head.team == whichteam)
1384                 {
1385                         if(name != "")
1386                                 if(!found)
1387                                         print_to(e, strcat(" ", colorcode, name, ":"));
1388                         found = TRUE;
1389                         fragtotal = fragtotal + head.frags;
1390                         s = ftos(head.frags);
1391                         s = strcat(s, "/", ftos(head.deaths));
1392                         s = strcat(s, " @ ", ftos(head.ping));
1393                         if(clienttype(head) == CLIENTTYPE_BOT)
1394                                 s = strcat(s, "botms");
1395                         else
1396                                 s = strcat(s, "ms");
1397                         v = PlayerValue(head);
1398                         teamvalue += v;
1399                         s = strcat(s, " / ", ftos(v));
1400                         print_to(e, strcat("  ", colorcode, head.netname, colorcode, " (", s, ")"));
1401                 }
1402         }
1403         if(whichteam && found)
1404         {
1405                 s = ftos(fragtotal);
1406                 s = strcat(s, " / ", ftos(teamvalue));
1407                 print_to(e, strcat(colorcode, "  (total: ", s, ")"));
1408         }
1409 }
1410
1411 void PrintScoreboard(entity e)
1412 {
1413         print_to(e, strcat("Time:      ", ftos(time / 60)));
1414         print_to(e, strcat("Timelimit: ", ftos(cvar("timelimit"))));
1415         print_to(e, strcat("Fraglimit: ", ftos(cvar("fraglimit"))));
1416         print_to(e, "Scoreboard:");
1417         if(teams_matter)
1418         {
1419                 PrintScoreboardFor(e, "Red", "^1", COLOR_TEAM1);
1420                 PrintScoreboardFor(e, "Blue", "^4", COLOR_TEAM2);
1421                 PrintScoreboardFor(e, "Pink", "^6", COLOR_TEAM3);
1422                 PrintScoreboardFor(e, "Yellow", "^3", COLOR_TEAM4);
1423         }
1424         else
1425         {
1426                 PrintScoreboardFor(e, "", "^7", 0);
1427         }
1428         print_to(e, ".");
1429 }
1430
1431 void RemoveFromMaplist(string m)
1432 {
1433         string result;
1434         float litems;
1435         float i;
1436         float found;
1437
1438         litems = tokenize(cvar_string("g_maplist"));
1439         found = 0;
1440         result = "";
1441         for(i = 0; i < litems; ++i)
1442         {
1443                 m = strcat1(m);
1444                 result = strcat1(result);
1445                 if(argv(i) == m)
1446                         found += 1;
1447                 else
1448                         result = strcat(result, "'", argv(i), "'");
1449         }
1450         if(found)
1451                 cvar_set("g_maplist", result);
1452         ServerConsoleEcho(strcat("Removed ", ftos(found), " items."), FALSE);
1453 }
1454
1455 void AddToMaplist(string m)
1456 {
1457         string result;
1458         float found;
1459         float litems;
1460         float i;
1461         float ipos;
1462         float inserted;
1463
1464         if(!TryFile(strcat("maps/", m, ".mapcfg")))
1465         {
1466                 ServerConsoleEcho("Map not found.", FALSE);
1467                 return;
1468         }
1469
1470         litems = tokenize(cvar_string("g_maplist"));
1471         if(cvar("g_maplist_shuffle"))
1472                 ipos = ceil(random() * (litems + 1)) - 1;
1473         else
1474                 ipos = litems;
1475         found = 0;
1476         inserted = 0;
1477         for(i = 0; i < litems; ++i)
1478         {
1479                 m = strcat1(m);
1480                 if(i == ipos)
1481                 {
1482                         result = strcat(result, "'", m, "'");
1483                         inserted = 1;
1484                 }
1485                 result = strcat(result, "'", argv(i), "'");
1486                 if(argv(i) == m)
1487                         found += 1;
1488         }
1489         if(!inserted)
1490                 result = strcat(result, "'", m, "'");
1491         if(!found)
1492         {
1493                 cvar_set("g_maplist", result);
1494                 ServerConsoleEcho("Map added.", FALSE);
1495         }
1496         else
1497                 ServerConsoleEcho("Map already in list.", FALSE);
1498 }
1499
1500 void MakeFirstInMaplist(string m)
1501 {
1502         if(!TryFile(strcat("maps/", m, ".mapcfg")))
1503         {
1504                 ServerConsoleEcho("Map not found.", FALSE);
1505                 return;
1506         }
1507
1508         m = strzone(m);
1509         RemoveFromMaplist(m);
1510         cvar_set("g_maplist", strcat("'", m, "'", cvar_string("g_maplist")));
1511         strunzone(m);
1512
1513         ServerConsoleEcho("Map added as first one.", FALSE);
1514 }
1515
1516 void ShuffleMaplist()
1517 {
1518         string result;
1519         float start;
1520         float litems;
1521         float selected;
1522         float i;
1523
1524         result = cvar_string("g_maplist");
1525         litems = tokenize(result);
1526
1527         for(start = 0; start < litems - 1; ++start)
1528         {
1529                 result = "";
1530
1531                 // select a random item
1532                 selected = ceil(random() * (litems - start) + start) - 1;
1533
1534                 // shift this item to the place start
1535                 for(i = 0; i < start; ++i)
1536                         result = strcat(result, "'", argv(i), "'");
1537                 result = strcat(result, "'", argv(selected), "'");
1538                 for(i = start; i < litems; ++i)
1539                         if(i != selected)
1540                                 result = strcat(result, "'", argv(i), "'");
1541
1542                 litems = tokenize(result);
1543
1544                 //dprint(result, "\n");
1545         }
1546
1547         cvar_set("g_maplist", result);
1548 }
1549
1550 void() HandleMaplistShuffleCommands =
1551 {
1552         // automatically shuffle when setting g_maplist_shuffle
1553         if(cvar_string("_g_maplist_add") != "")
1554         {
1555                 AddToMaplist(cvar_string("_g_maplist_add"));
1556                 cvar_set("_g_maplist_add", "");
1557         }
1558         if(cvar_string("_g_maplist_remove") != "")
1559         {
1560                 RemoveFromMaplist(cvar_string("_g_maplist_remove"));
1561                 cvar_set("_g_maplist_remove", "");
1562         }
1563         if(cvar_string("_g_maplist_putfirst") != "")
1564         {
1565                 MakeFirstInMaplist(cvar_string("_g_maplist_putfirst"));
1566                 cvar_set("_g_maplist_putfirst", "");
1567         }
1568         if(cvar("_g_maplist_shufflenow") || (cvar("g_maplist_shuffle") && !cvar("_g_maplist_have_shuffled")))
1569         {
1570                 ShuffleMaplist();
1571                 cvar_set("_g_maplist_shufflenow", "0");
1572                 cvar_set("_g_maplist_have_shuffled", "1");
1573                 ServerConsoleEcho("Shuffled map list.", FALSE);
1574         }
1575         if(cvar("_g_maplist_have_shuffled"))
1576                 if(!cvar("g_maplist_shuffle"))
1577                         cvar_set("_g_maplist_have_shuffled", "0");
1578 }
1579
1580 /*
1581 ============
1582 CheckRules_World
1583
1584 Exit deathmatch games upon conditions
1585 ============
1586 */
1587 void() CheckRules_World =
1588 {
1589         local float status;
1590         local float timelimit;
1591         local float fraglimit;
1592
1593         VoteThink();
1594
1595         SetDefaultAlpha();
1596
1597         if (intermission_running)
1598                 if (time >= intermission_exittime + 60)
1599                 {
1600                         if(!DoNextMapOverride())
1601                                 GotoNextMap();
1602                         return;
1603                 }
1604
1605         if (gameover)   // someone else quit the game already
1606                 return;
1607
1608         DumpStats();
1609
1610         if(cvar("_scoreboard"))
1611         {
1612                 cvar_set("_scoreboard", "0");
1613                 PrintScoreboard(world);
1614         }
1615
1616         HandleMaplistShuffleCommands();
1617
1618         timelimit = cvar("timelimit") * 60;
1619         fraglimit = cvar("fraglimit");
1620
1621         if(checkrules_overtimeend)
1622         {
1623                 if(!checkrules_overtimewarning)
1624                 {
1625                         checkrules_overtimewarning = TRUE;
1626                         //sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1627                         bcenterprint("^3Now playing ^1OVERTIME^3!\n\n^3Keep fragging until we have a ^1winner^3!");
1628                 }
1629         }
1630         else
1631         {
1632                 if (timelimit && time >= timelimit)
1633                         InitiateOvertime();
1634         }
1635
1636         if (checkrules_overtimeend && time >= checkrules_overtimeend)
1637         {
1638                 NextLevel();
1639                 return;
1640         }
1641
1642         if (!checkrules_oneminutewarning && timelimit > 0 && time > timelimit - 60)
1643         {
1644                 checkrules_oneminutewarning = TRUE;
1645                 sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1646         }
1647
1648         status = WINNING_NO;
1649         if(cvar("g_lms"))
1650         {
1651                 status = WinningCondition_LMS();
1652         }
1653         else
1654         {
1655                 if(teams_matter)
1656                 {
1657                         if(cvar("g_tdm") || cvar("g_runematch") || cvar("g_ctf") || cvar("g_domination"))
1658                                 status = WinningCondition_MaxTeamSum(fraglimit);
1659                         //else if()
1660                         //      status = WinningCondition_MaxTeamMax(fraglimit);
1661                         else
1662                         {
1663                                 dprint("div0: How can this happen?\n");
1664                                 status = WinningCondition_MaxTeamMax(fraglimit);
1665                         }
1666                 }
1667                 else
1668                         status = WinningCondition_MaxIndividualScore(fraglimit);
1669         }
1670
1671         if(status == WINNING_STARTOVERTIME)
1672         {
1673                 status = WINNING_NEVER;
1674                 InitiateOvertime();
1675         }
1676
1677         if(status == WINNING_NEVER)
1678                 // equality cases! Nobody wins if the overtime ends in a draw.
1679                 ClearWinners();
1680
1681         if(checkrules_overtimeend)
1682                 if(status != WINNING_NEVER || time >= checkrules_overtimeend)
1683                         status = WINNING_YES;
1684
1685         if(status == WINNING_YES)
1686                 NextLevel();
1687 };
1688
1689 float randsel_value;
1690 float randsel_priority;
1691 float randsel_count;
1692 void RandSel_Init()
1693 {
1694         randsel_value = -1;
1695         randsel_priority = -1;
1696         randsel_count = -1;
1697 }
1698 void RandSel_Add(float priority, float value)
1699 {
1700         if(priority > randsel_priority)
1701         {
1702                 randsel_priority = priority;
1703                 randsel_value = value;
1704                 randsel_count = 1;
1705         }
1706         else if(priority == randsel_priority)
1707         {
1708                 randsel_count += 1;
1709                 if(ceil(random() * randsel_count) == 1)
1710                         randsel_value = value;
1711         }
1712 }
1713
1714 float mapvote_nextthink;
1715 float mapvote_initialized;
1716 float mapvote_keeptwotime;
1717 float mapvote_timeout;
1718 string mapvote_message;
1719
1720 #define MAPVOTE_COUNT 10
1721 float mapvote_count;
1722 string mapvote_maps[MAPVOTE_COUNT];
1723 float mapvote_maps_suggested[MAPVOTE_COUNT];
1724 string mapvote_suggestions[MAPVOTE_COUNT];
1725 float mapvote_suggestion_ptr;
1726 string mapvote_fillstr;
1727 float mapvote_maxlen;
1728 float mapvote_voters;
1729 float mapvote_votes[MAPVOTE_COUNT];
1730 .float mapvote;
1731
1732 void MapVote_ClearAllVotes()
1733 {
1734         FOR_EACH_CLIENT(other)
1735                 other.mapvote = 0;
1736 }
1737
1738 string MapVote_Suggest(string m)
1739 {
1740         float i;
1741         if(m == "")
1742                 return "That's not how to use this command.";
1743         if(!cvar("g_maplist_votable_suggestions"))
1744                 return "Suggestions are not accepted on this server.";
1745         if(mapvote_initialized)
1746                 return "Can't suggest - voting is already in progress!";
1747         if(!TryFile(strcat("maps/", m, ".mapcfg")))
1748                 return "The map you suggested is not available on this server.";
1749         for(i = 0; i < mapvote_suggestion_ptr; ++i)
1750                 if(mapvote_suggestions[i] == m)
1751                         return "This map was already suggested.";
1752         if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
1753         {
1754                 i = ceil(random() * mapvote_suggestion_ptr) - 1;
1755         }
1756         else
1757         {
1758                 i = mapvote_suggestion_ptr;
1759                 mapvote_suggestion_ptr += 1;
1760         }
1761         if(mapvote_suggestions[i] != "")
1762                 strunzone(mapvote_suggestions[i]);
1763         mapvote_suggestions[i] = strzone(m);
1764         GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid), ":", self.netname), TRUE);
1765         return "Suggestion accepted.";
1766 }
1767
1768 void MapVote_AddVotable(string nextMap, float isSuggestion)
1769 {
1770         float j;
1771         if(nextMap == "")
1772                 return;
1773         for(j = 0; j < mapvote_count; ++j)
1774                 if(mapvote_maps[j] == nextMap)
1775                         return;
1776         if(strlen(nextMap) > mapvote_maxlen)
1777                 mapvote_maxlen = strlen(nextMap);
1778         mapvote_maps[mapvote_count] = strzone(nextMap);
1779         mapvote_maps_suggested[mapvote_count] = isSuggestion;
1780         mapvote_count += 1;
1781 }
1782
1783 void MapVote_Init()
1784 {
1785         float i;
1786         float nmax, smax;
1787
1788         MapVote_ClearAllVotes();
1789
1790         nmax = min(MAPVOTE_COUNT, cvar("g_maplist_votable"));
1791         smax = min(nmax, cvar("g_maplist_votable_suggestions"));
1792         mapvote_count = 0;
1793
1794         for(i = 0; i < 100 && mapvote_count < smax; ++i)
1795                 MapVote_AddVotable(mapvote_suggestions[ceil(random() * mapvote_suggestion_ptr) - 1], TRUE);
1796
1797         for(i = 0; i < 100 && mapvote_count < nmax; ++i)
1798                 MapVote_AddVotable(GetNextMap(), FALSE);
1799
1800         if(mapvote_count == 0)
1801         {
1802                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );
1803                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
1804                 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
1805                         MapVote_AddVotable(GetNextMap(), FALSE);
1806         }
1807
1808         //dprint("mapvote count is ", ftos(mapvote_count), "\n");
1809
1810         mapvote_fillstr = " ";
1811         while(strlen(mapvote_fillstr) < mapvote_maxlen + 16)
1812                 mapvote_fillstr = strcat(mapvote_fillstr, mapvote_fillstr);
1813         mapvote_fillstr = strzone(mapvote_fillstr);
1814
1815         mapvote_keeptwotime = time + cvar("g_maplist_votable_keeptwotime");
1816         mapvote_timeout = time + cvar("g_maplist_votable_timeout");
1817         if(mapvote_count < 3 || mapvote_keeptwotime <= time)
1818                 mapvote_keeptwotime = 0;
1819         mapvote_message = "Choose a map and press its key!";
1820 }
1821 float MapVote_Finished(float mappos)
1822 {
1823         string result;
1824         float i;
1825
1826         result = strcat(":vote:finished:", mapvote_maps[mappos]);
1827         result = strcat(result, ":", ftos(mapvote_votes[mappos]), "::");
1828         for(i = 0; i < mapvote_count; ++i)
1829                 if(i != mappos)
1830                         if(mapvote_maps[i] != "")
1831                         {
1832                                 result = strcat(result, ":", mapvote_maps[i]);
1833                                 result = strcat(result, ":", ftos(mapvote_votes[i]));
1834                         }
1835         GameLogEcho(result, FALSE);
1836         if(mapvote_maps_suggested[mappos])
1837                 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]), FALSE);
1838
1839         FOR_EACH_REALCLIENT(other)
1840                 FixClientCvars(other);
1841
1842         Map_Goto_SetStr(mapvote_maps[mappos]);
1843         Map_Goto();
1844         return TRUE;
1845 }
1846 void MapVote_CheckRules_1()
1847 {
1848         float i;
1849
1850         for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")
1851         {
1852                 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
1853                 mapvote_votes[i] = 0;
1854         }
1855
1856         mapvote_voters = 0;
1857         FOR_EACH_REALCLIENT(other)
1858         {
1859                 ++mapvote_voters;
1860                 if(other.mapvote)
1861                 {
1862                         i = other.mapvote - 1;
1863                         //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
1864                         mapvote_votes[i] = mapvote_votes[i] + 1;
1865                 }
1866         }
1867 }
1868
1869 float MapVote_CheckRules_2()
1870 {
1871         float i;
1872         float firstPlace, secondPlace;
1873         float firstPlaceVotes, secondPlaceVotes;
1874         string result;
1875
1876         RandSel_Init();
1877         for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")
1878                 RandSel_Add(mapvote_votes[i], i);
1879         firstPlace = randsel_value;
1880         firstPlaceVotes = randsel_priority;
1881         //dprint("First place: ", ftos(firstPlace), "\n");
1882         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
1883
1884         RandSel_Init();
1885         for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")
1886                 if(i != firstPlace)
1887                         RandSel_Add(mapvote_votes[i], i);
1888         secondPlace = randsel_value;
1889         secondPlaceVotes = randsel_priority;
1890         //dprint("Second place: ", ftos(secondPlace), "\n");
1891         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
1892
1893         if(firstPlace == -1)
1894                 error("No first place in map vote... WTF?");
1895
1896         if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters - firstPlaceVotes) < firstPlaceVotes)
1897                 return MapVote_Finished(firstPlace);
1898
1899         if(mapvote_keeptwotime)
1900                 if(time > mapvote_keeptwotime || (mapvote_voters - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
1901                 {
1902                         mapvote_message = "Now decide between the TOP TWO!";
1903                         mapvote_keeptwotime = 0;
1904                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
1905                         result = strcat(result, ":", ftos(firstPlaceVotes));
1906                         result = strcat(result, ":", mapvote_maps[secondPlace]);
1907                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");
1908                         for(i = 0; i < mapvote_count; ++i)
1909                                 if(i != firstPlace)
1910                                         if(i != secondPlace)
1911                                                 if(mapvote_maps[i] != "")
1912                                                 {
1913                                                         result = strcat(result, ":", mapvote_maps[i]);
1914                                                         result = strcat(result, ":", ftos(mapvote_votes[i]));
1915                                                         strunzone(mapvote_maps[i]);
1916                                                         mapvote_maps[i] = "";
1917                                                 }
1918                         GameLogEcho(result, FALSE);
1919                 }
1920
1921         return FALSE;
1922 }
1923 void MapVote_Tick()
1924 {
1925         string msgstr;
1926         string tmp;
1927         float i;
1928         float keeptwo;
1929
1930         keeptwo = mapvote_keeptwotime;
1931         MapVote_CheckRules_1(); // count
1932         if(MapVote_CheckRules_2()) // decide
1933                 return;
1934
1935         FOR_EACH_REALCLIENT(other)
1936         {
1937                 // hide scoreboard again
1938                 if(other.health != 2342)
1939                 {
1940                         other.health = 2342;
1941                         other.impulse = 0;
1942                         if(clienttype(other) == CLIENTTYPE_REAL)
1943                         {
1944                                 stuffcmd(other, "\nin_bind 7 1 \"impulse 1\"; in_bind 7 2 \"impulse 2\"; in_bind 7 3 \"impulse 3\"; in_bind 7 4 \"impulse 4\"; in_bind 7 5 \"impulse 5\"; in_bind 7 6 \"impulse 6\"; in_bind 7 7 \"impulse 7\"; in_bind 7 8 \"impulse 8\"; in_bind 7 9 \"impulse 9\"; in_bind 7 0 \"impulse 10\"; in_bind 7 KP_1 \"impulse 1\"; in_bind 7 KP_2 \"impulse 2\"; in_bind 7 KP_3 \"impulse 3\"; in_bind 7 KP_4 \"impulse 4\"; in_bind 7 KP_5 \"impulse 5\"; in_bind 7 KP_6 \"impulse 6\"; in_bind 7 KP_7 \"impulse 7\"; in_bind 7 KP_8 \"impulse 8\"; in_bind 7 KP_9 \"impulse 9\"; in_bind 7 KP_0 \"impulse 10\"; in_bindmap 7 0\n");
1945                                 msg_entity = other;
1946                                 WriteByte(MSG_ONE, SVC_FINALE);
1947                                 WriteString(MSG_ONE, "");
1948                         }
1949                 }
1950
1951                 // notify about keep-two
1952                 if(keeptwo != 0 && mapvote_keeptwotime == 0)
1953                         stuffcmd(other, "\nplay2 misc/invshot.wav\n");
1954
1955                 // clear possibly invalid votes
1956                 if(mapvote_maps[other.mapvote - 1] == "")
1957                         other.mapvote = 0;
1958                 // use impulses as new vote
1959                 if(other.impulse >= 1 && other.impulse <= mapvote_count)
1960                         if(mapvote_maps[other.impulse - 1] != "")
1961                                 other.mapvote = other.impulse;
1962                 other.impulse = 0;
1963         }
1964
1965         MapVote_CheckRules_1(); // just count
1966
1967         FOR_EACH_REALCLIENT(other)
1968         {
1969                 // display voting screen
1970                 msgstr = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
1971                 msgstr = substring(msgstr, 0, strlen(msgstr) - mapvote_count);
1972                 msgstr = strcat(msgstr, mapvote_message);
1973                 msgstr = strcat(msgstr, "\n\n");
1974                 for(i = 0; i < mapvote_count; ++i)
1975                         if(mapvote_maps[i] == "")
1976                                 msgstr = strcat(msgstr, "\n");
1977                         else
1978                         {
1979                                 tmp = mapvote_maps[i];
1980                                 tmp = strcat(tmp, substring(mapvote_fillstr, 0, mapvote_maxlen  - strlen(tmp)));
1981                                 tmp = strcat(ftos(math_mod(i + 1, 10)), ": ", tmp);
1982                                 tmp = strcat(tmp, " ^2(", ftos(mapvote_votes[i]), " vote");
1983                                 if(mapvote_votes[i] != 1)
1984                                         tmp = strcat(tmp, "s");
1985                                 tmp = strcat(tmp, ")");
1986                                 tmp = strcat(tmp, substring(mapvote_fillstr, 0, mapvote_maxlen + 15 - strlen(tmp)));
1987                                 if(other.mapvote == i + 1)
1988                                         msgstr = strcat(msgstr, "^3> ", tmp, "\n");
1989                                 else
1990                                         msgstr = strcat(msgstr, "^7  ", tmp, "\n");
1991                         }
1992                 i = ceil(mapvote_timeout - time);
1993                 msgstr = strcat(msgstr, "\n\n", ftos(i), " second");
1994                 if(i != 1)
1995                         msgstr = strcat(msgstr, "s");
1996                 msgstr = strcat(msgstr, " left");
1997
1998                 centerprint_atprio(other, CENTERPRIO_MAPVOTE, msgstr);
1999         }
2000 }
2001 void MapVote_Think()
2002 {
2003         if(alreadychangedlevel)
2004                 return;
2005         
2006         if(time < mapvote_nextthink)
2007                 return;
2008         //dprint("tick\n");
2009
2010         mapvote_nextthink = time + 0.5;
2011
2012         if(!mapvote_initialized)
2013         {
2014                 mapvote_initialized = TRUE;
2015                 if(DoNextMapOverride())
2016                 {
2017                         alreadychangedlevel = TRUE;
2018                         return;
2019                 }
2020                 if(!cvar("g_maplist_votable"))
2021                 {
2022                         GotoNextMap();
2023                         return;
2024                 }
2025                 MapVote_Init();
2026         }
2027
2028         MapVote_Tick();
2029 };
2030
2031
2032
2033 float beamindex_modulo;
2034 void EndFrame()
2035 {
2036         entity e;
2037         string value;
2038         vector mi, ma;
2039         float i;
2040
2041         value = cvar_string("_entityboxes_for");
2042         if(value == "")
2043                 return;
2044
2045         i = beamindex_modulo;
2046         beamindex_modulo += 1;
2047         if(beamindex_modulo >= 32)
2048                 beamindex_modulo = 0;
2049         for(e = world; (e = find(e, classname, value)) != world; )
2050         {
2051                 mi = e.absmin;
2052                 ma = e.absmax;
2053                 i = i + 1;
2054                 if(i >= 32)
2055                 {
2056                         i = 0;
2057                         te_lightning1(world, mi_x * '1 0 0' + mi_y * '0 1 0' + mi_z * '0 0 1', ma_x * '1 0 0' + mi_y * '0 1 0' + mi_z * '0 0 1');
2058                         te_lightning1(world, mi_x * '1 0 0' + mi_y * '0 1 0' + ma_z * '0 0 1', ma_x * '1 0 0' + mi_y * '0 1 0' + ma_z * '0 0 1');
2059                         te_lightning1(world, mi_x * '1 0 0' + ma_y * '0 1 0' + mi_z * '0 0 1', ma_x * '1 0 0' + ma_y * '0 1 0' + mi_z * '0 0 1');
2060                         te_lightning1(world, mi_x * '1 0 0' + ma_y * '0 1 0' + ma_z * '0 0 1', ma_x * '1 0 0' + ma_y * '0 1 0' + ma_z * '0 0 1');
2061                         te_lightning1(world, mi_x * '1 0 0' + mi_y * '0 1 0' + mi_z * '0 0 1', mi_x * '1 0 0' + ma_y * '0 1 0' + mi_z * '0 0 1');
2062                         te_lightning1(world, mi_x * '1 0 0' + mi_y * '0 1 0' + ma_z * '0 0 1', mi_x * '1 0 0' + ma_y * '0 1 0' + ma_z * '0 0 1');
2063                         te_lightning1(world, ma_x * '1 0 0' + mi_y * '0 1 0' + mi_z * '0 0 1', ma_x * '1 0 0' + ma_y * '0 1 0' + mi_z * '0 0 1');
2064                         te_lightning1(world, ma_x * '1 0 0' + mi_y * '0 1 0' + ma_z * '0 0 1', ma_x * '1 0 0' + ma_y * '0 1 0' + ma_z * '0 0 1');
2065                         te_lightning1(world, mi_x * '1 0 0' + mi_y * '0 1 0' + mi_z * '0 0 1', mi_x * '1 0 0' + mi_y * '0 1 0' + ma_z * '0 0 1');
2066                         te_lightning1(world, mi_x * '1 0 0' + ma_y * '0 1 0' + mi_z * '0 0 1', mi_x * '1 0 0' + ma_y * '0 1 0' + ma_z * '0 0 1');
2067                         te_lightning1(world, ma_x * '1 0 0' + mi_y * '0 1 0' + mi_z * '0 0 1', ma_x * '1 0 0' + mi_y * '0 1 0' + ma_z * '0 0 1');
2068                         te_lightning1(world, ma_x * '1 0 0' + ma_y * '0 1 0' + mi_z * '0 0 1', ma_x * '1 0 0' + ma_y * '0 1 0' + ma_z * '0 0 1');
2069                 }
2070         }
2071 }