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