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