remove g_ctf_win_mode
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapinfo.qc
1 // generic string stuff
2
3 float _MapInfo_Cache_Active;
4 float _MapInfo_Cache_DB_NameToIndex;
5 float _MapInfo_Cache_Buf_IndexToMapData;
6
7 void MapInfo_Cache_Destroy()
8 {
9         if(!_MapInfo_Cache_Active)
10                 return;
11
12         db_close(_MapInfo_Cache_DB_NameToIndex);
13         buf_del(_MapInfo_Cache_Buf_IndexToMapData);
14         _MapInfo_Cache_Active = 0;
15 }
16
17 void MapInfo_Cache_Create()
18 {
19         MapInfo_Cache_Destroy();
20         _MapInfo_Cache_DB_NameToIndex = db_create();
21         _MapInfo_Cache_Buf_IndexToMapData = buf_create();
22         _MapInfo_Cache_Active = 1;
23 }
24
25 void MapInfo_Cache_Invalidate()
26 {
27         if(!_MapInfo_Cache_Active)
28                 return;
29
30         MapInfo_Cache_Create();
31 }
32
33 void MapInfo_Cache_Store()
34 {
35         float i;
36         string s;
37         if(!_MapInfo_Cache_Active)
38                 return;
39
40         s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
41         if(!s) // empty string is NOT valid here!
42         {
43                 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
44                 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
45         }
46         else
47                 i = stof(s);
48
49         // now store all the stuff
50         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData,   i, MapInfo_Map_bspname);
51         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
52         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
53         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
54         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
55         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
56         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
57         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
58 }
59
60 float MapInfo_Cache_Retrieve(string map)
61 {
62         float i;
63         string s;
64         if(!_MapInfo_Cache_Active)
65                 return 0;
66
67         s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
68         if(!s)
69                 return 0;
70         i = stof(s);
71
72         // now retrieve all the stuff
73         MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
74         MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
75         MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
76         MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
77         MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
78         MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
79         MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
80         MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
81
82         return 1;
83 }
84
85 // GLOB HANDLING (for all BSP files)
86 float _MapInfo_globopen;
87 float _MapInfo_globcount; 
88 float _MapInfo_globhandle;
89 string _MapInfo_GlobItem(float i)
90 {
91         string s;
92         s = search_getfilename(_MapInfo_globhandle, i);
93         return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
94 }
95
96 void MapInfo_Enumerate()
97 {
98         if(_MapInfo_globopen)
99                 search_end(_MapInfo_globhandle);
100         MapInfo_Cache_Invalidate();
101         _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
102         _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
103         _MapInfo_globopen = 1;
104 }
105
106 // filter the info by game type mask (updates MapInfo_count)
107 //
108 float _MapInfo_filtered;
109 float _MapInfo_filtered_allocated;
110 float MapInfo_FilterList_Lookup(float i)
111 {
112         return stof(bufstr_get(_MapInfo_filtered, i));
113 }
114
115 void _MapInfo_FilterList_swap(float i, float j, entity pass)
116 {
117         string h;
118         h = bufstr_get(_MapInfo_filtered, i);
119         bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
120         bufstr_set(_MapInfo_filtered, j, h);
121 }
122
123 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
124 {
125         string a, b;
126         a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
127         b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
128         return strcasecmp(a, b);
129 }
130
131 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
132 {
133         float i, j;
134         if not(_MapInfo_filtered_allocated)
135         {
136                 _MapInfo_filtered_allocated = 1;
137                 _MapInfo_filtered = buf_create();
138         }
139         MapInfo_count = 0;
140         for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
141         {
142                 if(MapInfo_Get_ByName(_MapInfo_GlobItem(i), 1, 0) == 2) // if we generated one... BAIL OUT and let the caller continue in the next frame.
143                         if(pAbortOnGenerate)
144                         {
145                                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
146                                 MapInfo_progress = i / _MapInfo_globcount;
147                                 return 0;
148                         }
149                 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
150                 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
151                 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
152                 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
153                         bufstr_set(_MapInfo_filtered, ++j, ftos(i));
154         }
155         MapInfo_count = j + 1;
156         MapInfo_ClearTemps();
157         
158         // sometimes the glob isn't sorted nicely, so fix it here...
159         heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
160
161         return 1;
162 }
163
164 void MapInfo_Filter_Free()
165 {
166         if(_MapInfo_filtered_allocated)
167         {
168                 buf_del(_MapInfo_filtered);
169                 _MapInfo_filtered_allocated = 0;
170         }
171 }
172
173 // load info about the i-th map into the MapInfo_Map_* globals
174 string MapInfo_BSPName_ByID(float i)
175 {
176         return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
177 }
178
179 string unquote(string s)
180 {
181         float i, j, l;
182         l = strlen(s);
183         j = -1;
184         for(i = 0; i < l; ++i)
185         {
186                 string ch;
187                 ch = substring(s, i, 1);
188                 if(ch != " ") if(ch != "\"")
189                 {
190                         for(j = strlen(s) - i - 1; j > 0; --j)
191                         {
192                                 ch = substring(s, i+j, 1);
193                                 if(ch != " ") if(ch != "\"")
194                                         return substring(s, i, j+1);
195                         }
196                         return substring(s, i, 1);
197                 }
198         }
199         return "";
200 }
201
202 float MapInfo_Get_ByID(float i)
203 {
204         if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
205                 return 1;
206         return 0;
207 }
208
209 string _MapInfo_Map_worldspawn_music;
210
211 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
212 {
213         string fn;
214         float fh;
215         string s, k, v;
216         vector o;
217         float i;
218         float inWorldspawn;
219         float r;
220         float twoBaseModes;
221         float diameter, spawnpoints;
222         float spawnplaces;
223
224         vector mapMins, mapMaxs;
225
226         r = 1;
227         fn = strcat("maps/", pFilename, ".ent");
228         fh = fopen(fn, FILE_READ);
229         if(fh < 0)
230         {
231                 r = 2;
232                 fn = strcat("maps/", pFilename, ".bsp");
233                 fh = fopen(fn, FILE_READ);
234         }
235         if(fh < 0)
236                 return 0;
237         print("Analyzing ", fn, " to generate initial mapinfo\n");
238
239         inWorldspawn = 2;
240         MapInfo_Map_flags = 0;
241         MapInfo_Map_supportedGametypes = 0;
242         spawnpoints = 0;
243         spawnplaces = 0;
244         _MapInfo_Map_worldspawn_music = "";
245
246         for(;;)
247         {
248                 if not((s = fgets(fh)))
249                         break;
250                 if(inWorldspawn == 1)
251                         if(startsWith(s, "}"))
252                                 inWorldspawn = 0;
253                 k = unquote(car(s));
254                 v = unquote(cdr(s));
255                 if(inWorldspawn)
256                 {
257                         if(k == "classname" && v == "worldspawn")
258                                 inWorldspawn = 1;
259                         else if(k == "author")
260                                 MapInfo_Map_author = v;
261                         else if(k == "_description")
262                                 MapInfo_Map_description = v;
263                         else if(k == "music")
264                                 _MapInfo_Map_worldspawn_music = v;
265                         else if(k == "noise")
266                                 _MapInfo_Map_worldspawn_music = v;
267                         else if(k == "message")
268                         {
269                                 i = strstrofs(v, " by ", 0);
270                                 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
271                                 {
272                                         MapInfo_Map_title = substring(v, 0, i);
273                                         MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
274                                 }
275                                 else
276                                         MapInfo_Map_title = v;
277                         }
278                 }
279                 else
280                 {
281                         if(k == "origin")
282                         {
283                                 o = stov(strcat("'", v, "'"));
284                                 mapMins_x = min(mapMins_x, o_x);
285                                 mapMins_y = min(mapMins_y, o_y);
286                                 mapMins_z = min(mapMins_z, o_z);
287                                 mapMaxs_x = max(mapMaxs_x, o_x);
288                                 mapMaxs_y = max(mapMaxs_y, o_y);
289                                 mapMaxs_z = max(mapMaxs_z, o_z);
290                         }
291                         else if(k == "race_place")
292                         {
293                                 if(stof(v) > 0)
294                                         spawnplaces = 1;
295                         }
296                         else if(k == "classname")
297                         {
298                                 if(v == "dom_controlpoint")
299                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
300                                 else if(v == "item_flag_team2")
301                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
302                                 else if(v == "team_CTF_blueflag")
303                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
304                                 else if(v == "runematch_spawn_point")
305                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
306                                 else if(v == "target_assault_roundend")
307                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
308                                 else if(v == "onslaught_generator")
309                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
310                                 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
311                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
312                                 else if(v == "info_player_team1")
313                                         ++spawnpoints;
314                                 else if(v == "info_player_team2")
315                                         ++spawnpoints;
316                                 else if(v == "info_player_start")
317                                         ++spawnpoints;
318                                 else if(v == "info_player_deathmatch")
319                                         ++spawnpoints;
320                                 else if(v == "trigger_race_checkpoint")
321                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
322                                 else if(v == "target_startTimer")
323                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
324                                 else if(v == "weapon_nex")
325                                         { }
326                                 else if(v == "weapon_railgun")
327                                         { }
328                                 else if(startsWith(v, "weapon_"))
329                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
330                                 else if(startsWith(v, "turret_"))
331                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
332                                 else if(startsWith(v, "vehicle_"))
333                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
334                                 else if(v == "target_music" || v == "trigger_music")
335                                         _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
336                         }
337                 }
338         }
339         if(inWorldspawn)
340         {
341                 print(fn, " ended still in worldspawn, BUG\n");
342                 return 0;
343         }
344         diameter = vlen(mapMaxs - mapMins);
345
346         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
347         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
348         {
349                 // we have a CTF-only or Assault-only map. Don't add other modes then,
350                 // as the map is too symmetric for them.
351         }
352         else
353         {
354                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
355                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;       // Rune always works
356                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
357                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY;                // Keepaway always works
358
359                 if(spawnpoints >= 8  && diameter > 4096) {
360                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
361                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
362                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
363                 }
364                 if(                     diameter < 4096)
365                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
366                 if(spawnpoints >= 12 && diameter > 5120)
367                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
368         }
369
370         if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
371         if(!spawnplaces)
372         {
373                 MapInfo_Map_supportedGametypes &~= MAPINFO_TYPE_RACE;
374                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
375         }
376
377         dprint("-> diameter ",    ftos(diameter));
378         dprint(";  spawnpoints ", ftos(spawnpoints));
379         dprint(";  modes ",       ftos(MapInfo_Map_supportedGametypes), "\n");
380
381         fclose(fh);
382
383         return r;
384 }
385
386 void _MapInfo_Map_Reset()
387 {
388         MapInfo_Map_title = "<TITLE>";
389         MapInfo_Map_titlestring = "<TITLE>";
390         MapInfo_Map_description = "<DESCRIPTION>";
391         MapInfo_Map_author = "<AUTHOR>";
392         MapInfo_Map_supportedGametypes = 0;
393         MapInfo_Map_supportedFeatures = 0;
394         MapInfo_Map_flags = 0;
395         MapInfo_Map_clientstuff = "";
396         MapInfo_Map_fog = "";
397         MapInfo_Map_mins = '0 0 0';
398         MapInfo_Map_maxs = '0 0 0';
399 }
400
401 string _MapInfo_GetDefault(float t)
402 {
403         switch(t)
404         {
405                 case MAPINFO_TYPE_DEATHMATCH:      return "30 20 0";
406                 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
407                 case MAPINFO_TYPE_DOMINATION:      return "200 20 0";
408                 case MAPINFO_TYPE_CTF:             return "300 20 10 0";
409                 case MAPINFO_TYPE_RUNEMATCH:       return "200 20 0";
410                 case MAPINFO_TYPE_LMS:             return "9 20 0";
411                 case MAPINFO_TYPE_ARENA:           return "10 20 0";
412                 case MAPINFO_TYPE_CA:              return "10 20 0";
413                 case MAPINFO_TYPE_KEYHUNT:         return "1000 20 3 0";
414                 case MAPINFO_TYPE_ASSAULT:         return "20 0";
415                 case MAPINFO_TYPE_RACE:            return "20 5 7 15 0";
416                 case MAPINFO_TYPE_ONSLAUGHT:       return "20 0";
417                 case MAPINFO_TYPE_NEXBALL:         return "5 20 0";
418                 case MAPINFO_TYPE_CTS:             return "20 0 0";
419                 case MAPINFO_TYPE_FREEZETAG:       return "10 20 0";
420                 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
421                 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
422                 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
423                 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
424                 default:                           return "";
425         }
426 }
427
428 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, float load_default)
429 {
430         string sa;
431         MapInfo_Map_supportedGametypes |= pThisType;
432         if(!(pThisType & pWantedType))
433                 return;
434
435         if(load_default)
436                 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, FALSE);
437         
438         if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
439         {
440                 cvar_set("fraglimit", "0");
441         }
442         else
443         {
444                 sa = car(s);
445                 if(sa != "")
446                         cvar_set("fraglimit", sa);
447                 s = cdr(s);
448         }
449
450         sa = car(s);
451         if(sa != "")
452                 cvar_set("timelimit", sa);
453         s = cdr(s);
454
455         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
456         {
457                 sa = car(s);
458                 if(sa != "")
459                         cvar_set("g_tdm_teams", sa);
460                 s = cdr(s);
461         }
462
463         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
464         {
465                 sa = car(s);
466                 if(sa != "")
467                         cvar_set("g_keyhunt_teams", sa);
468                 s = cdr(s);
469         }
470
471         if(pWantedType == MAPINFO_TYPE_CTF)
472         {
473                 sa = car(s);
474                 if(sa != "")
475                         cvar_set("fraglimit", sa);
476                 s = cdr(s);
477         }
478
479         /* keepaway wuz here
480         if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
481         {
482                 sa = car(s);
483                 if(sa != "")
484                         cvar_set("fraglimit", sa);
485                 s = cdr(s);
486         }
487         */
488         
489         // rc = timelimit timelimit_qualification laps laps_teamplay
490         if(pWantedType == MAPINFO_TYPE_RACE)
491         {
492                 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
493                 cvar_set("g_race_qualifying_timelimit", sa);
494                 s = cdr(s);
495
496                 sa = car(s);
497                 if(sa != "")
498                         if(cvar("g_race_teams") < 2)
499                                 cvar_set("fraglimit", sa);
500                 s = cdr(s);
501
502                 sa = car(s);
503                 if(sa != "")
504                         if(cvar("g_race_teams") >= 2)
505                                 cvar_set("fraglimit", sa);
506                 s = cdr(s);
507         }
508
509         if(pWantedType == MAPINFO_TYPE_CTS)
510         {
511                 sa = car(s);
512
513                 // this is the skill of the map
514                 // not parsed by anything yet
515                 // for map databases
516                 //if(sa != "")
517                 //      cvar_set("fraglimit", sa);
518
519                 s = cdr(s);
520         }
521
522         if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
523         {
524                 cvar_set("leadlimit", "0");
525         }
526         else
527         {
528                 sa = car(s);
529                 if(sa != "")
530                         cvar_set("leadlimit", sa);
531                 s = cdr(s);
532         }
533 }
534
535 string _MapInfo_GetDefaultEx(float t)
536 {
537         entity e;
538         for(e = MapInfo_Type_first; e; e = e.enemy)
539                 if(t == e.weapons)
540                         return e.model2;
541         return "";
542 }
543
544 void _MapInfo_Map_ApplyGametypeEx(string s, float pWantedType, float pThisType)
545 {
546         string sa, k, v;
547         float p;
548         string fraglimit_normal;
549         string fraglimit_teams;
550
551         MapInfo_Map_supportedGametypes |= pThisType;
552         if(!(pThisType & pWantedType))
553                 return;
554
555         // reset all the cvars to their defaults
556
557         cvar_set("timelimit", cvar_defstring("timelimit"));
558         cvar_set("leadlimit", cvar_defstring("leadlimit"));
559         cvar_set("fraglimit", cvar_defstring("fraglimit"));
560         cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
561         cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
562         cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
563         cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
564
565         fraglimit_normal = string_null;
566         fraglimit_teams = string_null;
567
568         s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
569         while(s != "")
570         {
571                 sa = car(s);
572                 s = cdr(s);
573
574                 if(sa == "")
575                         continue;
576
577                 p = strstrofs(sa, "=", 0);
578                 if(p < 0)
579                 {
580                         k = "timelimit";
581                         v = s;
582                 }
583                 else
584                 {
585                         k = substring(sa, 0, p);
586                         v = substring(sa, p+1, -1);
587                 }
588
589                 if(k == "timelimit")
590                 {
591                         cvar_set("timelimit", v);
592                 }
593                 else if(k == "leadlimit")
594                 {
595                         cvar_set("leadlimit", v);
596                 }
597                 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit" || k == "caplimit")
598                 {
599                         fraglimit_normal = v;
600                 }
601                 else if(k == "teampointlimit" || k == "teamlaplimit")
602                 {
603                         fraglimit_teams = v;
604                 }
605                 else if(k == "teams")
606                 {
607                         cvar_set("g_tdm_teams", v);
608                         cvar_set("g_keyhunt_teams", v);
609                         cvar_set("g_domination_default_teams", v);
610                 }
611                 else if(k == "qualifying_timelimit")
612                 {
613                         cvar_set("g_race_qualifying_timelimit", v);
614                 }
615                 else if(k == "skill")
616                 {
617                         // ignore
618                 }
619                 else
620                 {
621                         print("Invalid gametype key in mapinfo: ", k, "\n");
622                 }
623         }
624
625         if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
626         {
627                 if(fraglimit_teams)
628                         cvar_set("fraglimit", fraglimit_teams);
629         }
630         else
631         {
632                 if(fraglimit_normal)
633                         cvar_set("fraglimit", fraglimit_normal);
634         }
635 }
636
637 float MapInfo_Type_FromString(string t)
638 {
639         entity e;
640         if(t == "all")
641                 return MAPINFO_TYPE_ALL;
642         for(e = MapInfo_Type_first; e; e = e.enemy)
643                 if(t == e.mdl)
644                         return e.weapons;
645         return 0;
646 }
647
648 string MapInfo_Type_ToString(float t)
649 {
650         entity e;
651         if(t == MAPINFO_TYPE_ALL)     
652                 return "all";
653         for(e = MapInfo_Type_first; e; e = e.enemy)
654                 if(t == e.weapons)
655                         return e.mdl;
656         return "";
657 }
658
659 string MapInfo_Type_ToText(float t)
660 {
661         entity e;
662         for(e = MapInfo_Type_first; e; e = e.enemy)
663                 if(t == e.weapons)
664                         return e.message;
665         return _("@!#%'n Tuba Throwing");
666 }
667
668 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
669 {
670         string t;
671         float fh, o;
672         t = car(s); s = cdr(s);
673
674         // limited support of "" and comments
675         //   remove trailing and leading " of t
676         if(substring(t, 0, 1) == "\"")
677         {
678                 if(substring(t, -1, 1) == "\"")
679                         t = substring(t, 1, -2);
680         }
681
682         //   remove leading " of s
683         if(substring(s, 0, 1) == "\"")
684         {
685                 s = substring(s, 1, -1);
686         }
687         //   remove trailing " of s, and all that follows (cvar description)
688         o = strstrofs(s, "\"", 0);
689         if(o >= 0)
690                 s = substring(s, 0, o);
691         
692         //   remove // comments
693         o = strstrofs(s, "//", 0);
694         if(o >= 0)
695                 s = substring(s, 0, o);
696         
697         //   remove trailing spaces
698         while(substring(s, -1, 1) == " ")
699                 s = substring(s, 0, -2);
700
701         if(t == "#include")
702         {
703                 if(recurse > 0)
704                 {
705                         fh = fopen(s, FILE_READ);
706                         if(fh < 0)
707                                 print("Map ", pFilename, " references not existing config file ", s, "\n");
708                         else
709                         {
710                                 for(;;)
711                                 {
712                                         if not((s = fgets(fh)))
713                                                 break;
714
715                                         // catch different sorts of comments
716                                         if(s == "")                    // empty lines
717                                                 continue;
718                                         if(substring(s, 0, 1) == "#")  // UNIX style
719                                                 continue;
720                                         if(substring(s, 0, 2) == "//") // C++ style
721                                                 continue;
722                                         if(substring(s, 0, 1) == "_")  // q3map style
723                                                 continue;
724
725                                         if(substring(s, 0, 4) == "set ")
726                                                 s = substring(s, 4, -1);
727                                         if(substring(s, 0, 5) == "seta ")
728                                                 s = substring(s, 5, -1);
729
730                                         _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
731                                 }
732                                 fclose(fh);
733                         }
734                 }
735                 else
736                         print("Map ", pFilename, " uses too many levels of inclusion\n");
737         }
738         else if(t == "")
739                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
740         else if not(cvar_value_issafe(t))
741                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
742         else if not (cvar_value_issafe(s))
743                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
744         else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
745                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
746         else if(matchacl(acl, t) <= 0)
747                 print("Map ", pFilename, " contains a denied setting, ignored\n");
748         else
749         {
750                 if(type == 0) // server set
751                 {
752                         dprint("Applying temporary setting ", t, " := ", s, "\n");
753                         if(cvar("g_campaign"))
754                                 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
755                         else
756                                 cvar_settemp(t, s);
757                 }
758                 else
759                 {
760                         dprint("Applying temporary client setting ", t, " := ", s, "\n");
761                         MapInfo_Map_clientstuff = strcat(
762                                         MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
763                                         );
764                 }
765         }
766 }
767
768 float MapInfo_isRedundant(string fn, string t)
769 {
770         // normalize file name
771         fn = strreplace("_", "-", fn);
772
773         // normalize visible title
774         t = strreplace(": ", "-", t);
775         t = strreplace(":", "-", t);
776         t = strreplace(" ", "-", t);
777         t = strreplace("_", "-", t);
778         t = strreplace("'", "-", t);
779
780         if(!strcasecmp(fn, t))
781                 return TRUE;
782
783         // we allow the visible title to have punctuation the file name does
784         // not, but not vice versa
785         t = strreplace("-", "", t);
786
787         if(!strcasecmp(fn, t))
788                 return TRUE;
789
790         return FALSE;
791 }
792
793 // load info about a map by name into the MapInfo_Map_* globals
794 float MapInfo_Get_ByName_NoFallbacks(string pFilename, float pAllowGenerate, float pGametypeToSet)
795 {
796         string fn;
797         string s, t;
798         float fh;
799         float r, f, n, i, p;
800         string acl;
801
802         acl = MAPINFO_SETTEMP_ACL_USER;
803
804         if(strstrofs(pFilename, "/", 0) >= 0)
805         {
806                 print("Invalid character in map name, ignored\n");
807                 return 0;
808         }
809
810         if(pGametypeToSet == 0)
811                 if(MapInfo_Cache_Retrieve(pFilename))
812                         return 1;
813
814         r = 1;
815
816         MapInfo_Map_bspname = pFilename;
817
818         // default all generic fields so they have "good" values in case something fails
819         fn = strcat("maps/", pFilename, ".mapinfo");
820         fh = fopen(fn, FILE_READ);
821         if(fh < 0)
822         {
823                 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
824                 fh = fopen(fn, FILE_READ);
825                 if(fh < 0)
826                 {
827                         if(!pAllowGenerate)
828                                 return 0;
829                         _MapInfo_Map_Reset();
830                         r = _MapInfo_Generate(pFilename);
831                         if(!r)
832                                 return 0;
833                         fh = fopen(fn, FILE_WRITE);
834                         fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
835                         fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
836                         fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
837                         if(_MapInfo_Map_worldspawn_music != "")
838                         {
839                                 if(
840                                         substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
841                                         ||
842                                         substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
843                                 )
844                                         fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
845                                 else
846                                         fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
847                         }
848                         else
849                         {
850                                 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
851                                 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
852                                 for(;;)
853                                 {
854                                         i = floor(random() * n);
855                                         if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
856                                                 break;
857                                 }
858                                 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
859                         }
860                         if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
861                                 fputs(fh, "has weapons\n");
862                         else
863                                 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
864                         if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
865                                 fputs(fh, "has turrets\n");
866                         else
867                                 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
868                         if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
869                                 fputs(fh, "has weapons\n");
870                         else
871                                 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
872                         if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
873                                 fputs(fh, "frustrating\n");
874
875                         for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
876                                 if(MapInfo_Map_supportedGametypes & i)
877                                         fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
878
879                         if(fexists(strcat("scripts/", pFilename, ".arena")))
880                                 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
881
882                         fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
883                         fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
884                         fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
885                         fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
886                         fputs(fh, "// optional: hidden\n");
887
888                         fclose(fh);
889                         r = 2;
890                         // return r;
891                         fh = fopen(fn, FILE_READ);
892                         if(fh < 0)
893                                 error("... but I just wrote it!");
894                 }
895
896                 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
897         }
898
899         _MapInfo_Map_Reset();
900         for(;;)
901         {
902                 if not((s = fgets(fh)))
903                         break;
904
905                 // catch different sorts of comments
906                 if(s == "")                    // empty lines
907                         continue;
908                 if(substring(s, 0, 1) == "#")  // UNIX style
909                         continue;
910                 if(substring(s, 0, 2) == "//") // C++ style
911                         continue;
912                 if(substring(s, 0, 1) == "_")  // q3map style
913                         continue;
914
915                 p = strstrofs(s, "//", 0);
916                 if(p >= 0)
917                         s = substring(s, 0, p);
918
919                 t = car(s); s = cdr(s);
920                 if(t == "title")
921                         MapInfo_Map_title = s;
922                 else if(t == "description")
923                         MapInfo_Map_description = s;
924                 else if(t == "author")
925                         MapInfo_Map_author = s;
926                 else if(t == "has")
927                 {
928                         t = car(s); s = cdr(s);
929                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
930                         else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
931                         else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
932                         else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
933                         else
934                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
935                 }
936                 else if(t == "hidden")
937                 {
938                         MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
939                 }
940                 else if(t == "forbidden")
941                 {
942                         MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
943                 }
944                 else if(t == "frustrating")
945                 {
946                         MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
947                 }
948                 else if(t == "type")
949                 {
950                         t = car(s); s = cdr(s);
951                         f = MapInfo_Type_FromString(t);
952                         print("Map ", pFilename, " contains the legacy 'type' keyword which is deprecated and will be removed in the future. Please migrate the mapinfo file to 'gametype'.\n");
953                         if(f)
954                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, TRUE);
955                         else
956                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
957                 }
958                 else if(t == "gametype")
959                 {
960                         t = car(s); s = cdr(s);
961                         f = MapInfo_Type_FromString(t);
962                         if(f)
963                                 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
964                         else
965                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
966                 }
967                 else if(t == "size")
968                 {
969                         float a, b, c, d, e;
970                         t = car(s); s = cdr(s); a = stof(t);
971                         t = car(s); s = cdr(s); b = stof(t);
972                         t = car(s); s = cdr(s); c = stof(t);
973                         t = car(s); s = cdr(s); d = stof(t);
974                         t = car(s); s = cdr(s); e = stof(t);
975                         if(s == "")
976                                 print("Map ", pFilename, " contains an incorrect size line (not enough params), syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
977                         else
978                         {
979                                 t = car(s); s = cdr(s); f = stof(t);
980                                 if(s != "")
981                                         print("Map ", pFilename, " contains an incorrect size line (too many params), syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
982                                 else
983                                 {
984                                         if(a >= d || b >= e || c >= f)
985                                                 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
986                                         else
987                                         {
988                                                 MapInfo_Map_mins_x = a;
989                                                 MapInfo_Map_mins_y = b;
990                                                 MapInfo_Map_mins_z = c;
991                                                 MapInfo_Map_maxs_x = d;
992                                                 MapInfo_Map_maxs_y = e;
993                                                 MapInfo_Map_maxs_z = f;
994                                         }
995                                 }
996                         }
997                 }
998                 else if(t == "settemp_for_type")
999                 {
1000                         t = car(s); s = cdr(s);
1001                         if((f = MapInfo_Type_FromString(t)))
1002                         {
1003                                 if(f & pGametypeToSet)
1004                                 {
1005                                         _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1006                                 }
1007                         }
1008                         else
1009                         {
1010                                 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1011                         }
1012                 }
1013                 else if(t == "clientsettemp_for_type")
1014                 {
1015                         t = car(s); s = cdr(s);
1016                         if((f = MapInfo_Type_FromString(t)))
1017                         {
1018                                 if(f & pGametypeToSet)
1019                                 {
1020                                         _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1021                                 }
1022                         }
1023                         else
1024                         {
1025                                 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1026                         }
1027                 }
1028                 else if(t == "fog")
1029                 {
1030                         if not(cvar_value_issafe(t))
1031                                 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1032                         else
1033                                 MapInfo_Map_fog = s;
1034                 }
1035                 else if(t == "cdtrack")
1036                 {
1037                         if(pGametypeToSet)
1038                         {
1039                                 if not(cvar_value_issafe(t))
1040                                         print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1041                                 else
1042                                         MapInfo_Map_clientstuff = strcat(
1043                                                 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
1044                                         );
1045                         }
1046                 }
1047                 else
1048                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1049         }
1050         fclose(fh);
1051
1052         if(MapInfo_Map_title == "<TITLE>")
1053                 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1054         else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1055                 MapInfo_Map_titlestring = MapInfo_Map_title;
1056         else
1057                 MapInfo_Map_titlestring = sprintf(_("%s: %s"), MapInfo_Map_bspname, MapInfo_Map_title);
1058
1059         MapInfo_Cache_Store();
1060         if(MapInfo_Map_supportedGametypes != 0)
1061                 return r;
1062         dprint("Map ", pFilename, " supports no game types, ignored\n");
1063         return 0;
1064 }
1065 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
1066 {
1067         float r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1068
1069         if(cvar("g_tdm_on_dm_maps"))
1070         {
1071                 // if this is set, all DM maps support TDM too
1072                 if not(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH)
1073                         if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)
1074                                 _MapInfo_Map_ApplyGametypeEx ("", pGametypeToSet, MAPINFO_TYPE_TEAM_DEATHMATCH);
1075         }
1076
1077         if(pGametypeToSet)
1078         {
1079                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1080                 {
1081                         error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1082                         //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1083                         //return;
1084                 }
1085         }
1086
1087         return r;
1088 }
1089
1090 float MapInfo_FindName(string s)
1091 {
1092         // if there is exactly one map of prefix s, return it
1093         // if not, return the null string
1094         // note that DP sorts glob results... so I can use a binary search
1095         float l, r, m, cmp;
1096         l = 0;
1097         r = MapInfo_count;
1098         // invariants: r is behind s, l-1 is equal or before
1099         while(l != r)
1100         {
1101                 m = floor((l + r) / 2);
1102                 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1103                 cmp = strcasecmp(MapInfo_FindName_match, s);
1104                 if(cmp == 0)
1105                         return m; // found and good
1106                 if(cmp < 0)
1107                         l = m + 1; // l-1 is before s
1108                 else
1109                         r = m; // behind s
1110         }
1111         MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1112         MapInfo_FindName_firstResult = l;
1113         // r == l, so: l is behind s, l-1 is before
1114         // SO: if there is any, l is the one with the right prefix
1115         //     and l+1 may be one too
1116         if(l == MapInfo_count)
1117         {
1118                 MapInfo_FindName_match = string_null;
1119                 MapInfo_FindName_firstResult = -1;
1120                 return -1; // no MapInfo_FindName_match, behind last item
1121         }
1122         if(!startsWithNocase(MapInfo_FindName_match, s))
1123         {
1124                 MapInfo_FindName_match = string_null;
1125                 MapInfo_FindName_firstResult = -1;
1126                 return -1; // wrong prefix
1127         }
1128         if(l == MapInfo_count - 1)
1129                 return l; // last one, nothing can follow => unique
1130         if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1131         {
1132                 MapInfo_FindName_match = string_null;
1133                 return -1; // ambigous MapInfo_FindName_match
1134         }
1135         return l;
1136 }
1137
1138 string MapInfo_FixName(string s)
1139 {
1140         MapInfo_FindName(s);
1141         return MapInfo_FindName_match;
1142 }
1143
1144 float MapInfo_CurrentFeatures()
1145 {
1146         float req;
1147         req = 0;
1148         if(!(cvar("g_lms") || cvar("g_minstagib") || cvar("g_nix") || cvar("g_weaponarena") || !cvar("g_pickup_items") || cvar("g_race") || cvar("g_cts") || cvar("g_nexball")))
1149                 req |= MAPINFO_FEATURE_WEAPONS;
1150         return req;
1151 }
1152
1153 float MapInfo_CurrentGametype()
1154 {
1155         float prev;
1156         entity e;
1157         prev = cvar("gamecfg");
1158         for(e = MapInfo_Type_first; e; e = e.enemy)
1159                 if(cvar(e.netname))
1160                         if(prev != e.weapons)
1161                                 return e.weapons;
1162         if(prev)
1163                 return prev;
1164         return MAPINFO_TYPE_DEATHMATCH;
1165 }
1166
1167 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1168 {
1169         if(!MapInfo_Get_ByName(s, 1, 0))
1170                 return 0;
1171         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1172                 return 0;
1173         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1174                 return 0;
1175         return 1;
1176 }
1177
1178 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1179 {
1180         float r;
1181         r = _MapInfo_CheckMap(s);
1182         MapInfo_ClearTemps();
1183         return r;
1184 }
1185
1186 void MapInfo_SwitchGameType(float t)
1187 {
1188         entity e;
1189         for(e = MapInfo_Type_first; e; e = e.enemy)
1190                 cvar_set(e.netname, (t == e.weapons) ? "1" : "0");
1191 }
1192
1193 void MapInfo_LoadMap(string s, float reinit)
1194 {
1195         MapInfo_Map_supportedGametypes = 0;
1196         // we shouldn't need this, as LoadMapSettings already fixes the gametype
1197         //if(!MapInfo_CheckMap(s))
1198         //{
1199         //      print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1200         //      MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1201         //}
1202         
1203         cvar_settemp_restore();
1204         if(reinit)
1205                 localcmd(strcat("\nmap ", s, "\n"));
1206         else
1207                 localcmd(strcat("\nchangelevel ", s, "\n"));
1208 }
1209
1210 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1211 {
1212         string out;
1213         float i;
1214
1215         // to make absolutely sure:
1216         MapInfo_Enumerate();
1217         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1218
1219         out = "";
1220         for(i = 0; i < MapInfo_count; ++i)
1221                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1222         return substring(out, 1, strlen(out) - 1);
1223 }
1224
1225 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1226 {
1227         string out;
1228         float i;
1229
1230         // to make absolutely sure:
1231         MapInfo_Enumerate();
1232         MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1233
1234         out = "";
1235         for(i = 0; i < MapInfo_count; ++i)
1236                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1237
1238         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1239
1240         return substring(out, 1, strlen(out) - 1);
1241 }
1242
1243 void MapInfo_LoadMapSettings_SaveGameType(float t)
1244 {
1245         MapInfo_SwitchGameType(t);
1246         cvar_set("gamecfg", ftos(t));
1247         MapInfo_LoadedGametype = t;
1248 }
1249
1250 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1251 {
1252         float t;
1253
1254         t = MapInfo_CurrentGametype();
1255         MapInfo_LoadMapSettings_SaveGameType(t);
1256
1257         if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1258         {
1259                 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1260                 {
1261                         print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1262                         _MapInfo_Map_ApplyGametypeEx("", t, t);
1263                         return; // do not call Get_ByName!
1264                 }
1265
1266                 if(MapInfo_Map_supportedGametypes == 0)
1267                 {
1268                         print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1269                         MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1270                         MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1271                         _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1272                         return; // do not call Get_ByName!
1273                 }
1274
1275                 t = 1;
1276                 while(!(MapInfo_Map_supportedGametypes & 1))
1277                 {
1278                         t *= 2;
1279                         MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1280                 }
1281
1282                 // t is now a supported mode!
1283                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1284                 MapInfo_LoadMapSettings_SaveGameType(t);
1285         }
1286         MapInfo_Get_ByName(s, 1, t);
1287 }
1288
1289 void MapInfo_ClearTemps()
1290 {
1291         MapInfo_Map_bspname = string_null;
1292         MapInfo_Map_title = string_null;
1293         MapInfo_Map_titlestring = string_null;
1294         MapInfo_Map_description = string_null;
1295         MapInfo_Map_author = string_null;
1296         MapInfo_Map_clientstuff = string_null;
1297         MapInfo_Map_supportedGametypes = 0;
1298         MapInfo_Map_supportedFeatures = 0;
1299 }
1300
1301 void MapInfo_Shutdown()
1302 {
1303         MapInfo_ClearTemps();
1304         MapInfo_Filter_Free();
1305         MapInfo_Cache_Destroy();
1306         if(_MapInfo_globopen)
1307         {
1308                 search_end(_MapInfo_globhandle);
1309                 _MapInfo_globhandle = -1;
1310                 _MapInfo_globopen = FALSE;
1311         }
1312 }
1313
1314 float MapInfo_ForbiddenFlags()
1315 {
1316         float f;
1317         f = MAPINFO_FLAG_FORBIDDEN;
1318
1319 #ifndef MENUQC
1320         if not(cvar("g_maplist_allow_hidden"))
1321 #endif
1322                 f |= MAPINFO_FLAG_HIDDEN;
1323
1324         if not(cvar("g_maplist_allow_frustrating"))
1325                 f |= MAPINFO_FLAG_FRUSTRATING;
1326
1327         return f;
1328 }
1329
1330 float MapInfo_RequiredFlags()
1331 {
1332         float f;
1333         f = 0;
1334
1335         if(cvar("g_maplist_allow_frustrating") > 1)
1336                 f |= MAPINFO_FLAG_FRUSTRATING;
1337
1338         return f;
1339 }