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