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