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