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