2 #include "../dpdefs/csprogsdefs.qh"
3 #include "../client/defs.qh"
6 #include "weapons/weapons.qh"
10 #include "../dpdefs/progsdefs.qh"
11 #include "../dpdefs/dpextensions.qh"
12 #include "../server/sys-post.qh"
15 #include "monsters/monsters.qh"
19 // generic string stuff
21 float _MapInfo_Cache_Active;
22 float _MapInfo_Cache_DB_NameToIndex;
23 float _MapInfo_Cache_Buf_IndexToMapData;
25 void MapInfo_Cache_Destroy()
27 if(!_MapInfo_Cache_Active)
30 db_close(_MapInfo_Cache_DB_NameToIndex);
31 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
32 _MapInfo_Cache_Active = 0;
35 void MapInfo_Cache_Create()
37 MapInfo_Cache_Destroy();
38 _MapInfo_Cache_DB_NameToIndex = db_create();
39 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
40 _MapInfo_Cache_Active = 1;
43 void MapInfo_Cache_Invalidate()
45 if(!_MapInfo_Cache_Active)
48 MapInfo_Cache_Create();
51 void MapInfo_Cache_Store()
55 if(!_MapInfo_Cache_Active)
58 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
61 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
62 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
67 // now store all the stuff
68 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
69 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
70 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
71 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
72 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
73 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
74 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
75 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
78 float MapInfo_Cache_Retrieve(string map)
82 if(!_MapInfo_Cache_Active)
85 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
90 // now retrieve all the stuff
91 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
92 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
93 MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
94 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
95 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
96 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
97 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
98 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
103 // GLOB HANDLING (for all BSP files)
104 float _MapInfo_globopen;
105 float _MapInfo_globcount;
106 float _MapInfo_globhandle;
107 string _MapInfo_GlobItem(float i)
110 if(!_MapInfo_globopen)
112 s = search_getfilename(_MapInfo_globhandle, i);
113 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
116 void MapInfo_Enumerate()
118 if(_MapInfo_globopen)
120 search_end(_MapInfo_globhandle);
121 _MapInfo_globopen = 0;
123 MapInfo_Cache_Invalidate();
124 _MapInfo_globhandle = search_begin("maps/*.bsp", true, true);
125 if(_MapInfo_globhandle >= 0)
127 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
128 _MapInfo_globopen = 1;
131 _MapInfo_globcount = 0;
134 // filter the info by game type mask (updates MapInfo_count)
136 float _MapInfo_filtered;
137 float _MapInfo_filtered_allocated;
138 float MapInfo_FilterList_Lookup(float i)
140 return stof(bufstr_get(_MapInfo_filtered, i));
143 void _MapInfo_FilterList_swap(float i, float j, entity pass)
146 h = bufstr_get(_MapInfo_filtered, i);
147 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
148 bufstr_set(_MapInfo_filtered, j, h);
151 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
154 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
155 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
156 return strcasecmp(a, b);
159 float MapInfo_FilterGametype(int pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate)
162 if (!_MapInfo_filtered_allocated)
164 _MapInfo_filtered_allocated = 1;
165 _MapInfo_filtered = buf_create();
168 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
170 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.
173 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
174 MapInfo_progress = i / _MapInfo_globcount;
177 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
178 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
179 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
180 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
181 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
183 MapInfo_count = j + 1;
184 MapInfo_ClearTemps();
186 // sometimes the glob isn't sorted nicely, so fix it here...
187 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
192 void MapInfo_Filter_Free()
194 if(_MapInfo_filtered_allocated)
196 buf_del(_MapInfo_filtered);
197 _MapInfo_filtered_allocated = 0;
201 // load info about the i-th map into the MapInfo_Map_* globals
202 string MapInfo_BSPName_ByID(float i)
204 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
207 string unquote(string s)
212 for(i = 0; i < l; ++i)
215 ch = substring(s, i, 1);
216 if(ch != " ") if(ch != "\"")
218 for(j = strlen(s) - i - 1; j > 0; --j)
220 ch = substring(s, i+j, 1);
221 if(ch != " ") if(ch != "\"")
222 return substring(s, i, j+1);
224 return substring(s, i, 1);
230 float MapInfo_Get_ByID(float i)
232 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
237 string _MapInfo_Map_worldspawn_music;
239 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
249 float diameter, spawnpoints;
252 vector mapMins, mapMaxs;
255 fn = strcat("maps/", pFilename, ".ent");
256 fh = fopen(fn, FILE_READ);
260 fn = strcat("maps/", pFilename, ".bsp");
261 fh = fopen(fn, FILE_READ);
265 print("Analyzing ", fn, " to generate initial mapinfo\n");
268 MapInfo_Map_flags = 0;
269 MapInfo_Map_supportedGametypes = 0;
272 _MapInfo_Map_worldspawn_music = "";
278 if (!((s = fgets(fh))))
280 if(inWorldspawn == 1)
281 if(startsWith(s, "}"))
287 if(k == "classname" && v == "worldspawn")
289 else if(k == "author")
290 MapInfo_Map_author = v;
291 else if(k == "_description")
292 MapInfo_Map_description = v;
293 else if(k == "music")
294 _MapInfo_Map_worldspawn_music = v;
295 else if(k == "noise")
296 _MapInfo_Map_worldspawn_music = v;
297 else if(k == "message")
299 i = strstrofs(v, " by ", 0);
300 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
302 MapInfo_Map_title = substring(v, 0, i);
303 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
306 MapInfo_Map_title = v;
313 o = stov(strcat("'", v, "'"));
314 mapMins_x = min(mapMins.x, o.x);
315 mapMins_y = min(mapMins.y, o.y);
316 mapMins_z = min(mapMins.z, o.z);
317 mapMaxs_x = max(mapMaxs.x, o.x);
318 mapMaxs_y = max(mapMaxs.y, o.y);
319 mapMaxs_z = max(mapMaxs.z, o.z);
321 else if(k == "race_place")
326 else if(k == "classname")
328 if(v == "dom_controlpoint")
329 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
330 else if(v == "item_flag_team2")
331 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
332 else if(v == "team_CTF_blueflag")
333 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
334 else if(v == "invasion_spawnpoint")
335 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_INVASION;
336 else if(v == "target_assault_roundend")
337 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
338 else if(v == "onslaught_generator")
339 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
340 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
341 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
342 else if(v == "info_player_team1")
344 else if(v == "info_player_team2")
346 else if(v == "info_player_start")
348 else if(v == "info_player_deathmatch")
350 else if(v == "trigger_race_checkpoint")
351 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
352 else if(v == "target_startTimer")
353 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
354 else if(v == "weapon_nex")
356 else if(v == "weapon_railgun")
358 else if(startsWith(v, "weapon_"))
359 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
360 else if(startsWith(v, "turret_"))
361 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
362 else if(startsWith(v, "vehicle_"))
363 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
364 else if(startsWith(v, "monster_"))
365 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
366 else if(v == "target_music" || v == "trigger_music")
367 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
373 print(fn, " ended still in worldspawn, BUG\n");
376 diameter = vlen(mapMaxs - mapMins);
378 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
379 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
381 // we have a CTF-only or Assault-only map. Don't add other modes then,
382 // as the map is too symmetric for them.
386 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
387 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
388 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY; // Keepaway always works
390 if(spawnpoints >= 8 && diameter > 4096) {
391 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
392 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
393 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
395 if(spawnpoints >= 12 && diameter > 5120)
396 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
399 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
402 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE;
403 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
406 dprint("-> diameter ", ftos(diameter));
407 dprint("; spawnpoints ", ftos(spawnpoints));
408 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
415 void _MapInfo_Map_Reset()
417 MapInfo_Map_title = "<TITLE>";
418 MapInfo_Map_titlestring = "<TITLE>";
419 MapInfo_Map_description = "<DESCRIPTION>";
420 MapInfo_Map_author = "<AUTHOR>";
421 MapInfo_Map_supportedGametypes = 0;
422 MapInfo_Map_supportedFeatures = 0;
423 MapInfo_Map_flags = 0;
424 MapInfo_Map_clientstuff = "";
425 MapInfo_Map_fog = "";
426 MapInfo_Map_mins = '0 0 0';
427 MapInfo_Map_maxs = '0 0 0';
430 string _MapInfo_GetDefault(float t)
434 case MAPINFO_TYPE_DEATHMATCH: return "30 20 0";
435 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
436 case MAPINFO_TYPE_DOMINATION: return "200 20 0";
437 case MAPINFO_TYPE_CTF: return "300 20 10 0";
438 case MAPINFO_TYPE_LMS: return "9 20 0";
439 case MAPINFO_TYPE_CA: return "10 20 0";
440 case MAPINFO_TYPE_KEYHUNT: return "1000 20 3 0";
441 case MAPINFO_TYPE_ASSAULT: return "20 0";
442 case MAPINFO_TYPE_RACE: return "20 5 7 15 0";
443 case MAPINFO_TYPE_ONSLAUGHT: return "20 0";
444 case MAPINFO_TYPE_NEXBALL: return "5 20 0";
445 case MAPINFO_TYPE_CTS: return "20 0 0";
446 case MAPINFO_TYPE_FREEZETAG: return "10 20 0";
447 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
448 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
449 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
450 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
455 void _MapInfo_Map_ApplyGametype(string s, int pWantedType, int pThisType, int load_default)
458 MapInfo_Map_supportedGametypes |= pThisType;
459 if(!(pThisType & pWantedType))
463 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, false);
465 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
467 cvar_set("fraglimit", "0");
473 cvar_set("fraglimit", sa);
479 cvar_set("timelimit", sa);
482 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
486 cvar_set("g_tdm_teams", sa);
490 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
494 cvar_set("g_keyhunt_teams", sa);
498 if(pWantedType == MAPINFO_TYPE_CA)
502 cvar_set("g_ca_teams", sa);
506 if(pWantedType == MAPINFO_TYPE_FREEZETAG)
510 cvar_set("g_freezetag_teams", sa);
514 if(pWantedType == MAPINFO_TYPE_CTF)
518 cvar_set("fraglimit", sa);
523 if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
527 cvar_set("fraglimit", sa);
532 // rc = timelimit timelimit_qualification laps laps_teamplay
533 if(pWantedType == MAPINFO_TYPE_RACE)
535 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
536 cvar_set("g_race_qualifying_timelimit", sa);
541 if(cvar("g_race_teams") < 2)
542 cvar_set("fraglimit", sa);
547 if(cvar("g_race_teams") >= 2)
548 cvar_set("fraglimit", sa);
552 if(pWantedType == MAPINFO_TYPE_CTS)
556 // this is the skill of the map
557 // not parsed by anything yet
560 // cvar_set("fraglimit", sa);
565 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
567 cvar_set("leadlimit", "0");
573 cvar_set("leadlimit", sa);
578 string _MapInfo_GetDefaultEx(float t)
581 for(e = MapInfo_Type_first; e; e = e.enemy)
587 float _MapInfo_GetTeamPlayBool(float t)
590 for(e = MapInfo_Type_first; e; e = e.enemy)
596 void _MapInfo_Map_ApplyGametypeEx(string s, int pWantedType, int pThisType)
600 string fraglimit_normal;
601 string fraglimit_teams;
603 MapInfo_Map_supportedGametypes |= pThisType;
604 if(!(pThisType & pWantedType))
607 // reset all the cvars to their defaults
609 cvar_set("timelimit", cvar_defstring("timelimit"));
610 cvar_set("leadlimit", cvar_defstring("leadlimit"));
611 cvar_set("fraglimit", cvar_defstring("fraglimit"));
612 cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
613 cvar_set("g_ca_teams", cvar_defstring("g_ca_teams"));
614 cvar_set("g_freezetag_teams", cvar_defstring("g_freezetag_teams"));
615 cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
616 cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
617 cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
619 fraglimit_normal = string_null;
620 fraglimit_teams = string_null;
622 s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
631 p = strstrofs(sa, "=", 0);
634 print("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
637 k = substring(sa, 0, p);
638 v = substring(sa, p+1, -1);
642 cvar_set("timelimit", v);
644 else if(k == "leadlimit")
646 cvar_set("leadlimit", v);
648 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit" || k == "caplimit")
650 fraglimit_normal = v;
652 else if(k == "teampointlimit" || k == "teamlaplimit")
656 else if(k == "teams")
658 cvar_set("g_tdm_teams", v);
659 cvar_set("g_ca_teams", v);
660 cvar_set("g_freezetag_teams", v);
661 cvar_set("g_keyhunt_teams", v);
662 cvar_set("g_domination_default_teams", v);
663 cvar_set("g_invasion_teams", v);
665 else if(k == "qualifying_timelimit")
667 cvar_set("g_race_qualifying_timelimit", v);
669 else if(k == "skill")
675 print("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
679 if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
682 cvar_set("fraglimit", fraglimit_teams);
687 cvar_set("fraglimit", fraglimit_normal);
691 float MapInfo_Type_FromString(string t)
696 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
698 print("'. Should use '", t, "'.\n");
702 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
704 print("'. Should use '", t, "'.\n");
708 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
710 print("'. Should use '", t, "'.\n");
714 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
716 print("'. Should use '", t, "'.\n");
720 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
722 print("'. Should use '", t, "'.\n");
726 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
728 print("'. Should use '", t, "'.\n");
731 return MAPINFO_TYPE_ALL;
732 for(e = MapInfo_Type_first; e; e = e.enemy)
738 string MapInfo_Type_Description(float t)
741 for(e = MapInfo_Type_first; e; e = e.enemy)
743 return e.gametype_description;
747 string MapInfo_Type_ToString(float t)
750 if(t == MAPINFO_TYPE_ALL)
752 for(e = MapInfo_Type_first; e; e = e.enemy)
758 string MapInfo_Type_ToText(float t)
761 for(e = MapInfo_Type_first; e; e = e.enemy)
764 /* xgettext:no-c-format */
765 return _("@!#%'n Tuba Throwing");
768 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
772 t = car(s); s = cdr(s);
774 // limited support of "" and comments
775 // remove trailing and leading " of t
776 if(substring(t, 0, 1) == "\"")
778 if(substring(t, -1, 1) == "\"")
779 t = substring(t, 1, -2);
782 // remove leading " of s
783 if(substring(s, 0, 1) == "\"")
785 s = substring(s, 1, -1);
787 // remove trailing " of s, and all that follows (cvar description)
788 o = strstrofs(s, "\"", 0);
790 s = substring(s, 0, o);
792 // remove // comments
793 o = strstrofs(s, "//", 0);
795 s = substring(s, 0, o);
797 // remove trailing spaces
798 while(substring(s, -1, 1) == " ")
799 s = substring(s, 0, -2);
805 fh = fopen(s, FILE_READ);
807 print("Map ", pFilename, " references not existing config file ", s, "\n");
812 if (!((s = fgets(fh))))
815 // catch different sorts of comments
816 if(s == "") // empty lines
818 if(substring(s, 0, 1) == "#") // UNIX style
820 if(substring(s, 0, 2) == "//") // C++ style
822 if(substring(s, 0, 1) == "_") // q3map style
825 if(substring(s, 0, 4) == "set ")
826 s = substring(s, 4, -1);
827 if(substring(s, 0, 5) == "seta ")
828 s = substring(s, 5, -1);
830 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
836 print("Map ", pFilename, " uses too many levels of inclusion\n");
839 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
840 else if (!cvar_value_issafe(t))
841 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
842 else if (!cvar_value_issafe(s))
843 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
844 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
845 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
846 else if(matchacl(acl, t) <= 0)
847 print("Map ", pFilename, " contains a denied setting, ignored\n");
850 if(type == 0) // server set
852 dprint("Applying temporary setting ", t, " := ", s, "\n");
853 if(cvar("g_campaign"))
854 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
860 dprint("Applying temporary client setting ", t, " := ", s, "\n");
861 MapInfo_Map_clientstuff = strcat(
862 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
868 float MapInfo_isRedundant(string fn, string t)
870 // normalize file name
871 fn = strreplace("_", "-", fn);
873 // normalize visible title
874 t = strreplace(": ", "-", t);
875 t = strreplace(":", "-", t);
876 t = strreplace(" ", "-", t);
877 t = strreplace("_", "-", t);
878 t = strreplace("'", "-", t);
880 if(!strcasecmp(fn, t))
883 // we allow the visible title to have punctuation the file name does
884 // not, but not vice versa
885 t = strreplace("-", "", t);
887 if(!strcasecmp(fn, t))
893 // load info about a map by name into the MapInfo_Map_* globals
894 float MapInfo_Get_ByName_NoFallbacks(string pFilename, int pAllowGenerate, int pGametypeToSet)
903 acl = MAPINFO_SETTEMP_ACL_USER;
905 if(strstrofs(pFilename, "/", 0) >= 0)
907 print("Invalid character in map name, ignored\n");
911 if(pGametypeToSet == 0)
912 if(MapInfo_Cache_Retrieve(pFilename))
917 MapInfo_Map_bspname = pFilename;
919 // default all generic fields so they have "good" values in case something fails
920 fn = strcat("maps/", pFilename, ".mapinfo");
921 fh = fopen(fn, FILE_READ);
924 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
925 fh = fopen(fn, FILE_READ);
930 _MapInfo_Map_Reset();
931 r = _MapInfo_Generate(pFilename);
934 fh = fopen(fn, FILE_WRITE);
935 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
936 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
937 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
938 if(_MapInfo_Map_worldspawn_music != "")
941 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
943 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
945 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
947 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
951 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
952 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
955 i = floor(random() * n);
956 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
959 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
961 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
962 fputs(fh, "has weapons\n");
964 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
965 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
966 fputs(fh, "has turrets\n");
968 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
969 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
970 fputs(fh, "has vehicles\n");
972 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
973 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
974 fputs(fh, "frustrating\n");
976 for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
977 if(MapInfo_Map_supportedGametypes & i)
978 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
980 if(fexists(strcat("scripts/", pFilename, ".arena")))
981 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
983 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
984 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
985 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
986 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
987 fputs(fh, "// optional: hidden\n");
992 fh = fopen(fn, FILE_READ);
994 error("... but I just wrote it!");
997 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
1000 _MapInfo_Map_Reset();
1003 if (!((s = fgets(fh))))
1006 // catch different sorts of comments
1007 if(s == "") // empty lines
1009 if(substring(s, 0, 1) == "#") // UNIX style
1011 if(substring(s, 0, 2) == "//") // C++ style
1013 if(substring(s, 0, 1) == "_") // q3map style
1016 p = strstrofs(s, "//", 0);
1018 s = substring(s, 0, p);
1020 t = car(s); s = cdr(s);
1022 MapInfo_Map_title = s;
1023 else if(t == "description")
1024 MapInfo_Map_description = s;
1025 else if(t == "author")
1026 MapInfo_Map_author = s;
1029 t = car(s); // s = cdr(s);
1030 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1031 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
1032 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
1033 else if(t == "monsters") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
1034 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1036 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
1038 else if(t == "hidden")
1040 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
1042 else if(t == "forbidden")
1044 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
1046 else if(t == "frustrating")
1048 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
1050 else if(t == "noautomaplist")
1052 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1054 else if(t == "type")
1056 t = car(s); s = cdr(s);
1057 f = MapInfo_Type_FromString(t);
1058 dprint("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");
1060 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, true);
1062 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1064 else if(t == "gametype")
1066 t = car(s); s = cdr(s);
1067 f = MapInfo_Type_FromString(t);
1069 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
1071 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1073 else if(t == "size")
1075 float a, b, c, d, e;
1076 t = car(s); s = cdr(s); a = stof(t);
1077 t = car(s); s = cdr(s); b = stof(t);
1078 t = car(s); s = cdr(s); c = stof(t);
1079 t = car(s); s = cdr(s); d = stof(t);
1080 t = car(s); s = cdr(s); e = stof(t);
1082 print("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");
1085 t = car(s); s = cdr(s); f = stof(t);
1087 print("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");
1090 if(a >= d || b >= e || c >= f)
1091 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
1094 MapInfo_Map_mins_x = a;
1095 MapInfo_Map_mins_y = b;
1096 MapInfo_Map_mins_z = c;
1097 MapInfo_Map_maxs_x = d;
1098 MapInfo_Map_maxs_y = e;
1099 MapInfo_Map_maxs_z = f;
1104 else if(t == "settemp_for_type")
1106 t = car(s); s = cdr(s);
1107 if((f = MapInfo_Type_FromString(t)))
1109 if(f & pGametypeToSet)
1111 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1116 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1119 else if(t == "clientsettemp_for_type")
1121 t = car(s); s = cdr(s);
1122 if((f = MapInfo_Type_FromString(t)))
1124 if(f & pGametypeToSet)
1126 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1131 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1136 if (!cvar_value_issafe(s))
1137 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1139 MapInfo_Map_fog = s;
1141 else if(t == "cdtrack")
1143 t = car(s); s = cdr(s);
1144 if(pGametypeToSet) // FIXME is this check right here?
1146 if (!cvar_value_issafe(t))
1147 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1149 MapInfo_Map_clientstuff = strcat(
1150 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1155 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1159 if(MapInfo_Map_title == "<TITLE>")
1160 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1161 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1162 MapInfo_Map_titlestring = MapInfo_Map_title;
1164 MapInfo_Map_titlestring = sprintf("%s: %s", MapInfo_Map_bspname, MapInfo_Map_title);
1166 MapInfo_Cache_Store();
1167 if(MapInfo_Map_supportedGametypes != 0)
1169 dprint("Map ", pFilename, " supports no game types, ignored\n");
1172 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, int pGametypeToSet)
1174 float r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1176 if(cvar("g_tdm_on_dm_maps"))
1178 // if this is set, all DM maps support TDM too
1179 if (!(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH))
1180 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)
1181 _MapInfo_Map_ApplyGametypeEx ("", pGametypeToSet, MAPINFO_TYPE_TEAM_DEATHMATCH);
1186 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1188 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1189 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1197 float MapInfo_FindName(string s)
1199 // if there is exactly one map of prefix s, return it
1200 // if not, return the null string
1201 // note that DP sorts glob results... so I can use a binary search
1205 // invariants: r is behind s, l-1 is equal or before
1208 m = floor((l + r) / 2);
1209 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1210 cmp = strcasecmp(MapInfo_FindName_match, s);
1212 return m; // found and good
1214 l = m + 1; // l-1 is before s
1218 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1219 MapInfo_FindName_firstResult = l;
1220 // r == l, so: l is behind s, l-1 is before
1221 // SO: if there is any, l is the one with the right prefix
1222 // and l+1 may be one too
1223 if(l == MapInfo_count)
1225 MapInfo_FindName_match = string_null;
1226 MapInfo_FindName_firstResult = -1;
1227 return -1; // no MapInfo_FindName_match, behind last item
1229 if(!startsWithNocase(MapInfo_FindName_match, s))
1231 MapInfo_FindName_match = string_null;
1232 MapInfo_FindName_firstResult = -1;
1233 return -1; // wrong prefix
1235 if(l == MapInfo_count - 1)
1236 return l; // last one, nothing can follow => unique
1237 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1239 MapInfo_FindName_match = string_null;
1240 return -1; // ambigous MapInfo_FindName_match
1245 string MapInfo_FixName(string s)
1247 MapInfo_FindName(s);
1248 return MapInfo_FindName_match;
1251 float MapInfo_CurrentFeatures()
1254 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")))
1255 req |= MAPINFO_FEATURE_WEAPONS;
1259 float MapInfo_CurrentGametype()
1262 int prev = cvar("gamecfg");
1263 for(e = MapInfo_Type_first; e; e = e.enemy)
1269 return MAPINFO_TYPE_DEATHMATCH;
1272 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1274 if(!MapInfo_Get_ByName(s, 1, 0))
1276 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1278 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1283 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1286 r = _MapInfo_CheckMap(s);
1287 MapInfo_ClearTemps();
1291 void MapInfo_SwitchGameType(float t)
1294 for(e = MapInfo_Type_first; e; e = e.enemy)
1295 cvar_set(e.netname, (t == e.items) ? "1" : "0");
1298 void MapInfo_LoadMap(string s, float reinit)
1300 MapInfo_Map_supportedGametypes = 0;
1301 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1302 //if(!MapInfo_CheckMap(s))
1304 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1305 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1308 cvar_settemp_restore();
1310 localcmd(strcat("\nmap ", s, "\n"));
1312 localcmd(strcat("\nchangelevel ", s, "\n"));
1315 string MapInfo_ListAllowedMaps(float type, float pRequiredFlags, float pForbiddenFlags)
1320 // to make absolutely sure:
1321 MapInfo_Enumerate();
1322 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1325 for(i = 0; i < MapInfo_count; ++i)
1326 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1327 return substring(out, 1, strlen(out) - 1);
1330 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1335 // to make absolutely sure:
1336 MapInfo_Enumerate();
1337 MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1340 for(i = 0; i < MapInfo_count; ++i)
1341 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1343 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1345 return substring(out, 1, strlen(out) - 1);
1348 void MapInfo_LoadMapSettings_SaveGameType(float t)
1350 MapInfo_SwitchGameType(t);
1351 cvar_set("gamecfg", ftos(t));
1352 MapInfo_LoadedGametype = t;
1355 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1359 t = MapInfo_CurrentGametype();
1360 MapInfo_LoadMapSettings_SaveGameType(t);
1362 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1364 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1366 print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1367 _MapInfo_Map_ApplyGametypeEx("", t, t);
1368 return; // do not call Get_ByName!
1371 if(MapInfo_Map_supportedGametypes == 0)
1373 print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1374 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1375 MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1376 _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1377 return; // do not call Get_ByName!
1381 while(!(MapInfo_Map_supportedGametypes & 1))
1384 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1387 // t is now a supported mode!
1388 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1389 MapInfo_LoadMapSettings_SaveGameType(t);
1391 MapInfo_Get_ByName(s, 1, t);
1394 void MapInfo_ClearTemps()
1396 MapInfo_Map_bspname = string_null;
1397 MapInfo_Map_title = string_null;
1398 MapInfo_Map_titlestring = string_null;
1399 MapInfo_Map_description = string_null;
1400 MapInfo_Map_author = string_null;
1401 MapInfo_Map_clientstuff = string_null;
1402 MapInfo_Map_supportedGametypes = 0;
1403 MapInfo_Map_supportedFeatures = 0;
1406 void MapInfo_Shutdown()
1408 MapInfo_ClearTemps();
1409 MapInfo_Filter_Free();
1410 MapInfo_Cache_Destroy();
1411 if(_MapInfo_globopen)
1413 search_end(_MapInfo_globhandle);
1414 _MapInfo_globhandle = -1;
1415 _MapInfo_globopen = false;
1419 float MapInfo_ForbiddenFlags()
1421 int f = MAPINFO_FLAG_FORBIDDEN;
1424 if (!cvar("g_maplist_allow_hidden"))
1426 f |= MAPINFO_FLAG_HIDDEN;
1428 if (!cvar("g_maplist_allow_frustrating"))
1429 f |= MAPINFO_FLAG_FRUSTRATING;
1434 float MapInfo_RequiredFlags()
1438 if(cvar("g_maplist_allow_frustrating") > 1)
1439 f |= MAPINFO_FLAG_FRUSTRATING;