]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/mapinfo.qc
mapinfo: do an explicit heapsort for the mapinfo list, to fix problems with "-" in...
[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
250         vector mapMins, mapMaxs;
251
252         r = 1;
253         fn = strcat("maps/", pFilename, ".ent");
254         fh = fopen(fn, FILE_READ);
255         if(fh < 0)
256         {
257                 r = 2;
258                 fn = strcat("maps/", pFilename, ".bsp");
259                 fh = fopen(fn, FILE_READ);
260         }
261         if(fh < 0)
262                 return 0;
263         print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
264
265         inWorldspawn = 2;
266         MapInfo_Map_supportedGametypes = 0;
267         spawnpoints = 0;
268         _MapInfo_Map_worldspawn_music = "";
269
270         for(;;)
271         {
272                 if not((s = fgets(fh)))
273                         break;
274                 if(inWorldspawn == 1)
275                         if(startsWith(s, "}"))
276                                 inWorldspawn = 0;
277                 k = unquote(car(s));
278                 v = unquote(cdr(s));
279                 if(inWorldspawn)
280                 {
281                         if(k == "classname" && v == "worldspawn")
282                                 inWorldspawn = 1;
283                         else if(k == "author")
284                                 MapInfo_Map_author = v;
285                         else if(k == "_description")
286                                 MapInfo_Map_description = v;
287                         else if(k == "music")
288                                 _MapInfo_Map_worldspawn_music = v;
289                         else if(k == "noise")
290                                 _MapInfo_Map_worldspawn_music = v;
291                         else if(k == "message")
292                         {
293                                 i = strstrofs(v, " by ", 0);
294                                 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
295                                 {
296                                         MapInfo_Map_title = substring(v, 0, i);
297                                         MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
298                                 }
299                                 else
300                                         MapInfo_Map_title = v;
301                         }
302                 }
303                 else
304                 {
305                         if(k == "origin")
306                         {
307                                 o = stov(strcat("'", v, "'"));
308                                 mapMins_x = min(mapMins_x, o_x);
309                                 mapMins_y = min(mapMins_y, o_y);
310                                 mapMins_z = min(mapMins_z, o_z);
311                                 mapMaxs_x = max(mapMaxs_x, o_x);
312                                 mapMaxs_y = max(mapMaxs_y, o_y);
313                                 mapMaxs_z = max(mapMaxs_z, o_z);
314                         }
315                         else if(k == "classname")
316                         {
317                                 if(v == "dom_controlpoint")
318                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
319                                 else if(v == "item_flag_team2")
320                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
321                                 else if(v == "team_CTF_blueflag")
322                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
323                                 else if(v == "runematch_spawn_point")
324                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
325                                 else if(v == "target_assault_roundend")
326                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
327                                 else if(v == "onslaught_generator")
328                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
329                                 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
330                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
331                                 else if(v == "info_player_team1")
332                                         ++spawnpoints;
333                                 else if(v == "info_player_team2")
334                                         ++spawnpoints;
335                                 else if(v == "info_player_start")
336                                         ++spawnpoints;
337                                 else if(v == "info_player_deathmatch")
338                                         ++spawnpoints;
339                                 else if(v == "trigger_race_checkpoint")
340                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
341                                 else if(v == "weapon_nex")
342                                         { }
343                                 else if(v == "weapon_railgun")
344                                         { }
345                                 else if(startsWith(v, "weapon_"))
346                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
347                         }
348                 }
349         }
350         if(inWorldspawn)
351         {
352                 print(fn, " ended still in worldspawn, BUG\n");
353                 return 0;
354         }
355         diameter = vlen(mapMaxs - mapMins);
356
357         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE);
358         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
359         {
360                 // we have a CTF-only or Assault-only map. Don't add other modes then,
361                 // as the map is too symmetric for them.
362         }
363         else
364         {
365                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
366                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;       // Rune always works
367                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
368
369                 if(spawnpoints >= 8  && diameter > 4096)
370                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
371                 if(                     diameter < 4096)
372                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
373                 if(spawnpoints >= 12 && diameter > 5120)
374                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
375         }
376
377         dprint("-> diameter ",    ftos(diameter));
378         dprint(";  spawnpoints ", ftos(spawnpoints));
379         dprint(";  modes ",       ftos(MapInfo_Map_supportedGametypes), "\n");
380
381         fclose(fh);
382
383         return r;
384 }
385
386 void _MapInfo_Map_Reset()
387 {
388         MapInfo_Map_title = "<TITLE>";
389         MapInfo_Map_description = "<DESCRIPTION>";
390         MapInfo_Map_author = "<AUTHOR>";
391         MapInfo_Map_supportedGametypes = 0;
392         MapInfo_Map_supportedFeatures = 0;
393         MapInfo_Map_flags = 0;
394         MapInfo_Map_clientstuff = "";
395         MapInfo_Map_fog = "";
396         MapInfo_Map_mins = '0 0 0';
397         MapInfo_Map_maxs = '0 0 0';
398 }
399
400 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
401 {
402         string sa;
403         MapInfo_Map_supportedGametypes |= pThisType;
404         if(!(pThisType & pWantedType))
405                 return;
406         
407         if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT) // these modes don't use fraglimit
408         {
409                 cvar_set("fraglimit", "0");
410         }
411         else
412         {
413                 cvar_set("fraglimit", car(s));
414                 s = cdr(s);
415         }
416
417         cvar_set("timelimit", car(s));
418         s = cdr(s);
419
420         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
421         {
422                 sa = car(s); if(sa == "") sa = "2";
423                 cvar_set("g_tdm_teams", sa);
424                 s = cdr(s);
425         }
426
427         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
428         {
429                 sa = car(s); if(sa == "") sa = "3";
430                 cvar_set("g_keyhunt_teams", sa);
431                 s = cdr(s);
432         }
433
434         if(pWantedType == MAPINFO_TYPE_CTF)
435         {
436                 sa = car(s); if(sa == "") sa = "10";
437                 if(cvar("g_ctf_win_mode") < 2)
438                         cvar_set("fraglimit", sa);
439                 s = cdr(s);
440         }
441
442         if(pWantedType == MAPINFO_TYPE_RACE)
443         {
444                 sa = car(s); if(sa == "") sa = cvar_string("fraglimit");
445                 if(cvar("g_race_teams"))
446                         cvar_set("fraglimit", sa);
447                 s = cdr(s);
448         }
449 }
450
451 float MapInfo_Type_FromString(string t)
452 {
453         if     (t == "dm")      return MAPINFO_TYPE_DEATHMATCH;
454         else if(t == "tdm")     return MAPINFO_TYPE_TEAM_DEATHMATCH;
455         else if(t == "dom")     return MAPINFO_TYPE_DOMINATION;
456         else if(t == "ctf")     return MAPINFO_TYPE_CTF;
457         else if(t == "rune")    return MAPINFO_TYPE_RUNEMATCH;
458         else if(t == "lms")     return MAPINFO_TYPE_LMS;
459         else if(t == "arena")   return MAPINFO_TYPE_ARENA;
460         else if(t == "kh")      return MAPINFO_TYPE_KEYHUNT;
461         else if(t == "as")      return MAPINFO_TYPE_ASSAULT;
462         else if(t == "ons")     return MAPINFO_TYPE_ONSLAUGHT;
463         else if(t == "race")    return MAPINFO_TYPE_RACE;
464         else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
465         else if(t == "all")     return MAPINFO_TYPE_ALL;
466         else                    return 0;
467 }
468
469 // load info about a map by name into the MapInfo_Map_* globals
470 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
471 {
472         string fn;
473         string s, t;
474         float fh, fh2;
475         float r, f, n, i;
476
477         if(strstrofs(pFilename, "/", 0) >= 0)
478         {
479                 print("Invalid character in map name, ignored\n");
480                 return 0;
481         }
482
483         if(pGametypeToSet == 0)
484                 if(MapInfo_Cache_Retrieve(pFilename))
485                         return 1;
486
487         r = 1;
488
489         MapInfo_Map_bspname = pFilename;
490
491         // default all generic fields so they have "good" values in case something fails
492         fn = strcat("maps/", pFilename, ".mapinfo");
493         fh = fopen(fn, FILE_READ);
494         if(fh < 0)
495         {
496                 if(!pAllowGenerate)
497                         return 0;
498                 _MapInfo_Map_Reset();
499                 r = _MapInfo_Generate(pFilename);
500                 if(!r)
501                         return 0;
502                 fh = fopen(fn, FILE_WRITE);
503                 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
504                 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
505                 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
506                 if(_MapInfo_Map_worldspawn_music != "")
507                 {
508                         if(
509                                 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
510                                 ||
511                                 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
512                         )
513                                 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
514                         else
515                                 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
516                 }
517                 else
518                 {
519                         n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
520                         s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
521                         for(;;)
522                         {
523                                 i = floor(random() * n);
524                                 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
525                                         break;
526                         }
527                         fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
528                 }
529                 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)    
530                         fputs(fh, "has weapons\n");
531                 else
532                         fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
533                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)      fputs(fh, "type dm 30 20\n");
534                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
535                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION)      fputs(fh, "type dom 200 20\n");
536                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF)             fputs(fh, "type ctf 300 20 10\n");
537                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH)       fputs(fh, "type rune 200 20\n");
538                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS)             fputs(fh, "type lms 9 20\n");
539                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA)           fputs(fh, "type arena 10 20\n");
540                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT)         fputs(fh, "type kh 1000 20 3\n");
541                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT)         fputs(fh, "type as 20\n");
542                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)            fputs(fh, "type race 5 20 15\n");
543                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT)       fputs(fh, "type ons 20\n");
544                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_NEXBALL)         fputs(fh, "type nexball 5 20\n");
545
546                 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
547                 if(fh2 >= 0)
548                 {
549                         fclose(fh2);
550                         fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
551                 }
552
553                 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
554                 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
555                 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
556                 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
557                 fputs(fh, "// optional: hidden\n");
558
559                 fclose(fh);
560                 r = 2;
561                 // return r;
562                 fh = fopen(fn, FILE_READ);
563                 if(fh < 0)
564                         error("... but I just wrote it!");
565         }
566
567         _MapInfo_Map_Reset();
568         for(;;)
569         {
570                 if not((s = fgets(fh)))
571                         break;
572
573                 // catch different sorts of comments
574                 if(s == "")                    // empty lines
575                         continue;
576                 if(substring(s, 0, 1) == "#")  // UNIX style
577                         continue;
578                 if(substring(s, 0, 2) == "//") // C++ style
579                         continue;
580                 if(substring(s, 0, 1) == "_")  // q3map style
581                         continue;
582
583                 t = car(s); s = cdr(s);
584                 if(t == "title")
585                         MapInfo_Map_title = s;
586                 else if(t == "description")
587                         MapInfo_Map_description = s;
588                 else if(t == "author")
589                         MapInfo_Map_author = s;
590                 else if(t == "has")
591                 {
592                         t = car(s); s = cdr(s);
593                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
594                         else
595                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
596                 }
597                 else if(t == "hidden")
598                 {
599                         MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
600                 }
601                 else if(t == "forbidden")
602                 {
603                         MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
604                 }
605                 else if(t == "type")
606                 {
607                         t = car(s); s = cdr(s);
608                         f = MapInfo_Type_FromString(t);
609                         if(f)
610                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
611                         else
612                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
613                 }
614                 else if(t == "size")
615                 {
616                         float a, b, c, d, e;
617                         t = car(s); s = cdr(s); a = stof(t);
618                         t = car(s); s = cdr(s); b = stof(t);
619                         t = car(s); s = cdr(s); c = stof(t);
620                         t = car(s); s = cdr(s); d = stof(t);
621                         t = car(s); s = cdr(s); e = stof(t);
622                         if(s == "")
623                                 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");
624                         else
625                         {
626                                 t = car(s); s = cdr(s); f = stof(t);
627                                 if(s != "")
628                                         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");
629                                 else
630                                 {
631                                         if(a >= d || b >= e || c >= f)
632                                                 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
633                                         else
634                                         {
635                                                 MapInfo_Map_mins_x = a;
636                                                 MapInfo_Map_mins_y = b;
637                                                 MapInfo_Map_mins_z = c;
638                                                 MapInfo_Map_maxs_x = d;
639                                                 MapInfo_Map_maxs_y = e;
640                                                 MapInfo_Map_maxs_z = f;
641                                         }
642                                 }
643                         }
644                 }
645                 else if(t == "settemp_for_type")
646                 {
647                         t = car(s); s = cdr(s);
648                         if((f = MapInfo_Type_FromString(t)))
649                         {
650                                 if(f & pGametypeToSet)
651                                 {
652                                         t = car(s); s = cdr(s);
653                                         if not(cvar_value_issafe(t))
654                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
655                                         else if not (cvar_value_issafe(s))
656                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
657                                         else
658                                         {
659                                                 dprint("Applying temporary setting ", t, " := ", s, "\n");
660                                                 cvar_settemp(t, s);
661                                         }
662                                 }
663                         }
664                         else
665                         {
666                                 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
667                         }
668                 }
669                 else if(t == "clientsettemp_for_type")
670                 {
671                         t = car(s); s = cdr(s);
672                         if((f = MapInfo_Type_FromString(t)))
673                         {
674                                 if(f & pGametypeToSet)
675                                 {
676                                         t = car(s); s = cdr(s);
677                                         if not(cvar_value_issafe(t))
678                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
679                                         else if not (cvar_value_issafe(s))
680                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
681                                         else
682                                         {
683                                                 dprint("Applying temporary client setting ", t, " := ", s, "\n");
684                                                 MapInfo_Map_clientstuff = strcat(
685                                                         MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
686                                                 );
687                                         }
688                                 }
689                         }
690                         else
691                         {
692                                 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
693                         }
694                 }
695                 else if(t == "fog")
696                 {
697                         if not(cvar_value_issafe(t))
698                                 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
699                         else
700                                 MapInfo_Map_fog = s;
701                 }
702                 else if(t == "cdtrack")
703                 {
704                         if(pGametypeToSet)
705                         {
706                                 if not(cvar_value_issafe(t))
707                                         print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
708                                 else
709                                         MapInfo_Map_clientstuff = strcat(
710                                                 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
711                                         );
712                         }
713                 }
714                 else
715                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
716         }
717         fclose(fh);
718
719         if(!MapInfo_Map_supportedGametypes)
720                 _MapInfo_Map_ApplyGametype("30 20", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
721
722         if(pGametypeToSet)
723                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
724                         error("Can't select the requested game type. Bailing out.");
725         MapInfo_Cache_Store();
726         if(MapInfo_Map_supportedGametypes != 0)
727                 return r;
728         dprint("Map ", pFilename, " supports no game types, ignored\n");
729         return 0;
730 }
731
732 float MapInfo_FindName(string s)
733 {
734         // if there is exactly one map of prefix s, return it
735         // if not, return the null string
736         // note that DP sorts glob results... so I can use a binary search
737         float l, r, m, cmp;
738         l = 0;
739         r = MapInfo_count;
740         // invariants: r is behind s, l-1 is equal or before
741         while(l != r)
742         {
743                 m = floor((l + r) / 2);
744                 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
745                 cmp = strcasecmp(MapInfo_FindName_match, s);
746                 if(cmp == 0)
747                         return m; // found and good
748                 if(cmp < 0)
749                         l = m + 1; // l-1 is before s
750                 else
751                         r = m; // behind s
752         }
753         MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
754         MapInfo_FindName_firstResult = l;
755         // r == l, so: l is behind s, l-1 is before
756         // SO: if there is any, l is the one with the right prefix
757         //     and l+1 may be one too
758         if(l == MapInfo_count)
759         {
760                 MapInfo_FindName_match = string_null;
761                 MapInfo_FindName_firstResult = -1;
762                 return -1; // no MapInfo_FindName_match, behind last item
763         }
764         if(!startsWithNocase(MapInfo_FindName_match, s))
765         {
766                 MapInfo_FindName_match = string_null;
767                 MapInfo_FindName_firstResult = -1;
768                 return -1; // wrong prefix
769         }
770         if(l == MapInfo_count - 1)
771                 return l; // last one, nothing can follow => unique
772         if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
773         {
774                 MapInfo_FindName_match = string_null;
775                 return -1; // ambigous MapInfo_FindName_match
776         }
777         return l;
778 }
779
780 string MapInfo_FixName(string s)
781 {
782         MapInfo_FindName(s);
783         return MapInfo_FindName_match;
784 }
785
786 float MapInfo_CurrentFeatures()
787 {
788         float req;
789         req = 0;
790         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")))
791                 req |= MAPINFO_FEATURE_WEAPONS;
792         return req;
793 }
794
795 float MapInfo_CurrentGametype()
796 {
797         if(cvar("g_domination"))
798                 return MAPINFO_TYPE_DOMINATION;
799         else if(cvar("g_ctf"))
800                 return MAPINFO_TYPE_CTF;
801         else if(cvar("g_runematch"))
802                 return MAPINFO_TYPE_RUNEMATCH;
803         else if(cvar("g_tdm"))
804                 return MAPINFO_TYPE_TEAM_DEATHMATCH;
805         else if(cvar("g_assault"))
806                 return MAPINFO_TYPE_ASSAULT;
807         else if(cvar("g_lms"))
808                 return MAPINFO_TYPE_LMS;
809         else if(cvar("g_arena"))
810                 return MAPINFO_TYPE_ARENA;
811         else if(cvar("g_keyhunt"))
812                 return MAPINFO_TYPE_KEYHUNT;
813         else if(cvar("g_onslaught"))
814                 return MAPINFO_TYPE_ONSLAUGHT;
815         else if(cvar("g_race"))
816                 return MAPINFO_TYPE_RACE;
817         else if(cvar("g_nexball"))
818                 return MAPINFO_TYPE_NEXBALL;
819         else
820                 return MAPINFO_TYPE_DEATHMATCH;
821 }
822
823 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
824 {
825         if(!MapInfo_Get_ByName(s, 1, 0))
826                 return 0;
827         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
828                 return 0;
829         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
830                 return 0;
831         return 1;
832 }
833
834 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
835 {
836         float r;
837         r = _MapInfo_CheckMap(s);
838         MapInfo_ClearTemps();
839         return r;
840 }
841
842 string MapInfo_GetGameTypeCvar(float t)
843 {
844         switch(t)
845         {
846                 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
847                 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
848                 case MAPINFO_TYPE_DOMINATION: return "g_domination";
849                 case MAPINFO_TYPE_CTF: return "g_ctf";
850                 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
851                 case MAPINFO_TYPE_LMS: return "g_lms";
852                 case MAPINFO_TYPE_ARENA: return "g_arena";
853                 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
854                 case MAPINFO_TYPE_ASSAULT: return "g_assault";
855                 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
856                 case MAPINFO_TYPE_RACE: return "g_race";
857                 default: return "";
858         }
859 }
860
861 void MapInfo_SwitchGameType(float t)
862 {
863         cvar_set("gamecfg",      "0");
864         cvar_set("g_dm",         (t == MAPINFO_TYPE_DEATHMATCH)      ? "1" : "0");
865         cvar_set("g_tdm",        (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
866         cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION)      ? "1" : "0");
867         cvar_set("g_ctf",        (t == MAPINFO_TYPE_CTF)             ? "1" : "0");
868         cvar_set("g_runematch",  (t == MAPINFO_TYPE_RUNEMATCH)       ? "1" : "0");
869         cvar_set("g_lms",        (t == MAPINFO_TYPE_LMS)             ? "1" : "0");
870         cvar_set("g_arena",      (t == MAPINFO_TYPE_ARENA)           ? "1" : "0");
871         cvar_set("g_keyhunt",    (t == MAPINFO_TYPE_KEYHUNT)         ? "1" : "0");
872         cvar_set("g_assault",    (t == MAPINFO_TYPE_ASSAULT)         ? "1" : "0");
873         cvar_set("g_onslaught",  (t == MAPINFO_TYPE_ONSLAUGHT)       ? "1" : "0");
874         cvar_set("g_race",       (t == MAPINFO_TYPE_RACE)            ? "1" : "0");
875         cvar_set("g_nexball",    (t == MAPINFO_TYPE_NEXBALL)         ? "1" : "0");
876 }
877
878 void MapInfo_LoadMap(string s)
879 {
880         MapInfo_Map_supportedGametypes = 0;
881         if(!MapInfo_CheckMap(s))
882         {
883                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
884                 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
885         }
886         localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
887 }
888
889 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
890 {
891         string out;
892         float i;
893
894         // to make absolutely sure:
895         MapInfo_Enumerate();
896         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
897
898         out = "";
899         for(i = 0; i < MapInfo_count; ++i)
900                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
901         return substring(out, 1, strlen(out) - 1);
902 }
903
904 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
905 {
906         float t;
907         if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
908         {
909                 if(MapInfo_Map_supportedGametypes <= 0)
910                         error("Mapinfo system is not functional at all. BAILED OUT.\n");
911
912                 t = 1;
913                 while(!(MapInfo_Map_supportedGametypes & 1))
914                 {
915                         t *= 2;
916                         MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
917                 }
918                 // t is now a supported mode!
919                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
920                 MapInfo_SwitchGameType(t);
921         }
922         cvar_settemp_restore();
923         MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
924 }
925
926 void MapInfo_ClearTemps()
927 {
928         MapInfo_Map_bspname = string_null;
929         MapInfo_Map_title = string_null;
930         MapInfo_Map_description = string_null;
931         MapInfo_Map_author = string_null;
932         MapInfo_Map_clientstuff = string_null;
933         MapInfo_Map_supportedGametypes = 0;
934         MapInfo_Map_supportedFeatures = 0;
935 }
936
937 void MapInfo_Shutdown()
938 {
939         MapInfo_ClearTemps();
940         MapInfo_Filter_Free();
941         MapInfo_Cache_Destroy();
942         if(_MapInfo_globopen)
943         {
944                 search_end(_MapInfo_globhandle);
945                 _MapInfo_globhandle = -1;
946                 _MapInfo_globopen = FALSE;
947         }
948 }