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