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