]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/mapinfo.qc
oops... forgot to commit this
[divverent/nexuiz.git] / data / qcsrc / common / mapinfo.qc
1 // HUGE SET - stored in a string
2 string HugeSetOfIntegers_empty()
3 {
4         return "";
5 }
6 float HugeSetOfIntegers_get(string pArr, float i)
7 {
8         return stof(substring(pArr, i * 4, 4));
9 }
10 float HugeSetOfIntegers_length(string pArr)
11 {
12         return strlen(pArr) / 4;
13 }
14 string HugeSetOfIntegers_concat(string a1, string a2)
15 {
16         return strcat(a1, a2);
17 }
18 string HugeSetOfIntegers_insert(string a1, float n, string a2)
19         // special concat function to build up large lists in less time by binary concatenation
20 {
21         string s;
22         s = strcat("    ", ftos(n));
23         return strcat(a1, substring(s, strlen(s) - 4, 4), a2);
24 }
25
26 // generic string stuff
27 float startsWith(string haystack, string needle)
28 {
29         return substring(haystack, 0, strlen(needle)) == needle;
30 }
31 string extractRestOfLine(string haystack, string needle)
32 {
33         if(startsWith(haystack, needle))
34                 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
35         return string_null;
36 }
37 string car(string s)
38 {
39         float o;
40         o = strstrofs(s, " ", 0);
41         if(o < 0)
42                 return s;
43         return substring(s, 0, o);
44 }
45 string cdr(string s)
46 {
47         float o;
48         o = strstrofs(s, " ", 0);
49         if(o < 0)
50                 return string_null;
51         return substring(s, o + 1, strlen(s) - (o + 1));
52 }
53
54 // GLOB HANDLING (for all BSP files)
55 float _MapInfo_globopen;
56 float _MapInfo_globcount; 
57 float _MapInfo_globhandle;
58 string _MapInfo_GlobItem(float i)
59 {
60         string s;
61         s = search_getfilename(_MapInfo_globhandle, i);
62         return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
63 }
64
65 void MapInfo_Enumerate()
66 {
67         if(_MapInfo_globopen)
68                 search_end(_MapInfo_globhandle);
69         _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
70         _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
71         _MapInfo_globopen = 1;
72 }
73
74 // filter the info by game type mask (updates MapInfo_count)
75 string _MapInfo_filtered;
76 string MapInfo_FilterGametype_Recursive(float pGametype, float pFeatures, float pBegin, float pEnd)
77 {
78         float m, valid;
79         string l, r;
80
81         if(pBegin == pEnd)
82                 return HugeSetOfIntegers_empty();
83
84         m = floor((pBegin + pEnd) / 2);
85
86         l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m);
87         if not(l)
88                 return string_null; // BAIL OUT
89         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.
90                 return string_null; // BAIL OUT
91         valid = (((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures));
92         r = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, m + 1, pEnd);
93         if not(r)
94                 return string_null; // BAIL OUT
95
96         if(valid)
97                 return HugeSetOfIntegers_insert(l, m, r);
98         else
99                 return HugeSetOfIntegers_concat(l, r);
100 }
101 float MapInfo_FilterGametype(float pGametype, float pFeatures)
102 {
103         if(_MapInfo_filtered)
104                 strunzone(_MapInfo_filtered);
105         _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, 0, _MapInfo_globcount);
106         if not(_MapInfo_filtered)
107         {
108                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
109                 return 0;
110         }
111         _MapInfo_filtered = strzone(_MapInfo_filtered);
112         MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
113         print("Filter ", ftos(pGametype), "/", ftos(pFeatures), " has ", ftos(MapInfo_count), "\n");
114         // TODO clear cache
115         return 1;
116 }
117
118 // load info about the i-th map into the MapInfo_Map_* globals
119 string MapInfo_BSPName_ByID(float i)
120 {
121         return _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i));
122 }
123
124 float MapInfo_Get_ByID(float i)
125 {
126         // TODO check cache
127         if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
128         {
129                 // TODO save in cache
130                 return 1;
131         }
132         return 0;
133 }
134
135 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
136 {
137         string fn;
138         float fh;
139         string s, v;
140         vector o;
141         float inWorldspawn, l;
142         float r;
143         float twoBaseModes;
144
145         vector mapMins, mapMaxs;
146
147         r = 1;
148         fn = strcat("maps/", pFilename, ".ent");
149         fh = fopen(fn, FILE_READ);
150         if(fh < 0)
151         {
152                 r = 2;
153                 fn = strcat("maps/", pFilename, ".bsp");
154                 fh = fopen(fn, FILE_READ);
155         }
156         if(fh < 0)
157                 return 0;
158         print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
159
160         inWorldspawn = 2;
161         MapInfo_Map_supportedGametypes = 0;
162
163         for(;;)
164         {
165                 if not((s = fgets(fh)))
166                         break;
167                 if(inWorldspawn == 1)
168                         if(startsWith(s, "}"))
169                                 inWorldspawn = 0;
170                 if(inWorldspawn)
171                 {
172                         if(startsWith(s, "\"classname\" \"worldspawn\""))
173                                 inWorldspawn = 1;
174                         else if((v = extractRestOfLine(s, "\"author\" \"")))
175                         {
176                                 for(l = strlen(v) - 1; l > 0; --l)
177                                         if(substring(v, l, 1) == "\"")
178                                                 break;
179                                 MapInfo_Map_author = substring(v, 0, l);
180                         }
181                         else if((v = extractRestOfLine(s, "\"message\" \"")))
182                         {
183                                 for(l = strlen(v) - 1; l > 0; --l)
184                                         if(substring(v, l, 1) == "\"")
185                                                 break;
186                                 MapInfo_Map_title = substring(v, 0, l);
187                         }
188                 }
189                 else
190                 {
191                         if((v = extractRestOfLine(s, "\"origin\" \"")))
192                         {
193                                 for(l = strlen(v) - 1; l > 0; --l)
194                                         if(substring(v, l, 1) == "\"")
195                                                 break;
196                                 o = stov(strcat("'", substring(v, 0, l), "'"));
197                                 mapMins_x = min(mapMins_x, o_x);
198                                 mapMins_y = min(mapMins_y, o_y);
199                                 mapMins_z = min(mapMins_z, o_z);
200                                 mapMaxs_x = max(mapMaxs_x, o_x);
201                                 mapMaxs_y = max(mapMaxs_y, o_y);
202                                 mapMaxs_z = max(mapMaxs_z, o_z);
203                         }
204                         else if((v = extractRestOfLine(s, "\"classname\" \"")))
205                         {
206                                 if(startsWith(v, "dom_controlpoint\""))
207                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
208                                 else if(startsWith(v, "item_flag_team2\""))
209                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
210                                 else if(startsWith(v, "runematch_spawn_point\""))
211                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
212                                 else if(startsWith(v, "target_assault_roundend\""))
213                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
214                                 else if(startsWith(v, "onslaught_generator\""))
215                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
216                                 else if(startsWith(v, "info_player_team1\""))
217                                         ++MapInfo_Map_spawnpoints;
218                                 else if(startsWith(v, "info_player_team2\""))
219                                         ++MapInfo_Map_spawnpoints;
220                                 else if(startsWith(v, "info_player_deathmatch\""))
221                                         ++MapInfo_Map_spawnpoints;
222                                 else if(startsWith(v, "info_player_start\""))
223                                         ++MapInfo_Map_spawnpoints;
224                                 else if(startsWith(v, "weapon_") && !startsWith(v, "weapon_nex\"") && !startsWith(v, "weapon_railgun\""))
225                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
226                         }
227                 }
228         }
229         if(inWorldspawn)
230         {
231                 print(fn, " ended still in worldspawn, BUG\n");
232                 return 0;
233         }
234         MapInfo_Map_diameter = vlen(mapMaxs - mapMins);
235
236         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT);
237         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
238         {
239                 // we have a CTF-only or Assault-only map. Don't add other modes then,
240                 // as the map is too symmetric for them.
241         }
242         else
243         {
244                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
245                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;       // Rune always works
246                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
247
248                 if(MapInfo_Map_spawnpoints >= 8  && MapInfo_Map_diameter > 4096)
249                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
250                 if(                MapInfo_Map_diameter < 4096)
251                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
252                 if(MapInfo_Map_spawnpoints >= 12 && MapInfo_Map_diameter > 5120)
253                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
254         }
255
256         fclose(fh);
257         return r;
258 }
259
260 void _MapInfo_Map_Reset()
261 {
262         MapInfo_Map_title = "Untitled1";
263         MapInfo_Map_description = "Bleh.";
264         MapInfo_Map_author = "He-Who-Must-Not-Be-Named";
265         MapInfo_Map_supportedGametypes = 0;
266         MapInfo_Map_supportedFeatures = 0;
267         MapInfo_Map_diameter = 0;
268         MapInfo_Map_spawnpoints = 0;
269 }
270
271 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
272 {
273         MapInfo_Map_supportedGametypes |= pThisType;
274         if(!(pThisType & pWantedType))
275                 return;
276         
277         cvar_set("fraglimit", car(s));
278         s = cdr(s);
279
280         cvar_set("timelimit", car(s));
281         s = cdr(s);
282
283         if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
284         {
285                 cvar_set("g_tdm_teams", car(s));
286                 s = cdr(s);
287         }
288
289         if(pWantedType == MAPINFO_TYPE_KEYHUNT)
290         {
291                 cvar_set("g_keyhunt_teams", car(s));
292                 s = cdr(s);
293         }
294 }
295
296 float MapInfo_Type_FromString(string t)
297 {
298         if     (t == "dm")    return MAPINFO_TYPE_DEATHMATCH;
299         else if(t == "tdm")   return MAPINFO_TYPE_TEAM_DEATHMATCH;
300         else if(t == "dom")   return MAPINFO_TYPE_DOMINATION;
301         else if(t == "ctf")   return MAPINFO_TYPE_CTF;
302         else if(t == "rune")  return MAPINFO_TYPE_RUNEMATCH;
303         else if(t == "lms")   return MAPINFO_TYPE_LMS;
304         else if(t == "arena") return MAPINFO_TYPE_ARENA;
305         else if(t == "kh")    return MAPINFO_TYPE_KEYHUNT;
306         else if(t == "as")    return MAPINFO_TYPE_ASSAULT;
307         else if(t == "ons")   return MAPINFO_TYPE_ONSLAUGHT;
308         else                  return 0;
309 }
310
311 // load info about a map by name into the MapInfo_Map_* globals
312 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
313 {
314         string fn;
315         string s, t;
316         float fh;
317         float r, f;
318
319         r = 1;
320
321         MapInfo_Map_bspname = pFilename;
322
323         // default all generic fields so they have "good" values in case something fails
324         fn = strcat("maps/", pFilename, ".mapinfo");
325         fh = fopen(fn, FILE_READ);
326         if(fh < 0)
327         {
328                 if(!pAllowGenerate)
329                         return 0;
330                 _MapInfo_Map_Reset();
331                 r = _MapInfo_Generate(pFilename);
332                 if(!r)
333                         return 0;
334                 fh = fopen(fn, FILE_WRITE);
335                 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
336                 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
337                 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
338                 fputs(fh, strcat("_diameter ", ftos(MapInfo_Map_diameter), "\n"));
339                 fputs(fh, strcat("_spawnpoints ", ftos(MapInfo_Map_spawnpoints), "\n"));
340                 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)       fputs(fh, "has weapons\n");
341                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)      fputs(fh, "type dm 30 20\n");
342                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
343                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION)      fputs(fh, "type dom 200 20\n");
344                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF)             fputs(fh, "type ctf 300 20\n");
345                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH)       fputs(fh, "type rune 200 20\n");
346                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS)             fputs(fh, "type lms 9 20\n");
347                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA)           fputs(fh, "type arena 10 20\n");
348                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT)         fputs(fh, "type kh 1000 20 3\n");
349                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT)         fputs(fh, "type as 20\n");
350                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT)       fputs(fh, "type ons 20\n");
351                 fclose(fh);
352                 r = 2;
353                 // return r;
354                 fh = fopen(fn, FILE_READ);
355                 if(fh < 0)
356                         error("... but I just wrote it!");
357         }
358
359         _MapInfo_Map_Reset();
360         for(;;)
361         {
362                 if not((s = fgets(fh)))
363                         break;
364                 t = car(s); s = cdr(s);
365                 if     (t == "title")
366                         MapInfo_Map_title = s;
367                 else if(t == "description")
368                         MapInfo_Map_description = s;
369                 else if(t == "author")
370                         MapInfo_Map_author = s;
371                 else if(t == "_diameter")
372                         MapInfo_Map_diameter = stof(s);
373                 else if(t == "_spawnpoints")
374                         MapInfo_Map_spawnpoints = stof(s);
375                 else if(t == "has")
376                 {
377                         t = car(s); s = cdr(s);
378                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
379                         else
380                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
381                 }
382                 else if(t == "type")
383                 {
384                         t = car(s); s = cdr(s);
385                         f = MapInfo_Type_FromString(t);
386                         if(f)
387                                 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
388                         else
389                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
390                 }
391                 else
392                         dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
393         }
394         fclose(fh);
395         if(pGametypeToSet)
396                 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
397                         error("Can't select the requested game type. Bailing out.");
398         if(MapInfo_Map_supportedGametypes != 0)
399                 return r;
400         dprint("Map ", pFilename, " supports no game types, ignored\n");
401         return 0;
402 }
403
404 string _MapInfo_FindName_match;
405 float MapInfo_FindName(string s)
406 {
407         // if there is exactly one map of prefix s, return it
408         // if not, return the null string
409         // note that DP sorts glob results... so I can use a binary search
410         float l, r, m, cmp;
411         l = 0;
412         r = MapInfo_count;
413         _MapInfo_FindName_match = string_null;
414         // invariants: r is behind s, l-1 is equal or before
415         while(l != r)
416         {
417                 m = floor((l + r) / 2);
418                 cmp = strcasecmp(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m)), s);
419                 if(cmp == 0)
420                         return m; // found and good
421                 if(cmp < 0)
422                         l = m + 1; // l-1 is before s
423                 else
424                         r = m; // behind s
425         }
426         // r == l, so: l is behind s, l-1 is before
427         // SO: if there is any, l is the one with the right prefix
428         //     and l+1 may be one too
429         if(l == MapInfo_count)
430                 return -1; // no _MapInfo_FindName_match, behind last item
431         _MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l));
432         if(!startsWith(_MapInfo_FindName_match, s))
433                 return -1; // wrong prefix
434         if(l == MapInfo_count - 1)
435                 return l; // last one, nothing can follow => unique
436         if(startsWith(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l + 1)), s))
437                 return -1; // ambigous _MapInfo_FindName_match
438         return l;
439 }
440
441 string MapInfo_FixName(string s)
442 {
443         MapInfo_FindName(s);
444         return _MapInfo_FindName_match;
445 }
446
447 float MapInfo_CurrentFeatures()
448 {
449         float req;
450         req = 0;
451         if(!(cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena")))
452                 req |= MAPINFO_FEATURE_WEAPONS;
453         print(ftos(req), "\n");
454         return req;
455 }
456
457 float MapInfo_CurrentGametype()
458 {
459         if(cvar("g_domination"))
460                 return MAPINFO_TYPE_DOMINATION;
461         else if(cvar("g_ctf"))
462                 return MAPINFO_TYPE_CTF;
463         else if(cvar("g_runematch"))
464                 return MAPINFO_TYPE_RUNEMATCH;
465         else if(cvar("g_tdm"))
466                 return MAPINFO_TYPE_TEAM_DEATHMATCH;
467         else if(cvar("g_assault"))
468                 return MAPINFO_TYPE_ASSAULT;
469         else if(cvar("g_lms"))
470                 return MAPINFO_TYPE_LMS;
471         else if(cvar("g_arena"))
472                 return MAPINFO_TYPE_ARENA;
473         else if(cvar("g_keyhunt"))
474                 return MAPINFO_TYPE_KEYHUNT;
475         else if(cvar("g_onslaught"))
476                 return MAPINFO_TYPE_ONSLAUGHT;
477         else
478                 return MAPINFO_TYPE_DEATHMATCH;
479 }
480
481 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
482 {
483         if(!MapInfo_Get_ByName(s, 1, 0))
484                 return 0;
485         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
486                 return 0;
487         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
488                 return 0;
489         return 1;
490 }
491
492 void MapInfo_SwitchGameType(float t)
493 {
494         cvar_set("gamecfg",      "0");
495         cvar_set("g_dm",         (t == MAPINFO_TYPE_DEATHMATCH)      ? "0" : "1");
496         cvar_set("g_tdm",        (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "0" : "1");
497         cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION)      ? "0" : "1");
498         cvar_set("g_ctf",        (t == MAPINFO_TYPE_CTF)             ? "0" : "1");
499         cvar_set("g_runematch",  (t == MAPINFO_TYPE_RUNEMATCH)       ? "0" : "1");
500         cvar_set("g_lms",        (t == MAPINFO_TYPE_LMS)             ? "0" : "1");
501         cvar_set("g_arena",      (t == MAPINFO_TYPE_ARENA)           ? "0" : "1");
502         cvar_set("g_keyhunt",    (t == MAPINFO_TYPE_KEYHUNT)         ? "0" : "1");
503         cvar_set("g_assault",    (t == MAPINFO_TYPE_ASSAULT)         ? "0" : "1");
504         cvar_set("g_onslaught",  (t == MAPINFO_TYPE_ONSLAUGHT)       ? "0" : "1");
505 }
506
507 void MapInfo_LoadMap(string s)
508 {
509         if(!MapInfo_CheckMap(s))
510         {
511                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to deathmatch.\n");
512                 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
513         }
514         MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
515         localcmd(strcat("\nchangelevel ", s, "\n"));
516 }
517
518 string MapInfo_ListAllowedMaps()
519 {
520         string out;
521         float i;
522
523         // to make absolutely sure:
524         MapInfo_Enumerate();
525         for(;;)
526                 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures()))
527                         break;
528
529         out = "";
530         for(i = 0; i < MapInfo_count; ++i)
531                 out = strcat(out, " ", _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i)));
532         return substring(out, 1, strlen(out) - 1);
533 }