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