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