]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/mapinfo.qc
ACLs
[divverent/nexuiz.git] / data / qcsrc / common / mapinfo.qc
1 // generic string stuff
2 float startsWith(string haystack, string needle)
3 {
4         return substring(haystack, 0, strlen(needle)) == needle;
5 }
6 float startsWithNocase(string haystack, string needle)
7 {
8         return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
9 }
10 string extractRestOfLine(string haystack, string needle)
11 {
12         if(startsWith(haystack, needle))
13                 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
14         return string_null;
15 }
16 string car(string s)
17 {
18         float o;
19         o = strstrofs(s, " ", 0);
20         if(o < 0)
21                 return s;
22         return substring(s, 0, o);
23 }
24 string cdr(string s)
25 {
26         float o;
27         o = strstrofs(s, " ", 0);
28         if(o < 0)
29                 return string_null;
30         return substring(s, o + 1, strlen(s) - (o + 1));
31 }
32 float matchacl(string acl, string str)
33 {
34         string t, s;
35         float r, d;
36         r = 0;
37         while(acl)
38         {
39                 t = car(acl); acl = cdr(acl);
40                 d = 1;
41                 if(substring(t, 0, 1) == "-")
42                 {
43                         d = -1;
44                         t = substring(t, 1, strlen(t) - 1);
45                 }
46                 else if(substring(t, 0, 1) == "+")
47                         t = substring(t, 1, strlen(t) - 1);
48                 if(substring(t, -1, 1) == "*")
49                 {
50                         t = substring(t, 0, strlen(t) - 1);
51                         s = substring(s, 0, strlen(t));
52                 }
53                 else
54                         s = str;
55
56                 if(s == t)
57                 {
58                         r = d;
59                 }
60         }
61         return r;
62 }
63
64 float _MapInfo_Cache_Active;
65 float _MapInfo_Cache_DB_NameToIndex;
66 float _MapInfo_Cache_Buf_IndexToMapData;
67
68 void MapInfo_Cache_Destroy()
69 {
70         if(!_MapInfo_Cache_Active)
71                 return;
72
73         db_close(_MapInfo_Cache_DB_NameToIndex);
74         buf_del(_MapInfo_Cache_Buf_IndexToMapData);
75         _MapInfo_Cache_Active = 0;
76 }
77
78 void MapInfo_Cache_Create()
79 {
80         MapInfo_Cache_Destroy();
81         _MapInfo_Cache_DB_NameToIndex = db_create();
82         _MapInfo_Cache_Buf_IndexToMapData = buf_create();
83         _MapInfo_Cache_Active = 1;
84 }
85
86 void MapInfo_Cache_Invalidate()
87 {
88         if(!_MapInfo_Cache_Active)
89                 return;
90
91         MapInfo_Cache_Create();
92 }
93
94 void MapInfo_Cache_Store()
95 {
96         float i;
97         string s;
98         if(!_MapInfo_Cache_Active)
99                 return;
100
101         s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
102         if(!s) // empty string is NOT valid here!
103         {
104                 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
105                 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
106         }
107         else
108                 i = stof(s);
109
110         // now store all the stuff
111         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData,   i, MapInfo_Map_bspname);
112         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
113         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
114         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
115         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
116         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
117         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
118 }
119
120 float MapInfo_Cache_Retrieve(string map)
121 {
122         float i;
123         string s;
124         if(!_MapInfo_Cache_Active)
125                 return 0;
126
127         s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
128         if(!s)
129                 return 0;
130         i = stof(s);
131
132         // now retrieve all the stuff
133         MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
134         MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
135         MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
136         MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
137         MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
138         MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
139         MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
140         return 1;
141 }
142
143 // GLOB HANDLING (for all BSP files)
144 float _MapInfo_globopen;
145 float _MapInfo_globcount; 
146 float _MapInfo_globhandle;
147 string _MapInfo_GlobItem(float i)
148 {
149         string s;
150         s = search_getfilename(_MapInfo_globhandle, i);
151         return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
152 }
153
154 void MapInfo_Enumerate()
155 {
156         if(_MapInfo_globopen)
157                 search_end(_MapInfo_globhandle);
158         MapInfo_Cache_Invalidate();
159         _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
160         _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
161         _MapInfo_globopen = 1;
162 }
163
164 // filter the info by game type mask (updates MapInfo_count)
165 //
166 float _MapInfo_filtered;
167 float _MapInfo_filtered_allocated;
168 float MapInfo_FilterList_Lookup(float i)
169 {
170         return stof(bufstr_get(_MapInfo_filtered, i));
171 }
172
173 void _MapInfo_FilterList_swap(float i, float j, entity pass)
174 {
175         string h;
176         h = bufstr_get(_MapInfo_filtered, i);
177         bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
178         bufstr_set(_MapInfo_filtered, j, h);
179 }
180
181 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
182 {
183         string a, b;
184         a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
185         b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
186         return strcasecmp(a, b);
187 }
188
189 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
190 {
191         float i, j;
192         if not(_MapInfo_filtered_allocated)
193         {
194                 _MapInfo_filtered_allocated = 1;
195                 _MapInfo_filtered = buf_create();
196         }
197         MapInfo_count = 0;
198         for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
199         {
200                 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.
201                         if(pAbortOnGenerate)
202                         {
203                                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
204                                 MapInfo_progress = i / _MapInfo_globcount;
205                                 return 0;
206                         }
207                 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
208                 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
209                 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
210                 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
211                         bufstr_set(_MapInfo_filtered, ++j, ftos(i));
212         }
213         MapInfo_count = j + 1;
214         MapInfo_ClearTemps();
215         
216         // sometimes the glob isn't sorted nicely, so fix it here...
217         heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
218
219         return 1;
220 }
221
222 void MapInfo_Filter_Free()
223 {
224         if(_MapInfo_filtered_allocated)
225         {
226                 buf_del(_MapInfo_filtered);
227                 _MapInfo_filtered_allocated = 0;
228         }
229 }
230
231 // load info about the i-th map into the MapInfo_Map_* globals
232 string MapInfo_BSPName_ByID(float i)
233 {
234         return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
235 }
236
237 string unquote(string s)
238 {
239         float i, j, l;
240         l = strlen(s);
241         j = -1;
242         for(i = 0; i < l; ++i)
243         {
244                 string ch;
245                 ch = substring(s, i, 1);
246                 if(ch != " ") if(ch != "\"")
247                 {
248                         for(j = strlen(s) - i - 1; j > 0; --j)
249                         {
250                                 ch = substring(s, i+j, 1);
251                                 if(ch != " ") if(ch != "\"")
252                                         return substring(s, i, j+1);
253                         }
254                         return substring(s, i, 1);
255                 }
256         }
257         return "";
258 }
259
260 float MapInfo_Get_ByID(float i)
261 {
262         if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
263                 return 1;
264         return 0;
265 }
266
267 string _MapInfo_Map_worldspawn_music;
268
269 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
270 {
271         string fn;
272         float fh;
273         string s, k, v;
274         vector o;
275         float i;
276         float inWorldspawn;
277         float r;
278         float twoBaseModes;
279         float diameter, spawnpoints;
280         float spawnplaces;
281
282         vector mapMins, mapMaxs;
283
284         r = 1;
285         fn = strcat("maps/", pFilename, ".ent");
286         fh = fopen(fn, FILE_READ);
287         if(fh < 0)
288         {
289                 r = 2;
290                 fn = strcat("maps/", pFilename, ".bsp");
291                 fh = fopen(fn, FILE_READ);
292         }
293         if(fh < 0)
294                 return 0;
295         print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
296
297         inWorldspawn = 2;
298         MapInfo_Map_flags = 0;
299         MapInfo_Map_supportedGametypes = 0;
300         spawnpoints = 0;
301         spawnplaces = 0;
302         _MapInfo_Map_worldspawn_music = "";
303
304         for(;;)
305         {
306                 if not((s = fgets(fh)))
307                         break;
308                 if(inWorldspawn == 1)
309                         if(startsWith(s, "}"))
310                                 inWorldspawn = 0;
311                 k = unquote(car(s));
312                 v = unquote(cdr(s));
313                 if(inWorldspawn)
314                 {
315                         if(k == "classname" && v == "worldspawn")
316                                 inWorldspawn = 1;
317                         else if(k == "author")
318                                 MapInfo_Map_author = v;
319                         else if(k == "_description")
320                                 MapInfo_Map_description = v;
321                         else if(k == "music")
322                                 _MapInfo_Map_worldspawn_music = v;
323                         else if(k == "noise")
324                                 _MapInfo_Map_worldspawn_music = v;
325                         else if(k == "message")
326                         {
327                                 i = strstrofs(v, " by ", 0);
328                                 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
329                                 {
330                                         MapInfo_Map_title = substring(v, 0, i);
331                                         MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
332                                 }
333                                 else
334                                         MapInfo_Map_title = v;
335                         }
336                 }
337                 else
338                 {
339                         if(k == "origin")
340                         {
341                                 o = stov(strcat("'", v, "'"));
342                                 mapMins_x = min(mapMins_x, o_x);
343                                 mapMins_y = min(mapMins_y, o_y);
344                                 mapMins_z = min(mapMins_z, o_z);
345                                 mapMaxs_x = max(mapMaxs_x, o_x);
346                                 mapMaxs_y = max(mapMaxs_y, o_y);
347                                 mapMaxs_z = max(mapMaxs_z, o_z);
348                         }
349                         else if(k == "race_place")
350                         {
351                                 if(v == "1")
352                                         spawnplaces |= 1;
353                                 else if(v == "2")
354                                         spawnplaces |= 2;
355                                 else if(v == "3")
356                                         spawnplaces |= 4;
357                         }
358                         else if(k == "classname")
359                         {
360                                 if(v == "dom_controlpoint")
361                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
362                                 else if(v == "item_flag_team2")
363                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
364                                 else if(v == "team_CTF_blueflag")
365                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
366                                 else if(v == "runematch_spawn_point")
367                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
368                                 else if(v == "target_assault_roundend")
369                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
370                                 else if(v == "onslaught_generator")
371                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
372                                 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
373                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
374                                 else if(v == "info_player_team1")
375                                         ++spawnpoints;
376                                 else if(v == "info_player_team2")
377                                         ++spawnpoints;
378                                 else if(v == "info_player_start")
379                                         ++spawnpoints;
380                                 else if(v == "info_player_deathmatch")
381                                         ++spawnpoints;
382                                 else if(v == "trigger_race_checkpoint")
383                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
384                                 else if(v == "weapon_nex")
385                                         { }
386                                 else if(v == "weapon_railgun")
387                                         { }
388                                 else if(startsWith(v, "weapon_"))
389                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
390                         }
391                 }
392         }
393         if(inWorldspawn)
394         {
395                 print(fn, " ended still in worldspawn, BUG\n");
396                 return 0;
397         }
398         diameter = vlen(mapMaxs - mapMins);
399
400         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
401         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
402         {
403                 // we have a CTF-only or Assault-only map. Don't add other modes then,
404                 // as the map is too symmetric for them.
405         }
406         else
407         {
408                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
409                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;       // Rune always works
410                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
411
412                 if(spawnpoints >= 8  && diameter > 4096)
413                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
414                 if(                     diameter < 4096)
415                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
416                 if(spawnpoints >= 12 && diameter > 5120)
417                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
418         }
419
420         if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
421                 if(spawnplaces != 7)
422                         MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
423
424         dprint("-> diameter ",    ftos(diameter));
425         dprint(";  spawnpoints ", ftos(spawnpoints));
426         dprint(";  modes ",       ftos(MapInfo_Map_supportedGametypes), "\n");
427
428         fclose(fh);
429
430         return r;
431 }
432
433 void _MapInfo_Map_Reset()
434 {
435         MapInfo_Map_title = "<TITLE>";
436         MapInfo_Map_description = "<DESCRIPTION>";
437         MapInfo_Map_author = "<AUTHOR>";
438         MapInfo_Map_supportedGametypes = 0;
439         MapInfo_Map_supportedFeatures = 0;
440         MapInfo_Map_flags = 0;
441         MapInfo_Map_clientstuff = "";
442         MapInfo_Map_fog = "";
443         MapInfo_Map_mins = '0 0 0';
444         MapInfo_Map_maxs = '0 0 0';
445 }
446
447 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
448 {
449         string sa;
450         MapInfo_Map_supportedGametypes |= pThisType;
451         if(!(pThisType & pWantedType))
452                 return;
453         
454         if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT) // these modes don't use fraglimit
455         {
456                 cvar_set("fraglimit", "0");
457         }
458         else
459         {
460                 cvar_set("fraglimit", car(s));
461                 s = cdr(s);
462         }
463
464         cvar_set("timelimit", car(s));
465         s = cdr(s);
466
467         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
468         {
469                 sa = car(s); if(sa == "") sa = "2";
470                 cvar_set("g_tdm_teams", sa);
471                 s = cdr(s);
472         }
473
474         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
475         {
476                 sa = car(s); if(sa == "") sa = "3";
477                 cvar_set("g_keyhunt_teams", sa);
478                 s = cdr(s);
479         }
480
481         if(pWantedType == MAPINFO_TYPE_CTF)
482         {
483                 sa = car(s); if(sa == "") sa = "10";
484                 if(cvar("g_ctf_win_mode") < 2)
485                         cvar_set("fraglimit", sa);
486                 s = cdr(s);
487         }
488
489         if(pWantedType == MAPINFO_TYPE_RACE)
490         {
491                 sa = car(s); if(sa == "") sa = cvar_string("fraglimit");
492                 if(cvar("g_race_teams"))
493                         cvar_set("fraglimit", sa);
494                 s = cdr(s);
495         }
496
497         sa = car(s); if(sa == "") sa = "0";
498         cvar_set("leadlimit", sa);
499         s = cdr(s);
500 }
501
502 float MapInfo_Type_FromString(string t)
503 {
504         if     (t == "dm")      return MAPINFO_TYPE_DEATHMATCH;
505         else if(t == "tdm")     return MAPINFO_TYPE_TEAM_DEATHMATCH;
506         else if(t == "dom")     return MAPINFO_TYPE_DOMINATION;
507         else if(t == "ctf")     return MAPINFO_TYPE_CTF;
508         else if(t == "rune")    return MAPINFO_TYPE_RUNEMATCH;
509         else if(t == "lms")     return MAPINFO_TYPE_LMS;
510         else if(t == "arena")   return MAPINFO_TYPE_ARENA;
511         else if(t == "kh")      return MAPINFO_TYPE_KEYHUNT;
512         else if(t == "as")      return MAPINFO_TYPE_ASSAULT;
513         else if(t == "ons")     return MAPINFO_TYPE_ONSLAUGHT;
514         else if(t == "race")    return MAPINFO_TYPE_RACE;
515         else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
516         else if(t == "all")     return MAPINFO_TYPE_ALL;
517         else                    return 0;
518 }
519
520 // load info about a map by name into the MapInfo_Map_* globals
521 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
522 {
523         string fn;
524         string s, t;
525         float fh, fh2;
526         float r, f, n, i;
527         string acl;
528
529         acl = cvar_string("g_mapinfo_settemp_acl");
530
531         if(strstrofs(pFilename, "/", 0) >= 0)
532         {
533                 print("Invalid character in map name, ignored\n");
534                 return 0;
535         }
536
537         if(pGametypeToSet == 0)
538                 if(MapInfo_Cache_Retrieve(pFilename))
539                         return 1;
540
541         r = 1;
542
543         MapInfo_Map_bspname = pFilename;
544
545         // default all generic fields so they have "good" values in case something fails
546         fn = strcat("maps/", pFilename, ".mapinfo");
547         fh = fopen(fn, FILE_READ);
548         if(fh < 0)
549         {
550                 if(!pAllowGenerate)
551                         return 0;
552                 _MapInfo_Map_Reset();
553                 r = _MapInfo_Generate(pFilename);
554                 if(!r)
555                         return 0;
556                 fh = fopen(fn, FILE_WRITE);
557                 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
558                 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
559                 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
560                 if(_MapInfo_Map_worldspawn_music != "")
561                 {
562                         if(
563                                 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
564                                 ||
565                                 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
566                         )
567                                 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
568                         else
569                                 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
570                 }
571                 else
572                 {
573                         n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
574                         s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
575                         for(;;)
576                         {
577                                 i = floor(random() * n);
578                                 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
579                                         break;
580                         }
581                         fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
582                 }
583                 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
584                         fputs(fh, "has weapons\n");
585                 else
586                         fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
587                 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
588                         fputs(fh, "frustrating\n");
589                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)      fputs(fh, "type dm 30 20\n");
590                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
591                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION)      fputs(fh, "type dom 200 20\n");
592                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF)             fputs(fh, "type ctf 300 20 10\n");
593                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH)       fputs(fh, "type rune 200 20\n");
594                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS)             fputs(fh, "type lms 9 20\n");
595                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA)           fputs(fh, "type arena 10 20\n");
596                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT)         fputs(fh, "type kh 1000 20 3\n");
597                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT)         fputs(fh, "type as 20\n");
598                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)            fputs(fh, "type race 5 20 15\n");
599                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT)       fputs(fh, "type ons 20\n");
600                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_NEXBALL)         fputs(fh, "type nexball 5 20\n");
601
602                 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
603                 if(fh2 >= 0)
604                 {
605                         fclose(fh2);
606                         fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
607                 }
608
609                 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
610                 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
611                 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
612                 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
613                 fputs(fh, "// optional: hidden\n");
614
615                 fclose(fh);
616                 r = 2;
617                 // return r;
618                 fh = fopen(fn, FILE_READ);
619                 if(fh < 0)
620                         error("... but I just wrote it!");
621         }
622
623         _MapInfo_Map_Reset();
624         for(;;)
625         {
626                 if not((s = fgets(fh)))
627                         break;
628
629                 // catch different sorts of comments
630                 if(s == "")                    // empty lines
631                         continue;
632                 if(substring(s, 0, 1) == "#")  // UNIX style
633                         continue;
634                 if(substring(s, 0, 2) == "//") // C++ style
635                         continue;
636                 if(substring(s, 0, 1) == "_")  // q3map style
637                         continue;
638
639                 t = car(s); s = cdr(s);
640                 if(t == "title")
641                         MapInfo_Map_title = s;
642                 else if(t == "description")
643                         MapInfo_Map_description = s;
644                 else if(t == "author")
645                         MapInfo_Map_author = s;
646                 else if(t == "has")
647                 {
648                         t = car(s); s = cdr(s);
649                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
650                         else
651                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
652                 }
653                 else if(t == "hidden")
654                 {
655                         MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
656                 }
657                 else if(t == "forbidden")
658                 {
659                         MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
660                 }
661                 else if(t == "frustrating")
662                 {
663                         MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
664                 }
665                 else if(t == "type")
666                 {
667                         t = car(s); s = cdr(s);
668                         f = MapInfo_Type_FromString(t);
669                         if(f)
670                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
671                         else
672                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
673                 }
674                 else if(t == "size")
675                 {
676                         float a, b, c, d, e;
677                         t = car(s); s = cdr(s); a = stof(t);
678                         t = car(s); s = cdr(s); b = stof(t);
679                         t = car(s); s = cdr(s); c = stof(t);
680                         t = car(s); s = cdr(s); d = stof(t);
681                         t = car(s); s = cdr(s); e = stof(t);
682                         if(s == "")
683                                 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");
684                         else
685                         {
686                                 t = car(s); s = cdr(s); f = stof(t);
687                                 if(s != "")
688                                         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");
689                                 else
690                                 {
691                                         if(a >= d || b >= e || c >= f)
692                                                 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
693                                         else
694                                         {
695                                                 MapInfo_Map_mins_x = a;
696                                                 MapInfo_Map_mins_y = b;
697                                                 MapInfo_Map_mins_z = c;
698                                                 MapInfo_Map_maxs_x = d;
699                                                 MapInfo_Map_maxs_y = e;
700                                                 MapInfo_Map_maxs_z = f;
701                                         }
702                                 }
703                         }
704                 }
705                 else if(t == "settemp_for_type")
706                 {
707                         t = car(s); s = cdr(s);
708                         if((f = MapInfo_Type_FromString(t)))
709                         {
710                                 if(f & pGametypeToSet)
711                                 {
712                                         t = car(s); s = cdr(s);
713                                         if not(cvar_value_issafe(t))
714                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
715                                         else if not (cvar_value_issafe(s))
716                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
717                                         else if(substring(t, 0, 10) == "g_mapinfo_")
718                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
719                                         else if(matchacl(acl, t) <= 0)
720                                                 print("Map ", pFilename, " contains a denied setting, ignored\n");
721                                         else
722                                         {
723                                                 dprint("Applying temporary setting ", t, " := ", s, "\n");
724                                                 cvar_settemp(t, s);
725                                         }
726                                 }
727                         }
728                         else
729                         {
730                                 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
731                         }
732                 }
733                 else if(t == "clientsettemp_for_type")
734                 {
735                         t = car(s); s = cdr(s);
736                         if((f = MapInfo_Type_FromString(t)))
737                         {
738                                 if(f & pGametypeToSet)
739                                 {
740                                         t = car(s); s = cdr(s);
741                                         if not(cvar_value_issafe(t))
742                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
743                                         else if not (cvar_value_issafe(s))
744                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
745                                         else
746                                         {
747                                                 dprint("Applying temporary client setting ", t, " := ", s, "\n");
748                                                 MapInfo_Map_clientstuff = strcat(
749                                                         MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
750                                                 );
751                                         }
752                                 }
753                         }
754                         else
755                         {
756                                 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
757                         }
758                 }
759                 else if(t == "fog")
760                 {
761                         if not(cvar_value_issafe(t))
762                                 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
763                         else
764                                 MapInfo_Map_fog = s;
765                 }
766                 else if(t == "cdtrack")
767                 {
768                         if(pGametypeToSet)
769                         {
770                                 if not(cvar_value_issafe(t))
771                                         print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
772                                 else
773                                         MapInfo_Map_clientstuff = strcat(
774                                                 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
775                                         );
776                         }
777                 }
778                 else
779                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
780         }
781         fclose(fh);
782
783         if(!MapInfo_Map_supportedGametypes)
784                 _MapInfo_Map_ApplyGametype("30 20", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
785
786         if(pGametypeToSet)
787                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
788                         error("Can't select the requested game type. Bailing out.");
789         MapInfo_Cache_Store();
790         if(MapInfo_Map_supportedGametypes != 0)
791                 return r;
792         dprint("Map ", pFilename, " supports no game types, ignored\n");
793         return 0;
794 }
795
796 float MapInfo_FindName(string s)
797 {
798         // if there is exactly one map of prefix s, return it
799         // if not, return the null string
800         // note that DP sorts glob results... so I can use a binary search
801         float l, r, m, cmp;
802         l = 0;
803         r = MapInfo_count;
804         // invariants: r is behind s, l-1 is equal or before
805         while(l != r)
806         {
807                 m = floor((l + r) / 2);
808                 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
809                 cmp = strcasecmp(MapInfo_FindName_match, s);
810                 if(cmp == 0)
811                         return m; // found and good
812                 if(cmp < 0)
813                         l = m + 1; // l-1 is before s
814                 else
815                         r = m; // behind s
816         }
817         MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
818         MapInfo_FindName_firstResult = l;
819         // r == l, so: l is behind s, l-1 is before
820         // SO: if there is any, l is the one with the right prefix
821         //     and l+1 may be one too
822         if(l == MapInfo_count)
823         {
824                 MapInfo_FindName_match = string_null;
825                 MapInfo_FindName_firstResult = -1;
826                 return -1; // no MapInfo_FindName_match, behind last item
827         }
828         if(!startsWithNocase(MapInfo_FindName_match, s))
829         {
830                 MapInfo_FindName_match = string_null;
831                 MapInfo_FindName_firstResult = -1;
832                 return -1; // wrong prefix
833         }
834         if(l == MapInfo_count - 1)
835                 return l; // last one, nothing can follow => unique
836         if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
837         {
838                 MapInfo_FindName_match = string_null;
839                 return -1; // ambigous MapInfo_FindName_match
840         }
841         return l;
842 }
843
844 string MapInfo_FixName(string s)
845 {
846         MapInfo_FindName(s);
847         return MapInfo_FindName_match;
848 }
849
850 float MapInfo_CurrentFeatures()
851 {
852         float req;
853         req = 0;
854         if(!(cvar("g_lms") || cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena") || !cvar("g_pickup_items") || cvar("g_race") || cvar("g_nexball")))
855                 req |= MAPINFO_FEATURE_WEAPONS;
856         return req;
857 }
858
859 float MapInfo_CurrentGametype()
860 {
861         if(cvar("g_domination"))
862                 return MAPINFO_TYPE_DOMINATION;
863         else if(cvar("g_ctf"))
864                 return MAPINFO_TYPE_CTF;
865         else if(cvar("g_runematch"))
866                 return MAPINFO_TYPE_RUNEMATCH;
867         else if(cvar("g_tdm"))
868                 return MAPINFO_TYPE_TEAM_DEATHMATCH;
869         else if(cvar("g_assault"))
870                 return MAPINFO_TYPE_ASSAULT;
871         else if(cvar("g_lms"))
872                 return MAPINFO_TYPE_LMS;
873         else if(cvar("g_arena"))
874                 return MAPINFO_TYPE_ARENA;
875         else if(cvar("g_keyhunt"))
876                 return MAPINFO_TYPE_KEYHUNT;
877         else if(cvar("g_onslaught"))
878                 return MAPINFO_TYPE_ONSLAUGHT;
879         else if(cvar("g_race"))
880                 return MAPINFO_TYPE_RACE;
881         else if(cvar("g_nexball"))
882                 return MAPINFO_TYPE_NEXBALL;
883         else
884                 return MAPINFO_TYPE_DEATHMATCH;
885 }
886
887 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
888 {
889         if(!MapInfo_Get_ByName(s, 1, 0))
890                 return 0;
891         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
892                 return 0;
893         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
894                 return 0;
895         return 1;
896 }
897
898 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
899 {
900         float r;
901         r = _MapInfo_CheckMap(s);
902         MapInfo_ClearTemps();
903         return r;
904 }
905
906 string MapInfo_GetGameTypeCvar(float t)
907 {
908         switch(t)
909         {
910                 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
911                 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
912                 case MAPINFO_TYPE_DOMINATION: return "g_domination";
913                 case MAPINFO_TYPE_CTF: return "g_ctf";
914                 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
915                 case MAPINFO_TYPE_LMS: return "g_lms";
916                 case MAPINFO_TYPE_ARENA: return "g_arena";
917                 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
918                 case MAPINFO_TYPE_ASSAULT: return "g_assault";
919                 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
920                 case MAPINFO_TYPE_RACE: return "g_race";
921                 default: return "";
922         }
923 }
924
925 void MapInfo_SwitchGameType(float t)
926 {
927         cvar_set("gamecfg",      "0");
928         cvar_set("g_dm",         (t == MAPINFO_TYPE_DEATHMATCH)      ? "1" : "0");
929         cvar_set("g_tdm",        (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
930         cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION)      ? "1" : "0");
931         cvar_set("g_ctf",        (t == MAPINFO_TYPE_CTF)             ? "1" : "0");
932         cvar_set("g_runematch",  (t == MAPINFO_TYPE_RUNEMATCH)       ? "1" : "0");
933         cvar_set("g_lms",        (t == MAPINFO_TYPE_LMS)             ? "1" : "0");
934         cvar_set("g_arena",      (t == MAPINFO_TYPE_ARENA)           ? "1" : "0");
935         cvar_set("g_keyhunt",    (t == MAPINFO_TYPE_KEYHUNT)         ? "1" : "0");
936         cvar_set("g_assault",    (t == MAPINFO_TYPE_ASSAULT)         ? "1" : "0");
937         cvar_set("g_onslaught",  (t == MAPINFO_TYPE_ONSLAUGHT)       ? "1" : "0");
938         cvar_set("g_race",       (t == MAPINFO_TYPE_RACE)            ? "1" : "0");
939         cvar_set("g_nexball",    (t == MAPINFO_TYPE_NEXBALL)         ? "1" : "0");
940 }
941
942 void MapInfo_LoadMap(string s)
943 {
944         MapInfo_Map_supportedGametypes = 0;
945         if(!MapInfo_CheckMap(s))
946         {
947                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
948                 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
949         }
950         localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
951 }
952
953 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
954 {
955         string out;
956         float i;
957
958         // to make absolutely sure:
959         MapInfo_Enumerate();
960         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
961
962         out = "";
963         for(i = 0; i < MapInfo_count; ++i)
964                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
965         return substring(out, 1, strlen(out) - 1);
966 }
967
968 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
969 {
970         float t;
971         if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
972         {
973                 if(MapInfo_Map_supportedGametypes <= 0)
974                         error("Mapinfo system is not functional at all. BAILED OUT.\n");
975
976                 t = 1;
977                 while(!(MapInfo_Map_supportedGametypes & 1))
978                 {
979                         t *= 2;
980                         MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
981                 }
982                 // t is now a supported mode!
983                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
984                 MapInfo_SwitchGameType(t);
985         }
986         cvar_settemp_restore();
987         MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
988 }
989
990 void MapInfo_ClearTemps()
991 {
992         MapInfo_Map_bspname = string_null;
993         MapInfo_Map_title = string_null;
994         MapInfo_Map_description = string_null;
995         MapInfo_Map_author = string_null;
996         MapInfo_Map_clientstuff = string_null;
997         MapInfo_Map_supportedGametypes = 0;
998         MapInfo_Map_supportedFeatures = 0;
999 }
1000
1001 void MapInfo_Shutdown()
1002 {
1003         MapInfo_ClearTemps();
1004         MapInfo_Filter_Free();
1005         MapInfo_Cache_Destroy();
1006         if(_MapInfo_globopen)
1007         {
1008                 search_end(_MapInfo_globhandle);
1009                 _MapInfo_globhandle = -1;
1010                 _MapInfo_globopen = FALSE;
1011         }
1012 }
1013
1014 float MapInfo_ForbiddenFlags()
1015 {
1016         float f;
1017         f = MAPINFO_FLAG_FORBIDDEN;
1018
1019 #ifndef MENUQC
1020         if not(cvar("g_maplist_allow_hidden"))
1021 #endif
1022                 f |= MAPINFO_FLAG_HIDDEN;
1023
1024         if not(cvar("g_maplist_allow_frustrating"))
1025                 f |= MAPINFO_FLAG_FRUSTRATING;
1026
1027         return f;
1028 }
1029
1030 float MapInfo_RequiredFlags()
1031 {
1032         float f;
1033         f = 0;
1034
1035         if(cvar("g_maplist_allow_frustrating") > 1)
1036                 f |= MAPINFO_FLAG_FRUSTRATING;
1037
1038         return f;
1039 }