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