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