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