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