]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapinfo.qc
Merge branch 'master' into samual/keepaway
[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_CA;
355                 }
356                 if(                     diameter < 4096)
357                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
358                 if(spawnpoints >= 12 && diameter > 5120)
359                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
360         }
361
362         if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
363         if(!spawnplaces)
364         {
365                 MapInfo_Map_supportedGametypes &~= MAPINFO_TYPE_RACE;
366                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
367         }
368
369         dprint("-> diameter ",    ftos(diameter));
370         dprint(";  spawnpoints ", ftos(spawnpoints));
371         dprint(";  modes ",       ftos(MapInfo_Map_supportedGametypes), "\n");
372
373         fclose(fh);
374
375         return r;
376 }
377
378 void _MapInfo_Map_Reset()
379 {
380         MapInfo_Map_title = "<TITLE>";
381         MapInfo_Map_description = "<DESCRIPTION>";
382         MapInfo_Map_author = "<AUTHOR>";
383         MapInfo_Map_supportedGametypes = 0;
384         MapInfo_Map_supportedFeatures = 0;
385         MapInfo_Map_flags = 0;
386         MapInfo_Map_clientstuff = "";
387         MapInfo_Map_fog = "";
388         MapInfo_Map_mins = '0 0 0';
389         MapInfo_Map_maxs = '0 0 0';
390 }
391
392 string _MapInfo_GetDefault(float t)
393 {
394         switch(t)
395         {
396                 case MAPINFO_TYPE_DEATHMATCH:      return "30 20 0";
397                 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
398                 case MAPINFO_TYPE_DOMINATION:      return "200 20 0";
399                 case MAPINFO_TYPE_CTF:             return "300 20 10 0";
400                 case MAPINFO_TYPE_RUNEMATCH:       return "200 20 0";
401                 case MAPINFO_TYPE_LMS:             return "9 20 0";
402                 case MAPINFO_TYPE_ARENA:           return "10 20 0";
403                 case MAPINFO_TYPE_CA:              return "10 20 0";
404                 case MAPINFO_TYPE_KEYHUNT:         return "1000 20 3 0";
405                 case MAPINFO_TYPE_ASSAULT:         return "20 0";
406                 case MAPINFO_TYPE_RACE:            return "20 5 7 15 0";
407                 case MAPINFO_TYPE_ONSLAUGHT:       return "20 0";
408                 case MAPINFO_TYPE_NEXBALL:         return "5 20 0";
409                 case MAPINFO_TYPE_CTS:             return "20 0 0";
410                 case MAPINFO_TYPE_KEEPAWAY:        return "30 20 0";
411                 default:                           return "";
412         }
413 }
414
415 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, float load_default)
416 {
417         string sa;
418         MapInfo_Map_supportedGametypes |= pThisType;
419         if(!(pThisType & pWantedType))
420                 return;
421
422         if(load_default)
423                 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, FALSE);
424         
425         if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
426         {
427                 cvar_set("fraglimit", "0");
428         }
429         else
430         {
431                 sa = car(s);
432                 if(sa != "")
433                         cvar_set("fraglimit", sa);
434                 s = cdr(s);
435         }
436
437         sa = car(s);
438         if(sa != "")
439                 cvar_set("timelimit", sa);
440         s = cdr(s);
441
442         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
443         {
444                 sa = car(s);
445                 if(sa != "")
446                         cvar_set("g_tdm_teams", sa);
447                 s = cdr(s);
448         }
449
450         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
451         {
452                 sa = car(s);
453                 if(sa != "")
454                         cvar_set("g_keyhunt_teams", sa);
455                 s = cdr(s);
456         }
457
458         if(pWantedType == MAPINFO_TYPE_CTF)
459         {
460                 sa = car(s);
461                 if(sa != "")
462                         if(cvar("g_ctf_win_mode") < 2)
463                                 cvar_set("fraglimit", sa);
464                 s = cdr(s);
465         }
466
467         if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
468         {
469                 sa = car(s);
470                 if(sa != "")
471                         cvar_set("fraglimit", sa);
472                 s = cdr(s);
473         }
474
475         // rc = timelimit timelimit_qualification laps laps_teamplay
476         if(pWantedType == MAPINFO_TYPE_RACE)
477         {
478                 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
479                 cvar_set("g_race_qualifying_timelimit", sa);
480                 s = cdr(s);
481
482                 sa = car(s);
483                 if(sa != "")
484                         if(cvar("g_race_teams") < 2)
485                                 cvar_set("fraglimit", 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
495         if(pWantedType == MAPINFO_TYPE_CTS)
496         {
497                 sa = car(s);
498
499                 // this is the skill of the map
500                 // not parsed by anything yet
501                 // for map databases
502                 //if(sa != "")
503                 //      cvar_set("fraglimit", sa);
504
505                 s = cdr(s);
506         }
507
508         if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
509         {
510                 cvar_set("leadlimit", "0");
511         }
512         else
513         {
514                 sa = car(s);
515                 if(sa != "")
516                         cvar_set("leadlimit", sa);
517                 s = cdr(s);
518         }
519 }
520
521 string _MapInfo_GetDefaultEx(float t)
522 {
523         switch(t)
524         {
525                 case MAPINFO_TYPE_DEATHMATCH:      return "timelimit=20 pointlimit=30 leadlimit=0";
526                 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "timelimit=20 pointlimit=50 teams=2 leadlimit=0";
527                 case MAPINFO_TYPE_DOMINATION:      return "timelimit=20 pointlimit=200 teams=2 leadlimit=0";
528                 case MAPINFO_TYPE_CTF:             return "timelimit=20 pointlimit=300 caplimit=10 leadlimit=0";
529                 case MAPINFO_TYPE_RUNEMATCH:       return "timelimit=20 pointlimit=200 leadlimit=0";
530                 case MAPINFO_TYPE_LMS:             return "timelimit=20 lives=9 leadlimit=0";
531                 case MAPINFO_TYPE_ARENA:           return "timelimit=20 pointlimit=10 leadlimit=0";
532                 case MAPINFO_TYPE_CA:              return "timelimit=20 pointlimit=10 leadlimit=0";
533                 case MAPINFO_TYPE_KEYHUNT:         return "timelimit=20 pointlimit=1000 teams=3 leadlimit=0";
534                 case MAPINFO_TYPE_ASSAULT:         return "timelimit=20";
535                 case MAPINFO_TYPE_RACE:            return "timelimit=20 qualifying_timelimit=5 laplimit=7 teamlaplimit=15 leadlimit=0";
536                 case MAPINFO_TYPE_ONSLAUGHT:       return "timelimit=20";
537                 case MAPINFO_TYPE_NEXBALL:         return "timelimit=20 pointlimit=5 leadlimit=0";
538                 case MAPINFO_TYPE_CTS:             return "timelimit=20 skill=-1";
539                 case MAPINFO_TYPE_KEEPAWAY:        return "timelimit=20 pointlimit=30";
540                 default:                           return "";
541         }
542 }
543
544 void _MapInfo_Map_ApplyGametypeEx(string s, float pWantedType, float pThisType)
545 {
546         string sa, k, v;
547         float p;
548         string fraglimit_normal;
549         string fraglimit_caps;
550         string fraglimit_teams;
551
552         MapInfo_Map_supportedGametypes |= pThisType;
553         if(!(pThisType & pWantedType))
554                 return;
555
556         // reset all the cvars to their defaults
557
558         cvar_set("timelimit", cvar_defstring("timelimit"));
559         cvar_set("leadlimit", cvar_defstring("leadlimit"));
560         cvar_set("fraglimit", cvar_defstring("fraglimit"));
561         cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
562         cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
563         cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
564         cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
565
566         fraglimit_normal = string_null;
567         fraglimit_caps = string_null;
568         fraglimit_teams = string_null;
569
570         s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
571         while(s != "")
572         {
573                 sa = car(s);
574                 s = cdr(s);
575
576                 if(sa == "")
577                         continue;
578
579                 p = strstrofs(sa, "=", 0);
580                 if(p < 0)
581                 {
582                         k = "timelimit";
583                         v = s;
584                 }
585                 else
586                 {
587                         k = substring(sa, 0, p);
588                         v = substring(sa, p+1, -1);
589                 }
590
591                 if(k == "timelimit")
592                 {
593                         cvar_set("timelimit", v);
594                 }
595                 else if(k == "leadlimit")
596                 {
597                         cvar_set("leadlimit", v);
598                 }
599                 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit")
600                 {
601                         fraglimit_normal = v;
602                 }
603                 else if(k == "caplimit")
604                 {
605                         fraglimit_caps = v;
606                 }
607                 else if(k == "teampointlimit" || k == "teamlaplimit")
608                 {
609                         fraglimit_teams = v;
610                 }
611                 else if(k == "teams")
612                 {
613                         cvar_set("g_tdm_teams", v);
614                         cvar_set("g_keyhunt_teams", v);
615                         cvar_set("g_domination_default_teams", v);
616                 }
617                 else if(k == "qualifying_timelimit")
618                 {
619                         cvar_set("g_race_qualifying_timelimit", v);
620                 }
621                 else if(k == "skill")
622                 {
623                         // ignore
624                 }
625                 else
626                 {
627                         print("Invalid gametype key in mapinfo: ", k, "\n");
628                 }
629         }
630
631         if(pWantedType == MAPINFO_TYPE_CTF && cvar("g_ctf_win_mode") < 2)
632         {
633                 if(fraglimit_caps)
634                         cvar_set("fraglimit", fraglimit_caps);
635         }
636         else if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
637         {
638                 if(fraglimit_teams)
639                         cvar_set("fraglimit", fraglimit_teams);
640         }
641         else
642         {
643                 if(fraglimit_normal)
644                         cvar_set("fraglimit", fraglimit_normal);
645         }
646 }
647
648 float MapInfo_Type_FromString(string t)
649 {
650         if     (t == "dm")      return MAPINFO_TYPE_DEATHMATCH;
651         else if(t == "tdm")     return MAPINFO_TYPE_TEAM_DEATHMATCH;
652         else if(t == "dom")     return MAPINFO_TYPE_DOMINATION;
653         else if(t == "ctf")     return MAPINFO_TYPE_CTF;
654         else if(t == "rune")    return MAPINFO_TYPE_RUNEMATCH;
655         else if(t == "lms")     return MAPINFO_TYPE_LMS;
656         else if(t == "arena")   return MAPINFO_TYPE_ARENA;
657         else if(t == "ca")      return MAPINFO_TYPE_CA;
658         else if(t == "kh")      return MAPINFO_TYPE_KEYHUNT;
659         else if(t == "as")      return MAPINFO_TYPE_ASSAULT;
660         else if(t == "ons")     return MAPINFO_TYPE_ONSLAUGHT;
661         else if(t == "rc")      return MAPINFO_TYPE_RACE;
662         else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
663         else if(t == "cts")     return MAPINFO_TYPE_CTS;
664         else if(t == "keepaway") return MAPINFO_TYPE_KEEPAWAY;
665         else if(t == "all")     return MAPINFO_TYPE_ALL;
666         else                    return 0;
667 }
668
669 string MapInfo_Type_ToString(float t)
670 {
671         if     (t == MAPINFO_TYPE_DEATHMATCH)      return "dm";
672         else if(t == MAPINFO_TYPE_TEAM_DEATHMATCH) return "tdm";
673         else if(t == MAPINFO_TYPE_DOMINATION)      return "dom";
674         else if(t == MAPINFO_TYPE_CTF)             return "ctf";
675         else if(t == MAPINFO_TYPE_RUNEMATCH)       return "rune";
676         else if(t == MAPINFO_TYPE_LMS)             return "lms";
677         else if(t == MAPINFO_TYPE_ARENA)           return "arena";
678         else if(t == MAPINFO_TYPE_CA)              return "ca";
679         else if(t == MAPINFO_TYPE_KEYHUNT)         return "kh";
680         else if(t == MAPINFO_TYPE_ASSAULT)         return "as";
681         else if(t == MAPINFO_TYPE_ONSLAUGHT)       return "ons";
682         else if(t == MAPINFO_TYPE_RACE)            return "rc";
683         else if(t == MAPINFO_TYPE_NEXBALL)         return "nexball";
684         else if(t == MAPINFO_TYPE_CTS)             return "cts";
685         else if(t == MAPINFO_TYPE_KEEPAWAY)        return "keepaway";
686         else if(t == MAPINFO_TYPE_ALL)             return "all";
687         else                                       return "";
688 }
689
690 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
691 {
692         string t;
693         float fh, o;
694         t = car(s); s = cdr(s);
695
696         // limited support of "" and comments
697         //   remove trailing and leading " of t
698         if(substring(t, 0, 1) == "\"")
699         {
700                 if(substring(t, -1, 1) == "\"")
701                         t = substring(t, 1, -2);
702         }
703
704         //   remove leading " of s
705         if(substring(s, 0, 1) == "\"")
706         {
707                 s = substring(s, 1, -1);
708         }
709         //   remove trailing " of s, and all that follows (cvar description)
710         o = strstrofs(s, "\"", 0);
711         if(o >= 0)
712                 s = substring(s, 0, o);
713         
714         //   remove // comments
715         o = strstrofs(s, "//", 0);
716         if(o >= 0)
717                 s = substring(s, 0, o);
718         
719         //   remove trailing spaces
720         while(substring(s, -1, 1) == " ")
721                 s = substring(s, 0, -2);
722
723         if(t == "#include")
724         {
725                 if(recurse > 0)
726                 {
727                         fh = fopen(s, FILE_READ);
728                         if(fh < 0)
729                                 print("Map ", pFilename, " references not existing config file ", s, "\n");
730                         else
731                         {
732                                 for(;;)
733                                 {
734                                         if not((s = fgets(fh)))
735                                                 break;
736
737                                         // catch different sorts of comments
738                                         if(s == "")                    // empty lines
739                                                 continue;
740                                         if(substring(s, 0, 1) == "#")  // UNIX style
741                                                 continue;
742                                         if(substring(s, 0, 2) == "//") // C++ style
743                                                 continue;
744                                         if(substring(s, 0, 1) == "_")  // q3map style
745                                                 continue;
746
747                                         if(substring(s, 0, 4) == "set ")
748                                                 s = substring(s, 4, -1);
749                                         if(substring(s, 0, 5) == "seta ")
750                                                 s = substring(s, 5, -1);
751
752                                         _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
753                                 }
754                                 fclose(fh);
755                         }
756                 }
757                 else
758                         print("Map ", pFilename, " uses too many levels of inclusion\n");
759         }
760         else if(t == "")
761                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
762         else if not(cvar_value_issafe(t))
763                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
764         else if not (cvar_value_issafe(s))
765                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
766         else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
767                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
768         else if(matchacl(acl, t) <= 0)
769                 print("Map ", pFilename, " contains a denied setting, ignored\n");
770         else
771         {
772                 if(type == 0) // server set
773                 {
774                         dprint("Applying temporary setting ", t, " := ", s, "\n");
775                         if(cvar("g_campaign"))
776                                 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
777                         else
778                                 cvar_settemp(t, s);
779                 }
780                 else
781                 {
782                         dprint("Applying temporary client setting ", t, " := ", s, "\n");
783                         MapInfo_Map_clientstuff = strcat(
784                                         MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
785                                         );
786                 }
787         }
788 }
789
790 // load info about a map by name into the MapInfo_Map_* globals
791 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
792 {
793         string fn;
794         string s, t;
795         float fh, fh2;
796         float r, f, n, i, p;
797         string acl;
798
799         acl = MAPINFO_SETTEMP_ACL_USER;
800
801         if(strstrofs(pFilename, "/", 0) >= 0)
802         {
803                 print("Invalid character in map name, ignored\n");
804                 return 0;
805         }
806
807         if(pGametypeToSet == 0)
808                 if(MapInfo_Cache_Retrieve(pFilename))
809                         return 1;
810
811         r = 1;
812
813         MapInfo_Map_bspname = pFilename;
814
815         // default all generic fields so they have "good" values in case something fails
816         fn = strcat("maps/", pFilename, ".mapinfo");
817         fh = fopen(fn, FILE_READ);
818         if(fh < 0)
819         {
820                 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
821                 fh = fopen(fn, FILE_READ);
822                 if(fh < 0)
823                 {
824                         if(!pAllowGenerate)
825                                 return 0;
826                         _MapInfo_Map_Reset();
827                         r = _MapInfo_Generate(pFilename);
828                         if(!r)
829                                 return 0;
830                         fh = fopen(fn, FILE_WRITE);
831                         fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
832                         fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
833                         fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
834                         if(_MapInfo_Map_worldspawn_music != "")
835                         {
836                                 if(
837                                         substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
838                                         ||
839                                         substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
840                                 )
841                                         fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
842                                 else
843                                         fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
844                         }
845                         else
846                         {
847                                 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
848                                 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
849                                 for(;;)
850                                 {
851                                         i = floor(random() * n);
852                                         if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
853                                                 break;
854                                 }
855                                 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
856                         }
857                         if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
858                                 fputs(fh, "has weapons\n");
859                         else
860                                 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
861                         if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
862                                 fputs(fh, "frustrating\n");
863
864                         for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
865                                 if(MapInfo_Map_supportedGametypes & i)
866                                         fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
867
868                         fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
869                         if(fh2 >= 0)
870                         {
871                                 fclose(fh2);
872                                 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
873                         }
874
875                         fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
876                         fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
877                         fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
878                         fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
879                         fputs(fh, "// optional: hidden\n");
880
881                         fclose(fh);
882                         r = 2;
883                         // return r;
884                         fh = fopen(fn, FILE_READ);
885                         if(fh < 0)
886                                 error("... but I just wrote it!");
887                 }
888
889                 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
890         }
891
892         _MapInfo_Map_Reset();
893         for(;;)
894         {
895                 if not((s = fgets(fh)))
896                         break;
897
898                 // catch different sorts of comments
899                 if(s == "")                    // empty lines
900                         continue;
901                 if(substring(s, 0, 1) == "#")  // UNIX style
902                         continue;
903                 if(substring(s, 0, 2) == "//") // C++ style
904                         continue;
905                 if(substring(s, 0, 1) == "_")  // q3map style
906                         continue;
907
908                 p = strstrofs(s, "//", 0);
909                 if(p >= 0)
910                         s = substring(s, 0, p);
911
912                 t = car(s); s = cdr(s);
913                 if(t == "title")
914                         MapInfo_Map_title = s;
915                 else if(t == "description")
916                         MapInfo_Map_description = s;
917                 else if(t == "author")
918                         MapInfo_Map_author = s;
919                 else if(t == "has")
920                 {
921                         t = car(s); s = cdr(s);
922                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
923                         else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
924                         else
925                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
926                 }
927                 else if(t == "hidden")
928                 {
929                         MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
930                 }
931                 else if(t == "forbidden")
932                 {
933                         MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
934                 }
935                 else if(t == "frustrating")
936                 {
937                         MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
938                 }
939                 else if(t == "type")
940                 {
941                         t = car(s); s = cdr(s);
942                         f = MapInfo_Type_FromString(t);
943                         if(f)
944                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, TRUE);
945                         else
946                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
947                 }
948                 else if(t == "gametype")
949                 {
950                         t = car(s); s = cdr(s);
951                         f = MapInfo_Type_FromString(t);
952                         if(f)
953                                 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
954                         else
955                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
956                 }
957                 else if(t == "size")
958                 {
959                         float a, b, c, d, e;
960                         t = car(s); s = cdr(s); a = stof(t);
961                         t = car(s); s = cdr(s); b = stof(t);
962                         t = car(s); s = cdr(s); c = stof(t);
963                         t = car(s); s = cdr(s); d = stof(t);
964                         t = car(s); s = cdr(s); e = stof(t);
965                         if(s == "")
966                                 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");
967                         else
968                         {
969                                 t = car(s); s = cdr(s); f = stof(t);
970                                 if(s != "")
971                                         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");
972                                 else
973                                 {
974                                         if(a >= d || b >= e || c >= f)
975                                                 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
976                                         else
977                                         {
978                                                 MapInfo_Map_mins_x = a;
979                                                 MapInfo_Map_mins_y = b;
980                                                 MapInfo_Map_mins_z = c;
981                                                 MapInfo_Map_maxs_x = d;
982                                                 MapInfo_Map_maxs_y = e;
983                                                 MapInfo_Map_maxs_z = f;
984                                         }
985                                 }
986                         }
987                 }
988                 else if(t == "settemp_for_type")
989                 {
990                         t = car(s); s = cdr(s);
991                         if((f = MapInfo_Type_FromString(t)))
992                         {
993                                 if(f & pGametypeToSet)
994                                 {
995                                         _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
996                                 }
997                         }
998                         else
999                         {
1000                                 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1001                         }
1002                 }
1003                 else if(t == "clientsettemp_for_type")
1004                 {
1005                         t = car(s); s = cdr(s);
1006                         if((f = MapInfo_Type_FromString(t)))
1007                         {
1008                                 if(f & pGametypeToSet)
1009                                 {
1010                                         _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1011                                 }
1012                         }
1013                         else
1014                         {
1015                                 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1016                         }
1017                 }
1018                 else if(t == "fog")
1019                 {
1020                         if not(cvar_value_issafe(t))
1021                                 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1022                         else
1023                                 MapInfo_Map_fog = s;
1024                 }
1025                 else if(t == "cdtrack")
1026                 {
1027                         if(pGametypeToSet)
1028                         {
1029                                 if not(cvar_value_issafe(t))
1030                                         print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1031                                 else
1032                                         MapInfo_Map_clientstuff = strcat(
1033                                                 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
1034                                         );
1035                         }
1036                 }
1037                 else
1038                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1039         }
1040         fclose(fh);
1041
1042         if(pGametypeToSet)
1043         {
1044                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1045                 {
1046                         error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1047                         //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1048                         //return;
1049                 }
1050         }
1051
1052         MapInfo_Cache_Store();
1053         if(MapInfo_Map_supportedGametypes != 0)
1054                 return r;
1055         dprint("Map ", pFilename, " supports no game types, ignored\n");
1056         return 0;
1057 }
1058
1059 float MapInfo_FindName(string s)
1060 {
1061         // if there is exactly one map of prefix s, return it
1062         // if not, return the null string
1063         // note that DP sorts glob results... so I can use a binary search
1064         float l, r, m, cmp;
1065         l = 0;
1066         r = MapInfo_count;
1067         // invariants: r is behind s, l-1 is equal or before
1068         while(l != r)
1069         {
1070                 m = floor((l + r) / 2);
1071                 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1072                 cmp = strcasecmp(MapInfo_FindName_match, s);
1073                 if(cmp == 0)
1074                         return m; // found and good
1075                 if(cmp < 0)
1076                         l = m + 1; // l-1 is before s
1077                 else
1078                         r = m; // behind s
1079         }
1080         MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1081         MapInfo_FindName_firstResult = l;
1082         // r == l, so: l is behind s, l-1 is before
1083         // SO: if there is any, l is the one with the right prefix
1084         //     and l+1 may be one too
1085         if(l == MapInfo_count)
1086         {
1087                 MapInfo_FindName_match = string_null;
1088                 MapInfo_FindName_firstResult = -1;
1089                 return -1; // no MapInfo_FindName_match, behind last item
1090         }
1091         if(!startsWithNocase(MapInfo_FindName_match, s))
1092         {
1093                 MapInfo_FindName_match = string_null;
1094                 MapInfo_FindName_firstResult = -1;
1095                 return -1; // wrong prefix
1096         }
1097         if(l == MapInfo_count - 1)
1098                 return l; // last one, nothing can follow => unique
1099         if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1100         {
1101                 MapInfo_FindName_match = string_null;
1102                 return -1; // ambigous MapInfo_FindName_match
1103         }
1104         return l;
1105 }
1106
1107 string MapInfo_FixName(string s)
1108 {
1109         MapInfo_FindName(s);
1110         return MapInfo_FindName_match;
1111 }
1112
1113 float MapInfo_CurrentFeatures()
1114 {
1115         float req;
1116         req = 0;
1117         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")))
1118                 req |= MAPINFO_FEATURE_WEAPONS;
1119         return req;
1120 }
1121
1122 float MapInfo_CurrentGametype()
1123 {
1124         if(cvar("g_domination"))
1125                 return MAPINFO_TYPE_DOMINATION;
1126         else if(cvar("g_ctf"))
1127                 return MAPINFO_TYPE_CTF;
1128         else if(cvar("g_runematch"))
1129                 return MAPINFO_TYPE_RUNEMATCH;
1130         else if(cvar("g_tdm"))
1131                 return MAPINFO_TYPE_TEAM_DEATHMATCH;
1132         else if(cvar("g_assault"))
1133                 return MAPINFO_TYPE_ASSAULT;
1134         else if(cvar("g_lms"))
1135                 return MAPINFO_TYPE_LMS;
1136         else if(cvar("g_arena"))
1137                 return MAPINFO_TYPE_ARENA;
1138         else if(cvar("g_ca"))
1139                 return MAPINFO_TYPE_CA; 
1140         else if(cvar("g_keyhunt"))
1141                 return MAPINFO_TYPE_KEYHUNT;
1142         else if(cvar("g_onslaught"))
1143                 return MAPINFO_TYPE_ONSLAUGHT;
1144         else if(cvar("g_race"))
1145                 return MAPINFO_TYPE_RACE;
1146         else if(cvar("g_nexball"))
1147                 return MAPINFO_TYPE_NEXBALL;
1148         else if(cvar("g_cts"))
1149                 return MAPINFO_TYPE_CTS;
1150         else if(cvar("g_keepaway"))
1151                 return MAPINFO_TYPE_KEEPAWAY;
1152         else
1153                 return MAPINFO_TYPE_DEATHMATCH;
1154 }
1155
1156 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1157 {
1158         if(!MapInfo_Get_ByName(s, 1, 0))
1159                 return 0;
1160         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1161                 return 0;
1162         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1163                 return 0;
1164         return 1;
1165 }
1166
1167 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1168 {
1169         float r;
1170         r = _MapInfo_CheckMap(s);
1171         MapInfo_ClearTemps();
1172         return r;
1173 }
1174
1175 string MapInfo_GetGameTypeCvar(float t)
1176 {
1177         switch(t)
1178         {
1179                 case MAPINFO_TYPE_DEATHMATCH:           return "g_dm";
1180                 case MAPINFO_TYPE_TEAM_DEATHMATCH:      return "g_tdm";
1181                 case MAPINFO_TYPE_DOMINATION:           return "g_domination";
1182                 case MAPINFO_TYPE_CTF:                  return "g_ctf";
1183                 case MAPINFO_TYPE_RUNEMATCH:            return "g_runematch";
1184                 case MAPINFO_TYPE_LMS:                  return "g_lms";
1185                 case MAPINFO_TYPE_ARENA:                        return "g_arena";
1186                 case MAPINFO_TYPE_CA:                   return "g_ca";
1187                 case MAPINFO_TYPE_KEYHUNT:              return "g_kh";
1188                 case MAPINFO_TYPE_ASSAULT:              return "g_assault";
1189                 case MAPINFO_TYPE_ONSLAUGHT:            return "g_onslaught";
1190                 case MAPINFO_TYPE_RACE:                         return "g_race";
1191                 case MAPINFO_TYPE_NEXBALL:              return "g_nexball";
1192                 case MAPINFO_TYPE_CTS:                  return "g_cts";
1193                 case MAPINFO_TYPE_KEEPAWAY:             return "g_keepaway";
1194                 default: return "";
1195         }
1196 }
1197
1198 void MapInfo_SwitchGameType(float t)
1199 {
1200         cvar_set("gamecfg",      "0");
1201         cvar_set("g_dm",         (t == MAPINFO_TYPE_DEATHMATCH)      ? "1" : "0");
1202         cvar_set("g_tdm",        (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
1203         cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION)      ? "1" : "0");
1204         cvar_set("g_ctf",        (t == MAPINFO_TYPE_CTF)             ? "1" : "0");
1205         cvar_set("g_runematch",  (t == MAPINFO_TYPE_RUNEMATCH)       ? "1" : "0");
1206         cvar_set("g_lms",        (t == MAPINFO_TYPE_LMS)             ? "1" : "0");
1207         cvar_set("g_arena",      (t == MAPINFO_TYPE_ARENA)           ? "1" : "0");
1208         cvar_set("g_ca",         (t == MAPINFO_TYPE_CA)              ? "1" : "0");
1209         cvar_set("g_keyhunt",    (t == MAPINFO_TYPE_KEYHUNT)         ? "1" : "0");
1210         cvar_set("g_assault",    (t == MAPINFO_TYPE_ASSAULT)         ? "1" : "0");
1211         cvar_set("g_onslaught",  (t == MAPINFO_TYPE_ONSLAUGHT)       ? "1" : "0");
1212         cvar_set("g_race",       (t == MAPINFO_TYPE_RACE)            ? "1" : "0");
1213         cvar_set("g_nexball",    (t == MAPINFO_TYPE_NEXBALL)         ? "1" : "0");
1214         cvar_set("g_cts",        (t == MAPINFO_TYPE_CTS)             ? "1" : "0");
1215         cvar_set("g_keepaway",   (t == MAPINFO_TYPE_KEEPAWAY)        ? "1" : "0");
1216 }
1217
1218 void MapInfo_LoadMap(string s)
1219 {
1220         MapInfo_Map_supportedGametypes = 0;
1221         // we shouldn't need this, as LoadMapSettings already fixes the gametype
1222         //if(!MapInfo_CheckMap(s))
1223         //{
1224         //      print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1225         //      MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1226         //}
1227         localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
1228 }
1229
1230 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1231 {
1232         string out;
1233         float i;
1234
1235         // to make absolutely sure:
1236         MapInfo_Enumerate();
1237         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1238
1239         out = "";
1240         for(i = 0; i < MapInfo_count; ++i)
1241                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1242         return substring(out, 1, strlen(out) - 1);
1243 }
1244
1245 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1246 {
1247         float t, t0;
1248         if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1249         {
1250                 if(MapInfo_Map_supportedGametypes == 0)
1251                 {
1252                         print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1253                         MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1254                         _MapInfo_Map_ApplyGametypeEx("", t0, t0);
1255                         return; // do not call Get_ByName!
1256                 }
1257
1258                 t = 1;
1259                 while(!(MapInfo_Map_supportedGametypes & 1))
1260                 {
1261                         t *= 2;
1262                         MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1263                 }
1264                 // t is now a supported mode!
1265                 t0 = MapInfo_CurrentGametype();
1266                 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1267                 {
1268                         print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1269                         cvar_settemp_restore();
1270                         _MapInfo_Map_ApplyGametypeEx("", t0, t0);
1271                         return; // do not call Get_ByName!
1272                 }
1273                 else
1274                 {
1275                         print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1276                         MapInfo_SwitchGameType(t);
1277                 }
1278         }
1279         cvar_settemp_restore();
1280         MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
1281 }
1282
1283 void MapInfo_ClearTemps()
1284 {
1285         MapInfo_Map_bspname = string_null;
1286         MapInfo_Map_title = string_null;
1287         MapInfo_Map_description = string_null;
1288         MapInfo_Map_author = string_null;
1289         MapInfo_Map_clientstuff = string_null;
1290         MapInfo_Map_supportedGametypes = 0;
1291         MapInfo_Map_supportedFeatures = 0;
1292 }
1293
1294 void MapInfo_Shutdown()
1295 {
1296         MapInfo_ClearTemps();
1297         MapInfo_Filter_Free();
1298         MapInfo_Cache_Destroy();
1299         if(_MapInfo_globopen)
1300         {
1301                 search_end(_MapInfo_globhandle);
1302                 _MapInfo_globhandle = -1;
1303                 _MapInfo_globopen = FALSE;
1304         }
1305 }
1306
1307 float MapInfo_ForbiddenFlags()
1308 {
1309         float f;
1310         f = MAPINFO_FLAG_FORBIDDEN;
1311
1312 #ifndef MENUQC
1313         if not(cvar("g_maplist_allow_hidden"))
1314 #endif
1315                 f |= MAPINFO_FLAG_HIDDEN;
1316
1317         if not(cvar("g_maplist_allow_frustrating"))
1318                 f |= MAPINFO_FLAG_FRUSTRATING;
1319
1320         return f;
1321 }
1322
1323 float MapInfo_RequiredFlags()
1324 {
1325         float f;
1326         f = 0;
1327
1328         if(cvar("g_maplist_allow_frustrating") > 1)
1329                 f |= MAPINFO_FLAG_FRUSTRATING;
1330
1331         return f;
1332 }