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