1 // generic string stuff
3 float _MapInfo_Cache_Active;
4 float _MapInfo_Cache_DB_NameToIndex;
5 float _MapInfo_Cache_Buf_IndexToMapData;
7 void MapInfo_Cache_Destroy()
9 if(!_MapInfo_Cache_Active)
12 db_close(_MapInfo_Cache_DB_NameToIndex);
13 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
14 _MapInfo_Cache_Active = 0;
17 void MapInfo_Cache_Create()
19 MapInfo_Cache_Destroy();
20 _MapInfo_Cache_DB_NameToIndex = db_create();
21 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
22 _MapInfo_Cache_Active = 1;
25 void MapInfo_Cache_Invalidate()
27 if(!_MapInfo_Cache_Active)
30 MapInfo_Cache_Create();
33 void MapInfo_Cache_Store()
37 if(!_MapInfo_Cache_Active)
40 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
43 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
44 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
49 // now store all the stuff
50 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
51 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
52 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_titlestring);
53 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
54 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
55 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
56 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
57 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
60 float MapInfo_Cache_Retrieve(string map)
64 if(!_MapInfo_Cache_Active)
67 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
72 // now retrieve all the stuff
73 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
74 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
75 MapInfo_Map_titlestring = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
76 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
77 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
78 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
79 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
80 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
85 // GLOB HANDLING (for all BSP files)
86 float _MapInfo_globopen;
87 float _MapInfo_globcount;
88 float _MapInfo_globhandle;
89 string _MapInfo_GlobItem(float i)
92 if(!_MapInfo_globopen)
94 s = search_getfilename(_MapInfo_globhandle, i);
95 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
98 void MapInfo_Enumerate()
100 if(_MapInfo_globopen)
102 search_end(_MapInfo_globhandle);
103 _MapInfo_globopen = 0;
105 MapInfo_Cache_Invalidate();
106 _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
107 if(_MapInfo_globhandle >= 0)
109 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
110 _MapInfo_globopen = 1;
113 _MapInfo_globcount = 0;
116 // filter the info by game type mask (updates MapInfo_count)
118 float _MapInfo_filtered;
119 float _MapInfo_filtered_allocated;
120 float MapInfo_FilterList_Lookup(float i)
122 return stof(bufstr_get(_MapInfo_filtered, i));
125 void _MapInfo_FilterList_swap(float i, float j, entity pass)
128 h = bufstr_get(_MapInfo_filtered, i);
129 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
130 bufstr_set(_MapInfo_filtered, j, h);
133 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
136 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
137 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
138 return strcasecmp(a, b);
141 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
144 if (!_MapInfo_filtered_allocated)
146 _MapInfo_filtered_allocated = 1;
147 _MapInfo_filtered = buf_create();
150 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
152 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.
155 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
156 MapInfo_progress = i / _MapInfo_globcount;
159 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
160 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
161 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
162 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
163 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
165 MapInfo_count = j + 1;
166 MapInfo_ClearTemps();
168 // sometimes the glob isn't sorted nicely, so fix it here...
169 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
174 void MapInfo_Filter_Free()
176 if(_MapInfo_filtered_allocated)
178 buf_del(_MapInfo_filtered);
179 _MapInfo_filtered_allocated = 0;
183 // load info about the i-th map into the MapInfo_Map_* globals
184 string MapInfo_BSPName_ByID(float i)
186 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
189 string unquote(string s)
194 for(i = 0; i < l; ++i)
197 ch = substring(s, i, 1);
198 if(ch != " ") if(ch != "\"")
200 for(j = strlen(s) - i - 1; j > 0; --j)
202 ch = substring(s, i+j, 1);
203 if(ch != " ") if(ch != "\"")
204 return substring(s, i, j+1);
206 return substring(s, i, 1);
212 float MapInfo_Get_ByID(float i)
214 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
219 string _MapInfo_Map_worldspawn_music;
221 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
231 float diameter, spawnpoints;
234 vector mapMins, mapMaxs;
237 fn = strcat("maps/", pFilename, ".ent");
238 fh = fopen(fn, FILE_READ);
242 fn = strcat("maps/", pFilename, ".bsp");
243 fh = fopen(fn, FILE_READ);
247 print("Analyzing ", fn, " to generate initial mapinfo\n");
250 MapInfo_Map_flags = 0;
251 MapInfo_Map_supportedGametypes = 0;
254 _MapInfo_Map_worldspawn_music = "";
260 if (!((s = fgets(fh))))
262 if(inWorldspawn == 1)
263 if(startsWith(s, "}"))
269 if(k == "classname" && v == "worldspawn")
271 else if(k == "author")
272 MapInfo_Map_author = v;
273 else if(k == "_description")
274 MapInfo_Map_description = v;
275 else if(k == "music")
276 _MapInfo_Map_worldspawn_music = v;
277 else if(k == "noise")
278 _MapInfo_Map_worldspawn_music = v;
279 else if(k == "message")
281 i = strstrofs(v, " by ", 0);
282 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
284 MapInfo_Map_title = substring(v, 0, i);
285 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
288 MapInfo_Map_title = v;
295 o = stov(strcat("'", v, "'"));
296 mapMins_x = min(mapMins_x, o_x);
297 mapMins_y = min(mapMins_y, o_y);
298 mapMins_z = min(mapMins_z, o_z);
299 mapMaxs_x = max(mapMaxs_x, o_x);
300 mapMaxs_y = max(mapMaxs_y, o_y);
301 mapMaxs_z = max(mapMaxs_z, o_z);
303 else if(k == "race_place")
308 else if(k == "classname")
310 if(v == "dom_controlpoint")
311 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
312 else if(v == "item_flag_team2")
313 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
314 else if(v == "team_CTF_blueflag")
315 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
316 else if(v == "invasion_spawnpoint")
317 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_INVASION;
318 else if(v == "target_assault_roundend")
319 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
320 else if(v == "onslaught_generator")
321 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
322 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
323 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
324 else if(v == "info_player_team1")
326 else if(v == "info_player_team2")
328 else if(v == "info_player_start")
330 else if(v == "info_player_deathmatch")
332 else if(v == "trigger_race_checkpoint")
333 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
334 else if(v == "target_startTimer")
335 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
336 else if(v == "weapon_nex")
338 else if(v == "weapon_railgun")
340 else if(startsWith(v, "weapon_"))
341 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
342 else if(startsWith(v, "turret_"))
343 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
344 else if(startsWith(v, "vehicle_"))
345 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
346 else if(v == "target_music" || v == "trigger_music")
347 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
353 print(fn, " ended still in worldspawn, BUG\n");
356 diameter = vlen(mapMaxs - mapMins);
358 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
359 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
361 // we have a CTF-only or Assault-only map. Don't add other modes then,
362 // as the map is too symmetric for them.
366 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
367 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
368 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY; // Keepaway always works
370 if(spawnpoints >= 8 && diameter > 4096) {
371 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
372 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
373 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
375 if(spawnpoints >= 12 && diameter > 5120)
376 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
379 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
382 MapInfo_Map_supportedGametypes &= ~MAPINFO_TYPE_RACE;
383 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
386 dprint("-> diameter ", ftos(diameter));
387 dprint("; spawnpoints ", ftos(spawnpoints));
388 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
395 void _MapInfo_Map_Reset()
397 MapInfo_Map_title = "<TITLE>";
398 MapInfo_Map_titlestring = "<TITLE>";
399 MapInfo_Map_description = "<DESCRIPTION>";
400 MapInfo_Map_author = "<AUTHOR>";
401 MapInfo_Map_supportedGametypes = 0;
402 MapInfo_Map_supportedFeatures = 0;
403 MapInfo_Map_flags = 0;
404 MapInfo_Map_clientstuff = "";
405 MapInfo_Map_fog = "";
406 MapInfo_Map_mins = '0 0 0';
407 MapInfo_Map_maxs = '0 0 0';
410 string _MapInfo_GetDefault(float t)
414 case MAPINFO_TYPE_DEATHMATCH: return "30 20 0";
415 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
416 case MAPINFO_TYPE_DOMINATION: return "200 20 0";
417 case MAPINFO_TYPE_CTF: return "300 20 10 0";
418 case MAPINFO_TYPE_LMS: return "9 20 0";
419 case MAPINFO_TYPE_CA: return "10 20 0";
420 case MAPINFO_TYPE_KEYHUNT: return "1000 20 3 0";
421 case MAPINFO_TYPE_ASSAULT: return "20 0";
422 case MAPINFO_TYPE_RACE: return "20 5 7 15 0";
423 case MAPINFO_TYPE_ONSLAUGHT: return "20 0";
424 case MAPINFO_TYPE_NEXBALL: return "5 20 0";
425 case MAPINFO_TYPE_CTS: return "20 0 0";
426 case MAPINFO_TYPE_FREEZETAG: return "10 20 0";
427 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
428 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
429 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
430 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
435 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, float load_default)
438 MapInfo_Map_supportedGametypes |= pThisType;
439 if(!(pThisType & pWantedType))
443 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, FALSE);
445 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
447 cvar_set("fraglimit", "0");
453 cvar_set("fraglimit", sa);
459 cvar_set("timelimit", sa);
462 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
466 cvar_set("g_tdm_teams", sa);
470 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
474 cvar_set("g_keyhunt_teams", sa);
478 if(pWantedType == MAPINFO_TYPE_CA)
482 cvar_set("g_ca_teams", sa);
486 if(pWantedType == MAPINFO_TYPE_FREEZETAG)
490 cvar_set("g_freezetag_teams", sa);
494 if(pWantedType == MAPINFO_TYPE_CTF)
498 cvar_set("fraglimit", sa);
503 if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
507 cvar_set("fraglimit", sa);
512 // rc = timelimit timelimit_qualification laps laps_teamplay
513 if(pWantedType == MAPINFO_TYPE_RACE)
515 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
516 cvar_set("g_race_qualifying_timelimit", sa);
521 if(cvar("g_race_teams") < 2)
522 cvar_set("fraglimit", sa);
527 if(cvar("g_race_teams") >= 2)
528 cvar_set("fraglimit", sa);
532 if(pWantedType == MAPINFO_TYPE_CTS)
536 // this is the skill of the map
537 // not parsed by anything yet
540 // cvar_set("fraglimit", sa);
545 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
547 cvar_set("leadlimit", "0");
553 cvar_set("leadlimit", sa);
558 string _MapInfo_GetDefaultEx(float t)
561 for(e = MapInfo_Type_first; e; e = e.enemy)
567 float _MapInfo_GetTeamPlayBool(float t)
570 for(e = MapInfo_Type_first; e; e = e.enemy)
576 void _MapInfo_Map_ApplyGametypeEx(string s, float pWantedType, float pThisType)
580 string fraglimit_normal;
581 string fraglimit_teams;
583 MapInfo_Map_supportedGametypes |= pThisType;
584 if(!(pThisType & pWantedType))
587 // reset all the cvars to their defaults
589 cvar_set("timelimit", cvar_defstring("timelimit"));
590 cvar_set("leadlimit", cvar_defstring("leadlimit"));
591 cvar_set("fraglimit", cvar_defstring("fraglimit"));
592 cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
593 cvar_set("g_ca_teams", cvar_defstring("g_ca_teams"));
594 cvar_set("g_freezetag_teams", cvar_defstring("g_freezetag_teams"));
595 cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
596 cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
597 cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
599 fraglimit_normal = string_null;
600 fraglimit_teams = string_null;
602 s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
611 p = strstrofs(sa, "=", 0);
614 print("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
617 k = substring(sa, 0, p);
618 v = substring(sa, p+1, -1);
622 cvar_set("timelimit", v);
624 else if(k == "leadlimit")
626 cvar_set("leadlimit", v);
628 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit" || k == "caplimit")
630 fraglimit_normal = v;
632 else if(k == "teampointlimit" || k == "teamlaplimit")
636 else if(k == "teams")
638 cvar_set("g_tdm_teams", v);
639 cvar_set("g_ca_teams", v);
640 cvar_set("g_freezetag_teams", v);
641 cvar_set("g_keyhunt_teams", v);
642 cvar_set("g_domination_default_teams", v);
644 else if(k == "qualifying_timelimit")
646 cvar_set("g_race_qualifying_timelimit", v);
648 else if(k == "skill")
654 print("Invalid gametype setting in mapinfo for gametype ", MapInfo_Type_ToString(pWantedType), ": ", sa, "\n");
658 if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
661 cvar_set("fraglimit", fraglimit_teams);
666 cvar_set("fraglimit", fraglimit_normal);
670 float MapInfo_Type_FromString(string t)
675 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
677 print("'. Should use '", t, "'.\n");
681 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
683 print("'. Should use '", t, "'.\n");
687 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
689 print("'. Should use '", t, "'.\n");
693 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
695 print("'. Should use '", t, "'.\n");
698 return MAPINFO_TYPE_ALL;
699 for(e = MapInfo_Type_first; e; e = e.enemy)
705 string MapInfo_Type_ToString(float t)
708 if(t == MAPINFO_TYPE_ALL)
710 for(e = MapInfo_Type_first; e; e = e.enemy)
716 string MapInfo_Type_ToText(float t)
719 for(e = MapInfo_Type_first; e; e = e.enemy)
722 /* xgettext:no-c-format */
723 return _("@!#%'n Tuba Throwing");
726 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
730 t = car(s); s = cdr(s);
732 // limited support of "" and comments
733 // remove trailing and leading " of t
734 if(substring(t, 0, 1) == "\"")
736 if(substring(t, -1, 1) == "\"")
737 t = substring(t, 1, -2);
740 // remove leading " of s
741 if(substring(s, 0, 1) == "\"")
743 s = substring(s, 1, -1);
745 // remove trailing " of s, and all that follows (cvar description)
746 o = strstrofs(s, "\"", 0);
748 s = substring(s, 0, o);
750 // remove // comments
751 o = strstrofs(s, "//", 0);
753 s = substring(s, 0, o);
755 // remove trailing spaces
756 while(substring(s, -1, 1) == " ")
757 s = substring(s, 0, -2);
763 fh = fopen(s, FILE_READ);
765 print("Map ", pFilename, " references not existing config file ", s, "\n");
770 if (!((s = fgets(fh))))
773 // catch different sorts of comments
774 if(s == "") // empty lines
776 if(substring(s, 0, 1) == "#") // UNIX style
778 if(substring(s, 0, 2) == "//") // C++ style
780 if(substring(s, 0, 1) == "_") // q3map style
783 if(substring(s, 0, 4) == "set ")
784 s = substring(s, 4, -1);
785 if(substring(s, 0, 5) == "seta ")
786 s = substring(s, 5, -1);
788 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
794 print("Map ", pFilename, " uses too many levels of inclusion\n");
797 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
798 else if (!cvar_value_issafe(t))
799 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
800 else if (!cvar_value_issafe(s))
801 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
802 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
803 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
804 else if(matchacl(acl, t) <= 0)
805 print("Map ", pFilename, " contains a denied setting, ignored\n");
808 if(type == 0) // server set
810 dprint("Applying temporary setting ", t, " := ", s, "\n");
811 if(cvar("g_campaign"))
812 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
818 dprint("Applying temporary client setting ", t, " := ", s, "\n");
819 MapInfo_Map_clientstuff = strcat(
820 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
826 float MapInfo_isRedundant(string fn, string t)
828 // normalize file name
829 fn = strreplace("_", "-", fn);
831 // normalize visible title
832 t = strreplace(": ", "-", t);
833 t = strreplace(":", "-", t);
834 t = strreplace(" ", "-", t);
835 t = strreplace("_", "-", t);
836 t = strreplace("'", "-", t);
838 if(!strcasecmp(fn, t))
841 // we allow the visible title to have punctuation the file name does
842 // not, but not vice versa
843 t = strreplace("-", "", t);
845 if(!strcasecmp(fn, t))
851 // load info about a map by name into the MapInfo_Map_* globals
852 float MapInfo_Get_ByName_NoFallbacks(string pFilename, float pAllowGenerate, float pGametypeToSet)
860 acl = MAPINFO_SETTEMP_ACL_USER;
862 if(strstrofs(pFilename, "/", 0) >= 0)
864 print("Invalid character in map name, ignored\n");
868 if(pGametypeToSet == 0)
869 if(MapInfo_Cache_Retrieve(pFilename))
874 MapInfo_Map_bspname = pFilename;
876 // default all generic fields so they have "good" values in case something fails
877 fn = strcat("maps/", pFilename, ".mapinfo");
878 fh = fopen(fn, FILE_READ);
881 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
882 fh = fopen(fn, FILE_READ);
887 _MapInfo_Map_Reset();
888 r = _MapInfo_Generate(pFilename);
891 fh = fopen(fn, FILE_WRITE);
892 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
893 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
894 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
895 if(_MapInfo_Map_worldspawn_music != "")
898 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
900 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
902 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
904 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
908 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
909 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
912 i = floor(random() * n);
913 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
916 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
918 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
919 fputs(fh, "has weapons\n");
921 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
922 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
923 fputs(fh, "has turrets\n");
925 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
926 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
927 fputs(fh, "has vehicles\n");
929 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
930 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
931 fputs(fh, "frustrating\n");
933 for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
934 if(MapInfo_Map_supportedGametypes & i)
935 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
937 if(fexists(strcat("scripts/", pFilename, ".arena")))
938 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
940 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
941 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
942 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
943 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
944 fputs(fh, "// optional: hidden\n");
949 fh = fopen(fn, FILE_READ);
951 error("... but I just wrote it!");
954 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
957 _MapInfo_Map_Reset();
960 if (!((s = fgets(fh))))
963 // catch different sorts of comments
964 if(s == "") // empty lines
966 if(substring(s, 0, 1) == "#") // UNIX style
968 if(substring(s, 0, 2) == "//") // C++ style
970 if(substring(s, 0, 1) == "_") // q3map style
973 p = strstrofs(s, "//", 0);
975 s = substring(s, 0, p);
977 t = car(s); s = cdr(s);
979 MapInfo_Map_title = s;
980 else if(t == "description")
981 MapInfo_Map_description = s;
982 else if(t == "author")
983 MapInfo_Map_author = s;
986 t = car(s); // s = cdr(s);
987 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
988 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
989 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
990 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
992 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
994 else if(t == "hidden")
996 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
998 else if(t == "forbidden")
1000 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
1002 else if(t == "frustrating")
1004 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
1006 else if(t == "noautomaplist")
1008 MapInfo_Map_flags |= MAPINFO_FLAG_NOAUTOMAPLIST;
1010 else if(t == "type")
1012 t = car(s); s = cdr(s);
1013 f = MapInfo_Type_FromString(t);
1014 print("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");
1016 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, TRUE);
1018 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1020 else if(t == "gametype")
1022 t = car(s); s = cdr(s);
1023 f = MapInfo_Type_FromString(t);
1025 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
1027 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
1029 else if(t == "size")
1031 float a, b, c, d, e;
1032 t = car(s); s = cdr(s); a = stof(t);
1033 t = car(s); s = cdr(s); b = stof(t);
1034 t = car(s); s = cdr(s); c = stof(t);
1035 t = car(s); s = cdr(s); d = stof(t);
1036 t = car(s); s = cdr(s); e = stof(t);
1038 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");
1041 t = car(s); s = cdr(s); f = stof(t);
1043 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");
1046 if(a >= d || b >= e || c >= f)
1047 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
1050 MapInfo_Map_mins_x = a;
1051 MapInfo_Map_mins_y = b;
1052 MapInfo_Map_mins_z = c;
1053 MapInfo_Map_maxs_x = d;
1054 MapInfo_Map_maxs_y = e;
1055 MapInfo_Map_maxs_z = f;
1060 else if(t == "settemp_for_type")
1062 t = car(s); s = cdr(s);
1063 if((f = MapInfo_Type_FromString(t)))
1065 if(f & pGametypeToSet)
1067 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1072 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1075 else if(t == "clientsettemp_for_type")
1077 t = car(s); s = cdr(s);
1078 if((f = MapInfo_Type_FromString(t)))
1080 if(f & pGametypeToSet)
1082 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1087 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1092 if (!cvar_value_issafe(s))
1093 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1095 MapInfo_Map_fog = s;
1097 else if(t == "cdtrack")
1099 t = car(s); s = cdr(s);
1100 if(pGametypeToSet) // FIXME is this check right here?
1102 if (!cvar_value_issafe(t))
1103 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1105 MapInfo_Map_clientstuff = strcat(
1106 MapInfo_Map_clientstuff, "cd loop \"", t, "\"\n"
1111 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1115 if(MapInfo_Map_title == "<TITLE>")
1116 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1117 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1118 MapInfo_Map_titlestring = MapInfo_Map_title;
1120 MapInfo_Map_titlestring = sprintf(_("%s: %s"), MapInfo_Map_bspname, MapInfo_Map_title);
1122 MapInfo_Cache_Store();
1123 if(MapInfo_Map_supportedGametypes != 0)
1125 dprint("Map ", pFilename, " supports no game types, ignored\n");
1128 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
1130 float r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1132 if(cvar("g_tdm_on_dm_maps"))
1134 // if this is set, all DM maps support TDM too
1135 if (!(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH))
1136 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)
1137 _MapInfo_Map_ApplyGametypeEx ("", pGametypeToSet, MAPINFO_TYPE_TEAM_DEATHMATCH);
1142 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1144 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1145 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1153 float MapInfo_FindName(string s)
1155 // if there is exactly one map of prefix s, return it
1156 // if not, return the null string
1157 // note that DP sorts glob results... so I can use a binary search
1161 // invariants: r is behind s, l-1 is equal or before
1164 m = floor((l + r) / 2);
1165 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1166 cmp = strcasecmp(MapInfo_FindName_match, s);
1168 return m; // found and good
1170 l = m + 1; // l-1 is before s
1174 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1175 MapInfo_FindName_firstResult = l;
1176 // r == l, so: l is behind s, l-1 is before
1177 // SO: if there is any, l is the one with the right prefix
1178 // and l+1 may be one too
1179 if(l == MapInfo_count)
1181 MapInfo_FindName_match = string_null;
1182 MapInfo_FindName_firstResult = -1;
1183 return -1; // no MapInfo_FindName_match, behind last item
1185 if(!startsWithNocase(MapInfo_FindName_match, s))
1187 MapInfo_FindName_match = string_null;
1188 MapInfo_FindName_firstResult = -1;
1189 return -1; // wrong prefix
1191 if(l == MapInfo_count - 1)
1192 return l; // last one, nothing can follow => unique
1193 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1195 MapInfo_FindName_match = string_null;
1196 return -1; // ambigous MapInfo_FindName_match
1201 string MapInfo_FixName(string s)
1203 MapInfo_FindName(s);
1204 return MapInfo_FindName_match;
1207 float MapInfo_CurrentFeatures()
1211 if(!(cvar("g_lms") || cvar("g_minstagib") || cvar("g_nix") || cvar("g_weaponarena") || !cvar("g_pickup_items") || cvar("g_race") || cvar("g_cts") || cvar("g_nexball")))
1212 req |= MAPINFO_FEATURE_WEAPONS;
1216 float MapInfo_CurrentGametype()
1220 prev = cvar("gamecfg");
1221 for(e = MapInfo_Type_first; e; e = e.enemy)
1227 return MAPINFO_TYPE_DEATHMATCH;
1230 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1232 if(!MapInfo_Get_ByName(s, 1, 0))
1234 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1236 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1241 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1244 r = _MapInfo_CheckMap(s);
1245 MapInfo_ClearTemps();
1249 void MapInfo_SwitchGameType(float t)
1252 for(e = MapInfo_Type_first; e; e = e.enemy)
1253 cvar_set(e.netname, (t == e.items) ? "1" : "0");
1256 void MapInfo_LoadMap(string s, float reinit)
1258 MapInfo_Map_supportedGametypes = 0;
1259 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1260 //if(!MapInfo_CheckMap(s))
1262 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1263 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1266 cvar_settemp_restore();
1268 localcmd(strcat("\nmap ", s, "\n"));
1270 localcmd(strcat("\nchangelevel ", s, "\n"));
1273 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1278 // to make absolutely sure:
1279 MapInfo_Enumerate();
1280 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1283 for(i = 0; i < MapInfo_count; ++i)
1284 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1285 return substring(out, 1, strlen(out) - 1);
1288 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1293 // to make absolutely sure:
1294 MapInfo_Enumerate();
1295 MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1298 for(i = 0; i < MapInfo_count; ++i)
1299 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1301 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1303 return substring(out, 1, strlen(out) - 1);
1306 void MapInfo_LoadMapSettings_SaveGameType(float t)
1308 MapInfo_SwitchGameType(t);
1309 cvar_set("gamecfg", ftos(t));
1310 MapInfo_LoadedGametype = t;
1313 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1317 t = MapInfo_CurrentGametype();
1318 MapInfo_LoadMapSettings_SaveGameType(t);
1320 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1322 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1324 print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1325 _MapInfo_Map_ApplyGametypeEx("", t, t);
1326 return; // do not call Get_ByName!
1329 if(MapInfo_Map_supportedGametypes == 0)
1331 print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1332 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1333 MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1334 _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1335 return; // do not call Get_ByName!
1339 while(!(MapInfo_Map_supportedGametypes & 1))
1342 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1345 // t is now a supported mode!
1346 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1347 MapInfo_LoadMapSettings_SaveGameType(t);
1349 MapInfo_Get_ByName(s, 1, t);
1352 void MapInfo_ClearTemps()
1354 MapInfo_Map_bspname = string_null;
1355 MapInfo_Map_title = string_null;
1356 MapInfo_Map_titlestring = string_null;
1357 MapInfo_Map_description = string_null;
1358 MapInfo_Map_author = string_null;
1359 MapInfo_Map_clientstuff = string_null;
1360 MapInfo_Map_supportedGametypes = 0;
1361 MapInfo_Map_supportedFeatures = 0;
1364 void MapInfo_Shutdown()
1366 MapInfo_ClearTemps();
1367 MapInfo_Filter_Free();
1368 MapInfo_Cache_Destroy();
1369 if(_MapInfo_globopen)
1371 search_end(_MapInfo_globhandle);
1372 _MapInfo_globhandle = -1;
1373 _MapInfo_globopen = FALSE;
1377 float MapInfo_ForbiddenFlags()
1380 f = MAPINFO_FLAG_FORBIDDEN;
1383 if (!cvar("g_maplist_allow_hidden"))
1385 f |= MAPINFO_FLAG_HIDDEN;
1387 if (!cvar("g_maplist_allow_frustrating"))
1388 f |= MAPINFO_FLAG_FRUSTRATING;
1393 float MapInfo_RequiredFlags()
1398 if(cvar("g_maplist_allow_frustrating") > 1)
1399 f |= MAPINFO_FLAG_FRUSTRATING;