2 #include "../client/defs.qh"
4 #include <common/weapons/all.qh>
9 #include <common/monsters/all.qh>
13 bool autocvar_developer_mapper;
15 #define LOG_MAPWARN(...) MACRO_BEGIN { if (autocvar_developer_mapper) LOG_WARNING(__VA_ARGS__); } MACRO_END
16 #define LOG_MAPWARNF(...) MACRO_BEGIN { if (autocvar_developer_mapper) LOG_WARNINGF(__VA_ARGS__); } MACRO_END
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(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, 0) == 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.\n");
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, world);
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, world);
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)
237 for(i = 0; i < l; ++i)
240 ch = substring(s, i, 1);
241 if(ch != " ") if(ch != "\"")
243 for(j = strlen(s) - i - 1; j > 0; --j)
245 ch = substring(s, i+j, 1);
246 if(ch != " ") if(ch != "\"")
247 return substring(s, i, j+1);
249 return substring(s, i, 1);
255 float MapInfo_Get_ByID(float i)
257 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
262 string _MapInfo_Map_worldspawn_music;
264 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
274 float diameter, spawnpoints;
277 vector mapMins, mapMaxs;
280 fn = strcat("maps/", pFilename, ".ent");
281 fh = fopen(fn, FILE_READ);
285 fn = strcat("maps/", pFilename, ".bsp");
286 fh = fopen(fn, FILE_READ);
290 LOG_INFO("Analyzing ", fn, " to generate initial mapinfo\n");
293 MapInfo_Map_flags = 0;
294 MapInfo_Map_supportedGametypes = 0;
297 _MapInfo_Map_worldspawn_music = "";
303 if (!((s = fgets(fh))))
305 if(inWorldspawn == 1)
306 if(startsWith(s, "}"))
312 if(k == "classname" && v == "worldspawn")
314 else if(k == "author")
315 MapInfo_Map_author = v;
316 else if(k == "_description")
317 MapInfo_Map_description = v;
318 else if(k == "music")
319 _MapInfo_Map_worldspawn_music = v;
320 else if(k == "noise")
321 _MapInfo_Map_worldspawn_music = v;
322 else if(k == "message")
324 i = strstrofs(v, " by ", 0);
325 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
327 MapInfo_Map_title = substring(v, 0, i);
328 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
331 MapInfo_Map_title = v;
338 o = stov(strcat("'", v, "'"));
339 mapMins.x = min(mapMins.x, o.x);
340 mapMins.y = min(mapMins.y, o.y);
341 mapMins.z = min(mapMins.z, o.z);
342 mapMaxs.x = max(mapMaxs.x, o.x);
343 mapMaxs.y = max(mapMaxs.y, o.y);
344 mapMaxs.z = max(mapMaxs.z, o.z);
346 else if(k == "race_place")
351 else if(k == "classname")
353 if(v == "dom_controlpoint")
354 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
355 else if(v == "item_flag_team2")
356 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
357 else if(v == "team_CTF_blueflag")
358 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
359 else if(v == "invasion_spawnpoint")
360 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_INVASION;
361 else if(v == "target_assault_roundend")
362 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
363 else if(v == "onslaught_generator")
364 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
365 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
366 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
367 else if(v == "info_player_team1")
369 else if(v == "info_player_team2")
371 else if(v == "info_player_start")
373 else if(v == "info_player_deathmatch")
375 else if(v == "trigger_race_checkpoint")
376 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
377 else if(v == "target_startTimer")
378 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
379 else if(v == "weapon_nex")
381 else if(v == "weapon_railgun")
383 else if(startsWith(v, "weapon_"))
384 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
385 else if(startsWith(v, "turret_"))
386 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
387 else if(startsWith(v, "vehicle_"))
388 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
389 else if(startsWith(v, "monster_"))
390 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
391 else if(v == "target_music" || v == "trigger_music")
392 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
398 LOG_MAPWARN(fn, " ended still in worldspawn, BUG\n");
401 diameter = vlen(mapMaxs - mapMins);
403 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
404 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
406 // we have a CTF-only or Assault-only map. Don't add other modes then,
407 // as the map is too symmetric for them.
411 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
412 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
413 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY; // Keepaway always works
415 if(spawnpoints >= 8 && diameter > 4096) {
416 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
417 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
418 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
420 if(spawnpoints >= 12 && diameter > 5120)
421 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
424 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
427 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE;
428 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
431 LOG_TRACE("-> diameter ", ftos(diameter));
432 LOG_TRACE("; spawnpoints ", ftos(spawnpoints));
433 LOG_TRACE("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
440 void _MapInfo_Map_Reset()
442 MapInfo_Map_title = "<TITLE>";
443 MapInfo_Map_titlestring = "<TITLE>";
444 MapInfo_Map_description = "<DESCRIPTION>";
445 MapInfo_Map_author = "<AUTHOR>";
446 MapInfo_Map_supportedGametypes = 0;
447 MapInfo_Map_supportedFeatures = 0;
448 MapInfo_Map_flags = 0;
449 MapInfo_Map_clientstuff = "";
450 MapInfo_Map_fog = "";
451 MapInfo_Map_mins = '0 0 0';
452 MapInfo_Map_maxs = '0 0 0';
455 string _MapInfo_GetDefault(float t)
459 case MAPINFO_TYPE_DEATHMATCH: return "30 20 0";
460 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
461 case MAPINFO_TYPE_DOMINATION: return "200 20 0";
462 case MAPINFO_TYPE_CTF: return "300 20 10 0";
463 case MAPINFO_TYPE_LMS: return "9 20 0";
464 case MAPINFO_TYPE_CA: return "10 20 0";
465 case MAPINFO_TYPE_KEYHUNT: return "1000 20 3 0";
466 case MAPINFO_TYPE_ASSAULT: return "20 0";
467 case MAPINFO_TYPE_RACE: return "20 5 7 15 0";
468 case MAPINFO_TYPE_ONSLAUGHT: return "20 0";
469 case MAPINFO_TYPE_NEXBALL: return "5 20 0";
470 case MAPINFO_TYPE_CTS: return "20 0 0";
471 case MAPINFO_TYPE_FREEZETAG: return "10 20 0";
472 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
473 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
474 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
475 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
480 void _MapInfo_Map_ApplyGametype(string s, int pWantedType, int pThisType, int load_default)
483 MapInfo_Map_supportedGametypes |= pThisType;
484 if(!(pThisType & pWantedType))
488 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, false);
490 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
492 cvar_set("fraglimit", "0");
498 cvar_set("fraglimit", sa);
504 cvar_set("timelimit", sa);
507 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
511 cvar_set("g_tdm_teams", sa);
515 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
519 cvar_set("g_keyhunt_teams", sa);
523 if(pWantedType == MAPINFO_TYPE_CA)
527 cvar_set("g_ca_teams", sa);
531 if(pWantedType == MAPINFO_TYPE_FREEZETAG)
535 cvar_set("g_freezetag_teams", sa);
539 if(pWantedType == MAPINFO_TYPE_CTF)
543 cvar_set("fraglimit", sa);
548 if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
552 cvar_set("fraglimit", sa);
557 // rc = timelimit timelimit_qualification laps laps_teamplay
558 if(pWantedType == MAPINFO_TYPE_RACE)
560 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
561 cvar_set("g_race_qualifying_timelimit", sa);
566 if(cvar("g_race_teams") < 2)
567 cvar_set("fraglimit", sa);
572 if(cvar("g_race_teams") >= 2)
573 cvar_set("fraglimit", sa);
577 if(pWantedType == MAPINFO_TYPE_CTS)
581 // this is the skill of the map
582 // not parsed by anything yet
585 // cvar_set("fraglimit", sa);
590 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
592 cvar_set("leadlimit", "0");
598 cvar_set("leadlimit", sa);
603 string _MapInfo_GetDefaultEx(float t)
605 FOREACH(Gametypes, it.items == t, LAMBDA(return it.model2));
609 float _MapInfo_GetTeamPlayBool(float t)
611 FOREACH(Gametypes, it.items == t, LAMBDA(return it.team));
615 void _MapInfo_Map_ApplyGametypeEx(string s, int pWantedType, int pThisType)
617 MapInfo_Map_supportedGametypes |= pThisType;
618 if (!(pThisType & pWantedType))
621 // reset all the cvars to their defaults
623 cvar_set("timelimit", cvar_defstring("timelimit"));
624 cvar_set("leadlimit", cvar_defstring("leadlimit"));
625 cvar_set("fraglimit", cvar_defstring("fraglimit"));
626 FOREACH(Gametypes, true, LAMBDA(it.m_parse_mapinfo(string_null, string_null)));
628 string fraglimit_normal = string_null;
629 string fraglimit_teams = string_null;
631 for (s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s); s != ""; s = cdr(s)) {
633 if (sa == "") continue;
634 int p = strstrofs(sa, "=", 0);
636 LOG_MAPWARNF("Invalid gametype setting in mapinfo for gametype %s: %s\n", MapInfo_Type_ToString(pWantedType), sa);
639 string k = substring(sa, 0, p);
640 string v = substring(sa, p + 1, -1);
645 cvar_set("timelimit", v);
650 cvar_set("leadlimit", v);
659 fraglimit_normal = v;
662 case "teampointlimit":
674 FOREACH(Gametypes, true, LAMBDA(handled |= it.m_parse_mapinfo(k, v)));
676 LOG_MAPWARNF("Invalid gametype setting in mapinfo for gametype %s: %s\n", MapInfo_Type_ToString(pWantedType), sa);
679 if (pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
682 cvar_set("fraglimit", fraglimit_teams);
687 cvar_set("fraglimit", fraglimit_normal);
691 Gametype MapInfo_Type(int t)
693 FOREACH(Gametypes, it.items == t, LAMBDA(return it));
697 int MapInfo_Type_FromString(string t)
699 #define deprecate(from, to) MACRO_BEGIN { \
701 string replacement = #to; \
702 LOG_MAPWARNF("MapInfo_Type_FromString (probably %s): using deprecated name '%s'. Should use '%s'.\n", MapInfo_Map_bspname, t, replacement); \
706 deprecate(nexball, nb);
707 deprecate(freezetag, ft);
708 deprecate(keepaway, ka);
709 deprecate(invasion, inv);
710 deprecate(assault, as);
712 if (t == "all") return MAPINFO_TYPE_ALL;
713 FOREACH(Gametypes, it.mdl == t, LAMBDA(return it.items));
718 string MapInfo_Type_Description(float t)
720 FOREACH(Gametypes, it.items == t, LAMBDA(return it.gametype_description));
724 string MapInfo_Type_ToString(float t)
726 if(t == MAPINFO_TYPE_ALL)
728 FOREACH(Gametypes, it.items == t, LAMBDA(return it.mdl));
732 string MapInfo_Type_ToText(float t)
734 FOREACH(Gametypes, it.items == t, LAMBDA(return it.message));
735 /* xgettext:no-c-format */
736 return _("@!#%'n Tuba Throwing");
739 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
743 t = car(s); s = cdr(s);
745 // limited support of "" and comments
746 // remove trailing and leading " of t
747 if(substring(t, 0, 1) == "\"")
749 if(substring(t, -1, 1) == "\"")
750 t = substring(t, 1, -2);
753 // remove leading " of s
754 if(substring(s, 0, 1) == "\"")
756 s = substring(s, 1, -1);
758 // remove trailing " of s, and all that follows (cvar description)
759 o = strstrofs(s, "\"", 0);
761 s = substring(s, 0, o);
763 // remove // comments
764 o = strstrofs(s, "//", 0);
766 s = substring(s, 0, o);
768 // remove trailing spaces
769 while(substring(s, -1, 1) == " ")
770 s = substring(s, 0, -2);
776 fh = fopen(s, FILE_READ);
778 LOG_MAPWARN("Map ", pFilename, " references not existing config file ", s, "\n");
783 if (!((s = fgets(fh))))
786 // catch different sorts of comments
787 if(s == "") // empty lines
789 if(substring(s, 0, 1) == "#") // UNIX style
791 if(substring(s, 0, 2) == "//") // C++ style
793 if(substring(s, 0, 1) == "_") // q3map style
796 if(substring(s, 0, 4) == "set ")
797 s = substring(s, 4, -1);
798 if(substring(s, 0, 5) == "seta ")
799 s = substring(s, 5, -1);
801 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
807 LOG_MAPWARN("Map ", pFilename, " uses too many levels of inclusion\n");
810 LOG_MAPWARN("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
811 else if (!cvar_value_issafe(t))
812 LOG_MAPWARN("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
813 else if (!cvar_value_issafe(s))
814 LOG_MAPWARN("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
815 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
816 LOG_MAPWARN("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
817 else if(matchacl(acl, t) <= 0)
818 LOG_MAPWARN("Map ", pFilename, " contains a denied setting, ignored\n");
821 if(type == 0) // server set
823 LOG_TRACE("Applying temporary setting ", t, " := ", s, "\n");
824 if(cvar("g_campaign"))
825 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
831 LOG_TRACE("Applying temporary client setting ", t, " := ", s, "\n");
832 MapInfo_Map_clientstuff = strcat(
833 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
839 float MapInfo_isRedundant(string fn, string t)
841 // normalize file name
842 fn = strreplace("_", "-", fn);
844 // normalize visible title
845 t = strreplace(": ", "-", t);
846 t = strreplace(":", "-", t);
847 t = strreplace(" ", "-", t);
848 t = strreplace("_", "-", t);
849 t = strreplace("'", "-", t);
851 if(!strcasecmp(fn, t))
854 // we allow the visible title to have punctuation the file name does
855 // not, but not vice versa
856 t = strreplace("-", "", t);
858 if(!strcasecmp(fn, t))
864 // load info about a map by name into the MapInfo_Map_* globals
865 float MapInfo_Get_ByName_NoFallbacks(string pFilename, int pAllowGenerate, int pGametypeToSet)
874 acl = MAPINFO_SETTEMP_ACL_USER;
876 if(strstrofs(pFilename, "/", 0) >= 0)
878 LOG_MAPWARN("Invalid character in map name, ignored\n");
882 if(pGametypeToSet == 0)
883 if(MapInfo_Cache_Retrieve(pFilename))
888 MapInfo_Map_bspname = pFilename;
890 // default all generic fields so they have "good" values in case something fails
891 fn = strcat("maps/", pFilename, ".mapinfo");
892 fh = fopen(fn, FILE_READ);
895 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
896 fh = fopen(fn, FILE_READ);
901 _MapInfo_Map_Reset();
902 r = _MapInfo_Generate(pFilename);
905 fh = fopen(fn, FILE_WRITE);
906 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
907 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
908 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
909 if(_MapInfo_Map_worldspawn_music != "")
912 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
914 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
916 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
918 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
922 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
923 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
926 i = floor(random() * n);
927 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
930 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
932 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
933 fputs(fh, "has weapons\n");
935 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
936 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
937 fputs(fh, "has turrets\n");
939 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
940 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
941 fputs(fh, "has vehicles\n");
943 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
944 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
945 fputs(fh, "frustrating\n");
947 for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
948 if(MapInfo_Map_supportedGametypes & i)
949 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
951 if(fexists(strcat("scripts/", pFilename, ".arena")))
952 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
954 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
955 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
956 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
957 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
958 fputs(fh, "// optional: hidden\n");
963 fh = fopen(fn, FILE_READ);
965 error("... but I just wrote it!");
968 LOG_MAPWARN("autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
971 _MapInfo_Map_Reset();
974 if (!((s = fgets(fh))))
977 // catch different sorts of comments
978 if(s == "") // empty lines
980 if(substring(s, 0, 1) == "#") // UNIX style
982 if(substring(s, 0, 2) == "//") // C++ style
984 if(substring(s, 0, 1) == "_") // q3map style
987 p = strstrofs(s, "//", 0);
989 s = substring(s, 0, p);
991 t = car(s); s = cdr(s);
993 MapInfo_Map_title = s;
994 else if(t == "description")
995 MapInfo_Map_description = s;
996 else if(t == "author")
997 MapInfo_Map_author = s;
1000 t = car(s); // s = cdr(s);
1001 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1002 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
1003 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
1004 else if(t == "monsters") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
1005 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1007 LOG_MAPWARN("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
1009 else if(t == "hidden")
1011 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
1013 else if(t == "forbidden")
1015 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
1017 else if(t == "frustrating")
1019 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
1021 else if(t == "noautomaplist")
1023 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1025 else if(t == "gameversion_min")
1027 if (cvar("gameversion") < stof(s))
1028 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1030 else if(t == "type")
1032 t = car(s); s = cdr(s);
1033 f = MapInfo_Type_FromString(t);
1034 LOG_MAPWARN("Map ", pFilename, " contains the legacy 'type' keyword which is deprecated and will be removed in the future. Please migrate the mapinfo file to 'gametype'.\n");
1036 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, true);
1038 LOG_MAPWARN("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1040 else if(t == "gametype")
1042 t = car(s); s = cdr(s);
1043 f = MapInfo_Type_FromString(t);
1045 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
1047 LOG_MAPWARN("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1049 else if(t == "size")
1051 float a, b, c, d, e;
1052 t = car(s); s = cdr(s); a = stof(t);
1053 t = car(s); s = cdr(s); b = stof(t);
1054 t = car(s); s = cdr(s); c = stof(t);
1055 t = car(s); s = cdr(s); d = stof(t);
1056 t = car(s); s = cdr(s); e = stof(t);
1058 LOG_MAPWARN("Map ", pFilename, " contains an incorrect size line (not enough params), syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
1061 t = car(s); s = cdr(s); f = stof(t);
1063 LOG_MAPWARN("Map ", pFilename, " contains an incorrect size line (too many params), syntax: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
1066 if(a >= d || b >= e || c >= f)
1067 LOG_MAPWARN("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
1070 MapInfo_Map_mins.x = a;
1071 MapInfo_Map_mins.y = b;
1072 MapInfo_Map_mins.z = c;
1073 MapInfo_Map_maxs.x = d;
1074 MapInfo_Map_maxs.y = e;
1075 MapInfo_Map_maxs.z = f;
1080 else if(t == "settemp_for_type")
1082 t = car(s); s = cdr(s);
1083 if((f = MapInfo_Type_FromString(t)))
1085 if(f & pGametypeToSet)
1087 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1092 LOG_MAPWARN("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1095 else if(t == "clientsettemp_for_type")
1097 t = car(s); s = cdr(s);
1098 if((f = MapInfo_Type_FromString(t)))
1100 if(f & pGametypeToSet)
1102 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1107 LOG_MAPWARN("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1112 if (!cvar_value_issafe(s))
1113 LOG_MAPWARN("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1115 MapInfo_Map_fog = s;
1117 else if(t == "cdtrack")
1119 t = car(s); s = cdr(s);
1120 // We do this only if pGametypeToSet even though this
1121 // content is theoretically game type independent,
1122 // because MapInfo_Map_clientstuff contains otherwise
1123 // game type dependent stuff. That way this value stays
1124 // empty when not setting a game type to not set any
1125 // false expectations.
1128 if (!cvar_value_issafe(t))
1129 LOG_MAPWARN("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1131 MapInfo_Map_clientstuff = strcat(
1132 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1137 LOG_MAPWARN("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1141 if(MapInfo_Map_title == "<TITLE>")
1142 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1143 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1144 MapInfo_Map_titlestring = MapInfo_Map_title;
1146 MapInfo_Map_titlestring = sprintf("%s: %s", MapInfo_Map_bspname, MapInfo_Map_title);
1148 MapInfo_Cache_Store();
1149 if(MapInfo_Map_supportedGametypes != 0)
1151 LOG_MAPWARN("Map ", pFilename, " supports no game types, ignored\n");
1154 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, int pGametypeToSet)
1156 float r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1158 if(cvar("g_tdm_on_dm_maps"))
1160 // if this is set, all DM maps support TDM too
1161 if (!(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH))
1162 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)
1163 _MapInfo_Map_ApplyGametypeEx ("", pGametypeToSet, MAPINFO_TYPE_TEAM_DEATHMATCH);
1168 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1170 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1171 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1179 float MapInfo_FindName(string s)
1181 // if there is exactly one map of prefix s, return it
1182 // if not, return the null string
1183 // note that DP sorts glob results... so I can use a binary search
1187 // invariants: r is behind s, l-1 is equal or before
1190 m = floor((l + r) / 2);
1191 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1192 cmp = strcasecmp(MapInfo_FindName_match, s);
1194 return m; // found and good
1196 l = m + 1; // l-1 is before s
1200 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1201 MapInfo_FindName_firstResult = l;
1202 // r == l, so: l is behind s, l-1 is before
1203 // SO: if there is any, l is the one with the right prefix
1204 // and l+1 may be one too
1205 if(l == MapInfo_count)
1207 MapInfo_FindName_match = string_null;
1208 MapInfo_FindName_firstResult = -1;
1209 return -1; // no MapInfo_FindName_match, behind last item
1211 if(!startsWithNocase(MapInfo_FindName_match, s))
1213 MapInfo_FindName_match = string_null;
1214 MapInfo_FindName_firstResult = -1;
1215 return -1; // wrong prefix
1217 if(l == MapInfo_count - 1)
1218 return l; // last one, nothing can follow => unique
1219 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1221 MapInfo_FindName_match = string_null;
1222 return -1; // ambigous MapInfo_FindName_match
1227 string MapInfo_FixName(string s)
1229 MapInfo_FindName(s);
1230 return MapInfo_FindName_match;
1233 int MapInfo_CurrentFeatures()
1236 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")))
1237 req |= MAPINFO_FEATURE_WEAPONS;
1241 int MapInfo_CurrentGametype()
1243 int prev = cvar("gamecfg");
1244 FOREACH(Gametypes, cvar(it.netname) && it.items != prev, LAMBDA(return it.items));
1245 if (prev) return prev;
1246 return MAPINFO_TYPE_DEATHMATCH;
1249 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1251 if(!MapInfo_Get_ByName(s, 1, 0))
1253 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1255 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1260 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1263 r = _MapInfo_CheckMap(s);
1264 MapInfo_ClearTemps();
1268 void MapInfo_SwitchGameType(int t)
1270 FOREACH(Gametypes, true, LAMBDA(
1271 cvar_set(it.netname, (it.items == t) ? "1" : "0")
1275 void MapInfo_LoadMap(string s, float reinit)
1277 MapInfo_Map_supportedGametypes = 0;
1278 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1279 //if(!MapInfo_CheckMap(s))
1281 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1282 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1285 cvar_settemp_restore();
1287 localcmd(strcat("\nmap ", s, "\n"));
1289 localcmd(strcat("\nchangelevel ", s, "\n"));
1292 string MapInfo_ListAllowedMaps(float type, float pRequiredFlags, float pForbiddenFlags)
1297 // to make absolutely sure:
1298 MapInfo_Enumerate();
1299 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1302 for(i = 0; i < MapInfo_count; ++i)
1303 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1304 return substring(out, 1, strlen(out) - 1);
1307 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1312 // to make absolutely sure:
1313 MapInfo_Enumerate();
1314 MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1317 for(i = 0; i < MapInfo_count; ++i)
1318 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1320 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1322 return substring(out, 1, strlen(out) - 1);
1325 void MapInfo_LoadMapSettings_SaveGameType(float t)
1327 MapInfo_SwitchGameType(t);
1328 cvar_set("gamecfg", ftos(t));
1329 MapInfo_LoadedGametype = t;
1332 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1336 t = MapInfo_CurrentGametype();
1337 MapInfo_LoadMapSettings_SaveGameType(t);
1339 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1341 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1343 LOG_SEVERE("can't play the selected map in the given game mode. Working with only the override settings.\n");
1344 _MapInfo_Map_ApplyGametypeEx("", t, t);
1345 return; // do not call Get_ByName!
1348 if(MapInfo_Map_supportedGametypes == 0)
1350 LOG_SEVERE("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1351 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1352 MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1353 _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1354 return; // do not call Get_ByName!
1358 while(!(MapInfo_Map_supportedGametypes & 1))
1361 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1364 // t is now a supported mode!
1365 LOG_WARNING("can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1366 MapInfo_LoadMapSettings_SaveGameType(t);
1368 MapInfo_Get_ByName(s, 1, t);
1371 void MapInfo_ClearTemps()
1373 MapInfo_Map_bspname = string_null;
1374 MapInfo_Map_title = string_null;
1375 MapInfo_Map_titlestring = string_null;
1376 MapInfo_Map_description = string_null;
1377 MapInfo_Map_author = string_null;
1378 MapInfo_Map_clientstuff = string_null;
1379 MapInfo_Map_supportedGametypes = 0;
1380 MapInfo_Map_supportedFeatures = 0;
1383 void MapInfo_Shutdown()
1385 MapInfo_ClearTemps();
1386 MapInfo_Filter_Free();
1387 MapInfo_Cache_Destroy();
1388 if(_MapInfo_globopen)
1390 search_end(_MapInfo_globhandle);
1391 _MapInfo_globhandle = -1;
1392 _MapInfo_globopen = false;
1396 int MapInfo_ForbiddenFlags()
1398 int f = MAPINFO_FLAG_FORBIDDEN;
1401 if (!cvar("g_maplist_allow_hidden"))
1403 f |= MAPINFO_FLAG_HIDDEN;
1405 if (!cvar("g_maplist_allow_frustrating"))
1406 f |= MAPINFO_FLAG_FRUSTRATING;
1411 int MapInfo_RequiredFlags()
1415 if(cvar("g_maplist_allow_frustrating") > 1)
1416 f |= MAPINFO_FLAG_FRUSTRATING;