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