]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/mapinfo.qc
fix bug in mapinfo cache that made it not do anything at all
[divverent/nexuiz.git] / data / qcsrc / common / mapinfo.qc
1 // internal toy
2 void cvar_settemp(string pKey, string pValue)
3 {
4         //localcmd(strcat("\nsettemp ", t, " \"", s, "\"\n"));
5         
6         // duplicate what this alias does:
7         // alias settemp "settemp_list \"1 $1 $settemp_var $settemp_list\"; set $settemp_var \"${$1}\"; settemp_var ${settemp_var}x; $1 \"$2\""
8         
9         cvar_set("settemp_list", strcat("1 ", pKey, " ", cvar_string("settemp_var"), " ", cvar_string("settemp_list")));
10 #ifdef MENUQC
11         registercvar(cvar_string("settemp_var"), "", 0);
12 #else
13         registercvar(cvar_string("settemp_var"), "");
14 #endif
15         cvar_set(cvar_string("settemp_var"), cvar_string(pKey));
16         cvar_set("settemp_var", strcat(cvar_string("settemp_var"), "x"));
17         cvar_set(pKey, pValue);
18 }
19
20 void cvar_settemp_restore()
21 {
22         // undo what cvar_settemp did
23         float n, i;
24         n = tokenize(cvar_string("settemp_list"));
25         for(i = 0; i < n - 3; i += 3)
26                 cvar_set(argv(i + 1), cvar_string(argv(i + 2)));
27         cvar_set("settemp_list", "0");
28 }
29
30 // HUGE SET - stored in a string
31 string HugeSetOfIntegers_empty()
32 {
33         return "";
34 }
35 float HugeSetOfIntegers_get(string pArr, float i)
36 {
37         return stof(substring(pArr, i * 4, 4));
38 }
39 float HugeSetOfIntegers_length(string pArr)
40 {
41         return strlen(pArr) / 4;
42 }
43 string HugeSetOfIntegers_concat(string a1, string a2)
44 {
45         return strcat(a1, a2);
46 }
47 string HugeSetOfIntegers_insert(string a1, float n, string a2)
48         // special concat function to build up large lists in less time by binary concatenation
49 {
50         string s;
51         s = strcat("    ", ftos(n));
52         return strcat(a1, substring(s, strlen(s) - 4, 4), a2);
53 }
54
55 // generic string stuff
56 float startsWith(string haystack, string needle)
57 {
58         return substring(haystack, 0, strlen(needle)) == needle;
59 }
60 float startsWithNocase(string haystack, string needle)
61 {
62         return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
63 }
64 string extractRestOfLine(string haystack, string needle)
65 {
66         if(startsWith(haystack, needle))
67                 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
68         return string_null;
69 }
70 string car(string s)
71 {
72         float o;
73         o = strstrofs(s, " ", 0);
74         if(o < 0)
75                 return s;
76         return substring(s, 0, o);
77 }
78 string cdr(string s)
79 {
80         float o;
81         o = strstrofs(s, " ", 0);
82         if(o < 0)
83                 return string_null;
84         return substring(s, o + 1, strlen(s) - (o + 1));
85 }
86
87 float _MapInfo_Cache_Active;
88 float _MapInfo_Cache_DB_NameToIndex;
89 float _MapInfo_Cache_Buf_IndexToMapData;
90
91 void MapInfo_Cache_Destroy()
92 {
93         if(!_MapInfo_Cache_Active)
94                 return;
95
96         db_close(_MapInfo_Cache_DB_NameToIndex);
97         buf_del(_MapInfo_Cache_Buf_IndexToMapData);
98         _MapInfo_Cache_Active = 0;
99 }
100
101 void MapInfo_Cache_Create()
102 {
103         MapInfo_Cache_Destroy();
104         _MapInfo_Cache_DB_NameToIndex = db_create();
105         _MapInfo_Cache_Buf_IndexToMapData = buf_create();
106         _MapInfo_Cache_Active = 1;
107 }
108
109 void MapInfo_Cache_Invalidate()
110 {
111         if(!_MapInfo_Cache_Active)
112                 return;
113
114         MapInfo_Cache_Create();
115 }
116
117 void MapInfo_Cache_Store()
118 {
119         float i;
120         string s;
121         if(!_MapInfo_Cache_Active)
122                 return;
123
124         s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
125         if(!s) // empty string is NOT valid here!
126         {
127                 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
128                 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
129         }
130         else
131                 i = stof(s);
132
133         // now store all the stuff
134         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i++, MapInfo_Map_bspname);
135         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i++, MapInfo_Map_title);
136         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i++, MapInfo_Map_description);
137         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i++, MapInfo_Map_author);
138         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i++, ftos(MapInfo_Map_supportedGametypes));
139         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i++, ftos(MapInfo_Map_supportedFeatures));
140         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i++, ftos(MapInfo_Map_diameter));
141         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i++, ftos(MapInfo_Map_spawnpoints));
142 }
143
144 float MapInfo_Cache_Retrieve(string map)
145 {
146         float i;
147         string s;
148         if(!_MapInfo_Cache_Active)
149                 return 0;
150
151         s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
152         if(!s)
153                 return 0;
154         i = stof(s);
155
156         // now retrieve all the stuff
157         MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i++);
158         MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i++);
159         MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i++);
160         MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i++);
161         MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i++));
162         MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i++));
163         MapInfo_Map_diameter = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i++));
164         MapInfo_Map_spawnpoints = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i++));
165         return 1;
166 }
167
168 // GLOB HANDLING (for all BSP files)
169 float _MapInfo_globopen;
170 float _MapInfo_globcount; 
171 float _MapInfo_globhandle;
172 string _MapInfo_GlobItem(float i)
173 {
174         string s;
175         s = search_getfilename(_MapInfo_globhandle, i);
176         return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
177 }
178
179 void MapInfo_Enumerate()
180 {
181         if(_MapInfo_globopen)
182                 search_end(_MapInfo_globhandle);
183         MapInfo_Cache_Invalidate();
184         _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
185         _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
186         _MapInfo_globopen = 1;
187 }
188
189 // filter the info by game type mask (updates MapInfo_count)
190 string _MapInfo_filtered;
191 string MapInfo_FilterGametype_Recursive(float pGametype, float pFeatures, float pBegin, float pEnd, float pAbortOnGenerate)
192 {
193         float m, valid;
194         string l, r;
195
196         if(pBegin == pEnd)
197                 return HugeSetOfIntegers_empty();
198
199         m = floor((pBegin + pEnd) / 2);
200
201         l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m, pAbortOnGenerate);
202         if not(l)
203                 return string_null; // BAIL OUT
204         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.
205                 if(pAbortOnGenerate)
206                         return string_null; // BAIL OUT
207         valid = (((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures));
208         r = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, m + 1, pEnd, pAbortOnGenerate);
209         if not(r)
210                 return string_null; // BAIL OUT
211
212         if(valid)
213                 return HugeSetOfIntegers_insert(l, m, r);
214         else
215                 return HugeSetOfIntegers_concat(l, r);
216 }
217 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pAbortOnGenerate)
218 {
219         if(_MapInfo_filtered)
220                 strunzone(_MapInfo_filtered);
221         _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, 0, _MapInfo_globcount, pAbortOnGenerate);
222         if not(_MapInfo_filtered)
223         {
224                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
225                 return 0;
226         }
227         _MapInfo_filtered = strzone(_MapInfo_filtered);
228         MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
229         //print("Filter ", ftos(pGametype), "/", ftos(pFeatures), " has ", ftos(MapInfo_count), "\n");
230         // TODO clear cache
231         return 1;
232 }
233
234 // load info about the i-th map into the MapInfo_Map_* globals
235 string MapInfo_BSPName_ByID(float i)
236 {
237         return _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i));
238 }
239
240 string unquote(string s)
241 {
242         float i, j, l;
243         l = strlen(s);
244         j = -1;
245         for(i = 0; i < l; ++i)
246         {
247                 string ch;
248                 ch = substring(s, i, 1);
249                 if(ch != " ") if(ch != "\"")
250                 {
251                         for(j = strlen(s) - i - 1; j > 0; --j)
252                         {
253                                 ch = substring(s, i+j, 1);
254                                 if(ch != " ") if(ch != "\"")
255                                         return substring(s, i, j+1);
256                         }
257                         return substring(s, i, 1);
258                 }
259         }
260         return "";
261 }
262
263 float MapInfo_Get_ByID(float i)
264 {
265         if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
266                 return 1;
267         return 0;
268 }
269
270 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
271 {
272         string fn;
273         float fh;
274         string s, k, v;
275         vector o;
276         float i;
277         float inWorldspawn;
278         float r;
279         float twoBaseModes;
280
281         vector mapMins, mapMaxs;
282
283         r = 1;
284         fn = strcat("maps/", pFilename, ".ent");
285         fh = fopen(fn, FILE_READ);
286         if(fh < 0)
287         {
288                 r = 2;
289                 fn = strcat("maps/", pFilename, ".bsp");
290                 fh = fopen(fn, FILE_READ);
291         }
292         if(fh < 0)
293                 return 0;
294         print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
295
296         inWorldspawn = 2;
297         MapInfo_Map_supportedGametypes = 0;
298
299         for(;;)
300         {
301                 if not((s = fgets(fh)))
302                         break;
303                 if(inWorldspawn == 1)
304                         if(startsWith(s, "}"))
305                                 inWorldspawn = 0;
306                 k = unquote(car(s));
307                 v = unquote(cdr(s));
308                 if(inWorldspawn)
309                 {
310                         if(k == "classname" && v == "worldspawn")
311                                 inWorldspawn = 1;
312                         else if(k == "author")
313                                 MapInfo_Map_author = v;
314                         else if(k == "message")
315                         {
316                                 i = strstrofs(v, " by ", 0);
317                                 if(MapInfo_Map_author == "He-Who-Must-Not-Be-Named" && i >= 0)
318                                 {
319                                         MapInfo_Map_title = substring(v, 0, i);
320                                         MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
321                                 }
322                                 else
323                                         MapInfo_Map_title = v;
324                         }
325                 }
326                 else
327                 {
328                         if(k == "origin")
329                         {
330                                 o = stov(strcat("'", v, "'"));
331                                 mapMins_x = min(mapMins_x, o_x);
332                                 mapMins_y = min(mapMins_y, o_y);
333                                 mapMins_z = min(mapMins_z, o_z);
334                                 mapMaxs_x = max(mapMaxs_x, o_x);
335                                 mapMaxs_y = max(mapMaxs_y, o_y);
336                                 mapMaxs_z = max(mapMaxs_z, o_z);
337                         }
338                         else if(k == "classname")
339                         {
340                                 if(v == "dom_controlpoint")
341                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
342                                 else if(v == "item_flag_team2")
343                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
344                                 else if(v == "team_CTF_blueflag")
345                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
346                                 else if(v == "runematch_spawn_point")
347                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
348                                 else if(v == "target_assault_roundend")
349                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
350                                 else if(v == "onslaught_generator")
351                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
352                                 else if(v == "info_player_team1")
353                                         ++MapInfo_Map_spawnpoints;
354                                 else if(v == "info_player_team2")
355                                         ++MapInfo_Map_spawnpoints;
356                                 else if(v == "info_player_start")
357                                         ++MapInfo_Map_spawnpoints;
358                                 else if(v == "info_player_deathmatch")
359                                         ++MapInfo_Map_spawnpoints;
360                                 else if(v == "weapon_nex")
361                                         { }
362                                 else if(v == "weapon_railgun")
363                                         { }
364                                 else if(startsWith(v, "weapon_"))
365                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
366                         }
367                 }
368         }
369         if(inWorldspawn)
370         {
371                 print(fn, " ended still in worldspawn, BUG\n");
372                 return 0;
373         }
374         MapInfo_Map_diameter = vlen(mapMaxs - mapMins);
375
376         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT);
377         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
378         {
379                 // we have a CTF-only or Assault-only map. Don't add other modes then,
380                 // as the map is too symmetric for them.
381         }
382         else
383         {
384                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
385                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
386
387                 if(MapInfo_Map_spawnpoints >= 8  && MapInfo_Map_diameter > 4096)
388                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
389                 if(                MapInfo_Map_diameter < 4096)
390                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
391                 if(MapInfo_Map_spawnpoints >= 12 && MapInfo_Map_diameter > 5120)
392                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
393         }
394
395         fclose(fh);
396
397         return r;
398 }
399
400 void _MapInfo_Map_Reset()
401 {
402         MapInfo_Map_title = "Untitled1";
403         MapInfo_Map_description = "Bleh.";
404         MapInfo_Map_author = "He-Who-Must-Not-Be-Named";
405         MapInfo_Map_supportedGametypes = 0;
406         MapInfo_Map_supportedFeatures = 0;
407         MapInfo_Map_diameter = 0;
408         MapInfo_Map_spawnpoints = 0;
409 }
410
411 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
412 {
413         MapInfo_Map_supportedGametypes |= pThisType;
414         if(!(pThisType & pWantedType))
415                 return;
416         
417         cvar_set("fraglimit", car(s));
418         s = cdr(s);
419
420         cvar_set("timelimit", car(s));
421         s = cdr(s);
422
423         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
424         {
425                 cvar_set("g_tdm_teams", car(s));
426                 s = cdr(s);
427         }
428
429         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
430         {
431                 cvar_set("g_keyhunt_teams", car(s));
432                 s = cdr(s);
433         }
434 }
435
436 float MapInfo_Type_FromString(string t)
437 {
438         if     (t == "dm")    return MAPINFO_TYPE_DEATHMATCH;
439         else if(t == "tdm")   return MAPINFO_TYPE_TEAM_DEATHMATCH;
440         else if(t == "dom")   return MAPINFO_TYPE_DOMINATION;
441         else if(t == "ctf")   return MAPINFO_TYPE_CTF;
442         else if(t == "rune")  return MAPINFO_TYPE_RUNEMATCH;
443         else if(t == "lms")   return MAPINFO_TYPE_LMS;
444         else if(t == "arena") return MAPINFO_TYPE_ARENA;
445         else if(t == "kh")    return MAPINFO_TYPE_KEYHUNT;
446         else if(t == "as")    return MAPINFO_TYPE_ASSAULT;
447         else if(t == "ons")   return MAPINFO_TYPE_ONSLAUGHT;
448         else if(t == "all")   return MAPINFO_TYPE_ALL;
449         else                  return 0;
450 }
451
452 // load info about a map by name into the MapInfo_Map_* globals
453 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
454 {
455         string fn;
456         string s, t;
457         float fh, fh2;
458         float r, f;
459
460         if(pGametypeToSet == 0)
461                 if(MapInfo_Cache_Retrieve(pFilename))
462                         return 1;
463
464         r = 1;
465
466         MapInfo_Map_bspname = pFilename;
467
468         // default all generic fields so they have "good" values in case something fails
469         fn = strcat("maps/", pFilename, ".mapinfo");
470         fh = fopen(fn, FILE_READ);
471         if(fh < 0)
472         {
473                 if(!pAllowGenerate)
474                         return 0;
475                 _MapInfo_Map_Reset();
476                 r = _MapInfo_Generate(pFilename);
477                 if(!r)
478                         return 0;
479                 fh = fopen(fn, FILE_WRITE);
480                 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
481                 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
482                 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
483                 fputs(fh, strcat("_diameter ", ftos(MapInfo_Map_diameter), "\n"));
484                 fputs(fh, strcat("_spawnpoints ", ftos(MapInfo_Map_spawnpoints), "\n"));
485                 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)       fputs(fh, "has weapons\n");
486                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)      fputs(fh, "type dm 30 20\n");
487                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
488                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION)      fputs(fh, "type dom 200 20\n");
489                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF)             fputs(fh, "type ctf 300 20\n");
490                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH)       fputs(fh, "type rune 200 20\n");
491                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS)             fputs(fh, "type lms 9 20\n");
492                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA)           fputs(fh, "type arena 10 20\n");
493                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT)         fputs(fh, "type kh 1000 20 3\n");
494                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT)         fputs(fh, "type as 20\n");
495                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT)       fputs(fh, "type ons 20\n");
496
497                 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
498                 if(fh2 >= 0)
499                 {
500                         fclose(fh2);
501                         fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
502                 }
503
504                 fclose(fh);
505                 r = 2;
506                 // return r;
507                 fh = fopen(fn, FILE_READ);
508                 if(fh < 0)
509                         error("... but I just wrote it!");
510         }
511
512         _MapInfo_Map_Reset();
513         for(;;)
514         {
515                 if not((s = fgets(fh)))
516                         break;
517                 t = car(s); s = cdr(s);
518                 if     (t == "title")
519                         MapInfo_Map_title = s;
520                 else if(t == "description")
521                         MapInfo_Map_description = s;
522                 else if(t == "author")
523                         MapInfo_Map_author = s;
524                 else if(t == "_diameter")
525                         MapInfo_Map_diameter = stof(s);
526                 else if(t == "_spawnpoints")
527                         MapInfo_Map_spawnpoints = stof(s);
528                 else if(t == "has")
529                 {
530                         t = car(s); s = cdr(s);
531                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
532                         else
533                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
534                 }
535                 else if(t == "type")
536                 {
537                         t = car(s); s = cdr(s);
538                         f = MapInfo_Type_FromString(t);
539                         if(f)
540                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
541                         else
542                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
543                 }
544                 else if(t == "settemp_for_type")
545                 {
546                         t = car(s); s = cdr(s);
547                         if((f = MapInfo_Type_FromString(t)))
548                         {
549                                 if(f & pGametypeToSet)
550                                 {
551                                         t = car(s); s = cdr(s);
552                                         if(strstrofs(t, "\"", 0) >= 0)
553                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
554                                         else if(strstrofs(t, "\\", 0) >= 0)
555                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
556                                         else if(strstrofs(t, ";", 0) >= 0)
557                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
558                                         else if(strstrofs(s, "\"", 0) >= 0)
559                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
560                                         else if(strstrofs(s, "\\", 0) >= 0)
561                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
562                                         else if(strstrofs(s, ";", 0) >= 0)
563                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
564                                         else
565                                         {
566                                                 dprint("Applying temporary setting ", t, " := ", s, "\n");
567                                                 cvar_settemp(t, s);
568                                         }
569                                 }
570                         }
571                         else
572                         {
573                                 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
574                         }
575                 }
576                 else
577                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
578         }
579         fclose(fh);
580         if(pGametypeToSet)
581                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
582                         error("Can't select the requested game type. Bailing out.");
583         MapInfo_Cache_Store();
584         if(MapInfo_Map_supportedGametypes != 0)
585                 return r;
586         dprint("Map ", pFilename, " supports no game types, ignored\n");
587         return 0;
588 }
589
590 float MapInfo_FindName(string s)
591 {
592         // if there is exactly one map of prefix s, return it
593         // if not, return the null string
594         // note that DP sorts glob results... so I can use a binary search
595         float l, r, m, cmp;
596         l = 0;
597         r = MapInfo_count;
598         // invariants: r is behind s, l-1 is equal or before
599         while(l != r)
600         {
601                 m = floor((l + r) / 2);
602                 MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m));
603                 cmp = strcasecmp(MapInfo_FindName_match, s);
604                 if(cmp == 0)
605                         return m; // found and good
606                 if(cmp < 0)
607                         l = m + 1; // l-1 is before s
608                 else
609                         r = m; // behind s
610         }
611         MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l));
612         MapInfo_FindName_firstResult = l;
613         // r == l, so: l is behind s, l-1 is before
614         // SO: if there is any, l is the one with the right prefix
615         //     and l+1 may be one too
616         if(l == MapInfo_count)
617         {
618                 MapInfo_FindName_match = string_null;
619                 MapInfo_FindName_firstResult = -1;
620                 return -1; // no MapInfo_FindName_match, behind last item
621         }
622         if(!startsWithNocase(MapInfo_FindName_match, s))
623         {
624                 MapInfo_FindName_match = string_null;
625                 MapInfo_FindName_firstResult = -1;
626                 return -1; // wrong prefix
627         }
628         if(l == MapInfo_count - 1)
629                 return l; // last one, nothing can follow => unique
630         if(startsWithNocase(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l + 1)), s))
631         {
632                 MapInfo_FindName_match = string_null;
633                 return -1; // ambigous MapInfo_FindName_match
634         }
635         return l;
636 }
637
638 string MapInfo_FixName(string s)
639 {
640         MapInfo_FindName(s);
641         return MapInfo_FindName_match;
642 }
643
644 float MapInfo_CurrentFeatures()
645 {
646         float req;
647         req = 0;
648         if(!(cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena")))
649                 req |= MAPINFO_FEATURE_WEAPONS;
650         return req;
651 }
652
653 float MapInfo_CurrentGametype()
654 {
655         if(cvar("g_domination"))
656                 return MAPINFO_TYPE_DOMINATION;
657         else if(cvar("g_ctf"))
658                 return MAPINFO_TYPE_CTF;
659         else if(cvar("g_runematch"))
660                 return MAPINFO_TYPE_RUNEMATCH;
661         else if(cvar("g_tdm"))
662                 return MAPINFO_TYPE_TEAM_DEATHMATCH;
663         else if(cvar("g_assault"))
664                 return MAPINFO_TYPE_ASSAULT;
665         else if(cvar("g_lms"))
666                 return MAPINFO_TYPE_LMS;
667         else if(cvar("g_arena"))
668                 return MAPINFO_TYPE_ARENA;
669         else if(cvar("g_keyhunt"))
670                 return MAPINFO_TYPE_KEYHUNT;
671         else if(cvar("g_onslaught"))
672                 return MAPINFO_TYPE_ONSLAUGHT;
673         else
674                 return MAPINFO_TYPE_DEATHMATCH;
675 }
676
677 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
678 {
679         if(!MapInfo_Get_ByName(s, 1, 0))
680                 return 0;
681         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
682                 return 0;
683         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
684                 return 0;
685         return 1;
686 }
687
688 void MapInfo_SwitchGameType(float t)
689 {
690         cvar_set("gamecfg",      "0");
691         cvar_set("g_dm",         (t == MAPINFO_TYPE_DEATHMATCH)      ? "1" : "0");
692         cvar_set("g_tdm",        (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
693         cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION)      ? "1" : "0");
694         cvar_set("g_ctf",        (t == MAPINFO_TYPE_CTF)             ? "1" : "0");
695         cvar_set("g_runematch",  (t == MAPINFO_TYPE_RUNEMATCH)       ? "1" : "0");
696         cvar_set("g_lms",        (t == MAPINFO_TYPE_LMS)             ? "1" : "0");
697         cvar_set("g_arena",      (t == MAPINFO_TYPE_ARENA)           ? "1" : "0");
698         cvar_set("g_keyhunt",    (t == MAPINFO_TYPE_KEYHUNT)         ? "1" : "0");
699         cvar_set("g_assault",    (t == MAPINFO_TYPE_ASSAULT)         ? "1" : "0");
700         cvar_set("g_onslaught",  (t == MAPINFO_TYPE_ONSLAUGHT)       ? "1" : "0");
701 }
702
703 void MapInfo_LoadMap(string s)
704 {
705         MapInfo_Map_supportedGametypes = 0;
706         if(!MapInfo_CheckMap(s))
707         {
708                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
709                 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
710         }
711         localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
712 }
713
714 string MapInfo_ListAllowedMaps()
715 {
716         string out;
717         float i;
718
719         // to make absolutely sure:
720         MapInfo_Enumerate();
721         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0);
722
723         out = "";
724         for(i = 0; i < MapInfo_count; ++i)
725                 out = strcat(out, " ", _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i)));
726         return substring(out, 1, strlen(out) - 1);
727 }
728
729 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
730 {
731         float t;
732         if(!MapInfo_CheckMap(s))
733         {
734                 if(MapInfo_Map_supportedGametypes <= 0)
735                         error("Mapinfo system is not functional at all. BAILED OUT.\n");
736
737                 t = 1;
738                 while(!(MapInfo_Map_supportedGametypes & 1))
739                 {
740                         t *= 2;
741                         MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
742                 }
743                 // t is now a supported mode!
744                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
745                 MapInfo_SwitchGameType(t);
746         }
747         cvar_settemp_restore();
748         MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
749 }