3 #include "../client/defs.qh"
5 #include <common/weapons/_all.qh>
9 #include <common/monsters/_mod.qh>
12 bool autocvar_g_mapinfo_ignore_warnings;
14 // generic string stuff
16 int _MapInfo_Cache_Active;
17 int _MapInfo_Cache_DB_NameToIndex;
18 int _MapInfo_Cache_Buf_IndexToMapData;
20 void MapInfo_Cache_Destroy()
22 if(!_MapInfo_Cache_Active)
25 db_close(_MapInfo_Cache_DB_NameToIndex);
26 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
27 _MapInfo_Cache_Active = 0;
30 void MapInfo_Cache_Create()
32 MapInfo_Cache_Destroy();
33 _MapInfo_Cache_DB_NameToIndex = db_create();
34 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
35 _MapInfo_Cache_Active = 1;
38 void MapInfo_Cache_Invalidate()
40 if(!_MapInfo_Cache_Active)
43 MapInfo_Cache_Create();
46 void MapInfo_Cache_Store()
50 if(!_MapInfo_Cache_Active)
53 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
56 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
57 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
62 // now store all the stuff
63 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
64 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
65 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
66 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
67 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
68 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
69 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
70 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
73 float MapInfo_Cache_Retrieve(string map)
77 if(!_MapInfo_Cache_Active)
80 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
85 // now retrieve all the stuff
86 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
87 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
88 MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
89 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
90 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
91 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
92 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
93 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
98 // GLOB HANDLING (for all BSP files)
99 float _MapInfo_globopen;
100 float _MapInfo_globcount;
101 float _MapInfo_globhandle;
102 string _MapInfo_GlobItem(float i)
105 if(!_MapInfo_globopen)
107 s = search_getfilename(_MapInfo_globhandle, i);
108 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
111 void MapInfo_Enumerate()
113 if(_MapInfo_globopen)
115 search_end(_MapInfo_globhandle);
116 _MapInfo_globopen = 0;
118 MapInfo_Cache_Invalidate();
119 _MapInfo_globhandle = search_begin("maps/*.bsp", true, true);
120 if(_MapInfo_globhandle >= 0)
122 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
123 _MapInfo_globopen = 1;
126 _MapInfo_globcount = 0;
129 // filter the info by game type mask (updates MapInfo_count)
131 float _MapInfo_filtered;
132 float _MapInfo_filtered_allocated;
133 float MapInfo_FilterList_Lookup(float i)
135 return stof(bufstr_get(_MapInfo_filtered, i));
138 void _MapInfo_FilterList_swap(float i, float j, entity pass)
141 h = bufstr_get(_MapInfo_filtered, i);
142 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
143 bufstr_set(_MapInfo_filtered, j, h);
146 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
149 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
150 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
151 return strcasecmp(a, b);
154 float MapInfo_FilterGametype(Gametype pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
156 return _MapInfo_FilterGametype(pGametype.m_flags, pFeatures, pFlagsRequired, pFlagsForbidden, pAbortOnGenerate);
158 float _MapInfo_FilterGametype(int pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
161 if (!_MapInfo_filtered_allocated)
163 _MapInfo_filtered_allocated = 1;
164 _MapInfo_filtered = buf_create();
167 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
169 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.
172 LOG_TRACE("Autogenerated a .mapinfo, doing the rest later.");
173 MapInfo_progress = i / _MapInfo_globcount;
176 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
177 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
178 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
179 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
180 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
182 MapInfo_count = j + 1;
183 MapInfo_ClearTemps();
185 // sometimes the glob isn't sorted nicely, so fix it here...
186 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
190 void MapInfo_FilterString(string sf)
192 // this function further filters _MapInfo_filtered, which is prepared by MapInfo_FilterGametype by string
196 for(i = 0, j = -1; i < MapInfo_count; ++i)
198 if (MapInfo_Get_ByID(i))
200 // prepare for keyword filter
201 if (MapInfo_Map_title && strstrofs(MapInfo_Map_title, "<TITLE>", 0) == -1)
202 title = MapInfo_Map_title;
204 title = MapInfo_Map_bspname;
206 if((strstrofs(strtolower(title), strtolower(sf), 0)) >= 0)
207 bufstr_set(_MapInfo_filtered, ++j, bufstr_get(_MapInfo_filtered, i));
210 MapInfo_count = j + 1;
211 MapInfo_ClearTemps();
213 // sometimes the glob isn't sorted nicely, so fix it here...
214 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, NULL);
217 void MapInfo_Filter_Free()
219 if(_MapInfo_filtered_allocated)
221 buf_del(_MapInfo_filtered);
222 _MapInfo_filtered_allocated = 0;
226 // load info about the i-th map into the MapInfo_Map_* globals
227 string MapInfo_BSPName_ByID(float i)
229 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
232 string unquote(string s)
235 for(float i = 0; i < l; ++i)
237 string ch = substring(s, i, 1);
238 if((ch != " ") && (ch != "\""))
240 for(float j = l - i - 1; j > 0; --j)
242 ch = substring(s, i+j, 1);
243 if(ch != " ") if(ch != "\"")
244 return substring(s, i, j+1);
246 return substring(s, i, 1);
252 float MapInfo_Get_ByID(float i)
254 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, NULL))
259 string _MapInfo_Map_worldspawn_music;
261 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
270 float diameter, spawnpoints;
273 vector mapMins, mapMaxs;
276 fn = strcat("maps/", pFilename, ".ent");
277 fh = fopen(fn, FILE_READ);
281 fn = strcat("maps/", pFilename, ".bsp");
282 fh = fopen(fn, FILE_READ);
286 LOG_INFO("Analyzing ", fn, " to generate initial mapinfo");
289 MapInfo_Map_flags = 0;
290 MapInfo_Map_supportedGametypes = 0;
293 _MapInfo_Map_worldspawn_music = "";
299 if (!((s = fgets(fh))))
301 if(inWorldspawn == 1)
302 if(startsWith(s, "}"))
308 if(k == "classname" && v == "worldspawn")
310 else if(k == "author")
311 MapInfo_Map_author = v;
312 else if(k == "_description")
313 MapInfo_Map_description = v;
314 else if(k == "music")
315 _MapInfo_Map_worldspawn_music = v;
316 else if(k == "noise")
317 _MapInfo_Map_worldspawn_music = v;
318 else if(k == "message")
320 i = strstrofs(v, " by ", 0);
321 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
323 MapInfo_Map_title = substring(v, 0, i);
324 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
327 MapInfo_Map_title = v;
334 o = stov(strcat("'", v, "'"));
335 mapMins.x = min(mapMins.x, o.x);
336 mapMins.y = min(mapMins.y, o.y);
337 mapMins.z = min(mapMins.z, o.z);
338 mapMaxs.x = max(mapMaxs.x, o.x);
339 mapMaxs.y = max(mapMaxs.y, o.y);
340 mapMaxs.z = max(mapMaxs.z, o.z);
342 else if(k == "race_place")
347 else if(k == "classname")
349 if(v == "info_player_team1")
351 else if(v == "info_player_team2")
353 else if(v == "info_player_start")
355 else if(v == "info_player_deathmatch")
357 else if(v == "weapon_nex")
359 else if(v == "weapon_railgun")
361 else if(startsWith(v, "weapon_"))
362 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
363 else if(startsWith(v, "turret_"))
364 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
365 else if(startsWith(v, "vehicle_"))
366 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
367 else if(startsWith(v, "monster_"))
368 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
369 else if(v == "target_music" || v == "trigger_music")
370 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
372 FOREACH(Gametypes, true, it.m_generate_mapinfo(it, v));
378 LOG_WARN(fn, " ended still in worldspawn, BUG");
381 diameter = vlen(mapMaxs - mapMins);
383 int twoBaseModes = 0;
384 FOREACH(Gametypes, it.m_isTwoBaseMode(), twoBaseModes |= it.m_flags);
385 if(twoBaseModes && (twoBaseModes &= MapInfo_Map_supportedGametypes))
387 // we have a symmetrical map, don't add the modes without bases
391 FOREACH(Gametypes, it.m_isAlwaysSupported(it, spawnpoints, diameter), MapInfo_Map_supportedGametypes |= it.m_flags);
394 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE.m_flags)
397 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE.m_flags;
398 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS.m_flags;
401 LOG_TRACE("-> diameter ", ftos(diameter));
402 LOG_TRACE("; spawnpoints ", ftos(spawnpoints));
403 LOG_TRACE("; modes ", ftos(MapInfo_Map_supportedGametypes));
410 void _MapInfo_Map_Reset()
412 MapInfo_Map_title = "<TITLE>";
413 MapInfo_Map_titlestring = "<TITLE>";
414 MapInfo_Map_description = "<DESCRIPTION>";
415 MapInfo_Map_author = "<AUTHOR>";
416 MapInfo_Map_supportedGametypes = 0;
417 MapInfo_Map_supportedFeatures = 0;
418 MapInfo_Map_flags = 0;
419 MapInfo_Map_clientstuff = "";
420 MapInfo_Map_fog = "";
421 MapInfo_Map_mins = '0 0 0';
422 MapInfo_Map_maxs = '0 0 0';
425 string _MapInfo_GetDefault(Gametype t)
429 case MAPINFO_TYPE_DEATHMATCH: return "30 20 0";
430 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
431 case MAPINFO_TYPE_DOMINATION: return "200 20 0";
432 case MAPINFO_TYPE_CTF: return "300 20 10 0";
433 case MAPINFO_TYPE_LMS: return "9 20 0";
434 case MAPINFO_TYPE_CA: return "10 20 0";
435 case MAPINFO_TYPE_KEYHUNT: return "1000 20 3 0";
436 case MAPINFO_TYPE_ASSAULT: return "20 0";
437 case MAPINFO_TYPE_RACE: return "20 5 7 15 0";
438 case MAPINFO_TYPE_ONSLAUGHT: return "20 0";
439 case MAPINFO_TYPE_NEXBALL: return "5 20 0";
440 case MAPINFO_TYPE_CTS: return "20 0 0";
441 case MAPINFO_TYPE_FREEZETAG: return "10 20 0";
442 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
443 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
444 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
445 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
450 void _MapInfo_Map_ApplyGametype(string s, Gametype pWantedType, Gametype pThisType, int load_default)
453 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
454 if(!(pThisType.m_flags & pWantedType.m_flags))
458 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, false);
460 if(!pWantedType.frags) // these modes don't use fraglimit
462 cvar_set("fraglimit", "0");
468 cvar_set("fraglimit", sa);
474 cvar_set("timelimit", sa);
477 if(pWantedType.m_setTeams)
481 pWantedType.m_setTeams(sa);
485 // rc = timelimit timelimit_qualification laps laps_teamplay
486 if(pWantedType == MAPINFO_TYPE_RACE)
488 cvar_set("fraglimit", "0"); // special case!
490 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
491 cvar_set("g_race_qualifying_timelimit", sa);
496 if(cvar("g_race_teams") < 2)
497 cvar_set("fraglimit", sa);
502 if(cvar("g_race_teams") >= 2)
503 cvar_set("fraglimit", sa);
507 if(!pWantedType.frags) // these modes don't use fraglimit
509 cvar_set("leadlimit", "0");
515 cvar_set("leadlimit", sa);
520 string _MapInfo_GetDefaultEx(Gametype t)
522 return t ? t.model2 : "";
525 float _MapInfo_GetTeamPlayBool(Gametype t)
527 return t ? t.team : false;
530 void _MapInfo_Map_ApplyGametypeEx(string s, Gametype pWantedType, Gametype pThisType)
532 MapInfo_Map_supportedGametypes |= pThisType.m_flags;
533 if (!(pThisType.m_flags & pWantedType.m_flags))
536 // reset all the cvars to their defaults
538 cvar_set("timelimit", cvar_defstring("timelimit"));
539 cvar_set("leadlimit", cvar_defstring("leadlimit"));
540 cvar_set("fraglimit", cvar_defstring("fraglimit"));
541 FOREACH(Gametypes, true, it.m_parse_mapinfo(string_null, string_null));
543 string fraglimit_normal = string_null;
544 string fraglimit_teams = string_null;
546 for (s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s); s != ""; s = cdr(s)) {
548 if (sa == "") continue;
549 int p = strstrofs(sa, "=", 0);
551 if(!autocvar_g_mapinfo_ignore_warnings)
552 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
555 string k = substring(sa, 0, p);
556 string v = substring(sa, p + 1, -1);
561 cvar_set("timelimit", v);
566 cvar_set("leadlimit", v);
575 fraglimit_normal = v;
578 case "teampointlimit":
590 FOREACH(Gametypes, true, handled |= it.m_parse_mapinfo(k, v));
591 if (!handled && !autocvar_g_mapinfo_ignore_warnings)
592 LOG_WARNF("Invalid gametype setting in mapinfo for gametype %s: %s", MapInfo_Type_ToString(pWantedType), sa);
595 if (pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
598 cvar_set("fraglimit", fraglimit_teams);
603 cvar_set("fraglimit", fraglimit_normal);
607 Gametype MapInfo_Type_FromString(string t)
609 #define deprecate(from, to) MACRO_BEGIN { \
611 string replacement = #to; \
612 if(!autocvar_g_mapinfo_ignore_warnings) \
613 LOG_WARNF("MapInfo_Type_FromString (probably %s): using deprecated name '%s'. Should use '%s'.", MapInfo_Map_bspname, t, replacement); \
617 deprecate(nexball, nb);
618 deprecate(freezetag, ft);
619 deprecate(keepaway, ka);
620 deprecate(invasion, inv);
621 deprecate(assault, as);
623 FOREACH(Gametypes, it.mdl == t, return it);
628 string MapInfo_Type_Description(Gametype t)
630 return t ? t.gametype_description : "";
633 string MapInfo_Type_ToString(Gametype t)
635 return t ? t.mdl : "";
638 string MapInfo_Type_ToText(Gametype t)
640 /* xgettext:no-c-format */
641 return t ? t.message : _("@!#%'n Tuba Throwing");
644 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
648 t = car(s); s = cdr(s);
650 // limited support of "" and comments
651 // remove trailing and leading " of t
652 if(substring(t, 0, 1) == "\"")
654 if(substring(t, -1, 1) == "\"")
655 t = substring(t, 1, -2);
658 // remove leading " of s
659 if(substring(s, 0, 1) == "\"")
661 s = substring(s, 1, -1);
663 // remove trailing " of s, and all that follows (cvar description)
664 o = strstrofs(s, "\"", 0);
666 s = substring(s, 0, o);
668 // remove // comments
669 o = strstrofs(s, "//", 0);
671 s = substring(s, 0, o);
673 // remove trailing spaces
674 while(substring(s, -1, 1) == " ")
675 s = substring(s, 0, -2);
681 fh = fopen(s, FILE_READ);
683 LOG_WARN("Map ", pFilename, " references not existing config file ", s);
688 if (!((s = fgets(fh))))
691 // catch different sorts of comments
692 if(s == "") // empty lines
694 if(substring(s, 0, 1) == "#") // UNIX style
696 if(substring(s, 0, 2) == "//") // C++ style
698 if(substring(s, 0, 1) == "_") // q3map style
701 if(substring(s, 0, 4) == "set ")
702 s = substring(s, 4, -1);
703 if(substring(s, 0, 5) == "seta ")
704 s = substring(s, 5, -1);
706 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
712 LOG_WARN("Map ", pFilename, " uses too many levels of inclusion");
715 LOG_WARN("Map ", pFilename, " contains a potentially harmful setting, ignored");
716 else if (!cvar_value_issafe(t))
717 LOG_WARN("Map ", pFilename, " contains a potentially harmful setting, ignored");
718 else if (!cvar_value_issafe(s))
719 LOG_WARN("Map ", pFilename, " contains a potentially harmful setting, ignored");
720 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
721 LOG_WARN("Map ", pFilename, " contains a potentially harmful setting, ignored");
722 else if(matchacl(acl, t) <= 0)
723 LOG_WARN("Map ", pFilename, " contains a denied setting, ignored");
726 if(type == 0) // server set
728 LOG_TRACE("Applying temporary setting ", t, " := ", s);
729 if(cvar("g_campaign"))
730 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
736 LOG_TRACE("Applying temporary client setting ", t, " := ", s);
737 MapInfo_Map_clientstuff = strcat(
738 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
744 float MapInfo_isRedundant(string fn, string t)
746 // normalize file name
747 fn = strreplace("_", "-", fn);
749 // normalize visible title
750 t = strreplace(": ", "-", t);
751 t = strreplace(":", "-", t);
752 t = strreplace(" ", "-", t);
753 t = strreplace("_", "-", t);
754 t = strreplace("'", "-", t);
756 if(!strcasecmp(fn, t))
759 // we allow the visible title to have punctuation the file name does
760 // not, but not vice versa
761 t = strreplace("-", "", t);
763 if(!strcasecmp(fn, t))
769 // load info about a map by name into the MapInfo_Map_* globals
770 float MapInfo_Get_ByName_NoFallbacks(string pFilename, int pAllowGenerate, Gametype pGametypeToSet)
779 acl = MAPINFO_SETTEMP_ACL_USER;
781 if(strstrofs(pFilename, "/", 0) >= 0)
783 LOG_WARN("Invalid character in map name, ignored");
787 if(pGametypeToSet == NULL)
788 if(MapInfo_Cache_Retrieve(pFilename))
793 MapInfo_Map_bspname = pFilename;
795 // default all generic fields so they have "good" values in case something fails
796 fn = strcat("maps/", pFilename, ".mapinfo");
797 fh = fopen(fn, FILE_READ);
800 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
801 fh = fopen(fn, FILE_READ);
806 _MapInfo_Map_Reset();
807 r = _MapInfo_Generate(pFilename);
810 fh = fopen(fn, FILE_WRITE);
811 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
812 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
813 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
814 if(_MapInfo_Map_worldspawn_music != "")
817 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
819 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
821 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
823 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
827 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
828 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
831 i = floor(random() * n);
832 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
835 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
837 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
838 fputs(fh, "has weapons\n");
840 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
841 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
842 fputs(fh, "has turrets\n");
844 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
845 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
846 fputs(fh, "has vehicles\n");
848 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
849 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
850 fputs(fh, "frustrating\n");
852 FOREACH(Gametypes, MapInfo_Map_supportedGametypes & it.m_flags, {
853 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(it), _MapInfo_GetDefaultEx(it)));
856 if(fexists(strcat("scripts/", pFilename, ".arena")))
857 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
859 if(fexists(strcat("scripts/", pFilename, ".defi")))
860 fputs(fh, "settemp_for_type all sv_vq3compat 1\n");
862 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
863 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
864 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
865 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
866 fputs(fh, "// optional: hidden\n");
871 fh = fopen(fn, FILE_READ);
873 error("... but I just wrote it!");
876 if(!autocvar_g_mapinfo_ignore_warnings)
877 LOG_WARN("autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo");
880 _MapInfo_Map_Reset();
883 if (!((s = fgets(fh))))
886 // catch different sorts of comments
887 if(s == "") // empty lines
889 if(substring(s, 0, 1) == "#") // UNIX style
891 if(substring(s, 0, 2) == "//") // C++ style
893 if(substring(s, 0, 1) == "_") // q3map style
896 p = strstrofs(s, "//", 0);
898 s = substring(s, 0, p);
900 t = car(s); s = cdr(s);
902 MapInfo_Map_title = s;
903 else if(t == "description")
904 MapInfo_Map_description = s;
905 else if(t == "author")
906 MapInfo_Map_author = s;
909 t = car(s); // s = cdr(s);
910 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
911 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
912 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
913 else if(t == "monsters") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
914 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
916 LOG_WARN("Map ", pFilename, " supports unknown feature ", t, ", ignored");
918 else if(t == "hidden")
920 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
922 else if(t == "forbidden")
924 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
926 else if(t == "frustrating")
928 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
930 else if(t == "noautomaplist")
932 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
934 else if(t == "gameversion_min")
936 if (cvar("gameversion") < stof(s))
937 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
941 t = car(s); s = cdr(s);
942 Gametype f = MapInfo_Type_FromString(t);
943 //if(!autocvar_g_mapinfo_ignore_warnings)
944 //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'.");
946 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, true);
947 else if(!autocvar_g_mapinfo_ignore_warnings)
948 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
950 else if(t == "gametype")
952 t = car(s); s = cdr(s);
953 Gametype f = MapInfo_Type_FromString(t);
955 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
956 else if(!autocvar_g_mapinfo_ignore_warnings)
957 LOG_DEBUG("Map ", pFilename, " supports unknown game type ", t, ", ignored");
962 t = car(s); s = cdr(s); a = stof(t);
963 t = car(s); s = cdr(s); b = stof(t);
964 t = car(s); s = cdr(s); c = stof(t);
965 t = car(s); s = cdr(s); d = stof(t);
966 t = car(s); s = cdr(s); e = stof(t);
968 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");
971 t = car(s); s = cdr(s); f = stof(t);
973 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");
976 if(a >= d || b >= e || c >= f)
977 LOG_WARN("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs");
980 MapInfo_Map_mins.x = a;
981 MapInfo_Map_mins.y = b;
982 MapInfo_Map_mins.z = c;
983 MapInfo_Map_maxs.x = d;
984 MapInfo_Map_maxs.y = e;
985 MapInfo_Map_maxs.z = f;
990 else if(t == "settemp_for_type")
992 t = car(s); s = cdr(s);
993 bool all = t == "all";
995 if(all || (f = MapInfo_Type_FromString(t)))
997 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
999 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1004 LOG_DEBUG("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored");
1007 else if(t == "clientsettemp_for_type")
1009 t = car(s); s = cdr(s);
1010 bool all = t == "all";
1012 if(all || (f = MapInfo_Type_FromString(t)))
1014 if((all ? MAPINFO_TYPE_ALL : f.m_flags) & pGametypeToSet.m_flags)
1016 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1021 LOG_DEBUG("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored");
1026 if (!cvar_value_issafe(s))
1027 LOG_WARN("Map ", pFilename, " contains a potentially harmful fog setting, ignored");
1029 MapInfo_Map_fog = s;
1031 else if(t == "cdtrack")
1033 t = car(s); s = cdr(s);
1034 // We do this only if pGametypeToSet even though this
1035 // content is theoretically game type independent,
1036 // because MapInfo_Map_clientstuff contains otherwise
1037 // game type dependent stuff. That way this value stays
1038 // empty when not setting a game type to not set any
1039 // false expectations.
1042 if (!cvar_value_issafe(t))
1043 LOG_WARN("Map ", pFilename, " contains a potentially harmful cdtrack, ignored");
1045 MapInfo_Map_clientstuff = strcat(
1046 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1050 else if(!autocvar_g_mapinfo_ignore_warnings)
1051 LOG_WARN("Map ", pFilename, " provides unknown info item ", t, ", ignored");
1055 if(MapInfo_Map_title == "<TITLE>")
1056 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1057 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1058 MapInfo_Map_titlestring = MapInfo_Map_title;
1060 MapInfo_Map_titlestring = sprintf("%s: %s", MapInfo_Map_bspname, MapInfo_Map_title);
1062 MapInfo_Cache_Store();
1063 if(MapInfo_Map_supportedGametypes != 0)
1065 LOG_WARN("Map ", pFilename, " supports no game types, ignored");
1068 int MapInfo_Get_ByName(string pFilename, float pAllowGenerate, Gametype pGametypeToSet)
1070 int r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1072 FOREACH(Gametypes, it.m_isForcedSupported(it), _MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, it));
1076 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet.m_flags))
1078 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1079 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1087 float MapInfo_FindName(string s)
1089 // if there is exactly one map of prefix s, return it
1090 // if not, return the null string
1091 // note that DP sorts glob results... so I can use a binary search
1095 // invariants: r is behind s, l-1 is equal or before
1098 m = floor((l + r) / 2);
1099 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1100 cmp = strcasecmp(MapInfo_FindName_match, s);
1102 return m; // found and good
1104 l = m + 1; // l-1 is before s
1108 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1109 MapInfo_FindName_firstResult = l;
1110 // r == l, so: l is behind s, l-1 is before
1111 // SO: if there is any, l is the one with the right prefix
1112 // and l+1 may be one too
1113 if(l == MapInfo_count)
1115 MapInfo_FindName_match = string_null;
1116 MapInfo_FindName_firstResult = -1;
1117 return -1; // no MapInfo_FindName_match, behind last item
1119 if(!startsWithNocase(MapInfo_FindName_match, s))
1121 MapInfo_FindName_match = string_null;
1122 MapInfo_FindName_firstResult = -1;
1123 return -1; // wrong prefix
1125 if(l == MapInfo_count - 1)
1126 return l; // last one, nothing can follow => unique
1127 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1129 MapInfo_FindName_match = string_null;
1130 return -1; // ambigous MapInfo_FindName_match
1135 string MapInfo_FixName(string s)
1137 MapInfo_FindName(s);
1138 return MapInfo_FindName_match;
1141 int MapInfo_CurrentFeatures()
1144 if(!(cvar("g_lms") || cvar("g_instagib") || cvar("g_overkill") || cvar("g_nix") || cvar("g_weaponarena") || !cvar("g_pickup_items") || cvar("g_race") || cvar("g_cts") || cvar("g_nexball")))
1145 req |= MAPINFO_FEATURE_WEAPONS;
1149 Gametype MapInfo_CurrentGametype()
1151 Gametype prev = Gametypes_from(cvar("gamecfg"));
1152 FOREACH(Gametypes, cvar(it.netname) && it != prev, return it);
1153 return prev ? prev : MAPINFO_TYPE_DEATHMATCH;
1156 float _MapInfo_CheckMap(string s, bool gametype_only) // returns 0 if the map can't be played with the current settings, 1 otherwise
1158 if(!MapInfo_Get_ByName(s, 1, NULL))
1160 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype().m_flags) == 0)
1164 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1169 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1172 r = _MapInfo_CheckMap(s, false);
1173 MapInfo_ClearTemps();
1177 void MapInfo_SwitchGameType(Gametype t)
1179 FOREACH(Gametypes, true, cvar_set(it.netname, (it == t) ? "1" : "0"));
1182 void MapInfo_LoadMap(string s, float reinit)
1184 MapInfo_Map_supportedGametypes = 0;
1185 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1186 //if(!MapInfo_CheckMap(s))
1188 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1189 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH.m_flags);
1192 LOG_INFO("Switching to map ", s);
1194 cvar_settemp_restore();
1196 localcmd(strcat("\nmap ", s, "\n"));
1198 localcmd(strcat("\nchangelevel ", s, "\n"));
1201 string MapInfo_ListAllowedMaps(Gametype type, float pRequiredFlags, float pForbiddenFlags)
1205 // to make absolutely sure:
1206 MapInfo_Enumerate();
1207 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1210 for(float i = 0; i < MapInfo_count; ++i)
1211 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1212 return substring(out, 1, strlen(out) - 1);
1215 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1219 // to make absolutely sure:
1220 MapInfo_Enumerate();
1221 _MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1224 for(float i = 0; i < MapInfo_count; ++i)
1225 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1227 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1229 return substring(out, 1, strlen(out) - 1);
1232 void MapInfo_LoadMapSettings_SaveGameType(Gametype t)
1234 MapInfo_SwitchGameType(t);
1235 cvar_set("gamecfg", ftos(t.m_id));
1236 MapInfo_LoadedGametype = t;
1239 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1241 Gametype t = MapInfo_CurrentGametype();
1242 MapInfo_LoadMapSettings_SaveGameType(t);
1244 if(!_MapInfo_CheckMap(s, true)) // with underscore, it keeps temps
1246 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1248 LOG_SEVERE("can't play the selected map in the given game mode. Working with only the override settings.");
1249 _MapInfo_Map_ApplyGametypeEx("", t, t);
1250 return; // do not call Get_ByName!
1253 if(MapInfo_Map_supportedGametypes == 0)
1255 LOG_SEVERE("Mapinfo system is not functional at all. Assuming deathmatch.");
1256 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH.m_flags;
1257 MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1258 _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1259 return; // do not call Get_ByName!
1263 while(!(MapInfo_Map_supportedGametypes & 1))
1266 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes >> 1);
1268 Gametype t_prev = t;
1269 FOREACH(Gametypes, it.m_flags == _t, { t = it; break; });
1271 // t is now a supported mode!
1272 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);
1273 MapInfo_LoadMapSettings_SaveGameType(t);
1275 if(!_MapInfo_CheckMap(s, false)) { // with underscore, it keeps temps
1276 LOG_WARNF("the selected map lacks features required by current settings; playing anyway.");
1278 MapInfo_Get_ByName(s, 1, t);
1281 void MapInfo_ClearTemps()
1283 MapInfo_Map_bspname = string_null;
1284 MapInfo_Map_title = string_null;
1285 MapInfo_Map_titlestring = string_null;
1286 MapInfo_Map_description = string_null;
1287 MapInfo_Map_author = string_null;
1288 MapInfo_Map_clientstuff = string_null;
1289 MapInfo_Map_supportedGametypes = 0;
1290 MapInfo_Map_supportedFeatures = 0;
1293 void MapInfo_Shutdown()
1295 MapInfo_ClearTemps();
1296 MapInfo_Filter_Free();
1297 MapInfo_Cache_Destroy();
1298 if(_MapInfo_globopen)
1300 search_end(_MapInfo_globhandle);
1301 _MapInfo_globhandle = -1;
1302 _MapInfo_globopen = false;
1306 int MapInfo_ForbiddenFlags()
1308 int f = MAPINFO_FLAG_FORBIDDEN;
1311 if (!cvar("g_maplist_allow_hidden"))
1313 f |= MAPINFO_FLAG_HIDDEN;
1315 if (!cvar("g_maplist_allow_frustrating"))
1316 f |= MAPINFO_FLAG_FRUSTRATING;
1321 int MapInfo_RequiredFlags()
1325 if(cvar("g_maplist_allow_frustrating") > 1)
1326 f |= MAPINFO_FLAG_FRUSTRATING;