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