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