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