1 // HUGE SET - stored in a string
2 string HugeSetOfIntegers_empty()
6 float HugeSetOfIntegers_get(string pArr, float i)
8 return stof(substring(pArr, i * 4, 4));
10 float HugeSetOfIntegers_length(string pArr)
12 return strlen(pArr) / 4;
14 string HugeSetOfIntegers_concat(string a1, string a2)
16 return strcat(a1, a2);
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
22 s = strcat(" ", ftos(n));
23 return strcat(a1, substring(s, strlen(s) - 4, 4), a2);
26 // generic string stuff
27 float startsWith(string haystack, string needle)
29 return substring(haystack, 0, strlen(needle)) == needle;
31 string extractRestOfLine(string haystack, string needle)
33 if(startsWith(haystack, needle))
34 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
40 o = strstrofs(s, " ", 0);
43 return substring(s, 0, o);
48 o = strstrofs(s, " ", 0);
51 return substring(s, o + 1, strlen(s) - (o + 1));
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)
61 s = search_getfilename(_MapInfo_globhandle, i);
62 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
65 void MapInfo_Enumerate()
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;
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)
82 return HugeSetOfIntegers_empty();
84 m = floor((pBegin + pEnd) / 2);
86 l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m);
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);
94 return string_null; // BAIL OUT
97 return HugeSetOfIntegers_insert(l, m, r);
99 return HugeSetOfIntegers_concat(l, r);
101 float MapInfo_FilterGametype(float pGametype, float pFeatures)
105 if(_MapInfo_filtered)
106 strunzone(_MapInfo_filtered);
107 _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, 0, _MapInfo_globcount);
108 if(!_MapInfo_filtered)
110 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
113 _MapInfo_filtered = strzone(_MapInfo_filtered);
114 MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
115 dprint("Filter ", ftos(pGametype), "/", ftos(pFeatures), " results in ", _MapInfo_filtered, "\n");
120 // load info about the i-th map into the MapInfo_Map_* globals
121 float MapInfo_Get_ByID(float i)
124 if(MapInfo_Get_ByName(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i)), 0, 0))
126 // TODO save in cache
132 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
138 float inWorldspawn, l;
142 vector mapMins, mapMaxs;
145 fn = strcat("maps/", pFilename, ".ent");
146 fh = fopen(fn, FILE_READ);
150 fn = strcat("maps/", pFilename, ".bsp");
151 fh = fopen(fn, FILE_READ);
155 print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
158 MapInfo_Map_supportedGametypes = 0;
162 if not((s = fgets(fh)))
164 if(inWorldspawn == 1)
165 if(startsWith(s, "}"))
169 if(startsWith(s, "\"classname\" \"worldspawn\""))
171 else if((v = extractRestOfLine(s, "\"message\" \"")))
173 for(l = strlen(v) - 1; l > 0; --l)
174 if(substring(v, l, 1) == "\"")
176 MapInfo_Map_title = substring(v, 0, l);
181 if((v = extractRestOfLine(s, "\"origin\" \"")))
183 for(l = strlen(v) - 1; l > 0; --l)
184 if(substring(v, l, 1) == "\"")
186 o = stov(strcat("'", substring(v, 0, l), "'"));
187 mapMins_x = min(mapMins_x, o_x);
188 mapMins_y = min(mapMins_y, o_y);
189 mapMins_z = min(mapMins_z, o_z);
190 mapMaxs_x = max(mapMaxs_x, o_x);
191 mapMaxs_y = max(mapMaxs_y, o_y);
192 mapMaxs_z = max(mapMaxs_z, o_z);
194 else if((v = extractRestOfLine(s, "\"classname\" \"")))
196 if(startsWith(v, "dom_controlpoint\""))
197 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
198 else if(startsWith(v, "item_flag_team2\""))
199 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
200 else if(startsWith(v, "runematch_spawn_point\""))
201 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
202 else if(startsWith(v, "target_assault_roundend\""))
203 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
204 else if(startsWith(v, "onslaught_generator\""))
205 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
206 else if(startsWith(v, "info_player_team1\""))
207 ++MapInfo_Map_spawnpoints;
208 else if(startsWith(v, "info_player_team2\""))
209 ++MapInfo_Map_spawnpoints;
210 else if(startsWith(v, "info_player_deathmatch\""))
211 ++MapInfo_Map_spawnpoints;
212 else if(startsWith(v, "info_player_start\""))
213 ++MapInfo_Map_spawnpoints;
214 else if(startsWith(v, "weapon_") && !startsWith(v, "weapon_nex\"") && !startsWith(v, "weapon_railgun\""))
215 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
221 print(fn, " ended still in worldspawn, BUG\n");
224 MapInfo_Map_diameter = vlen(mapMaxs - mapMins);
226 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT);
227 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
229 // we have a CTF-only or Assault-only map. Don't add other modes then,
230 // as the map is too symmetric for them.
234 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
235 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH; // Rune always works
236 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
238 if(MapInfo_Map_spawnpoints >= 8 && MapInfo_Map_diameter > 4096)
239 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
240 if( MapInfo_Map_diameter < 4096)
241 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
242 if(MapInfo_Map_spawnpoints >= 12 && MapInfo_Map_diameter > 5120)
243 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
247 dprint(fn, ": types = ", ftos(MapInfo_Map_supportedGametypes), " MapInfo_Map_spawnpoints ", ftos(MapInfo_Map_spawnpoints), " MapInfo_Map_diameter ", ftos(MapInfo_Map_diameter), "\n");
251 void _MapInfo_Map_Reset()
253 MapInfo_Map_title = "Untitled1";
254 MapInfo_Map_description = "Bleh.";
255 MapInfo_Map_supportedGametypes = 0;
256 MapInfo_Map_supportedFeatures = 0;
257 MapInfo_Map_diameter = 0;
258 MapInfo_Map_spawnpoints = 0;
261 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
263 MapInfo_Map_supportedGametypes |= pThisType;
264 if(!(pThisType & pWantedType))
267 cvar_set("fraglimit", car(s));
270 cvar_set("timelimit", car(s));
273 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
275 cvar_set("g_tdm_teams", car(s));
279 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
281 cvar_set("g_keyhunt_teams", car(s));
286 // load info about a map by name into the MapInfo_Map_* globals
287 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
296 // default all generic fields so they have "good" values in case something fails
297 fn = strcat("maps/", pFilename, ".mapinfo");
298 fh = fopen(fn, FILE_READ);
303 _MapInfo_Map_Reset();
304 r = _MapInfo_Generate(pFilename);
307 fh = fopen(fn, FILE_WRITE);
308 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
309 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
310 fputs(fh, strcat("_diameter ", ftos(MapInfo_Map_diameter), "\n"));
311 fputs(fh, strcat("_spawnpoints ", ftos(MapInfo_Map_spawnpoints), "\n"));
312 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS) fputs(fh, "has weapons\n");
313 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH) fputs(fh, "type dm 30 20\n");
314 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
315 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION) fputs(fh, "type dom 200 20\n");
316 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF) fputs(fh, "type ctf 300 20\n");
317 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH) fputs(fh, "type rune 200 20\n");
318 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS) fputs(fh, "type lms 9 20\n");
319 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA) fputs(fh, "type arena 10 20\n");
320 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT) fputs(fh, "type kh 1000 20 3\n");
321 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT) fputs(fh, "type as 20\n");
322 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT) fputs(fh, "type ons 20\n");
326 fh = fopen(fn, FILE_READ);
328 error("... but I just wrote it!");
331 _MapInfo_Map_Reset();
334 if not((s = fgets(fh)))
336 t = car(s); s = cdr(s);
338 MapInfo_Map_title = t;
339 else if(t == "description")
340 MapInfo_Map_description = substring(s, 12, strlen(s) - 12);
341 else if(t == "_diameter")
342 MapInfo_Map_diameter = stof(s);
343 else if(t == "_spawnpoints")
344 MapInfo_Map_spawnpoints = stof(s);
347 t = car(s); s = cdr(s);
348 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
350 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
354 t = car(s); s = cdr(s);
355 if (t == "dm") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
356 else if(t == "tdm") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_TEAM_DEATHMATCH);
357 else if(t == "dom") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_DOMINATION);
358 else if(t == "ctf") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_CTF);
359 else if(t == "rune") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_RUNEMATCH);
360 else if(t == "lms") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_LMS);
361 else if(t == "arena") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_ARENA);
362 else if(t == "kh") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_KEYHUNT);
363 else if(t == "as") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_ASSAULT);
364 else if(t == "ons") _MapInfo_Map_ApplyGametype (s, pGametypeToSet, MAPINFO_TYPE_ONSLAUGHT);
366 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
369 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
372 print(pFilename, " -> ", ftos(MapInfo_Map_supportedGametypes), ", ", ftos(MapInfo_Map_supportedFeatures), "\n");
374 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
375 error("Can't select the requested game type. Bailing out.");
376 if(MapInfo_Map_supportedGametypes != 0)
378 dprint("Map ", pFilename, " supports no game types, ignored\n");
382 string MapInfo_FixName(string s)
384 // if there is exactly one map of prefix s, return it
385 // if not, return the null string
386 // note that DP sorts glob results... so I can use a binary search
390 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures());
393 // invariants: r is behind s, l-1 is equal or before
396 m = floor((l + r) / 2);
397 cmp = strcasecmp(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m)), s);
399 return s; // found and good
401 l = m + 1; // l-1 is before s
405 // r == l, so: l is behind s, l-1 is before
406 // SO: if there is any, l is the one with the right prefix
407 // and l+1 may be one too
408 if(l == MapInfo_count)
409 return string_null; // no match, behind last item
410 match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l));
411 if(!startsWith(match, s))
412 return string_null; // wrong prefix
413 if(l == MapInfo_count - 1)
414 return match; // last one, nothing can follow => unique
415 if(startsWith(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l + 1)), s))
416 return string_null; // ambigous match
420 float MapInfo_CurrentFeatures()
424 if(!(cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena")))
425 req |= MAPINFO_FEATURE_WEAPONS;
429 float MapInfo_CurrentGametype()
431 if(cvar("g_domination"))
432 return MAPINFO_TYPE_DOMINATION;
433 else if(cvar("g_ctf"))
434 return MAPINFO_TYPE_CTF;
435 else if(cvar("g_runematch"))
436 return MAPINFO_TYPE_RUNEMATCH;
437 else if(cvar("g_tdm"))
438 return MAPINFO_TYPE_TEAM_DEATHMATCH;
439 else if(cvar("g_assault"))
440 return MAPINFO_TYPE_ASSAULT;
441 else if(cvar("g_lms"))
442 return MAPINFO_TYPE_LMS;
443 else if(cvar("g_arena"))
444 return MAPINFO_TYPE_ARENA;
445 else if(cvar("g_keyhunt"))
446 return MAPINFO_TYPE_KEYHUNT;
447 else if(cvar("g_onslaught"))
448 return MAPINFO_TYPE_ONSLAUGHT;
450 return MAPINFO_TYPE_DEATHMATCH;
453 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
455 if(!MapInfo_Get_ByName(s, 1, 0))
457 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
459 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
464 void MapInfo_LoadMap(string s)
466 if(!MapInfo_CheckMap(s))
468 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to deathmatch.\n");
469 cvar_set("g_domination", "0");
470 cvar_set("g_ctf", "0");
471 cvar_set("g_runematch", "0");
472 cvar_set("g_tdm", "0");
473 cvar_set("g_assault", "0");
474 cvar_set("g_lms", "0");
475 cvar_set("g_arena", "0");
476 cvar_set("g_keyhunt", "0");
477 cvar_set("g_onslaught", "0");
479 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
480 localcmd(strcat("\nchangelevel ", s, "\n"));
483 string MapInfo_ListAllowedMaps()
488 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures());
490 for(i = 0; i < MapInfo_count; ++i)
491 out = strcat(out, " ", _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i)));
492 return substring(out, 1, strlen(out) - 1);