]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/mapinfo.qc
Fix the race server best and speed award by sending to all clients when neccessary...
[divverent/nexuiz.git] / data / qcsrc / common / mapinfo.qc
1 // generic string stuff
2 float startsWith(string haystack, string needle)
3 {
4         return substring(haystack, 0, strlen(needle)) == needle;
5 }
6 float startsWithNocase(string haystack, string needle)
7 {
8         return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
9 }
10 string extractRestOfLine(string haystack, string needle)
11 {
12         if(startsWith(haystack, needle))
13                 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
14         return string_null;
15 }
16 string car(string s)
17 {
18         float o;
19         o = strstrofs(s, " ", 0);
20         if(o < 0)
21                 return s;
22         return substring(s, 0, o);
23 }
24 string cdr(string s)
25 {
26         float o;
27         o = strstrofs(s, " ", 0);
28         if(o < 0)
29                 return string_null;
30         return substring(s, o + 1, strlen(s) - (o + 1));
31 }
32 float matchacl(string acl, string str)
33 {
34         string t, s;
35         float r, d;
36         r = 0;
37         while(acl)
38         {
39                 t = car(acl); acl = cdr(acl);
40                 d = 1;
41                 if(substring(t, 0, 1) == "-")
42                 {
43                         d = -1;
44                         t = substring(t, 1, strlen(t) - 1);
45                 }
46                 else if(substring(t, 0, 1) == "+")
47                         t = substring(t, 1, strlen(t) - 1);
48                 if(substring(t, -1, 1) == "*")
49                 {
50                         t = substring(t, 0, strlen(t) - 1);
51                         s = substring(s, 0, strlen(t));
52                 }
53                 else
54                         s = str;
55
56                 if(s == t)
57                 {
58                         r = d;
59                 }
60         }
61         return r;
62 }
63
64 float _MapInfo_Cache_Active;
65 float _MapInfo_Cache_DB_NameToIndex;
66 float _MapInfo_Cache_Buf_IndexToMapData;
67
68 void MapInfo_Cache_Destroy()
69 {
70         if(!_MapInfo_Cache_Active)
71                 return;
72
73         db_close(_MapInfo_Cache_DB_NameToIndex);
74         buf_del(_MapInfo_Cache_Buf_IndexToMapData);
75         _MapInfo_Cache_Active = 0;
76 }
77
78 void MapInfo_Cache_Create()
79 {
80         MapInfo_Cache_Destroy();
81         _MapInfo_Cache_DB_NameToIndex = db_create();
82         _MapInfo_Cache_Buf_IndexToMapData = buf_create();
83         _MapInfo_Cache_Active = 1;
84 }
85
86 void MapInfo_Cache_Invalidate()
87 {
88         if(!_MapInfo_Cache_Active)
89                 return;
90
91         MapInfo_Cache_Create();
92 }
93
94 void MapInfo_Cache_Store()
95 {
96         float i;
97         string s;
98         if(!_MapInfo_Cache_Active)
99                 return;
100
101         s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
102         if(!s) // empty string is NOT valid here!
103         {
104                 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
105                 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
106         }
107         else
108                 i = stof(s);
109
110         // now store all the stuff
111         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData,   i, MapInfo_Map_bspname);
112         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
113         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
114         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
115         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
116         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
117         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
118 }
119
120 float MapInfo_Cache_Retrieve(string map)
121 {
122         float i;
123         string s;
124         if(!_MapInfo_Cache_Active)
125                 return 0;
126
127         s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
128         if(!s)
129                 return 0;
130         i = stof(s);
131
132         // now retrieve all the stuff
133         MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
134         MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
135         MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
136         MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
137         MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
138         MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
139         MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
140         return 1;
141 }
142
143 // GLOB HANDLING (for all BSP files)
144 float _MapInfo_globopen;
145 float _MapInfo_globcount; 
146 float _MapInfo_globhandle;
147 string _MapInfo_GlobItem(float i)
148 {
149         string s;
150         s = search_getfilename(_MapInfo_globhandle, i);
151         return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
152 }
153
154 void MapInfo_Enumerate()
155 {
156         if(_MapInfo_globopen)
157                 search_end(_MapInfo_globhandle);
158         MapInfo_Cache_Invalidate();
159         _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
160         _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
161         _MapInfo_globopen = 1;
162 }
163
164 // filter the info by game type mask (updates MapInfo_count)
165 //
166 float _MapInfo_filtered;
167 float _MapInfo_filtered_allocated;
168 float MapInfo_FilterList_Lookup(float i)
169 {
170         return stof(bufstr_get(_MapInfo_filtered, i));
171 }
172
173 void _MapInfo_FilterList_swap(float i, float j, entity pass)
174 {
175         string h;
176         h = bufstr_get(_MapInfo_filtered, i);
177         bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
178         bufstr_set(_MapInfo_filtered, j, h);
179 }
180
181 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
182 {
183         string a, b;
184         a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
185         b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
186         return strcasecmp(a, b);
187 }
188
189 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
190 {
191         float i, j;
192         if not(_MapInfo_filtered_allocated)
193         {
194                 _MapInfo_filtered_allocated = 1;
195                 _MapInfo_filtered = buf_create();
196         }
197         MapInfo_count = 0;
198         for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
199         {
200                 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.
201                         if(pAbortOnGenerate)
202                         {
203                                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
204                                 MapInfo_progress = i / _MapInfo_globcount;
205                                 return 0;
206                         }
207                 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
208                 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
209                 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
210                 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
211                         bufstr_set(_MapInfo_filtered, ++j, ftos(i));
212         }
213         MapInfo_count = j + 1;
214         MapInfo_ClearTemps();
215         
216         // sometimes the glob isn't sorted nicely, so fix it here...
217         heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
218
219         return 1;
220 }
221
222 void MapInfo_Filter_Free()
223 {
224         if(_MapInfo_filtered_allocated)
225         {
226                 buf_del(_MapInfo_filtered);
227                 _MapInfo_filtered_allocated = 0;
228         }
229 }
230
231 // load info about the i-th map into the MapInfo_Map_* globals
232 string MapInfo_BSPName_ByID(float i)
233 {
234         return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
235 }
236
237 string unquote(string s)
238 {
239         float i, j, l;
240         l = strlen(s);
241         j = -1;
242         for(i = 0; i < l; ++i)
243         {
244                 string ch;
245                 ch = substring(s, i, 1);
246                 if(ch != " ") if(ch != "\"")
247                 {
248                         for(j = strlen(s) - i - 1; j > 0; --j)
249                         {
250                                 ch = substring(s, i+j, 1);
251                                 if(ch != " ") if(ch != "\"")
252                                         return substring(s, i, j+1);
253                         }
254                         return substring(s, i, 1);
255                 }
256         }
257         return "";
258 }
259
260 float MapInfo_Get_ByID(float i)
261 {
262         if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
263                 return 1;
264         return 0;
265 }
266
267 string _MapInfo_Map_worldspawn_music;
268
269 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
270 {
271         string fn;
272         float fh;
273         string s, k, v;
274         vector o;
275         float i;
276         float inWorldspawn;
277         float r;
278         float twoBaseModes;
279         float diameter, spawnpoints;
280         float spawnplaces;
281
282         vector mapMins, mapMaxs;
283
284         r = 1;
285         fn = strcat("maps/", pFilename, ".ent");
286         fh = fopen(fn, FILE_READ);
287         if(fh < 0)
288         {
289                 r = 2;
290                 fn = strcat("maps/", pFilename, ".bsp");
291                 fh = fopen(fn, FILE_READ);
292         }
293         if(fh < 0)
294                 return 0;
295         print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
296
297         inWorldspawn = 2;
298         MapInfo_Map_flags = 0;
299         MapInfo_Map_supportedGametypes = 0;
300         spawnpoints = 0;
301         spawnplaces = 0;
302         _MapInfo_Map_worldspawn_music = "";
303
304         for(;;)
305         {
306                 if not((s = fgets(fh)))
307                         break;
308                 if(inWorldspawn == 1)
309                         if(startsWith(s, "}"))
310                                 inWorldspawn = 0;
311                 k = unquote(car(s));
312                 v = unquote(cdr(s));
313                 if(inWorldspawn)
314                 {
315                         if(k == "classname" && v == "worldspawn")
316                                 inWorldspawn = 1;
317                         else if(k == "author")
318                                 MapInfo_Map_author = v;
319                         else if(k == "_description")
320                                 MapInfo_Map_description = v;
321                         else if(k == "music")
322                                 _MapInfo_Map_worldspawn_music = v;
323                         else if(k == "noise")
324                                 _MapInfo_Map_worldspawn_music = v;
325                         else if(k == "message")
326                         {
327                                 i = strstrofs(v, " by ", 0);
328                                 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
329                                 {
330                                         MapInfo_Map_title = substring(v, 0, i);
331                                         MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
332                                 }
333                                 else
334                                         MapInfo_Map_title = v;
335                         }
336                 }
337                 else
338                 {
339                         if(k == "origin")
340                         {
341                                 o = stov(strcat("'", v, "'"));
342                                 mapMins_x = min(mapMins_x, o_x);
343                                 mapMins_y = min(mapMins_y, o_y);
344                                 mapMins_z = min(mapMins_z, o_z);
345                                 mapMaxs_x = max(mapMaxs_x, o_x);
346                                 mapMaxs_y = max(mapMaxs_y, o_y);
347                                 mapMaxs_z = max(mapMaxs_z, o_z);
348                         }
349                         else if(k == "race_place")
350                         {
351                                 if(stof(v) > 0)
352                                         spawnplaces = 1;
353                         }
354                         else if(k == "classname")
355                         {
356                                 if(v == "dom_controlpoint")
357                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
358                                 else if(v == "item_flag_team2")
359                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
360                                 else if(v == "team_CTF_blueflag")
361                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
362                                 else if(v == "runematch_spawn_point")
363                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
364                                 else if(v == "target_assault_roundend")
365                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
366                                 else if(v == "onslaught_generator")
367                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
368                                 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
369                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
370                                 else if(v == "info_player_team1")
371                                         ++spawnpoints;
372                                 else if(v == "info_player_team2")
373                                         ++spawnpoints;
374                                 else if(v == "info_player_start")
375                                         ++spawnpoints;
376                                 else if(v == "info_player_deathmatch")
377                                         ++spawnpoints;
378                                 else if(v == "trigger_race_checkpoint")
379                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
380                                 else if(v == "weapon_nex")
381                                         { }
382                                 else if(v == "weapon_railgun")
383                                         { }
384                                 else if(startsWith(v, "weapon_"))
385                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
386                         }
387                 }
388         }
389         if(inWorldspawn)
390         {
391                 print(fn, " ended still in worldspawn, BUG\n");
392                 return 0;
393         }
394         diameter = vlen(mapMaxs - mapMins);
395
396         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
397         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
398         {
399                 // we have a CTF-only or Assault-only map. Don't add other modes then,
400                 // as the map is too symmetric for them.
401         }
402         else
403         {
404                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
405                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;       // Rune always works
406                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
407
408                 if(spawnpoints >= 8  && diameter > 4096)
409                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
410                 if(                     diameter < 4096)
411                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
412                 if(spawnpoints >= 12 && diameter > 5120)
413                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
414         }
415
416         if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
417         if(!spawnplaces)
418         {
419                 MapInfo_Map_supportedGametypes &~= MAPINFO_TYPE_RACE;
420                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
421         }
422
423         dprint("-> diameter ",    ftos(diameter));
424         dprint(";  spawnpoints ", ftos(spawnpoints));
425         dprint(";  modes ",       ftos(MapInfo_Map_supportedGametypes), "\n");
426
427         fclose(fh);
428
429         return r;
430 }
431
432 void _MapInfo_Map_Reset()
433 {
434         MapInfo_Map_title = "<TITLE>";
435         MapInfo_Map_description = "<DESCRIPTION>";
436         MapInfo_Map_author = "<AUTHOR>";
437         MapInfo_Map_supportedGametypes = 0;
438         MapInfo_Map_supportedFeatures = 0;
439         MapInfo_Map_flags = 0;
440         MapInfo_Map_clientstuff = "";
441         MapInfo_Map_fog = "";
442         MapInfo_Map_mins = '0 0 0';
443         MapInfo_Map_maxs = '0 0 0';
444 }
445
446 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
447 {
448         string sa;
449         MapInfo_Map_supportedGametypes |= pThisType;
450         if(!(pThisType & pWantedType))
451                 return;
452         
453         if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
454         {
455                 cvar_set("fraglimit", "0");
456         }
457         else
458         {
459                 cvar_set("fraglimit", car(s));
460                 s = cdr(s);
461         }
462
463         cvar_set("timelimit", car(s));
464         s = cdr(s);
465
466         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
467         {
468                 sa = car(s); if(sa == "") sa = "2";
469                 cvar_set("g_tdm_teams", sa);
470                 s = cdr(s);
471         }
472
473         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
474         {
475                 sa = car(s); if(sa == "") sa = "3";
476                 cvar_set("g_keyhunt_teams", sa);
477                 s = cdr(s);
478         }
479
480         if(pWantedType == MAPINFO_TYPE_CTF)
481         {
482                 sa = car(s); if(sa == "") sa = "10";
483                 if(cvar("g_ctf_win_mode") < 2)
484                         cvar_set("fraglimit", sa);
485                 s = cdr(s);
486         }
487
488         // rc = timelimit timelimit_qualification laps laps_teamplay
489         if(pWantedType == MAPINFO_TYPE_RACE)
490         {
491                 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
492                 cvar_set("g_race_qualifying_timelimit", sa);
493                 s = cdr(s);
494
495                 sa = car(s); if(sa == "") sa = "10";
496                 if(cvar("g_race_teams") < 2)
497                         cvar_set("fraglimit", sa);
498                 s = cdr(s);
499
500                 sa = car(s); if(sa == "") sa = "20";
501                 if(cvar("g_race_teams") >= 2)
502                         cvar_set("fraglimit", sa);
503                 s = cdr(s);
504         }
505
506         if(pWantedType == MAPINFO_TYPE_CTS)
507         {
508                 sa = car(s); if(sa == "") sa = cvar_string("fraglimit");
509                 if(cvar("g_race_teams"))
510                         cvar_set("fraglimit", sa);
511                 s = cdr(s);
512         }
513
514         sa = car(s); if(sa == "") sa = "0";
515         cvar_set("leadlimit", sa);
516         s = cdr(s);
517 }
518
519 float MapInfo_Type_FromString(string t)
520 {
521         if     (t == "dm")      return MAPINFO_TYPE_DEATHMATCH;
522         else if(t == "tdm")     return MAPINFO_TYPE_TEAM_DEATHMATCH;
523         else if(t == "dom")     return MAPINFO_TYPE_DOMINATION;
524         else if(t == "ctf")     return MAPINFO_TYPE_CTF;
525         else if(t == "rune")    return MAPINFO_TYPE_RUNEMATCH;
526         else if(t == "lms")     return MAPINFO_TYPE_LMS;
527         else if(t == "arena")   return MAPINFO_TYPE_ARENA;
528         else if(t == "kh")      return MAPINFO_TYPE_KEYHUNT;
529         else if(t == "as")      return MAPINFO_TYPE_ASSAULT;
530         else if(t == "ons")     return MAPINFO_TYPE_ONSLAUGHT;
531         else if(t == "rc")      return MAPINFO_TYPE_RACE;
532         else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
533         else if(t == "cts")     return MAPINFO_TYPE_CTS;
534         else if(t == "all")     return MAPINFO_TYPE_ALL;
535         else                    return 0;
536 }
537
538 void _MapInfo_Parse_Settemp(string pFilename, string acl, string s, float recurse)
539 {
540         string t;
541         float fh, o;
542         t = car(s); s = cdr(s);
543
544         // limited support of "" and comments
545         //   remove trailing and leading " of t
546         if(substring(t, 0, 1) == "\"")
547         {
548                 if(substring(t, -1, 1) == "\"")
549                         t = substring(t, 1, -2);
550         }
551
552         //   remove leading " of s
553         if(substring(s, 0, 1) == "\"")
554         {
555                 s = substring(s, 1, -1);
556         }
557         //   remove trailing " of s, and all that follows (cvar description)
558         o = strstrofs(s, "\"", 0);
559         if(o >= 0)
560                 s = substring(s, 0, o);
561         
562         //   remove // comments
563         o = strstrofs(s, "//", 0);
564         if(o >= 0)
565                 s = substring(s, 0, o);
566         
567         //   remove trailing spaces
568         while(substring(s, -1, 1) == " ")
569                 s = substring(s, 0, -2);
570
571         if(t == "#include")
572         {
573                 if(recurse > 0)
574                 {
575                         fh = fopen(s, FILE_READ);
576                         if(fh < 0)
577                                 print("Map ", pFilename, " references not existing config file ", s, "\n");
578                         else
579                         {
580                                 for(;;)
581                                 {
582                                         if not((s = fgets(fh)))
583                                                 break;
584
585                                         // catch different sorts of comments
586                                         if(s == "")                    // empty lines
587                                                 continue;
588                                         if(substring(s, 0, 1) == "#")  // UNIX style
589                                                 continue;
590                                         if(substring(s, 0, 2) == "//") // C++ style
591                                                 continue;
592                                         if(substring(s, 0, 1) == "_")  // q3map style
593                                                 continue;
594
595                                         if(substring(s, 0, 4) == "set ")
596                                                 s = substring(s, 4, -1);
597                                         if(substring(s, 0, 5) == "seta ")
598                                                 s = substring(s, 5, -1);
599
600                                         _MapInfo_Parse_Settemp(pFilename, acl, s, recurse - 1);
601                                 }
602                                 fclose(fh);
603                         }
604                 }
605                 else
606                         print("Map ", pFilename, " uses too many levels of inclusion\n");
607         }
608         else if not(cvar_value_issafe(t))
609                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
610         else if not (cvar_value_issafe(s))
611                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
612         else if(substring(t, 0, 10) == "g_mapinfo_")
613                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
614         else if(matchacl(acl, t) <= 0)
615                 print("Map ", pFilename, " contains a denied setting, ignored\n");
616         else
617         {
618                 dprint("Applying temporary setting ", t, " := ", s, "\n");
619                 if(cvar("g_campaign"))
620                         cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
621                 else
622                         cvar_settemp(t, s);
623         }
624 }
625
626 // load info about a map by name into the MapInfo_Map_* globals
627 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
628 {
629         string fn;
630         string s, t;
631         float fh, fh2;
632         float r, f, n, i;
633         string acl;
634
635         acl = cvar_string("g_mapinfo_settemp_acl");
636
637         if(strstrofs(pFilename, "/", 0) >= 0)
638         {
639                 print("Invalid character in map name, ignored\n");
640                 return 0;
641         }
642
643         if(pGametypeToSet == 0)
644                 if(MapInfo_Cache_Retrieve(pFilename))
645                         return 1;
646
647         r = 1;
648
649         MapInfo_Map_bspname = pFilename;
650
651         // default all generic fields so they have "good" values in case something fails
652         fn = strcat("maps/", pFilename, ".mapinfo");
653         fh = fopen(fn, FILE_READ);
654         if(fh < 0)
655         {
656                 if(!pAllowGenerate)
657                         return 0;
658                 _MapInfo_Map_Reset();
659                 r = _MapInfo_Generate(pFilename);
660                 if(!r)
661                         return 0;
662                 fh = fopen(fn, FILE_WRITE);
663                 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
664                 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
665                 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
666                 if(_MapInfo_Map_worldspawn_music != "")
667                 {
668                         if(
669                                 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
670                                 ||
671                                 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
672                         )
673                                 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
674                         else
675                                 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
676                 }
677                 else
678                 {
679                         n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
680                         s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
681                         for(;;)
682                         {
683                                 i = floor(random() * n);
684                                 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
685                                         break;
686                         }
687                         fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
688                 }
689                 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
690                         fputs(fh, "has weapons\n");
691                 else
692                         fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
693                 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
694                         fputs(fh, "frustrating\n");
695                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)      fputs(fh, "type dm 30 20\n");
696                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
697                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION)      fputs(fh, "type dom 200 20\n");
698                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF)             fputs(fh, "type ctf 300 20 10\n");
699                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH)       fputs(fh, "type rune 200 20\n");
700                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS)             fputs(fh, "type lms 9 20\n");
701                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA)           fputs(fh, "type arena 10 20\n");
702                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT)         fputs(fh, "type kh 1000 20 3\n");
703                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT)         fputs(fh, "type as 20\n");
704                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)            fputs(fh, "type rc 20 5 7 15\n");
705                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT)       fputs(fh, "type ons 20\n");
706                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_NEXBALL)         fputs(fh, "type nexball 5 20\n");
707                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTS)             fputs(fh, "type cts 20 -1\n");
708
709                 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
710                 if(fh2 >= 0)
711                 {
712                         fclose(fh2);
713                         fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
714                 }
715
716                 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
717                 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
718                 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
719                 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
720                 fputs(fh, "// optional: hidden\n");
721
722                 fclose(fh);
723                 r = 2;
724                 // return r;
725                 fh = fopen(fn, FILE_READ);
726                 if(fh < 0)
727                         error("... but I just wrote it!");
728         }
729
730         _MapInfo_Map_Reset();
731         for(;;)
732         {
733                 if not((s = fgets(fh)))
734                         break;
735
736                 // catch different sorts of comments
737                 if(s == "")                    // empty lines
738                         continue;
739                 if(substring(s, 0, 1) == "#")  // UNIX style
740                         continue;
741                 if(substring(s, 0, 2) == "//") // C++ style
742                         continue;
743                 if(substring(s, 0, 1) == "_")  // q3map style
744                         continue;
745
746                 t = car(s); s = cdr(s);
747                 if(t == "title")
748                         MapInfo_Map_title = s;
749                 else if(t == "description")
750                         MapInfo_Map_description = s;
751                 else if(t == "author")
752                         MapInfo_Map_author = s;
753                 else if(t == "has")
754                 {
755                         t = car(s); s = cdr(s);
756                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
757                         else
758                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
759                 }
760                 else if(t == "hidden")
761                 {
762                         MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
763                 }
764                 else if(t == "forbidden")
765                 {
766                         MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
767                 }
768                 else if(t == "frustrating")
769                 {
770                         MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
771                 }
772                 else if(t == "type")
773                 {
774                         t = car(s); s = cdr(s);
775                         f = MapInfo_Type_FromString(t);
776                         if(f)
777                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
778                         else
779                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
780                 }
781                 else if(t == "size")
782                 {
783                         float a, b, c, d, e;
784                         t = car(s); s = cdr(s); a = stof(t);
785                         t = car(s); s = cdr(s); b = stof(t);
786                         t = car(s); s = cdr(s); c = stof(t);
787                         t = car(s); s = cdr(s); d = stof(t);
788                         t = car(s); s = cdr(s); e = stof(t);
789                         if(s == "")
790                                 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");
791                         else
792                         {
793                                 t = car(s); s = cdr(s); f = stof(t);
794                                 if(s != "")
795                                         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");
796                                 else
797                                 {
798                                         if(a >= d || b >= e || c >= f)
799                                                 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
800                                         else
801                                         {
802                                                 MapInfo_Map_mins_x = a;
803                                                 MapInfo_Map_mins_y = b;
804                                                 MapInfo_Map_mins_z = c;
805                                                 MapInfo_Map_maxs_x = d;
806                                                 MapInfo_Map_maxs_y = e;
807                                                 MapInfo_Map_maxs_z = f;
808                                         }
809                                 }
810                         }
811                 }
812                 else if(t == "settemp_for_type")
813                 {
814                         t = car(s); s = cdr(s);
815                         if((f = MapInfo_Type_FromString(t)))
816                         {
817                                 if(f & pGametypeToSet)
818                                 {
819                                         _MapInfo_Parse_Settemp(pFilename, acl, s, 1);
820                                 }
821                         }
822                         else
823                         {
824                                 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
825                         }
826                 }
827                 else if(t == "clientsettemp_for_type")
828                 {
829                         t = car(s); s = cdr(s);
830                         if((f = MapInfo_Type_FromString(t)))
831                         {
832                                 if(f & pGametypeToSet)
833                                 {
834                                         t = car(s); s = cdr(s);
835                                         if not(cvar_value_issafe(t))
836                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
837                                         else if not (cvar_value_issafe(s))
838                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
839                                         else
840                                         {
841                                                 dprint("Applying temporary client setting ", t, " := ", s, "\n");
842                                                 MapInfo_Map_clientstuff = strcat(
843                                                         MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
844                                                 );
845                                         }
846                                 }
847                         }
848                         else
849                         {
850                                 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
851                         }
852                 }
853                 else if(t == "fog")
854                 {
855                         if not(cvar_value_issafe(t))
856                                 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
857                         else
858                                 MapInfo_Map_fog = s;
859                 }
860                 else if(t == "cdtrack")
861                 {
862                         if(pGametypeToSet)
863                         {
864                                 if not(cvar_value_issafe(t))
865                                         print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
866                                 else
867                                         MapInfo_Map_clientstuff = strcat(
868                                                 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
869                                         );
870                         }
871                 }
872                 else
873                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
874         }
875         fclose(fh);
876
877         if(!MapInfo_Map_supportedGametypes)
878                 _MapInfo_Map_ApplyGametype("30 20", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
879
880         if(pGametypeToSet)
881                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
882                         error("Can't select the requested game type. Bailing out.");
883         MapInfo_Cache_Store();
884         if(MapInfo_Map_supportedGametypes != 0)
885                 return r;
886         dprint("Map ", pFilename, " supports no game types, ignored\n");
887         return 0;
888 }
889
890 float MapInfo_FindName(string s)
891 {
892         // if there is exactly one map of prefix s, return it
893         // if not, return the null string
894         // note that DP sorts glob results... so I can use a binary search
895         float l, r, m, cmp;
896         l = 0;
897         r = MapInfo_count;
898         // invariants: r is behind s, l-1 is equal or before
899         while(l != r)
900         {
901                 m = floor((l + r) / 2);
902                 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
903                 cmp = strcasecmp(MapInfo_FindName_match, s);
904                 if(cmp == 0)
905                         return m; // found and good
906                 if(cmp < 0)
907                         l = m + 1; // l-1 is before s
908                 else
909                         r = m; // behind s
910         }
911         MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
912         MapInfo_FindName_firstResult = l;
913         // r == l, so: l is behind s, l-1 is before
914         // SO: if there is any, l is the one with the right prefix
915         //     and l+1 may be one too
916         if(l == MapInfo_count)
917         {
918                 MapInfo_FindName_match = string_null;
919                 MapInfo_FindName_firstResult = -1;
920                 return -1; // no MapInfo_FindName_match, behind last item
921         }
922         if(!startsWithNocase(MapInfo_FindName_match, s))
923         {
924                 MapInfo_FindName_match = string_null;
925                 MapInfo_FindName_firstResult = -1;
926                 return -1; // wrong prefix
927         }
928         if(l == MapInfo_count - 1)
929                 return l; // last one, nothing can follow => unique
930         if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
931         {
932                 MapInfo_FindName_match = string_null;
933                 return -1; // ambigous MapInfo_FindName_match
934         }
935         return l;
936 }
937
938 string MapInfo_FixName(string s)
939 {
940         MapInfo_FindName(s);
941         return MapInfo_FindName_match;
942 }
943
944 float MapInfo_CurrentFeatures()
945 {
946         float req;
947         req = 0;
948         if(!(cvar("g_lms") || cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena") || !cvar("g_pickup_items") || cvar("g_race") || cvar("g_cts") || cvar("g_nexball")))
949                 req |= MAPINFO_FEATURE_WEAPONS;
950         return req;
951 }
952
953 float MapInfo_CurrentGametype()
954 {
955         if(cvar("g_domination"))
956                 return MAPINFO_TYPE_DOMINATION;
957         else if(cvar("g_ctf"))
958                 return MAPINFO_TYPE_CTF;
959         else if(cvar("g_runematch"))
960                 return MAPINFO_TYPE_RUNEMATCH;
961         else if(cvar("g_tdm"))
962                 return MAPINFO_TYPE_TEAM_DEATHMATCH;
963         else if(cvar("g_assault"))
964                 return MAPINFO_TYPE_ASSAULT;
965         else if(cvar("g_lms"))
966                 return MAPINFO_TYPE_LMS;
967         else if(cvar("g_arena"))
968                 return MAPINFO_TYPE_ARENA;
969         else if(cvar("g_keyhunt"))
970                 return MAPINFO_TYPE_KEYHUNT;
971         else if(cvar("g_onslaught"))
972                 return MAPINFO_TYPE_ONSLAUGHT;
973         else if(cvar("g_race"))
974                 return MAPINFO_TYPE_RACE;
975         else if(cvar("g_nexball"))
976                 return MAPINFO_TYPE_NEXBALL;
977         else if(cvar("g_cts"))
978                 return MAPINFO_TYPE_CTS;
979         else
980                 return MAPINFO_TYPE_DEATHMATCH;
981 }
982
983 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
984 {
985         if(!MapInfo_Get_ByName(s, 1, 0))
986                 return 0;
987         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
988                 return 0;
989         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
990                 return 0;
991         return 1;
992 }
993
994 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
995 {
996         float r;
997         r = _MapInfo_CheckMap(s);
998         MapInfo_ClearTemps();
999         return r;
1000 }
1001
1002 string MapInfo_GetGameTypeCvar(float t)
1003 {
1004         switch(t)
1005         {
1006                 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
1007                 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
1008                 case MAPINFO_TYPE_DOMINATION: return "g_domination";
1009                 case MAPINFO_TYPE_CTF: return "g_ctf";
1010                 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
1011                 case MAPINFO_TYPE_LMS: return "g_lms";
1012                 case MAPINFO_TYPE_ARENA: return "g_arena";
1013                 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
1014                 case MAPINFO_TYPE_ASSAULT: return "g_assault";
1015                 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
1016                 case MAPINFO_TYPE_RACE: return "g_race";
1017                 case MAPINFO_TYPE_NEXBALL: return "g_nexball";
1018                 case MAPINFO_TYPE_CTS: return "g_cts";
1019                 default: return "";
1020         }
1021 }
1022
1023 void MapInfo_SwitchGameType(float t)
1024 {
1025         cvar_set("gamecfg",      "0");
1026         cvar_set("g_dm",         (t == MAPINFO_TYPE_DEATHMATCH)      ? "1" : "0");
1027         cvar_set("g_tdm",        (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
1028         cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION)      ? "1" : "0");
1029         cvar_set("g_ctf",        (t == MAPINFO_TYPE_CTF)             ? "1" : "0");
1030         cvar_set("g_runematch",  (t == MAPINFO_TYPE_RUNEMATCH)       ? "1" : "0");
1031         cvar_set("g_lms",        (t == MAPINFO_TYPE_LMS)             ? "1" : "0");
1032         cvar_set("g_arena",      (t == MAPINFO_TYPE_ARENA)           ? "1" : "0");
1033         cvar_set("g_keyhunt",    (t == MAPINFO_TYPE_KEYHUNT)         ? "1" : "0");
1034         cvar_set("g_assault",    (t == MAPINFO_TYPE_ASSAULT)         ? "1" : "0");
1035         cvar_set("g_onslaught",  (t == MAPINFO_TYPE_ONSLAUGHT)       ? "1" : "0");
1036         cvar_set("g_race",       (t == MAPINFO_TYPE_RACE)            ? "1" : "0");
1037         cvar_set("g_nexball",    (t == MAPINFO_TYPE_NEXBALL)         ? "1" : "0");
1038         cvar_set("g_cts",        (t == MAPINFO_TYPE_CTS)             ? "1" : "0");
1039 }
1040
1041 void MapInfo_LoadMap(string s)
1042 {
1043         MapInfo_Map_supportedGametypes = 0;
1044         if(!MapInfo_CheckMap(s))
1045         {
1046                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1047                 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1048         }
1049         localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
1050 }
1051
1052 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1053 {
1054         string out;
1055         float i;
1056
1057         // to make absolutely sure:
1058         MapInfo_Enumerate();
1059         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1060
1061         out = "";
1062         for(i = 0; i < MapInfo_count; ++i)
1063                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1064         return substring(out, 1, strlen(out) - 1);
1065 }
1066
1067 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1068 {
1069         float t;
1070         if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1071         {
1072                 if(MapInfo_Map_supportedGametypes <= 0)
1073                         error("Mapinfo system is not functional at all. BAILED OUT.\n");
1074
1075                 t = 1;
1076                 while(!(MapInfo_Map_supportedGametypes & 1))
1077                 {
1078                         t *= 2;
1079                         MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1080                 }
1081                 // t is now a supported mode!
1082                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1083                 MapInfo_SwitchGameType(t);
1084         }
1085         cvar_settemp_restore();
1086         MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
1087 }
1088
1089 void MapInfo_ClearTemps()
1090 {
1091         MapInfo_Map_bspname = string_null;
1092         MapInfo_Map_title = string_null;
1093         MapInfo_Map_description = string_null;
1094         MapInfo_Map_author = string_null;
1095         MapInfo_Map_clientstuff = string_null;
1096         MapInfo_Map_supportedGametypes = 0;
1097         MapInfo_Map_supportedFeatures = 0;
1098 }
1099
1100 void MapInfo_Shutdown()
1101 {
1102         MapInfo_ClearTemps();
1103         MapInfo_Filter_Free();
1104         MapInfo_Cache_Destroy();
1105         if(_MapInfo_globopen)
1106         {
1107                 search_end(_MapInfo_globhandle);
1108                 _MapInfo_globhandle = -1;
1109                 _MapInfo_globopen = FALSE;
1110         }
1111 }
1112
1113 float MapInfo_ForbiddenFlags()
1114 {
1115         float f;
1116         f = MAPINFO_FLAG_FORBIDDEN;
1117
1118 #ifndef MENUQC
1119         if not(cvar("g_maplist_allow_hidden"))
1120 #endif
1121                 f |= MAPINFO_FLAG_HIDDEN;
1122
1123         if not(cvar("g_maplist_allow_frustrating"))
1124                 f |= MAPINFO_FLAG_FRUSTRATING;
1125
1126         return f;
1127 }
1128
1129 float MapInfo_RequiredFlags()
1130 {
1131         float f;
1132         f = 0;
1133
1134         if(cvar("g_maplist_allow_frustrating") > 1)
1135                 f |= MAPINFO_FLAG_FRUSTRATING;
1136
1137         return f;
1138 }