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