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