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