]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/mapinfo.qc
experimental team radar (try: cl_teamradar 1)
[divverent/nexuiz.git] / data / qcsrc / common / mapinfo.qc
1 #ifdef HSOI
2 // HUGE SET - stored in a string
3 string HugeSetOfIntegers_empty()
4 {
5         return "";
6 }
7 float HugeSetOfIntegers_get(string pArr, float i)
8 {
9         return stof(substring(pArr, i * 4, 4));
10 }
11 float HugeSetOfIntegers_length(string pArr)
12 {
13         return strlen(pArr) / 4;
14 }
15 string HugeSetOfIntegers_concat(string a1, string a2)
16 {
17         return strcat(a1, a2);
18 }
19 string HugeSetOfIntegers_insert(string a1, float n, string a2)
20         // special concat function to build up large lists in less time by binary concatenation
21 {
22         string s;
23         s = strcat("    ", ftos(n));
24         return strcat(a1, substring(s, strlen(s) - 4, 4), a2);
25 }
26 #endif
27
28 // generic string stuff
29 float startsWith(string haystack, string needle)
30 {
31         return substring(haystack, 0, strlen(needle)) == needle;
32 }
33 float startsWithNocase(string haystack, string needle)
34 {
35         return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
36 }
37 string extractRestOfLine(string haystack, string needle)
38 {
39         if(startsWith(haystack, needle))
40                 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
41         return string_null;
42 }
43 string car(string s)
44 {
45         float o;
46         o = strstrofs(s, " ", 0);
47         if(o < 0)
48                 return s;
49         return substring(s, 0, o);
50 }
51 string cdr(string s)
52 {
53         float o;
54         o = strstrofs(s, " ", 0);
55         if(o < 0)
56                 return string_null;
57         return substring(s, o + 1, strlen(s) - (o + 1));
58 }
59
60 float _MapInfo_Cache_Active;
61 float _MapInfo_Cache_DB_NameToIndex;
62 float _MapInfo_Cache_Buf_IndexToMapData;
63
64 void MapInfo_Cache_Destroy()
65 {
66         if(!_MapInfo_Cache_Active)
67                 return;
68
69         db_close(_MapInfo_Cache_DB_NameToIndex);
70         buf_del(_MapInfo_Cache_Buf_IndexToMapData);
71         _MapInfo_Cache_Active = 0;
72 }
73
74 void MapInfo_Cache_Create()
75 {
76         MapInfo_Cache_Destroy();
77         _MapInfo_Cache_DB_NameToIndex = db_create();
78         _MapInfo_Cache_Buf_IndexToMapData = buf_create();
79         _MapInfo_Cache_Active = 1;
80 }
81
82 void MapInfo_Cache_Invalidate()
83 {
84         if(!_MapInfo_Cache_Active)
85                 return;
86
87         MapInfo_Cache_Create();
88 }
89
90 void MapInfo_Cache_Store()
91 {
92         float i;
93         string s;
94         if(!_MapInfo_Cache_Active)
95                 return;
96
97         s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
98         if(!s) // empty string is NOT valid here!
99         {
100                 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
101                 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
102         }
103         else
104                 i = stof(s);
105
106         // now store all the stuff
107         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData,   i, MapInfo_Map_bspname);
108         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
109         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
110         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
111         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
112         bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
113 }
114
115 float MapInfo_Cache_Retrieve(string map)
116 {
117         float i;
118         string s;
119         if(!_MapInfo_Cache_Active)
120                 return 0;
121
122         s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
123         if(!s)
124                 return 0;
125         i = stof(s);
126
127         // now retrieve all the stuff
128         MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
129         MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
130         MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
131         MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
132         MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
133         MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
134         return 1;
135 }
136
137 // GLOB HANDLING (for all BSP files)
138 float _MapInfo_globopen;
139 float _MapInfo_globcount; 
140 float _MapInfo_globhandle;
141 string _MapInfo_GlobItem(float i)
142 {
143         string s;
144         s = search_getfilename(_MapInfo_globhandle, i);
145         return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
146 }
147
148 void MapInfo_Enumerate()
149 {
150         if(_MapInfo_globopen)
151                 search_end(_MapInfo_globhandle);
152         MapInfo_Cache_Invalidate();
153         _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
154         _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
155         _MapInfo_globopen = 1;
156 }
157
158 // filter the info by game type mask (updates MapInfo_count)
159 //
160 #ifdef HSOI
161 string _MapInfo_filtered;
162 float MapInfo_FilterList_Lookup(float i)
163 {
164         return MapInfo_FilterList_Lookup(i);
165 }
166
167 string MapInfo_FilterGametype_Recursive(float pGametype, float pFeatures, float pBegin, float pEnd, float pAbortOnGenerate)
168 {
169         float m, valid;
170         string l, r;
171
172         if(pBegin == pEnd)
173                 return HugeSetOfIntegers_empty();
174
175         m = floor((pBegin + pEnd) / 2);
176
177         l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m, pAbortOnGenerate);
178         if not(l)
179                 return string_null; // BAIL OUT
180         if(MapInfo_Get_ByName(_MapInfo_GlobItem(m), 1, 0) == 2) // if we generated one... BAIL OUT and let the caller continue in the next frame.
181                 if(pAbortOnGenerate)
182                 {
183                         MapInfo_progress = m / _MapInfo_globcount;
184                         return string_null; // BAIL OUT
185                 }
186         valid = (((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures));
187         r = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, m + 1, pEnd, pAbortOnGenerate);
188         if not(r)
189                 return string_null; // BAIL OUT
190
191         if(valid)
192                 return HugeSetOfIntegers_insert(l, m, r);
193         else
194                 return HugeSetOfIntegers_concat(l, r);
195 }
196
197 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pAbortOnGenerate)
198 {
199         if(_MapInfo_filtered)
200                 strunzone(_MapInfo_filtered);
201         _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, 0, _MapInfo_globcount, pAbortOnGenerate);
202         if not(_MapInfo_filtered)
203         {
204                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
205                 return 0;
206         }
207         _MapInfo_filtered = strzone(_MapInfo_filtered);
208         MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
209         MapInfo_ClearTemps();
210         return 1;
211 }
212
213 void MapInfo_Filter_Free()
214 {
215         if(_MapInfo_filtered)
216         {
217                 strunzone(_MapInfo_filtered);
218                 _MapInfo_filtered = string_null;
219         }
220 }
221 #else
222 float _MapInfo_filtered;
223 float _MapInfo_filtered_allocated;
224 float MapInfo_FilterList_Lookup(float i)
225 {
226         return stof(bufstr_get(_MapInfo_filtered, i));
227 }
228
229 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pAbortOnGenerate)
230 {
231         float i, j;
232         if not(_MapInfo_filtered_allocated)
233         {
234                 _MapInfo_filtered_allocated = 1;
235                 _MapInfo_filtered = buf_create();
236         }
237         MapInfo_count = 0;
238         for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
239         {
240                 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.
241                         if(pAbortOnGenerate)
242                         {
243                                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
244                                 MapInfo_progress = i / _MapInfo_globcount;
245                                 return 0;
246                         }
247                 if(((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures))
248                         bufstr_set(_MapInfo_filtered, ++j, ftos(i));
249         }
250         MapInfo_count = j + 1;
251         MapInfo_ClearTemps();
252         return 1;
253 }
254
255 void MapInfo_Filter_Free()
256 {
257         if(_MapInfo_filtered_allocated)
258         {
259                 buf_del(_MapInfo_filtered);
260                 _MapInfo_filtered_allocated = 0;
261         }
262 }
263 #endif
264
265 // load info about the i-th map into the MapInfo_Map_* globals
266 string MapInfo_BSPName_ByID(float i)
267 {
268         return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
269 }
270
271 string unquote(string s)
272 {
273         float i, j, l;
274         l = strlen(s);
275         j = -1;
276         for(i = 0; i < l; ++i)
277         {
278                 string ch;
279                 ch = substring(s, i, 1);
280                 if(ch != " ") if(ch != "\"")
281                 {
282                         for(j = strlen(s) - i - 1; j > 0; --j)
283                         {
284                                 ch = substring(s, i+j, 1);
285                                 if(ch != " ") if(ch != "\"")
286                                         return substring(s, i, j+1);
287                         }
288                         return substring(s, i, 1);
289                 }
290         }
291         return "";
292 }
293
294 float MapInfo_Get_ByID(float i)
295 {
296         if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
297                 return 1;
298         return 0;
299 }
300
301 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
302 {
303         string fn;
304         float fh;
305         string s, k, v;
306         vector o;
307         float i;
308         float inWorldspawn;
309         float r;
310         float twoBaseModes;
311         float diameter, spawnpoints;
312
313         vector mapMins, mapMaxs;
314
315         r = 1;
316         fn = strcat("maps/", pFilename, ".ent");
317         fh = fopen(fn, FILE_READ);
318         if(fh < 0)
319         {
320                 r = 2;
321                 fn = strcat("maps/", pFilename, ".bsp");
322                 fh = fopen(fn, FILE_READ);
323         }
324         if(fh < 0)
325                 return 0;
326         print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
327
328         inWorldspawn = 2;
329         MapInfo_Map_supportedGametypes = 0;
330         spawnpoints = 0;
331
332         for(;;)
333         {
334                 if not((s = fgets(fh)))
335                         break;
336                 if(inWorldspawn == 1)
337                         if(startsWith(s, "}"))
338                                 inWorldspawn = 0;
339                 k = unquote(car(s));
340                 v = unquote(cdr(s));
341                 if(inWorldspawn)
342                 {
343                         if(k == "classname" && v == "worldspawn")
344                                 inWorldspawn = 1;
345                         else if(k == "author")
346                                 MapInfo_Map_author = v;
347                         else if(k == "_description")
348                                 MapInfo_Map_description = v;
349                         else if(k == "message")
350                         {
351                                 i = strstrofs(v, " by ", 0);
352                                 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
353                                 {
354                                         MapInfo_Map_title = substring(v, 0, i);
355                                         MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
356                                 }
357                                 else
358                                         MapInfo_Map_title = v;
359                         }
360                 }
361                 else
362                 {
363                         if(k == "origin")
364                         {
365                                 o = stov(strcat("'", v, "'"));
366                                 mapMins_x = min(mapMins_x, o_x);
367                                 mapMins_y = min(mapMins_y, o_y);
368                                 mapMins_z = min(mapMins_z, o_z);
369                                 mapMaxs_x = max(mapMaxs_x, o_x);
370                                 mapMaxs_y = max(mapMaxs_y, o_y);
371                                 mapMaxs_z = max(mapMaxs_z, o_z);
372                         }
373                         else if(k == "classname")
374                         {
375                                 if(v == "dom_controlpoint")
376                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
377                                 else if(v == "item_flag_team2")
378                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
379                                 else if(v == "team_CTF_blueflag")
380                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
381                                 else if(v == "runematch_spawn_point")
382                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
383                                 else if(v == "target_assault_roundend")
384                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
385                                 else if(v == "onslaught_generator")
386                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
387                                 else if(v == "info_player_team1")
388                                         ++spawnpoints;
389                                 else if(v == "info_player_team2")
390                                         ++spawnpoints;
391                                 else if(v == "info_player_start")
392                                         ++spawnpoints;
393                                 else if(v == "info_player_deathmatch")
394                                         ++spawnpoints;
395                                 else if(v == "trigger_race_checkpoint")
396                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
397                                 else if(v == "weapon_nex")
398                                         { }
399                                 else if(v == "weapon_railgun")
400                                         { }
401                                 else if(startsWith(v, "weapon_"))
402                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
403                         }
404                 }
405         }
406         if(inWorldspawn)
407         {
408                 print(fn, " ended still in worldspawn, BUG\n");
409                 return 0;
410         }
411         diameter = vlen(mapMaxs - mapMins);
412
413         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE);
414         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
415         {
416                 // we have a CTF-only or Assault-only map. Don't add other modes then,
417                 // as the map is too symmetric for them.
418         }
419         else
420         {
421                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
422                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
423
424                 if(spawnpoints >= 8  && diameter > 4096)
425                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
426                 if(                     diameter < 4096)
427                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
428                 if(spawnpoints >= 12 && diameter > 5120)
429                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
430         }
431
432         dprint("-> diameter ",    ftos(diameter));
433         dprint(";  spawnpoints ", ftos(spawnpoints));
434         dprint(";  modes ",       ftos(MapInfo_Map_supportedGametypes), "\n");
435
436         fclose(fh);
437
438         return r;
439 }
440
441 void _MapInfo_Map_Reset()
442 {
443         MapInfo_Map_title = "<TITLE>";
444         MapInfo_Map_description = "<DESCRIPTION>";
445         MapInfo_Map_author = "<AUTHOR>";
446         MapInfo_Map_supportedGametypes = 0;
447         MapInfo_Map_supportedFeatures = 0;
448         MapInfo_Map_clientstuff = "";
449         MapInfo_Map_fog = "";
450         MapInfo_Map_mins = '0 0 0';
451         MapInfo_Map_maxs = '0 0 0';
452 }
453
454 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
455 {
456         string sa;
457         MapInfo_Map_supportedGametypes |= pThisType;
458         if(!(pThisType & pWantedType))
459                 return;
460         
461         cvar_set("fraglimit", car(s));
462         s = cdr(s);
463
464         cvar_set("timelimit", car(s));
465         s = cdr(s);
466
467         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
468         {
469                 sa = car(s); if(sa == "") sa = "2";
470                 cvar_set("g_tdm_teams", sa);
471                 s = cdr(s);
472         }
473
474         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
475         {
476                 sa = car(s); if(sa == "") sa = "3";
477                 cvar_set("g_keyhunt_teams", sa);
478                 s = cdr(s);
479         }
480
481         if(pWantedType == MAPINFO_TYPE_CTF)
482         {
483                 sa = car(s); if(sa == "") sa = "10";
484                 if(cvar("g_ctf_win_mode") < 2)
485                         cvar_set("fraglimit", sa);
486                 s = cdr(s);
487         }
488
489         if(pWantedType == MAPINFO_TYPE_RACE)
490         {
491                 sa = car(s); if(sa == "") sa = cvar_string("fraglimit");
492                 if(cvar("g_race_teams"))
493                         cvar_set("fraglimit", sa);
494                 s = cdr(s);
495         }
496 }
497
498 float MapInfo_Type_FromString(string t)
499 {
500         if     (t == "dm")    return MAPINFO_TYPE_DEATHMATCH;
501         else if(t == "tdm")   return MAPINFO_TYPE_TEAM_DEATHMATCH;
502         else if(t == "dom")   return MAPINFO_TYPE_DOMINATION;
503         else if(t == "ctf")   return MAPINFO_TYPE_CTF;
504         else if(t == "rune")  return MAPINFO_TYPE_RUNEMATCH;
505         else if(t == "lms")   return MAPINFO_TYPE_LMS;
506         else if(t == "arena") return MAPINFO_TYPE_ARENA;
507         else if(t == "kh")    return MAPINFO_TYPE_KEYHUNT;
508         else if(t == "as")    return MAPINFO_TYPE_ASSAULT;
509         else if(t == "ons")   return MAPINFO_TYPE_ONSLAUGHT;
510         else if(t == "race")  return MAPINFO_TYPE_RACE;
511         else if(t == "all")   return MAPINFO_TYPE_ALL;
512         else                  return 0;
513 }
514
515 // load info about a map by name into the MapInfo_Map_* globals
516 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
517 {
518         string fn;
519         string s, t;
520         float fh, fh2;
521         float r, f;
522
523         if(pGametypeToSet == 0)
524                 if(MapInfo_Cache_Retrieve(pFilename))
525                         return 1;
526
527         r = 1;
528
529         MapInfo_Map_bspname = pFilename;
530
531         // default all generic fields so they have "good" values in case something fails
532         fn = strcat("maps/", pFilename, ".mapinfo");
533         fh = fopen(fn, FILE_READ);
534         if(fh < 0)
535         {
536                 if(!pAllowGenerate)
537                         return 0;
538                 _MapInfo_Map_Reset();
539                 r = _MapInfo_Generate(pFilename);
540                 if(!r)
541                         return 0;
542                 fh = fopen(fn, FILE_WRITE);
543                 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
544                 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
545                 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
546                 fputs(fh, strcat("cdtrack ", ftos(ceil(random() * 9 + 1)), "\n")); // track from 2 to 10
547                 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)       fputs(fh, "has weapons\n");
548                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)      fputs(fh, "type dm 30 20\n");
549                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
550                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION)      fputs(fh, "type dom 200 20\n");
551                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF)             fputs(fh, "type ctf 300 20 10\n");
552                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH)       fputs(fh, "type rune 200 20\n");
553                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS)             fputs(fh, "type lms 9 20\n");
554                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA)           fputs(fh, "type arena 10 20\n");
555                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT)         fputs(fh, "type kh 1000 20 3\n");
556                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT)         fputs(fh, "type as 20\n");
557                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)            fputs(fh, "type race 5 20 15\n");
558                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT)       fputs(fh, "type ons 20\n");
559
560                 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
561                 if(fh2 >= 0)
562                 {
563                         fclose(fh2);
564                         fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
565                 }
566
567                 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
568                 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
569                 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
570                 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z (for team map)\n");
571
572                 fclose(fh);
573                 r = 2;
574                 // return r;
575                 fh = fopen(fn, FILE_READ);
576                 if(fh < 0)
577                         error("... but I just wrote it!");
578         }
579
580         _MapInfo_Map_Reset();
581         for(;;)
582         {
583                 if not((s = fgets(fh)))
584                         break;
585
586                 // catch different sorts of comments
587                 if(s == "")                    // empty lines
588                         continue;
589                 if(substring(s, 0, 1) == "#")  // UNIX style
590                         continue;
591                 if(substring(s, 0, 2) == "//") // C++ style
592                         continue;
593                 if(substring(s, 0, 1) == "_")  // q3map style
594                         continue;
595
596                 t = car(s); s = cdr(s);
597                 if(t == "title")
598                         MapInfo_Map_title = s;
599                 else if(t == "description")
600                         MapInfo_Map_description = s;
601                 else if(t == "author")
602                         MapInfo_Map_author = s;
603                 else if(t == "has")
604                 {
605                         t = car(s); s = cdr(s);
606                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
607                         else
608                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
609                 }
610                 else if(t == "type")
611                 {
612                         t = car(s); s = cdr(s);
613                         f = MapInfo_Type_FromString(t);
614                         if(f)
615                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
616                         else
617                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
618                 }
619                 else if(t == "size")
620                 {
621                         float a, b, c, d, e;
622                         t = car(s); s = cdr(s); a = stof(t);
623                         t = car(s); s = cdr(s); b = stof(t);
624                         t = car(s); s = cdr(s); c = stof(t);
625                         t = car(s); s = cdr(s); d = stof(t);
626                         t = car(s); s = cdr(s); e = stof(t);
627                         if(s == "")
628                                 print("Map ", pFilename, " contains an incorrect size line, syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
629                         else
630                         {
631                                 t = car(s); s = cdr(s); f = stof(t);
632                                 if(s == "")
633                                         print("Map ", pFilename, " contains an incorrect size line, syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
634                                 else
635                                 {
636                                         if(a >= d || b >= e || c >= f)
637                                                 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
638                                         else
639                                         {
640                                                 MapInfo_Map_mins_x = a;
641                                                 MapInfo_Map_mins_y = b;
642                                                 MapInfo_Map_mins_z = c;
643                                                 MapInfo_Map_maxs_x = d;
644                                                 MapInfo_Map_maxs_y = e;
645                                                 MapInfo_Map_maxs_z = f;
646                                         }
647                                 }
648                         }
649                 }
650                 else if(t == "settemp_for_type")
651                 {
652                         t = car(s); s = cdr(s);
653                         if((f = MapInfo_Type_FromString(t)))
654                         {
655                                 if(f & pGametypeToSet)
656                                 {
657                                         t = car(s); s = cdr(s);
658                                         if not(cvar_value_issafe(t))
659                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
660                                         else if not (cvar_value_issafe(s))
661                                                 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
662                                         else
663                                         {
664                                                 dprint("Applying temporary setting ", t, " := ", s, "\n");
665                                                 cvar_settemp(t, s);
666                                         }
667                                 }
668                         }
669                         else
670                         {
671                                 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
672                         }
673                 }
674                 else if(t == "clientsettemp_for_type")
675                 {
676                         t = car(s); s = cdr(s);
677                         if((f = MapInfo_Type_FromString(t)))
678                         {
679                                 if(f & pGametypeToSet)
680                                 {
681                                         t = car(s); s = cdr(s);
682                                         if not(cvar_value_issafe(t))
683                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
684                                         else if not (cvar_value_issafe(s))
685                                                 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
686                                         else
687                                         {
688                                                 dprint("Applying temporary client setting ", t, " := ", s, "\n");
689                                                 MapInfo_Map_clientstuff = strcat(
690                                                         MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
691                                                 );
692                                         }
693                                 }
694                         }
695                         else
696                         {
697                                 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
698                         }
699                 }
700                 else if(t == "fog")
701                 {
702                         if not(cvar_value_issafe(t))
703                                 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
704                         else
705                                 MapInfo_Map_fog = s;
706                 }
707                 else if(t == "cdtrack")
708                 {
709                         if(pGametypeToSet)
710                         {
711                                 if not(cvar_value_issafe(t))
712                                         print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
713                                 else
714                                         MapInfo_Map_clientstuff = strcat(
715                                                 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
716                                         );
717                         }
718                 }
719                 else
720                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
721         }
722         fclose(fh);
723         if(pGametypeToSet)
724                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
725                         error("Can't select the requested game type. Bailing out.");
726         MapInfo_Cache_Store();
727         if(MapInfo_Map_supportedGametypes != 0)
728                 return r;
729         dprint("Map ", pFilename, " supports no game types, ignored\n");
730         return 0;
731 }
732
733 float MapInfo_FindName(string s)
734 {
735         // if there is exactly one map of prefix s, return it
736         // if not, return the null string
737         // note that DP sorts glob results... so I can use a binary search
738         float l, r, m, cmp;
739         l = 0;
740         r = MapInfo_count;
741         // invariants: r is behind s, l-1 is equal or before
742         while(l != r)
743         {
744                 m = floor((l + r) / 2);
745                 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
746                 cmp = strcasecmp(MapInfo_FindName_match, s);
747                 if(cmp == 0)
748                         return m; // found and good
749                 if(cmp < 0)
750                         l = m + 1; // l-1 is before s
751                 else
752                         r = m; // behind s
753         }
754         MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
755         MapInfo_FindName_firstResult = l;
756         // r == l, so: l is behind s, l-1 is before
757         // SO: if there is any, l is the one with the right prefix
758         //     and l+1 may be one too
759         if(l == MapInfo_count)
760         {
761                 MapInfo_FindName_match = string_null;
762                 MapInfo_FindName_firstResult = -1;
763                 return -1; // no MapInfo_FindName_match, behind last item
764         }
765         if(!startsWithNocase(MapInfo_FindName_match, s))
766         {
767                 MapInfo_FindName_match = string_null;
768                 MapInfo_FindName_firstResult = -1;
769                 return -1; // wrong prefix
770         }
771         if(l == MapInfo_count - 1)
772                 return l; // last one, nothing can follow => unique
773         if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
774         {
775                 MapInfo_FindName_match = string_null;
776                 return -1; // ambigous MapInfo_FindName_match
777         }
778         return l;
779 }
780
781 string MapInfo_FixName(string s)
782 {
783         MapInfo_FindName(s);
784         return MapInfo_FindName_match;
785 }
786
787 float MapInfo_CurrentFeatures()
788 {
789         float req;
790         req = 0;
791         if(!(cvar("g_lms") || cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena") || !cvar("g_pickup_items") || cvar("g_race")))
792                 req |= MAPINFO_FEATURE_WEAPONS;
793         return req;
794 }
795
796 float MapInfo_CurrentGametype()
797 {
798         if(cvar("g_domination"))
799                 return MAPINFO_TYPE_DOMINATION;
800         else if(cvar("g_ctf"))
801                 return MAPINFO_TYPE_CTF;
802         else if(cvar("g_runematch"))
803                 return MAPINFO_TYPE_RUNEMATCH;
804         else if(cvar("g_tdm"))
805                 return MAPINFO_TYPE_TEAM_DEATHMATCH;
806         else if(cvar("g_assault"))
807                 return MAPINFO_TYPE_ASSAULT;
808         else if(cvar("g_lms"))
809                 return MAPINFO_TYPE_LMS;
810         else if(cvar("g_arena"))
811                 return MAPINFO_TYPE_ARENA;
812         else if(cvar("g_keyhunt"))
813                 return MAPINFO_TYPE_KEYHUNT;
814         else if(cvar("g_onslaught"))
815                 return MAPINFO_TYPE_ONSLAUGHT;
816         else if(cvar("g_race"))
817                 return MAPINFO_TYPE_RACE;
818         else
819                 return MAPINFO_TYPE_DEATHMATCH;
820 }
821
822 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
823 {
824         if(!MapInfo_Get_ByName(s, 1, 0))
825                 return 0;
826         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
827                 return 0;
828         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
829                 return 0;
830         return 1;
831 }
832
833 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
834 {
835         float r;
836         r = _MapInfo_CheckMap(s);
837         MapInfo_ClearTemps();
838         return r;
839 }
840
841 void MapInfo_SwitchGameType(float t)
842 {
843         cvar_set("gamecfg",      "0");
844         cvar_set("g_dm",         (t == MAPINFO_TYPE_DEATHMATCH)      ? "1" : "0");
845         cvar_set("g_tdm",        (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
846         cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION)      ? "1" : "0");
847         cvar_set("g_ctf",        (t == MAPINFO_TYPE_CTF)             ? "1" : "0");
848         cvar_set("g_runematch",  (t == MAPINFO_TYPE_RUNEMATCH)       ? "1" : "0");
849         cvar_set("g_lms",        (t == MAPINFO_TYPE_LMS)             ? "1" : "0");
850         cvar_set("g_arena",      (t == MAPINFO_TYPE_ARENA)           ? "1" : "0");
851         cvar_set("g_keyhunt",    (t == MAPINFO_TYPE_KEYHUNT)         ? "1" : "0");
852         cvar_set("g_assault",    (t == MAPINFO_TYPE_ASSAULT)         ? "1" : "0");
853         cvar_set("g_onslaught",  (t == MAPINFO_TYPE_ONSLAUGHT)       ? "1" : "0");
854         cvar_set("g_race",       (t == MAPINFO_TYPE_RACE)            ? "1" : "0");
855 }
856
857 void MapInfo_LoadMap(string s)
858 {
859         MapInfo_Map_supportedGametypes = 0;
860         if(!MapInfo_CheckMap(s))
861         {
862                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
863                 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
864         }
865         localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
866 }
867
868 string MapInfo_ListAllowedMaps()
869 {
870         string out;
871         float i;
872
873         // to make absolutely sure:
874         MapInfo_Enumerate();
875         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0);
876
877         out = "";
878         for(i = 0; i < MapInfo_count; ++i)
879                 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
880         return substring(out, 1, strlen(out) - 1);
881 }
882
883 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
884 {
885         float t;
886         if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
887         {
888                 if(MapInfo_Map_supportedGametypes <= 0)
889                         error("Mapinfo system is not functional at all. BAILED OUT.\n");
890
891                 t = 1;
892                 while(!(MapInfo_Map_supportedGametypes & 1))
893                 {
894                         t *= 2;
895                         MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
896                 }
897                 // t is now a supported mode!
898                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
899                 MapInfo_SwitchGameType(t);
900         }
901         cvar_settemp_restore();
902         MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
903 }
904
905 void MapInfo_ClearTemps()
906 {
907         MapInfo_Map_bspname = string_null;
908         MapInfo_Map_title = string_null;
909         MapInfo_Map_description = string_null;
910         MapInfo_Map_author = string_null;
911         MapInfo_Map_clientstuff = string_null;
912         MapInfo_Map_supportedGametypes = 0;
913         MapInfo_Map_supportedFeatures = 0;
914 }
915
916 void MapInfo_Shutdown()
917 {
918         MapInfo_ClearTemps();
919         MapInfo_Filter_Free();
920         MapInfo_Cache_Destroy();
921         if(_MapInfo_globopen)
922         {
923                 search_end(_MapInfo_globhandle);
924                 _MapInfo_globhandle = -1;
925                 _MapInfo_globopen = FALSE;
926         }
927 }