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