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