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 string extractRestOfLine(string haystack, string needle)
62 if(startsWith(haystack, needle))
63 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
69 o = strstrofs(s, " ", 0);
72 return substring(s, 0, o);
77 o = strstrofs(s, " ", 0);
80 return substring(s, o + 1, strlen(s) - (o + 1));
83 // GLOB HANDLING (for all BSP files)
84 float _MapInfo_globopen;
85 float _MapInfo_globcount;
86 float _MapInfo_globhandle;
87 string _MapInfo_GlobItem(float i)
90 s = search_getfilename(_MapInfo_globhandle, i);
91 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
94 void MapInfo_Enumerate()
97 search_end(_MapInfo_globhandle);
98 _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
99 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
100 _MapInfo_globopen = 1;
103 // filter the info by game type mask (updates MapInfo_count)
104 string _MapInfo_filtered;
105 string MapInfo_FilterGametype_Recursive(float pGametype, float pFeatures, float pBegin, float pEnd, float pAbortOnGenerate)
111 return HugeSetOfIntegers_empty();
113 m = floor((pBegin + pEnd) / 2);
115 l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m, pAbortOnGenerate);
117 return string_null; // BAIL OUT
118 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.
120 return string_null; // BAIL OUT
121 valid = (((MapInfo_Map_supportedGametypes & pGametype) != 0) && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures));
122 r = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, m + 1, pEnd, pAbortOnGenerate);
124 return string_null; // BAIL OUT
127 return HugeSetOfIntegers_insert(l, m, r);
129 return HugeSetOfIntegers_concat(l, r);
131 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pAbortOnGenerate)
133 if(_MapInfo_filtered)
134 strunzone(_MapInfo_filtered);
135 _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, 0, _MapInfo_globcount, pAbortOnGenerate);
136 if not(_MapInfo_filtered)
138 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
141 _MapInfo_filtered = strzone(_MapInfo_filtered);
142 MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
143 //print("Filter ", ftos(pGametype), "/", ftos(pFeatures), " has ", ftos(MapInfo_count), "\n");
148 // load info about the i-th map into the MapInfo_Map_* globals
149 string MapInfo_BSPName_ByID(float i)
151 return _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i));
154 string unquote(string s)
159 for(i = 0; i < l; ++i)
162 ch = substring(s, i, 1);
163 if(ch != " ") if(ch != "\"")
165 for(j = strlen(s) - i - 1; j > 0; --j)
167 ch = substring(s, i+j, 1);
168 if(ch != " ") if(ch != "\"")
169 return substring(s, i, j+1);
171 return substring(s, i, 1);
177 float MapInfo_Get_ByID(float i)
180 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
182 // TODO save in cache
188 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
199 vector mapMins, mapMaxs;
202 fn = strcat("maps/", pFilename, ".ent");
203 fh = fopen(fn, FILE_READ);
207 fn = strcat("maps/", pFilename, ".bsp");
208 fh = fopen(fn, FILE_READ);
212 print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
215 MapInfo_Map_supportedGametypes = 0;
219 if not((s = fgets(fh)))
221 if(inWorldspawn == 1)
222 if(startsWith(s, "}"))
228 if(k == "classname" && v == "worldspawn")
230 else if(k == "author")
231 MapInfo_Map_author = v;
232 else if(k == "message")
234 i = strstrofs(v, " by ", 0);
235 if(MapInfo_Map_author == "He-Who-Must-Not-Be-Named" && i >= 0)
237 MapInfo_Map_title = substring(v, 0, i);
238 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
241 MapInfo_Map_title = v;
248 o = stov(strcat("'", v, "'"));
249 mapMins_x = min(mapMins_x, o_x);
250 mapMins_y = min(mapMins_y, o_y);
251 mapMins_z = min(mapMins_z, o_z);
252 mapMaxs_x = max(mapMaxs_x, o_x);
253 mapMaxs_y = max(mapMaxs_y, o_y);
254 mapMaxs_z = max(mapMaxs_z, o_z);
256 else if(k == "classname")
258 if(v == "dom_controlpoint")
259 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
260 else if(v == "item_flag_team2")
261 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
262 else if(v == "team_CTF_blueflag")
263 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
264 else if(v == "runematch_spawn_point")
265 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
266 else if(v == "target_assault_roundend")
267 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
268 else if(v == "onslaught_generator")
269 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
270 else if(v == "info_player_team1")
271 ++MapInfo_Map_spawnpoints;
272 else if(v == "info_player_team2")
273 ++MapInfo_Map_spawnpoints;
274 else if(v == "info_player_start")
275 ++MapInfo_Map_spawnpoints;
276 else if(v == "info_player_deathmatch")
277 ++MapInfo_Map_spawnpoints;
278 else if(v == "weapon_nex")
280 else if(v == "weapon_railgun")
282 else if(startsWith(v, "weapon_"))
283 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
289 print(fn, " ended still in worldspawn, BUG\n");
292 MapInfo_Map_diameter = vlen(mapMaxs - mapMins);
294 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT);
295 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
297 // we have a CTF-only or Assault-only map. Don't add other modes then,
298 // as the map is too symmetric for them.
302 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
303 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
305 if(MapInfo_Map_spawnpoints >= 8 && MapInfo_Map_diameter > 4096)
306 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
307 if( MapInfo_Map_diameter < 4096)
308 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
309 if(MapInfo_Map_spawnpoints >= 12 && MapInfo_Map_diameter > 5120)
310 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
318 void _MapInfo_Map_Reset()
320 MapInfo_Map_title = "Untitled1";
321 MapInfo_Map_description = "Bleh.";
322 MapInfo_Map_author = "He-Who-Must-Not-Be-Named";
323 MapInfo_Map_supportedGametypes = 0;
324 MapInfo_Map_supportedFeatures = 0;
325 MapInfo_Map_diameter = 0;
326 MapInfo_Map_spawnpoints = 0;
329 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
331 MapInfo_Map_supportedGametypes |= pThisType;
332 if(!(pThisType & pWantedType))
335 cvar_set("fraglimit", car(s));
338 cvar_set("timelimit", car(s));
341 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
343 cvar_set("g_tdm_teams", car(s));
347 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
349 cvar_set("g_keyhunt_teams", car(s));
354 float MapInfo_Type_FromString(string t)
356 if (t == "dm") return MAPINFO_TYPE_DEATHMATCH;
357 else if(t == "tdm") return MAPINFO_TYPE_TEAM_DEATHMATCH;
358 else if(t == "dom") return MAPINFO_TYPE_DOMINATION;
359 else if(t == "ctf") return MAPINFO_TYPE_CTF;
360 else if(t == "rune") return MAPINFO_TYPE_RUNEMATCH;
361 else if(t == "lms") return MAPINFO_TYPE_LMS;
362 else if(t == "arena") return MAPINFO_TYPE_ARENA;
363 else if(t == "kh") return MAPINFO_TYPE_KEYHUNT;
364 else if(t == "as") return MAPINFO_TYPE_ASSAULT;
365 else if(t == "ons") return MAPINFO_TYPE_ONSLAUGHT;
366 else if(t == "all") return MAPINFO_TYPE_ALL;
370 // load info about a map by name into the MapInfo_Map_* globals
371 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
380 MapInfo_Map_bspname = pFilename;
382 // default all generic fields so they have "good" values in case something fails
383 fn = strcat("maps/", pFilename, ".mapinfo");
384 fh = fopen(fn, FILE_READ);
389 _MapInfo_Map_Reset();
390 r = _MapInfo_Generate(pFilename);
393 fh = fopen(fn, FILE_WRITE);
394 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
395 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
396 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
397 fputs(fh, strcat("_diameter ", ftos(MapInfo_Map_diameter), "\n"));
398 fputs(fh, strcat("_spawnpoints ", ftos(MapInfo_Map_spawnpoints), "\n"));
399 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS) fputs(fh, "has weapons\n");
400 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH) fputs(fh, "type dm 30 20\n");
401 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
402 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION) fputs(fh, "type dom 200 20\n");
403 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF) fputs(fh, "type ctf 300 20\n");
404 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH) fputs(fh, "type rune 200 20\n");
405 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS) fputs(fh, "type lms 9 20\n");
406 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA) fputs(fh, "type arena 10 20\n");
407 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT) fputs(fh, "type kh 1000 20 3\n");
408 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT) fputs(fh, "type as 20\n");
409 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT) fputs(fh, "type ons 20\n");
411 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
415 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
421 fh = fopen(fn, FILE_READ);
423 error("... but I just wrote it!");
426 _MapInfo_Map_Reset();
429 if not((s = fgets(fh)))
431 t = car(s); s = cdr(s);
433 MapInfo_Map_title = s;
434 else if(t == "description")
435 MapInfo_Map_description = s;
436 else if(t == "author")
437 MapInfo_Map_author = s;
438 else if(t == "_diameter")
439 MapInfo_Map_diameter = stof(s);
440 else if(t == "_spawnpoints")
441 MapInfo_Map_spawnpoints = stof(s);
444 t = car(s); s = cdr(s);
445 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
447 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
451 t = car(s); s = cdr(s);
452 f = MapInfo_Type_FromString(t);
454 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
456 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
458 else if(t == "settemp_for_type")
460 t = car(s); s = cdr(s);
461 if((f = MapInfo_Type_FromString(t)))
463 if(f & pGametypeToSet)
465 t = car(s); s = cdr(s);
466 if(strstrofs(t, "\"", 0) >= 0)
467 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
468 else if(strstrofs(t, "\\", 0) >= 0)
469 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
470 else if(strstrofs(t, ";", 0) >= 0)
471 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
472 else if(strstrofs(s, "\"", 0) >= 0)
473 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
474 else if(strstrofs(s, "\\", 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");
480 dprint("Applying temporary setting ", t, " := ", s, "\n");
487 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
491 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
495 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
496 error("Can't select the requested game type. Bailing out.");
497 if(MapInfo_Map_supportedGametypes != 0)
499 dprint("Map ", pFilename, " supports no game types, ignored\n");
503 float MapInfo_FindName(string s)
505 // if there is exactly one map of prefix s, return it
506 // if not, return the null string
507 // note that DP sorts glob results... so I can use a binary search
511 // invariants: r is behind s, l-1 is equal or before
514 m = floor((l + r) / 2);
515 MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, m));
516 cmp = strcasecmp(MapInfo_FindName_match, s);
518 return m; // found and good
520 l = m + 1; // l-1 is before s
524 MapInfo_FindName_match = _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l));
525 MapInfo_FindName_firstResult = l;
526 // r == l, so: l is behind s, l-1 is before
527 // SO: if there is any, l is the one with the right prefix
528 // and l+1 may be one too
529 if(l == MapInfo_count)
531 MapInfo_FindName_match = string_null;
532 MapInfo_FindName_firstResult = -1;
533 return -1; // no MapInfo_FindName_match, behind last item
535 if(!startsWith(MapInfo_FindName_match, s))
537 MapInfo_FindName_match = string_null;
538 MapInfo_FindName_firstResult = -1;
539 return -1; // wrong prefix
541 if(l == MapInfo_count - 1)
542 return l; // last one, nothing can follow => unique
543 if(startsWith(_MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, l + 1)), s))
545 MapInfo_FindName_match = string_null;
546 return -1; // ambigous MapInfo_FindName_match
551 string MapInfo_FixName(string s)
554 return MapInfo_FindName_match;
557 float MapInfo_CurrentFeatures()
561 if(!(cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena")))
562 req |= MAPINFO_FEATURE_WEAPONS;
566 float MapInfo_CurrentGametype()
568 if(cvar("g_domination"))
569 return MAPINFO_TYPE_DOMINATION;
570 else if(cvar("g_ctf"))
571 return MAPINFO_TYPE_CTF;
572 else if(cvar("g_runematch"))
573 return MAPINFO_TYPE_RUNEMATCH;
574 else if(cvar("g_tdm"))
575 return MAPINFO_TYPE_TEAM_DEATHMATCH;
576 else if(cvar("g_assault"))
577 return MAPINFO_TYPE_ASSAULT;
578 else if(cvar("g_lms"))
579 return MAPINFO_TYPE_LMS;
580 else if(cvar("g_arena"))
581 return MAPINFO_TYPE_ARENA;
582 else if(cvar("g_keyhunt"))
583 return MAPINFO_TYPE_KEYHUNT;
584 else if(cvar("g_onslaught"))
585 return MAPINFO_TYPE_ONSLAUGHT;
587 return MAPINFO_TYPE_DEATHMATCH;
590 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
592 if(!MapInfo_Get_ByName(s, 1, 0))
594 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
596 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
601 void MapInfo_SwitchGameType(float t)
603 cvar_set("gamecfg", "0");
604 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "1" : "0");
605 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
606 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "1" : "0");
607 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "1" : "0");
608 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "1" : "0");
609 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "1" : "0");
610 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "1" : "0");
611 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "1" : "0");
612 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "1" : "0");
613 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "1" : "0");
616 void MapInfo_LoadMap(string s)
618 MapInfo_Map_supportedGametypes = 0;
619 if(!MapInfo_CheckMap(s))
621 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
622 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
624 localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
627 string MapInfo_ListAllowedMaps()
632 // to make absolutely sure:
634 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0);
637 for(i = 0; i < MapInfo_count; ++i)
638 out = strcat(out, " ", _MapInfo_GlobItem(HugeSetOfIntegers_get(_MapInfo_filtered, i)));
639 return substring(out, 1, strlen(out) - 1);
642 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
645 if(!MapInfo_CheckMap(s))
647 if(MapInfo_Map_supportedGametypes <= 0)
648 error("Mapinfo system is not functional at all. BAILED OUT.\n");
651 while(!(MapInfo_Map_supportedGametypes & 1))
654 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
656 // t is now a supported mode!
657 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
658 MapInfo_SwitchGameType(t);
660 cvar_settemp_restore();
661 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());