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