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