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