]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_world.qc
waypoint sprites system
[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_fire.ogg");
225         precache_sound ("weapons/grenade_impact.ogg");
226         precache_sound ("weapons/hagar_fire.ogg");
227         precache_sound ("weapons/hagexp1.ogg");
228         precache_sound ("weapons/hagexp2.ogg");
229         precache_sound ("weapons/hagexp3.ogg");
230         precache_sound ("weapons/hook_fire.ogg");
231         precache_sound ("weapons/hook_impact.ogg");
232         precache_sound ("weapons/lasergun_fire.ogg");
233         precache_sound ("weapons/laserimpact.ogg");
234         precache_sound ("weapons/nexfire.ogg");
235         precache_sound ("weapons/neximpact.ogg");
236         precache_sound ("weapons/ric1.ogg");
237         precache_sound ("weapons/ric2.ogg");
238         precache_sound ("weapons/ric3.ogg");
239         precache_sound ("weapons/rocket_fire.ogg");
240         precache_sound ("weapons/rocket_fly.wav");
241         precache_sound ("weapons/rocket_impact.ogg");
242         precache_sound ("weapons/rocket_det.ogg");
243         precache_sound ("weapons/shotgun_fire.ogg");
244         precache_sound ("weapons/tink1.ogg");
245         precache_sound ("weapons/uzi_fire.ogg");
246         precache_sound ("weapons/weapon_switch.ogg");
247         precache_sound ("weapons/weaponpickup.ogg");
248         precache_sound ("weapons/strength_fire.ogg");
249
250         //precache_sound ("announce/male/kill10.ogg");
251         //precache_sound ("announce/male/kill15.ogg");
252         //precache_sound ("announce/male/kill20.ogg");
253         //precache_sound ("announce/male/kill25.ogg");
254         //precache_sound ("announce/male/kill3.ogg");
255         //precache_sound ("announce/male/kill30.ogg");
256         //precache_sound ("announce/male/kill4.ogg");
257         //precache_sound ("announce/male/kill5.ogg");
258         //precache_sound ("announce/male/kill6.ogg");
259         //precache_sound ("announce/male/mapkill1.ogg");
260         //precache_sound ("announce/robotic/last_second_save.ogg");
261         //precache_sound ("announce/robotic/narrowly_averted.ogg");
262         //precache_sound ("minstagib/mockery.ogg");
263
264         // announcer sounds - male
265         precache_sound ("announcer/male/03kills.ogg");
266         precache_sound ("announcer/male/05kills.ogg");
267         precache_sound ("announcer/male/10kills.ogg");
268         precache_sound ("announcer/male/15kills.ogg");
269         precache_sound ("announcer/male/20kills.ogg");
270         precache_sound ("announcer/male/25kills.ogg");
271         precache_sound ("announcer/male/30kills.ogg");
272         precache_sound ("announcer/male/botlike.ogg");
273         precache_sound ("announcer/male/electrobitch.ogg");
274         precache_sound ("announcer/male/welcome.ogg");
275         precache_sound ("announcer/male/yoda.ogg");
276
277         // announcer sounds - robotic
278         precache_sound ("announcer/robotic/1fragleft.ogg");
279         precache_sound ("announcer/robotic/1minuteremains.ogg");
280         precache_sound ("announcer/robotic/2fragsleft.ogg");
281         precache_sound ("announcer/robotic/3fragsleft.ogg");
282         precache_sound ("announcer/robotic/lastsecond.ogg");
283         precache_sound ("announcer/robotic/narrowly.ogg");
284         precache_sound ("announcer/robotic/1.ogg");
285         precache_sound ("announcer/robotic/2.ogg");
286         precache_sound ("announcer/robotic/3.ogg");
287         precache_sound ("announcer/robotic/4.ogg");
288         precache_sound ("announcer/robotic/5.ogg");
289         precache_sound ("announcer/robotic/6.ogg");
290         precache_sound ("announcer/robotic/7.ogg");
291         precache_sound ("announcer/robotic/8.ogg");
292         precache_sound ("announcer/robotic/9.ogg");
293         precache_sound ("announcer/robotic/10.ogg");
294
295         // plays music for the level if there is any
296         if (self.noise)
297         {
298                 precache_sound (self.noise);
299                 ambientsound ('0 0 0', self.noise, 1.00, ATTN_NONE);
300         }
301
302         // 0 normal
303         lightstyle(0, "m");
304
305         // 1 FLICKER (first variety)
306         lightstyle(1, "mmnmmommommnonmmonqnmmo");
307
308         // 2 SLOW STRONG PULSE
309         lightstyle(2, "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba");
310
311         // 3 CANDLE (first variety)
312         lightstyle(3, "mmmmmaaaaammmmmaaaaaabcdefgabcdefg");
313
314         // 4 FAST STROBE
315         lightstyle(4, "mamamamamama");
316
317         // 5 GENTLE PULSE 1
318         lightstyle(5,"jklmnopqrstuvwxyzyxwvutsrqponmlkj");
319
320         // 6 FLICKER (second variety)
321         lightstyle(6, "nmonqnmomnmomomno");
322
323         // 7 CANDLE (second variety)
324         lightstyle(7, "mmmaaaabcdefgmmmmaaaammmaamm");
325
326         // 8 CANDLE (third variety)
327         lightstyle(8, "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa");
328
329         // 9 SLOW STROBE (fourth variety)
330         lightstyle(9, "aaaaaaaazzzzzzzz");
331
332         // 10 FLUORESCENT FLICKER
333         lightstyle(10, "mmamammmmammamamaaamammma");
334
335         // 11 SLOW PULSE NOT FADE TO BLACK
336         lightstyle(11, "abcdefghijklmnopqrrqponmlkjihgfedcba");
337
338         // styles 32-62 are assigned by the light program for switchable lights
339
340         // 63 testing
341         lightstyle(63, "a");
342
343         registercvar("_g_maplist_shufflenow", "0");
344         registercvar("_g_maplist_have_shuffled", "0");
345         registercvar("_g_maplist_add", "");
346         registercvar("_g_maplist_remove", "");
347         registercvar("_g_maplist_putfirst", "");
348
349         player_count = 0;
350         lms_lowest_lives = 0;
351         lms_next_place = 0;
352
353         GotoFirstMap();
354
355         bot_waypoints_for_items = cvar("g_waypoints_for_items");
356         if(bot_waypoints_for_items == 1)
357                 if(self.spawnflags & SPAWNFLAG_NO_WAYPOINTS_FOR_ITEMS)
358                         bot_waypoints_for_items = 0;
359
360         if(cvar("g_campaign"))
361                 CampaignPreInit();
362
363         InitGameplayMode();
364
365         WaypointSprite_Init();
366
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_string("quit_and_redirect") != "")
733         {
734                 entity head;
735                 FOR_EACH_REALCLIENT(head)
736                         stuffcmd(head, strcat("\nconnect ", cvar_string("quit_and_redirect"), "\n"));
737                 localcmd("wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; wait; quit\n");
738                 return TRUE;
739         }
740         if (cvar("samelevel")) // if samelevel is set, stay on same level
741         {
742                 // 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)
743                 //localcmd(strcat("exec \"maps/", mapname, ".mapcfg\"\n"));
744                 // so instead just restart the current map using the restart command (DOES NOT WORK PROPERLY WITH exit_cfg STUFF)
745                 localcmd("restart\n");
746                 //changelevel (mapname);
747                 return TRUE;
748         }
749         if(cvar_string("nextmap") != "")
750                 if(TryFile(strcat("maps/", cvar_string("nextmap"), ".mapcfg")))
751                 {
752                         Map_Goto_SetStr(cvar_string("nextmap"));
753                         Map_Goto();
754                         return TRUE;
755                 }
756         if(cvar("lastlevel"))
757         {
758                 GameResetCfg();
759                 localcmd("set lastlevel 0\ntogglemenu\n");
760                 return TRUE;
761         }
762         return FALSE;
763 };
764
765 void() GotoNextMap =
766 {
767         //local string nextmap;
768         //local float n, nummaps;
769         //local string s;
770         if (alreadychangedlevel)
771                 return;
772         alreadychangedlevel = TRUE;
773
774         {
775                 string nextMap;
776                 float allowReset;
777
778                 for(allowReset = 1; allowReset >= 0; --allowReset)
779                 {
780                         nextMap = GetNextMap();
781                         if(nextMap != "")
782                                 break;
783
784                         if(allowReset)
785                         {
786                                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );
787                                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
788                         }
789                         else
790                         {
791                                 error("Everything is broken - not even the default map list works. Please report this to the developers.");
792                         }
793                 }
794                 Map_Goto();
795         }
796 };
797
798
799 /*
800 ============
801 IntermissionThink
802
803 When the player presses attack or jump, change to the next level
804 ============
805 */
806 .float autoscreenshot;
807 void() MapVote_Think;
808 float mapvote_initialized;
809 void() IntermissionThink =
810 {
811         FixIntermissionClient(self);
812
813         if(cvar("sv_autoscreenshot"))
814         if(self.autoscreenshot)
815         if(time > self.autoscreenshot)
816         {
817                 self.autoscreenshot = FALSE;
818                 if(clienttype(self) == CLIENTTYPE_REAL)
819                         stuffcmd(self, "\nscreenshot\necho \"^5A screenshot has been taken at request of the server.\"\n");
820                 return;
821         }
822
823         if (time < intermission_exittime)
824                 return;
825
826         if(!mapvote_initialized)
827                 if (time < intermission_exittime + 10 && !self.button0 && !self.button2 && !self.button3 && !self.button6 && !self.buttonuse)
828                         return;
829
830         if(intermission_exittime >= 0)
831                 MapVote_Think();
832 };
833
834 /*
835 ============
836 FindIntermission
837
838 Returns the entity to view from
839 ============
840 */
841 /*
842 entity() FindIntermission =
843 {
844         local   entity spot;
845         local   float cyc;
846
847 // look for info_intermission first
848         spot = find (world, classname, "info_intermission");
849         if (spot)
850         {       // pick a random one
851                 cyc = random() * 4;
852                 while (cyc > 1)
853                 {
854                         spot = find (spot, classname, "info_intermission");
855                         if (!spot)
856                                 spot = find (spot, classname, "info_intermission");
857                         cyc = cyc - 1;
858                 }
859                 return spot;
860         }
861
862 // then look for the start position
863         spot = find (world, classname, "info_player_start");
864         if (spot)
865                 return spot;
866
867 // testinfo_player_start is only found in regioned levels
868         spot = find (world, classname, "testplayerstart");
869         if (spot)
870                 return spot;
871
872 // then look for the start position
873         spot = find (world, classname, "info_player_deathmatch");
874         if (spot)
875                 return spot;
876
877         //objerror ("FindIntermission: no spot");
878         return world;
879 };
880 */
881
882 /*
883 ===============================================================================
884
885 RULES
886
887 ===============================================================================
888 */
889
890 void() DumpStats =
891 {
892         local float file;
893         local string s;
894
895         if(cvar("_printstats"))
896                 cvar_set("_printstats", "0");
897         else if(!gameover)
898                 return;
899
900         if(gameover)
901                 s = ":scores:";
902         else
903                 s = ":status:";
904
905         s = strcat(s, GetMapname(), ":", ftos(rint(time)));
906
907         if(cvar("sv_eventlog") && gameover)
908                 GameLogEcho(s, FALSE);
909         else if(cvar("sv_logscores_console"))
910                 ServerConsoleEcho(s, FALSE);
911         if(cvar("sv_logscores_file"))
912         {
913                 file = fopen(cvar_string("sv_logscores_filename"), FILE_APPEND);
914                 fputs(file, strcat(s, "\n"));
915         }
916
917         FOR_EACH_CLIENT(other)
918         {
919                 if ((clienttype(other) == CLIENTTYPE_REAL) || (clienttype(other) == CLIENTTYPE_BOT && cvar("sv_logscores_bots")))
920                 {
921                         s = strcat(":player:", ftos(other.frags), ":");
922                         s = strcat(s, ftos(other.deaths), ":");
923                         s = strcat(s, ftos(rint(time - other.jointime)), ":");
924                         s = strcat(s, ftos(other.team), ":");
925
926                         if(cvar("sv_logscores_file"))
927                                 fputs(file, strcat(s, other.netname, "\n"));
928                         if(cvar("sv_eventlog") && gameover)
929                                 GameLogEcho(strcat(s, ftos(other.playerid), ":", other.netname), TRUE);
930                         else if(cvar("sv_logscores_console"))
931                                 ServerConsoleEcho(strcat(s, other.netname), TRUE);
932                 }
933         }
934
935         if(cvar("sv_eventlog") && gameover)
936                 GameLogEcho(":end", FALSE);
937         else if(cvar("sv_logscores_console"))
938                 ServerConsoleEcho(":end", FALSE);
939         if(cvar("sv_logscores_file"))
940         {
941                 fputs(file, ":end\n");
942                 fclose(file);
943         }
944 }
945
946 void FixIntermissionClient(entity e)
947 {
948         if(!e.autoscreenshot) // initial call
949         {
950                 e.angles = e.v_angle;
951                 e.angles_x = -e.angles_x;
952                 e.autoscreenshot = time + 0.8;  // used for autoscreenshot
953                 e.health = -2342;
954                 // first intermission phase; voting phase has positive health (used to decide whether to send SVC_FINALE or not)
955                 e.solid = SOLID_NOT;
956                 e.movetype = MOVETYPE_NONE;
957                 e.takedamage = DAMAGE_NO;
958                 if(e.weaponentity)
959                         e.weaponentity.effects = EF_NODRAW;
960                 stuffcmd(e, "\nscr_printspeed 1000000\n");
961                 if(clienttype(e) == CLIENTTYPE_REAL)
962                 {
963                         msg_entity = e;
964                         WriteByte(MSG_ONE, SVC_INTERMISSION);
965                 }
966         }
967
968         //e.velocity = '0 0 0';
969         //e.fixangle = TRUE;
970
971         // TODO halt weapon animation
972 }
973
974
975 /*
976 go to the next level for deathmatch
977 only called if a time or frag limit has expired
978 */
979 void() NextLevel =
980 {
981         float minTotalFrags;
982         float maxTotalFrags;
983         float score;
984         float f;
985
986         gameover = TRUE;
987
988         intermission_running = 1;
989
990 // enforce a wait time before allowing changelevel
991         if(player_count > 0)
992                 intermission_exittime = time + cvar("sv_mapchange_delay");
993         else
994                 intermission_exittime = -60;
995
996         WriteByte (MSG_ALL, SVC_CDTRACK);
997         WriteByte (MSG_ALL, 3);
998         WriteByte (MSG_ALL, 3);
999
1000         //pos = FindIntermission ();
1001
1002         VoteReset();
1003
1004         DumpStats();
1005
1006         if(cvar("sv_eventlog"))
1007                 GameLogEcho(":gameover", FALSE);
1008
1009         GameLogClose();
1010
1011         maxTotalFrags = 0;
1012         FOR_EACH_CLIENT(other)
1013         {
1014                 if(maxTotalFrags < other.totalfrags)
1015                         maxTotalFrags = other.totalfrags;
1016                 if(minTotalFrags > other.totalfrags)
1017                         minTotalFrags = other.totalfrags;
1018         }
1019
1020         FOR_EACH_CLIENT(other)
1021         {
1022                 FixIntermissionClient(other);
1023
1024                 self = other;
1025
1026                 if(other.winning)
1027                         bprint(other.netname, " ^7wins.\n");
1028
1029                 if(!currentbots)
1030                 {
1031                         score = (other.frags - minTotalFrags) / max(maxTotalFrags - minTotalFrags, 1);
1032                         f = bound(0, other.play_time / max(time, 1), 1);
1033                         // store some statistics?
1034                 }
1035         }
1036
1037         if(cvar("g_campaign"))
1038                 CampaignPreIntermission();
1039
1040         // WriteByte (MSG_ALL, SVC_INTERMISSION);
1041 };
1042
1043 /*
1044 ============
1045 CheckRules_Player
1046
1047 Exit deathmatch games upon conditions
1048 ============
1049 */
1050 void() CheckRules_Player =
1051 {
1052         if (gameover)   // someone else quit the game already
1053                 return;
1054
1055         if(self.deadflag == DEAD_NO)
1056                 self.play_time += frametime;
1057
1058         // fixme: don't check players; instead check dom_team and ctf_team entities
1059         //   (div0: and that in CheckRules_World please)
1060 };
1061
1062 float checkrules_oneminutewarning;
1063 float checkrules_leaderfrags;
1064 float tdm_max_score, tdm_old_score;
1065
1066 float checkrules_equality;
1067 float checkrules_overtimewarning;
1068 float checkrules_overtimeend;
1069
1070 void() InitiateOvertime =
1071 {
1072         if(!checkrules_overtimeend)
1073                 checkrules_overtimeend = time + 60 * cvar("timelimit_maxovertime");
1074 }
1075
1076 float WINNING_NO = 0; // no winner, but time limits may terminate the game
1077 float WINNING_YES = 1; // winner found
1078 float WINNING_NEVER = 2; // no winner, enter overtime if time limit is reached
1079 float WINNING_STARTOVERTIME = 3; // no winner, enter overtime NOW
1080
1081 float(float fraglimitreached, float equality) GetWinningCode =
1082 {
1083         if(equality)
1084                 if(fraglimitreached)
1085                         return WINNING_STARTOVERTIME;
1086                 else
1087                         return WINNING_NEVER;
1088         else
1089                 if(fraglimitreached)
1090                         return WINNING_YES;
1091                 else
1092                         return WINNING_NO;
1093 }
1094
1095 // set the .winning flag for exactly those players with a given field value
1096 void(.float field, float value) SetWinners =
1097 {
1098         entity head;
1099         FOR_EACH_PLAYER(head)
1100                 head.winning = (head.field == value);
1101 }
1102
1103 // set the .winning flag for those players with a given field value
1104 void(.float field, float value) AddWinners =
1105 {
1106         entity head;
1107         FOR_EACH_PLAYER(head)
1108                 if(head.field == value)
1109                         head.winning = 1;
1110 }
1111
1112 // clear the .winning flags
1113 void(void) ClearWinners =
1114 {
1115         entity head;
1116         FOR_EACH_PLAYER(head)
1117                 head.winning = 0;
1118 }
1119
1120 float() LMS_NewPlayerLives =
1121 {
1122         float fl;
1123         fl = cvar("fraglimit");
1124         if(fl == 0)
1125                 fl = 999;
1126
1127         // first player has left the game for dying too much? Nobody else can get in.
1128         if(lms_lowest_lives < 1)
1129                 return FALSE;
1130
1131         if(!cvar("g_lms_join_anytime"))
1132                 if(lms_lowest_lives < fl - cvar("g_lms_last_join"))
1133                         return FALSE;
1134
1135         return bound(1, lms_lowest_lives, fl);
1136 }
1137
1138 // LMS winning condition: game terminates if and only if there's at most one
1139 // one player who's living lives. Top two scores being equal cancels the time
1140 // limit.
1141 float() WinningCondition_LMS =
1142 {
1143         entity head;
1144         float have_player;
1145         float have_players;
1146         float l;
1147
1148         have_player = FALSE;
1149         have_players = FALSE;
1150         l = LMS_NewPlayerLives();
1151
1152         head = find(world, classname, "player");
1153         if(head)
1154                 have_player = TRUE;
1155         head = find(head, classname, "player");
1156         if(head)
1157                 have_players = TRUE;
1158
1159         if(have_player)
1160         {
1161                 // we have at least one player
1162                 if(have_players)
1163                 {
1164                         // two or more active players - continue with the game
1165                 }
1166                 else
1167                 {
1168                         // exactly one player?
1169                         if(l)
1170                         {
1171                                 // but no game has taken place yet
1172                         }
1173                         else
1174                         {
1175                                 // a winner!
1176                                 ClearWinners(); SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
1177                                 dprint("Have a winner, ending game.\n");
1178                                 return WINNING_YES;
1179                         }
1180                 }
1181         }
1182         else
1183         {
1184                 // nobody is playing at all...
1185                 if(l)
1186                 {
1187                         // wait for players...
1188                 }
1189                 else
1190                 {
1191                         // SNAFU (maybe a draw game?)
1192                         ClearWinners();
1193                         dprint("No players, ending game.\n");
1194                         return WINNING_YES;
1195                 }
1196         }
1197
1198         // When we get here, we have at least two players who are actually LIVING,
1199         // or one player who is still waiting for a victim to join the server. Now
1200         // check if the top two players have equal score.
1201
1202         checkrules_leaderfrags = 0;
1203         checkrules_equality = FALSE;
1204         FOR_EACH_PLAYER(head)
1205         {
1206                 if(head.frags > checkrules_leaderfrags)
1207                 {
1208                         checkrules_leaderfrags = head.frags;
1209                         checkrules_equality = FALSE;
1210                 }
1211                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
1212                         checkrules_equality = TRUE;
1213         }
1214
1215         SetWinners(frags, checkrules_leaderfrags);
1216
1217         // The top two players have the same amount of lives? No timelimit then,
1218         // enter overtime...
1219
1220         if(checkrules_equality)
1221                 return WINNING_NEVER;
1222
1223         // Top two have different scores? Way to go for our beloved TIMELIMIT!
1224         return WINNING_NO;
1225 }
1226
1227 // DM winning condition: game terminates if a player reached the fraglimit,
1228 // unless the first two players have the same score. The latter case also
1229 // breaks the time limit.
1230 float(float fraglimit) WinningCondition_MaxIndividualScore =
1231 {
1232         float checkrules_oldleaderfrags;
1233         entity head;
1234
1235         checkrules_oldleaderfrags = checkrules_leaderfrags;
1236         checkrules_leaderfrags = 0;
1237         checkrules_equality = FALSE;
1238         FOR_EACH_PLAYER(head)
1239         {
1240                 if(head.frags > checkrules_leaderfrags)
1241                 {
1242                         checkrules_leaderfrags = head.frags;
1243                         checkrules_equality = FALSE;
1244                 }
1245                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
1246                         checkrules_equality = TRUE;
1247         }
1248
1249         if(checkrules_leaderfrags > 0)
1250                 SetWinners(frags, checkrules_leaderfrags);
1251         else
1252                 ClearWinners();
1253
1254         if (!cvar("g_runematch"))
1255                 if (checkrules_leaderfrags != checkrules_oldleaderfrags)
1256                 {
1257                         if (checkrules_leaderfrags == fraglimit - 1)
1258                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
1259                         else if (checkrules_leaderfrags == fraglimit - 2)
1260                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
1261                         else if (checkrules_leaderfrags == fraglimit - 3)
1262                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
1263                 }
1264
1265         return GetWinningCode(fraglimit && checkrules_leaderfrags >= fraglimit, checkrules_equality);
1266 }
1267
1268 float(float fraglimit) WinningConditionBase_Teamplay =
1269 {
1270         tdm_old_score = tdm_max_score;
1271         tdm_max_score = max4(team1_score, team2_score, team3_score, team4_score);
1272
1273         checkrules_equality =
1274         (
1275                 (tdm_max_score > 0)
1276                 &&
1277                 (
1278                           (team1_score == tdm_max_score)
1279                         + (team2_score == tdm_max_score)
1280                         + (team3_score == tdm_max_score)
1281                         + (team4_score == tdm_max_score)
1282                         >= 2));
1283
1284         ClearWinners();
1285         if(tdm_max_score > 0)
1286         {
1287                 if(team1_score == tdm_max_score)
1288                         AddWinners(team, COLOR_TEAM1);
1289                 if(team2_score == tdm_max_score)
1290                         AddWinners(team, COLOR_TEAM2);
1291                 if(team3_score == tdm_max_score)
1292                         AddWinners(team, COLOR_TEAM3);
1293                 if(team4_score == tdm_max_score)
1294                         AddWinners(team, COLOR_TEAM4);
1295         }
1296
1297         if(!cvar("g_runematch") && !cvar("g_domination"))
1298                 if(tdm_max_score != tdm_old_score)
1299                 {
1300                         if(tdm_max_score == fraglimit - 1)
1301                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
1302                         else if(tdm_max_score == fraglimit - 2)
1303                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
1304                         else if(tdm_max_score == fraglimit - 3)
1305                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
1306                 }
1307
1308         return GetWinningCode(fraglimit && tdm_max_score >= fraglimit, checkrules_equality);
1309 }
1310
1311 // TDM winning condition: game terminates if a team's score sum reached the
1312 // fraglimit, unless the first two teams have the same total score. The latter
1313 // case also breaks the time limit.
1314 float(float fraglimit) WinningCondition_MaxTeamSum =
1315 {
1316         entity head;
1317
1318         team1_score = team2_score = team3_score = team4_score = 0;
1319
1320         FOR_EACH_PLAYER(head)
1321         {
1322                 if(head.team == COLOR_TEAM1)
1323                         team1_score += head.frags;
1324                 else if(head.team == COLOR_TEAM2)
1325                         team2_score += head.frags;
1326                 else if(head.team == COLOR_TEAM3)
1327                         team3_score += head.frags;
1328                 else if(head.team == COLOR_TEAM4)
1329                         team4_score += head.frags;
1330         }
1331
1332         return WinningConditionBase_Teamplay(fraglimit);
1333 }
1334
1335 // DOM/CTF winning condition: game terminates if the max of a team's players'
1336 // score reached the fraglimit, unless the first two teams have the same
1337 // maximum score. The latter case also breaks the time limit.
1338 float(float fraglimit) WinningCondition_MaxTeamMax =
1339 {
1340         entity head;
1341
1342         team1_score = team2_score = team3_score = team4_score = 0;
1343
1344         FOR_EACH_PLAYER(head)
1345         {
1346                 if(head.team == COLOR_TEAM1)
1347                 {
1348                         if(head.frags > team1_score)
1349                                 team1_score = head.frags;
1350                 }
1351                 else if(head.team == COLOR_TEAM2)
1352                 {
1353                         if(head.frags > team2_score)
1354                                 team2_score = head.frags;
1355                 }
1356                 else if(head.team == COLOR_TEAM3)
1357                 {
1358                         if(head.frags > team3_score)
1359                                 team3_score = head.frags;
1360                 }
1361                 else if(head.team == COLOR_TEAM4)
1362                 {
1363                         if(head.frags > team4_score)
1364                                 team4_score = head.frags;
1365                 }
1366         }
1367
1368         return WinningConditionBase_Teamplay(fraglimit);
1369 }
1370
1371 void print_to(entity e, string s)
1372 {
1373         if(e)
1374                 sprint(e, strcat(s, "\n"));
1375         else
1376                 ServerConsoleEcho(s, TRUE);
1377 }
1378
1379 void PrintScoreboardFor(entity e, string name, string colorcode, float whichteam)
1380 {
1381         entity head;
1382         float v;
1383         float teamvalue;
1384         float fragtotal;
1385         string s;
1386         float found;
1387         found = FALSE;
1388         teamvalue = 0;
1389         FOR_EACH_PLAYER(head)
1390         {
1391                 if(!whichteam || head.team == whichteam)
1392                 {
1393                         if(name != "")
1394                                 if(!found)
1395                                         print_to(e, strcat(" ", colorcode, name, ":"));
1396                         found = TRUE;
1397                         fragtotal = fragtotal + head.frags;
1398                         s = ftos(head.frags);
1399                         s = strcat(s, "/", ftos(head.deaths));
1400                         s = strcat(s, " @ ", ftos(head.ping));
1401                         if(clienttype(head) == CLIENTTYPE_BOT)
1402                                 s = strcat(s, "botms");
1403                         else
1404                                 s = strcat(s, "ms");
1405                         v = PlayerValue(head);
1406                         teamvalue += v;
1407                         s = strcat(s, " / ", ftos(v));
1408                         print_to(e, strcat("  ", colorcode, head.netname, colorcode, " (", s, ")"));
1409                 }
1410         }
1411         if(whichteam && found)
1412         {
1413                 s = ftos(fragtotal);
1414                 s = strcat(s, " / ", ftos(teamvalue));
1415                 print_to(e, strcat(colorcode, "  (total: ", s, ")"));
1416         }
1417 }
1418
1419 void PrintScoreboard(entity e)
1420 {
1421         print_to(e, strcat("Time:      ", ftos(time / 60)));
1422         print_to(e, strcat("Timelimit: ", ftos(cvar("timelimit"))));
1423         print_to(e, strcat("Fraglimit: ", ftos(cvar("fraglimit"))));
1424         print_to(e, "Scoreboard:");
1425         if(teams_matter)
1426         {
1427                 PrintScoreboardFor(e, "Red", "^1", COLOR_TEAM1);
1428                 PrintScoreboardFor(e, "Blue", "^4", COLOR_TEAM2);
1429                 PrintScoreboardFor(e, "Pink", "^6", COLOR_TEAM3);
1430                 PrintScoreboardFor(e, "Yellow", "^3", COLOR_TEAM4);
1431         }
1432         else
1433         {
1434                 PrintScoreboardFor(e, "", "^7", 0);
1435         }
1436         print_to(e, ".");
1437 }
1438
1439 void RemoveFromMaplist(string m)
1440 {
1441         string result;
1442         float litems;
1443         float i;
1444         float found;
1445
1446         litems = tokenize(cvar_string("g_maplist"));
1447         found = 0;
1448         result = "";
1449         for(i = 0; i < litems; ++i)
1450         {
1451                 m = strcat1(m);
1452                 result = strcat1(result);
1453                 if(argv(i) == m)
1454                         found += 1;
1455                 else
1456                         result = strcat(result, "'", argv(i), "'");
1457         }
1458         if(found)
1459                 cvar_set("g_maplist", result);
1460         ServerConsoleEcho(strcat("Removed ", ftos(found), " items."), FALSE);
1461 }
1462
1463 void AddToMaplist(string m)
1464 {
1465         string result;
1466         float found;
1467         float litems;
1468         float i;
1469         float ipos;
1470         float inserted;
1471
1472         if(!TryFile(strcat("maps/", m, ".mapcfg")))
1473         {
1474                 ServerConsoleEcho("Map not found.", FALSE);
1475                 return;
1476         }
1477
1478         litems = tokenize(cvar_string("g_maplist"));
1479         if(cvar("g_maplist_shuffle"))
1480                 ipos = ceil(random() * (litems + 1)) - 1;
1481         else
1482                 ipos = litems;
1483         found = 0;
1484         inserted = 0;
1485         for(i = 0; i < litems; ++i)
1486         {
1487                 m = strcat1(m);
1488                 if(i == ipos)
1489                 {
1490                         result = strcat(result, "'", m, "'");
1491                         inserted = 1;
1492                 }
1493                 result = strcat(result, "'", argv(i), "'");
1494                 if(argv(i) == m)
1495                         found += 1;
1496         }
1497         if(!inserted)
1498                 result = strcat(result, "'", m, "'");
1499         if(!found)
1500         {
1501                 cvar_set("g_maplist", result);
1502                 ServerConsoleEcho("Map added.", FALSE);
1503         }
1504         else
1505                 ServerConsoleEcho("Map already in list.", FALSE);
1506 }
1507
1508 void MakeFirstInMaplist(string m)
1509 {
1510         if(!TryFile(strcat("maps/", m, ".mapcfg")))
1511         {
1512                 ServerConsoleEcho("Map not found.", FALSE);
1513                 return;
1514         }
1515
1516         m = strzone(m);
1517         RemoveFromMaplist(m);
1518         cvar_set("g_maplist", strcat("'", m, "'", cvar_string("g_maplist")));
1519         strunzone(m);
1520
1521         ServerConsoleEcho("Map added as first one.", FALSE);
1522 }
1523
1524 void ShuffleMaplist()
1525 {
1526         string result;
1527         float start;
1528         float litems;
1529         float selected;
1530         float i;
1531
1532         result = cvar_string("g_maplist");
1533         litems = tokenize(result);
1534
1535         for(start = 0; start < litems - 1; ++start)
1536         {
1537                 result = "";
1538
1539                 // select a random item
1540                 selected = ceil(random() * (litems - start) + start) - 1;
1541
1542                 // shift this item to the place start
1543                 for(i = 0; i < start; ++i)
1544                         result = strcat(result, "'", argv(i), "'");
1545                 result = strcat(result, "'", argv(selected), "'");
1546                 for(i = start; i < litems; ++i)
1547                         if(i != selected)
1548                                 result = strcat(result, "'", argv(i), "'");
1549
1550                 litems = tokenize(result);
1551
1552                 //dprint(result, "\n");
1553         }
1554
1555         cvar_set("g_maplist", result);
1556 }
1557
1558 void() HandleMaplistShuffleCommands =
1559 {
1560         // automatically shuffle when setting g_maplist_shuffle
1561         if(cvar_string("_g_maplist_add") != "")
1562         {
1563                 AddToMaplist(cvar_string("_g_maplist_add"));
1564                 cvar_set("_g_maplist_add", "");
1565         }
1566         if(cvar_string("_g_maplist_remove") != "")
1567         {
1568                 RemoveFromMaplist(cvar_string("_g_maplist_remove"));
1569                 cvar_set("_g_maplist_remove", "");
1570         }
1571         if(cvar_string("_g_maplist_putfirst") != "")
1572         {
1573                 MakeFirstInMaplist(cvar_string("_g_maplist_putfirst"));
1574                 cvar_set("_g_maplist_putfirst", "");
1575         }
1576         if(cvar("_g_maplist_shufflenow") || (cvar("g_maplist_shuffle") && !cvar("_g_maplist_have_shuffled")))
1577         {
1578                 ShuffleMaplist();
1579                 cvar_set("_g_maplist_shufflenow", "0");
1580                 cvar_set("_g_maplist_have_shuffled", "1");
1581                 ServerConsoleEcho("Shuffled map list.", FALSE);
1582         }
1583         if(cvar("_g_maplist_have_shuffled"))
1584                 if(!cvar("g_maplist_shuffle"))
1585                         cvar_set("_g_maplist_have_shuffled", "0");
1586 }
1587
1588 /*
1589 ============
1590 CheckRules_World
1591
1592 Exit deathmatch games upon conditions
1593 ============
1594 */
1595 void() CheckRules_World =
1596 {
1597         local float status;
1598         local float timelimit;
1599         local float fraglimit;
1600
1601         VoteThink();
1602
1603         SetDefaultAlpha();
1604
1605         if (intermission_running)
1606                 if (time >= intermission_exittime + 60)
1607                 {
1608                         if(!DoNextMapOverride())
1609                                 GotoNextMap();
1610                         return;
1611                 }
1612
1613         if (gameover)   // someone else quit the game already
1614                 return;
1615
1616         DumpStats();
1617
1618         if(cvar("_scoreboard"))
1619         {
1620                 cvar_set("_scoreboard", "0");
1621                 PrintScoreboard(world);
1622         }
1623
1624         HandleMaplistShuffleCommands();
1625
1626         timelimit = cvar("timelimit") * 60;
1627         fraglimit = cvar("fraglimit");
1628
1629         if(checkrules_overtimeend)
1630         {
1631                 if(!checkrules_overtimewarning)
1632                 {
1633                         checkrules_overtimewarning = TRUE;
1634                         //sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1635                         bcenterprint("^3Now playing ^1OVERTIME^3!\n\n^3Keep fragging until we have a ^1winner^3!");
1636                 }
1637         }
1638         else
1639         {
1640                 if (timelimit && time >= timelimit)
1641                         InitiateOvertime();
1642         }
1643
1644         if (checkrules_overtimeend && time >= checkrules_overtimeend)
1645         {
1646                 NextLevel();
1647                 return;
1648         }
1649
1650         if (!checkrules_oneminutewarning && timelimit > 0 && time > timelimit - 60)
1651         {
1652                 checkrules_oneminutewarning = TRUE;
1653                 sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1654         }
1655
1656         status = WINNING_NO;
1657         if(cvar("g_lms"))
1658         {
1659                 status = WinningCondition_LMS();
1660         }
1661         else
1662         {
1663                 if(teams_matter)
1664                 {
1665                         if(cvar("g_tdm") || cvar("g_runematch") || cvar("g_ctf") || cvar("g_domination"))
1666                                 status = WinningCondition_MaxTeamSum(fraglimit);
1667                         //else if()
1668                         //      status = WinningCondition_MaxTeamMax(fraglimit);
1669                         else
1670                         {
1671                                 dprint("div0: How can this happen?\n");
1672                                 status = WinningCondition_MaxTeamMax(fraglimit);
1673                         }
1674                 }
1675                 else
1676                         status = WinningCondition_MaxIndividualScore(fraglimit);
1677         }
1678
1679         if(status == WINNING_STARTOVERTIME)
1680         {
1681                 status = WINNING_NEVER;
1682                 InitiateOvertime();
1683         }
1684
1685         if(status == WINNING_NEVER)
1686                 // equality cases! Nobody wins if the overtime ends in a draw.
1687                 ClearWinners();
1688
1689         if(checkrules_overtimeend)
1690                 if(status != WINNING_NEVER || time >= checkrules_overtimeend)
1691                         status = WINNING_YES;
1692
1693         if(status == WINNING_YES)
1694                 NextLevel();
1695 };
1696
1697 float randsel_value;
1698 float randsel_priority;
1699 float randsel_count;
1700 void RandSel_Init()
1701 {
1702         randsel_value = -1;
1703         randsel_priority = -1;
1704         randsel_count = -1;
1705 }
1706 void RandSel_Add(float priority, float value)
1707 {
1708         if(priority > randsel_priority)
1709         {
1710                 randsel_priority = priority;
1711                 randsel_value = value;
1712                 randsel_count = 1;
1713         }
1714         else if(priority == randsel_priority)
1715         {
1716                 randsel_count += 1;
1717                 if(ceil(random() * randsel_count) == 1)
1718                         randsel_value = value;
1719         }
1720 }
1721
1722 float mapvote_nextthink;
1723 float mapvote_initialized;
1724 float mapvote_keeptwotime;
1725 float mapvote_timeout;
1726 string mapvote_message;
1727
1728 #define MAPVOTE_COUNT 10
1729 float mapvote_count;
1730 string mapvote_maps[MAPVOTE_COUNT];
1731 float mapvote_maps_suggested[MAPVOTE_COUNT];
1732 string mapvote_suggestions[MAPVOTE_COUNT];
1733 float mapvote_suggestion_ptr;
1734 string mapvote_fillstr;
1735 float mapvote_maxlen;
1736 float mapvote_voters;
1737 float mapvote_votes[MAPVOTE_COUNT];
1738 .float mapvote;
1739
1740 void MapVote_ClearAllVotes()
1741 {
1742         FOR_EACH_CLIENT(other)
1743                 other.mapvote = 0;
1744 }
1745
1746 string MapVote_Suggest(string m)
1747 {
1748         float i;
1749         if(m == "")
1750                 return "That's not how to use this command.";
1751         if(!cvar("g_maplist_votable_suggestions"))
1752                 return "Suggestions are not accepted on this server.";
1753         if(mapvote_initialized)
1754                 return "Can't suggest - voting is already in progress!";
1755         if(!TryFile(strcat("maps/", m, ".mapcfg")))
1756                 return "The map you suggested is not available on this server.";
1757         for(i = 0; i < mapvote_suggestion_ptr; ++i)
1758                 if(mapvote_suggestions[i] == m)
1759                         return "This map was already suggested.";
1760         if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
1761         {
1762                 i = ceil(random() * mapvote_suggestion_ptr) - 1;
1763         }
1764         else
1765         {
1766                 i = mapvote_suggestion_ptr;
1767                 mapvote_suggestion_ptr += 1;
1768         }
1769         if(mapvote_suggestions[i] != "")
1770                 strunzone(mapvote_suggestions[i]);
1771         mapvote_suggestions[i] = strzone(m);
1772         GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid), ":", self.netname), TRUE);
1773         return "Suggestion accepted.";
1774 }
1775
1776 void MapVote_AddVotable(string nextMap, float isSuggestion)
1777 {
1778         float j;
1779         if(nextMap == "")
1780                 return;
1781         for(j = 0; j < mapvote_count; ++j)
1782                 if(mapvote_maps[j] == nextMap)
1783                         return;
1784         if(strlen(nextMap) > mapvote_maxlen)
1785                 mapvote_maxlen = strlen(nextMap);
1786         mapvote_maps[mapvote_count] = strzone(nextMap);
1787         mapvote_maps_suggested[mapvote_count] = isSuggestion;
1788         mapvote_count += 1;
1789 }
1790
1791 void MapVote_Init()
1792 {
1793         float i;
1794         float nmax, smax;
1795
1796         MapVote_ClearAllVotes();
1797
1798         nmax = min(MAPVOTE_COUNT, cvar("g_maplist_votable"));
1799         smax = min(nmax, cvar("g_maplist_votable_suggestions"));
1800         mapvote_count = 0;
1801
1802         for(i = 0; i < 100 && mapvote_count < smax; ++i)
1803                 MapVote_AddVotable(mapvote_suggestions[ceil(random() * mapvote_suggestion_ptr) - 1], TRUE);
1804
1805         for(i = 0; i < 100 && mapvote_count < nmax; ++i)
1806                 MapVote_AddVotable(GetNextMap(), FALSE);
1807
1808         if(mapvote_count == 0)
1809         {
1810                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );
1811                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
1812                 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
1813                         MapVote_AddVotable(GetNextMap(), FALSE);
1814         }
1815
1816         //dprint("mapvote count is ", ftos(mapvote_count), "\n");
1817
1818         mapvote_fillstr = " ";
1819         while(strlen(mapvote_fillstr) < mapvote_maxlen + 16)
1820                 mapvote_fillstr = strcat(mapvote_fillstr, mapvote_fillstr);
1821         mapvote_fillstr = strzone(mapvote_fillstr);
1822
1823         mapvote_keeptwotime = time + cvar("g_maplist_votable_keeptwotime");
1824         mapvote_timeout = time + cvar("g_maplist_votable_timeout");
1825         if(mapvote_count < 3 || mapvote_keeptwotime <= time)
1826                 mapvote_keeptwotime = 0;
1827         mapvote_message = "Choose a map and press its key!";
1828 }
1829 float MapVote_Finished(float mappos)
1830 {
1831         string result;
1832         float i;
1833
1834         result = strcat(":vote:finished:", mapvote_maps[mappos]);
1835         result = strcat(result, ":", ftos(mapvote_votes[mappos]), "::");
1836         for(i = 0; i < mapvote_count; ++i)
1837                 if(i != mappos)
1838                         if(mapvote_maps[i] != "")
1839                         {
1840                                 result = strcat(result, ":", mapvote_maps[i]);
1841                                 result = strcat(result, ":", ftos(mapvote_votes[i]));
1842                         }
1843         GameLogEcho(result, FALSE);
1844         if(mapvote_maps_suggested[mappos])
1845                 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]), FALSE);
1846
1847         FOR_EACH_REALCLIENT(other)
1848                 FixClientCvars(other);
1849
1850         Map_Goto_SetStr(mapvote_maps[mappos]);
1851         Map_Goto();
1852         return TRUE;
1853 }
1854 void MapVote_CheckRules_1()
1855 {
1856         float i;
1857
1858         for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")
1859         {
1860                 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
1861                 mapvote_votes[i] = 0;
1862         }
1863
1864         mapvote_voters = 0;
1865         FOR_EACH_REALCLIENT(other)
1866         {
1867                 ++mapvote_voters;
1868                 if(other.mapvote)
1869                 {
1870                         i = other.mapvote - 1;
1871                         //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
1872                         mapvote_votes[i] = mapvote_votes[i] + 1;
1873                 }
1874         }
1875 }
1876
1877 float MapVote_CheckRules_2()
1878 {
1879         float i;
1880         float firstPlace, secondPlace;
1881         float firstPlaceVotes, secondPlaceVotes;
1882         string result;
1883
1884         RandSel_Init();
1885         for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")
1886                 RandSel_Add(mapvote_votes[i], i);
1887         firstPlace = randsel_value;
1888         firstPlaceVotes = randsel_priority;
1889         //dprint("First place: ", ftos(firstPlace), "\n");
1890         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
1891
1892         RandSel_Init();
1893         for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")
1894                 if(i != firstPlace)
1895                         RandSel_Add(mapvote_votes[i], i);
1896         secondPlace = randsel_value;
1897         secondPlaceVotes = randsel_priority;
1898         //dprint("Second place: ", ftos(secondPlace), "\n");
1899         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
1900
1901         if(firstPlace == -1)
1902                 error("No first place in map vote... WTF?");
1903
1904         if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters - firstPlaceVotes) < firstPlaceVotes)
1905                 return MapVote_Finished(firstPlace);
1906
1907         if(mapvote_keeptwotime)
1908                 if(time > mapvote_keeptwotime || (mapvote_voters - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
1909                 {
1910                         mapvote_message = "Now decide between the TOP TWO!";
1911                         mapvote_keeptwotime = 0;
1912                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
1913                         result = strcat(result, ":", ftos(firstPlaceVotes));
1914                         result = strcat(result, ":", mapvote_maps[secondPlace]);
1915                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");
1916                         for(i = 0; i < mapvote_count; ++i)
1917                                 if(i != firstPlace)
1918                                         if(i != secondPlace)
1919                                                 if(mapvote_maps[i] != "")
1920                                                 {
1921                                                         result = strcat(result, ":", mapvote_maps[i]);
1922                                                         result = strcat(result, ":", ftos(mapvote_votes[i]));
1923                                                         strunzone(mapvote_maps[i]);
1924                                                         mapvote_maps[i] = "";
1925                                                 }
1926                         GameLogEcho(result, FALSE);
1927                 }
1928
1929         return FALSE;
1930 }
1931 void MapVote_Tick()
1932 {
1933         string msgstr;
1934         string tmp;
1935         float i;
1936         float keeptwo;
1937
1938         keeptwo = mapvote_keeptwotime;
1939         MapVote_CheckRules_1(); // count
1940         if(MapVote_CheckRules_2()) // decide
1941                 return;
1942
1943         FOR_EACH_REALCLIENT(other)
1944         {
1945                 // hide scoreboard again
1946                 if(other.health != 2342)
1947                 {
1948                         other.health = 2342;
1949                         other.impulse = 0;
1950                         if(clienttype(other) == CLIENTTYPE_REAL)
1951                         {
1952                                 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");
1953                                 msg_entity = other;
1954                                 WriteByte(MSG_ONE, SVC_FINALE);
1955                                 WriteString(MSG_ONE, "");
1956                         }
1957                 }
1958
1959                 // notify about keep-two
1960                 if(keeptwo != 0 && mapvote_keeptwotime == 0)
1961                         stuffcmd(other, "\nplay2 misc/invshot.wav\n");
1962
1963                 // clear possibly invalid votes
1964                 if(mapvote_maps[other.mapvote - 1] == "")
1965                         other.mapvote = 0;
1966                 // use impulses as new vote
1967                 if(other.impulse >= 1 && other.impulse <= mapvote_count)
1968                         if(mapvote_maps[other.impulse - 1] != "")
1969                                 other.mapvote = other.impulse;
1970                 other.impulse = 0;
1971         }
1972
1973         MapVote_CheckRules_1(); // just count
1974
1975         FOR_EACH_REALCLIENT(other)
1976         {
1977                 // display voting screen
1978                 msgstr = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
1979                 msgstr = substring(msgstr, 0, strlen(msgstr) - mapvote_count);
1980                 msgstr = strcat(msgstr, mapvote_message);
1981                 msgstr = strcat(msgstr, "\n\n");
1982                 for(i = 0; i < mapvote_count; ++i)
1983                         if(mapvote_maps[i] == "")
1984                                 msgstr = strcat(msgstr, "\n");
1985                         else
1986                         {
1987                                 tmp = mapvote_maps[i];
1988                                 tmp = strcat(tmp, substring(mapvote_fillstr, 0, mapvote_maxlen  - strlen(tmp)));
1989                                 tmp = strcat(ftos(math_mod(i + 1, 10)), ": ", tmp);
1990                                 tmp = strcat(tmp, " ^2(", ftos(mapvote_votes[i]), " vote");
1991                                 if(mapvote_votes[i] != 1)
1992                                         tmp = strcat(tmp, "s");
1993                                 tmp = strcat(tmp, ")");
1994                                 tmp = strcat(tmp, substring(mapvote_fillstr, 0, mapvote_maxlen + 15 - strlen(tmp)));
1995                                 if(other.mapvote == i + 1)
1996                                         msgstr = strcat(msgstr, "^3> ", tmp, "\n");
1997                                 else
1998                                         msgstr = strcat(msgstr, "^7  ", tmp, "\n");
1999                         }
2000                 i = ceil(mapvote_timeout - time);
2001                 msgstr = strcat(msgstr, "\n\n", ftos(i), " second");
2002                 if(i != 1)
2003                         msgstr = strcat(msgstr, "s");
2004                 msgstr = strcat(msgstr, " left");
2005
2006                 centerprint_atprio(other, CENTERPRIO_MAPVOTE, msgstr);
2007         }
2008 }
2009 void MapVote_Think()
2010 {
2011         if(alreadychangedlevel)
2012                 return;
2013         
2014         if(time < mapvote_nextthink)
2015                 return;
2016         //dprint("tick\n");
2017
2018         mapvote_nextthink = time + 0.5;
2019
2020         if(!mapvote_initialized)
2021         {
2022                 mapvote_initialized = TRUE;
2023                 if(DoNextMapOverride())
2024                 {
2025                         alreadychangedlevel = TRUE;
2026                         return;
2027                 }
2028                 if(!cvar("g_maplist_votable"))
2029                 {
2030                         GotoNextMap();
2031                         return;
2032                 }
2033                 MapVote_Init();
2034         }
2035
2036         MapVote_Tick();
2037 };
2038
2039
2040
2041 float beamindex_modulo;
2042 void EndFrame()
2043 {
2044         entity e;
2045         string value;
2046         vector mi, ma;
2047         float i;
2048
2049         value = cvar_string("_entityboxes_for");
2050         if(value == "")
2051                 return;
2052
2053         i = beamindex_modulo;
2054         beamindex_modulo += 1;
2055         if(beamindex_modulo >= 32)
2056                 beamindex_modulo = 0;
2057         for(e = world; (e = find(e, classname, value)) != world; )
2058         {
2059                 mi = e.absmin;
2060                 ma = e.absmax;
2061                 i = i + 1;
2062                 if(i >= 32)
2063                 {
2064                         i = 0;
2065                         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');
2066                         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');
2067                         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');
2068                         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');
2069                         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');
2070                         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');
2071                         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');
2072                         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');
2073                         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');
2074                         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');
2075                         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');
2076                         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');
2077                 }
2078         }
2079 }