3 #include "../client/defs.qh"
5 #include <common/weapons/_all.qh>
9 #include <common/monsters/_mod.qh>
13 #define WARN_COND false
15 bool autocvar_g_mapinfo_ignore_warnings;
16 #define WARN_COND (!autocvar_g_mapinfo_ignore_warnings && MapInfo_Map_bspname == mi_shortname)
19 // generic string stuff
21 int _MapInfo_Cache_Active;
22 int _MapInfo_Cache_DB_NameToIndex;
23 int _MapInfo_Cache_Buf_IndexToMapData;
25 void MapInfo_Cache_Destroy()
27 if(!_MapInfo_Cache_Active)
30 db_close(_MapInfo_Cache_DB_NameToIndex);
31 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
32 _MapInfo_Cache_Active = 0;
35 void MapInfo_Cache_Create()
37 MapInfo_Cache_Destroy();
38 _MapInfo_Cache_DB_NameToIndex = db_create();
39 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
40 _MapInfo_Cache_Active = 1;
43 void MapInfo_Cache_Invalidate()
45 if(!_MapInfo_Cache_Active)
48 MapInfo_Cache_Create();
51 void MapInfo_Cache_Store()
55 if(!_MapInfo_Cache_Active)
58 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
61 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
62 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
67 // now store all the stuff
68 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
69 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
70 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
71 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
72 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
73 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
74 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
75 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
78 float MapInfo_Cache_Retrieve(string map)
82 if(!_MapInfo_Cache_Active)
85 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
90 // now retrieve all the stuff
91 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
92 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
93 MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
94 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
95 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
96 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
97 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
98 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
103 // GLOB HANDLING (for all BSP files)
104 float _MapInfo_globopen;
105 float _MapInfo_globcount;
106 float _MapInfo_globhandle;
107 string _MapInfo_GlobItem(float i)
110 if(!_MapInfo_globopen)
112 s = search_getfilename(_MapInfo_globhandle, i);
113 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
116 void MapInfo_Enumerate()
118 if(_MapInfo_globopen)
120 search_end(_MapInfo_globhandle);
121 _MapInfo_globopen = 0;
123 MapInfo_Cache_Invalidate();
124 _MapInfo_globhandle = search_begin("maps/*.bsp", true, true);
125 if(_MapInfo_globhandle >= 0)
127 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
128 _MapInfo_globopen = 1;
131 _MapInfo_globcount = 0;
134 // filter the info by game type mask (updates MapInfo_count)
136 float _MapInfo_filtered;
137 float _MapInfo_filtered_allocated;
138 float MapInfo_FilterList_Lookup(float i)
140 return stof(bufstr_get(_MapInfo_filtered, i));
143 void _MapInfo_FilterList_swap(float i, float j, entity pass)
146 h = bufstr_get(_MapInfo_filtered, i);
147 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
148 bufstr_set(_MapInfo_filtered, j, h);
151 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
154 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
155 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
156 return strcasecmp(a, b);
159 float MapInfo_FilterGametype(Gametype pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
161 return _MapInfo_FilterGametype(pGametype.m_flags, pFeatures, pFlagsRequired, pFlagsForbidden, pAbortOnGenerate);
163 float _MapInfo_FilterGametype(int pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
166 if (!_MapInfo_filtered_allocated)
168 _MapInfo_filtered_allocated = 1;
169 _MapInfo_filtered = buf_create();
172 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
174 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.
177 LOG_TRACE("Autogenerated a .mapinfo, doing the rest later.");
178 MapInfo_progress = i / _MapInfo_globcount;
181 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
182 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
183 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
184 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
185 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
187 MapInfo_count = j + 1;
188 MapInfo_ClearTemps();
190 // sometimes the glob isn't sorted nicely, so fix it here...
191 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
195 void MapInfo_FilterString(string sf)
197 // this function further filters _MapInfo_filtered, which is prepared by MapInfo_FilterGametype by string
201 for(i = 0, j = -1; i < MapInfo_count; ++i)
203 if (MapInfo_Get_ByID(i))
205 // prepare for keyword filter
206 if (MapInfo_Map_title && strstrofs(MapInfo_Map_title, "<TITLE>", 0) == -1)
207 title = MapInfo_Map_title;
209 title = MapInfo_Map_bspname;
211 if((strstrofs(strtolower(title), strtolower(sf), 0)) >= 0)
212 bufstr_set(_MapInfo_filtered, ++j, bufstr_get(_MapInfo_filtered, i));
215 MapInfo_count = j + 1;
216 MapInfo_ClearTemps();
218 // sometimes the glob isn't sorted nicely, so fix it here...
219 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
222 void MapInfo_Filter_Free()
224 if(_MapInfo_filtered_allocated)
226 buf_del(_MapInfo_filtered);
227 _MapInfo_filtered_allocated = 0;
231 // load info about the i-th map into the MapInfo_Map_* globals
232 string MapInfo_BSPName_ByID(float i)
234 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
237 string unquote(string s)
240 for(float i = 0; i < l; ++i)
242 string ch = substring(s, i, 1);
243 if((ch != " ") && (ch != "\""))
245 for(float j = l - i - 1; j > 0; --j)
247 ch = substring(s, i+j, 1);
248 if(ch != " ") if(ch != "\"")
249 return substring(s, i, j+1);
251 return substring(s, i, 1);
257 float MapInfo_Get_ByID(float i)
259 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, NULL))
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 = "";
304 if (!((s = fgets(fh))))
306 if(inWorldspawn == 1)
307 if(startsWith(s, "}"))
313 if(k == "classname" && v == "worldspawn")
315 else if(k == "author")
316 MapInfo_Map_author = v;
317 else if(k == "_description")
318 MapInfo_Map_description = v;
319 else if(k == "music")
320 _MapInfo_Map_worldspawn_music = v;
321 else if(k == "noise")
322 _MapInfo_Map_worldspawn_music = v;
323 else if(k == "message")
325 i = strstrofs(v, " by ", 0);
326 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
328 MapInfo_Map_title = substring(v, 0, i);
329 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
332 MapInfo_Map_title = v;
339 o = stov(strcat("'", v, "'"));
340 mapMins.x = min(mapMins.x, o.x);
341 mapMins.y = min(mapMins.y, o.y);
342 mapMins.z = min(mapMins.z, o.z);
343 mapMaxs.x = max(mapMaxs.x, o.x);
344 mapMaxs.y = max(mapMaxs.y, o.y);
345 mapMaxs.z = max(mapMaxs.z, o.z);
347 else if(k == "race_place")
352 else if(k == "classname")
354 if(v == "info_player_team1")
356 else if(v == "info_player_team2")
358 else if(v == "info_player_start")
360 else if(v == "info_player_deathmatch")
362 else if(v == "weapon_nex")
364 else if(v == "weapon_railgun")
366 else if(startsWith(v, "weapon_"))
367 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
368 else if(startsWith(v, "turret_"))
369 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
370 else if(startsWith(v, "vehicle_"))
371 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
372 else if(startsWith(v, "monster_"))
373 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
374 else if(v == "target_music" || v == "trigger_music")
375 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
377 FOREACH(Gametypes, true, it.m_generate_mapinfo(it, v));
383 LOG_WARN(fn, " ended still in worldspawn, BUG");
386 diameter = vlen(mapMaxs - mapMins);
388 int twoBaseModes = 0;
389 FOREACH(Gametypes, it.m_isTwoBaseMode(), twoBaseModes |= it.m_flags);
390 if(twoBaseModes && (twoBaseModes &= MapInfo_Map_supportedGametypes))
392 // we have a symmetrical map, don't add the modes without bases
396 FOREACH(Gametypes, it.m_isAlwaysSupported(it, spawnpoints, diameter), MapInfo_Map_supportedGametypes |= it.m_flags);
399 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE.m_flags)
402 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE.m_flags;
403 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS.m_flags;
406 LOG_TRACE("-> diameter ", ftos(diameter));
407 LOG_TRACE("; spawnpoints ", ftos(spawnpoints));
408 LOG_TRACE("; modes ", ftos(MapInfo_Map_supportedGametypes));
415 void _MapInfo_Map_Reset()
417 MapInfo_Map_title = "<TITLE>";
418 MapInfo_Map_titlestring = "<TITLE>";
419 MapInfo_Map_description = "<DESCRIPTION>";
420 MapInfo_Map_author = "<AUTHOR>";
421 MapInfo_Map_supportedGametypes = 0;
422 MapInfo_Map_supportedFeatures = 0;
423 MapInfo_Map_flags = 0;
424 MapInfo_Map_clientstuff = "";
425 MapInfo_Map_fog = "";
426 MapInfo_Map_mins = '0 0 0';
427 MapInfo_Map_maxs = '0 0 0';
430 string _MapInfo_GetDefault(Gametype t)
432 return t.m_legacydefaults;
435 void _MapInfo_Map_ApplyGametype(string s, Gametype pWantedType, Gametype pThisType, int load_default)
438 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
439 if(!(pThisType.m_flags & pWantedType.m_flags))
443 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, false);
445 if(!pWantedType.frags) // these modes don't use fraglimit
447 cvar_set("fraglimit", "0");
453 cvar_set("fraglimit", sa);
459 cvar_set("timelimit", sa);
462 if(pWantedType.m_setTeams)
466 pWantedType.m_setTeams(sa);
470 // rc = timelimit timelimit_qualification laps laps_teamplay
471 if(pWantedType == MAPINFO_TYPE_RACE)
473 cvar_set("fraglimit", "0"); // special case!
475 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
476 cvar_set("g_race_qualifying_timelimit", sa);
481 if(cvar("g_race_teams") < 2)
482 cvar_set("fraglimit", sa);
487 if(cvar("g_race_teams") >= 2)
488 cvar_set("fraglimit", sa);
492 if(!pWantedType.frags) // these modes don't use fraglimit
494 cvar_set("leadlimit", "0");
500 cvar_set("leadlimit", sa);
505 string _MapInfo_GetDefaultEx(Gametype t)
507 return t ? t.model2 : "";
510 float _MapInfo_GetTeamPlayBool(Gametype t)
512 return t ? t.team : false;
515 void _MapInfo_Map_ApplyGametypeEx(string s, Gametype pWantedType, Gametype pThisType)
517 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
518 if (!(pThisType.m_flags & pWantedType.m_flags))
521 // reset all the cvars to their defaults
523 cvar_set("timelimit", cvar_defstring("timelimit"));
524 cvar_set("leadlimit", cvar_defstring("leadlimit"));
525 cvar_set("fraglimit", cvar_defstring("fraglimit"));
526 FOREACH(Gametypes, true, it.m_parse_mapinfo(string_null, string_null));
528 string fraglimit_normal = string_null;
529 string fraglimit_teams = string_null;
531 for (s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s); s != ""; s = cdr(s)) {
533 if (sa == "") continue;
534 int p = strstrofs(sa, "=", 0);
537 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
540 string k = substring(sa, 0, p);
541 string v = substring(sa, p + 1, -1);
546 cvar_set("timelimit", v);
551 cvar_set("leadlimit", v);
560 fraglimit_normal = v;
563 case "teampointlimit":
575 FOREACH(Gametypes, true, handled |= it.m_parse_mapinfo(k, v));
576 if (!handled && WARN_COND)
577 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
580 if (pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
583 cvar_set("fraglimit", fraglimit_teams);
588 cvar_set("fraglimit", fraglimit_normal);
592 Gametype MapInfo_Type_FromString(string gtype)
594 string replacement = "";
598 case "nexball": replacement = "nb"; break;
599 case "freezetag": replacement = "ft"; break;
600 case "keepaway": replacement = "ka"; break;
601 case "invasion": replacement = "inv"; break;
602 case "assault": replacement = "as"; break;
603 case "race": replacement = "rc"; break;
605 case "ffa": replacement = "dm"; do_warn = false; break;
607 case "oneflag": replacement = "ctf"; do_warn = false; break;
608 case "team": replacement = "tdm"; do_warn = false; break;
609 case "tourney": replacement = "duel"; do_warn = false; break;
611 if (replacement != "")
613 if(do_warn && WARN_COND)
614 LOG_WARNF("MapInfo_Type_FromString (probably %s): using deprecated name '%s'. Should use '%s'.", MapInfo_Map_bspname, gtype, replacement);
617 FOREACH(Gametypes, it.mdl == gtype, return it);
621 string MapInfo_Type_Description(Gametype t)
623 return t ? t.gametype_description : "";
626 string MapInfo_Type_ToString(Gametype t)
628 return t ? t.mdl : "";
631 string MapInfo_Type_ToText(Gametype t)
633 /* xgettext:no-c-format */
634 return t ? t.message : _("@!#%'n Tuba Throwing");
637 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
641 t = car(s); s = cdr(s);
643 // limited support of "" and comments
644 // remove trailing and leading " of t
645 if(substring(t, 0, 1) == "\"")
647 if(substring(t, -1, 1) == "\"")
648 t = substring(t, 1, -2);
651 // remove leading " of s
652 if(substring(s, 0, 1) == "\"")
654 s = substring(s, 1, -1);
656 // remove trailing " of s, and all that follows (cvar description)
657 o = strstrofs(s, "\"", 0);
659 s = substring(s, 0, o);
661 // remove // comments
662 o = strstrofs(s, "//", 0);
664 s = substring(s, 0, o);
666 // remove trailing spaces
667 while(substring(s, -1, 1) == " ")
668 s = substring(s, 0, -2);
674 fh = fopen(s, FILE_READ);
678 LOG_WARN("Map ", pFilename, " references not existing config file ", s);
682 while((s = fgets(fh)))
684 // catch different sorts of comments
685 if(s == "") // empty lines
687 if(substring(s, 0, 1) == "#") // UNIX style
689 if(substring(s, 0, 2) == "//") // C++ style
691 if(substring(s, 0, 1) == "_") // q3map style
694 if(substring(s, 0, 4) == "set ")
695 s = substring(s, 4, -1);
696 if(substring(s, 0, 5) == "seta ")
697 s = substring(s, 5, -1);
699 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
705 LOG_WARN("Map ", pFilename, " uses too many levels of inclusion");
708 || !cvar_value_issafe(t)
709 || !cvar_value_issafe(s)
710 || matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
713 LOG_WARN("Map ", pFilename, " contains a potentially harmful setting, ignored");
715 else if(matchacl(acl, t) <= 0)
718 LOG_WARN("Map ", pFilename, " contains a denied setting, ignored");
722 if(type == 0) // server set
724 LOG_TRACE("Applying temporary setting ", t, " := ", s);
726 if(cvar("g_campaign"))
727 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
734 LOG_TRACE("Applying temporary client setting ", t, " := ", s);
735 MapInfo_Map_clientstuff = strcat(
736 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
742 float MapInfo_isRedundant(string fn, string t)
744 // normalize file name
745 fn = strreplace("_", "", fn);
746 fn = strreplace("-", "", fn);
748 // normalize visible title
749 t = strreplace(":", "", t);
750 t = strreplace(" ", "", t);
751 t = strreplace("_", "", t);
752 t = strreplace("-", "", t);
753 t = strreplace("'", "", t);
754 t = strdecolorize(t);
756 // we allow the visible title to have punctuation the file name does
757 // not, but not vice versa
758 if(!strcasecmp(fn, t))
764 bool _MapInfo_ParseArena(string arena_filename, int fh, string pFilename, Gametype pGametypeToSet, bool isdefi)
766 // NOTE: .arena files can hold more than 1 map's information!
767 // to handle this, we're going to store gathered information in local variables and save it if we encounter the correct map name
768 bool in_brackets = false; // testing a potential mapinfo section (within brackets)
769 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!)
770 string stored_Map_description = "";
771 string stored_Map_title = "";
772 string stored_Map_author = "";
773 int stored_supportedGametypes = 0;
777 if (!((s = fgets(fh))))
780 // catch different sorts of comments
781 if(s == "") // empty lines
783 if(substring(s, 0, 1) == "#") // UNIX style
785 if(substring(s, 0, 2) == "//") // C++ style
787 if(substring(s, 0, 1) == "_") // q3map style
789 if(strstrofs(s, "{", 0) >= 0)
792 return false; // edge case? already in a bracketed section!
796 else if(!in_brackets)
798 // if we're not inside a bracket, don't process map info
801 if(strstrofs(s, "}", 0) >= 0)
804 return false; // no starting bracket! let the mapinfo generation system handle it
808 MapInfo_Map_description = stored_Map_description;
809 if(stored_Map_title != "")
810 MapInfo_Map_title = stored_Map_title;
811 MapInfo_Map_author = stored_Map_author;
812 FOREACH(Gametypes, it.m_flags & stored_supportedGametypes,
814 _MapInfo_Map_ApplyGametype ("", pGametypeToSet, it, true);
816 return true; // no need to continue through the file, we have our map!
820 // discard any gathered locals, we're not using the correct map!
821 stored_Map_description = "";
822 stored_Map_title = "";
823 stored_Map_author = "";
824 stored_supportedGametypes = 0;
829 s = strreplace("\t", " ", s);
831 float p = strstrofs(s, "//", 0);
833 s = substring(s, 0, p);
835 // perform an initial trim to ensure the first argument is properly obtained
836 // remove leading spaces
837 while(substring(s, 0, 1) == " ")
838 s = substring(s, 1, -1);
840 t = car(s); s = cdr(s);
841 t = strtolower(t); // apparently some q3 maps use capitalized parameters
843 // remove trailing spaces
844 while(substring(t, -1, 1) == " ")
845 t = substring(t, 0, -2);
847 // remove trailing spaces
848 while(substring(s, -1, 1) == " ")
849 s = substring(s, 0, -2);
850 // remove leading spaces
851 while(substring(s, 0, 1) == " ")
852 s = substring(s, 1, -1);
853 // limited support of ""
854 // remove trailing and leading " of s
855 if(substring(s, 0, 1) == "\"")
857 if(substring(s, -1, 1) == "\"")
858 s = substring(s, 1, -2);
861 stored_Map_title = s;
862 else if(t == "author")
863 stored_Map_author = s;
866 // if there is a valid gametype in this .arena file, include it in the menu
867 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
868 // type in quake 3 holds all the supported gametypes, so we must loop through all of them
869 FOREACH_WORD(s, true,
871 Gametype f = MapInfo_Type_FromString(it);
873 stored_supportedGametypes |= f.m_flags;
876 else if(t == "style" && isdefi)
878 // we have a defrag map on our hands, add CTS!
880 stored_supportedGametypes |= MAPINFO_TYPE_CTS.m_flags;
884 if(strtolower(s) == strtolower(pFilename))
885 dosave = true; // yay, found our map!
887 else if(t == "quote")
888 stored_Map_description = s;
892 // if the map wasn't found in the .arena, fall back to generated .mapinfo
896 #if defined(CSQC) || defined(MENUQC)
897 string(string filename) whichpack = #503;
899 string _MapInfo_FindArenaFile(string pFilename, string extension)
901 string base_pack = whichpack(strcat("maps/", pFilename, ".bsp"));
902 string fallback = strcat("scripts/", pFilename, extension);
903 if(base_pack == "") // this map isn't packaged!
906 int glob = search_begin(strcat("scripts/*", extension), true, true);
909 int n = search_getsize(glob);
910 for(int j = 0; j < n; ++j)
912 string file = search_getfilename(glob, j);
913 if(whichpack(file) != base_pack)
914 continue; // not in the same pk3!
916 int fh = fopen(file, FILE_READ);
919 for(string s; (s = fgets(fh)); )
921 int offset = strstrofs(s, "map", 0);
924 if(strstrofs(strtolower(s), strcat("\"", strtolower(pFilename), "\""), offset) >= 0) // quake 3 is case insensitive
928 return file; // FOUND IT!
936 return fallback; // if we get here, a valid .arena file could not be found
939 // load info about a map by name into the MapInfo_Map_* globals
940 float MapInfo_Get_ByName_NoFallbacks(string pFilename, int pAllowGenerate, Gametype pGametypeToSet)
949 acl = MAPINFO_SETTEMP_ACL_USER;
951 if(strstrofs(pFilename, "/", 0) >= 0)
953 LOG_WARN("Invalid character in map name, ignored");
957 if(pGametypeToSet == NULL)
958 if(MapInfo_Cache_Retrieve(pFilename))
963 MapInfo_Map_bspname = pFilename;
965 // default all generic fields so they have "good" values in case something fails
966 fn = strcat("maps/", pFilename, ".mapinfo");
967 fh = fopen(fn, FILE_READ);
971 // try for a .arena or .defi file if no .mapinfo exists
972 fn = _MapInfo_FindArenaFile(pFilename, ".arena");
973 fh = fopen(fn, FILE_READ);
977 fn = _MapInfo_FindArenaFile(pFilename, ".defi");
978 fh = fopen(fn, FILE_READ);
982 _MapInfo_Map_Reset();
983 if(_MapInfo_ParseArena(fn, fh, pFilename, pGametypeToSet, isdefi))
984 goto mapinfo_handled; // skip generation
987 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
988 fh = fopen(fn, FILE_READ);
993 _MapInfo_Map_Reset();
994 r = _MapInfo_Generate(pFilename);
997 fh = fopen(fn, FILE_WRITE);
998 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
999 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
1000 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
1001 if(_MapInfo_Map_worldspawn_music != "")
1004 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
1006 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
1008 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
1010 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
1014 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
1015 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
1018 i = floor(random() * n);
1019 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
1022 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
1024 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
1025 fputs(fh, "has weapons\n");
1027 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
1028 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
1029 fputs(fh, "has turrets\n");
1031 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
1032 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
1033 fputs(fh, "has vehicles\n");
1035 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
1036 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
1037 fputs(fh, "frustrating\n");
1039 FOREACH(Gametypes, MapInfo_Map_supportedGametypes & it.m_flags, {
1040 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(it), _MapInfo_GetDefaultEx(it)));
1043 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
1044 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
1045 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
1046 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
1047 fputs(fh, "// optional: hidden\n");
1052 fh = fopen(fn, FILE_READ);
1054 error("... but I just wrote it!");
1058 LOG_WARN("autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo");
1061 _MapInfo_Map_Reset();
1064 if (!((s = fgets(fh))))
1067 // catch different sorts of comments
1068 if(s == "") // empty lines
1070 if(substring(s, 0, 1) == "#") // UNIX style
1072 if(substring(s, 0, 2) == "//") // C++ style
1074 if(substring(s, 0, 1) == "_") // q3map style
1077 float p = strstrofs(s, "//", 0);
1079 s = substring(s, 0, p);
1081 t = car(s); s = cdr(s);
1083 MapInfo_Map_title = s;
1084 else if(t == "description")
1085 MapInfo_Map_description = s;
1086 else if(t == "author")
1087 MapInfo_Map_author = s;
1090 t = car(s); // s = cdr(s);
1091 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1092 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
1093 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
1094 else if(t == "monsters") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
1095 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1097 LOG_WARN("Map ", pFilename, " supports unknown feature ", t, ", ignored");
1099 else if(t == "hidden")
1101 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
1103 else if(t == "forbidden")
1105 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
1107 else if(t == "frustrating")
1109 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
1111 else if(t == "noautomaplist")
1113 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1115 else if(t == "gameversion_min")
1117 if (cvar("gameversion") < stof(s))
1118 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1120 else if(t == "type")
1122 t = car(s); s = cdr(s);
1123 Gametype f = MapInfo_Type_FromString(t);
1125 //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'.");
1127 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, true);
1129 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
1131 else if(t == "gametype")
1133 t = car(s); s = cdr(s);
1134 Gametype f = MapInfo_Type_FromString(t);
1136 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
1138 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
1140 else if(t == "size")
1142 float a, b, c, d, e;
1143 t = car(s); s = cdr(s); a = stof(t);
1144 t = car(s); s = cdr(s); b = stof(t);
1145 t = car(s); s = cdr(s); c = stof(t);
1146 t = car(s); s = cdr(s); d = stof(t);
1147 t = car(s); s = cdr(s); e = stof(t);
1151 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");
1155 t = car(s); s = cdr(s); f = stof(t);
1159 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");
1163 if(a >= d || b >= e || c >= f)
1166 LOG_WARN("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs");
1170 MapInfo_Map_mins.x = a;
1171 MapInfo_Map_mins.y = b;
1172 MapInfo_Map_mins.z = c;
1173 MapInfo_Map_maxs.x = d;
1174 MapInfo_Map_maxs.y = e;
1175 MapInfo_Map_maxs.z = f;
1180 else if(t == "settemp_for_type")
1182 t = car(s); s = cdr(s);
1183 bool all = t == "all";
1185 if(all || (f = MapInfo_Type_FromString(t)))
1187 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
1189 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1194 LOG_DEBUG("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored");
1197 else if(t == "clientsettemp_for_type")
1199 t = car(s); s = cdr(s);
1200 bool all = t == "all";
1202 if(all || (f = MapInfo_Type_FromString(t)))
1204 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
1206 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1211 LOG_DEBUG("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored");
1216 if (!cvar_value_issafe(s))
1219 LOG_WARN("Map ", pFilename, " contains a potentially harmful fog setting, ignored");
1222 MapInfo_Map_fog = s;
1224 else if(t == "cdtrack")
1226 t = car(s); s = cdr(s);
1227 // We do this only if pGametypeToSet even though this
1228 // content is theoretically game type independent,
1229 // because MapInfo_Map_clientstuff contains otherwise
1230 // game type dependent stuff. That way this value stays
1231 // empty when not setting a game type to not set any
1232 // false expectations.
1235 if (!cvar_value_issafe(t))
1238 LOG_WARN("Map ", pFilename, " contains a potentially harmful cdtrack, ignored");
1241 MapInfo_Map_clientstuff = strcat(
1242 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1247 LOG_WARN("Map ", pFilename, " provides unknown info item ", t, ", ignored");
1249 LABEL(mapinfo_handled)
1252 if(MapInfo_Map_title == "<TITLE>")
1253 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1254 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1255 MapInfo_Map_titlestring = MapInfo_Map_title;
1257 MapInfo_Map_titlestring = sprintf("%s: %s", MapInfo_Map_bspname, MapInfo_Map_title);
1259 MapInfo_Cache_Store();
1260 if(MapInfo_Map_supportedGametypes != 0)
1263 LOG_WARN("Map ", pFilename, " supports no game types, ignored");
1266 int MapInfo_Get_ByName(string pFilename, float pAllowGenerate, Gametype pGametypeToSet)
1268 int r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1270 FOREACH(Gametypes, it.m_isForcedSupported(it), _MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, it));
1274 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet.m_flags))
1276 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1277 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1285 float MapInfo_FindName(string s)
1287 // if there is exactly one map of prefix s, return it
1288 // if not, return the null string
1289 // note that DP sorts glob results... so I can use a binary search
1293 // invariants: r is behind s, l-1 is equal or before
1296 m = floor((l + r) / 2);
1297 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1298 cmp = strcasecmp(MapInfo_FindName_match, s);
1300 return m; // found and good
1302 l = m + 1; // l-1 is before s
1306 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1307 MapInfo_FindName_firstResult = l;
1308 // r == l, so: l is behind s, l-1 is before
1309 // SO: if there is any, l is the one with the right prefix
1310 // and l+1 may be one too
1311 if(l == MapInfo_count)
1313 MapInfo_FindName_match = string_null;
1314 MapInfo_FindName_firstResult = -1;
1315 return -1; // no MapInfo_FindName_match, behind last item
1317 if(!startsWithNocase(MapInfo_FindName_match, s))
1319 MapInfo_FindName_match = string_null;
1320 MapInfo_FindName_firstResult = -1;
1321 return -1; // wrong prefix
1323 if(l == MapInfo_count - 1)
1324 return l; // last one, nothing can follow => unique
1325 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1327 MapInfo_FindName_match = string_null;
1328 return -1; // ambigous MapInfo_FindName_match
1333 string MapInfo_FixName(string s)
1335 MapInfo_FindName(s);
1336 return MapInfo_FindName_match;
1339 int MapInfo_CurrentFeatures()
1342 // TODO: find a better way to check if weapons are required on the map
1343 if(!(cvar("g_instagib") || cvar("g_overkill") || cvar("g_nix") || cvar("g_weaponarena") || !cvar("g_pickup_items")
1344 || cvar("g_race") || cvar("g_cts") || cvar("g_nexball") || cvar("g_ca") || cvar("g_freezetag") || cvar("g_lms")))
1345 req |= MAPINFO_FEATURE_WEAPONS;
1349 Gametype MapInfo_CurrentGametype()
1351 Gametype prev = REGISTRY_GET(Gametypes, cvar("gamecfg"));
1352 FOREACH(Gametypes, cvar(it.netname) && it != prev, return it);
1353 return prev ? prev : MAPINFO_TYPE_DEATHMATCH;
1356 float _MapInfo_CheckMap(string s, bool gametype_only) // returns 0 if the map can't be played with the current settings, 1 otherwise
1358 if(!MapInfo_Get_ByName(s, 1, NULL))
1360 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype().m_flags) == 0)
1364 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1369 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1372 r = _MapInfo_CheckMap(s, false);
1373 MapInfo_ClearTemps();
1377 void MapInfo_SwitchGameType(Gametype t)
1379 FOREACH(Gametypes, true, cvar_set(it.netname, (it == t) ? "1" : "0"));
1382 void MapInfo_LoadMap(string s, float reinit)
1384 MapInfo_Map_supportedGametypes = 0;
1385 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1386 //if(!MapInfo_CheckMap(s))
1388 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1389 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH.m_flags);
1392 LOG_INFO("Switching to map ", s);
1394 cvar_settemp_restore();
1396 localcmd(strcat("\nmap ", s, "\n"));
1398 localcmd(strcat("\nchangelevel ", s, "\n"));
1401 string MapInfo_ListAllowedMaps(Gametype type, float pRequiredFlags, float pForbiddenFlags)
1405 // to make absolutely sure:
1406 MapInfo_Enumerate();
1407 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1410 for(float i = 0; i < MapInfo_count; ++i)
1411 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1412 return substring(out, 1, strlen(out) - 1);
1415 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1419 // to make absolutely sure:
1420 MapInfo_Enumerate();
1421 _MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1424 for(float i = 0; i < MapInfo_count; ++i)
1425 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1427 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1429 return substring(out, 1, strlen(out) - 1);
1432 void MapInfo_LoadMapSettings_SaveGameType(Gametype t)
1434 MapInfo_SwitchGameType(t);
1435 cvar_set("gamecfg", ftos(t.m_id));
1436 MapInfo_LoadedGametype = t;
1439 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1441 Gametype t = MapInfo_CurrentGametype();
1442 MapInfo_LoadMapSettings_SaveGameType(t);
1444 if(!_MapInfo_CheckMap(s, true)) // with underscore, it keeps temps
1446 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1448 LOG_SEVERE("can't play the selected map in the given game mode. Working with only the override settings.");
1449 _MapInfo_Map_ApplyGametypeEx("", t, t);
1450 return; // do not call Get_ByName!
1453 if(MapInfo_Map_supportedGametypes == 0)
1455 LOG_SEVERE("Mapinfo system is not functional at all. Assuming deathmatch.");
1456 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH.m_flags;
1457 MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1458 _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1459 return; // do not call Get_ByName!
1463 while(!(MapInfo_Map_supportedGametypes & 1))
1466 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes >> 1);
1468 Gametype t_prev = t;
1469 FOREACH(Gametypes, it.m_flags == _t, { t = it; break; });
1471 // t is now a supported mode!
1472 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);
1473 MapInfo_LoadMapSettings_SaveGameType(t);
1475 if(!_MapInfo_CheckMap(s, false)) { // with underscore, it keeps temps
1476 LOG_WARNF("the selected map lacks features required by current settings; playing anyway.");
1478 MapInfo_Get_ByName(s, 1, t);
1481 void MapInfo_ClearTemps()
1483 MapInfo_Map_bspname = string_null;
1484 MapInfo_Map_title = string_null;
1485 MapInfo_Map_titlestring = string_null;
1486 MapInfo_Map_description = string_null;
1487 MapInfo_Map_author = string_null;
1488 MapInfo_Map_clientstuff = string_null;
1489 MapInfo_Map_supportedGametypes = 0;
1490 MapInfo_Map_supportedFeatures = 0;
1493 void MapInfo_Shutdown()
1495 MapInfo_ClearTemps();
1496 MapInfo_Filter_Free();
1497 MapInfo_Cache_Destroy();
1498 if(_MapInfo_globopen)
1500 search_end(_MapInfo_globhandle);
1501 _MapInfo_globhandle = -1;
1502 _MapInfo_globopen = false;
1506 int MapInfo_ForbiddenFlags()
1508 int f = MAPINFO_FLAG_FORBIDDEN;
1511 if (!cvar("g_maplist_allow_hidden"))
1513 f |= MAPINFO_FLAG_HIDDEN;
1515 if (!cvar("g_maplist_allow_frustrating"))
1516 f |= MAPINFO_FLAG_FRUSTRATING;
1521 int MapInfo_RequiredFlags()
1525 if(cvar("g_maplist_allow_frustrating") > 1)
1526 f |= MAPINFO_FLAG_FRUSTRATING;