2 #include "../client/defs.qh"
4 #include "buffs/all.qh"
5 #include "weapons/all.qh"
10 #include "buffs/all.qh"
11 #include "monsters/all.qh"
15 // generic string stuff
17 int _MapInfo_Cache_Active;
18 int _MapInfo_Cache_DB_NameToIndex;
19 int _MapInfo_Cache_Buf_IndexToMapData;
21 void MapInfo_Cache_Destroy()
23 if(!_MapInfo_Cache_Active)
26 db_close(_MapInfo_Cache_DB_NameToIndex);
27 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
28 _MapInfo_Cache_Active = 0;
31 void MapInfo_Cache_Create()
33 MapInfo_Cache_Destroy();
34 _MapInfo_Cache_DB_NameToIndex = db_create();
35 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
36 _MapInfo_Cache_Active = 1;
39 void MapInfo_Cache_Invalidate()
41 if(!_MapInfo_Cache_Active)
44 MapInfo_Cache_Create();
47 void MapInfo_Cache_Store()
51 if(!_MapInfo_Cache_Active)
54 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
57 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
58 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
63 // now store all the stuff
64 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
65 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
66 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
67 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
68 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
69 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
70 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
71 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
74 float MapInfo_Cache_Retrieve(string map)
78 if(!_MapInfo_Cache_Active)
81 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
86 // now retrieve all the stuff
87 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
88 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
89 MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
90 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
91 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
92 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
93 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
94 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
99 // GLOB HANDLING (for all BSP files)
100 float _MapInfo_globopen;
101 float _MapInfo_globcount;
102 float _MapInfo_globhandle;
103 string _MapInfo_GlobItem(float i)
106 if(!_MapInfo_globopen)
108 s = search_getfilename(_MapInfo_globhandle, i);
109 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
112 void MapInfo_Enumerate()
114 if(_MapInfo_globopen)
116 search_end(_MapInfo_globhandle);
117 _MapInfo_globopen = 0;
119 MapInfo_Cache_Invalidate();
120 _MapInfo_globhandle = search_begin("maps/*.bsp", true, true);
121 if(_MapInfo_globhandle >= 0)
123 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
124 _MapInfo_globopen = 1;
127 _MapInfo_globcount = 0;
130 // filter the info by game type mask (updates MapInfo_count)
132 float _MapInfo_filtered;
133 float _MapInfo_filtered_allocated;
134 float MapInfo_FilterList_Lookup(float i)
136 return stof(bufstr_get(_MapInfo_filtered, i));
139 void _MapInfo_FilterList_swap(float i, float j, entity pass)
142 h = bufstr_get(_MapInfo_filtered, i);
143 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
144 bufstr_set(_MapInfo_filtered, j, h);
147 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
150 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
151 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
152 return strcasecmp(a, b);
155 float MapInfo_FilterGametype(int pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
158 if (!_MapInfo_filtered_allocated)
160 _MapInfo_filtered_allocated = 1;
161 _MapInfo_filtered = buf_create();
164 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
166 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.
169 LOG_TRACE("Autogenerated a .mapinfo, doing the rest later.\n");
170 MapInfo_progress = i / _MapInfo_globcount;
173 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
174 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
175 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
176 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
177 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
179 MapInfo_count = j + 1;
180 MapInfo_ClearTemps();
182 // sometimes the glob isn't sorted nicely, so fix it here...
183 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
187 void MapInfo_FilterString(string sf)
189 // this function further filters _MapInfo_filtered, which is prepared by MapInfo_FilterGametype by string
193 for(i = 0, j = -1; i < MapInfo_count; ++i)
195 if (MapInfo_Get_ByID(i))
197 // prepare for keyword filter
198 if (MapInfo_Map_title && strstrofs(MapInfo_Map_title, "<TITLE>", 0) == -1)
199 title = MapInfo_Map_title;
201 title = MapInfo_Map_bspname;
203 if((strstrofs(strtolower(title), strtolower(sf), 0)) >= 0)
204 bufstr_set(_MapInfo_filtered, ++j, bufstr_get(_MapInfo_filtered, i));
207 MapInfo_count = j + 1;
208 MapInfo_ClearTemps();
210 // sometimes the glob isn't sorted nicely, so fix it here...
211 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
214 void MapInfo_Filter_Free()
216 if(_MapInfo_filtered_allocated)
218 buf_del(_MapInfo_filtered);
219 _MapInfo_filtered_allocated = 0;
223 // load info about the i-th map into the MapInfo_Map_* globals
224 string MapInfo_BSPName_ByID(float i)
226 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
229 string unquote(string s)
234 for(i = 0; i < l; ++i)
237 ch = substring(s, i, 1);
238 if(ch != " ") if(ch != "\"")
240 for(j = strlen(s) - 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, 0))
259 string _MapInfo_Map_worldspawn_music;
261 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
271 float diameter, spawnpoints;
274 vector mapMins, mapMaxs;
277 fn = strcat("maps/", pFilename, ".ent");
278 fh = fopen(fn, FILE_READ);
282 fn = strcat("maps/", pFilename, ".bsp");
283 fh = fopen(fn, FILE_READ);
287 LOG_INFO("Analyzing ", fn, " to generate initial mapinfo\n");
290 MapInfo_Map_flags = 0;
291 MapInfo_Map_supportedGametypes = 0;
294 _MapInfo_Map_worldspawn_music = "";
300 if (!((s = fgets(fh))))
302 if(inWorldspawn == 1)
303 if(startsWith(s, "}"))
309 if(k == "classname" && v == "worldspawn")
311 else if(k == "author")
312 MapInfo_Map_author = v;
313 else if(k == "_description")
314 MapInfo_Map_description = v;
315 else if(k == "music")
316 _MapInfo_Map_worldspawn_music = v;
317 else if(k == "noise")
318 _MapInfo_Map_worldspawn_music = v;
319 else if(k == "message")
321 i = strstrofs(v, " by ", 0);
322 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
324 MapInfo_Map_title = substring(v, 0, i);
325 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
328 MapInfo_Map_title = v;
335 o = stov(strcat("'", v, "'"));
336 mapMins.x = min(mapMins.x, o.x);
337 mapMins.y = min(mapMins.y, o.y);
338 mapMins.z = min(mapMins.z, o.z);
339 mapMaxs.x = max(mapMaxs.x, o.x);
340 mapMaxs.y = max(mapMaxs.y, o.y);
341 mapMaxs.z = max(mapMaxs.z, o.z);
343 else if(k == "race_place")
348 else if(k == "classname")
350 if(v == "dom_controlpoint")
351 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
352 else if(v == "item_flag_team2")
353 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
354 else if(v == "team_CTF_blueflag")
355 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
356 else if(v == "invasion_spawnpoint")
357 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_INVASION;
358 else if(v == "target_assault_roundend")
359 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
360 else if(v == "onslaught_generator")
361 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
362 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
363 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
364 else if(v == "info_player_team1")
366 else if(v == "info_player_team2")
368 else if(v == "info_player_start")
370 else if(v == "info_player_deathmatch")
372 else if(v == "trigger_race_checkpoint")
373 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
374 else if(v == "target_startTimer")
375 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
376 else if(v == "weapon_nex")
378 else if(v == "weapon_railgun")
380 else if(startsWith(v, "weapon_"))
381 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
382 else if(startsWith(v, "turret_"))
383 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
384 else if(startsWith(v, "vehicle_"))
385 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
386 else if(startsWith(v, "monster_"))
387 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
388 else if(v == "target_music" || v == "trigger_music")
389 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
395 LOG_INFO(fn, " ended still in worldspawn, BUG\n");
398 diameter = vlen(mapMaxs - mapMins);
400 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
401 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
403 // we have a CTF-only or Assault-only map. Don't add other modes then,
404 // as the map is too symmetric for them.
408 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
409 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
410 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY; // Keepaway always works
412 if(spawnpoints >= 8 && diameter > 4096) {
413 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
414 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
415 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
417 if(spawnpoints >= 12 && diameter > 5120)
418 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
421 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
424 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE;
425 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
428 LOG_TRACE("-> diameter ", ftos(diameter));
429 LOG_TRACE("; spawnpoints ", ftos(spawnpoints));
430 LOG_TRACE("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
437 void _MapInfo_Map_Reset()
439 MapInfo_Map_title = "<TITLE>";
440 MapInfo_Map_titlestring = "<TITLE>";
441 MapInfo_Map_description = "<DESCRIPTION>";
442 MapInfo_Map_author = "<AUTHOR>";
443 MapInfo_Map_supportedGametypes = 0;
444 MapInfo_Map_supportedFeatures = 0;
445 MapInfo_Map_flags = 0;
446 MapInfo_Map_clientstuff = "";
447 MapInfo_Map_fog = "";
448 MapInfo_Map_mins = '0 0 0';
449 MapInfo_Map_maxs = '0 0 0';
452 string _MapInfo_GetDefault(float t)
456 case MAPINFO_TYPE_DEATHMATCH: return "30 20 0";
457 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
458 case MAPINFO_TYPE_DOMINATION: return "200 20 0";
459 case MAPINFO_TYPE_CTF: return "300 20 10 0";
460 case MAPINFO_TYPE_LMS: return "9 20 0";
461 case MAPINFO_TYPE_CA: return "10 20 0";
462 case MAPINFO_TYPE_KEYHUNT: return "1000 20 3 0";
463 case MAPINFO_TYPE_ASSAULT: return "20 0";
464 case MAPINFO_TYPE_RACE: return "20 5 7 15 0";
465 case MAPINFO_TYPE_ONSLAUGHT: return "20 0";
466 case MAPINFO_TYPE_NEXBALL: return "5 20 0";
467 case MAPINFO_TYPE_CTS: return "20 0 0";
468 case MAPINFO_TYPE_FREEZETAG: return "10 20 0";
469 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
470 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
471 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
472 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
477 void _MapInfo_Map_ApplyGametype(string s, int pWantedType, int pThisType, int load_default)
480 MapInfo_Map_supportedGametypes |= pThisType;
481 if(!(pThisType & pWantedType))
485 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, false);
487 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
489 cvar_set("fraglimit", "0");
495 cvar_set("fraglimit", sa);
501 cvar_set("timelimit", sa);
504 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
508 cvar_set("g_tdm_teams", sa);
512 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
516 cvar_set("g_keyhunt_teams", sa);
520 if(pWantedType == MAPINFO_TYPE_CA)
524 cvar_set("g_ca_teams", sa);
528 if(pWantedType == MAPINFO_TYPE_FREEZETAG)
532 cvar_set("g_freezetag_teams", sa);
536 if(pWantedType == MAPINFO_TYPE_CTF)
540 cvar_set("fraglimit", sa);
545 if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
549 cvar_set("fraglimit", sa);
554 // rc = timelimit timelimit_qualification laps laps_teamplay
555 if(pWantedType == MAPINFO_TYPE_RACE)
557 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
558 cvar_set("g_race_qualifying_timelimit", sa);
563 if(cvar("g_race_teams") < 2)
564 cvar_set("fraglimit", sa);
569 if(cvar("g_race_teams") >= 2)
570 cvar_set("fraglimit", sa);
574 if(pWantedType == MAPINFO_TYPE_CTS)
578 // this is the skill of the map
579 // not parsed by anything yet
582 // cvar_set("fraglimit", sa);
587 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
589 cvar_set("leadlimit", "0");
595 cvar_set("leadlimit", sa);
600 string _MapInfo_GetDefaultEx(float t)
602 FOREACH(Gametypes, it.items == t, LAMBDA(return it.model2));
606 float _MapInfo_GetTeamPlayBool(float t)
608 FOREACH(Gametypes, it.items == t, LAMBDA(return it.team));
612 void _MapInfo_Map_ApplyGametypeEx(string s, int pWantedType, int pThisType)
616 string fraglimit_normal;
617 string fraglimit_teams;
619 MapInfo_Map_supportedGametypes |= pThisType;
620 if(!(pThisType & pWantedType))
623 // reset all the cvars to their defaults
625 cvar_set("timelimit", cvar_defstring("timelimit"));
626 cvar_set("leadlimit", cvar_defstring("leadlimit"));
627 cvar_set("fraglimit", cvar_defstring("fraglimit"));
628 cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
629 cvar_set("g_ca_teams", cvar_defstring("g_ca_teams"));
630 cvar_set("g_freezetag_teams", cvar_defstring("g_freezetag_teams"));
631 cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
632 cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
633 cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
635 fraglimit_normal = string_null;
636 fraglimit_teams = string_null;
638 s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
647 p = strstrofs(sa, "=", 0);
650 LOG_INFO("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
653 k = substring(sa, 0, p);
654 v = substring(sa, p+1, -1);
658 cvar_set("timelimit", v);
660 else if(k == "leadlimit")
662 cvar_set("leadlimit", v);
664 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit" || k == "caplimit")
666 fraglimit_normal = v;
668 else if(k == "teampointlimit" || k == "teamlaplimit")
672 else if(k == "teams")
674 cvar_set("g_tdm_teams", v);
675 cvar_set("g_ca_teams", v);
676 cvar_set("g_freezetag_teams", v);
677 cvar_set("g_keyhunt_teams", v);
678 cvar_set("g_domination_default_teams", v);
679 cvar_set("g_invasion_teams", v);
681 else if(k == "qualifying_timelimit")
683 cvar_set("g_race_qualifying_timelimit", v);
685 else if(k == "skill")
691 LOG_INFO("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
695 if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
698 cvar_set("fraglimit", fraglimit_teams);
703 cvar_set("fraglimit", fraglimit_normal);
707 int MapInfo_Type_FromString(string t)
709 #define deprecate(from, to) do { \
711 string replacement = #to; \
712 LOG_WARNINGF("MapInfo_Type_FromString (probably %s): using deprecated name '%s'. Should use '%s'.\n", MapInfo_Map_bspname, t, replacement); \
716 deprecate(nexball, nb);
717 deprecate(freezetag, ft);
718 deprecate(keepaway, ka);
719 deprecate(invasion, inv);
720 deprecate(assault, as);
722 if (t == "all") return MAPINFO_TYPE_ALL;
723 FOREACH(Gametypes, it.mdl == t, LAMBDA(return it.items));
728 string MapInfo_Type_Description(float t)
730 FOREACH(Gametypes, it.items == t, LAMBDA(return it.gametype_description));
734 string MapInfo_Type_ToString(float t)
736 if(t == MAPINFO_TYPE_ALL)
738 FOREACH(Gametypes, it.items == t, LAMBDA(return it.mdl));
742 string MapInfo_Type_ToText(float t)
744 FOREACH(Gametypes, it.items == t, LAMBDA(return it.message));
745 /* xgettext:no-c-format */
746 return _("@!#%'n Tuba Throwing");
749 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
753 t = car(s); s = cdr(s);
755 // limited support of "" and comments
756 // remove trailing and leading " of t
757 if(substring(t, 0, 1) == "\"")
759 if(substring(t, -1, 1) == "\"")
760 t = substring(t, 1, -2);
763 // remove leading " of s
764 if(substring(s, 0, 1) == "\"")
766 s = substring(s, 1, -1);
768 // remove trailing " of s, and all that follows (cvar description)
769 o = strstrofs(s, "\"", 0);
771 s = substring(s, 0, o);
773 // remove // comments
774 o = strstrofs(s, "//", 0);
776 s = substring(s, 0, o);
778 // remove trailing spaces
779 while(substring(s, -1, 1) == " ")
780 s = substring(s, 0, -2);
786 fh = fopen(s, FILE_READ);
788 LOG_INFO("Map ", pFilename, " references not existing config file ", s, "\n");
793 if (!((s = fgets(fh))))
796 // catch different sorts of comments
797 if(s == "") // empty lines
799 if(substring(s, 0, 1) == "#") // UNIX style
801 if(substring(s, 0, 2) == "//") // C++ style
803 if(substring(s, 0, 1) == "_") // q3map style
806 if(substring(s, 0, 4) == "set ")
807 s = substring(s, 4, -1);
808 if(substring(s, 0, 5) == "seta ")
809 s = substring(s, 5, -1);
811 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
817 LOG_INFO("Map ", pFilename, " uses too many levels of inclusion\n");
820 LOG_INFO("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
821 else if (!cvar_value_issafe(t))
822 LOG_INFO("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
823 else if (!cvar_value_issafe(s))
824 LOG_INFO("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
825 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
826 LOG_INFO("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
827 else if(matchacl(acl, t) <= 0)
828 LOG_INFO("Map ", pFilename, " contains a denied setting, ignored\n");
831 if(type == 0) // server set
833 LOG_TRACE("Applying temporary setting ", t, " := ", s, "\n");
834 if(cvar("g_campaign"))
835 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
841 LOG_TRACE("Applying temporary client setting ", t, " := ", s, "\n");
842 MapInfo_Map_clientstuff = strcat(
843 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
849 float MapInfo_isRedundant(string fn, string t)
851 // normalize file name
852 fn = strreplace("_", "-", fn);
854 // normalize visible title
855 t = strreplace(": ", "-", t);
856 t = strreplace(":", "-", t);
857 t = strreplace(" ", "-", t);
858 t = strreplace("_", "-", t);
859 t = strreplace("'", "-", t);
861 if(!strcasecmp(fn, t))
864 // we allow the visible title to have punctuation the file name does
865 // not, but not vice versa
866 t = strreplace("-", "", t);
868 if(!strcasecmp(fn, t))
874 // load info about a map by name into the MapInfo_Map_* globals
875 float MapInfo_Get_ByName_NoFallbacks(string pFilename, int pAllowGenerate, int pGametypeToSet)
884 acl = MAPINFO_SETTEMP_ACL_USER;
886 if(strstrofs(pFilename, "/", 0) >= 0)
888 LOG_INFO("Invalid character in map name, ignored\n");
892 if(pGametypeToSet == 0)
893 if(MapInfo_Cache_Retrieve(pFilename))
898 MapInfo_Map_bspname = pFilename;
900 // default all generic fields so they have "good" values in case something fails
901 fn = strcat("maps/", pFilename, ".mapinfo");
902 fh = fopen(fn, FILE_READ);
905 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
906 fh = fopen(fn, FILE_READ);
911 _MapInfo_Map_Reset();
912 r = _MapInfo_Generate(pFilename);
915 fh = fopen(fn, FILE_WRITE);
916 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
917 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
918 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
919 if(_MapInfo_Map_worldspawn_music != "")
922 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
924 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
926 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
928 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
932 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
933 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
936 i = floor(random() * n);
937 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
940 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
942 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
943 fputs(fh, "has weapons\n");
945 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
946 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
947 fputs(fh, "has turrets\n");
949 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
950 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
951 fputs(fh, "has vehicles\n");
953 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
954 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
955 fputs(fh, "frustrating\n");
957 for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
958 if(MapInfo_Map_supportedGametypes & i)
959 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
961 if(fexists(strcat("scripts/", pFilename, ".arena")))
962 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
964 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
965 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
966 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
967 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
968 fputs(fh, "// optional: hidden\n");
973 fh = fopen(fn, FILE_READ);
975 error("... but I just wrote it!");
978 LOG_INFO("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
981 _MapInfo_Map_Reset();
984 if (!((s = fgets(fh))))
987 // catch different sorts of comments
988 if(s == "") // empty lines
990 if(substring(s, 0, 1) == "#") // UNIX style
992 if(substring(s, 0, 2) == "//") // C++ style
994 if(substring(s, 0, 1) == "_") // q3map style
997 p = strstrofs(s, "//", 0);
999 s = substring(s, 0, p);
1001 t = car(s); s = cdr(s);
1003 MapInfo_Map_title = s;
1004 else if(t == "description")
1005 MapInfo_Map_description = s;
1006 else if(t == "author")
1007 MapInfo_Map_author = s;
1010 t = car(s); // s = cdr(s);
1011 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1012 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
1013 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
1014 else if(t == "monsters") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
1015 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1017 LOG_TRACE("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
1019 else if(t == "hidden")
1021 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
1023 else if(t == "forbidden")
1025 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
1027 else if(t == "frustrating")
1029 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
1031 else if(t == "noautomaplist")
1033 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1035 else if(t == "type")
1037 t = car(s); s = cdr(s);
1038 f = MapInfo_Type_FromString(t);
1039 LOG_WARNING("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");
1041 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, true);
1043 LOG_TRACE("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1045 else if(t == "gametype")
1047 t = car(s); s = cdr(s);
1048 f = MapInfo_Type_FromString(t);
1050 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
1052 LOG_TRACE("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1054 else if(t == "size")
1056 float a, b, c, d, e;
1057 t = car(s); s = cdr(s); a = stof(t);
1058 t = car(s); s = cdr(s); b = stof(t);
1059 t = car(s); s = cdr(s); c = stof(t);
1060 t = car(s); s = cdr(s); d = stof(t);
1061 t = car(s); s = cdr(s); e = stof(t);
1063 LOG_INFO("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");
1066 t = car(s); s = cdr(s); f = stof(t);
1068 LOG_INFO("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");
1071 if(a >= d || b >= e || c >= f)
1072 LOG_INFO("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
1075 MapInfo_Map_mins.x = a;
1076 MapInfo_Map_mins.y = b;
1077 MapInfo_Map_mins.z = c;
1078 MapInfo_Map_maxs.x = d;
1079 MapInfo_Map_maxs.y = e;
1080 MapInfo_Map_maxs.z = f;
1085 else if(t == "settemp_for_type")
1087 t = car(s); s = cdr(s);
1088 if((f = MapInfo_Type_FromString(t)))
1090 if(f & pGametypeToSet)
1092 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1097 LOG_TRACE("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1100 else if(t == "clientsettemp_for_type")
1102 t = car(s); s = cdr(s);
1103 if((f = MapInfo_Type_FromString(t)))
1105 if(f & pGametypeToSet)
1107 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1112 LOG_TRACE("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1117 if (!cvar_value_issafe(s))
1118 LOG_INFO("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1120 MapInfo_Map_fog = s;
1122 else if(t == "cdtrack")
1124 t = car(s); s = cdr(s);
1125 // We do this only if pGametypeToSet even though this
1126 // content is theoretically game type independent,
1127 // because MapInfo_Map_clientstuff contains otherwise
1128 // game type dependent stuff. That way this value stays
1129 // empty when not setting a game type to not set any
1130 // false expectations.
1133 if (!cvar_value_issafe(t))
1134 LOG_INFO("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1136 MapInfo_Map_clientstuff = strcat(
1137 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1142 LOG_TRACE("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1146 if(MapInfo_Map_title == "<TITLE>")
1147 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1148 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1149 MapInfo_Map_titlestring = MapInfo_Map_title;
1151 MapInfo_Map_titlestring = sprintf("%s: %s", MapInfo_Map_bspname, MapInfo_Map_title);
1153 MapInfo_Cache_Store();
1154 if(MapInfo_Map_supportedGametypes != 0)
1156 LOG_TRACE("Map ", pFilename, " supports no game types, ignored\n");
1159 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, int pGametypeToSet)
1161 float r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1163 if(cvar("g_tdm_on_dm_maps"))
1165 // if this is set, all DM maps support TDM too
1166 if (!(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH))
1167 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)
1168 _MapInfo_Map_ApplyGametypeEx ("", pGametypeToSet, MAPINFO_TYPE_TEAM_DEATHMATCH);
1173 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1175 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1176 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1184 float MapInfo_FindName(string s)
1186 // if there is exactly one map of prefix s, return it
1187 // if not, return the null string
1188 // note that DP sorts glob results... so I can use a binary search
1192 // invariants: r is behind s, l-1 is equal or before
1195 m = floor((l + r) / 2);
1196 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1197 cmp = strcasecmp(MapInfo_FindName_match, s);
1199 return m; // found and good
1201 l = m + 1; // l-1 is before s
1205 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1206 MapInfo_FindName_firstResult = l;
1207 // r == l, so: l is behind s, l-1 is before
1208 // SO: if there is any, l is the one with the right prefix
1209 // and l+1 may be one too
1210 if(l == MapInfo_count)
1212 MapInfo_FindName_match = string_null;
1213 MapInfo_FindName_firstResult = -1;
1214 return -1; // no MapInfo_FindName_match, behind last item
1216 if(!startsWithNocase(MapInfo_FindName_match, s))
1218 MapInfo_FindName_match = string_null;
1219 MapInfo_FindName_firstResult = -1;
1220 return -1; // wrong prefix
1222 if(l == MapInfo_count - 1)
1223 return l; // last one, nothing can follow => unique
1224 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1226 MapInfo_FindName_match = string_null;
1227 return -1; // ambigous MapInfo_FindName_match
1232 string MapInfo_FixName(string s)
1234 MapInfo_FindName(s);
1235 return MapInfo_FindName_match;
1238 int MapInfo_CurrentFeatures()
1241 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")))
1242 req |= MAPINFO_FEATURE_WEAPONS;
1246 int MapInfo_CurrentGametype()
1248 int prev = cvar("gamecfg");
1249 FOREACH(Gametypes, cvar(it.netname) && it.items != prev, LAMBDA(return it.items));
1250 if (prev) return prev;
1251 return MAPINFO_TYPE_DEATHMATCH;
1254 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1256 if(!MapInfo_Get_ByName(s, 1, 0))
1258 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1260 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1265 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1268 r = _MapInfo_CheckMap(s);
1269 MapInfo_ClearTemps();
1273 void MapInfo_SwitchGameType(int t)
1275 FOREACH(Gametypes, true, LAMBDA(
1276 cvar_set(it.netname, (it.items == t) ? "1" : "0")
1280 void MapInfo_LoadMap(string s, float reinit)
1282 MapInfo_Map_supportedGametypes = 0;
1283 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1284 //if(!MapInfo_CheckMap(s))
1286 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1287 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1290 cvar_settemp_restore();
1292 localcmd(strcat("\nmap ", s, "\n"));
1294 localcmd(strcat("\nchangelevel ", s, "\n"));
1297 string MapInfo_ListAllowedMaps(float type, float pRequiredFlags, float pForbiddenFlags)
1302 // to make absolutely sure:
1303 MapInfo_Enumerate();
1304 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1307 for(i = 0; i < MapInfo_count; ++i)
1308 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1309 return substring(out, 1, strlen(out) - 1);
1312 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1317 // to make absolutely sure:
1318 MapInfo_Enumerate();
1319 MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1322 for(i = 0; i < MapInfo_count; ++i)
1323 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1325 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1327 return substring(out, 1, strlen(out) - 1);
1330 void MapInfo_LoadMapSettings_SaveGameType(float t)
1332 MapInfo_SwitchGameType(t);
1333 cvar_set("gamecfg", ftos(t));
1334 MapInfo_LoadedGametype = t;
1337 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1341 t = MapInfo_CurrentGametype();
1342 MapInfo_LoadMapSettings_SaveGameType(t);
1344 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1346 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1348 LOG_INFO("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1349 _MapInfo_Map_ApplyGametypeEx("", t, t);
1350 return; // do not call Get_ByName!
1353 if(MapInfo_Map_supportedGametypes == 0)
1355 LOG_INFO("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1356 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1357 MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1358 _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1359 return; // do not call Get_ByName!
1363 while(!(MapInfo_Map_supportedGametypes & 1))
1366 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1369 // t is now a supported mode!
1370 LOG_INFO("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1371 MapInfo_LoadMapSettings_SaveGameType(t);
1373 MapInfo_Get_ByName(s, 1, t);
1376 void MapInfo_ClearTemps()
1378 MapInfo_Map_bspname = string_null;
1379 MapInfo_Map_title = string_null;
1380 MapInfo_Map_titlestring = string_null;
1381 MapInfo_Map_description = string_null;
1382 MapInfo_Map_author = string_null;
1383 MapInfo_Map_clientstuff = string_null;
1384 MapInfo_Map_supportedGametypes = 0;
1385 MapInfo_Map_supportedFeatures = 0;
1388 void MapInfo_Shutdown()
1390 MapInfo_ClearTemps();
1391 MapInfo_Filter_Free();
1392 MapInfo_Cache_Destroy();
1393 if(_MapInfo_globopen)
1395 search_end(_MapInfo_globhandle);
1396 _MapInfo_globhandle = -1;
1397 _MapInfo_globopen = false;
1401 int MapInfo_ForbiddenFlags()
1403 int f = MAPINFO_FLAG_FORBIDDEN;
1406 if (!cvar("g_maplist_allow_hidden"))
1408 f |= MAPINFO_FLAG_HIDDEN;
1410 if (!cvar("g_maplist_allow_frustrating"))
1411 f |= MAPINFO_FLAG_FRUSTRATING;
1416 int MapInfo_RequiredFlags()
1420 if(cvar("g_maplist_allow_frustrating") > 1)
1421 f |= MAPINFO_FLAG_FRUSTRATING;