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