2 void cvar_settemp(string pKey, string pValue)
4 //localcmd(strcat("\nsettemp ", t, " \"", s, "\"\n"));
6 // duplicate what this alias does:
7 // alias settemp "settemp_list \"1 $1 $settemp_var $settemp_list\"; set $settemp_var \"${$1}\"; settemp_var ${settemp_var}x; $1 \"$2\""
9 cvar_set("settemp_list", strcat("1 ", pKey, " ", cvar_string("settemp_var"), " ", cvar_string("settemp_list")));
11 registercvar(cvar_string("settemp_var"), "", 0);
13 registercvar(cvar_string("settemp_var"), "");
15 cvar_set(cvar_string("settemp_var"), cvar_string(pKey));
16 cvar_set("settemp_var", strcat(cvar_string("settemp_var"), "x"));
17 cvar_set(pKey, pValue);
20 void cvar_settemp_restore()
22 // undo what cvar_settemp did
24 n = tokenize(cvar_string("settemp_list"));
25 for(i = 0; i < n - 3; i += 3)
26 cvar_set(argv(i + 1), cvar_string(argv(i + 2)));
27 cvar_set("settemp_list", "0");
30 // HUGE SET - stored in a string
31 string HugeSetOfIntegers_empty()
35 float HugeSetOfIntegers_get(string pArr, float i)
37 return stof(substring(pArr, i * 4, 4));
39 float HugeSetOfIntegers_length(string pArr)
41 return strlen(pArr) / 4;
43 string HugeSetOfIntegers_concat(string a1, string a2)
45 return strcat(a1, a2);
47 string HugeSetOfIntegers_insert(string a1, float n, string a2)
48 // special concat function to build up large lists in less time by binary concatenation
51 s = strcat(" ", ftos(n));
52 return strcat(a1, substring(s, strlen(s) - 4, 4), a2);
55 // generic string stuff
56 float startsWith(string haystack, string needle)
58 return substring(haystack, 0, strlen(needle)) == needle;
60 float startsWithNocase(string haystack, string needle)
62 return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
64 string extractRestOfLine(string haystack, string needle)
66 if(startsWith(haystack, needle))
67 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
73 o = strstrofs(s, " ", 0);
76 return substring(s, 0, o);
81 o = strstrofs(s, " ", 0);
84 return substring(s, o + 1, strlen(s) - (o + 1));
87 // GLOB HANDLING (for all BSP files)
88 float _MapInfo_globopen;
89 float _MapInfo_globcount;
90 float _MapInfo_globhandle;
91 string _MapInfo_GlobItem(float i)
94 s = search_getfilename(_MapInfo_globhandle, i);
95 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
98 void MapInfo_Enumerate()
100 if(_MapInfo_globopen)
101 search_end(_MapInfo_globhandle);
102 _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
103 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
104 _MapInfo_globopen = 1;
107 // filter the info by game type mask (updates MapInfo_count)
108 string _MapInfo_filtered;
109 string MapInfo_FilterGametype_Recursive(float pGametype, float pFeatures, float pBegin, float pEnd, float pAbortOnGenerate)
115 return HugeSetOfIntegers_empty();
117 m = floor((pBegin + pEnd) / 2);
119 l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m, pAbortOnGenerate);
121 return string_null; // BAIL OUT
122 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.
124 return string_null; // BAIL OUT
125 valid = (((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures));
126 r = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, m + 1, pEnd, pAbortOnGenerate);
128 return string_null; // BAIL OUT
131 return HugeSetOfIntegers_insert(l, m, r);
133 return HugeSetOfIntegers_concat(l, r);
135 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pAbortOnGenerate)
137 if(_MapInfo_filtered)
138 strunzone(_MapInfo_filtered);
139 _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, 0, _MapInfo_globcount, pAbortOnGenerate);
140 if not(_MapInfo_filtered)
142 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
145 _MapInfo_filtered = strzone(_MapInfo_filtered);
146 MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
147 //print("Filter ", ftos(pGametype), "/", ftos(pFeatures), " has ", ftos(MapInfo_count), "\n");
152 // load info about the i-th map into the MapInfo_Map_* globals
153 string MapInfo_BSPName_ByID(float i)
155 return _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i));
158 string unquote(string s)
163 for(i = 0; i < l; ++i)
166 ch = substring(s, i, 1);
167 if(ch != " ") if(ch != "\"")
169 for(j = strlen(s) - i - 1; j > 0; --j)
171 ch = substring(s, i+j, 1);
172 if(ch != " ") if(ch != "\"")
173 return substring(s, i, j+1);
175 return substring(s, i, 1);
181 float MapInfo_Get_ByID(float i)
184 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
186 // TODO save in cache
192 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
203 vector mapMins, mapMaxs;
206 fn = strcat("maps/", pFilename, ".ent");
207 fh = fopen(fn, FILE_READ);
211 fn = strcat("maps/", pFilename, ".bsp");
212 fh = fopen(fn, FILE_READ);
216 print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
219 MapInfo_Map_supportedGametypes = 0;
223 if not((s = fgets(fh)))
225 if(inWorldspawn == 1)
226 if(startsWith(s, "}"))
232 if(k == "classname" && v == "worldspawn")
234 else if(k == "author")
235 MapInfo_Map_author = v;
236 else if(k == "message")
238 i = strstrofs(v, " by ", 0);
239 if(MapInfo_Map_author == "He-Who-Must-Not-Be-Named" && i >= 0)
241 MapInfo_Map_title = substring(v, 0, i);
242 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
245 MapInfo_Map_title = v;
252 o = stov(strcat("'", v, "'"));
253 mapMins_x = min(mapMins_x, o_x);
254 mapMins_y = min(mapMins_y, o_y);
255 mapMins_z = min(mapMins_z, o_z);
256 mapMaxs_x = max(mapMaxs_x, o_x);
257 mapMaxs_y = max(mapMaxs_y, o_y);
258 mapMaxs_z = max(mapMaxs_z, o_z);
260 else if(k == "classname")
262 if(v == "dom_controlpoint")
263 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
264 else if(v == "item_flag_team2")
265 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
266 else if(v == "team_CTF_blueflag")
267 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
268 else if(v == "runematch_spawn_point")
269 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
270 else if(v == "target_assault_roundend")
271 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
272 else if(v == "onslaught_generator")
273 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
274 else if(v == "info_player_team1")
275 ++MapInfo_Map_spawnpoints;
276 else if(v == "info_player_team2")
277 ++MapInfo_Map_spawnpoints;
278 else if(v == "info_player_start")
279 ++MapInfo_Map_spawnpoints;
280 else if(v == "info_player_deathmatch")
281 ++MapInfo_Map_spawnpoints;
282 else if(v == "weapon_nex")
284 else if(v == "weapon_railgun")
286 else if(startsWith(v, "weapon_"))
287 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
293 print(fn, " ended still in worldspawn, BUG\n");
296 MapInfo_Map_diameter = vlen(mapMaxs - mapMins);
298 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT);
299 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
301 // we have a CTF-only or Assault-only map. Don't add other modes then,
302 // as the map is too symmetric for them.
306 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
307 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
309 if(MapInfo_Map_spawnpoints >= 8 && MapInfo_Map_diameter > 4096)
310 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
311 if( MapInfo_Map_diameter < 4096)
312 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
313 if(MapInfo_Map_spawnpoints >= 12 && MapInfo_Map_diameter > 5120)
314 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
322 void _MapInfo_Map_Reset()
324 MapInfo_Map_title = "Untitled1";
325 MapInfo_Map_description = "Bleh.";
326 MapInfo_Map_author = "He-Who-Must-Not-Be-Named";
327 MapInfo_Map_supportedGametypes = 0;
328 MapInfo_Map_supportedFeatures = 0;
329 MapInfo_Map_diameter = 0;
330 MapInfo_Map_spawnpoints = 0;
333 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
335 MapInfo_Map_supportedGametypes |= pThisType;
336 if(!(pThisType & pWantedType))
339 cvar_set("fraglimit", car(s));
342 cvar_set("timelimit", car(s));
345 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
347 cvar_set("g_tdm_teams", car(s));
351 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
353 cvar_set("g_keyhunt_teams", car(s));
358 float MapInfo_Type_FromString(string t)
360 if (t == "dm") return MAPINFO_TYPE_DEATHMATCH;
361 else if(t == "tdm") return MAPINFO_TYPE_TEAM_DEATHMATCH;
362 else if(t == "dom") return MAPINFO_TYPE_DOMINATION;
363 else if(t == "ctf") return MAPINFO_TYPE_CTF;
364 else if(t == "rune") return MAPINFO_TYPE_RUNEMATCH;
365 else if(t == "lms") return MAPINFO_TYPE_LMS;
366 else if(t == "arena") return MAPINFO_TYPE_ARENA;
367 else if(t == "kh") return MAPINFO_TYPE_KEYHUNT;
368 else if(t == "as") return MAPINFO_TYPE_ASSAULT;
369 else if(t == "ons") return MAPINFO_TYPE_ONSLAUGHT;
370 else if(t == "all") return MAPINFO_TYPE_ALL;
374 // load info about a map by name into the MapInfo_Map_* globals
375 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
384 MapInfo_Map_bspname = pFilename;
386 // default all generic fields so they have "good" values in case something fails
387 fn = strcat("maps/", pFilename, ".mapinfo");
388 fh = fopen(fn, FILE_READ);
393 _MapInfo_Map_Reset();
394 r = _MapInfo_Generate(pFilename);
397 fh = fopen(fn, FILE_WRITE);
398 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
399 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
400 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
401 fputs(fh, strcat("_diameter ", ftos(MapInfo_Map_diameter), "\n"));
402 fputs(fh, strcat("_spawnpoints ", ftos(MapInfo_Map_spawnpoints), "\n"));
403 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS) fputs(fh, "has weapons\n");
404 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH) fputs(fh, "type dm 30 20\n");
405 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
406 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION) fputs(fh, "type dom 200 20\n");
407 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF) fputs(fh, "type ctf 300 20\n");
408 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH) fputs(fh, "type rune 200 20\n");
409 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS) fputs(fh, "type lms 9 20\n");
410 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA) fputs(fh, "type arena 10 20\n");
411 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT) fputs(fh, "type kh 1000 20 3\n");
412 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT) fputs(fh, "type as 20\n");
413 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT) fputs(fh, "type ons 20\n");
415 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
419 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
425 fh = fopen(fn, FILE_READ);
427 error("... but I just wrote it!");
430 _MapInfo_Map_Reset();
433 if not((s = fgets(fh)))
435 t = car(s); s = cdr(s);
437 MapInfo_Map_title = s;
438 else if(t == "description")
439 MapInfo_Map_description = s;
440 else if(t == "author")
441 MapInfo_Map_author = s;
442 else if(t == "_diameter")
443 MapInfo_Map_diameter = stof(s);
444 else if(t == "_spawnpoints")
445 MapInfo_Map_spawnpoints = stof(s);
448 t = car(s); s = cdr(s);
449 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
451 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
455 t = car(s); s = cdr(s);
456 f = MapInfo_Type_FromString(t);
458 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
460 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
462 else if(t == "settemp_for_type")
464 t = car(s); s = cdr(s);
465 if((f = MapInfo_Type_FromString(t)))
467 if(f & pGametypeToSet)
469 t = car(s); s = cdr(s);
470 if(strstrofs(t, "\"", 0) >= 0)
471 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
472 else if(strstrofs(t, "\\", 0) >= 0)
473 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
474 else if(strstrofs(t, ";", 0) >= 0)
475 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
476 else if(strstrofs(s, "\"", 0) >= 0)
477 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
478 else if(strstrofs(s, "\\", 0) >= 0)
479 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
480 else if(strstrofs(s, ";", 0) >= 0)
481 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
484 dprint("Applying temporary setting ", t, " := ", s, "\n");
491 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
495 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
499 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
500 error("Can't select the requested game type. Bailing out.");
501 if(MapInfo_Map_supportedGametypes != 0)
503 dprint("Map ", pFilename, " supports no game types, ignored\n");
507 float MapInfo_FindName(string s)
509 // if there is exactly one map of prefix s, return it
510 // if not, return the null string
511 // note that DP sorts glob results... so I can use a binary search
515 // invariants: r is behind s, l-1 is equal or before
518 m = floor((l + r) / 2);
519 MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m));
520 cmp = strcasecmp(MapInfo_FindName_match, s);
522 return m; // found and good
524 l = m + 1; // l-1 is before s
528 MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l));
529 MapInfo_FindName_firstResult = l;
530 // r == l, so: l is behind s, l-1 is before
531 // SO: if there is any, l is the one with the right prefix
532 // and l+1 may be one too
533 if(l == MapInfo_count)
535 MapInfo_FindName_match = string_null;
536 MapInfo_FindName_firstResult = -1;
537 return -1; // no MapInfo_FindName_match, behind last item
539 if(!startsWithNocase(MapInfo_FindName_match, s))
541 MapInfo_FindName_match = string_null;
542 MapInfo_FindName_firstResult = -1;
543 return -1; // wrong prefix
545 if(l == MapInfo_count - 1)
546 return l; // last one, nothing can follow => unique
547 if(startsWithNocase(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l + 1)), s))
549 MapInfo_FindName_match = string_null;
550 return -1; // ambigous MapInfo_FindName_match
555 string MapInfo_FixName(string s)
558 return MapInfo_FindName_match;
561 float MapInfo_CurrentFeatures()
565 if(!(cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena")))
566 req |= MAPINFO_FEATURE_WEAPONS;
570 float MapInfo_CurrentGametype()
572 if(cvar("g_domination"))
573 return MAPINFO_TYPE_DOMINATION;
574 else if(cvar("g_ctf"))
575 return MAPINFO_TYPE_CTF;
576 else if(cvar("g_runematch"))
577 return MAPINFO_TYPE_RUNEMATCH;
578 else if(cvar("g_tdm"))
579 return MAPINFO_TYPE_TEAM_DEATHMATCH;
580 else if(cvar("g_assault"))
581 return MAPINFO_TYPE_ASSAULT;
582 else if(cvar("g_lms"))
583 return MAPINFO_TYPE_LMS;
584 else if(cvar("g_arena"))
585 return MAPINFO_TYPE_ARENA;
586 else if(cvar("g_keyhunt"))
587 return MAPINFO_TYPE_KEYHUNT;
588 else if(cvar("g_onslaught"))
589 return MAPINFO_TYPE_ONSLAUGHT;
591 return MAPINFO_TYPE_DEATHMATCH;
594 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
596 if(!MapInfo_Get_ByName(s, 1, 0))
598 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
600 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
605 void MapInfo_SwitchGameType(float t)
607 cvar_set("gamecfg", "0");
608 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "1" : "0");
609 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
610 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "1" : "0");
611 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "1" : "0");
612 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "1" : "0");
613 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "1" : "0");
614 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "1" : "0");
615 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "1" : "0");
616 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "1" : "0");
617 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "1" : "0");
620 void MapInfo_LoadMap(string s)
622 MapInfo_Map_supportedGametypes = 0;
623 if(!MapInfo_CheckMap(s))
625 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
626 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
628 localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
631 string MapInfo_ListAllowedMaps()
636 // to make absolutely sure:
638 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0);
641 for(i = 0; i < MapInfo_count; ++i)
642 out = strcat(out, " ", _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i)));
643 return substring(out, 1, strlen(out) - 1);
646 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
649 if(!MapInfo_CheckMap(s))
651 if(MapInfo_Map_supportedGametypes <= 0)
652 error("Mapinfo system is not functional at all. BAILED OUT.\n");
655 while(!(MapInfo_Map_supportedGametypes & 1))
658 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
660 // t is now a supported mode!
661 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
662 MapInfo_SwitchGameType(t);
664 cvar_settemp_restore();
665 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());