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