3 #include <common/util.qh>
4 #include <common/weapons/_all.qh>
7 #include <common/util.qh>
8 #include <common/monsters/_mod.qh>
11 bool autocvar_g_mapinfo_arena_compat = true;
12 bool autocvar_g_mapinfo_arena_generate = false;
15 #define WARN_COND false
17 bool autocvar_g_mapinfo_ignore_warnings;
18 #define WARN_COND (!autocvar_g_mapinfo_ignore_warnings && MapInfo_Map_bspname == mi_shortname)
21 // generic string stuff
23 int _MapInfo_Cache_Active;
24 int _MapInfo_Cache_DB_NameToIndex;
25 int _MapInfo_Cache_Buf_IndexToMapData;
27 void MapInfo_Cache_Destroy()
29 if(!_MapInfo_Cache_Active)
32 db_close(_MapInfo_Cache_DB_NameToIndex);
33 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
34 _MapInfo_Cache_Active = 0;
37 void MapInfo_Cache_Create()
39 MapInfo_Cache_Destroy();
40 _MapInfo_Cache_DB_NameToIndex = db_create();
41 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
42 _MapInfo_Cache_Active = 1;
45 void MapInfo_Cache_Invalidate()
47 if(!_MapInfo_Cache_Active)
50 MapInfo_Cache_Create();
53 void MapInfo_Cache_Store()
57 if(!_MapInfo_Cache_Active)
60 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
63 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
64 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
69 // now store all the stuff
70 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
71 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
72 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
73 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
74 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
75 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
76 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
77 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
80 float MapInfo_Cache_Retrieve(string map)
84 if(!_MapInfo_Cache_Active)
87 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
92 // now retrieve all the stuff
93 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
94 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
95 MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
96 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
97 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
98 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
99 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
100 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
105 // GLOB HANDLING (for all BSP files)
106 float _MapInfo_globopen;
107 float _MapInfo_globcount;
108 float _MapInfo_globhandle;
109 string _MapInfo_GlobItem(float i)
112 if(!_MapInfo_globopen)
114 s = search_getfilename(_MapInfo_globhandle, i);
115 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
118 void MapInfo_Enumerate()
120 if(_MapInfo_globopen)
122 search_end(_MapInfo_globhandle);
123 _MapInfo_globopen = 0;
125 MapInfo_Cache_Invalidate();
126 _MapInfo_globhandle = search_begin("maps/*.bsp", true, true);
127 if(_MapInfo_globhandle >= 0)
129 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
130 _MapInfo_globopen = 1;
133 _MapInfo_globcount = 0;
136 // filter the info by game type mask (updates MapInfo_count)
138 float _MapInfo_filtered;
139 float _MapInfo_filtered_allocated;
140 float MapInfo_FilterList_Lookup(float i)
142 return stof(bufstr_get(_MapInfo_filtered, i));
145 void _MapInfo_FilterList_swap(float i, float j, entity pass)
148 h = bufstr_get(_MapInfo_filtered, i);
149 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
150 bufstr_set(_MapInfo_filtered, j, h);
153 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
156 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
157 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
158 return strcasecmp(a, b);
161 float MapInfo_FilterGametype(Gametype pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
163 return _MapInfo_FilterGametype(pGametype.m_flags, pFeatures, pFlagsRequired, pFlagsForbidden, pAbortOnGenerate);
165 float _MapInfo_FilterGametype(int pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
168 if (!_MapInfo_filtered_allocated)
170 _MapInfo_filtered_allocated = 1;
171 _MapInfo_filtered = buf_create();
174 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
176 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.
179 LOG_TRACE("Autogenerated a .mapinfo, doing the rest later.");
180 MapInfo_progress = i / _MapInfo_globcount;
183 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
184 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
185 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
186 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
187 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
189 MapInfo_count = j + 1;
190 MapInfo_ClearTemps();
192 // sometimes the glob isn't sorted nicely, so fix it here...
193 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
197 void MapInfo_FilterString(string sf)
199 // this function further filters _MapInfo_filtered, which is prepared by MapInfo_FilterGametype by string
203 for(i = 0, j = -1; i < MapInfo_count; ++i)
205 if (MapInfo_Get_ByID(i))
207 // prepare for keyword filter
208 if (MapInfo_Map_title && strstrofs(MapInfo_Map_title, "<TITLE>", 0) == -1)
209 title = MapInfo_Map_title;
211 title = MapInfo_Map_bspname;
213 if((strstrofs(strtolower(title), strtolower(sf), 0)) >= 0)
214 bufstr_set(_MapInfo_filtered, ++j, bufstr_get(_MapInfo_filtered, i));
217 MapInfo_count = j + 1;
218 MapInfo_ClearTemps();
220 // sometimes the glob isn't sorted nicely, so fix it here...
221 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
224 void MapInfo_Filter_Free()
226 if(_MapInfo_filtered_allocated)
228 buf_del(_MapInfo_filtered);
229 _MapInfo_filtered_allocated = 0;
233 // load info about the i-th map into the MapInfo_Map_* globals
234 string MapInfo_BSPName_ByID(float i)
236 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
239 string unquote(string s)
242 for(float i = 0; i < l; ++i)
244 string ch = substring(s, i, 1);
245 if((ch != " ") && (ch != "\""))
247 for(float j = l - i - 1; j > 0; --j)
249 ch = substring(s, i+j, 1);
250 if(ch != " ") if(ch != "\"")
251 return substring(s, i, j+1);
253 return substring(s, i, 1);
259 bool MapInfo_Get_ByID(int i)
261 return MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, NULL) ? true : false;
264 string _MapInfo_Map_worldspawn_music;
266 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
275 float diameter, spawnpoints;
278 vector mapMins, mapMaxs;
281 fn = strcat("maps/", pFilename, ".ent");
282 fh = fopen(fn, FILE_READ);
286 fn = strcat("maps/", pFilename, ".bsp");
287 fh = fopen(fn, FILE_READ);
291 LOG_INFO("Analyzing ", fn, " to generate initial mapinfo");
294 MapInfo_Map_flags = 0;
295 MapInfo_Map_supportedGametypes = 0;
298 _MapInfo_Map_worldspawn_music = "";
302 if(autocvar_g_mapinfo_arena_generate)
304 // try for .arena or .defi files, as they may have more accurate information
306 string arena_fn = _MapInfo_FindArenaFile(pFilename, ".arena");
307 int arena_fh = fopen(arena_fn, FILE_READ);
311 arena_fn = _MapInfo_FindArenaFile(pFilename, ".defi");
312 arena_fh = fopen(arena_fn, FILE_READ);
316 _MapInfo_ParseArena(arena_fn, arena_fh, pFilename, NULL, isdefi, true);
323 if (!((s = fgets(fh))))
325 if(inWorldspawn == 1)
326 if(startsWith(s, "}"))
332 if(k == "classname" && v == "worldspawn")
334 else if(k == "author")
335 MapInfo_Map_author = v;
336 else if(k == "_description")
337 MapInfo_Map_description = v;
338 else if(k == "music")
339 _MapInfo_Map_worldspawn_music = v;
340 else if(k == "noise")
341 _MapInfo_Map_worldspawn_music = v;
342 else if(k == "message")
344 i = strstrofs(v, " by ", 0);
345 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
347 MapInfo_Map_title = substring(v, 0, i);
348 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
351 MapInfo_Map_title = v;
358 o = stov(strcat("'", v, "'"));
359 mapMins.x = min(mapMins.x, o.x);
360 mapMins.y = min(mapMins.y, o.y);
361 mapMins.z = min(mapMins.z, o.z);
362 mapMaxs.x = max(mapMaxs.x, o.x);
363 mapMaxs.y = max(mapMaxs.y, o.y);
364 mapMaxs.z = max(mapMaxs.z, o.z);
366 else if(k == "race_place")
371 else if(k == "classname")
373 if(v == "info_player_team1")
375 else if(v == "info_player_team2")
377 else if(v == "info_player_start")
379 else if(v == "info_player_deathmatch")
381 else if(v == "weapon_nex")
383 else if(v == "weapon_railgun")
385 else if(startsWith(v, "weapon_"))
386 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
387 else if(startsWith(v, "turret_"))
388 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
389 else if(startsWith(v, "vehicle_"))
390 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
391 else if(startsWith(v, "monster_"))
392 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
393 else if(v == "target_music" || v == "trigger_music")
394 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
396 FOREACH(Gametypes, true, it.m_generate_mapinfo(it, v));
402 LOG_WARN(fn, " ended still in worldspawn, BUG");
405 diameter = vlen(mapMaxs - mapMins);
407 int twoBaseModes = 0;
408 FOREACH(Gametypes, it.m_isTwoBaseMode(), twoBaseModes |= it.m_flags);
409 if(twoBaseModes && (twoBaseModes &= MapInfo_Map_supportedGametypes))
411 // we have a symmetrical map, don't add the modes without bases
415 FOREACH(Gametypes, it.m_isAlwaysSupported(it, spawnpoints, diameter), MapInfo_Map_supportedGametypes |= it.m_flags);
418 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE.m_flags)
421 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE.m_flags;
422 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS.m_flags;
425 LOG_TRACE("-> diameter ", ftos(diameter));
426 LOG_TRACE("; spawnpoints ", ftos(spawnpoints));
427 LOG_TRACE("; modes ", ftos(MapInfo_Map_supportedGametypes));
434 void _MapInfo_Map_Reset()
436 MapInfo_Map_title = "<TITLE>";
437 MapInfo_Map_titlestring = "<TITLE>";
438 MapInfo_Map_description = "<DESCRIPTION>";
439 MapInfo_Map_author = "<AUTHOR>";
440 MapInfo_Map_supportedGametypes = 0;
441 MapInfo_Map_supportedFeatures = 0;
442 MapInfo_Map_flags = 0;
443 MapInfo_Map_clientstuff = "";
444 MapInfo_Map_fog = "";
445 MapInfo_Map_mins = '0 0 0';
446 MapInfo_Map_maxs = '0 0 0';
449 string _MapInfo_GetDefault(Gametype t)
451 return t.m_legacydefaults;
454 void _MapInfo_Map_ApplyGametype(string s, Gametype pWantedType, Gametype pThisType, int load_default)
457 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
458 if(!(pThisType.m_flags & pWantedType.m_flags))
462 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, false);
464 if(!pWantedType.frags) // these modes don't use fraglimit
466 cvar_set("fraglimit", "0");
472 cvar_set("fraglimit", sa);
478 cvar_set("timelimit", sa);
481 if(pWantedType.m_setTeams)
485 pWantedType.m_setTeams(sa);
489 // rc = timelimit timelimit_qualification laps laps_teamplay
490 if(pWantedType == MAPINFO_TYPE_RACE)
492 cvar_set("fraglimit", "0"); // special case!
494 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
495 cvar_set("g_race_qualifying_timelimit", sa);
500 if(cvar("g_race_teams") < 2)
501 cvar_set("fraglimit", sa);
506 if(cvar("g_race_teams") >= 2)
507 cvar_set("fraglimit", sa);
511 if(!pWantedType.frags) // these modes don't use fraglimit
513 cvar_set("leadlimit", "0");
519 cvar_set("leadlimit", sa);
524 string _MapInfo_GetDefaultEx(Gametype t)
526 return t ? t.model2 : "";
529 float _MapInfo_GetTeamPlayBool(Gametype t)
531 return t ? t.team : false;
534 void _MapInfo_Map_ApplyGametypeEx(string s, Gametype pWantedType, Gametype pThisType)
536 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
537 if (!(pThisType.m_flags & pWantedType.m_flags))
540 // reset all the cvars to their defaults
542 cvar_set("timelimit", cvar_defstring("timelimit"));
543 cvar_set("leadlimit", cvar_defstring("leadlimit"));
544 cvar_set("fraglimit", cvar_defstring("fraglimit"));
545 FOREACH(Gametypes, true, it.m_parse_mapinfo(string_null, string_null));
547 string fraglimit_normal = string_null;
548 string fraglimit_teams = string_null;
550 for (s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s); s != ""; s = cdr(s)) {
552 if (sa == "") continue;
553 int p = strstrofs(sa, "=", 0);
556 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
559 string k = substring(sa, 0, p);
560 string v = substring(sa, p + 1, -1);
565 cvar_set("timelimit", v);
570 cvar_set("leadlimit", v);
579 fraglimit_normal = v;
582 case "teampointlimit":
594 FOREACH(Gametypes, true, handled |= it.m_parse_mapinfo(k, v));
595 if (!handled && WARN_COND)
596 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
599 if (pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
602 cvar_set("fraglimit", fraglimit_teams);
607 cvar_set("fraglimit", fraglimit_normal);
611 Gametype MapInfo_Type_FromString(string gtype, bool dowarn, bool is_q3compat)
613 string replacement = "";
617 case "nexball": replacement = "nb"; break;
618 case "freezetag": replacement = "ft"; break;
619 case "keepaway": replacement = "ka"; break;
620 case "invasion": replacement = "inv"; break;
621 case "assault": replacement = "as"; break;
622 case "race": replacement = "rc"; break;
624 case "ffa": replacement = "dm"; do_warn = false; break;
626 case "oneflag": replacement = "ctf"; do_warn = false; break;
627 case "tourney": replacement = "duel"; do_warn = false; break;
628 case "arena": if(is_q3compat) { replacement = "ca"; do_warn = false; } break;
630 if (replacement != "")
632 if (dowarn && WARN_COND)
633 LOG_WARNF("MapInfo_Type_FromString (probably %s): using deprecated name '%s'. Should use '%s'.", MapInfo_Map_bspname, gtype, replacement);
636 FOREACH(Gametypes, it.mdl == gtype, return it);
640 string MapInfo_Type_Description(Gametype t)
642 return t ? t.gametype_description : "";
645 string MapInfo_Type_ToString(Gametype t)
647 return t ? t.mdl : "";
650 string MapInfo_Type_ToText(Gametype t)
652 /* xgettext:no-c-format */
653 return t ? t.message : _("@!#%'n Tuba Throwing");
656 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
660 // tabs are invalid, treat them as "empty"
661 s = strreplace("\t", "", s);
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 float fh = fopen(s, FILE_READ);
700 LOG_WARN("Map ", pFilename, " references not existing config file ", s);
704 while((s = fgets(fh)))
706 s = strreplace("\t", "", s); // treat tabs as "empty", perform here first to ensure coments are detected
707 // catch different sorts of comments
708 if(s == "") // empty lines
710 if(substring(s, 0, 1) == "#") // UNIX style
712 if(substring(s, 0, 2) == "//") // C++ style
714 if(substring(s, 0, 1) == "_") // q3map style
717 if(substring(s, 0, 4) == "set ")
718 s = substring(s, 4, -1);
719 if(substring(s, 0, 5) == "seta ")
720 s = substring(s, 5, -1);
722 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
728 LOG_WARN("Map ", pFilename, " uses too many levels of inclusion");
731 || !cvar_value_issafe(t)
732 || !cvar_value_issafe(s)
733 || matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
736 LOG_WARN("Map ", pFilename, " contains a potentially harmful setting, ignored");
738 else if(matchacl(acl, t) <= 0)
741 LOG_WARN("Map ", pFilename, " contains a denied setting, ignored");
745 if(type == 0) // server set
747 LOG_TRACE("Applying temporary setting ", t, " := ", s);
749 if(cvar("g_campaign"))
750 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
757 LOG_TRACE("Applying temporary client setting ", t, " := ", s);
758 MapInfo_Map_clientstuff = strcat(
759 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
765 float MapInfo_isRedundant(string fn, string t)
767 // normalize file name
768 fn = strreplace("_", "", fn);
769 fn = strreplace("-", "", fn);
771 // normalize visible title
772 t = strreplace(":", "", t);
773 t = strreplace(" ", "", t);
774 t = strreplace("_", "", t);
775 t = strreplace("-", "", t);
776 t = strreplace("'", "", t);
777 t = strdecolorize(t);
779 // we allow the visible title to have punctuation the file name does
780 // not, but not vice versa
781 if(!strcasecmp(fn, t))
787 bool _MapInfo_ParseArena(string arena_filename, int fh, string pFilename, Gametype pGametypeToSet, bool isdefi, bool isgenerator)
789 // NOTE: .arena files can hold more than 1 map's information!
790 // to handle this, we're going to store gathered information in local variables and save it if we encounter the correct map name
791 bool in_brackets = false; // testing a potential mapinfo section (within brackets)
792 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!)
793 string stored_Map_description = "";
794 string stored_Map_title = "";
795 string stored_Map_author = "";
796 int stored_supportedGametypes = 0;
797 int stored_supportedFeatures = 0;
798 int stored_flags = 0;
802 if (!((s = fgets(fh))))
805 // catch different sorts of comments
806 if(s == "") // empty lines
808 if(substring(s, 0, 1) == "#") // UNIX style
810 if(substring(s, 0, 2) == "//") // C++ style
812 if(substring(s, 0, 1) == "_") // q3map style
814 if(strstrofs(s, "{", 0) >= 0)
817 return false; // edge case? already in a bracketed section!
821 else if(!in_brackets)
823 // if we're not inside a bracket, don't process map info
826 if(strstrofs(s, "}", 0) >= 0)
829 return false; // no starting bracket! let the mapinfo generation system handle it
833 MapInfo_Map_description = stored_Map_description;
834 if(stored_Map_title != "")
835 MapInfo_Map_title = stored_Map_title;
836 MapInfo_Map_author = stored_Map_author;
838 MapInfo_Map_supportedGametypes = stored_supportedGametypes;
841 FOREACH(Gametypes, it.m_flags & stored_supportedGametypes,
843 _MapInfo_Map_ApplyGametype ("", pGametypeToSet, it, true);
846 MapInfo_Map_supportedFeatures = stored_supportedFeatures;
847 MapInfo_Map_flags = stored_flags;
848 return true; // no need to continue through the file, we have our map!
852 // discard any gathered locals, we're not using the correct map!
853 stored_Map_description = "";
854 stored_Map_title = "";
855 stored_Map_author = "";
856 stored_supportedGametypes = 0;
857 stored_supportedFeatures = 0;
863 s = strreplace("\t", " ", s);
865 float p = strstrofs(s, "//", 0);
867 s = substring(s, 0, p);
869 // perform an initial trim to ensure the first argument is properly obtained
870 // remove leading spaces
871 while(substring(s, 0, 1) == " ")
872 s = substring(s, 1, -1);
874 t = car(s); s = cdr(s);
875 t = strtolower(t); // apparently some q3 maps use capitalized parameters
877 // remove trailing spaces
878 while(substring(t, -1, 1) == " ")
879 t = substring(t, 0, -2);
881 // remove trailing spaces
882 while(substring(s, -1, 1) == " ")
883 s = substring(s, 0, -2);
884 // remove leading spaces
885 while(substring(s, 0, 1) == " ")
886 s = substring(s, 1, -1);
887 // limited support of ""
888 // remove trailing and leading " of s
889 if(substring(s, 0, 1) == "\"")
891 if(substring(s, -1, 1) == "\"")
892 s = substring(s, 1, -2);
895 stored_Map_title = s;
896 else if(t == "author")
897 stored_Map_author = s;
900 // if there is a valid gametype in this .arena file, include it in the menu
901 stored_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
902 // type in quake 3 holds all the supported gametypes, so we must loop through all of them
903 string types = strreplace("team", "tdm ft", s); // TODO: handle support here better to include more Xonotic teamplay modes
904 FOREACH_WORD(types, true,
906 Gametype f = MapInfo_Type_FromString(it, false, true);
908 stored_supportedGametypes |= f.m_flags;
911 else if(t == "style" && isdefi)
913 // we have a defrag map on our hands, add CTS!
915 stored_supportedGametypes |= MAPINFO_TYPE_CTS.m_flags;
919 if(strtolower(s) == strtolower(pFilename))
920 dosave = true; // yay, found our map!
922 else if(t == "quote")
923 stored_Map_description = s;
927 // if the map wasn't found in the .arena, fall back to generated .mapinfo
931 #if defined(CSQC) || defined(MENUQC)
932 string(string filename) whichpack = #503;
934 string _MapInfo_FindArenaFile(string pFilename, string extension)
936 string fallback = strcat("scripts/", pFilename, extension);
937 if(!checkextension("DP_QC_FS_SEARCH_PACKFILE"))
939 string base_pack = whichpack(strcat("maps/", pFilename, ".bsp"));
940 if(base_pack == "") // this map isn't packaged!
943 int glob = search_packfile_begin(strcat("scripts/*", extension), true, true, base_pack);
946 int n = search_getsize(glob);
947 for(int j = 0; j < n; ++j)
949 string file = search_getfilename(glob, j);
951 int fh = fopen(file, FILE_READ);
954 for(string s; (s = fgets(fh)); )
956 int offset = strstrofs(s, "map", 0);
959 if(strstrofs(strtolower(s), strcat("\"", strtolower(pFilename), "\""), offset) >= 0) // quake 3 is case insensitive
963 return file; // FOUND IT!
971 return fallback; // if we get here, a valid .arena file could not be found
974 // load info about a map by name into the MapInfo_Map_* globals
975 float MapInfo_Get_ByName_NoFallbacks(string pFilename, int pAllowGenerate, Gametype pGametypeToSet)
984 acl = MAPINFO_SETTEMP_ACL_USER;
986 if(strstrofs(pFilename, "/", 0) >= 0)
988 LOG_WARN("Invalid character in map name, ignored");
992 if(pGametypeToSet == NULL)
993 if(MapInfo_Cache_Retrieve(pFilename))
998 MapInfo_Map_bspname = pFilename;
1000 // default all generic fields so they have "good" values in case something fails
1001 fn = strcat("maps/", pFilename, ".mapinfo");
1002 fh = fopen(fn, FILE_READ);
1005 if(autocvar_g_mapinfo_arena_compat)
1007 // try for .arena or .defi files if no .mapinfo exists
1008 bool isdefi = false;
1009 fn = _MapInfo_FindArenaFile(pFilename, ".arena");
1010 fh = fopen(fn, FILE_READ);
1014 fn = _MapInfo_FindArenaFile(pFilename, ".defi");
1015 fh = fopen(fn, FILE_READ);
1019 _MapInfo_Map_Reset();
1020 if(_MapInfo_ParseArena(fn, fh, pFilename, pGametypeToSet, isdefi, false))
1021 goto mapinfo_handled; // skip generation
1025 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
1026 fh = fopen(fn, FILE_READ);
1031 _MapInfo_Map_Reset();
1032 r = _MapInfo_Generate(pFilename);
1035 fh = fopen(fn, FILE_WRITE);
1036 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
1037 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
1038 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
1039 if(_MapInfo_Map_worldspawn_music != "")
1042 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
1044 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
1046 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
1048 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
1052 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
1053 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
1056 i = floor(random() * n);
1057 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
1060 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
1062 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
1063 fputs(fh, "has weapons\n");
1065 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
1066 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
1067 fputs(fh, "has turrets\n");
1069 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
1070 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
1071 fputs(fh, "has vehicles\n");
1073 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
1074 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
1075 fputs(fh, "frustrating\n");
1077 FOREACH(Gametypes, MapInfo_Map_supportedGametypes & it.m_flags, {
1078 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(it), _MapInfo_GetDefaultEx(it)));
1081 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
1082 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
1083 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
1084 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
1085 fputs(fh, "// optional: hidden\n");
1090 fh = fopen(fn, FILE_READ);
1092 error("... but I just wrote it!");
1096 LOG_WARN("autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo");
1099 _MapInfo_Map_Reset();
1102 if (!((s = fgets(fh))))
1105 // catch different sorts of comments
1106 if(s == "") // empty lines
1108 if(substring(s, 0, 1) == "#") // UNIX style
1110 if(substring(s, 0, 2) == "//") // C++ style
1112 if(substring(s, 0, 1) == "_") // q3map style
1115 float p = strstrofs(s, "//", 0);
1117 s = substring(s, 0, p);
1119 t = car(s); s = cdr(s);
1121 MapInfo_Map_title = s;
1122 else if(t == "description")
1123 MapInfo_Map_description = s;
1124 else if(t == "author")
1125 MapInfo_Map_author = s;
1128 t = car(s); // s = cdr(s);
1129 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1130 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
1131 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
1132 else if(t == "monsters") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
1133 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1135 LOG_WARN("Map ", pFilename, " supports unknown feature ", t, ", ignored");
1137 else if(t == "hidden")
1139 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
1141 else if(t == "forbidden")
1143 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
1145 else if(t == "frustrating")
1147 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
1149 else if(t == "noautomaplist")
1151 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1153 else if(t == "gameversion_min")
1155 if (cvar("gameversion") < stof(s))
1156 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1158 else if(t == "type")
1160 t = car(s); s = cdr(s);
1161 Gametype f = MapInfo_Type_FromString(t, true, false);
1163 //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'.");
1165 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, true);
1167 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
1169 else if(t == "gametype")
1171 t = car(s); s = cdr(s);
1172 Gametype f = MapInfo_Type_FromString(t, true, false);
1174 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
1176 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
1178 else if(t == "size")
1180 float a, b, c, d, e;
1181 t = car(s); s = cdr(s); a = stof(t);
1182 t = car(s); s = cdr(s); b = stof(t);
1183 t = car(s); s = cdr(s); c = stof(t);
1184 t = car(s); s = cdr(s); d = stof(t);
1185 t = car(s); s = cdr(s); e = stof(t);
1189 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");
1193 t = car(s); s = cdr(s); f = stof(t);
1197 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");
1201 if(a >= d || b >= e || c >= f)
1204 LOG_WARN("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs");
1208 MapInfo_Map_mins.x = a;
1209 MapInfo_Map_mins.y = b;
1210 MapInfo_Map_mins.z = c;
1211 MapInfo_Map_maxs.x = d;
1212 MapInfo_Map_maxs.y = e;
1213 MapInfo_Map_maxs.z = f;
1218 else if(t == "settemp_for_type")
1220 t = car(s); s = cdr(s);
1221 bool all = t == "all";
1223 if(all || (f = MapInfo_Type_FromString(t, true, false)))
1225 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
1227 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1232 LOG_DEBUG("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored");
1235 else if(t == "clientsettemp_for_type")
1237 t = car(s); s = cdr(s);
1238 bool all = t == "all";
1240 if(all || (f = MapInfo_Type_FromString(t, true, false)))
1242 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
1244 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1249 LOG_DEBUG("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored");
1254 if (!cvar_value_issafe(s))
1257 LOG_WARN("Map ", pFilename, " contains a potentially harmful fog setting, ignored");
1260 MapInfo_Map_fog = s;
1262 else if(t == "cdtrack")
1264 t = car(s); s = cdr(s);
1265 // We do this only if pGametypeToSet even though this
1266 // content is theoretically game type independent,
1267 // because MapInfo_Map_clientstuff contains otherwise
1268 // game type dependent stuff. That way this value stays
1269 // empty when not setting a game type to not set any
1270 // false expectations.
1273 if (!cvar_value_issafe(t))
1276 LOG_WARN("Map ", pFilename, " contains a potentially harmful cdtrack, ignored");
1279 MapInfo_Map_clientstuff = strcat(
1280 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1285 LOG_WARN("Map ", pFilename, " provides unknown info item ", t, ", ignored");
1287 LABEL(mapinfo_handled)
1290 if(MapInfo_Map_title == "<TITLE>")
1291 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1292 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1293 MapInfo_Map_titlestring = MapInfo_Map_title;
1295 MapInfo_Map_titlestring = sprintf("%s: %s", MapInfo_Map_bspname, MapInfo_Map_title);
1297 MapInfo_Cache_Store();
1298 if(MapInfo_Map_supportedGametypes != 0)
1301 LOG_WARN("Map ", pFilename, " supports no game types, ignored");
1304 int MapInfo_Get_ByName(string pFilename, float pAllowGenerate, Gametype pGametypeToSet)
1306 int r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1308 FOREACH(Gametypes, it.m_isForcedSupported(it), _MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, it));
1312 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet.m_flags))
1314 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1315 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1323 float MapInfo_FindName(string s)
1325 // if there is exactly one map of prefix s, return it
1326 // if not, return the null string
1327 // note that DP sorts glob results... so I can use a binary search
1331 // invariants: r is behind s, l-1 is equal or before
1334 m = floor((l + r) / 2);
1335 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1336 cmp = strcasecmp(MapInfo_FindName_match, s);
1338 return m; // found and good
1340 l = m + 1; // l-1 is before s
1344 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1345 MapInfo_FindName_firstResult = l;
1346 // r == l, so: l is behind s, l-1 is before
1347 // SO: if there is any, l is the one with the right prefix
1348 // and l+1 may be one too
1349 if(l == MapInfo_count)
1351 MapInfo_FindName_match = string_null;
1352 MapInfo_FindName_firstResult = -1;
1353 return -1; // no MapInfo_FindName_match, behind last item
1355 if(!startsWithNocase(MapInfo_FindName_match, s))
1357 MapInfo_FindName_match = string_null;
1358 MapInfo_FindName_firstResult = -1;
1359 return -1; // wrong prefix
1361 if(l == MapInfo_count - 1)
1362 return l; // last one, nothing can follow => unique
1363 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1365 MapInfo_FindName_match = string_null;
1366 return -1; // ambigous MapInfo_FindName_match
1371 string MapInfo_FixName(string s)
1373 MapInfo_FindName(s);
1374 return MapInfo_FindName_match;
1377 int MapInfo_CurrentFeatures()
1380 // TODO: find a better way to check if weapons are required on the map
1381 if(!(cvar("g_instagib") || cvar("g_overkill") || cvar("g_nix") || cvar("g_weaponarena") || !cvar("g_pickup_items") || !cvar("g_melee_only")
1382 || cvar("g_race") || cvar("g_cts") || cvar("g_nexball") || cvar("g_ca") || cvar("g_freezetag") || cvar("g_lms")))
1383 req |= MAPINFO_FEATURE_WEAPONS;
1387 Gametype MapInfo_CurrentGametype()
1389 Gametype prev = MapInfo_Type_FromString(cvar_string("gamecfg"), false, false);
1390 FOREACH(Gametypes, cvar(it.netname) && it != prev, return it);
1391 return prev ? prev : MAPINFO_TYPE_DEATHMATCH;
1394 float _MapInfo_CheckMap(string s, bool gametype_only) // returns 0 if the map can't be played with the current settings, 1 otherwise
1396 if(!MapInfo_Get_ByName(s, 1, NULL))
1398 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype().m_flags) == 0)
1402 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1407 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1410 r = _MapInfo_CheckMap(s, false);
1411 MapInfo_ClearTemps();
1415 void MapInfo_SwitchGameType(Gametype t)
1417 FOREACH(Gametypes, true, cvar_set(it.netname, (it == t) ? "1" : "0"));
1420 void MapInfo_LoadMap(string s, float reinit)
1422 MapInfo_Map_supportedGametypes = 0;
1423 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1424 //if(!MapInfo_CheckMap(s))
1426 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1427 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH.m_flags);
1430 LOG_INFO("Switching to map ", s);
1432 cvar_settemp_restore();
1434 localcmd(strcat("\nmap ", s, "\n"));
1436 localcmd(strcat("\nchangelevel ", s, "\n"));
1439 string MapInfo_ListAllowedMaps(Gametype type, float pRequiredFlags, float pForbiddenFlags)
1443 // to make absolutely sure:
1444 MapInfo_Enumerate();
1445 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1448 for(float i = 0; i < MapInfo_count; ++i)
1449 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1450 return substring(out, 1, strlen(out) - 1);
1453 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1457 // to make absolutely sure:
1458 MapInfo_Enumerate();
1459 _MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1462 for(float i = 0; i < MapInfo_count; ++i)
1463 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1465 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1467 return substring(out, 1, strlen(out) - 1);
1470 void MapInfo_LoadMapSettings_SaveGameType(Gametype t)
1472 MapInfo_SwitchGameType(t);
1473 cvar_set("gamecfg", t.mdl);
1474 MapInfo_LoadedGametype = t;
1477 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1479 Gametype t = MapInfo_CurrentGametype();
1480 MapInfo_LoadMapSettings_SaveGameType(t);
1482 if(!_MapInfo_CheckMap(s, true)) // with underscore, it keeps temps
1484 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1486 LOG_SEVERE("can't play the selected map in the given game mode. Working with only the override settings.");
1487 _MapInfo_Map_ApplyGametypeEx("", t, t);
1488 return; // do not call Get_ByName!
1491 if(MapInfo_Map_supportedGametypes == 0)
1493 RandomSelection_Init();
1494 FOREACH(Gametypes, it.m_priority == 2,
1496 MapInfo_Map_supportedGametypes |= it.m_flags;
1497 RandomSelection_AddEnt(it, 1, 1);
1499 if(RandomSelection_chosen_ent)
1500 t = RandomSelection_chosen_ent;
1501 LOG_SEVEREF("Mapinfo system is not functional at all. Falling back to a preferred mode (%s).", t.mdl);
1502 MapInfo_LoadMapSettings_SaveGameType(t);
1503 _MapInfo_Map_ApplyGametypeEx("", t, t);
1504 return; // do not call Get_ByName!
1508 // find the lowest bit in the supported gametypes
1509 // unnecessary now that we select one at random
1511 while(!(MapInfo_Map_supportedGametypes & 1))
1514 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes >> 1);
1517 RandomSelection_Init();
1518 Gametype t_prev = t;
1519 FOREACH(Gametypes, MapInfo_Map_supportedGametypes & it.m_flags,
1521 RandomSelection_AddEnt(it, 1, it.m_priority);
1523 if(RandomSelection_chosen_ent)
1524 t = RandomSelection_chosen_ent;
1526 // t is now a supported mode!
1527 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);
1528 MapInfo_LoadMapSettings_SaveGameType(t);
1530 if(!_MapInfo_CheckMap(s, false)) { // with underscore, it keeps temps
1531 LOG_WARNF("the selected map lacks features required by current settings; playing anyway.");
1533 MapInfo_Get_ByName(s, 1, t);
1536 void MapInfo_ClearTemps()
1538 MapInfo_Map_bspname = string_null;
1539 MapInfo_Map_title = string_null;
1540 MapInfo_Map_titlestring = string_null;
1541 MapInfo_Map_description = string_null;
1542 MapInfo_Map_author = string_null;
1543 MapInfo_Map_clientstuff = string_null;
1544 MapInfo_Map_supportedGametypes = 0;
1545 MapInfo_Map_supportedFeatures = 0;
1548 void MapInfo_Shutdown()
1550 MapInfo_ClearTemps();
1551 MapInfo_Filter_Free();
1552 MapInfo_Cache_Destroy();
1553 if(_MapInfo_globopen)
1555 search_end(_MapInfo_globhandle);
1556 _MapInfo_globhandle = -1;
1557 _MapInfo_globopen = false;
1561 int MapInfo_ForbiddenFlags()
1563 int f = MAPINFO_FLAG_FORBIDDEN;
1566 if (!cvar("g_maplist_allow_hidden"))
1568 f |= MAPINFO_FLAG_HIDDEN;
1570 if (!cvar("g_maplist_allow_frustrating"))
1571 f |= MAPINFO_FLAG_FRUSTRATING;
1576 int MapInfo_RequiredFlags()
1580 if(cvar("g_maplist_allow_frustrating") > 1)
1581 f |= MAPINFO_FLAG_FRUSTRATING;