]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/mapinfo.qc
added some primitives to the mapinfo system to be aware of game mode requirements
[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
38 // GLOB HANDLING (for all BSP files)
39 float _MapInfo_globopen;
40 float _MapInfo_globcount; 
41 float _MapInfo_globhandle;
42 string _MapInfo_GlobItem(float i)
43 {
44         string s;
45         s = search_getfilename(_MapInfo_globhandle, i);
46         return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
47 }
48
49 void MapInfo_Enumerate()
50 {
51         if(_MapInfo_globopen)
52                 search_end(_MapInfo_globhandle);
53         _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
54         _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
55         _MapInfo_globopen = 1;
56 }
57
58 // filter the info by game type mask (updates MapInfo_count)
59 string _MapInfo_filtered;
60 string MapInfo_FilterGametype_Recursive(float pGametype, float pFeatures, float pBegin, float pEnd)
61 {
62         float m;
63         string l, r;
64
65         if(pBegin == pEnd)
66                 return HugeSetOfIntegers_empty();
67
68         m = floor((pBegin + pEnd) / 2);
69
70         l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m);
71         if not(l)
72                 return string_null; // BAIL OUT
73         if(MapInfo_Get_ByName(_MapInfo_GlobItem(m), 1) == 2) // if we generated one... BAIL OUT and let the caller continue in the next frame.
74                 return string_null; // BAIL OUT
75         r = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, m + 1, pEnd);
76         if not(r)
77                 return string_null; // BAIL OUT
78
79         if(((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures))
80                 return HugeSetOfIntegers_insert(l, m, r);
81         else
82                 return HugeSetOfIntegers_concat(l, r);
83 }
84 float MapInfo_FilterGametype(float pGametype, float pFeatures)
85 {
86         if(_MapInfo_filtered)
87                 strunzone(_MapInfo_filtered);
88         _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, 0, _MapInfo_globcount);
89         if(!_MapInfo_filtered)
90         {
91                 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
92                 return 0;
93         }
94         _MapInfo_filtered = strzone(_MapInfo_filtered);
95         MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
96         dprint("Filter ", ftos(pGametype), " results in ", _MapInfo_filtered, "\n");
97         // TODO clear cache
98         return 1;
99 }
100
101 // load info about the i-th map into the MapInfo_Map_* globals
102 float MapInfo_Get_ByID(float i)
103 {
104         // TODO check cache
105         if(MapInfo_Get_ByName(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i)), 0))
106         {
107                 // TODO save in cache
108                 return 1;
109         }
110         return 0;
111 }
112
113 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
114 {
115         string fn;
116         float fh;
117         string s, v;
118         vector o;
119         float inWorldspawn, l;
120         float r;
121         float twoBaseModes;
122
123         vector mapMins, mapMaxs;
124
125         r = 1;
126         fn = strcat("maps/", pFilename, ".ent");
127         fh = fopen(fn, FILE_READ);
128         if(fh < 0)
129         {
130                 r = 2;
131                 fn = strcat("maps/", pFilename, ".bsp");
132                 fh = fopen(fn, FILE_READ);
133         }
134         if(fh < 0)
135                 return 0;
136         print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
137
138         inWorldspawn = 2;
139
140         for(;;)
141         {
142                 if not((s = fgets(fh)))
143                         break;
144                 if(inWorldspawn == 1)
145                         if(startsWith(s, "}"))
146                                 inWorldspawn = 0;
147                 if(inWorldspawn)
148                 {
149                         if(startsWith(s, "\"classname\" \"worldspawn\""))
150                                 inWorldspawn = 1;
151                         else if((v = extractRestOfLine(s, "\"message\" \"")))
152                         {
153                                 for(l = strlen(v) - 1; l > 0; --l)
154                                         if(substring(v, l, 1) == "\"")
155                                                 break;
156                                 MapInfo_Map_title = substring(v, 0, l);
157                         }
158                 }
159                 else
160                 {
161                         if((v = extractRestOfLine(s, "\"origin\" \"")))
162                         {
163                                 for(l = strlen(v) - 1; l > 0; --l)
164                                         if(substring(v, l, 1) == "\"")
165                                                 break;
166                                 o = stov(strcat("'", substring(v, 0, l), "'"));
167                                 mapMins_x = min(mapMins_x, o_x);
168                                 mapMins_y = min(mapMins_y, o_y);
169                                 mapMins_z = min(mapMins_z, o_z);
170                                 mapMaxs_x = max(mapMaxs_x, o_x);
171                                 mapMaxs_y = max(mapMaxs_y, o_y);
172                                 mapMaxs_z = max(mapMaxs_z, o_z);
173                         }
174                         else if((v = extractRestOfLine(s, "\"classname\" \"")))
175                         {
176                                 if(startsWith(v, "dom_controlpoint\""))
177                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
178                                 else if(startsWith(v, "item_flag_team2\""))
179                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
180                                 else if(startsWith(v, "runematch_spawn_point\""))
181                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
182                                 else if(startsWith(v, "target_assault_roundend\""))
183                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
184                                 else if(startsWith(v, "onslaught_generator\""))
185                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
186                                 else if(startsWith(v, "info_player_team1\""))
187                                         ++MapInfo_Map_spawnpoints;
188                                 else if(startsWith(v, "info_player_team2\""))
189                                         ++MapInfo_Map_spawnpoints;
190                                 else if(startsWith(v, "info_player_deathmatch\""))
191                                         ++MapInfo_Map_spawnpoints;
192                                 else if(startsWith(v, "info_player_start\""))
193                                         ++MapInfo_Map_spawnpoints;
194                                 else if(startsWith(v, "weapon_") && !startsWith(v, "weapon_nex\"") && !startsWith(v, "weapon_railgun\""))
195                                         MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
196                         }
197                 }
198         }
199         if(inWorldspawn)
200         {
201                 print(fn, " ended still in worldspawn, BUG\n");
202                 return 0;
203         }
204         MapInfo_Map_diameter = vlen(mapMaxs - mapMins);
205
206         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT);
207         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
208         {
209                 // we have a CTF-only or Assault-only map. Don't add other modes then,
210                 // as the map is too symmetric for them.
211         }
212         else
213         {
214                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
215                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;       // Rune always works
216                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
217
218                 if(MapInfo_Map_spawnpoints >= 8  && MapInfo_Map_diameter > 4096)
219                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
220                 if(                MapInfo_Map_diameter < 4096)
221                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
222                 if(MapInfo_Map_spawnpoints >= 12 && MapInfo_Map_diameter > 5120)
223                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
224         }
225
226         fclose(fh);
227         dprint(fn, ": types = ", ftos(MapInfo_Map_supportedGametypes), " MapInfo_Map_spawnpoints ", ftos(MapInfo_Map_spawnpoints), " MapInfo_Map_diameter ", ftos(MapInfo_Map_diameter), "\n");
228         return r;
229 }
230
231 // load info about a map by name into the MapInfo_Map_* globals
232 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate)
233 {
234         string fn;
235         string s, t;
236         float fh;
237         float r;
238
239         // default all generic fields so they have "good" values in case something fails
240         MapInfo_Map_title = "Untitled1";
241         MapInfo_Map_description = "Bleh.";
242         MapInfo_Map_supportedGametypes = 0;
243         MapInfo_Map_supportedFeatures = 0;
244         MapInfo_Map_diameter = 0;
245         MapInfo_Map_spawnpoints = 0;
246
247         fn = strcat("maps/", pFilename, ".mapinfo");
248         fh = fopen(fn, FILE_READ);
249         if(fh < 0)
250         {
251                 if(!pAllowGenerate)
252                         return 0;
253                 r = _MapInfo_Generate(pFilename);
254                 if(!r)
255                         return 0;
256                 fh = fopen(fn, FILE_WRITE);
257                 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
258                 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
259                 fputs(fh, strcat("_diameter ", ftos(MapInfo_Map_diameter), "\n"));
260                 fputs(fh, strcat("_spawnpoints ", ftos(MapInfo_Map_spawnpoints), "\n"));
261                 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)       fputs(fh, "has weapons\n");
262                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)      fputs(fh, "type dm 30 20\n");
263                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n"); // TODO count tdm_team entities
264                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION)      fputs(fh, "type dom 200 20 2\n"); // TODO count tdm_team entities
265                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF)             fputs(fh, "type ctf 300 20\n");
266                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH)       fputs(fh, "type rune 200 20\n");
267                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS)             fputs(fh, "type lms 9 20\n");
268                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA)           fputs(fh, "type arena 10 20\n");
269                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT)         fputs(fh, "type kh 1000 20 3\n");
270                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT)         fputs(fh, "type as 20\n");
271                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT)       fputs(fh, "type ons 20\n");
272                 fclose(fh);
273                 // return r;
274                 return 2;
275         }
276         for(;;)
277         {
278                 if not((s = fgets(fh)))
279                         break;
280                 tokenize(s);
281                 t = argv(0);
282                 if(t == "title")
283                         MapInfo_Map_title = substring(s, 6, strlen(s) - 6); // without "title"
284                 else if(t == "description")
285                         MapInfo_Map_description = substring(s, 12, strlen(s) - 12);
286                 else if(t == "_diameter")
287                         MapInfo_Map_diameter = stof(argv(1));
288                 else if(t == "_spawnpoints")
289                         MapInfo_Map_spawnpoints = stof(argv(1));
290                 else if(t == "has")
291                 {
292                         t = argv(1);
293                         if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
294                         else
295                                 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
296                 }
297                 else if(t == "type")
298                 {
299                         t = argv(1);
300                         if     (t == "dm")    MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;
301                         else if(t == "tdm")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
302                         else if(t == "dom")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
303                         else if(t == "ctf")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
304                         else if(t == "rune")  MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
305                         else if(t == "lms")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;
306                         else if(t == "arena") MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
307                         else if(t == "kh")    MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
308                         else if(t == "as")    MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
309                         else if(t == "ons")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
310                         else
311                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
312                 }
313                 else
314                         dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
315         }
316         fclose(fh);
317         if(MapInfo_Map_supportedGametypes != 0)
318                 return 1;
319         dprint("Map ", pFilename, " supports no game types, ignored\n");
320         return 0;
321 }
322
323 string MapInfo_FixName(string s)
324 {
325         // if there is exactly one map of prefix s, return it
326         // if not, return the null string
327         // note that DP sorts glob results... so I can use a binary search
328         string match;
329         float l, r, m, cmp;
330         l = 0;
331         r = MapInfo_count;
332         // invariants: r is behind s, l-1 is equal or before
333         while(l != r)
334         {
335                 m = floor((l + r) / 2);
336                 cmp = strcasecmp(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m)), s);
337                 if(cmp == 0)
338                         return s; // found and good
339                 if(cmp < 0)
340                         l = m + 1; // l-1 is before s
341                 else
342                         r = m; // behind s
343         }
344         // r == l, so: l is behind s, l-1 is before
345         // SO: if there is any, l is the one with the right prefix
346         //     and l+1 may be one too
347         if(l == MapInfo_count)
348                 return string_null; // no match, behind last item
349         match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l));
350         if(!startsWith(match, s))
351                 return string_null; // wrong prefix
352         if(l == MapInfo_count - 1)
353                 return match; // last one, nothing can follow => unique
354         if(startsWith(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l + 1)), s))
355                 return string_null; // ambigous match
356         return match;
357 }
358
359 float MapInfo_CurrentFeatures()
360 {
361         float req;
362         req = 0;
363         if(!(cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena")))
364                 req |= MAPINFO_FEATURE_WEAPONS;
365         return req;
366 }
367
368 float MapInfo_CurrentGametype()
369 {
370         if(cvar("g_domination"))
371                 return MAPINFO_TYPE_DOMINATION;
372         else if(cvar("g_ctf"))
373                 return MAPINFO_TYPE_CTF;
374         else if(cvar("g_runematch"))
375                 return MAPINFO_TYPE_RUNEMATCH;
376         else if(cvar("g_tdm"))
377                 return MAPINFO_TYPE_TEAM_DEATHMATCH;
378         else if(cvar("g_assault"))
379                 return MAPINFO_TYPE_ASSAULT;
380         else if(cvar("g_lms"))
381                 return MAPINFO_TYPE_LMS;
382         else if(cvar("g_arena"))
383                 return MAPINFO_TYPE_ARENA;
384         else if(cvar("g_keyhunt"))
385                 return MAPINFO_TYPE_KEYHUNT;
386         else if(cvar("g_onslaught"))
387                 return MAPINFO_TYPE_ONSLAUGHT;
388         else
389                 return MAPINFO_TYPE_DEATHMATCH;
390 }
391
392 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
393 {
394         if(!MapInfo_Get_ByName(s, 1))
395                 return 0;
396         if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
397                 return 0;
398         if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
399                 return 0;
400         return 1;
401 }
402
403 void MapInfo_LoadMap(string s)
404 {
405         if(!MapInfo_CheckMap(s))
406         {
407                 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to deathmatch.\n");
408                 cvar_set("g_domination", "0");
409                 cvar_set("g_ctf", "0");
410                 cvar_set("g_runematch", "0");
411                 cvar_set("g_tdm", "0");
412                 cvar_set("g_assault", "0");
413                 cvar_set("g_lms", "0");
414                 cvar_set("g_arena", "0");
415                 cvar_set("g_keyhunt", "0");
416                 cvar_set("g_onslaught", "0");
417         }
418         localcmd(strcat("\nchangelevel ", s, "\n"));
419 }