3 #include <common/util.qh>
4 #include <common/weapons/_all.qh>
7 #include <common/util.qh>
8 #include <common/monsters/_mod.qh>
12 #define WARN_COND false
14 bool autocvar_g_mapinfo_ignore_warnings;
15 #define WARN_COND (!autocvar_g_mapinfo_ignore_warnings && MapInfo_Map_bspname == mi_shortname)
18 // generic string stuff
20 int _MapInfo_Cache_Active;
21 int _MapInfo_Cache_DB_NameToIndex;
22 int _MapInfo_Cache_Buf_IndexToMapData;
24 void MapInfo_Cache_Destroy()
26 if(!_MapInfo_Cache_Active)
29 db_close(_MapInfo_Cache_DB_NameToIndex);
30 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
31 _MapInfo_Cache_Active = 0;
34 void MapInfo_Cache_Create()
36 MapInfo_Cache_Destroy();
37 _MapInfo_Cache_DB_NameToIndex = db_create();
38 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
39 _MapInfo_Cache_Active = 1;
42 void MapInfo_Cache_Invalidate()
44 if(!_MapInfo_Cache_Active)
47 MapInfo_Cache_Create();
50 void MapInfo_Cache_Store()
54 if(!_MapInfo_Cache_Active)
57 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
60 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
61 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
66 // now store all the stuff
67 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
68 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
69 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
70 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
71 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
72 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
73 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
74 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
77 float MapInfo_Cache_Retrieve(string map)
81 if(!_MapInfo_Cache_Active)
84 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
89 // now retrieve all the stuff
90 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
91 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
92 MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
93 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
94 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
95 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
96 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
97 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
102 // GLOB HANDLING (for all BSP files)
103 float _MapInfo_globopen;
104 float _MapInfo_globcount;
105 float _MapInfo_globhandle;
106 string _MapInfo_GlobItem(float i)
109 if(!_MapInfo_globopen)
111 s = search_getfilename(_MapInfo_globhandle, i);
112 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
115 void MapInfo_Enumerate()
117 if(_MapInfo_globopen)
119 search_end(_MapInfo_globhandle);
120 _MapInfo_globopen = 0;
122 MapInfo_Cache_Invalidate();
123 _MapInfo_globhandle = search_begin("maps/*.bsp", true, true);
124 if(_MapInfo_globhandle >= 0)
126 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
127 _MapInfo_globopen = 1;
130 _MapInfo_globcount = 0;
133 // filter the info by game type mask (updates MapInfo_count)
135 float _MapInfo_filtered;
136 float _MapInfo_filtered_allocated;
137 float MapInfo_FilterList_Lookup(float i)
139 return stof(bufstr_get(_MapInfo_filtered, i));
142 void _MapInfo_FilterList_swap(float i, float j, entity pass)
145 h = bufstr_get(_MapInfo_filtered, i);
146 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
147 bufstr_set(_MapInfo_filtered, j, h);
150 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
153 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
154 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
155 return strcasecmp(a, b);
158 float MapInfo_FilterGametype(Gametype pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
160 return _MapInfo_FilterGametype(pGametype.m_flags, pFeatures, pFlagsRequired, pFlagsForbidden, pAbortOnGenerate);
162 float _MapInfo_FilterGametype(int pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
165 if (!_MapInfo_filtered_allocated)
167 _MapInfo_filtered_allocated = 1;
168 _MapInfo_filtered = buf_create();
171 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
173 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.
176 LOG_TRACE("Autogenerated a .mapinfo, doing the rest later.");
177 MapInfo_progress = i / _MapInfo_globcount;
180 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
181 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
182 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
183 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
184 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
186 MapInfo_count = j + 1;
187 MapInfo_ClearTemps();
189 // sometimes the glob isn't sorted nicely, so fix it here...
190 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
194 void MapInfo_FilterString(string sf)
196 // this function further filters _MapInfo_filtered, which is prepared by MapInfo_FilterGametype by string
200 for(i = 0, j = -1; i < MapInfo_count; ++i)
202 if (MapInfo_Get_ByID(i))
204 // prepare for keyword filter
205 if (MapInfo_Map_title && strstrofs(MapInfo_Map_title, "<TITLE>", 0) == -1)
206 title = MapInfo_Map_title;
208 title = MapInfo_Map_bspname;
210 if((strstrofs(strtolower(title), strtolower(sf), 0)) >= 0)
211 bufstr_set(_MapInfo_filtered, ++j, bufstr_get(_MapInfo_filtered, i));
214 MapInfo_count = j + 1;
215 MapInfo_ClearTemps();
217 // sometimes the glob isn't sorted nicely, so fix it here...
218 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
221 void MapInfo_Filter_Free()
223 if(_MapInfo_filtered_allocated)
225 buf_del(_MapInfo_filtered);
226 _MapInfo_filtered_allocated = 0;
230 // load info about the i-th map into the MapInfo_Map_* globals
231 string MapInfo_BSPName_ByID(float i)
233 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
236 string unquote(string s)
239 for(float i = 0; i < l; ++i)
241 string ch = substring(s, i, 1);
242 if((ch != " ") && (ch != "\""))
244 for(float j = l - i - 1; j > 0; --j)
246 ch = substring(s, i+j, 1);
247 if(ch != " ") if(ch != "\"")
248 return substring(s, i, j+1);
250 return substring(s, i, 1);
256 bool MapInfo_Get_ByID(int i)
258 return MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, NULL) ? true : false;
261 string _MapInfo_Map_worldspawn_music;
263 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
272 float diameter, spawnpoints;
275 vector mapMins, mapMaxs;
278 fn = strcat("maps/", pFilename, ".ent");
279 fh = fopen(fn, FILE_READ);
283 fn = strcat("maps/", pFilename, ".bsp");
284 fh = fopen(fn, FILE_READ);
288 LOG_INFO("Analyzing ", fn, " to generate initial mapinfo");
291 MapInfo_Map_flags = 0;
292 MapInfo_Map_supportedGametypes = 0;
295 _MapInfo_Map_worldspawn_music = "";
301 if (!((s = fgets(fh))))
303 if(inWorldspawn == 1)
304 if(startsWith(s, "}"))
310 if(k == "classname" && v == "worldspawn")
312 else if(k == "author")
313 MapInfo_Map_author = v;
314 else if(k == "_description")
315 MapInfo_Map_description = v;
316 else if(k == "music")
317 _MapInfo_Map_worldspawn_music = v;
318 else if(k == "noise")
319 _MapInfo_Map_worldspawn_music = v;
320 else if(k == "message")
322 i = strstrofs(v, " by ", 0);
323 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
325 MapInfo_Map_title = substring(v, 0, i);
326 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
329 MapInfo_Map_title = v;
336 o = stov(strcat("'", v, "'"));
337 mapMins.x = min(mapMins.x, o.x);
338 mapMins.y = min(mapMins.y, o.y);
339 mapMins.z = min(mapMins.z, o.z);
340 mapMaxs.x = max(mapMaxs.x, o.x);
341 mapMaxs.y = max(mapMaxs.y, o.y);
342 mapMaxs.z = max(mapMaxs.z, o.z);
344 else if(k == "race_place")
349 else if(k == "classname")
351 if(v == "info_player_team1")
353 else if(v == "info_player_team2")
355 else if(v == "info_player_start")
357 else if(v == "info_player_deathmatch")
359 else if(v == "weapon_nex")
361 else if(v == "weapon_railgun")
363 else if(startsWith(v, "weapon_"))
364 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
365 else if(startsWith(v, "turret_"))
366 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
367 else if(startsWith(v, "vehicle_"))
368 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
369 else if(startsWith(v, "monster_"))
370 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
371 else if(v == "target_music" || v == "trigger_music")
372 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
374 FOREACH(Gametypes, true, it.m_generate_mapinfo(it, v));
380 LOG_WARN(fn, " ended still in worldspawn, BUG");
383 diameter = vlen(mapMaxs - mapMins);
385 int twoBaseModes = 0;
386 FOREACH(Gametypes, it.m_isTwoBaseMode(), twoBaseModes |= it.m_flags);
387 if(twoBaseModes && (twoBaseModes &= MapInfo_Map_supportedGametypes))
389 // we have a symmetrical map, don't add the modes without bases
393 FOREACH(Gametypes, it.m_isAlwaysSupported(it, spawnpoints, diameter), MapInfo_Map_supportedGametypes |= it.m_flags);
396 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE.m_flags)
399 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE.m_flags;
400 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS.m_flags;
403 LOG_TRACE("-> diameter ", ftos(diameter));
404 LOG_TRACE("; spawnpoints ", ftos(spawnpoints));
405 LOG_TRACE("; modes ", ftos(MapInfo_Map_supportedGametypes));
412 void _MapInfo_Map_Reset()
414 MapInfo_Map_title = "<TITLE>";
415 MapInfo_Map_titlestring = "<TITLE>";
416 MapInfo_Map_description = "<DESCRIPTION>";
417 MapInfo_Map_author = "<AUTHOR>";
418 MapInfo_Map_supportedGametypes = 0;
419 MapInfo_Map_supportedFeatures = 0;
420 MapInfo_Map_flags = 0;
421 MapInfo_Map_clientstuff = "";
422 MapInfo_Map_fog = "";
423 MapInfo_Map_mins = '0 0 0';
424 MapInfo_Map_maxs = '0 0 0';
427 string _MapInfo_GetDefault(Gametype t)
429 return t.m_legacydefaults;
432 void _MapInfo_Map_ApplyGametype(string s, Gametype pWantedType, Gametype pThisType, int load_default)
435 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
436 if(!(pThisType.m_flags & pWantedType.m_flags))
440 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, false);
442 if(!pWantedType.frags) // these modes don't use fraglimit
444 cvar_set("fraglimit", "0");
450 cvar_set("fraglimit", sa);
456 cvar_set("timelimit", sa);
459 if(pWantedType.m_setTeams)
463 pWantedType.m_setTeams(sa);
467 // rc = timelimit timelimit_qualification laps laps_teamplay
468 if(pWantedType == MAPINFO_TYPE_RACE)
470 cvar_set("fraglimit", "0"); // special case!
472 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
473 cvar_set("g_race_qualifying_timelimit", sa);
478 if(cvar("g_race_teams") < 2)
479 cvar_set("fraglimit", sa);
484 if(cvar("g_race_teams") >= 2)
485 cvar_set("fraglimit", sa);
489 if(!pWantedType.frags) // these modes don't use fraglimit
491 cvar_set("leadlimit", "0");
497 cvar_set("leadlimit", sa);
502 string _MapInfo_GetDefaultEx(Gametype t)
504 return t ? t.model2 : "";
507 float _MapInfo_GetTeamPlayBool(Gametype t)
509 return t ? t.team : false;
512 void _MapInfo_Map_ApplyGametypeEx(string s, Gametype pWantedType, Gametype pThisType)
514 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
515 if (!(pThisType.m_flags & pWantedType.m_flags))
518 // reset all the cvars to their defaults
520 cvar_set("timelimit", cvar_defstring("timelimit"));
521 cvar_set("leadlimit", cvar_defstring("leadlimit"));
522 cvar_set("fraglimit", cvar_defstring("fraglimit"));
523 FOREACH(Gametypes, true, it.m_parse_mapinfo(string_null, string_null));
525 string fraglimit_normal = string_null;
526 string fraglimit_teams = string_null;
528 for (s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s); s != ""; s = cdr(s)) {
530 if (sa == "") continue;
531 int p = strstrofs(sa, "=", 0);
534 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
537 string k = substring(sa, 0, p);
538 string v = substring(sa, p + 1, -1);
543 cvar_set("timelimit", v);
548 cvar_set("leadlimit", v);
557 fraglimit_normal = v;
560 case "teampointlimit":
572 FOREACH(Gametypes, true, handled |= it.m_parse_mapinfo(k, v));
573 if (!handled && WARN_COND)
574 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
577 if (pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
580 cvar_set("fraglimit", fraglimit_teams);
585 cvar_set("fraglimit", fraglimit_normal);
589 Gametype MapInfo_Type_FromString(string gtype, bool dowarn)
591 string replacement = "";
594 case "nexball": replacement = "nb"; break;
595 case "freezetag": replacement = "ft"; break;
596 case "keepaway": replacement = "ka"; break;
597 case "invasion": replacement = "inv"; break;
598 case "assault": replacement = "as"; break;
599 case "race": replacement = "rc"; break;
601 if (replacement != "")
603 if (dowarn && WARN_COND)
604 LOG_WARNF("MapInfo_Type_FromString (probably %s): using deprecated name '%s'. Should use '%s'.", MapInfo_Map_bspname, gtype, replacement);
607 FOREACH(Gametypes, it.mdl == gtype, return it);
611 string MapInfo_Type_Description(Gametype t)
613 return t ? t.gametype_description : "";
616 string MapInfo_Type_ToString(Gametype t)
618 return t ? t.mdl : "";
621 string MapInfo_Type_ToText(Gametype t)
623 /* xgettext:no-c-format */
624 return t ? t.message : _("@!#%'n Tuba Throwing");
627 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
631 // tabs are invalid, treat them as "empty"
632 s = strreplace("\t", "", s);
634 t = car(s); s = cdr(s);
636 // limited support of "" and comments
637 // remove trailing and leading " of t
638 if(substring(t, 0, 1) == "\"")
640 if(substring(t, -1, 1) == "\"")
641 t = substring(t, 1, -2);
644 // remove leading " of s
645 if(substring(s, 0, 1) == "\"")
647 s = substring(s, 1, -1);
649 // remove trailing " of s, and all that follows (cvar description)
650 o = strstrofs(s, "\"", 0);
652 s = substring(s, 0, o);
654 // remove // comments
655 o = strstrofs(s, "//", 0);
657 s = substring(s, 0, o);
659 // remove trailing spaces
660 while(substring(s, -1, 1) == " ")
661 s = substring(s, 0, -2);
667 float fh = fopen(s, FILE_READ);
671 LOG_WARN("Map ", pFilename, " references not existing config file ", s);
675 while((s = fgets(fh)))
677 s = strreplace("\t", "", s); // treat tabs as "empty", perform here first to ensure coments are detected
678 // catch different sorts of comments
679 if(s == "") // empty lines
681 if(substring(s, 0, 1) == "#") // UNIX style
683 if(substring(s, 0, 2) == "//") // C++ style
685 if(substring(s, 0, 1) == "_") // q3map style
688 if(substring(s, 0, 4) == "set ")
689 s = substring(s, 4, -1);
690 if(substring(s, 0, 5) == "seta ")
691 s = substring(s, 5, -1);
693 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
699 LOG_WARN("Map ", pFilename, " uses too many levels of inclusion");
702 || !cvar_value_issafe(t)
703 || !cvar_value_issafe(s)
704 || matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
707 LOG_WARN("Map ", pFilename, " contains a potentially harmful setting, ignored");
709 else if(matchacl(acl, t) <= 0)
712 LOG_WARN("Map ", pFilename, " contains a denied setting, ignored");
716 if(type == 0) // server set
718 LOG_TRACE("Applying temporary setting ", t, " := ", s);
720 if(cvar("g_campaign"))
721 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
728 LOG_TRACE("Applying temporary client setting ", t, " := ", s);
729 MapInfo_Map_clientstuff = strcat(
730 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
736 float MapInfo_isRedundant(string fn, string t)
738 // normalize file name
739 fn = strreplace("_", "", fn);
740 fn = strreplace("-", "", fn);
742 // normalize visible title
743 t = strreplace(":", "", t);
744 t = strreplace(" ", "", t);
745 t = strreplace("_", "", t);
746 t = strreplace("-", "", t);
747 t = strreplace("'", "", t);
748 t = strdecolorize(t);
750 // we allow the visible title to have punctuation the file name does
751 // not, but not vice versa
752 if(!strcasecmp(fn, t))
758 // load info about a map by name into the MapInfo_Map_* globals
759 float MapInfo_Get_ByName_NoFallbacks(string pFilename, int pAllowGenerate, Gametype pGametypeToSet)
768 acl = MAPINFO_SETTEMP_ACL_USER;
770 if(strstrofs(pFilename, "/", 0) >= 0)
772 LOG_WARN("Invalid character in map name, ignored");
776 if(pGametypeToSet == NULL)
777 if(MapInfo_Cache_Retrieve(pFilename))
782 MapInfo_Map_bspname = pFilename;
784 // default all generic fields so they have "good" values in case something fails
785 fn = strcat("maps/", pFilename, ".mapinfo");
786 fh = fopen(fn, FILE_READ);
789 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
790 fh = fopen(fn, FILE_READ);
795 _MapInfo_Map_Reset();
796 r = _MapInfo_Generate(pFilename);
799 fh = fopen(fn, FILE_WRITE);
800 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
801 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
802 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
803 if(_MapInfo_Map_worldspawn_music != "")
806 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
808 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
810 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
812 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
816 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
817 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
820 i = floor(random() * n);
821 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
824 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
826 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
827 fputs(fh, "has weapons\n");
829 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
830 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
831 fputs(fh, "has turrets\n");
833 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
834 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
835 fputs(fh, "has vehicles\n");
837 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
838 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
839 fputs(fh, "frustrating\n");
841 FOREACH(Gametypes, MapInfo_Map_supportedGametypes & it.m_flags, {
842 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(it), _MapInfo_GetDefaultEx(it)));
845 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
846 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
847 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
848 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
849 fputs(fh, "// optional: hidden\n");
854 fh = fopen(fn, FILE_READ);
856 error("... but I just wrote it!");
860 LOG_WARN("autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo");
863 _MapInfo_Map_Reset();
866 if (!((s = fgets(fh))))
869 // catch different sorts of comments
870 if(s == "") // empty lines
872 if(substring(s, 0, 1) == "#") // UNIX style
874 if(substring(s, 0, 2) == "//") // C++ style
876 if(substring(s, 0, 1) == "_") // q3map style
879 p = strstrofs(s, "//", 0);
881 s = substring(s, 0, p);
883 t = car(s); s = cdr(s);
885 MapInfo_Map_title = s;
886 else if(t == "description")
887 MapInfo_Map_description = s;
888 else if(t == "author")
889 MapInfo_Map_author = s;
892 t = car(s); // s = cdr(s);
893 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
894 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
895 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
896 else if(t == "monsters") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
897 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
899 LOG_WARN("Map ", pFilename, " supports unknown feature ", t, ", ignored");
901 else if(t == "hidden")
903 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
905 else if(t == "forbidden")
907 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
909 else if(t == "frustrating")
911 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
913 else if(t == "noautomaplist")
915 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
917 else if(t == "gameversion_min")
919 if (cvar("gameversion") < stof(s))
920 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
924 t = car(s); s = cdr(s);
925 Gametype f = MapInfo_Type_FromString(t, true);
927 //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'.");
929 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, true);
931 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
933 else if(t == "gametype")
935 t = car(s); s = cdr(s);
936 Gametype f = MapInfo_Type_FromString(t, true);
938 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
940 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
945 t = car(s); s = cdr(s); a = stof(t);
946 t = car(s); s = cdr(s); b = stof(t);
947 t = car(s); s = cdr(s); c = stof(t);
948 t = car(s); s = cdr(s); d = stof(t);
949 t = car(s); s = cdr(s); e = stof(t);
953 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");
957 t = car(s); s = cdr(s); f = stof(t);
961 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");
965 if(a >= d || b >= e || c >= f)
968 LOG_WARN("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs");
972 MapInfo_Map_mins.x = a;
973 MapInfo_Map_mins.y = b;
974 MapInfo_Map_mins.z = c;
975 MapInfo_Map_maxs.x = d;
976 MapInfo_Map_maxs.y = e;
977 MapInfo_Map_maxs.z = f;
982 else if(t == "settemp_for_type")
984 t = car(s); s = cdr(s);
985 bool all = t == "all";
987 if(all || (f = MapInfo_Type_FromString(t, true)))
989 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
991 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
996 LOG_DEBUG("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored");
999 else if(t == "clientsettemp_for_type")
1001 t = car(s); s = cdr(s);
1002 bool all = t == "all";
1004 if(all || (f = MapInfo_Type_FromString(t, true)))
1006 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
1008 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1013 LOG_DEBUG("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored");
1018 if (!cvar_value_issafe(s))
1021 LOG_WARN("Map ", pFilename, " contains a potentially harmful fog setting, ignored");
1024 MapInfo_Map_fog = s;
1026 else if(t == "cdtrack")
1028 t = car(s); s = cdr(s);
1029 // We do this only if pGametypeToSet even though this
1030 // content is theoretically game type independent,
1031 // because MapInfo_Map_clientstuff contains otherwise
1032 // game type dependent stuff. That way this value stays
1033 // empty when not setting a game type to not set any
1034 // false expectations.
1037 if (!cvar_value_issafe(t))
1040 LOG_WARN("Map ", pFilename, " contains a potentially harmful cdtrack, ignored");
1043 MapInfo_Map_clientstuff = strcat(
1044 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1049 LOG_WARN("Map ", pFilename, " provides unknown info item ", t, ", ignored");
1053 if(MapInfo_Map_title == "<TITLE>")
1054 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1055 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1056 MapInfo_Map_titlestring = MapInfo_Map_title;
1058 MapInfo_Map_titlestring = sprintf("%s: %s", MapInfo_Map_bspname, MapInfo_Map_title);
1060 MapInfo_Cache_Store();
1061 if(MapInfo_Map_supportedGametypes != 0)
1064 LOG_WARN("Map ", pFilename, " supports no game types, ignored");
1067 int MapInfo_Get_ByName(string pFilename, float pAllowGenerate, Gametype pGametypeToSet)
1069 int r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1071 FOREACH(Gametypes, it.m_isForcedSupported(it), _MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, it));
1075 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet.m_flags))
1077 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1078 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1086 float MapInfo_FindName(string s)
1088 // if there is exactly one map of prefix s, return it
1089 // if not, return the null string
1090 // note that DP sorts glob results... so I can use a binary search
1094 // invariants: r is behind s, l-1 is equal or before
1097 m = floor((l + r) / 2);
1098 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1099 cmp = strcasecmp(MapInfo_FindName_match, s);
1101 return m; // found and good
1103 l = m + 1; // l-1 is before s
1107 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1108 MapInfo_FindName_firstResult = l;
1109 // r == l, so: l is behind s, l-1 is before
1110 // SO: if there is any, l is the one with the right prefix
1111 // and l+1 may be one too
1112 if(l == MapInfo_count)
1114 MapInfo_FindName_match = string_null;
1115 MapInfo_FindName_firstResult = -1;
1116 return -1; // no MapInfo_FindName_match, behind last item
1118 if(!startsWithNocase(MapInfo_FindName_match, s))
1120 MapInfo_FindName_match = string_null;
1121 MapInfo_FindName_firstResult = -1;
1122 return -1; // wrong prefix
1124 if(l == MapInfo_count - 1)
1125 return l; // last one, nothing can follow => unique
1126 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1128 MapInfo_FindName_match = string_null;
1129 return -1; // ambigous MapInfo_FindName_match
1134 string MapInfo_FixName(string s)
1136 MapInfo_FindName(s);
1137 return MapInfo_FindName_match;
1140 int MapInfo_CurrentFeatures()
1143 // TODO: find a better way to check if weapons are required on the map
1144 if(!(cvar("g_instagib") || cvar("g_overkill") || cvar("g_nix") || cvar("g_weaponarena") || !cvar("g_pickup_items") || !cvar("g_melee_only")
1145 || cvar("g_race") || cvar("g_cts") || cvar("g_nexball") || cvar("g_ca") || cvar("g_freezetag") || cvar("g_lms")))
1146 req |= MAPINFO_FEATURE_WEAPONS;
1150 Gametype MapInfo_CurrentGametype()
1152 Gametype prev = MapInfo_Type_FromString(cvar_string("gamecfg"), false);
1153 FOREACH(Gametypes, cvar(it.netname) && it != prev, return it);
1154 return prev ? prev : MAPINFO_TYPE_DEATHMATCH;
1157 float _MapInfo_CheckMap(string s, bool gametype_only) // returns 0 if the map can't be played with the current settings, 1 otherwise
1159 if(!MapInfo_Get_ByName(s, 1, NULL))
1161 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype().m_flags) == 0)
1165 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1170 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1173 r = _MapInfo_CheckMap(s, false);
1174 MapInfo_ClearTemps();
1178 void MapInfo_SwitchGameType(Gametype t)
1180 FOREACH(Gametypes, true, cvar_set(it.netname, (it == t) ? "1" : "0"));
1183 void MapInfo_LoadMap(string s, float reinit)
1185 MapInfo_Map_supportedGametypes = 0;
1186 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1187 //if(!MapInfo_CheckMap(s))
1189 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1190 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH.m_flags);
1193 LOG_INFO("Switching to map ", s);
1195 cvar_settemp_restore();
1197 localcmd(strcat("\nmap ", s, "\n"));
1199 localcmd(strcat("\nchangelevel ", s, "\n"));
1202 string MapInfo_ListAllowedMaps(Gametype type, float pRequiredFlags, float pForbiddenFlags)
1206 // to make absolutely sure:
1207 MapInfo_Enumerate();
1208 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1211 for(float i = 0; i < MapInfo_count; ++i)
1212 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1213 return substring(out, 1, strlen(out) - 1);
1216 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1220 // to make absolutely sure:
1221 MapInfo_Enumerate();
1222 _MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1225 for(float i = 0; i < MapInfo_count; ++i)
1226 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1228 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1230 return substring(out, 1, strlen(out) - 1);
1233 void MapInfo_LoadMapSettings_SaveGameType(Gametype t)
1235 MapInfo_SwitchGameType(t);
1236 cvar_set("gamecfg", t.mdl);
1237 MapInfo_LoadedGametype = t;
1240 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1242 Gametype t = MapInfo_CurrentGametype();
1243 MapInfo_LoadMapSettings_SaveGameType(t);
1245 if(!_MapInfo_CheckMap(s, true)) // with underscore, it keeps temps
1247 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1249 LOG_SEVERE("can't play the selected map in the given game mode. Working with only the override settings.");
1250 _MapInfo_Map_ApplyGametypeEx("", t, t);
1251 return; // do not call Get_ByName!
1254 if(MapInfo_Map_supportedGametypes == 0)
1256 RandomSelection_Init();
1257 FOREACH(Gametypes, it.m_priority == 2,
1259 MapInfo_Map_supportedGametypes |= it.m_flags;
1260 RandomSelection_AddEnt(it, 1, 1);
1262 if(RandomSelection_chosen_ent)
1263 t = RandomSelection_chosen_ent;
1264 LOG_SEVEREF("Mapinfo system is not functional at all. Falling back to a preferred mode (%s).", t.mdl);
1265 MapInfo_LoadMapSettings_SaveGameType(t);
1266 _MapInfo_Map_ApplyGametypeEx("", t, t);
1267 return; // do not call Get_ByName!
1271 // find the lowest bit in the supported gametypes
1272 // unnecessary now that we select one at random
1274 while(!(MapInfo_Map_supportedGametypes & 1))
1277 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes >> 1);
1280 RandomSelection_Init();
1281 Gametype t_prev = t;
1282 FOREACH(Gametypes, MapInfo_Map_supportedGametypes & it.m_flags,
1284 RandomSelection_AddEnt(it, 1, it.m_priority);
1286 if(RandomSelection_chosen_ent)
1287 t = RandomSelection_chosen_ent;
1289 // t is now a supported mode!
1290 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);
1291 MapInfo_LoadMapSettings_SaveGameType(t);
1293 if(!_MapInfo_CheckMap(s, false)) { // with underscore, it keeps temps
1294 LOG_WARNF("the selected map lacks features required by current settings; playing anyway.");
1296 MapInfo_Get_ByName(s, 1, t);
1299 void MapInfo_ClearTemps()
1301 MapInfo_Map_bspname = string_null;
1302 MapInfo_Map_title = string_null;
1303 MapInfo_Map_titlestring = string_null;
1304 MapInfo_Map_description = string_null;
1305 MapInfo_Map_author = string_null;
1306 MapInfo_Map_clientstuff = string_null;
1307 MapInfo_Map_supportedGametypes = 0;
1308 MapInfo_Map_supportedFeatures = 0;
1311 void MapInfo_Shutdown()
1313 MapInfo_ClearTemps();
1314 MapInfo_Filter_Free();
1315 MapInfo_Cache_Destroy();
1316 if(_MapInfo_globopen)
1318 search_end(_MapInfo_globhandle);
1319 _MapInfo_globhandle = -1;
1320 _MapInfo_globopen = false;
1324 int MapInfo_ForbiddenFlags()
1326 int f = MAPINFO_FLAG_FORBIDDEN;
1329 if (!cvar("g_maplist_allow_hidden"))
1331 f |= MAPINFO_FLAG_HIDDEN;
1333 if (!cvar("g_maplist_allow_frustrating"))
1334 f |= MAPINFO_FLAG_FRUSTRATING;
1339 int MapInfo_RequiredFlags()
1343 if(cvar("g_maplist_allow_frustrating") > 1)
1344 f |= MAPINFO_FLAG_FRUSTRATING;