]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/mapinfo.qc
use the old Nex model and old Nex shot origin; make csqc aware of the weapon impulses...
[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 }
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         return 1;
164 }
165
166 // GLOB HANDLING (for all BSP files)
167 float _MapInfo_globopen;
168 float _MapInfo_globcount; 
169 float _MapInfo_globhandle;
170 string _MapInfo_GlobItem(float i)
171 {
172         string s;
173         s = search_getfilename(_MapInfo_globhandle, i);
174         return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
175 }
176
177 void MapInfo_Enumerate()
178 {
179         if(_MapInfo_globopen)
180                 search_end(_MapInfo_globhandle);
181         MapInfo_Cache_Invalidate();
182         _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
183         _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
184         _MapInfo_globopen = 1;
185 }
186
187 // filter the info by game type mask (updates MapInfo_count)
188 //
189 #ifdef HSOI
190 string _MapInfo_filtered;
191 float MapInfo_FilterList_Lookup(float i)
192 {
193         return MapInfo_FilterList_Lookup(i);
194 }
195
196 string MapInfo_FilterGametype_Recursive(float pGametype, float pFeatures, float pBegin, float pEnd, float pAbortOnGenerate)
197 {
198         float m, valid;
199         string l, r;
200
201         if(pBegin == pEnd)
202                 return HugeSetOfIntegers_empty();
203
204         m = floor((pBegin + pEnd) / 2);
205
206         l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m, pAbortOnGenerate);
207         if not(l)
208                 return string_null; // BAIL OUT
209         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.
210                 if(pAbortOnGenerate)
211                 {
212                         MapInfo_progress = m / _MapInfo_globcount;
213                         return string_null; // BAIL OUT
214                 }
215         valid = (((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures));
216         r = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, m + 1, pEnd, pAbortOnGenerate);
217         if not(r)
218                 return string_null; // BAIL OUT
219
220         if(valid)
221                 return HugeSetOfIntegers_insert(l, m, r);
222         else
223                 return HugeSetOfIntegers_concat(l, r);
224 }
225
226 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pAbortOnGenerate)
227 {
228         if(_MapInfo_filtered)
229                 strunzone(_MapInfo_filtered);
230         _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, 0, _MapInfo_globcount, pAbortOnGenerate);
231         if not(_MapInfo_filtered)
232         {
233                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
234                 return 0;
235         }
236         _MapInfo_filtered = strzone(_MapInfo_filtered);
237         MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
238         MapInfo_ClearTemps();
239         return 1;
240 }
241
242 void MapInfo_Filter_Free()
243 {
244         if(_MapInfo_filtered)
245         {
246                 strunzone(_MapInfo_filtered);
247                 _MapInfo_filtered = string_null;
248         }
249 }
250 #else
251 float _MapInfo_filtered;
252 float _MapInfo_filtered_allocated;
253 float MapInfo_FilterList_Lookup(float i)
254 {
255         return stof(bufstr_get(_MapInfo_filtered, i));
256 }
257
258 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pAbortOnGenerate)
259 {
260         float i, j;
261         if not(_MapInfo_filtered_allocated)
262         {
263                 _MapInfo_filtered_allocated = 1;
264                 _MapInfo_filtered = buf_create();
265         }
266         MapInfo_count = 0;
267         for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
268         {
269                 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.
270                         if(pAbortOnGenerate)
271                         {
272                                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
273                                 MapInfo_progress = i / _MapInfo_globcount;
274                                 return 0;
275                         }
276                 if(((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures))
277                         bufstr_set(_MapInfo_filtered, ++j, ftos(i));
278         }
279         MapInfo_count = j + 1;
280         MapInfo_ClearTemps();
281         return 1;
282 }
283
284 void MapInfo_Filter_Free()
285 {
286         if(_MapInfo_filtered_allocated)
287         {
288                 buf_del(_MapInfo_filtered);
289                 _MapInfo_filtered_allocated = 0;
290         }
291 }
292 #endif
293
294 // load info about the i-th map into the MapInfo_Map_* globals
295 string MapInfo_BSPName_ByID(float i)
296 {
297         return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
298 }
299
300 string unquote(string s)
301 {
302         float i, j, l;
303         l = strlen(s);
304         j = -1;
305         for(i = 0; i < l; ++i)
306         {
307                 string ch;
308                 ch = substring(s, i, 1);
309                 if(ch != " ") if(ch != "\"")
310                 {
311                         for(j = strlen(s) - i - 1; j > 0; --j)
312                         {
313                                 ch = substring(s, i+j, 1);
314                                 if(ch != " ") if(ch != "\"")
315                                         return substring(s, i, j+1);
316                         }
317                         return substring(s, i, 1);
318                 }
319         }
320         return "";
321 }
322
323 float MapInfo_Get_ByID(float i)
324 {
325         if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
326                 return 1;
327         return 0;
328 }
329
330 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
331 {
332         string fn;
333         float fh;
334         string s, k, v;
335         vector o;
336         float i;
337         float inWorldspawn;
338         float r;
339         float twoBaseModes;
340         float diameter, spawnpoints;
341
342         vector mapMins, mapMaxs;
343
344         r = 1;
345         fn = strcat("maps/", pFilename, ".ent");
346         fh = fopen(fn, FILE_READ);
347         if(fh < 0)
348         {
349                 r = 2;
350                 fn = strcat("maps/", pFilename, ".bsp");
351                 fh = fopen(fn, FILE_READ);
352         }
353         if(fh < 0)
354                 return 0;
355         print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
356
357         inWorldspawn = 2;
358         MapInfo_Map_supportedGametypes = 0;
359         spawnpoints = 0;
360
361         for(;;)
362         {
363                 if not((s = fgets(fh)))
364                         break;
365                 if(inWorldspawn == 1)
366                         if(startsWith(s, "}"))
367                                 inWorldspawn = 0;
368                 k = unquote(car(s));
369                 v = unquote(cdr(s));
370                 if(inWorldspawn)
371                 {
372                         if(k == "classname" && v == "worldspawn")
373                                 inWorldspawn = 1;
374                         else if(k == "author")
375                                 MapInfo_Map_author = v;
376                         else if(k == "_description")
377                                 MapInfo_Map_description = v;
378                         else if(k == "message")
379                         {
380                                 i = strstrofs(v, " by ", 0);
381                                 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
382                                 {
383                                         MapInfo_Map_title = substring(v, 0, i);
384                                         MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
385                                 }
386                                 else
387                                         MapInfo_Map_title = v;
388                         }
389                 }
390                 else
391                 {
392                         if(k == "origin")
393                         {
394                                 o = stov(strcat("'", v, "'"));
395                                 mapMins_x = min(mapMins_x, o_x);
396                                 mapMins_y = min(mapMins_y, o_y);
397                                 mapMins_z = min(mapMins_z, o_z);
398                                 mapMaxs_x = max(mapMaxs_x, o_x);
399                                 mapMaxs_y = max(mapMaxs_y, o_y);
400                                 mapMaxs_z = max(mapMaxs_z, o_z);
401                         }
402                         else if(k == "classname")
403                         {
404                                 if(v == "dom_controlpoint")
405                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
406                                 else if(v == "item_flag_team2")
407                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
408                                 else if(v == "team_CTF_blueflag")
409                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
410                                 else if(v == "runematch_spawn_point")
411                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
412                                 else if(v == "target_assault_roundend")
413                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
414                                 else if(v == "onslaught_generator")
415                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
416                                 else if(v == "info_player_team1")
417                                         ++spawnpoints;
418                                 else if(v == "info_player_team2")
419                                         ++spawnpoints;
420                                 else if(v == "info_player_start")
421                                         ++spawnpoints;
422                                 else if(v == "info_player_deathmatch")
423                                         ++spawnpoints;
424                                 else if(v == "trigger_race_checkpoint")
425                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
426                                 else if(v == "weapon_nex")
427                                         { }
428                                 else if(v == "weapon_railgun")
429                                         { }
430                                 else if(startsWith(v, "weapon_"))
431                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
432                         }
433                 }
434         }
435         if(inWorldspawn)
436         {
437                 print(fn, " ended still in worldspawn, BUG\n");
438                 return 0;
439         }
440         diameter = vlen(mapMaxs - mapMins);
441
442         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE);
443         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
444         {
445                 // we have a CTF-only or Assault-only map. Don't add other modes then,
446                 // as the map is too symmetric for them.
447         }
448         else
449         {
450                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
451                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
452
453                 if(spawnpoints >= 8  && diameter > 4096)
454                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
455                 if(                     diameter < 4096)
456                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
457                 if(spawnpoints >= 12 && diameter > 5120)
458                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
459         }
460
461         dprint("-> diameter ",    ftos(diameter));
462         dprint(";  spawnpoints ", ftos(spawnpoints));
463         dprint(";  modes ",       ftos(MapInfo_Map_supportedGametypes), "\n");
464
465         fclose(fh);
466
467         return r;
468 }
469
470 void _MapInfo_Map_Reset()
471 {
472         MapInfo_Map_title = "<TITLE>";
473         MapInfo_Map_description = "<DESCRIPTION>";
474         MapInfo_Map_author = "<AUTHOR>";
475         MapInfo_Map_supportedGametypes = 0;
476         MapInfo_Map_supportedFeatures = 0;
477         MapInfo_Map_clientstuff = "";
478 }
479
480 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
481 {
482         string sa;
483         MapInfo_Map_supportedGametypes |= pThisType;
484         if(!(pThisType & pWantedType))
485                 return;
486         
487         cvar_set("fraglimit", car(s));
488         s = cdr(s);
489
490         cvar_set("timelimit", car(s));
491         s = cdr(s);
492
493         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
494         {
495                 sa = car(s); if(sa == "") sa = "2";
496                 cvar_set("g_tdm_teams", sa);
497                 s = cdr(s);
498         }
499
500         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
501         {
502                 sa = car(s); if(sa == "") sa = "3";
503                 cvar_set("g_keyhunt_teams", sa);
504                 s = cdr(s);
505         }
506
507         if(pWantedType == MAPINFO_TYPE_CTF)
508         {
509                 sa = car(s); if(sa == "") sa = "10";
510                 if(cvar("g_ctf_win_mode") != 2)
511                         cvar_set("fraglimit", sa);
512                 s = cdr(s);
513         }
514
515         if(pWantedType == MAPINFO_TYPE_RACE)
516         {
517                 sa = car(s); if(sa == "") sa = cvar_string("fraglimit");
518                 if(cvar("g_race_teams"))
519                         cvar_set("fraglimit", sa);
520                 s = cdr(s);
521         }
522 }
523
524 float MapInfo_Type_FromString(string t)
525 {
526         if     (t == "dm")    return MAPINFO_TYPE_DEATHMATCH;
527         else if(t == "tdm")   return MAPINFO_TYPE_TEAM_DEATHMATCH;
528         else if(t == "dom")   return MAPINFO_TYPE_DOMINATION;
529         else if(t == "ctf")   return MAPINFO_TYPE_CTF;
530         else if(t == "rune")  return MAPINFO_TYPE_RUNEMATCH;
531         else if(t == "lms")   return MAPINFO_TYPE_LMS;
532         else if(t == "arena") return MAPINFO_TYPE_ARENA;
533         else if(t == "kh")    return MAPINFO_TYPE_KEYHUNT;
534         else if(t == "as")    return MAPINFO_TYPE_ASSAULT;
535         else if(t == "ons")   return MAPINFO_TYPE_ONSLAUGHT;
536         else if(t == "race")  return MAPINFO_TYPE_RACE;
537         else if(t == "all")   return MAPINFO_TYPE_ALL;
538         else                  return 0;
539 }
540
541 float cvar_value_issafe(string s)
542 {
543         if(strstrofs(s, "\"", 0) >= 0)
544                 return 0;
545         if(strstrofs(s, "\\", 0) >= 0)
546                 return 0;
547         if(strstrofs(s, ";", 0) >= 0)
548                 return 0;
549         if(strstrofs(s, "$", 0) >= 0)
550                 return 0;
551         if(strstrofs(s, "\r", 0) >= 0)
552                 return 0;
553         if(strstrofs(s, "\n", 0) >= 0)
554                 return 0;
555         return 1;
556 }
557
558 // load info about a map by name into the MapInfo_Map_* globals
559 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
560 {
561         string fn;
562         string s, t;
563         float fh, fh2;
564         float r, f;
565
566         if(pGametypeToSet == 0)
567                 if(MapInfo_Cache_Retrieve(pFilename))
568                         return 1;
569
570         r = 1;
571
572         MapInfo_Map_bspname = pFilename;
573
574         // default all generic fields so they have "good" values in case something fails
575         fn = strcat("maps/", pFilename, ".mapinfo");
576         fh = fopen(fn, FILE_READ);
577         if(fh < 0)
578         {
579                 if(!pAllowGenerate)
580                         return 0;
581                 _MapInfo_Map_Reset();
582                 r = _MapInfo_Generate(pFilename);
583                 if(!r)
584                         return 0;
585                 fh = fopen(fn, FILE_WRITE);
586                 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
587                 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
588                 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
589                 fputs(fh, strcat("cdtrack ", ftos(ceil(random() * 9 + 1)), "\n")); // track from 2 to 10
590                 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)       fputs(fh, "has weapons\n");
591                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)      fputs(fh, "type dm 30 20\n");
592                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
593                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION)      fputs(fh, "type dom 200 20\n");
594                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF)             fputs(fh, "type ctf 300 20 10\n");
595                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH)       fputs(fh, "type rune 200 20\n");
596                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS)             fputs(fh, "type lms 9 20\n");
597                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA)           fputs(fh, "type arena 10 20\n");
598                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT)         fputs(fh, "type kh 1000 20 3\n");
599                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT)         fputs(fh, "type as 20\n");
600                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)            fputs(fh, "type race 5 20 15\n");
601                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT)       fputs(fh, "type ons 20\n");
602
603                 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
604                 if(fh2 >= 0)
605                 {
606                         fclose(fh2);
607                         fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
608                 }
609
610                 fclose(fh);
611                 r = 2;
612                 // return r;
613                 fh = fopen(fn, FILE_READ);
614                 if(fh < 0)
615                         error("... but I just wrote it!");
616         }
617
618         _MapInfo_Map_Reset();
619         for(;;)
620         {
621                 if not((s = fgets(fh)))
622                         break;
623
624                 // catch different sorts of comments
625                 if(s == "")                    // empty lines
626                         continue;
627                 if(substring(s, 0, 1) == "#")  // UNIX style
628                         continue;
629                 if(substring(s, 0, 2) == "//") // C++ style
630                         continue;
631                 if(substring(s, 0, 1) == "_")  // q3map style
632                         continue;
633
634                 t = car(s); s = cdr(s);
635                 if(t == "title")
636                         MapInfo_Map_title = s;
637                 else if(t == "description")
638                         MapInfo_Map_description = s;
639                 else if(t == "author")
640                         MapInfo_Map_author = s;
641                 else if(t == "has")
642                 {
643                         t = car(s); s = cdr(s);
644                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
645                         else
646                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
647                 }
648                 else if(t == "type")
649                 {
650                         t = car(s); s = cdr(s);
651                         f = MapInfo_Type_FromString(t);
652                         if(f)
653                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
654                         else
655                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
656                 }
657                 else if(t == "settemp_for_type")
658                 {
659                         t = car(s); s = cdr(s);
660                         if((f = MapInfo_Type_FromString(t)))
661                         {
662                                 if(f & pGametypeToSet)
663                                 {
664                                         t = car(s); s = cdr(s);
665                                         if not(cvar_value_issafe(t))
666                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
667                                         else if not (cvar_value_issafe(s))
668                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
669                                         else
670                                         {
671                                                 dprint("Applying temporary setting ", t, " := ", s, "\n");
672                                                 cvar_settemp(t, s);
673                                         }
674                                 }
675                         }
676                         else
677                         {
678                                 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
679                         }
680                 }
681                 else if(t == "clientsettemp_for_type")
682                 {
683                         t = car(s); s = cdr(s);
684                         if((f = MapInfo_Type_FromString(t)))
685                         {
686                                 if(f & pGametypeToSet)
687                                 {
688                                         t = car(s); s = cdr(s);
689                                         if not(cvar_value_issafe(t))
690                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
691                                         else if not (cvar_value_issafe(s))
692                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
693                                         else
694                                         {
695                                                 dprint("Applying temporary client setting ", t, " := ", s, "\n");
696                                                 MapInfo_Map_clientstuff = strcat(
697                                                         MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
698                                                 );
699                                         }
700                                 }
701                         }
702                         else
703                         {
704                                 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
705                         }
706                 }
707                 else if(t == "cdtrack")
708                 {
709                         if(pGametypeToSet)
710                         {
711                                 if not(cvar_value_issafe(t))
712                                         print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
713                                 else
714                                         MapInfo_Map_clientstuff = strcat(
715                                                 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
716                                         );
717                         }
718                 }
719                 else
720                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
721         }
722         fclose(fh);
723         if(pGametypeToSet)
724                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
725                         error("Can't select the requested game type. Bailing out.");
726         MapInfo_Cache_Store();
727         if(MapInfo_Map_supportedGametypes != 0)
728                 return r;
729         dprint("Map ", pFilename, " supports no game types, ignored\n");
730         return 0;
731 }
732
733 float MapInfo_FindName(string s)
734 {
735         // if there is exactly one map of prefix s, return it
736         // if not, return the null string
737         // note that DP sorts glob results... so I can use a binary search
738         float l, r, m, cmp;
739         l = 0;
740         r = MapInfo_count;
741         // invariants: r is behind s, l-1 is equal or before
742         while(l != r)
743         {
744                 m = floor((l + r) / 2);
745                 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
746                 cmp = strcasecmp(MapInfo_FindName_match, s);
747                 if(cmp == 0)
748                         return m; // found and good
749                 if(cmp < 0)
750                         l = m + 1; // l-1 is before s
751                 else
752                         r = m; // behind s
753         }
754         MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
755         MapInfo_FindName_firstResult = l;
756         // r == l, so: l is behind s, l-1 is before
757         // SO: if there is any, l is the one with the right prefix
758         //     and l+1 may be one too
759         if(l == MapInfo_count)
760         {
761                 MapInfo_FindName_match = string_null;
762                 MapInfo_FindName_firstResult = -1;
763                 return -1; // no MapInfo_FindName_match, behind last item
764         }
765         if(!startsWithNocase(MapInfo_FindName_match, s))
766         {
767                 MapInfo_FindName_match = string_null;
768                 MapInfo_FindName_firstResult = -1;
769                 return -1; // wrong prefix
770         }
771         if(l == MapInfo_count - 1)
772                 return l; // last one, nothing can follow => unique
773         if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
774         {
775                 MapInfo_FindName_match = string_null;
776                 return -1; // ambigous MapInfo_FindName_match
777         }
778         return l;
779 }
780
781 string MapInfo_FixName(string s)
782 {
783         MapInfo_FindName(s);
784         return MapInfo_FindName_match;
785 }
786
787 float MapInfo_CurrentFeatures()
788 {
789         float req;
790         req = 0;
791         if(!(cvar("g_lms") || cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena") || !cvar("g_pickup_items")))
792                 req |= MAPINFO_FEATURE_WEAPONS;
793         return req;
794 }
795
796 float MapInfo_CurrentGametype()
797 {
798         if(cvar("g_domination"))
799                 return MAPINFO_TYPE_DOMINATION;
800         else if(cvar("g_ctf"))
801                 return MAPINFO_TYPE_CTF;
802         else if(cvar("g_runematch"))
803                 return MAPINFO_TYPE_RUNEMATCH;
804         else if(cvar("g_tdm"))
805                 return MAPINFO_TYPE_TEAM_DEATHMATCH;
806         else if(cvar("g_assault"))
807                 return MAPINFO_TYPE_ASSAULT;
808         else if(cvar("g_lms"))
809                 return MAPINFO_TYPE_LMS;
810         else if(cvar("g_arena"))
811                 return MAPINFO_TYPE_ARENA;
812         else if(cvar("g_keyhunt"))
813                 return MAPINFO_TYPE_KEYHUNT;
814         else if(cvar("g_onslaught"))
815                 return MAPINFO_TYPE_ONSLAUGHT;
816         else if(cvar("g_race"))
817                 return MAPINFO_TYPE_RACE;
818         else
819                 return MAPINFO_TYPE_DEATHMATCH;
820 }
821
822 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
823 {
824         if(!MapInfo_Get_ByName(s, 1, 0))
825                 return 0;
826         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
827                 return 0;
828         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
829                 return 0;
830         return 1;
831 }
832
833 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
834 {
835         float r;
836         r = _MapInfo_CheckMap(s);
837         MapInfo_ClearTemps();
838         return r;
839 }
840
841 void MapInfo_SwitchGameType(float t)
842 {
843         cvar_set("gamecfg",      "0");
844         cvar_set("g_dm",         (t == MAPINFO_TYPE_DEATHMATCH)      ? "1" : "0");
845         cvar_set("g_tdm",        (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
846         cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION)      ? "1" : "0");
847         cvar_set("g_ctf",        (t == MAPINFO_TYPE_CTF)             ? "1" : "0");
848         cvar_set("g_runematch",  (t == MAPINFO_TYPE_RUNEMATCH)       ? "1" : "0");
849         cvar_set("g_lms",        (t == MAPINFO_TYPE_LMS)             ? "1" : "0");
850         cvar_set("g_arena",      (t == MAPINFO_TYPE_ARENA)           ? "1" : "0");
851         cvar_set("g_keyhunt",    (t == MAPINFO_TYPE_KEYHUNT)         ? "1" : "0");
852         cvar_set("g_assault",    (t == MAPINFO_TYPE_ASSAULT)         ? "1" : "0");
853         cvar_set("g_onslaught",  (t == MAPINFO_TYPE_ONSLAUGHT)       ? "1" : "0");
854         cvar_set("g_race",       (t == MAPINFO_TYPE_RACE)            ? "1" : "0");
855 }
856
857 void MapInfo_LoadMap(string s)
858 {
859         MapInfo_Map_supportedGametypes = 0;
860         if(!MapInfo_CheckMap(s))
861         {
862                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
863                 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
864         }
865         localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
866 }
867
868 string MapInfo_ListAllowedMaps()
869 {
870         string out;
871         float i;
872
873         // to make absolutely sure:
874         MapInfo_Enumerate();
875         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0);
876
877         out = "";
878         for(i = 0; i < MapInfo_count; ++i)
879                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
880         return substring(out, 1, strlen(out) - 1);
881 }
882
883 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
884 {
885         float t;
886         if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
887         {
888                 if(MapInfo_Map_supportedGametypes <= 0)
889                         error("Mapinfo system is not functional at all. BAILED OUT.\n");
890
891                 t = 1;
892                 while(!(MapInfo_Map_supportedGametypes & 1))
893                 {
894                         t *= 2;
895                         MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
896                 }
897                 // t is now a supported mode!
898                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
899                 MapInfo_SwitchGameType(t);
900         }
901         cvar_settemp_restore();
902         MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
903 }
904
905 void MapInfo_ClearTemps()
906 {
907         MapInfo_Map_bspname = string_null;
908         MapInfo_Map_title = string_null;
909         MapInfo_Map_description = string_null;
910         MapInfo_Map_author = string_null;
911         MapInfo_Map_clientstuff = string_null;
912         MapInfo_Map_supportedGametypes = 0;
913         MapInfo_Map_supportedFeatures = 0;
914 }
915
916 void MapInfo_Shutdown()
917 {
918         MapInfo_ClearTemps();
919         MapInfo_Filter_Free();
920         MapInfo_Cache_Destroy();
921         if(_MapInfo_globopen)
922         {
923                 search_end(_MapInfo_globhandle);
924                 _MapInfo_globhandle = -1;
925                 _MapInfo_globopen = FALSE;
926         }
927 }