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"
14 #include "monsters/monsters.qh"
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 dprint("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 float MapInfo_FilterGametypeAndString(int pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate, string sf)
194 if (!_MapInfo_filtered_allocated)
196 _MapInfo_filtered_allocated = 1;
197 _MapInfo_filtered = buf_create();
200 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
202 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.
205 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
206 MapInfo_progress = i / _MapInfo_globcount;
209 // prepare for keyword filter
210 if (MapInfo_Map_title && strstrofs(MapInfo_Map_title, "<TITLE>", 0) == -1)
211 title = MapInfo_Map_title;
213 title = MapInfo_Map_bspname;
215 if((strstrofs(strtolower(title), strtolower(sf), 0)) >= 0)
216 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
217 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
218 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
219 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
220 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
222 MapInfo_count = j + 1;
223 MapInfo_ClearTemps();
225 // sometimes the glob isn't sorted nicely, so fix it here...
226 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
230 void MapInfo_Filter_Free()
232 if(_MapInfo_filtered_allocated)
234 buf_del(_MapInfo_filtered);
235 _MapInfo_filtered_allocated = 0;
239 // load info about the i-th map into the MapInfo_Map_* globals
240 string MapInfo_BSPName_ByID(float i)
242 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
245 string unquote(string s)
250 for(i = 0; i < l; ++i)
253 ch = substring(s, i, 1);
254 if(ch != " ") if(ch != "\"")
256 for(j = strlen(s) - i - 1; j > 0; --j)
258 ch = substring(s, i+j, 1);
259 if(ch != " ") if(ch != "\"")
260 return substring(s, i, j+1);
262 return substring(s, i, 1);
268 float MapInfo_Get_ByID(float i)
270 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
275 string _MapInfo_Map_worldspawn_music;
277 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
287 float diameter, spawnpoints;
290 vector mapMins, mapMaxs;
293 fn = strcat("maps/", pFilename, ".ent");
294 fh = fopen(fn, FILE_READ);
298 fn = strcat("maps/", pFilename, ".bsp");
299 fh = fopen(fn, FILE_READ);
303 print("Analyzing ", fn, " to generate initial mapinfo\n");
306 MapInfo_Map_flags = 0;
307 MapInfo_Map_supportedGametypes = 0;
310 _MapInfo_Map_worldspawn_music = "";
316 if (!((s = fgets(fh))))
318 if(inWorldspawn == 1)
319 if(startsWith(s, "}"))
325 if(k == "classname" && v == "worldspawn")
327 else if(k == "author")
328 MapInfo_Map_author = v;
329 else if(k == "_description")
330 MapInfo_Map_description = v;
331 else if(k == "music")
332 _MapInfo_Map_worldspawn_music = v;
333 else if(k == "noise")
334 _MapInfo_Map_worldspawn_music = v;
335 else if(k == "message")
337 i = strstrofs(v, " by ", 0);
338 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
340 MapInfo_Map_title = substring(v, 0, i);
341 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
344 MapInfo_Map_title = v;
351 o = stov(strcat("'", v, "'"));
352 mapMins.x = min(mapMins.x, o.x);
353 mapMins.y = min(mapMins.y, o.y);
354 mapMins.z = min(mapMins.z, o.z);
355 mapMaxs.x = max(mapMaxs.x, o.x);
356 mapMaxs.y = max(mapMaxs.y, o.y);
357 mapMaxs.z = max(mapMaxs.z, o.z);
359 else if(k == "race_place")
364 else if(k == "classname")
366 if(v == "dom_controlpoint")
367 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
368 else if(v == "item_flag_team2")
369 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
370 else if(v == "team_CTF_blueflag")
371 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
372 else if(v == "invasion_spawnpoint")
373 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_INVASION;
374 else if(v == "target_assault_roundend")
375 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
376 else if(v == "onslaught_generator")
377 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
378 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
379 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
380 else if(v == "info_player_team1")
382 else if(v == "info_player_team2")
384 else if(v == "info_player_start")
386 else if(v == "info_player_deathmatch")
388 else if(v == "trigger_race_checkpoint")
389 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
390 else if(v == "target_startTimer")
391 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
392 else if(v == "weapon_nex")
394 else if(v == "weapon_railgun")
396 else if(startsWith(v, "weapon_"))
397 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
398 else if(startsWith(v, "turret_"))
399 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
400 else if(startsWith(v, "vehicle_"))
401 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
402 else if(startsWith(v, "monster_"))
403 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
404 else if(v == "target_music" || v == "trigger_music")
405 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
411 print(fn, " ended still in worldspawn, BUG\n");
414 diameter = vlen(mapMaxs - mapMins);
416 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
417 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
419 // we have a CTF-only or Assault-only map. Don't add other modes then,
420 // as the map is too symmetric for them.
424 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
425 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
426 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY; // Keepaway always works
428 if(spawnpoints >= 8 && diameter > 4096) {
429 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
430 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
431 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
433 if(spawnpoints >= 12 && diameter > 5120)
434 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
437 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
440 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE;
441 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
444 dprint("-> diameter ", ftos(diameter));
445 dprint("; spawnpoints ", ftos(spawnpoints));
446 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
453 void _MapInfo_Map_Reset()
455 MapInfo_Map_title = "<TITLE>";
456 MapInfo_Map_titlestring = "<TITLE>";
457 MapInfo_Map_description = "<DESCRIPTION>";
458 MapInfo_Map_author = "<AUTHOR>";
459 MapInfo_Map_supportedGametypes = 0;
460 MapInfo_Map_supportedFeatures = 0;
461 MapInfo_Map_flags = 0;
462 MapInfo_Map_clientstuff = "";
463 MapInfo_Map_fog = "";
464 MapInfo_Map_mins = '0 0 0';
465 MapInfo_Map_maxs = '0 0 0';
468 string _MapInfo_GetDefault(float t)
472 case MAPINFO_TYPE_DEATHMATCH: return "30 20 0";
473 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
474 case MAPINFO_TYPE_DOMINATION: return "200 20 0";
475 case MAPINFO_TYPE_CTF: return "300 20 10 0";
476 case MAPINFO_TYPE_LMS: return "9 20 0";
477 case MAPINFO_TYPE_CA: return "10 20 0";
478 case MAPINFO_TYPE_KEYHUNT: return "1000 20 3 0";
479 case MAPINFO_TYPE_ASSAULT: return "20 0";
480 case MAPINFO_TYPE_RACE: return "20 5 7 15 0";
481 case MAPINFO_TYPE_ONSLAUGHT: return "20 0";
482 case MAPINFO_TYPE_NEXBALL: return "5 20 0";
483 case MAPINFO_TYPE_CTS: return "20 0 0";
484 case MAPINFO_TYPE_FREEZETAG: return "10 20 0";
485 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
486 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
487 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
488 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
493 void _MapInfo_Map_ApplyGametype(string s, int pWantedType, int pThisType, int load_default)
496 MapInfo_Map_supportedGametypes |= pThisType;
497 if(!(pThisType & pWantedType))
501 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, false);
503 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
505 cvar_set("fraglimit", "0");
511 cvar_set("fraglimit", sa);
517 cvar_set("timelimit", sa);
520 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
524 cvar_set("g_tdm_teams", sa);
528 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
532 cvar_set("g_keyhunt_teams", sa);
536 if(pWantedType == MAPINFO_TYPE_CA)
540 cvar_set("g_ca_teams", sa);
544 if(pWantedType == MAPINFO_TYPE_FREEZETAG)
548 cvar_set("g_freezetag_teams", sa);
552 if(pWantedType == MAPINFO_TYPE_CTF)
556 cvar_set("fraglimit", sa);
561 if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
565 cvar_set("fraglimit", sa);
570 // rc = timelimit timelimit_qualification laps laps_teamplay
571 if(pWantedType == MAPINFO_TYPE_RACE)
573 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
574 cvar_set("g_race_qualifying_timelimit", sa);
579 if(cvar("g_race_teams") < 2)
580 cvar_set("fraglimit", sa);
585 if(cvar("g_race_teams") >= 2)
586 cvar_set("fraglimit", sa);
590 if(pWantedType == MAPINFO_TYPE_CTS)
594 // this is the skill of the map
595 // not parsed by anything yet
598 // cvar_set("fraglimit", sa);
603 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
605 cvar_set("leadlimit", "0");
611 cvar_set("leadlimit", sa);
616 string _MapInfo_GetDefaultEx(float t)
619 for(e = MapInfo_Type_first; e; e = e.enemy)
625 float _MapInfo_GetTeamPlayBool(float t)
628 for(e = MapInfo_Type_first; e; e = e.enemy)
634 void _MapInfo_Map_ApplyGametypeEx(string s, int pWantedType, int pThisType)
638 string fraglimit_normal;
639 string fraglimit_teams;
641 MapInfo_Map_supportedGametypes |= pThisType;
642 if(!(pThisType & pWantedType))
645 // reset all the cvars to their defaults
647 cvar_set("timelimit", cvar_defstring("timelimit"));
648 cvar_set("leadlimit", cvar_defstring("leadlimit"));
649 cvar_set("fraglimit", cvar_defstring("fraglimit"));
650 cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
651 cvar_set("g_ca_teams", cvar_defstring("g_ca_teams"));
652 cvar_set("g_freezetag_teams", cvar_defstring("g_freezetag_teams"));
653 cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
654 cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
655 cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
657 fraglimit_normal = string_null;
658 fraglimit_teams = string_null;
660 s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
669 p = strstrofs(sa, "=", 0);
672 print("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
675 k = substring(sa, 0, p);
676 v = substring(sa, p+1, -1);
680 cvar_set("timelimit", v);
682 else if(k == "leadlimit")
684 cvar_set("leadlimit", v);
686 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit" || k == "caplimit")
688 fraglimit_normal = v;
690 else if(k == "teampointlimit" || k == "teamlaplimit")
694 else if(k == "teams")
696 cvar_set("g_tdm_teams", v);
697 cvar_set("g_ca_teams", v);
698 cvar_set("g_freezetag_teams", v);
699 cvar_set("g_keyhunt_teams", v);
700 cvar_set("g_domination_default_teams", v);
701 cvar_set("g_invasion_teams", v);
703 else if(k == "qualifying_timelimit")
705 cvar_set("g_race_qualifying_timelimit", v);
707 else if(k == "skill")
713 print("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
717 if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
720 cvar_set("fraglimit", fraglimit_teams);
725 cvar_set("fraglimit", fraglimit_normal);
729 float MapInfo_Type_FromString(string t)
734 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
736 print("'. Should use '", t, "'.\n");
740 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
742 print("'. Should use '", t, "'.\n");
746 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
748 print("'. Should use '", t, "'.\n");
752 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
754 print("'. Should use '", t, "'.\n");
758 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
760 print("'. Should use '", t, "'.\n");
764 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
766 print("'. Should use '", t, "'.\n");
769 return MAPINFO_TYPE_ALL;
770 for(e = MapInfo_Type_first; e; e = e.enemy)
776 string MapInfo_Type_Description(float t)
779 for(e = MapInfo_Type_first; e; e = e.enemy)
781 return e.gametype_description;
785 string MapInfo_Type_ToString(float t)
788 if(t == MAPINFO_TYPE_ALL)
790 for(e = MapInfo_Type_first; e; e = e.enemy)
796 string MapInfo_Type_ToText(float t)
799 for(e = MapInfo_Type_first; e; e = e.enemy)
802 /* xgettext:no-c-format */
803 return _("@!#%'n Tuba Throwing");
806 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
810 t = car(s); s = cdr(s);
812 // limited support of "" and comments
813 // remove trailing and leading " of t
814 if(substring(t, 0, 1) == "\"")
816 if(substring(t, -1, 1) == "\"")
817 t = substring(t, 1, -2);
820 // remove leading " of s
821 if(substring(s, 0, 1) == "\"")
823 s = substring(s, 1, -1);
825 // remove trailing " of s, and all that follows (cvar description)
826 o = strstrofs(s, "\"", 0);
828 s = substring(s, 0, o);
830 // remove // comments
831 o = strstrofs(s, "//", 0);
833 s = substring(s, 0, o);
835 // remove trailing spaces
836 while(substring(s, -1, 1) == " ")
837 s = substring(s, 0, -2);
843 fh = fopen(s, FILE_READ);
845 print("Map ", pFilename, " references not existing config file ", s, "\n");
850 if (!((s = fgets(fh))))
853 // catch different sorts of comments
854 if(s == "") // empty lines
856 if(substring(s, 0, 1) == "#") // UNIX style
858 if(substring(s, 0, 2) == "//") // C++ style
860 if(substring(s, 0, 1) == "_") // q3map style
863 if(substring(s, 0, 4) == "set ")
864 s = substring(s, 4, -1);
865 if(substring(s, 0, 5) == "seta ")
866 s = substring(s, 5, -1);
868 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
874 print("Map ", pFilename, " uses too many levels of inclusion\n");
877 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
878 else if (!cvar_value_issafe(t))
879 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
880 else if (!cvar_value_issafe(s))
881 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
882 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
883 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
884 else if(matchacl(acl, t) <= 0)
885 print("Map ", pFilename, " contains a denied setting, ignored\n");
888 if(type == 0) // server set
890 dprint("Applying temporary setting ", t, " := ", s, "\n");
891 if(cvar("g_campaign"))
892 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
898 dprint("Applying temporary client setting ", t, " := ", s, "\n");
899 MapInfo_Map_clientstuff = strcat(
900 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
906 float MapInfo_isRedundant(string fn, string t)
908 // normalize file name
909 fn = strreplace("_", "-", fn);
911 // normalize visible title
912 t = strreplace(": ", "-", t);
913 t = strreplace(":", "-", t);
914 t = strreplace(" ", "-", t);
915 t = strreplace("_", "-", t);
916 t = strreplace("'", "-", t);
918 if(!strcasecmp(fn, t))
921 // we allow the visible title to have punctuation the file name does
922 // not, but not vice versa
923 t = strreplace("-", "", t);
925 if(!strcasecmp(fn, t))
931 // load info about a map by name into the MapInfo_Map_* globals
932 float MapInfo_Get_ByName_NoFallbacks(string pFilename, int pAllowGenerate, int pGametypeToSet)
941 acl = MAPINFO_SETTEMP_ACL_USER;
943 if(strstrofs(pFilename, "/", 0) >= 0)
945 print("Invalid character in map name, ignored\n");
949 if(pGametypeToSet == 0)
950 if(MapInfo_Cache_Retrieve(pFilename))
955 MapInfo_Map_bspname = pFilename;
957 // default all generic fields so they have "good" values in case something fails
958 fn = strcat("maps/", pFilename, ".mapinfo");
959 fh = fopen(fn, FILE_READ);
962 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
963 fh = fopen(fn, FILE_READ);
968 _MapInfo_Map_Reset();
969 r = _MapInfo_Generate(pFilename);
972 fh = fopen(fn, FILE_WRITE);
973 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
974 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
975 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
976 if(_MapInfo_Map_worldspawn_music != "")
979 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
981 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
983 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
985 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
989 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
990 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
993 i = floor(random() * n);
994 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
997 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
999 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
1000 fputs(fh, "has weapons\n");
1002 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
1003 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
1004 fputs(fh, "has turrets\n");
1006 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
1007 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
1008 fputs(fh, "has vehicles\n");
1010 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
1011 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
1012 fputs(fh, "frustrating\n");
1014 for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
1015 if(MapInfo_Map_supportedGametypes & i)
1016 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
1018 if(fexists(strcat("scripts/", pFilename, ".arena")))
1019 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
1021 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
1022 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
1023 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
1024 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
1025 fputs(fh, "// optional: hidden\n");
1030 fh = fopen(fn, FILE_READ);
1032 error("... but I just wrote it!");
1035 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
1038 _MapInfo_Map_Reset();
1041 if (!((s = fgets(fh))))
1044 // catch different sorts of comments
1045 if(s == "") // empty lines
1047 if(substring(s, 0, 1) == "#") // UNIX style
1049 if(substring(s, 0, 2) == "//") // C++ style
1051 if(substring(s, 0, 1) == "_") // q3map style
1054 p = strstrofs(s, "//", 0);
1056 s = substring(s, 0, p);
1058 t = car(s); s = cdr(s);
1060 MapInfo_Map_title = s;
1061 else if(t == "description")
1062 MapInfo_Map_description = s;
1063 else if(t == "author")
1064 MapInfo_Map_author = s;
1067 t = car(s); // s = cdr(s);
1068 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1069 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
1070 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
1071 else if(t == "monsters") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_MONSTERS;
1072 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
1074 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
1076 else if(t == "hidden")
1078 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
1080 else if(t == "forbidden")
1082 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
1084 else if(t == "frustrating")
1086 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
1088 else if(t == "noautomaplist")
1090 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1092 else if(t == "type")
1094 t = car(s); s = cdr(s);
1095 f = MapInfo_Type_FromString(t);
1096 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");
1098 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, true);
1100 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1102 else if(t == "gametype")
1104 t = car(s); s = cdr(s);
1105 f = MapInfo_Type_FromString(t);
1107 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
1109 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1111 else if(t == "size")
1113 float a, b, c, d, e;
1114 t = car(s); s = cdr(s); a = stof(t);
1115 t = car(s); s = cdr(s); b = stof(t);
1116 t = car(s); s = cdr(s); c = stof(t);
1117 t = car(s); s = cdr(s); d = stof(t);
1118 t = car(s); s = cdr(s); e = stof(t);
1120 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");
1123 t = car(s); s = cdr(s); f = stof(t);
1125 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");
1128 if(a >= d || b >= e || c >= f)
1129 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
1132 MapInfo_Map_mins.x = a;
1133 MapInfo_Map_mins.y = b;
1134 MapInfo_Map_mins.z = c;
1135 MapInfo_Map_maxs.x = d;
1136 MapInfo_Map_maxs.y = e;
1137 MapInfo_Map_maxs.z = f;
1142 else if(t == "settemp_for_type")
1144 t = car(s); s = cdr(s);
1145 if((f = MapInfo_Type_FromString(t)))
1147 if(f & pGametypeToSet)
1149 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1154 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1157 else if(t == "clientsettemp_for_type")
1159 t = car(s); s = cdr(s);
1160 if((f = MapInfo_Type_FromString(t)))
1162 if(f & pGametypeToSet)
1164 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1169 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1174 if (!cvar_value_issafe(s))
1175 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1177 MapInfo_Map_fog = s;
1179 else if(t == "cdtrack")
1181 t = car(s); s = cdr(s);
1182 if(pGametypeToSet) // FIXME is this check right here?
1184 if (!cvar_value_issafe(t))
1185 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1187 MapInfo_Map_clientstuff = strcat(
1188 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1193 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1197 if(MapInfo_Map_title == "<TITLE>")
1198 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1199 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1200 MapInfo_Map_titlestring = MapInfo_Map_title;
1202 MapInfo_Map_titlestring = sprintf("%s: %s", MapInfo_Map_bspname, MapInfo_Map_title);
1204 MapInfo_Cache_Store();
1205 if(MapInfo_Map_supportedGametypes != 0)
1207 dprint("Map ", pFilename, " supports no game types, ignored\n");
1210 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, int pGametypeToSet)
1212 float r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1214 if(cvar("g_tdm_on_dm_maps"))
1216 // if this is set, all DM maps support TDM too
1217 if (!(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH))
1218 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)
1219 _MapInfo_Map_ApplyGametypeEx ("", pGametypeToSet, MAPINFO_TYPE_TEAM_DEATHMATCH);
1224 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1226 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1227 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1235 float MapInfo_FindName(string s)
1237 // if there is exactly one map of prefix s, return it
1238 // if not, return the null string
1239 // note that DP sorts glob results... so I can use a binary search
1243 // invariants: r is behind s, l-1 is equal or before
1246 m = floor((l + r) / 2);
1247 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1248 cmp = strcasecmp(MapInfo_FindName_match, s);
1250 return m; // found and good
1252 l = m + 1; // l-1 is before s
1256 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1257 MapInfo_FindName_firstResult = l;
1258 // r == l, so: l is behind s, l-1 is before
1259 // SO: if there is any, l is the one with the right prefix
1260 // and l+1 may be one too
1261 if(l == MapInfo_count)
1263 MapInfo_FindName_match = string_null;
1264 MapInfo_FindName_firstResult = -1;
1265 return -1; // no MapInfo_FindName_match, behind last item
1267 if(!startsWithNocase(MapInfo_FindName_match, s))
1269 MapInfo_FindName_match = string_null;
1270 MapInfo_FindName_firstResult = -1;
1271 return -1; // wrong prefix
1273 if(l == MapInfo_count - 1)
1274 return l; // last one, nothing can follow => unique
1275 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1277 MapInfo_FindName_match = string_null;
1278 return -1; // ambigous MapInfo_FindName_match
1283 string MapInfo_FixName(string s)
1285 MapInfo_FindName(s);
1286 return MapInfo_FindName_match;
1289 int MapInfo_CurrentFeatures()
1292 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")))
1293 req |= MAPINFO_FEATURE_WEAPONS;
1297 int MapInfo_CurrentGametype()
1300 int prev = cvar("gamecfg");
1301 for(e = MapInfo_Type_first; e; e = e.enemy)
1307 return MAPINFO_TYPE_DEATHMATCH;
1310 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1312 if(!MapInfo_Get_ByName(s, 1, 0))
1314 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1316 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1321 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1324 r = _MapInfo_CheckMap(s);
1325 MapInfo_ClearTemps();
1329 void MapInfo_SwitchGameType(int t)
1331 for (entity e = MapInfo_Type_first; e; e = e.enemy) {
1332 cvar_set(e.netname, (t == e.items) ? "1" : "0");
1336 void MapInfo_LoadMap(string s, float reinit)
1338 MapInfo_Map_supportedGametypes = 0;
1339 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1340 //if(!MapInfo_CheckMap(s))
1342 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1343 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1346 cvar_settemp_restore();
1348 localcmd(strcat("\nmap ", s, "\n"));
1350 localcmd(strcat("\nchangelevel ", s, "\n"));
1353 string MapInfo_ListAllowedMaps(float type, float pRequiredFlags, float pForbiddenFlags)
1358 // to make absolutely sure:
1359 MapInfo_Enumerate();
1360 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1363 for(i = 0; i < MapInfo_count; ++i)
1364 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1365 return substring(out, 1, strlen(out) - 1);
1368 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1373 // to make absolutely sure:
1374 MapInfo_Enumerate();
1375 MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1378 for(i = 0; i < MapInfo_count; ++i)
1379 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1381 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1383 return substring(out, 1, strlen(out) - 1);
1386 void MapInfo_LoadMapSettings_SaveGameType(float t)
1388 MapInfo_SwitchGameType(t);
1389 cvar_set("gamecfg", ftos(t));
1390 MapInfo_LoadedGametype = t;
1393 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1397 t = MapInfo_CurrentGametype();
1398 MapInfo_LoadMapSettings_SaveGameType(t);
1400 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1402 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1404 print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1405 _MapInfo_Map_ApplyGametypeEx("", t, t);
1406 return; // do not call Get_ByName!
1409 if(MapInfo_Map_supportedGametypes == 0)
1411 print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1412 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1413 MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1414 _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1415 return; // do not call Get_ByName!
1419 while(!(MapInfo_Map_supportedGametypes & 1))
1422 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1425 // t is now a supported mode!
1426 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1427 MapInfo_LoadMapSettings_SaveGameType(t);
1429 MapInfo_Get_ByName(s, 1, t);
1432 void MapInfo_ClearTemps()
1434 MapInfo_Map_bspname = string_null;
1435 MapInfo_Map_title = string_null;
1436 MapInfo_Map_titlestring = string_null;
1437 MapInfo_Map_description = string_null;
1438 MapInfo_Map_author = string_null;
1439 MapInfo_Map_clientstuff = string_null;
1440 MapInfo_Map_supportedGametypes = 0;
1441 MapInfo_Map_supportedFeatures = 0;
1444 void MapInfo_Shutdown()
1446 MapInfo_ClearTemps();
1447 MapInfo_Filter_Free();
1448 MapInfo_Cache_Destroy();
1449 if(_MapInfo_globopen)
1451 search_end(_MapInfo_globhandle);
1452 _MapInfo_globhandle = -1;
1453 _MapInfo_globopen = false;
1457 int MapInfo_ForbiddenFlags()
1459 int f = MAPINFO_FLAG_FORBIDDEN;
1462 if (!cvar("g_maplist_allow_hidden"))
1464 f |= MAPINFO_FLAG_HIDDEN;
1466 if (!cvar("g_maplist_allow_frustrating"))
1467 f |= MAPINFO_FLAG_FRUSTRATING;
1472 int MapInfo_RequiredFlags()
1476 if(cvar("g_maplist_allow_frustrating") > 1)
1477 f |= MAPINFO_FLAG_FRUSTRATING;