]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapinfo.qc
Update Luma simple items. Minor color changes
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapinfo.qc
1 // generic string stuff
2
3 float _MapInfo_Cache_Active;
4 float _MapInfo_Cache_DB_NameToIndex;
5 float _MapInfo_Cache_Buf_IndexToMapData;
6
7 void MapInfo_Cache_Destroy()
8 {
9         if(!_MapInfo_Cache_Active)
10                 return;
11
12         db_close(_MapInfo_Cache_DB_NameToIndex);
13         buf_del(_MapInfo_Cache_Buf_IndexToMapData);
14         _MapInfo_Cache_Active = 0;
15 }
16
17 void MapInfo_Cache_Create()
18 {
19         MapInfo_Cache_Destroy();
20         _MapInfo_Cache_DB_NameToIndex = db_create();
21         _MapInfo_Cache_Buf_IndexToMapData = buf_create();
22         _MapInfo_Cache_Active = 1;
23 }
24
25 void MapInfo_Cache_Invalidate()
26 {
27         if(!_MapInfo_Cache_Active)
28                 return;
29
30         MapInfo_Cache_Create();
31 }
32
33 void MapInfo_Cache_Store()
34 {
35         float i;
36         string s;
37         if(!_MapInfo_Cache_Active)
38                 return;
39
40         s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
41         if(s == "")
42         {
43                 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
44                 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
45         }
46         else
47                 i = stof(s);
48
49         // now store all the stuff
50         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData,   i, MapInfo_Map_bspname);
51         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
52         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
53         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
54         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
55         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
56         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
57         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
58 }
59
60 float MapInfo_Cache_Retrieve(string map)
61 {
62         float i;
63         string s;
64         if(!_MapInfo_Cache_Active)
65                 return 0;
66
67         s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
68         if(s == "")
69                 return 0;
70         i = stof(s);
71
72         // now retrieve all the stuff
73         MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
74         MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
75         MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
76         MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
77         MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
78         MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
79         MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
80         MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
81
82         return 1;
83 }
84
85 // GLOB HANDLING (for all BSP files)
86 float _MapInfo_globopen;
87 float _MapInfo_globcount;
88 float _MapInfo_globhandle;
89 string _MapInfo_GlobItem(float i)
90 {
91         string s;
92         if(!_MapInfo_globopen)
93                 return string_null;
94         s = search_getfilename(_MapInfo_globhandle, i);
95         return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
96 }
97
98 void MapInfo_Enumerate()
99 {
100         if(_MapInfo_globopen)
101         {
102                 search_end(_MapInfo_globhandle);
103                 _MapInfo_globopen = 0;
104         }
105         MapInfo_Cache_Invalidate();
106         _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
107         if(_MapInfo_globhandle >= 0)
108         {
109                 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
110                 _MapInfo_globopen = 1;
111         }
112         else
113                 _MapInfo_globcount = 0;
114 }
115
116 // filter the info by game type mask (updates MapInfo_count)
117 //
118 float _MapInfo_filtered;
119 float _MapInfo_filtered_allocated;
120 float MapInfo_FilterList_Lookup(float i)
121 {
122         return stof(bufstr_get(_MapInfo_filtered, i));
123 }
124
125 void _MapInfo_FilterList_swap(float i, float j, entity pass)
126 {
127         string h;
128         h = bufstr_get(_MapInfo_filtered, i);
129         bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
130         bufstr_set(_MapInfo_filtered, j, h);
131 }
132
133 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
134 {
135         string a, b;
136         a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
137         b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
138         return strcasecmp(a, b);
139 }
140
141 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
142 {
143         float i, j;
144         if (!_MapInfo_filtered_allocated)
145         {
146                 _MapInfo_filtered_allocated = 1;
147                 _MapInfo_filtered = buf_create();
148         }
149         MapInfo_count = 0;
150         for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
151         {
152                 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.
153                         if(pAbortOnGenerate)
154                         {
155                                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
156                                 MapInfo_progress = i / _MapInfo_globcount;
157                                 return 0;
158                         }
159                 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
160                 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
161                 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
162                 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
163                         bufstr_set(_MapInfo_filtered, ++j, ftos(i));
164         }
165         MapInfo_count = j + 1;
166         MapInfo_ClearTemps();
167
168         // sometimes the glob isn't sorted nicely, so fix it here...
169         heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
170
171         return 1;
172 }
173
174 void MapInfo_Filter_Free()
175 {
176         if(_MapInfo_filtered_allocated)
177         {
178                 buf_del(_MapInfo_filtered);
179                 _MapInfo_filtered_allocated = 0;
180         }
181 }
182
183 // load info about the i-th map into the MapInfo_Map_* globals
184 string MapInfo_BSPName_ByID(float i)
185 {
186         return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
187 }
188
189 string unquote(string s)
190 {
191         float i, j, l;
192         l = strlen(s);
193         j = -1;
194         for(i = 0; i < l; ++i)
195         {
196                 string ch;
197                 ch = substring(s, i, 1);
198                 if(ch != " ") if(ch != "\"")
199                 {
200                         for(j = strlen(s) - i - 1; j > 0; --j)
201                         {
202                                 ch = substring(s, i+j, 1);
203                                 if(ch != " ") if(ch != "\"")
204                                         return substring(s, i, j+1);
205                         }
206                         return substring(s, i, 1);
207                 }
208         }
209         return "";
210 }
211
212 float MapInfo_Get_ByID(float i)
213 {
214         if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
215                 return 1;
216         return 0;
217 }
218
219 string _MapInfo_Map_worldspawn_music;
220
221 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
222 {
223         string fn;
224         float fh;
225         string s, k, v;
226         vector o;
227         float i;
228         float inWorldspawn;
229         float r;
230         float twoBaseModes;
231         float diameter, spawnpoints;
232         float spawnplaces;
233
234         vector mapMins, mapMaxs;
235
236         r = 1;
237         fn = strcat("maps/", pFilename, ".ent");
238         fh = fopen(fn, FILE_READ);
239         if(fh < 0)
240         {
241                 r = 2;
242                 fn = strcat("maps/", pFilename, ".bsp");
243                 fh = fopen(fn, FILE_READ);
244         }
245         if(fh < 0)
246                 return 0;
247         print("Analyzing ", fn, " to generate initial mapinfo\n");
248
249         inWorldspawn = 2;
250         MapInfo_Map_flags = 0;
251         MapInfo_Map_supportedGametypes = 0;
252         spawnpoints = 0;
253         spawnplaces = 0;
254         _MapInfo_Map_worldspawn_music = "";
255         mapMins = '0 0 0';
256         mapMaxs = '0 0 0';
257
258         for(;;)
259         {
260                 if (!((s = fgets(fh))))
261                         break;
262                 if(inWorldspawn == 1)
263                         if(startsWith(s, "}"))
264                                 inWorldspawn = 0;
265                 k = unquote(car(s));
266                 v = unquote(cdr(s));
267                 if(inWorldspawn)
268                 {
269                         if(k == "classname" && v == "worldspawn")
270                                 inWorldspawn = 1;
271                         else if(k == "author")
272                                 MapInfo_Map_author = v;
273                         else if(k == "_description")
274                                 MapInfo_Map_description = v;
275                         else if(k == "music")
276                                 _MapInfo_Map_worldspawn_music = v;
277                         else if(k == "noise")
278                                 _MapInfo_Map_worldspawn_music = v;
279                         else if(k == "message")
280                         {
281                                 i = strstrofs(v, " by ", 0);
282                                 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
283                                 {
284                                         MapInfo_Map_title = substring(v, 0, i);
285                                         MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
286                                 }
287                                 else
288                                         MapInfo_Map_title = v;
289                         }
290                 }
291                 else
292                 {
293                         if(k == "origin")
294                         {
295                                 o = stov(strcat("'", v, "'"));
296                                 mapMins_x = min(mapMins_x, o_x);
297                                 mapMins_y = min(mapMins_y, o_y);
298                                 mapMins_z = min(mapMins_z, o_z);
299                                 mapMaxs_x = max(mapMaxs_x, o_x);
300                                 mapMaxs_y = max(mapMaxs_y, o_y);
301                                 mapMaxs_z = max(mapMaxs_z, o_z);
302                         }
303                         else if(k == "race_place")
304                         {
305                                 if(stof(v) > 0)
306                                         spawnplaces = 1;
307                         }
308                         else if(k == "classname")
309                         {
310                                 if(v == "dom_controlpoint")
311                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
312                                 else if(v == "item_flag_team2")
313                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
314                                 else if(v == "team_CTF_blueflag")
315                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
316                                 else if(v == "invasion_spawnpoint")
317                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_INVASION;
318                                 else if(v == "target_assault_roundend")
319                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
320                                 else if(v == "onslaught_generator")
321                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
322                                 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
323                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
324                                 else if(v == "info_player_team1")
325                                         ++spawnpoints;
326                                 else if(v == "info_player_team2")
327                                         ++spawnpoints;
328                                 else if(v == "info_player_start")
329                                         ++spawnpoints;
330                                 else if(v == "info_player_deathmatch")
331                                         ++spawnpoints;
332                                 else if(v == "trigger_race_checkpoint")
333                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
334                                 else if(v == "target_startTimer")
335                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
336                                 else if(v == "weapon_nex")
337                                         { }
338                                 else if(v == "weapon_railgun")
339                                         { }
340                                 else if(startsWith(v, "weapon_"))
341                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
342                                 else if(startsWith(v, "turret_"))
343                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
344                                 else if(startsWith(v, "vehicle_"))
345                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
346                                 else if(v == "target_music" || v == "trigger_music")
347                                         _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
348                         }
349                 }
350         }
351         if(inWorldspawn)
352         {
353                 print(fn, " ended still in worldspawn, BUG\n");
354                 return 0;
355         }
356         diameter = vlen(mapMaxs - mapMins);
357
358         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
359         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
360         {
361                 // we have a CTF-only or Assault-only map. Don't add other modes then,
362                 // as the map is too symmetric for them.
363         }
364         else
365         {
366                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
367                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
368                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY;                // Keepaway always works
369
370                 if(spawnpoints >= 8  && diameter > 4096) {
371                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
372                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
373                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
374                 }
375                 if(spawnpoints >= 12 && diameter > 5120)
376                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
377         }
378
379         if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
380         if(!spawnplaces)
381         {
382                 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE;
383                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
384         }
385
386         dprint("-> diameter ",    ftos(diameter));
387         dprint(";  spawnpoints ", ftos(spawnpoints));
388         dprint(";  modes ",       ftos(MapInfo_Map_supportedGametypes), "\n");
389
390         fclose(fh);
391
392         return r;
393 }
394
395 void _MapInfo_Map_Reset()
396 {
397         MapInfo_Map_title = "<TITLE>";
398         MapInfo_Map_titlestring = "<TITLE>";
399         MapInfo_Map_description = "<DESCRIPTION>";
400         MapInfo_Map_author = "<AUTHOR>";
401         MapInfo_Map_supportedGametypes = 0;
402         MapInfo_Map_supportedFeatures = 0;
403         MapInfo_Map_flags = 0;
404         MapInfo_Map_clientstuff = "";
405         MapInfo_Map_fog = "";
406         MapInfo_Map_mins = '0 0 0';
407         MapInfo_Map_maxs = '0 0 0';
408 }
409
410 string _MapInfo_GetDefault(float t)
411 {
412         switch(t)
413         {
414                 case MAPINFO_TYPE_DEATHMATCH:      return "30 20 0";
415                 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
416                 case MAPINFO_TYPE_DOMINATION:      return "200 20 0";
417                 case MAPINFO_TYPE_CTF:             return "300 20 10 0";
418                 case MAPINFO_TYPE_LMS:             return "9 20 0";
419                 case MAPINFO_TYPE_CA:              return "10 20 0";
420                 case MAPINFO_TYPE_KEYHUNT:         return "1000 20 3 0";
421                 case MAPINFO_TYPE_ASSAULT:         return "20 0";
422                 case MAPINFO_TYPE_RACE:            return "20 5 7 15 0";
423                 case MAPINFO_TYPE_ONSLAUGHT:       return "20 0";
424                 case MAPINFO_TYPE_NEXBALL:         return "5 20 0";
425                 case MAPINFO_TYPE_CTS:             return "20 0 0";
426                 case MAPINFO_TYPE_FREEZETAG:       return "10 20 0";
427                 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
428                 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
429                 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
430                 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
431                 default:                           return "";
432         }
433 }
434
435 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, float load_default)
436 {
437         string sa;
438         MapInfo_Map_supportedGametypes |= pThisType;
439         if(!(pThisType & pWantedType))
440                 return;
441
442         if(load_default)
443                 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, FALSE);
444
445         if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
446         {
447                 cvar_set("fraglimit", "0");
448         }
449         else
450         {
451                 sa = car(s);
452                 if(sa != "")
453                         cvar_set("fraglimit", sa);
454                 s = cdr(s);
455         }
456
457         sa = car(s);
458         if(sa != "")
459                 cvar_set("timelimit", sa);
460         s = cdr(s);
461
462         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
463         {
464                 sa = car(s);
465                 if(sa != "")
466                         cvar_set("g_tdm_teams", sa);
467                 s = cdr(s);
468         }
469
470         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
471         {
472                 sa = car(s);
473                 if(sa != "")
474                         cvar_set("g_keyhunt_teams", sa);
475                 s = cdr(s);
476         }
477
478         if(pWantedType == MAPINFO_TYPE_CA)
479         {
480                 sa = car(s);
481                 if(sa != "")
482                         cvar_set("g_ca_teams", sa);
483                 s = cdr(s);
484         }
485
486         if(pWantedType == MAPINFO_TYPE_FREEZETAG)
487         {
488                 sa = car(s);
489                 if(sa != "")
490                         cvar_set("g_freezetag_teams", sa);
491                 s = cdr(s);
492         }
493
494         if(pWantedType == MAPINFO_TYPE_CTF)
495         {
496                 sa = car(s);
497                 if(sa != "")
498                         cvar_set("fraglimit", sa);
499                 s = cdr(s);
500         }
501
502         /* keepaway wuz here
503         if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
504         {
505                 sa = car(s);
506                 if(sa != "")
507                         cvar_set("fraglimit", sa);
508                 s = cdr(s);
509         }
510         */
511
512         // rc = timelimit timelimit_qualification laps laps_teamplay
513         if(pWantedType == MAPINFO_TYPE_RACE)
514         {
515                 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
516                 cvar_set("g_race_qualifying_timelimit", sa);
517                 s = cdr(s);
518
519                 sa = car(s);
520                 if(sa != "")
521                         if(cvar("g_race_teams") < 2)
522                                 cvar_set("fraglimit", sa);
523                 s = cdr(s);
524
525                 sa = car(s);
526                 if(sa != "")
527                         if(cvar("g_race_teams") >= 2)
528                                 cvar_set("fraglimit", sa);
529                 s = cdr(s);
530         }
531
532         if(pWantedType == MAPINFO_TYPE_CTS)
533         {
534                 sa = car(s);
535
536                 // this is the skill of the map
537                 // not parsed by anything yet
538                 // for map databases
539                 //if(sa != "")
540                 //      cvar_set("fraglimit", sa);
541
542                 s = cdr(s);
543         }
544
545         if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
546         {
547                 cvar_set("leadlimit", "0");
548         }
549         else
550         {
551                 sa = car(s);
552                 if(sa != "")
553                         cvar_set("leadlimit", sa);
554                 s = cdr(s);
555         }
556 }
557
558 string _MapInfo_GetDefaultEx(float t)
559 {
560         entity e;
561         for(e = MapInfo_Type_first; e; e = e.enemy)
562                 if(t == e.items)
563                         return e.model2;
564         return "";
565 }
566
567 void _MapInfo_Map_ApplyGametypeEx(string s, float pWantedType, float pThisType)
568 {
569         string sa, k, v;
570         float p;
571         string fraglimit_normal;
572         string fraglimit_teams;
573
574         MapInfo_Map_supportedGametypes |= pThisType;
575         if(!(pThisType & pWantedType))
576                 return;
577
578         // reset all the cvars to their defaults
579
580         cvar_set("timelimit", cvar_defstring("timelimit"));
581         cvar_set("leadlimit", cvar_defstring("leadlimit"));
582         cvar_set("fraglimit", cvar_defstring("fraglimit"));
583         cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
584         cvar_set("g_ca_teams", cvar_defstring("g_ca_teams"));
585         cvar_set("g_freezetag_teams", cvar_defstring("g_freezetag_teams"));
586         cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
587         cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
588         cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
589
590         fraglimit_normal = string_null;
591         fraglimit_teams = string_null;
592
593         s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
594         while(s != "")
595         {
596                 sa = car(s);
597                 s = cdr(s);
598
599                 if(sa == "")
600                         continue;
601
602                 p = strstrofs(sa, "=", 0);
603                 if(p < 0)
604                 {
605                         print("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
606                         continue;
607                 }
608                 k = substring(sa, 0, p);
609                 v = substring(sa, p+1, -1);
610
611                 if(k == "timelimit")
612                 {
613                         cvar_set("timelimit", v);
614                 }
615                 else if(k == "leadlimit")
616                 {
617                         cvar_set("leadlimit", v);
618                 }
619                 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit" || k == "caplimit")
620                 {
621                         fraglimit_normal = v;
622                 }
623                 else if(k == "teampointlimit" || k == "teamlaplimit")
624                 {
625                         fraglimit_teams = v;
626                 }
627                 else if(k == "teams")
628                 {
629                         cvar_set("g_tdm_teams", v);
630                         cvar_set("g_ca_teams", v);
631                         cvar_set("g_freezetag_teams", v);
632                         cvar_set("g_keyhunt_teams", v);
633                         cvar_set("g_domination_default_teams", v);
634                 }
635                 else if(k == "qualifying_timelimit")
636                 {
637                         cvar_set("g_race_qualifying_timelimit", v);
638                 }
639                 else if(k == "skill")
640                 {
641                         // ignore
642                 }
643                 else
644                 {
645                         print("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
646                 }
647         }
648
649         if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
650         {
651                 if(fraglimit_teams)
652                         cvar_set("fraglimit", fraglimit_teams);
653         }
654         else
655         {
656                 if(fraglimit_normal)
657                         cvar_set("fraglimit", fraglimit_normal);
658         }
659 }
660
661 float MapInfo_Type_FromString(string t)
662 {
663         entity e;
664         if(t == "nexball")
665         {
666                 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
667                 t = "nb";
668                 print("'. Should use '", t, "'.\n");
669         }
670         if(t == "freezetag")
671         {
672                 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
673                 t = "ft";
674                 print("'. Should use '", t, "'.\n");
675         }
676         if(t == "keepaway")
677         {
678                 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
679                 t = "ka";
680                 print("'. Should use '", t, "'.\n");
681         }
682         if(t == "invasion")
683         {
684                 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
685                 t = "inv";
686                 print("'. Should use '", t, "'.\n");
687         }
688         if(t == "all")
689                 return MAPINFO_TYPE_ALL;
690         for(e = MapInfo_Type_first; e; e = e.enemy)
691                 if(t == e.mdl)
692                         return e.items;
693         return 0;
694 }
695
696 string MapInfo_Type_ToString(float t)
697 {
698         entity e;
699         if(t == MAPINFO_TYPE_ALL)
700                 return "all";
701         for(e = MapInfo_Type_first; e; e = e.enemy)
702                 if(t == e.items)
703                         return e.mdl;
704         return "";
705 }
706
707 string MapInfo_Type_ToText(float t)
708 {
709         entity e;
710         for(e = MapInfo_Type_first; e; e = e.enemy)
711                 if(t == e.items)
712                         return e.message;
713         /* xgettext:no-c-format */
714         return _("@!#%'n Tuba Throwing");
715 }
716
717 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
718 {
719         string t;
720         float fh, o;
721         t = car(s); s = cdr(s);
722
723         // limited support of "" and comments
724         //   remove trailing and leading " of t
725         if(substring(t, 0, 1) == "\"")
726         {
727                 if(substring(t, -1, 1) == "\"")
728                         t = substring(t, 1, -2);
729         }
730
731         //   remove leading " of s
732         if(substring(s, 0, 1) == "\"")
733         {
734                 s = substring(s, 1, -1);
735         }
736         //   remove trailing " of s, and all that follows (cvar description)
737         o = strstrofs(s, "\"", 0);
738         if(o >= 0)
739                 s = substring(s, 0, o);
740
741         //   remove // comments
742         o = strstrofs(s, "//", 0);
743         if(o >= 0)
744                 s = substring(s, 0, o);
745
746         //   remove trailing spaces
747         while(substring(s, -1, 1) == " ")
748                 s = substring(s, 0, -2);
749
750         if(t == "#include")
751         {
752                 if(recurse > 0)
753                 {
754                         fh = fopen(s, FILE_READ);
755                         if(fh < 0)
756                                 print("Map ", pFilename, " references not existing config file ", s, "\n");
757                         else
758                         {
759                                 for(;;)
760                                 {
761                                         if (!((s = fgets(fh))))
762                                                 break;
763
764                                         // catch different sorts of comments
765                                         if(s == "")                    // empty lines
766                                                 continue;
767                                         if(substring(s, 0, 1) == "#")  // UNIX style
768                                                 continue;
769                                         if(substring(s, 0, 2) == "//") // C++ style
770                                                 continue;
771                                         if(substring(s, 0, 1) == "_")  // q3map style
772                                                 continue;
773
774                                         if(substring(s, 0, 4) == "set ")
775                                                 s = substring(s, 4, -1);
776                                         if(substring(s, 0, 5) == "seta ")
777                                                 s = substring(s, 5, -1);
778
779                                         _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
780                                 }
781                                 fclose(fh);
782                         }
783                 }
784                 else
785                         print("Map ", pFilename, " uses too many levels of inclusion\n");
786         }
787         else if(t == "")
788                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
789         else if (!cvar_value_issafe(t))
790                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
791         else if (!cvar_value_issafe(s))
792                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
793         else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
794                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
795         else if(matchacl(acl, t) <= 0)
796                 print("Map ", pFilename, " contains a denied setting, ignored\n");
797         else
798         {
799                 if(type == 0) // server set
800                 {
801                         dprint("Applying temporary setting ", t, " := ", s, "\n");
802                         if(cvar("g_campaign"))
803                                 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
804                         else
805                                 cvar_settemp(t, s);
806                 }
807                 else
808                 {
809                         dprint("Applying temporary client setting ", t, " := ", s, "\n");
810                         MapInfo_Map_clientstuff = strcat(
811                                         MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
812                                         );
813                 }
814         }
815 }
816
817 float MapInfo_isRedundant(string fn, string t)
818 {
819         // normalize file name
820         fn = strreplace("_", "-", fn);
821
822         // normalize visible title
823         t = strreplace(": ", "-", t);
824         t = strreplace(":", "-", t);
825         t = strreplace(" ", "-", t);
826         t = strreplace("_", "-", t);
827         t = strreplace("'", "-", t);
828
829         if(!strcasecmp(fn, t))
830                 return TRUE;
831
832         // we allow the visible title to have punctuation the file name does
833         // not, but not vice versa
834         t = strreplace("-", "", t);
835
836         if(!strcasecmp(fn, t))
837                 return TRUE;
838
839         return FALSE;
840 }
841
842 // load info about a map by name into the MapInfo_Map_* globals
843 float MapInfo_Get_ByName_NoFallbacks(string pFilename, float pAllowGenerate, float pGametypeToSet)
844 {
845         string fn;
846         string s, t;
847         float fh;
848         float r, f, n, i, p;
849         string acl;
850
851         acl = MAPINFO_SETTEMP_ACL_USER;
852
853         if(strstrofs(pFilename, "/", 0) >= 0)
854         {
855                 print("Invalid character in map name, ignored\n");
856                 return 0;
857         }
858
859         if(pGametypeToSet == 0)
860                 if(MapInfo_Cache_Retrieve(pFilename))
861                         return 1;
862
863         r = 1;
864
865         MapInfo_Map_bspname = pFilename;
866
867         // default all generic fields so they have "good" values in case something fails
868         fn = strcat("maps/", pFilename, ".mapinfo");
869         fh = fopen(fn, FILE_READ);
870         if(fh < 0)
871         {
872                 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
873                 fh = fopen(fn, FILE_READ);
874                 if(fh < 0)
875                 {
876                         if(!pAllowGenerate)
877                                 return 0;
878                         _MapInfo_Map_Reset();
879                         r = _MapInfo_Generate(pFilename);
880                         if(!r)
881                                 return 0;
882                         fh = fopen(fn, FILE_WRITE);
883                         fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
884                         fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
885                         fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
886                         if(_MapInfo_Map_worldspawn_music != "")
887                         {
888                                 if(
889                                         substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
890                                         ||
891                                         substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
892                                 )
893                                         fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
894                                 else
895                                         fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
896                         }
897                         else
898                         {
899                                 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
900                                 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
901                                 for(;;)
902                                 {
903                                         i = floor(random() * n);
904                                         if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
905                                                 break;
906                                 }
907                                 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
908                         }
909                         if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
910                                 fputs(fh, "has weapons\n");
911                         else
912                                 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
913                         if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
914                                 fputs(fh, "has turrets\n");
915                         else
916                                 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
917                         if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
918                                 fputs(fh, "has vehicles\n");
919                         else
920                                 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
921                         if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
922                                 fputs(fh, "frustrating\n");
923
924                         for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
925                                 if(MapInfo_Map_supportedGametypes & i)
926                                         fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
927
928                         if(fexists(strcat("scripts/", pFilename, ".arena")))
929                                 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
930
931                         fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
932                         fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
933                         fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
934                         fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
935                         fputs(fh, "// optional: hidden\n");
936
937                         fclose(fh);
938                         r = 2;
939                         // return r;
940                         fh = fopen(fn, FILE_READ);
941                         if(fh < 0)
942                                 error("... but I just wrote it!");
943                 }
944
945                 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
946         }
947
948         _MapInfo_Map_Reset();
949         for(;;)
950         {
951                 if (!((s = fgets(fh))))
952                         break;
953
954                 // catch different sorts of comments
955                 if(s == "")                    // empty lines
956                         continue;
957                 if(substring(s, 0, 1) == "#")  // UNIX style
958                         continue;
959                 if(substring(s, 0, 2) == "//") // C++ style
960                         continue;
961                 if(substring(s, 0, 1) == "_")  // q3map style
962                         continue;
963
964                 p = strstrofs(s, "//", 0);
965                 if(p >= 0)
966                         s = substring(s, 0, p);
967
968                 t = car(s); s = cdr(s);
969                 if(t == "title")
970                         MapInfo_Map_title = s;
971                 else if(t == "description")
972                         MapInfo_Map_description = s;
973                 else if(t == "author")
974                         MapInfo_Map_author = s;
975                 else if(t == "has")
976                 {
977                         t = car(s); // s = cdr(s);
978                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
979                         else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
980                         else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
981                         else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
982                         else
983                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
984                 }
985                 else if(t == "hidden")
986                 {
987                         MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
988                 }
989                 else if(t == "forbidden")
990                 {
991                         MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
992                 }
993                 else if(t == "frustrating")
994                 {
995                         MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
996                 }
997                 else if(t == "noautomaplist")
998                 {
999                         MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1000                 }
1001                 else if(t == "type")
1002                 {
1003                         t = car(s); s = cdr(s);
1004                         f = MapInfo_Type_FromString(t);
1005                         print("Map ", pFilename, " contains the legacy 'type' keyword which is deprecated and will be removed in the future. Please migrate the mapinfo file to 'gametype'.\n");
1006                         if(f)
1007                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, TRUE);
1008                         else
1009                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1010                 }
1011                 else if(t == "gametype")
1012                 {
1013                         t = car(s); s = cdr(s);
1014                         f = MapInfo_Type_FromString(t);
1015                         if(f)
1016                                 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
1017                         else
1018                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1019                 }
1020                 else if(t == "size")
1021                 {
1022                         float a, b, c, d, e;
1023                         t = car(s); s = cdr(s); a = stof(t);
1024                         t = car(s); s = cdr(s); b = stof(t);
1025                         t = car(s); s = cdr(s); c = stof(t);
1026                         t = car(s); s = cdr(s); d = stof(t);
1027                         t = car(s); s = cdr(s); e = stof(t);
1028                         if(s == "")
1029                                 print("Map ", pFilename, " contains an incorrect size line (not enough params), syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
1030                         else
1031                         {
1032                                 t = car(s); s = cdr(s); f = stof(t);
1033                                 if(s != "")
1034                                         print("Map ", pFilename, " contains an incorrect size line (too many params), syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
1035                                 else
1036                                 {
1037                                         if(a >= d || b >= e || c >= f)
1038                                                 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
1039                                         else
1040                                         {
1041                                                 MapInfo_Map_mins_x = a;
1042                                                 MapInfo_Map_mins_y = b;
1043                                                 MapInfo_Map_mins_z = c;
1044                                                 MapInfo_Map_maxs_x = d;
1045                                                 MapInfo_Map_maxs_y = e;
1046                                                 MapInfo_Map_maxs_z = f;
1047                                         }
1048                                 }
1049                         }
1050                 }
1051                 else if(t == "settemp_for_type")
1052                 {
1053                         t = car(s); s = cdr(s);
1054                         if((f = MapInfo_Type_FromString(t)))
1055                         {
1056                                 if(f & pGametypeToSet)
1057                                 {
1058                                         _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1059                                 }
1060                         }
1061                         else
1062                         {
1063                                 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1064                         }
1065                 }
1066                 else if(t == "clientsettemp_for_type")
1067                 {
1068                         t = car(s); s = cdr(s);
1069                         if((f = MapInfo_Type_FromString(t)))
1070                         {
1071                                 if(f & pGametypeToSet)
1072                                 {
1073                                         _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1074                                 }
1075                         }
1076                         else
1077                         {
1078                                 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1079                         }
1080                 }
1081                 else if(t == "fog")
1082                 {
1083                         if (!cvar_value_issafe(s))
1084                                 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1085                         else
1086                                 MapInfo_Map_fog = s;
1087                 }
1088                 else if(t == "cdtrack")
1089                 {
1090                         t = car(s); s = cdr(s);
1091                         if(pGametypeToSet) // FIXME is this check right here?
1092                         {
1093                                 if (!cvar_value_issafe(t))
1094                                         print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1095                                 else
1096                                         MapInfo_Map_clientstuff = strcat(
1097                                                 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1098                                         );
1099                         }
1100                 }
1101                 else
1102                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1103         }
1104         fclose(fh);
1105
1106         if(MapInfo_Map_title == "<TITLE>")
1107                 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1108         else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1109                 MapInfo_Map_titlestring = MapInfo_Map_title;
1110         else
1111                 MapInfo_Map_titlestring = sprintf(_("%s: %s"), MapInfo_Map_bspname, MapInfo_Map_title);
1112
1113         MapInfo_Cache_Store();
1114         if(MapInfo_Map_supportedGametypes != 0)
1115                 return r;
1116         dprint("Map ", pFilename, " supports no game types, ignored\n");
1117         return 0;
1118 }
1119 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
1120 {
1121         float r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1122
1123         if(cvar("g_tdm_on_dm_maps"))
1124         {
1125                 // if this is set, all DM maps support TDM too
1126                 if (!(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH))
1127                         if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)
1128                                 _MapInfo_Map_ApplyGametypeEx ("", pGametypeToSet, MAPINFO_TYPE_TEAM_DEATHMATCH);
1129         }
1130
1131         if(pGametypeToSet)
1132         {
1133                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1134                 {
1135                         error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1136                         //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1137                         //return;
1138                 }
1139         }
1140
1141         return r;
1142 }
1143
1144 float MapInfo_FindName(string s)
1145 {
1146         // if there is exactly one map of prefix s, return it
1147         // if not, return the null string
1148         // note that DP sorts glob results... so I can use a binary search
1149         float l, r, m, cmp;
1150         l = 0;
1151         r = MapInfo_count;
1152         // invariants: r is behind s, l-1 is equal or before
1153         while(l != r)
1154         {
1155                 m = floor((l + r) / 2);
1156                 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1157                 cmp = strcasecmp(MapInfo_FindName_match, s);
1158                 if(cmp == 0)
1159                         return m; // found and good
1160                 if(cmp < 0)
1161                         l = m + 1; // l-1 is before s
1162                 else
1163                         r = m; // behind s
1164         }
1165         MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1166         MapInfo_FindName_firstResult = l;
1167         // r == l, so: l is behind s, l-1 is before
1168         // SO: if there is any, l is the one with the right prefix
1169         //     and l+1 may be one too
1170         if(l == MapInfo_count)
1171         {
1172                 MapInfo_FindName_match = string_null;
1173                 MapInfo_FindName_firstResult = -1;
1174                 return -1; // no MapInfo_FindName_match, behind last item
1175         }
1176         if(!startsWithNocase(MapInfo_FindName_match, s))
1177         {
1178                 MapInfo_FindName_match = string_null;
1179                 MapInfo_FindName_firstResult = -1;
1180                 return -1; // wrong prefix
1181         }
1182         if(l == MapInfo_count - 1)
1183                 return l; // last one, nothing can follow => unique
1184         if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1185         {
1186                 MapInfo_FindName_match = string_null;
1187                 return -1; // ambigous MapInfo_FindName_match
1188         }
1189         return l;
1190 }
1191
1192 string MapInfo_FixName(string s)
1193 {
1194         MapInfo_FindName(s);
1195         return MapInfo_FindName_match;
1196 }
1197
1198 float MapInfo_CurrentFeatures()
1199 {
1200         float req;
1201         req = 0;
1202         if(!(cvar("g_lms") || cvar("g_minstagib") || cvar("g_nix") || cvar("g_weaponarena") || !cvar("g_pickup_items") || cvar("g_race") || cvar("g_cts") || cvar("g_nexball")))
1203                 req |= MAPINFO_FEATURE_WEAPONS;
1204         return req;
1205 }
1206
1207 float MapInfo_CurrentGametype()
1208 {
1209         float prev;
1210         entity e;
1211         prev = cvar("gamecfg");
1212         for(e = MapInfo_Type_first; e; e = e.enemy)
1213                 if(cvar(e.netname))
1214                         if(prev != e.items)
1215                                 return e.items;
1216         if(prev)
1217                 return prev;
1218         return MAPINFO_TYPE_DEATHMATCH;
1219 }
1220
1221 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1222 {
1223         if(!MapInfo_Get_ByName(s, 1, 0))
1224                 return 0;
1225         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1226                 return 0;
1227         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1228                 return 0;
1229         return 1;
1230 }
1231
1232 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1233 {
1234         float r;
1235         r = _MapInfo_CheckMap(s);
1236         MapInfo_ClearTemps();
1237         return r;
1238 }
1239
1240 void MapInfo_SwitchGameType(float t)
1241 {
1242         entity e;
1243         for(e = MapInfo_Type_first; e; e = e.enemy)
1244                 cvar_set(e.netname, (t == e.items) ? "1" : "0");
1245 }
1246
1247 void MapInfo_LoadMap(string s, float reinit)
1248 {
1249         MapInfo_Map_supportedGametypes = 0;
1250         // we shouldn't need this, as LoadMapSettings already fixes the gametype
1251         //if(!MapInfo_CheckMap(s))
1252         //{
1253         //      print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1254         //      MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1255         //}
1256
1257         cvar_settemp_restore();
1258         if(reinit)
1259                 localcmd(strcat("\nmap ", s, "\n"));
1260         else
1261                 localcmd(strcat("\nchangelevel ", s, "\n"));
1262 }
1263
1264 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1265 {
1266         string out;
1267         float i;
1268
1269         // to make absolutely sure:
1270         MapInfo_Enumerate();
1271         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1272
1273         out = "";
1274         for(i = 0; i < MapInfo_count; ++i)
1275                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1276         return substring(out, 1, strlen(out) - 1);
1277 }
1278
1279 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1280 {
1281         string out;
1282         float i;
1283
1284         // to make absolutely sure:
1285         MapInfo_Enumerate();
1286         MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1287
1288         out = "";
1289         for(i = 0; i < MapInfo_count; ++i)
1290                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1291
1292         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1293
1294         return substring(out, 1, strlen(out) - 1);
1295 }
1296
1297 void MapInfo_LoadMapSettings_SaveGameType(float t)
1298 {
1299         MapInfo_SwitchGameType(t);
1300         cvar_set("gamecfg", ftos(t));
1301         MapInfo_LoadedGametype = t;
1302 }
1303
1304 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1305 {
1306         float t;
1307
1308         t = MapInfo_CurrentGametype();
1309         MapInfo_LoadMapSettings_SaveGameType(t);
1310
1311         if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1312         {
1313                 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1314                 {
1315                         print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1316                         _MapInfo_Map_ApplyGametypeEx("", t, t);
1317                         return; // do not call Get_ByName!
1318                 }
1319
1320                 if(MapInfo_Map_supportedGametypes == 0)
1321                 {
1322                         print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1323                         MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1324                         MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1325                         _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1326                         return; // do not call Get_ByName!
1327                 }
1328
1329                 t = 1;
1330                 while(!(MapInfo_Map_supportedGametypes & 1))
1331                 {
1332                         t *= 2;
1333                         MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1334                 }
1335
1336                 // t is now a supported mode!
1337                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1338                 MapInfo_LoadMapSettings_SaveGameType(t);
1339         }
1340         MapInfo_Get_ByName(s, 1, t);
1341 }
1342
1343 void MapInfo_ClearTemps()
1344 {
1345         MapInfo_Map_bspname = string_null;
1346         MapInfo_Map_title = string_null;
1347         MapInfo_Map_titlestring = string_null;
1348         MapInfo_Map_description = string_null;
1349         MapInfo_Map_author = string_null;
1350         MapInfo_Map_clientstuff = string_null;
1351         MapInfo_Map_supportedGametypes = 0;
1352         MapInfo_Map_supportedFeatures = 0;
1353 }
1354
1355 void MapInfo_Shutdown()
1356 {
1357         MapInfo_ClearTemps();
1358         MapInfo_Filter_Free();
1359         MapInfo_Cache_Destroy();
1360         if(_MapInfo_globopen)
1361         {
1362                 search_end(_MapInfo_globhandle);
1363                 _MapInfo_globhandle = -1;
1364                 _MapInfo_globopen = FALSE;
1365         }
1366 }
1367
1368 float MapInfo_ForbiddenFlags()
1369 {
1370         float f;
1371         f = MAPINFO_FLAG_FORBIDDEN;
1372
1373 #ifndef MENUQC
1374         if (!cvar("g_maplist_allow_hidden"))
1375 #endif
1376                 f |= MAPINFO_FLAG_HIDDEN;
1377
1378         if (!cvar("g_maplist_allow_frustrating"))
1379                 f |= MAPINFO_FLAG_FRUSTRATING;
1380
1381         return f;
1382 }
1383
1384 float MapInfo_RequiredFlags()
1385 {
1386         float f;
1387         f = 0;
1388
1389         if(cvar("g_maplist_allow_frustrating") > 1)
1390                 f |= MAPINFO_FLAG_FRUSTRATING;
1391
1392         return f;
1393 }