3 #include "../client/defs.qh"
5 #include <common/weapons/_all.qh>
9 #include <common/monsters/_mod.qh>
12 bool autocvar_g_mapinfo_arena_compat = true;
13 bool autocvar_g_mapinfo_arena_generate = false;
16 #define WARN_COND false
18 bool autocvar_g_mapinfo_ignore_warnings;
19 #define WARN_COND (!autocvar_g_mapinfo_ignore_warnings && MapInfo_Map_bspname == mi_shortname)
22 // generic string stuff
24 int _MapInfo_Cache_Active;
25 int _MapInfo_Cache_DB_NameToIndex;
26 int _MapInfo_Cache_Buf_IndexToMapData;
28 void MapInfo_Cache_Destroy()
30 if(!_MapInfo_Cache_Active)
33 db_close(_MapInfo_Cache_DB_NameToIndex);
34 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
35 _MapInfo_Cache_Active = 0;
38 void MapInfo_Cache_Create()
40 MapInfo_Cache_Destroy();
41 _MapInfo_Cache_DB_NameToIndex = db_create();
42 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
43 _MapInfo_Cache_Active = 1;
46 void MapInfo_Cache_Invalidate()
48 if(!_MapInfo_Cache_Active)
51 MapInfo_Cache_Create();
54 void MapInfo_Cache_Store()
58 if(!_MapInfo_Cache_Active)
61 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
64 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
65 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
70 // now store all the stuff
71 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
72 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
73 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
74 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
75 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
76 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
77 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
78 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
81 float MapInfo_Cache_Retrieve(string map)
85 if(!_MapInfo_Cache_Active)
88 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
93 // now retrieve all the stuff
94 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
95 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
96 MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
97 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
98 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
99 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
100 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
101 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
106 // GLOB HANDLING (for all BSP files)
107 float _MapInfo_globopen;
108 float _MapInfo_globcount;
109 float _MapInfo_globhandle;
110 string _MapInfo_GlobItem(float i)
113 if(!_MapInfo_globopen)
115 s = search_getfilename(_MapInfo_globhandle, i);
116 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
119 void MapInfo_Enumerate()
121 if(_MapInfo_globopen)
123 search_end(_MapInfo_globhandle);
124 _MapInfo_globopen = 0;
126 MapInfo_Cache_Invalidate();
127 _MapInfo_globhandle = search_begin("maps/*.bsp", true, true);
128 if(_MapInfo_globhandle >= 0)
130 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
131 _MapInfo_globopen = 1;
134 _MapInfo_globcount = 0;
137 // filter the info by game type mask (updates MapInfo_count)
139 float _MapInfo_filtered;
140 float _MapInfo_filtered_allocated;
141 float MapInfo_FilterList_Lookup(float i)
143 return stof(bufstr_get(_MapInfo_filtered, i));
146 void _MapInfo_FilterList_swap(float i, float j, entity pass)
149 h = bufstr_get(_MapInfo_filtered, i);
150 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
151 bufstr_set(_MapInfo_filtered, j, h);
154 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
157 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
158 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
159 return strcasecmp(a, b);
162 float MapInfo_FilterGametype(Gametype pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
164 return _MapInfo_FilterGametype(pGametype.m_flags, pFeatures, pFlagsRequired, pFlagsForbidden, pAbortOnGenerate);
166 float _MapInfo_FilterGametype(int pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
169 if (!_MapInfo_filtered_allocated)
171 _MapInfo_filtered_allocated = 1;
172 _MapInfo_filtered = buf_create();
175 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
177 if(MapInfo_Get_ByName(_MapInfo_GlobItem(i), 1, NULL) == 2) // if we generated one... BAIL OUT and let the caller continue in the next frame.
180 LOG_TRACE("Autogenerated a .mapinfo, doing the rest later.");
181 MapInfo_progress = i / _MapInfo_globcount;
184 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
185 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
186 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
187 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
188 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
190 MapInfo_count = j + 1;
191 MapInfo_ClearTemps();
193 // sometimes the glob isn't sorted nicely, so fix it here...
194 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
198 void MapInfo_FilterString(string sf)
200 // this function further filters _MapInfo_filtered, which is prepared by MapInfo_FilterGametype by string
204 for(i = 0, j = -1; i < MapInfo_count; ++i)
206 if (MapInfo_Get_ByID(i))
208 // prepare for keyword filter
209 if (MapInfo_Map_title && strstrofs(MapInfo_Map_title, "<TITLE>", 0) == -1)
210 title = MapInfo_Map_title;
212 title = MapInfo_Map_bspname;
214 if((strstrofs(strtolower(title), strtolower(sf), 0)) >= 0)
215 bufstr_set(_MapInfo_filtered, ++j, bufstr_get(_MapInfo_filtered, i));
218 MapInfo_count = j + 1;
219 MapInfo_ClearTemps();
221 // sometimes the glob isn't sorted nicely, so fix it here...
222 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
225 void MapInfo_Filter_Free()
227 if(_MapInfo_filtered_allocated)
229 buf_del(_MapInfo_filtered);
230 _MapInfo_filtered_allocated = 0;
234 // load info about the i-th map into the MapInfo_Map_* globals
235 string MapInfo_BSPName_ByID(float i)
237 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
240 string unquote(string s)
243 for(float i = 0; i < l; ++i)
245 string ch = substring(s, i, 1);
246 if((ch != " ") && (ch != "\""))
248 for(float j = l - i - 1; j > 0; --j)
250 ch = substring(s, i+j, 1);
251 if(ch != " ") if(ch != "\"")
252 return substring(s, i, j+1);
254 return substring(s, i, 1);
260 float MapInfo_Get_ByID(float i)
262 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, NULL))
267 string _MapInfo_Map_worldspawn_music;
269 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
278 float diameter, spawnpoints;
281 vector mapMins, mapMaxs;
284 fn = strcat("maps/", pFilename, ".ent");
285 fh = fopen(fn, FILE_READ);
289 fn = strcat("maps/", pFilename, ".bsp");
290 fh = fopen(fn, FILE_READ);
294 LOG_INFO("Analyzing ", fn, " to generate initial mapinfo");
297 MapInfo_Map_flags = 0;
298 MapInfo_Map_supportedGametypes = 0;
301 _MapInfo_Map_worldspawn_music = "";
305 if(autocvar_g_mapinfo_arena_generate)
307 // try for .arena or .defi files, as they may have more accurate information
309 string arena_fn = _MapInfo_FindArenaFile(pFilename, ".arena");
310 int arena_fh = fopen(arena_fn, FILE_READ);
314 arena_fn = _MapInfo_FindArenaFile(pFilename, ".defi");
315 arena_fh = fopen(arena_fn, FILE_READ);
319 _MapInfo_ParseArena(arena_fn, arena_fh, pFilename, NULL, isdefi, true);
326 if (!((s = fgets(fh))))
328 if(inWorldspawn == 1)
329 if(startsWith(s, "}"))
335 if(k == "classname" && v == "worldspawn")
337 else if(k == "author")
338 MapInfo_Map_author = v;
339 else if(k == "_description")
340 MapInfo_Map_description = v;
341 else if(k == "music")
342 _MapInfo_Map_worldspawn_music = v;
343 else if(k == "noise")
344 _MapInfo_Map_worldspawn_music = v;
345 else if(k == "message")
347 i = strstrofs(v, " by ", 0);
348 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
350 MapInfo_Map_title = substring(v, 0, i);
351 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
354 MapInfo_Map_title = v;
361 o = stov(strcat("'", v, "'"));
362 mapMins.x = min(mapMins.x, o.x);
363 mapMins.y = min(mapMins.y, o.y);
364 mapMins.z = min(mapMins.z, o.z);
365 mapMaxs.x = max(mapMaxs.x, o.x);
366 mapMaxs.y = max(mapMaxs.y, o.y);
367 mapMaxs.z = max(mapMaxs.z, o.z);
369 else if(k == "race_place")
374 else if(k == "classname")
376 if(v == "info_player_team1")
378 else if(v == "info_player_team2")
380 else if(v == "info_player_start")
382 else if(v == "info_player_deathmatch")
384 else if(v == "weapon_nex")
386 else if(v == "weapon_railgun")
388 else if(startsWith(v, "weapon_"))
389 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
390 else if(startsWith(v, "turret_"))
391 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
392 else if(startsWith(v, "vehicle_"))
393 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
394 else if(startsWith(v, "monster_"))
395 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
396 else if(v == "target_music" || v == "trigger_music")
397 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
399 FOREACH(Gametypes, true, it.m_generate_mapinfo(it, v));
405 LOG_WARN(fn, " ended still in worldspawn, BUG");
408 diameter = vlen(mapMaxs - mapMins);
410 int twoBaseModes = 0;
411 FOREACH(Gametypes, it.m_isTwoBaseMode(), twoBaseModes |= it.m_flags);
412 if(twoBaseModes && (twoBaseModes &= MapInfo_Map_supportedGametypes))
414 // we have a symmetrical map, don't add the modes without bases
418 FOREACH(Gametypes, it.m_isAlwaysSupported(it, spawnpoints, diameter), MapInfo_Map_supportedGametypes |= it.m_flags);
421 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE.m_flags)
424 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE.m_flags;
425 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS.m_flags;
428 LOG_TRACE("-> diameter ", ftos(diameter));
429 LOG_TRACE("; spawnpoints ", ftos(spawnpoints));
430 LOG_TRACE("; modes ", ftos(MapInfo_Map_supportedGametypes));
437 void _MapInfo_Map_Reset()
439 MapInfo_Map_title = "<TITLE>";
440 MapInfo_Map_titlestring = "<TITLE>";
441 MapInfo_Map_description = "<DESCRIPTION>";
442 MapInfo_Map_author = "<AUTHOR>";
443 MapInfo_Map_supportedGametypes = 0;
444 MapInfo_Map_supportedFeatures = 0;
445 MapInfo_Map_flags = 0;
446 MapInfo_Map_clientstuff = "";
447 MapInfo_Map_fog = "";
448 MapInfo_Map_mins = '0 0 0';
449 MapInfo_Map_maxs = '0 0 0';
452 string _MapInfo_GetDefault(Gametype t)
454 return t.m_legacydefaults;
457 void _MapInfo_Map_ApplyGametype(string s, Gametype pWantedType, Gametype pThisType, int load_default)
460 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
461 if(!(pThisType.m_flags & pWantedType.m_flags))
465 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, false);
467 if(!pWantedType.frags) // these modes don't use fraglimit
469 cvar_set("fraglimit", "0");
475 cvar_set("fraglimit", sa);
481 cvar_set("timelimit", sa);
484 if(pWantedType.m_setTeams)
488 pWantedType.m_setTeams(sa);
492 // rc = timelimit timelimit_qualification laps laps_teamplay
493 if(pWantedType == MAPINFO_TYPE_RACE)
495 cvar_set("fraglimit", "0"); // special case!
497 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
498 cvar_set("g_race_qualifying_timelimit", sa);
503 if(cvar("g_race_teams") < 2)
504 cvar_set("fraglimit", sa);
509 if(cvar("g_race_teams") >= 2)
510 cvar_set("fraglimit", sa);
514 if(!pWantedType.frags) // these modes don't use fraglimit
516 cvar_set("leadlimit", "0");
522 cvar_set("leadlimit", sa);
527 string _MapInfo_GetDefaultEx(Gametype t)
529 return t ? t.model2 : "";
532 float _MapInfo_GetTeamPlayBool(Gametype t)
534 return t ? t.team : false;
537 void _MapInfo_Map_ApplyGametypeEx(string s, Gametype pWantedType, Gametype pThisType)
539 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
540 if (!(pThisType.m_flags & pWantedType.m_flags))
543 // reset all the cvars to their defaults
545 cvar_set("timelimit", cvar_defstring("timelimit"));
546 cvar_set("leadlimit", cvar_defstring("leadlimit"));
547 cvar_set("fraglimit", cvar_defstring("fraglimit"));
548 FOREACH(Gametypes, true, it.m_parse_mapinfo(string_null, string_null));
550 string fraglimit_normal = string_null;
551 string fraglimit_teams = string_null;
553 for (s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s); s != ""; s = cdr(s)) {
555 if (sa == "") continue;
556 int p = strstrofs(sa, "=", 0);
559 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
562 string k = substring(sa, 0, p);
563 string v = substring(sa, p + 1, -1);
568 cvar_set("timelimit", v);
573 cvar_set("leadlimit", v);
582 fraglimit_normal = v;
585 case "teampointlimit":
597 FOREACH(Gametypes, true, handled |= it.m_parse_mapinfo(k, v));
598 if (!handled && WARN_COND)
599 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
602 if (pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
605 cvar_set("fraglimit", fraglimit_teams);
610 cvar_set("fraglimit", fraglimit_normal);
614 Gametype MapInfo_Type_FromString(string gtype, bool dowarn)
616 string replacement = "";
620 case "nexball": replacement = "nb"; break;
621 case "freezetag": replacement = "ft"; break;
622 case "keepaway": replacement = "ka"; break;
623 case "invasion": replacement = "inv"; break;
624 case "assault": replacement = "as"; break;
625 case "race": replacement = "rc"; break;
627 case "ffa": replacement = "dm"; do_warn = false; break;
629 case "oneflag": replacement = "ctf"; do_warn = false; break;
630 case "team": replacement = "tdm"; do_warn = false; break;
631 case "tourney": replacement = "duel"; do_warn = false; break;
633 if (replacement != "")
635 if (dowarn && WARN_COND)
636 LOG_WARNF("MapInfo_Type_FromString (probably %s): using deprecated name '%s'. Should use '%s'.", MapInfo_Map_bspname, gtype, replacement);
639 FOREACH(Gametypes, it.mdl == gtype, return it);
643 string MapInfo_Type_Description(Gametype t)
645 return t ? t.gametype_description : "";
648 string MapInfo_Type_ToString(Gametype t)
650 return t ? t.mdl : "";
653 string MapInfo_Type_ToText(Gametype t)
655 /* xgettext:no-c-format */
656 return t ? t.message : _("@!#%'n Tuba Throwing");
659 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
663 t = car(s); s = cdr(s);
665 // limited support of "" and comments
666 // remove trailing and leading " of t
667 if(substring(t, 0, 1) == "\"")
669 if(substring(t, -1, 1) == "\"")
670 t = substring(t, 1, -2);
673 // remove leading " of s
674 if(substring(s, 0, 1) == "\"")
676 s = substring(s, 1, -1);
678 // remove trailing " of s, and all that follows (cvar description)
679 o = strstrofs(s, "\"", 0);
681 s = substring(s, 0, o);
683 // remove // comments
684 o = strstrofs(s, "//", 0);
686 s = substring(s, 0, o);
688 // remove trailing spaces
689 while(substring(s, -1, 1) == " ")
690 s = substring(s, 0, -2);
696 fh = fopen(s, FILE_READ);
700 LOG_WARN("Map ", pFilename, " references not existing config file ", s);
704 while((s = fgets(fh)))
706 // catch different sorts of comments
707 if(s == "") // empty lines
709 if(substring(s, 0, 1) == "#") // UNIX style
711 if(substring(s, 0, 2) == "//") // C++ style
713 if(substring(s, 0, 1) == "_") // q3map style
716 if(substring(s, 0, 4) == "set ")
717 s = substring(s, 4, -1);
718 if(substring(s, 0, 5) == "seta ")
719 s = substring(s, 5, -1);
721 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
727 LOG_WARN("Map ", pFilename, " uses too many levels of inclusion");
730 || !cvar_value_issafe(t)
731 || !cvar_value_issafe(s)
732 || matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
735 LOG_WARN("Map ", pFilename, " contains a potentially harmful setting, ignored");
737 else if(matchacl(acl, t) <= 0)
740 LOG_WARN("Map ", pFilename, " contains a denied setting, ignored");
744 if(type == 0) // server set
746 LOG_TRACE("Applying temporary setting ", t, " := ", s);
748 if(cvar("g_campaign"))
749 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
756 LOG_TRACE("Applying temporary client setting ", t, " := ", s);
757 MapInfo_Map_clientstuff = strcat(
758 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
764 float MapInfo_isRedundant(string fn, string t)
766 // normalize file name
767 fn = strreplace("_", "", fn);
768 fn = strreplace("-", "", fn);
770 // normalize visible title
771 t = strreplace(":", "", t);
772 t = strreplace(" ", "", t);
773 t = strreplace("_", "", t);
774 t = strreplace("-", "", t);
775 t = strreplace("'", "", t);
776 t = strdecolorize(t);
778 // we allow the visible title to have punctuation the file name does
779 // not, but not vice versa
780 if(!strcasecmp(fn, t))
786 bool _MapInfo_ParseArena(string arena_filename, int fh, string pFilename, Gametype pGametypeToSet, bool isdefi, bool isgenerator)
788 // NOTE: .arena files can hold more than 1 map's information!
789 // to handle this, we're going to store gathered information in local variables and save it if we encounter the correct map name
790 bool in_brackets = false; // testing a potential mapinfo section (within brackets)
791 bool dosave = (arena_filename == strcat("scripts/", pFilename, ((isdefi) ? ".defi" : ".arena"))); // if the map is using the fallback, just accept the first found mapinfo (it's probably correct!)
792 string stored_Map_description = "";
793 string stored_Map_title = "";
794 string stored_Map_author = "";
795 int stored_supportedGametypes = 0;
799 if (!((s = fgets(fh))))
802 // catch different sorts of comments
803 if(s == "") // empty lines
805 if(substring(s, 0, 1) == "#") // UNIX style
807 if(substring(s, 0, 2) == "//") // C++ style
809 if(substring(s, 0, 1) == "_") // q3map style
811 if(strstrofs(s, "{", 0) >= 0)
814 return false; // edge case? already in a bracketed section!
818 else if(!in_brackets)
820 // if we're not inside a bracket, don't process map info
823 if(strstrofs(s, "}", 0) >= 0)
826 return false; // no starting bracket! let the mapinfo generation system handle it
830 MapInfo_Map_description = stored_Map_description;
831 if(stored_Map_title != "")
832 MapInfo_Map_title = stored_Map_title;
833 MapInfo_Map_author = stored_Map_author;
835 MapInfo_Map_supportedGametypes = stored_supportedGametypes;
838 FOREACH(Gametypes, it.m_flags & stored_supportedGametypes,
840 _MapInfo_Map_ApplyGametype ("", pGametypeToSet, it, true);
843 return true; // no need to continue through the file, we have our map!
847 // discard any gathered locals, we're not using the correct map!
848 stored_Map_description = "";
849 stored_Map_title = "";
850 stored_Map_author = "";
851 stored_supportedGametypes = 0;
856 s = strreplace("\t", " ", s);
858 float p = strstrofs(s, "//", 0);
860 s = substring(s, 0, p);
862 // perform an initial trim to ensure the first argument is properly obtained
863 // remove leading spaces
864 while(substring(s, 0, 1) == " ")
865 s = substring(s, 1, -1);
867 t = car(s); s = cdr(s);
868 t = strtolower(t); // apparently some q3 maps use capitalized parameters
870 // remove trailing spaces
871 while(substring(t, -1, 1) == " ")
872 t = substring(t, 0, -2);
874 // remove trailing spaces
875 while(substring(s, -1, 1) == " ")
876 s = substring(s, 0, -2);
877 // remove leading spaces
878 while(substring(s, 0, 1) == " ")
879 s = substring(s, 1, -1);
880 // limited support of ""
881 // remove trailing and leading " of s
882 if(substring(s, 0, 1) == "\"")
884 if(substring(s, -1, 1) == "\"")
885 s = substring(s, 1, -2);
888 stored_Map_title = s;
889 else if(t == "author")
890 stored_Map_author = s;
893 // if there is a valid gametype in this .arena file, include it in the menu
894 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
895 // type in quake 3 holds all the supported gametypes, so we must loop through all of them
896 FOREACH_WORD(s, true,
898 Gametype f = MapInfo_Type_FromString(it);
900 stored_supportedGametypes |= f.m_flags;
903 else if(t == "style" && isdefi)
905 // we have a defrag map on our hands, add CTS!
907 stored_supportedGametypes |= MAPINFO_TYPE_CTS.m_flags;
911 if(strtolower(s) == strtolower(pFilename))
912 dosave = true; // yay, found our map!
914 else if(t == "quote")
915 stored_Map_description = s;
919 // if the map wasn't found in the .arena, fall back to generated .mapinfo
923 #if defined(CSQC) || defined(MENUQC)
924 string(string filename) whichpack = #503;
926 string _MapInfo_FindArenaFile(string pFilename, string extension)
928 string fallback = strcat("scripts/", pFilename, extension);
929 if(!checkextension("DP_QC_FS_SEARCH_PACKFILE"))
931 string base_pack = whichpack(strcat("maps/", pFilename, ".bsp"));
932 if(base_pack == "") // this map isn't packaged!
935 int glob = search_packfile_begin(strcat("scripts/*", extension), true, true, base_pack);
938 int n = search_getsize(glob);
939 for(int j = 0; j < n; ++j)
941 string file = search_getfilename(glob, j);
943 int fh = fopen(file, FILE_READ);
946 for(string s; (s = fgets(fh)); )
948 int offset = strstrofs(s, "map", 0);
951 if(strstrofs(strtolower(s), strcat("\"", strtolower(pFilename), "\""), offset) >= 0) // quake 3 is case insensitive
955 return file; // FOUND IT!
963 return fallback; // if we get here, a valid .arena file could not be found
966 // load info about a map by name into the MapInfo_Map_* globals
967 float MapInfo_Get_ByName_NoFallbacks(string pFilename, int pAllowGenerate, Gametype pGametypeToSet)
976 acl = MAPINFO_SETTEMP_ACL_USER;
978 if(strstrofs(pFilename, "/", 0) >= 0)
980 LOG_WARN("Invalid character in map name, ignored");
984 if(pGametypeToSet == NULL)
985 if(MapInfo_Cache_Retrieve(pFilename))
990 MapInfo_Map_bspname = pFilename;
992 // default all generic fields so they have "good" values in case something fails
993 fn = strcat("maps/", pFilename, ".mapinfo");
994 fh = fopen(fn, FILE_READ);
997 if(autocvar_g_mapinfo_arena_compat)
999 // try for .arena or .defi files if no .mapinfo exists
1000 bool isdefi = false;
1001 fn = _MapInfo_FindArenaFile(pFilename, ".arena");
1002 fh = fopen(fn, FILE_READ);
1006 fn = _MapInfo_FindArenaFile(pFilename, ".defi");
1007 fh = fopen(fn, FILE_READ);
1011 _MapInfo_Map_Reset();
1012 if(_MapInfo_ParseArena(fn, fh, pFilename, pGametypeToSet, isdefi, false))
1013 goto mapinfo_handled; // skip generation
1017 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
1018 fh = fopen(fn, FILE_READ);
1023 _MapInfo_Map_Reset();
1024 r = _MapInfo_Generate(pFilename);
1027 fh = fopen(fn, FILE_WRITE);
1028 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
1029 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
1030 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
1031 if(_MapInfo_Map_worldspawn_music != "")
1034 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
1036 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
1038 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
1040 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
1044 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
1045 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
1048 i = floor(random() * n);
1049 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
1052 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
1054 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
1055 fputs(fh, "has weapons\n");
1057 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
1058 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
1059 fputs(fh, "has turrets\n");
1061 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
1062 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
1063 fputs(fh, "has vehicles\n");
1065 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
1066 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
1067 fputs(fh, "frustrating\n");
1069 FOREACH(Gametypes, MapInfo_Map_supportedGametypes & it.m_flags, {
1070 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(it), _MapInfo_GetDefaultEx(it)));
1073 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
1074 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
1075 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
1076 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
1077 fputs(fh, "// optional: hidden\n");
1082 fh = fopen(fn, FILE_READ);
1084 error("... but I just wrote it!");
1088 LOG_WARN("autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo");
1091 _MapInfo_Map_Reset();
1094 if (!((s = fgets(fh))))
1097 // catch different sorts of comments
1098 if(s == "") // empty lines
1100 if(substring(s, 0, 1) == "#") // UNIX style
1102 if(substring(s, 0, 2) == "//") // C++ style
1104 if(substring(s, 0, 1) == "_") // q3map style
1107 float p = strstrofs(s, "//", 0);
1109 s = substring(s, 0, p);
1111 t = car(s); s = cdr(s);
1113 MapInfo_Map_title = s;
1114 else if(t == "description")
1115 MapInfo_Map_description = s;
1116 else if(t == "author")
1117 MapInfo_Map_author = s;
1120 t = car(s); // s = cdr(s);
1121 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1122 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
1123 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
1124 else if(t == "monsters") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
1125 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1127 LOG_WARN("Map ", pFilename, " supports unknown feature ", t, ", ignored");
1129 else if(t == "hidden")
1131 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
1133 else if(t == "forbidden")
1135 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
1137 else if(t == "frustrating")
1139 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
1141 else if(t == "noautomaplist")
1143 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1145 else if(t == "gameversion_min")
1147 if (cvar("gameversion") < stof(s))
1148 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1150 else if(t == "type")
1152 t = car(s); s = cdr(s);
1153 Gametype f = MapInfo_Type_FromString(t, true);
1155 //LOG_WARN("Map ", pFilename, " contains the legacy 'type' keyword which is deprecated and will be removed in the future. Please migrate the mapinfo file to 'gametype'.");
1157 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, true);
1159 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
1161 else if(t == "gametype")
1163 t = car(s); s = cdr(s);
1164 Gametype f = MapInfo_Type_FromString(t, true);
1166 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
1168 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
1170 else if(t == "size")
1172 float a, b, c, d, e;
1173 t = car(s); s = cdr(s); a = stof(t);
1174 t = car(s); s = cdr(s); b = stof(t);
1175 t = car(s); s = cdr(s); c = stof(t);
1176 t = car(s); s = cdr(s); d = stof(t);
1177 t = car(s); s = cdr(s); e = stof(t);
1181 LOG_WARN("Map ", pFilename, " contains an incorrect size line (not enough params), syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z");
1185 t = car(s); s = cdr(s); f = stof(t);
1189 LOG_WARN("Map ", pFilename, " contains an incorrect size line (too many params), syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z");
1193 if(a >= d || b >= e || c >= f)
1196 LOG_WARN("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs");
1200 MapInfo_Map_mins.x = a;
1201 MapInfo_Map_mins.y = b;
1202 MapInfo_Map_mins.z = c;
1203 MapInfo_Map_maxs.x = d;
1204 MapInfo_Map_maxs.y = e;
1205 MapInfo_Map_maxs.z = f;
1210 else if(t == "settemp_for_type")
1212 t = car(s); s = cdr(s);
1213 bool all = t == "all";
1215 if(all || (f = MapInfo_Type_FromString(t, true)))
1217 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
1219 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1224 LOG_DEBUG("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored");
1227 else if(t == "clientsettemp_for_type")
1229 t = car(s); s = cdr(s);
1230 bool all = t == "all";
1232 if(all || (f = MapInfo_Type_FromString(t, true)))
1234 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
1236 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1241 LOG_DEBUG("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored");
1246 if (!cvar_value_issafe(s))
1249 LOG_WARN("Map ", pFilename, " contains a potentially harmful fog setting, ignored");
1252 MapInfo_Map_fog = s;
1254 else if(t == "cdtrack")
1256 t = car(s); s = cdr(s);
1257 // We do this only if pGametypeToSet even though this
1258 // content is theoretically game type independent,
1259 // because MapInfo_Map_clientstuff contains otherwise
1260 // game type dependent stuff. That way this value stays
1261 // empty when not setting a game type to not set any
1262 // false expectations.
1265 if (!cvar_value_issafe(t))
1268 LOG_WARN("Map ", pFilename, " contains a potentially harmful cdtrack, ignored");
1271 MapInfo_Map_clientstuff = strcat(
1272 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1277 LOG_WARN("Map ", pFilename, " provides unknown info item ", t, ", ignored");
1279 LABEL(mapinfo_handled)
1282 if(MapInfo_Map_title == "<TITLE>")
1283 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1284 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1285 MapInfo_Map_titlestring = MapInfo_Map_title;
1287 MapInfo_Map_titlestring = sprintf("%s: %s", MapInfo_Map_bspname, MapInfo_Map_title);
1289 MapInfo_Cache_Store();
1290 if(MapInfo_Map_supportedGametypes != 0)
1293 LOG_WARN("Map ", pFilename, " supports no game types, ignored");
1296 int MapInfo_Get_ByName(string pFilename, float pAllowGenerate, Gametype pGametypeToSet)
1298 int r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1300 FOREACH(Gametypes, it.m_isForcedSupported(it), _MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, it));
1304 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet.m_flags))
1306 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1307 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1315 float MapInfo_FindName(string s)
1317 // if there is exactly one map of prefix s, return it
1318 // if not, return the null string
1319 // note that DP sorts glob results... so I can use a binary search
1323 // invariants: r is behind s, l-1 is equal or before
1326 m = floor((l + r) / 2);
1327 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1328 cmp = strcasecmp(MapInfo_FindName_match, s);
1330 return m; // found and good
1332 l = m + 1; // l-1 is before s
1336 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1337 MapInfo_FindName_firstResult = l;
1338 // r == l, so: l is behind s, l-1 is before
1339 // SO: if there is any, l is the one with the right prefix
1340 // and l+1 may be one too
1341 if(l == MapInfo_count)
1343 MapInfo_FindName_match = string_null;
1344 MapInfo_FindName_firstResult = -1;
1345 return -1; // no MapInfo_FindName_match, behind last item
1347 if(!startsWithNocase(MapInfo_FindName_match, s))
1349 MapInfo_FindName_match = string_null;
1350 MapInfo_FindName_firstResult = -1;
1351 return -1; // wrong prefix
1353 if(l == MapInfo_count - 1)
1354 return l; // last one, nothing can follow => unique
1355 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1357 MapInfo_FindName_match = string_null;
1358 return -1; // ambigous MapInfo_FindName_match
1363 string MapInfo_FixName(string s)
1365 MapInfo_FindName(s);
1366 return MapInfo_FindName_match;
1369 int MapInfo_CurrentFeatures()
1372 // TODO: find a better way to check if weapons are required on the map
1373 if(!(cvar("g_instagib") || cvar("g_overkill") || cvar("g_nix") || cvar("g_weaponarena") || !cvar("g_pickup_items")
1374 || cvar("g_race") || cvar("g_cts") || cvar("g_nexball") || cvar("g_ca") || cvar("g_freezetag") || cvar("g_lms")))
1375 req |= MAPINFO_FEATURE_WEAPONS;
1379 Gametype MapInfo_CurrentGametype()
1381 Gametype prev = MapInfo_Type_FromString(cvar_string("gamecfg"), false);
1382 FOREACH(Gametypes, cvar(it.netname) && it != prev, return it);
1383 return prev ? prev : MAPINFO_TYPE_DEATHMATCH;
1386 float _MapInfo_CheckMap(string s, bool gametype_only) // returns 0 if the map can't be played with the current settings, 1 otherwise
1388 if(!MapInfo_Get_ByName(s, 1, NULL))
1390 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype().m_flags) == 0)
1394 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1399 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1402 r = _MapInfo_CheckMap(s, false);
1403 MapInfo_ClearTemps();
1407 void MapInfo_SwitchGameType(Gametype t)
1409 FOREACH(Gametypes, true, cvar_set(it.netname, (it == t) ? "1" : "0"));
1412 void MapInfo_LoadMap(string s, float reinit)
1414 MapInfo_Map_supportedGametypes = 0;
1415 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1416 //if(!MapInfo_CheckMap(s))
1418 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1419 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH.m_flags);
1422 LOG_INFO("Switching to map ", s);
1424 cvar_settemp_restore();
1426 localcmd(strcat("\nmap ", s, "\n"));
1428 localcmd(strcat("\nchangelevel ", s, "\n"));
1431 string MapInfo_ListAllowedMaps(Gametype type, float pRequiredFlags, float pForbiddenFlags)
1435 // to make absolutely sure:
1436 MapInfo_Enumerate();
1437 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1440 for(float i = 0; i < MapInfo_count; ++i)
1441 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1442 return substring(out, 1, strlen(out) - 1);
1445 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1449 // to make absolutely sure:
1450 MapInfo_Enumerate();
1451 _MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1454 for(float i = 0; i < MapInfo_count; ++i)
1455 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1457 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1459 return substring(out, 1, strlen(out) - 1);
1462 void MapInfo_LoadMapSettings_SaveGameType(Gametype t)
1464 MapInfo_SwitchGameType(t);
1465 cvar_set("gamecfg", t.mdl);
1466 MapInfo_LoadedGametype = t;
1469 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1471 Gametype t = MapInfo_CurrentGametype();
1472 MapInfo_LoadMapSettings_SaveGameType(t);
1474 if(!_MapInfo_CheckMap(s, true)) // with underscore, it keeps temps
1476 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1478 LOG_SEVERE("can't play the selected map in the given game mode. Working with only the override settings.");
1479 _MapInfo_Map_ApplyGametypeEx("", t, t);
1480 return; // do not call Get_ByName!
1483 if(MapInfo_Map_supportedGametypes == 0)
1485 RandomSelection_Init();
1486 FOREACH(Gametypes, it.m_priority == 2,
1488 MapInfo_Map_supportedGametypes |= it.m_flags;
1489 RandomSelection_AddEnt(it, 1, 1);
1491 if(RandomSelection_chosen_ent)
1492 t = RandomSelection_chosen_ent;
1493 LOG_SEVEREF("Mapinfo system is not functional at all. Falling back to a preferred mode (%s).", t.mdl);
1494 MapInfo_LoadMapSettings_SaveGameType(t);
1495 _MapInfo_Map_ApplyGametypeEx("", t, t);
1496 return; // do not call Get_ByName!
1500 // find the lowest bit in the supported gametypes
1501 // unnecessary now that we select one at random
1503 while(!(MapInfo_Map_supportedGametypes & 1))
1506 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes >> 1);
1509 RandomSelection_Init();
1510 Gametype t_prev = t;
1511 FOREACH(Gametypes, MapInfo_Map_supportedGametypes & it.m_flags,
1513 RandomSelection_AddEnt(it, 1, it.m_priority);
1515 if(RandomSelection_chosen_ent)
1516 t = RandomSelection_chosen_ent;
1518 // t is now a supported mode!
1519 LOG_WARNF("can't play the selected map in the given game mode (%s). Falling back to a supported mode (%s).", t_prev.mdl, t.mdl);
1520 MapInfo_LoadMapSettings_SaveGameType(t);
1522 if(!_MapInfo_CheckMap(s, false)) { // with underscore, it keeps temps
1523 LOG_WARNF("the selected map lacks features required by current settings; playing anyway.");
1525 MapInfo_Get_ByName(s, 1, t);
1528 void MapInfo_ClearTemps()
1530 MapInfo_Map_bspname = string_null;
1531 MapInfo_Map_title = string_null;
1532 MapInfo_Map_titlestring = string_null;
1533 MapInfo_Map_description = string_null;
1534 MapInfo_Map_author = string_null;
1535 MapInfo_Map_clientstuff = string_null;
1536 MapInfo_Map_supportedGametypes = 0;
1537 MapInfo_Map_supportedFeatures = 0;
1540 void MapInfo_Shutdown()
1542 MapInfo_ClearTemps();
1543 MapInfo_Filter_Free();
1544 MapInfo_Cache_Destroy();
1545 if(_MapInfo_globopen)
1547 search_end(_MapInfo_globhandle);
1548 _MapInfo_globhandle = -1;
1549 _MapInfo_globopen = false;
1553 int MapInfo_ForbiddenFlags()
1555 int f = MAPINFO_FLAG_FORBIDDEN;
1558 if (!cvar("g_maplist_allow_hidden"))
1560 f |= MAPINFO_FLAG_HIDDEN;
1562 if (!cvar("g_maplist_allow_frustrating"))
1563 f |= MAPINFO_FLAG_FRUSTRATING;
1568 int MapInfo_RequiredFlags()
1572 if(cvar("g_maplist_allow_frustrating") > 1)
1573 f |= MAPINFO_FLAG_FRUSTRATING;