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