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