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