]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/mapinfo.qc
mapinfo: add a search function to find a map with a given unique prefix
[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 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, 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, m + 1, pEnd);
76         if not(r)
77                 return string_null; // BAIL OUT
78
79         if(MapInfo_Map_supportedGametypes & pGametype)
80                 return HugeSetOfIntegers_insert(l, m, r);
81         else
82                 return HugeSetOfIntegers_concat(l, r);
83 }
84 float MapInfo_FilterGametype(float pGametype)
85 {
86         if(_MapInfo_filtered)
87                 strunzone(_MapInfo_filtered);
88         _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, 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         MapInfo_Map_supportedGametypes = 0;
141         MapInfo_Map_diameter = 0;
142         MapInfo_Map_spawnpoints = 0;
143         for(;;)
144         {
145                 if not((s = fgets(fh)))
146                         break;
147                 if(inWorldspawn == 1)
148                         if(startsWith(s, "}"))
149                                 inWorldspawn = 0;
150                 if(inWorldspawn)
151                 {
152                         if(startsWith(s, "\"classname\" \"worldspawn\""))
153                                 inWorldspawn = 1;
154                         else if((v = extractRestOfLine(s, "\"message\" \"")))
155                         {
156                                 for(l = strlen(v) - 1; l > 0; --l)
157                                         if(substring(v, l, 1) == "\"")
158                                                 break;
159                                 MapInfo_Map_title = substring(v, 0, l);
160                         }
161                 }
162                 else
163                 {
164                         if((v = extractRestOfLine(s, "\"origin\" \"")))
165                         {
166                                 for(l = strlen(v) - 1; l > 0; --l)
167                                         if(substring(v, l, 1) == "\"")
168                                                 break;
169                                 o = stov(strcat("'", substring(v, 0, l), "'"));
170                                 mapMins_x = min(mapMins_x, o_x);
171                                 mapMins_y = min(mapMins_y, o_y);
172                                 mapMins_z = min(mapMins_z, o_z);
173                                 mapMaxs_x = max(mapMaxs_x, o_x);
174                                 mapMaxs_y = max(mapMaxs_y, o_y);
175                                 mapMaxs_z = max(mapMaxs_z, o_z);
176                         }
177                         else if((v = extractRestOfLine(s, "\"classname\" \"")))
178                         {
179                                 if(startsWith(v, "dom_controlpoint\""))
180                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
181                                 else if(startsWith(v, "item_flag_team2\""))
182                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
183                                 else if(startsWith(v, "runematch_spawn_point\""))
184                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
185                                 else if(startsWith(v, "target_assault_roundend\""))
186                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
187                                 else if(startsWith(v, "onslaught_generator\""))
188                                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
189                                 else if(startsWith(v, "info_player_team1\""))
190                                         ++MapInfo_Map_spawnpoints;
191                                 else if(startsWith(v, "info_player_team2\""))
192                                         ++MapInfo_Map_spawnpoints;
193                                 else if(startsWith(v, "info_player_deathmatch\""))
194                                         ++MapInfo_Map_spawnpoints;
195                                 else if(startsWith(v, "info_player_start\""))
196                                         ++MapInfo_Map_spawnpoints;
197                         }
198                 }
199         }
200         if(inWorldspawn)
201         {
202                 print(fn, " ended still in worldspawn, BUG\n");
203                 return 0;
204         }
205         MapInfo_Map_diameter = vlen(mapMaxs - mapMins);
206
207         twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT);
208         if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
209         {
210                 // we have a CTF-only or Assault-only map. Don't add other modes then,
211                 // as the map is too symmetric for them.
212         }
213         else
214         {
215                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
216                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;       // Rune always works
217                 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
218
219                 if(MapInfo_Map_spawnpoints >= 8  && MapInfo_Map_diameter > 4096)
220                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
221                 if(                MapInfo_Map_diameter < 4096)
222                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
223                 if(MapInfo_Map_spawnpoints >= 12 && MapInfo_Map_diameter > 5120)
224                         MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
225         }
226
227         fclose(fh);
228         dprint(fn, ": types = ", ftos(MapInfo_Map_supportedGametypes), " MapInfo_Map_spawnpoints ", ftos(MapInfo_Map_spawnpoints), " MapInfo_Map_diameter ", ftos(MapInfo_Map_diameter), "\n");
229         return r;
230 }
231
232 // load info about a map by name into the MapInfo_Map_* globals
233 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate)
234 {
235         string fn;
236         string s, t;
237         float fh;
238         float r;
239
240         // default all generic fields so they have "good" values in case something fails
241         MapInfo_Map_title = "Untitled1";
242         MapInfo_Map_description = "Bleh.";
243         MapInfo_Map_supportedGametypes = 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_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)      fputs(fh, "type dm 30 20\n");
262                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n"); // TODO count tdm_team entities
263                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION)      fputs(fh, "type dom 200 20 2\n"); // TODO count tdm_team entities
264                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF)             fputs(fh, "type ctf 300 20\n");
265                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH)       fputs(fh, "type rune 200 20\n");
266                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS)             fputs(fh, "type lms 9 20\n");
267                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA)           fputs(fh, "type arena 10 20\n");
268                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT)         fputs(fh, "type kh 1000 20 3\n");
269                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT)         fputs(fh, "type as 20\n");
270                 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT)       fputs(fh, "type ons 20\n");
271                 fclose(fh);
272                 // return r;
273                 return 2;
274         }
275         for(;;)
276         {
277                 if not((s = fgets(fh)))
278                         break;
279                 tokenize(s);
280                 t = argv(0);
281                 if(t == "title")
282                         MapInfo_Map_title = substring(s, 6, strlen(s) - 6); // without "title"
283                 else if(t == "description")
284                         MapInfo_Map_description = substring(s, 12, strlen(s) - 12);
285                 else if(t == "_diameter")
286                         MapInfo_Map_diameter = stof(argv(1));
287                 else if(t == "_spawnpoints")
288                         MapInfo_Map_spawnpoints = stof(argv(1));
289                 else if(t == "type")
290                 {
291                         t = argv(1);
292                         if     (t == "dm")    MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;
293                         else if(t == "tdm")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
294                         else if(t == "dom")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
295                         else if(t == "ctf")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
296                         else if(t == "rune")  MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
297                         else if(t == "lms")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;
298                         else if(t == "arena") MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
299                         else if(t == "kh")    MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
300                         else if(t == "as")    MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
301                         else if(t == "ons")   MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
302                         else
303                                 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
304                 }
305                 else
306                         dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
307         }
308         fclose(fh);
309         if(MapInfo_Map_supportedGametypes != 0)
310                 return 1;
311         dprint("Map ", pFilename, " supports no game types, ignored\n");
312         return 0;
313 }
314
315 string MapInfo_FixName(string s)
316 {
317         // if there is exactly one map of prefix s, return it
318         // if not, return the null string
319         // note that DP sorts glob results... so I can use a binary search
320         string match;
321         float l, r, m, cmp;
322         l = 0;
323         r = MapInfo_count;
324         // invariants: r is behind s, l-1 is equal or before
325         while(l != r)
326         {
327                 m = floor((l + r) / 2);
328                 cmp = strcasecmp(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m)), s);
329                 if(cmp == 0)
330                         return s; // found and good
331                 if(cmp < 0)
332                         l = m + 1; // l-1 is before s
333                 else
334                         r = m; // behind s
335         }
336         // r == l, so: l is behind s, l-1 is before
337         // SO: if there is any, l is the one with the right prefix
338         //     and l+1 may be one too
339         if(l == MapInfo_count)
340                 return string_null; // no match, behind last item
341         match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l));
342         if(!startsWith(match, s))
343                 return string_null; // wrong prefix
344         if(l == MapInfo_count - 1)
345                 return match; // last one, nothing can follow => unique
346         if(startsWith(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l + 1)), s))
347                 return string_null; // ambigous match
348         return match;
349 }