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);
41 if(!s) // empty string is NOT valid here!
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 s = search_getfilename(_MapInfo_globhandle, i);
93 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
96 void MapInfo_Enumerate()
99 search_end(_MapInfo_globhandle);
100 MapInfo_Cache_Invalidate();
101 _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
102 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
103 _MapInfo_globopen = 1;
106 // filter the info by game type mask (updates MapInfo_count)
108 float _MapInfo_filtered;
109 float _MapInfo_filtered_allocated;
110 float MapInfo_FilterList_Lookup(float i)
112 return stof(bufstr_get(_MapInfo_filtered, i));
115 void _MapInfo_FilterList_swap(float i, float j, entity pass)
118 h = bufstr_get(_MapInfo_filtered, i);
119 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
120 bufstr_set(_MapInfo_filtered, j, h);
123 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
126 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
127 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
128 return strcasecmp(a, b);
131 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
134 if not(_MapInfo_filtered_allocated)
136 _MapInfo_filtered_allocated = 1;
137 _MapInfo_filtered = buf_create();
140 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
142 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.
145 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
146 MapInfo_progress = i / _MapInfo_globcount;
149 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
150 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
151 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
152 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
153 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
155 MapInfo_count = j + 1;
156 MapInfo_ClearTemps();
158 // sometimes the glob isn't sorted nicely, so fix it here...
159 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
164 void MapInfo_Filter_Free()
166 if(_MapInfo_filtered_allocated)
168 buf_del(_MapInfo_filtered);
169 _MapInfo_filtered_allocated = 0;
173 // load info about the i-th map into the MapInfo_Map_* globals
174 string MapInfo_BSPName_ByID(float i)
176 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
179 string unquote(string s)
184 for(i = 0; i < l; ++i)
187 ch = substring(s, i, 1);
188 if(ch != " ") if(ch != "\"")
190 for(j = strlen(s) - i - 1; j > 0; --j)
192 ch = substring(s, i+j, 1);
193 if(ch != " ") if(ch != "\"")
194 return substring(s, i, j+1);
196 return substring(s, i, 1);
202 float MapInfo_Get_ByID(float i)
204 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
209 string _MapInfo_Map_worldspawn_music;
211 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
221 float diameter, spawnpoints;
224 vector mapMins, mapMaxs;
227 fn = strcat("maps/", pFilename, ".ent");
228 fh = fopen(fn, FILE_READ);
232 fn = strcat("maps/", pFilename, ".bsp");
233 fh = fopen(fn, FILE_READ);
237 print("Analyzing ", fn, " to generate initial mapinfo\n");
240 MapInfo_Map_flags = 0;
241 MapInfo_Map_supportedGametypes = 0;
244 _MapInfo_Map_worldspawn_music = "";
248 if not((s = fgets(fh)))
250 if(inWorldspawn == 1)
251 if(startsWith(s, "}"))
257 if(k == "classname" && v == "worldspawn")
259 else if(k == "author")
260 MapInfo_Map_author = v;
261 else if(k == "_description")
262 MapInfo_Map_description = v;
263 else if(k == "music")
264 _MapInfo_Map_worldspawn_music = v;
265 else if(k == "noise")
266 _MapInfo_Map_worldspawn_music = v;
267 else if(k == "message")
269 i = strstrofs(v, " by ", 0);
270 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
272 MapInfo_Map_title = substring(v, 0, i);
273 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
276 MapInfo_Map_title = v;
283 o = stov(strcat("'", v, "'"));
284 mapMins_x = min(mapMins_x, o_x);
285 mapMins_y = min(mapMins_y, o_y);
286 mapMins_z = min(mapMins_z, o_z);
287 mapMaxs_x = max(mapMaxs_x, o_x);
288 mapMaxs_y = max(mapMaxs_y, o_y);
289 mapMaxs_z = max(mapMaxs_z, o_z);
291 else if(k == "race_place")
296 else if(k == "classname")
298 if(v == "dom_controlpoint")
299 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
300 else if(v == "item_flag_team2")
301 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
302 else if(v == "team_CTF_blueflag")
303 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
304 else if(v == "runematch_spawn_point")
305 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
306 else if(v == "target_assault_roundend")
307 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
308 else if(v == "onslaught_generator")
309 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
310 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
311 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
312 else if(v == "info_player_team1")
314 else if(v == "info_player_team2")
316 else if(v == "info_player_start")
318 else if(v == "info_player_deathmatch")
320 else if(v == "trigger_race_checkpoint")
321 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
322 else if(v == "target_startTimer")
323 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
324 else if(v == "weapon_nex")
326 else if(v == "weapon_railgun")
328 else if(startsWith(v, "weapon_"))
329 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
330 else if(startsWith(v, "turret_"))
331 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
332 else if(startsWith(v, "vehicle_"))
333 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
334 else if(v == "target_music" || v == "trigger_music")
335 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
341 print(fn, " ended still in worldspawn, BUG\n");
344 diameter = vlen(mapMaxs - mapMins);
346 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
347 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
349 // we have a CTF-only or Assault-only map. Don't add other modes then,
350 // as the map is too symmetric for them.
354 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
355 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH; // Rune always works
356 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
357 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY; // Keepaway always works
359 if(spawnpoints >= 8 && diameter > 4096) {
360 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
361 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
362 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
365 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
366 if(spawnpoints >= 12 && diameter > 5120)
367 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
370 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
373 MapInfo_Map_supportedGametypes &~= MAPINFO_TYPE_RACE;
374 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
377 dprint("-> diameter ", ftos(diameter));
378 dprint("; spawnpoints ", ftos(spawnpoints));
379 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
386 void _MapInfo_Map_Reset()
388 MapInfo_Map_title = "<TITLE>";
389 MapInfo_Map_titlestring = "<TITLE>";
390 MapInfo_Map_description = "<DESCRIPTION>";
391 MapInfo_Map_author = "<AUTHOR>";
392 MapInfo_Map_supportedGametypes = 0;
393 MapInfo_Map_supportedFeatures = 0;
394 MapInfo_Map_flags = 0;
395 MapInfo_Map_clientstuff = "";
396 MapInfo_Map_fog = "";
397 MapInfo_Map_mins = '0 0 0';
398 MapInfo_Map_maxs = '0 0 0';
401 string _MapInfo_GetDefault(float t)
405 case MAPINFO_TYPE_DEATHMATCH: return "30 20 0";
406 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
407 case MAPINFO_TYPE_DOMINATION: return "200 20 0";
408 case MAPINFO_TYPE_CTF: return "300 20 10 0";
409 case MAPINFO_TYPE_RUNEMATCH: return "200 20 0";
410 case MAPINFO_TYPE_LMS: return "9 20 0";
411 case MAPINFO_TYPE_ARENA: return "10 20 0";
412 case MAPINFO_TYPE_CA: return "10 20 0";
413 case MAPINFO_TYPE_KEYHUNT: return "1000 20 3 0";
414 case MAPINFO_TYPE_ASSAULT: return "20 0";
415 case MAPINFO_TYPE_RACE: return "20 5 7 15 0";
416 case MAPINFO_TYPE_ONSLAUGHT: return "20 0";
417 case MAPINFO_TYPE_NEXBALL: return "5 20 0";
418 case MAPINFO_TYPE_CTS: return "20 0 0";
419 case MAPINFO_TYPE_FREEZETAG: return "10 20 0";
420 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
421 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
422 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
423 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
428 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, float load_default)
431 MapInfo_Map_supportedGametypes |= pThisType;
432 if(!(pThisType & pWantedType))
436 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, FALSE);
438 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
440 cvar_set("fraglimit", "0");
446 cvar_set("fraglimit", sa);
452 cvar_set("timelimit", sa);
455 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
459 cvar_set("g_tdm_teams", sa);
463 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
467 cvar_set("g_keyhunt_teams", sa);
471 if(pWantedType == MAPINFO_TYPE_CTF)
475 cvar_set("fraglimit", sa);
480 if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
484 cvar_set("fraglimit", sa);
489 // rc = timelimit timelimit_qualification laps laps_teamplay
490 if(pWantedType == MAPINFO_TYPE_RACE)
492 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
493 cvar_set("g_race_qualifying_timelimit", sa);
498 if(cvar("g_race_teams") < 2)
499 cvar_set("fraglimit", sa);
504 if(cvar("g_race_teams") >= 2)
505 cvar_set("fraglimit", sa);
509 if(pWantedType == MAPINFO_TYPE_CTS)
513 // this is the skill of the map
514 // not parsed by anything yet
517 // cvar_set("fraglimit", sa);
522 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
524 cvar_set("leadlimit", "0");
530 cvar_set("leadlimit", sa);
535 string _MapInfo_GetDefaultEx(float t)
538 for(e = MapInfo_Type_first; e; e = e.enemy)
544 void _MapInfo_Map_ApplyGametypeEx(string s, float pWantedType, float pThisType)
548 string fraglimit_normal;
549 string fraglimit_teams;
551 MapInfo_Map_supportedGametypes |= pThisType;
552 if(!(pThisType & pWantedType))
555 // reset all the cvars to their defaults
557 cvar_set("timelimit", cvar_defstring("timelimit"));
558 cvar_set("leadlimit", cvar_defstring("leadlimit"));
559 cvar_set("fraglimit", cvar_defstring("fraglimit"));
560 cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
561 cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
562 cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
563 cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
565 fraglimit_normal = string_null;
566 fraglimit_teams = string_null;
568 s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
577 p = strstrofs(sa, "=", 0);
585 k = substring(sa, 0, p);
586 v = substring(sa, p+1, -1);
591 cvar_set("timelimit", v);
593 else if(k == "leadlimit")
595 cvar_set("leadlimit", v);
597 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit" || k == "caplimit")
599 fraglimit_normal = v;
601 else if(k == "teampointlimit" || k == "teamlaplimit")
605 else if(k == "teams")
607 cvar_set("g_tdm_teams", v);
608 cvar_set("g_keyhunt_teams", v);
609 cvar_set("g_domination_default_teams", v);
611 else if(k == "qualifying_timelimit")
613 cvar_set("g_race_qualifying_timelimit", v);
615 else if(k == "skill")
621 print("Invalid gametype key in mapinfo: ", k, "\n");
625 if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
628 cvar_set("fraglimit", fraglimit_teams);
633 cvar_set("fraglimit", fraglimit_normal);
637 float MapInfo_Type_FromString(string t)
642 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
644 print("'. Should use '", t, "'.\n");
648 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
650 print("'. Should use '", t, "'.\n");
654 print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t);
656 print("'. Should use '", t, "'.\n");
659 return MAPINFO_TYPE_ALL;
660 for(e = MapInfo_Type_first; e; e = e.enemy)
666 string MapInfo_Type_ToString(float t)
669 if(t == MAPINFO_TYPE_ALL)
671 for(e = MapInfo_Type_first; e; e = e.enemy)
677 string MapInfo_Type_ToText(float t)
680 for(e = MapInfo_Type_first; e; e = e.enemy)
683 return _("@!#%'n Tuba Throwing");
686 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
690 t = car(s); s = cdr(s);
692 // limited support of "" and comments
693 // remove trailing and leading " of t
694 if(substring(t, 0, 1) == "\"")
696 if(substring(t, -1, 1) == "\"")
697 t = substring(t, 1, -2);
700 // remove leading " of s
701 if(substring(s, 0, 1) == "\"")
703 s = substring(s, 1, -1);
705 // remove trailing " of s, and all that follows (cvar description)
706 o = strstrofs(s, "\"", 0);
708 s = substring(s, 0, o);
710 // remove // comments
711 o = strstrofs(s, "//", 0);
713 s = substring(s, 0, o);
715 // remove trailing spaces
716 while(substring(s, -1, 1) == " ")
717 s = substring(s, 0, -2);
723 fh = fopen(s, FILE_READ);
725 print("Map ", pFilename, " references not existing config file ", s, "\n");
730 if not((s = fgets(fh)))
733 // catch different sorts of comments
734 if(s == "") // empty lines
736 if(substring(s, 0, 1) == "#") // UNIX style
738 if(substring(s, 0, 2) == "//") // C++ style
740 if(substring(s, 0, 1) == "_") // q3map style
743 if(substring(s, 0, 4) == "set ")
744 s = substring(s, 4, -1);
745 if(substring(s, 0, 5) == "seta ")
746 s = substring(s, 5, -1);
748 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
754 print("Map ", pFilename, " uses too many levels of inclusion\n");
757 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
758 else if not(cvar_value_issafe(t))
759 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
760 else if not (cvar_value_issafe(s))
761 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
762 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
763 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
764 else if(matchacl(acl, t) <= 0)
765 print("Map ", pFilename, " contains a denied setting, ignored\n");
768 if(type == 0) // server set
770 dprint("Applying temporary setting ", t, " := ", s, "\n");
771 if(cvar("g_campaign"))
772 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
778 dprint("Applying temporary client setting ", t, " := ", s, "\n");
779 MapInfo_Map_clientstuff = strcat(
780 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
786 float MapInfo_isRedundant(string fn, string t)
788 // normalize file name
789 fn = strreplace("_", "-", fn);
791 // normalize visible title
792 t = strreplace(": ", "-", t);
793 t = strreplace(":", "-", t);
794 t = strreplace(" ", "-", t);
795 t = strreplace("_", "-", t);
796 t = strreplace("'", "-", t);
798 if(!strcasecmp(fn, t))
801 // we allow the visible title to have punctuation the file name does
802 // not, but not vice versa
803 t = strreplace("-", "", t);
805 if(!strcasecmp(fn, t))
811 // load info about a map by name into the MapInfo_Map_* globals
812 float MapInfo_Get_ByName_NoFallbacks(string pFilename, float pAllowGenerate, float pGametypeToSet)
820 acl = MAPINFO_SETTEMP_ACL_USER;
822 if(strstrofs(pFilename, "/", 0) >= 0)
824 print("Invalid character in map name, ignored\n");
828 if(pGametypeToSet == 0)
829 if(MapInfo_Cache_Retrieve(pFilename))
834 MapInfo_Map_bspname = pFilename;
836 // default all generic fields so they have "good" values in case something fails
837 fn = strcat("maps/", pFilename, ".mapinfo");
838 fh = fopen(fn, FILE_READ);
841 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
842 fh = fopen(fn, FILE_READ);
847 _MapInfo_Map_Reset();
848 r = _MapInfo_Generate(pFilename);
851 fh = fopen(fn, FILE_WRITE);
852 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
853 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
854 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
855 if(_MapInfo_Map_worldspawn_music != "")
858 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
860 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
862 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
864 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
868 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
869 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
872 i = floor(random() * n);
873 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
876 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
878 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
879 fputs(fh, "has weapons\n");
881 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
882 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_TURRETS)
883 fputs(fh, "has turrets\n");
885 fputs(fh, "// uncomment this if you added turrets: has turrets\n");
886 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_VEHICLES)
887 fputs(fh, "has weapons\n");
889 fputs(fh, "// uncomment this if you added vehicles: has vehicles\n");
890 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
891 fputs(fh, "frustrating\n");
893 for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
894 if(MapInfo_Map_supportedGametypes & i)
895 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
897 if(fexists(strcat("scripts/", pFilename, ".arena")))
898 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
900 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
901 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
902 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
903 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
904 fputs(fh, "// optional: hidden\n");
909 fh = fopen(fn, FILE_READ);
911 error("... but I just wrote it!");
914 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
917 _MapInfo_Map_Reset();
920 if not((s = fgets(fh)))
923 // catch different sorts of comments
924 if(s == "") // empty lines
926 if(substring(s, 0, 1) == "#") // UNIX style
928 if(substring(s, 0, 2) == "//") // C++ style
930 if(substring(s, 0, 1) == "_") // q3map style
933 p = strstrofs(s, "//", 0);
935 s = substring(s, 0, p);
937 t = car(s); s = cdr(s);
939 MapInfo_Map_title = s;
940 else if(t == "description")
941 MapInfo_Map_description = s;
942 else if(t == "author")
943 MapInfo_Map_author = s;
946 t = car(s); s = cdr(s);
947 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
948 else if(t == "turrets") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_TURRETS;
949 else if(t == "vehicles") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_VEHICLES;
950 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
952 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
954 else if(t == "hidden")
956 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
958 else if(t == "forbidden")
960 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
962 else if(t == "frustrating")
964 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
968 t = car(s); s = cdr(s);
969 f = MapInfo_Type_FromString(t);
970 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");
972 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, TRUE);
974 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
976 else if(t == "gametype")
978 t = car(s); s = cdr(s);
979 f = MapInfo_Type_FromString(t);
981 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
983 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
988 t = car(s); s = cdr(s); a = stof(t);
989 t = car(s); s = cdr(s); b = stof(t);
990 t = car(s); s = cdr(s); c = stof(t);
991 t = car(s); s = cdr(s); d = stof(t);
992 t = car(s); s = cdr(s); e = stof(t);
994 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");
997 t = car(s); s = cdr(s); f = stof(t);
999 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");
1002 if(a >= d || b >= e || c >= f)
1003 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
1006 MapInfo_Map_mins_x = a;
1007 MapInfo_Map_mins_y = b;
1008 MapInfo_Map_mins_z = c;
1009 MapInfo_Map_maxs_x = d;
1010 MapInfo_Map_maxs_y = e;
1011 MapInfo_Map_maxs_z = f;
1016 else if(t == "settemp_for_type")
1018 t = car(s); s = cdr(s);
1019 if((f = MapInfo_Type_FromString(t)))
1021 if(f & pGametypeToSet)
1023 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1028 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1031 else if(t == "clientsettemp_for_type")
1033 t = car(s); s = cdr(s);
1034 if((f = MapInfo_Type_FromString(t)))
1036 if(f & pGametypeToSet)
1038 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1043 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1048 if not(cvar_value_issafe(t))
1049 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1051 MapInfo_Map_fog = s;
1053 else if(t == "cdtrack")
1057 if not(cvar_value_issafe(t))
1058 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1060 MapInfo_Map_clientstuff = strcat(
1061 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
1066 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1070 if(MapInfo_Map_title == "<TITLE>")
1071 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1072 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1073 MapInfo_Map_titlestring = MapInfo_Map_title;
1075 MapInfo_Map_titlestring = sprintf(_("%s: %s"), MapInfo_Map_bspname, MapInfo_Map_title);
1077 MapInfo_Cache_Store();
1078 if(MapInfo_Map_supportedGametypes != 0)
1080 dprint("Map ", pFilename, " supports no game types, ignored\n");
1083 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
1085 float r = MapInfo_Get_ByName_NoFallbacks(pFilename, pAllowGenerate, pGametypeToSet);
1087 if(cvar("g_tdm_on_dm_maps"))
1089 // if this is set, all DM maps support TDM too
1090 if not(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH)
1091 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH)
1092 _MapInfo_Map_ApplyGametypeEx ("", pGametypeToSet, MAPINFO_TYPE_TEAM_DEATHMATCH);
1097 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1099 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1100 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1108 float MapInfo_FindName(string s)
1110 // if there is exactly one map of prefix s, return it
1111 // if not, return the null string
1112 // note that DP sorts glob results... so I can use a binary search
1116 // invariants: r is behind s, l-1 is equal or before
1119 m = floor((l + r) / 2);
1120 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1121 cmp = strcasecmp(MapInfo_FindName_match, s);
1123 return m; // found and good
1125 l = m + 1; // l-1 is before s
1129 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1130 MapInfo_FindName_firstResult = l;
1131 // r == l, so: l is behind s, l-1 is before
1132 // SO: if there is any, l is the one with the right prefix
1133 // and l+1 may be one too
1134 if(l == MapInfo_count)
1136 MapInfo_FindName_match = string_null;
1137 MapInfo_FindName_firstResult = -1;
1138 return -1; // no MapInfo_FindName_match, behind last item
1140 if(!startsWithNocase(MapInfo_FindName_match, s))
1142 MapInfo_FindName_match = string_null;
1143 MapInfo_FindName_firstResult = -1;
1144 return -1; // wrong prefix
1146 if(l == MapInfo_count - 1)
1147 return l; // last one, nothing can follow => unique
1148 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1150 MapInfo_FindName_match = string_null;
1151 return -1; // ambigous MapInfo_FindName_match
1156 string MapInfo_FixName(string s)
1158 MapInfo_FindName(s);
1159 return MapInfo_FindName_match;
1162 float MapInfo_CurrentFeatures()
1166 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")))
1167 req |= MAPINFO_FEATURE_WEAPONS;
1171 float MapInfo_CurrentGametype()
1175 prev = cvar("gamecfg");
1176 for(e = MapInfo_Type_first; e; e = e.enemy)
1182 return MAPINFO_TYPE_DEATHMATCH;
1185 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1187 if(!MapInfo_Get_ByName(s, 1, 0))
1189 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1191 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1196 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1199 r = _MapInfo_CheckMap(s);
1200 MapInfo_ClearTemps();
1204 void MapInfo_SwitchGameType(float t)
1207 for(e = MapInfo_Type_first; e; e = e.enemy)
1208 cvar_set(e.netname, (t == e.items) ? "1" : "0");
1211 void MapInfo_LoadMap(string s, float reinit)
1213 MapInfo_Map_supportedGametypes = 0;
1214 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1215 //if(!MapInfo_CheckMap(s))
1217 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1218 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1221 cvar_settemp_restore();
1223 localcmd(strcat("\nmap ", s, "\n"));
1225 localcmd(strcat("\nchangelevel ", s, "\n"));
1228 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1233 // to make absolutely sure:
1234 MapInfo_Enumerate();
1235 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1238 for(i = 0; i < MapInfo_count; ++i)
1239 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1240 return substring(out, 1, strlen(out) - 1);
1243 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1248 // to make absolutely sure:
1249 MapInfo_Enumerate();
1250 MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1253 for(i = 0; i < MapInfo_count; ++i)
1254 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1256 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1258 return substring(out, 1, strlen(out) - 1);
1261 void MapInfo_LoadMapSettings_SaveGameType(float t)
1263 MapInfo_SwitchGameType(t);
1264 cvar_set("gamecfg", ftos(t));
1265 MapInfo_LoadedGametype = t;
1268 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1272 t = MapInfo_CurrentGametype();
1273 MapInfo_LoadMapSettings_SaveGameType(t);
1275 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1277 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1279 print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1280 _MapInfo_Map_ApplyGametypeEx("", t, t);
1281 return; // do not call Get_ByName!
1284 if(MapInfo_Map_supportedGametypes == 0)
1286 print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1287 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1288 MapInfo_LoadMapSettings_SaveGameType(MAPINFO_TYPE_DEATHMATCH);
1289 _MapInfo_Map_ApplyGametypeEx("", MAPINFO_TYPE_DEATHMATCH, MAPINFO_TYPE_DEATHMATCH);
1290 return; // do not call Get_ByName!
1294 while(!(MapInfo_Map_supportedGametypes & 1))
1297 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1300 // t is now a supported mode!
1301 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1302 MapInfo_LoadMapSettings_SaveGameType(t);
1304 MapInfo_Get_ByName(s, 1, t);
1307 void MapInfo_ClearTemps()
1309 MapInfo_Map_bspname = string_null;
1310 MapInfo_Map_title = string_null;
1311 MapInfo_Map_titlestring = string_null;
1312 MapInfo_Map_description = string_null;
1313 MapInfo_Map_author = string_null;
1314 MapInfo_Map_clientstuff = string_null;
1315 MapInfo_Map_supportedGametypes = 0;
1316 MapInfo_Map_supportedFeatures = 0;
1319 void MapInfo_Shutdown()
1321 MapInfo_ClearTemps();
1322 MapInfo_Filter_Free();
1323 MapInfo_Cache_Destroy();
1324 if(_MapInfo_globopen)
1326 search_end(_MapInfo_globhandle);
1327 _MapInfo_globhandle = -1;
1328 _MapInfo_globopen = FALSE;
1332 float MapInfo_ForbiddenFlags()
1335 f = MAPINFO_FLAG_FORBIDDEN;
1338 if not(cvar("g_maplist_allow_hidden"))
1340 f |= MAPINFO_FLAG_HIDDEN;
1342 if not(cvar("g_maplist_allow_frustrating"))
1343 f |= MAPINFO_FLAG_FRUSTRATING;
1348 float MapInfo_RequiredFlags()
1353 if(cvar("g_maplist_allow_frustrating") > 1)
1354 f |= MAPINFO_FLAG_FRUSTRATING;