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