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