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, float pAbortOnGenerate)
82 return HugeSetOfIntegers_empty();
84 m = floor((pBegin + pEnd) / 2);
86 l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m, pAbortOnGenerate);
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.
91 return string_null; // BAIL OUT
92 valid = (((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures));
93 r = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, m + 1, pEnd, pAbortOnGenerate);
95 return string_null; // BAIL OUT
98 return HugeSetOfIntegers_insert(l, m, r);
100 return HugeSetOfIntegers_concat(l, r);
102 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pAbortOnGenerate)
104 if(_MapInfo_filtered)
105 strunzone(_MapInfo_filtered);
106 _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, 0, _MapInfo_globcount, pAbortOnGenerate);
107 if not(_MapInfo_filtered)
109 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
112 _MapInfo_filtered = strzone(_MapInfo_filtered);
113 MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
114 //print("Filter ", ftos(pGametype), "/", ftos(pFeatures), " has ", ftos(MapInfo_count), "\n");
119 // load info about the i-th map into the MapInfo_Map_* globals
120 string MapInfo_BSPName_ByID(float i)
122 return _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i));
125 string unquote(string s)
130 for(i = 0; i < l; ++i)
133 ch = substring(s, i, 1);
134 if(ch != " ") if(ch != "\"")
136 for(j = strlen(s) - i - 1; j > 0; --j)
138 ch = substring(s, i+j, 1);
139 if(ch != " ") if(ch != "\"")
140 return substring(s, i, j+1);
142 return substring(s, i, 1);
148 float MapInfo_Get_ByID(float i)
151 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
153 // TODO save in cache
159 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
170 vector mapMins, mapMaxs;
173 fn = strcat("maps/", pFilename, ".ent");
174 fh = fopen(fn, FILE_READ);
178 fn = strcat("maps/", pFilename, ".bsp");
179 fh = fopen(fn, FILE_READ);
183 print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
186 MapInfo_Map_supportedGametypes = 0;
190 if not((s = fgets(fh)))
192 if(inWorldspawn == 1)
193 if(startsWith(s, "}"))
199 if(k == "classname" && v == "worldspawn")
201 else if(k == "author")
202 MapInfo_Map_author = v;
203 else if(k == "message")
205 i = strstrofs(v, " by ", 0);
206 if(MapInfo_Map_author == "He-Who-Must-Not-Be-Named" && i >= 0)
208 MapInfo_Map_title = substring(v, 0, i);
209 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
212 MapInfo_Map_title = v;
219 o = stov(strcat("'", v, "'"));
220 mapMins_x = min(mapMins_x, o_x);
221 mapMins_y = min(mapMins_y, o_y);
222 mapMins_z = min(mapMins_z, o_z);
223 mapMaxs_x = max(mapMaxs_x, o_x);
224 mapMaxs_y = max(mapMaxs_y, o_y);
225 mapMaxs_z = max(mapMaxs_z, o_z);
227 else if(k == "classname")
229 if(v == "dom_controlpoint")
230 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
231 else if(v == "item_flag_team2")
232 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
233 else if(v == "team_CTF_blueflag")
234 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
235 else if(v == "runematch_spawn_point")
236 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
237 else if(v == "target_assault_roundend")
238 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
239 else if(v == "onslaught_generator")
240 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
241 else if(v == "info_player_team1")
242 ++MapInfo_Map_spawnpoints;
243 else if(v == "info_player_team2")
244 ++MapInfo_Map_spawnpoints;
245 else if(v == "info_player_start")
246 ++MapInfo_Map_spawnpoints;
247 else if(v == "info_player_deathmatch")
248 ++MapInfo_Map_spawnpoints;
249 else if(v == "weapon_nex")
251 else if(v == "weapon_railgun")
253 else if(startsWith(v, "weapon_"))
254 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
260 print(fn, " ended still in worldspawn, BUG\n");
263 MapInfo_Map_diameter = vlen(mapMaxs - mapMins);
265 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT);
266 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
268 // we have a CTF-only or Assault-only map. Don't add other modes then,
269 // as the map is too symmetric for them.
273 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
274 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
276 if(MapInfo_Map_spawnpoints >= 8 && MapInfo_Map_diameter > 4096)
277 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
278 if( MapInfo_Map_diameter < 4096)
279 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
280 if(MapInfo_Map_spawnpoints >= 12 && MapInfo_Map_diameter > 5120)
281 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
288 void _MapInfo_Map_Reset()
290 MapInfo_Map_title = "Untitled1";
291 MapInfo_Map_description = "Bleh.";
292 MapInfo_Map_author = "He-Who-Must-Not-Be-Named";
293 MapInfo_Map_supportedGametypes = 0;
294 MapInfo_Map_supportedFeatures = 0;
295 MapInfo_Map_diameter = 0;
296 MapInfo_Map_spawnpoints = 0;
299 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
301 MapInfo_Map_supportedGametypes |= pThisType;
302 if(!(pThisType & pWantedType))
305 cvar_set("fraglimit", car(s));
308 cvar_set("timelimit", car(s));
311 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
313 cvar_set("g_tdm_teams", car(s));
317 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
319 cvar_set("g_keyhunt_teams", car(s));
324 float MapInfo_Type_FromString(string t)
326 if (t == "dm") return MAPINFO_TYPE_DEATHMATCH;
327 else if(t == "tdm") return MAPINFO_TYPE_TEAM_DEATHMATCH;
328 else if(t == "dom") return MAPINFO_TYPE_DOMINATION;
329 else if(t == "ctf") return MAPINFO_TYPE_CTF;
330 else if(t == "rune") return MAPINFO_TYPE_RUNEMATCH;
331 else if(t == "lms") return MAPINFO_TYPE_LMS;
332 else if(t == "arena") return MAPINFO_TYPE_ARENA;
333 else if(t == "kh") return MAPINFO_TYPE_KEYHUNT;
334 else if(t == "as") return MAPINFO_TYPE_ASSAULT;
335 else if(t == "ons") return MAPINFO_TYPE_ONSLAUGHT;
339 // load info about a map by name into the MapInfo_Map_* globals
340 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
349 MapInfo_Map_bspname = pFilename;
351 // default all generic fields so they have "good" values in case something fails
352 fn = strcat("maps/", pFilename, ".mapinfo");
353 fh = fopen(fn, FILE_READ);
358 _MapInfo_Map_Reset();
359 r = _MapInfo_Generate(pFilename);
362 fh = fopen(fn, FILE_WRITE);
363 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
364 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
365 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
366 fputs(fh, strcat("_diameter ", ftos(MapInfo_Map_diameter), "\n"));
367 fputs(fh, strcat("_spawnpoints ", ftos(MapInfo_Map_spawnpoints), "\n"));
368 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS) fputs(fh, "has weapons\n");
369 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH) fputs(fh, "type dm 30 20\n");
370 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
371 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION) fputs(fh, "type dom 200 20\n");
372 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF) fputs(fh, "type ctf 300 20\n");
373 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH) fputs(fh, "type rune 200 20\n");
374 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS) fputs(fh, "type lms 9 20\n");
375 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA) fputs(fh, "type arena 10 20\n");
376 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT) fputs(fh, "type kh 1000 20 3\n");
377 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT) fputs(fh, "type as 20\n");
378 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT) fputs(fh, "type ons 20\n");
382 fh = fopen(fn, FILE_READ);
384 error("... but I just wrote it!");
387 _MapInfo_Map_Reset();
390 if not((s = fgets(fh)))
392 t = car(s); s = cdr(s);
394 MapInfo_Map_title = s;
395 else if(t == "description")
396 MapInfo_Map_description = s;
397 else if(t == "author")
398 MapInfo_Map_author = s;
399 else if(t == "_diameter")
400 MapInfo_Map_diameter = stof(s);
401 else if(t == "_spawnpoints")
402 MapInfo_Map_spawnpoints = stof(s);
405 t = car(s); s = cdr(s);
406 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
408 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
412 t = car(s); s = cdr(s);
413 f = MapInfo_Type_FromString(t);
415 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
417 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
420 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
424 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
425 error("Can't select the requested game type. Bailing out.");
426 if(MapInfo_Map_supportedGametypes != 0)
428 dprint("Map ", pFilename, " supports no game types, ignored\n");
432 string _MapInfo_FindName_match;
433 float MapInfo_FindName(string s)
435 // if there is exactly one map of prefix s, return it
436 // if not, return the null string
437 // note that DP sorts glob results... so I can use a binary search
441 _MapInfo_FindName_match = string_null;
442 // invariants: r is behind s, l-1 is equal or before
445 m = floor((l + r) / 2);
446 cmp = strcasecmp(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m)), s);
448 return m; // found and good
450 l = m + 1; // l-1 is before s
454 // r == l, so: l is behind s, l-1 is before
455 // SO: if there is any, l is the one with the right prefix
456 // and l+1 may be one too
457 if(l == MapInfo_count)
458 return -1; // no _MapInfo_FindName_match, behind last item
459 _MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l));
460 if(!startsWith(_MapInfo_FindName_match, s))
461 return -1; // wrong prefix
462 if(l == MapInfo_count - 1)
463 return l; // last one, nothing can follow => unique
464 if(startsWith(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l + 1)), s))
465 return -1; // ambigous _MapInfo_FindName_match
469 string MapInfo_FixName(string s)
472 return _MapInfo_FindName_match;
475 float MapInfo_CurrentFeatures()
479 if(!(cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena")))
480 req |= MAPINFO_FEATURE_WEAPONS;
484 float MapInfo_CurrentGametype()
486 if(cvar("g_domination"))
487 return MAPINFO_TYPE_DOMINATION;
488 else if(cvar("g_ctf"))
489 return MAPINFO_TYPE_CTF;
490 else if(cvar("g_runematch"))
491 return MAPINFO_TYPE_RUNEMATCH;
492 else if(cvar("g_tdm"))
493 return MAPINFO_TYPE_TEAM_DEATHMATCH;
494 else if(cvar("g_assault"))
495 return MAPINFO_TYPE_ASSAULT;
496 else if(cvar("g_lms"))
497 return MAPINFO_TYPE_LMS;
498 else if(cvar("g_arena"))
499 return MAPINFO_TYPE_ARENA;
500 else if(cvar("g_keyhunt"))
501 return MAPINFO_TYPE_KEYHUNT;
502 else if(cvar("g_onslaught"))
503 return MAPINFO_TYPE_ONSLAUGHT;
505 return MAPINFO_TYPE_DEATHMATCH;
508 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
510 if(!MapInfo_Get_ByName(s, 1, 0))
512 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
514 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
519 void MapInfo_SwitchGameType(float t)
521 cvar_set("gamecfg", "0");
522 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "0" : "1");
523 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "0" : "1");
524 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "0" : "1");
525 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "0" : "1");
526 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "0" : "1");
527 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "0" : "1");
528 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "0" : "1");
529 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "0" : "1");
530 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "0" : "1");
531 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "0" : "1");
534 void MapInfo_LoadMap(string s)
536 if(!MapInfo_CheckMap(s))
538 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to deathmatch.\n");
539 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
541 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
542 localcmd(strcat("\nchangelevel ", s, "\n"));
545 string MapInfo_ListAllowedMaps()
550 // to make absolutely sure:
552 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0);
555 for(i = 0; i < MapInfo_count; ++i)
556 out = strcat(out, " ", _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i)));
557 return substring(out, 1, strlen(out) - 1);