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