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