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