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