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