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_description);
53 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
54 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
55 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
56 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
59 float MapInfo_Cache_Retrieve(string map)
63 if(!_MapInfo_Cache_Active)
66 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
71 // now retrieve all the stuff
72 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
73 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
74 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
75 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
76 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
77 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
78 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
82 // GLOB HANDLING (for all BSP files)
83 float _MapInfo_globopen;
84 float _MapInfo_globcount;
85 float _MapInfo_globhandle;
86 string _MapInfo_GlobItem(float i)
89 s = search_getfilename(_MapInfo_globhandle, i);
90 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
93 void MapInfo_Enumerate()
96 search_end(_MapInfo_globhandle);
97 MapInfo_Cache_Invalidate();
98 _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
99 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
100 _MapInfo_globopen = 1;
103 // filter the info by game type mask (updates MapInfo_count)
105 float _MapInfo_filtered;
106 float _MapInfo_filtered_allocated;
107 float MapInfo_FilterList_Lookup(float i)
109 return stof(bufstr_get(_MapInfo_filtered, i));
112 void _MapInfo_FilterList_swap(float i, float j, entity pass)
115 h = bufstr_get(_MapInfo_filtered, i);
116 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
117 bufstr_set(_MapInfo_filtered, j, h);
120 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
123 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
124 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
125 return strcasecmp(a, b);
128 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
131 if not(_MapInfo_filtered_allocated)
133 _MapInfo_filtered_allocated = 1;
134 _MapInfo_filtered = buf_create();
137 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
139 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.
142 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
143 MapInfo_progress = i / _MapInfo_globcount;
146 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
147 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
148 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
149 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
150 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
152 MapInfo_count = j + 1;
153 MapInfo_ClearTemps();
155 // sometimes the glob isn't sorted nicely, so fix it here...
156 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
161 void MapInfo_Filter_Free()
163 if(_MapInfo_filtered_allocated)
165 buf_del(_MapInfo_filtered);
166 _MapInfo_filtered_allocated = 0;
170 // load info about the i-th map into the MapInfo_Map_* globals
171 string MapInfo_BSPName_ByID(float i)
173 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
176 string unquote(string s)
181 for(i = 0; i < l; ++i)
184 ch = substring(s, i, 1);
185 if(ch != " ") if(ch != "\"")
187 for(j = strlen(s) - i - 1; j > 0; --j)
189 ch = substring(s, i+j, 1);
190 if(ch != " ") if(ch != "\"")
191 return substring(s, i, j+1);
193 return substring(s, i, 1);
199 float MapInfo_Get_ByID(float i)
201 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
206 string _MapInfo_Map_worldspawn_music;
208 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
218 float diameter, spawnpoints;
221 vector mapMins, mapMaxs;
224 fn = strcat("maps/", pFilename, ".ent");
225 fh = fopen(fn, FILE_READ);
229 fn = strcat("maps/", pFilename, ".bsp");
230 fh = fopen(fn, FILE_READ);
234 print("Analyzing ", fn, " to generate initial mapinfo\n");
237 MapInfo_Map_flags = 0;
238 MapInfo_Map_supportedGametypes = 0;
241 _MapInfo_Map_worldspawn_music = "";
245 if not((s = fgets(fh)))
247 if(inWorldspawn == 1)
248 if(startsWith(s, "}"))
254 if(k == "classname" && v == "worldspawn")
256 else if(k == "author")
257 MapInfo_Map_author = v;
258 else if(k == "_description")
259 MapInfo_Map_description = v;
260 else if(k == "music")
261 _MapInfo_Map_worldspawn_music = v;
262 else if(k == "noise")
263 _MapInfo_Map_worldspawn_music = v;
264 else if(k == "message")
266 i = strstrofs(v, " by ", 0);
267 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
269 MapInfo_Map_title = substring(v, 0, i);
270 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
273 MapInfo_Map_title = v;
280 o = stov(strcat("'", v, "'"));
281 mapMins_x = min(mapMins_x, o_x);
282 mapMins_y = min(mapMins_y, o_y);
283 mapMins_z = min(mapMins_z, o_z);
284 mapMaxs_x = max(mapMaxs_x, o_x);
285 mapMaxs_y = max(mapMaxs_y, o_y);
286 mapMaxs_z = max(mapMaxs_z, o_z);
288 else if(k == "race_place")
293 else if(k == "classname")
295 if(v == "dom_controlpoint")
296 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
297 else if(v == "item_flag_team2")
298 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
299 else if(v == "team_CTF_blueflag")
300 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
301 else if(v == "runematch_spawn_point")
302 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
303 else if(v == "target_assault_roundend")
304 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
305 else if(v == "onslaught_generator")
306 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
307 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
308 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
309 else if(v == "info_player_team1")
311 else if(v == "info_player_team2")
313 else if(v == "info_player_start")
315 else if(v == "info_player_deathmatch")
317 else if(v == "trigger_race_checkpoint")
318 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
319 else if(v == "target_startTimer")
320 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
321 else if(v == "weapon_nex")
323 else if(v == "weapon_railgun")
325 else if(startsWith(v, "weapon_"))
326 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
327 else if(v == "target_music" || v == "trigger_music")
328 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
334 print(fn, " ended still in worldspawn, BUG\n");
337 diameter = vlen(mapMaxs - mapMins);
339 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
340 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
342 // we have a CTF-only or Assault-only map. Don't add other modes then,
343 // as the map is too symmetric for them.
347 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
348 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH; // Rune always works
349 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
350 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY; // Keepaway always works
352 if(spawnpoints >= 8 && diameter > 4096) {
353 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
354 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
355 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
358 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
359 if(spawnpoints >= 12 && diameter > 5120)
360 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
363 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
366 MapInfo_Map_supportedGametypes &~= MAPINFO_TYPE_RACE;
367 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
370 dprint("-> diameter ", ftos(diameter));
371 dprint("; spawnpoints ", ftos(spawnpoints));
372 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
379 void _MapInfo_Map_Reset()
381 MapInfo_Map_title = "<TITLE>";
382 MapInfo_Map_description = "<DESCRIPTION>";
383 MapInfo_Map_author = "<AUTHOR>";
384 MapInfo_Map_supportedGametypes = 0;
385 MapInfo_Map_supportedFeatures = 0;
386 MapInfo_Map_flags = 0;
387 MapInfo_Map_clientstuff = "";
388 MapInfo_Map_fog = "";
389 MapInfo_Map_mins = '0 0 0';
390 MapInfo_Map_maxs = '0 0 0';
393 string _MapInfo_GetDefault(float t)
397 case MAPINFO_TYPE_DEATHMATCH: return "30 20 0";
398 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
399 case MAPINFO_TYPE_DOMINATION: return "200 20 0";
400 case MAPINFO_TYPE_CTF: return "300 20 10 0";
401 case MAPINFO_TYPE_RUNEMATCH: return "200 20 0";
402 case MAPINFO_TYPE_LMS: return "9 20 0";
403 case MAPINFO_TYPE_ARENA: return "10 20 0";
404 case MAPINFO_TYPE_CA: return "10 20 0";
405 case MAPINFO_TYPE_KEYHUNT: return "1000 20 3 0";
406 case MAPINFO_TYPE_ASSAULT: return "20 0";
407 case MAPINFO_TYPE_RACE: return "20 5 7 15 0";
408 case MAPINFO_TYPE_ONSLAUGHT: return "20 0";
409 case MAPINFO_TYPE_NEXBALL: return "5 20 0";
410 case MAPINFO_TYPE_CTS: return "20 0 0";
411 case MAPINFO_TYPE_FREEZETAG: return "10 20 0";
412 case MAPINFO_TYPE_KEEPAWAY: return "30 20 0";
417 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, float load_default)
420 MapInfo_Map_supportedGametypes |= pThisType;
421 if(!(pThisType & pWantedType))
425 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, FALSE);
427 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
429 cvar_set("fraglimit", "0");
435 cvar_set("fraglimit", sa);
441 cvar_set("timelimit", sa);
444 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
448 cvar_set("g_tdm_teams", sa);
452 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
456 cvar_set("g_keyhunt_teams", sa);
460 if(pWantedType == MAPINFO_TYPE_CTF)
464 if(cvar("g_ctf_win_mode") < 2)
465 cvar_set("fraglimit", sa);
469 if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
473 cvar_set("fraglimit", sa);
477 // rc = timelimit timelimit_qualification laps laps_teamplay
478 if(pWantedType == MAPINFO_TYPE_RACE)
480 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
481 cvar_set("g_race_qualifying_timelimit", sa);
486 if(cvar("g_race_teams") < 2)
487 cvar_set("fraglimit", sa);
492 if(cvar("g_race_teams") >= 2)
493 cvar_set("fraglimit", sa);
497 if(pWantedType == MAPINFO_TYPE_CTS)
501 // this is the skill of the map
502 // not parsed by anything yet
505 // cvar_set("fraglimit", sa);
510 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
512 cvar_set("leadlimit", "0");
518 cvar_set("leadlimit", sa);
523 string _MapInfo_GetDefaultEx(float t)
527 case MAPINFO_TYPE_DEATHMATCH: return "timelimit=20 pointlimit=30 leadlimit=0";
528 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "timelimit=20 pointlimit=50 teams=2 leadlimit=0";
529 case MAPINFO_TYPE_DOMINATION: return "timelimit=20 pointlimit=200 teams=2 leadlimit=0";
530 case MAPINFO_TYPE_CTF: return "timelimit=20 pointlimit=300 caplimit=10 leadlimit=0";
531 case MAPINFO_TYPE_RUNEMATCH: return "timelimit=20 pointlimit=200 leadlimit=0";
532 case MAPINFO_TYPE_LMS: return "timelimit=20 lives=9 leadlimit=0";
533 case MAPINFO_TYPE_ARENA: return "timelimit=20 pointlimit=10 leadlimit=0";
534 case MAPINFO_TYPE_CA: return "timelimit=20 pointlimit=10 leadlimit=0";
535 case MAPINFO_TYPE_KEYHUNT: return "timelimit=20 pointlimit=1000 teams=3 leadlimit=0";
536 case MAPINFO_TYPE_ASSAULT: return "timelimit=20";
537 case MAPINFO_TYPE_RACE: return "timelimit=20 qualifying_timelimit=5 laplimit=7 teamlaplimit=15 leadlimit=0";
538 case MAPINFO_TYPE_ONSLAUGHT: return "timelimit=20";
539 case MAPINFO_TYPE_NEXBALL: return "timelimit=20 pointlimit=5 leadlimit=0";
540 case MAPINFO_TYPE_CTS: return "timelimit=20 skill=-1";
541 case MAPINFO_TYPE_FREEZETAG: return "timelimit=20 pointlimit=10 teams=2 leadlimit=0";
542 case MAPINFO_TYPE_KEEPAWAY: return "timelimit=20 pointlimit=30";
547 void _MapInfo_Map_ApplyGametypeEx(string s, float pWantedType, float pThisType)
551 string fraglimit_normal;
552 string fraglimit_caps;
553 string fraglimit_teams;
555 MapInfo_Map_supportedGametypes |= pThisType;
556 if(!(pThisType & pWantedType))
559 // reset all the cvars to their defaults
561 cvar_set("timelimit", cvar_defstring("timelimit"));
562 cvar_set("leadlimit", cvar_defstring("leadlimit"));
563 cvar_set("fraglimit", cvar_defstring("fraglimit"));
564 cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
565 cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
566 cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
567 cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
569 fraglimit_normal = string_null;
570 fraglimit_caps = string_null;
571 fraglimit_teams = string_null;
573 s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
582 p = strstrofs(sa, "=", 0);
590 k = substring(sa, 0, p);
591 v = substring(sa, p+1, -1);
596 cvar_set("timelimit", v);
598 else if(k == "leadlimit")
600 cvar_set("leadlimit", v);
602 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit")
604 fraglimit_normal = v;
606 else if(k == "caplimit")
610 else if(k == "teampointlimit" || k == "teamlaplimit")
614 else if(k == "teams")
616 cvar_set("g_tdm_teams", v);
617 cvar_set("g_keyhunt_teams", v);
618 cvar_set("g_domination_default_teams", v);
620 else if(k == "qualifying_timelimit")
622 cvar_set("g_race_qualifying_timelimit", v);
624 else if(k == "skill")
630 print("Invalid gametype key in mapinfo: ", k, "\n");
634 if(pWantedType == MAPINFO_TYPE_CTF && cvar("g_ctf_win_mode") < 2)
637 cvar_set("fraglimit", fraglimit_caps);
639 else if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
642 cvar_set("fraglimit", fraglimit_teams);
647 cvar_set("fraglimit", fraglimit_normal);
651 float MapInfo_Type_FromString(string t)
653 if (t == "dm") return MAPINFO_TYPE_DEATHMATCH;
654 else if(t == "tdm") return MAPINFO_TYPE_TEAM_DEATHMATCH;
655 else if(t == "dom") return MAPINFO_TYPE_DOMINATION;
656 else if(t == "ctf") return MAPINFO_TYPE_CTF;
657 else if(t == "rune") return MAPINFO_TYPE_RUNEMATCH;
658 else if(t == "lms") return MAPINFO_TYPE_LMS;
659 else if(t == "arena") return MAPINFO_TYPE_ARENA;
660 else if(t == "ca") return MAPINFO_TYPE_CA;
661 else if(t == "kh") return MAPINFO_TYPE_KEYHUNT;
662 else if(t == "as") return MAPINFO_TYPE_ASSAULT;
663 else if(t == "ons") return MAPINFO_TYPE_ONSLAUGHT;
664 else if(t == "rc") return MAPINFO_TYPE_RACE;
665 else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
666 else if(t == "cts") return MAPINFO_TYPE_CTS;
667 else if(t == "freezetag") return MAPINFO_TYPE_FREEZETAG;
668 else if(t == "keepaway") return MAPINFO_TYPE_KEEPAWAY;
669 else if(t == "all") return MAPINFO_TYPE_ALL;
673 string MapInfo_Type_ToString(float t)
675 if (t == MAPINFO_TYPE_DEATHMATCH) return "dm";
676 else if(t == MAPINFO_TYPE_TEAM_DEATHMATCH) return "tdm";
677 else if(t == MAPINFO_TYPE_DOMINATION) return "dom";
678 else if(t == MAPINFO_TYPE_CTF) return "ctf";
679 else if(t == MAPINFO_TYPE_RUNEMATCH) return "rune";
680 else if(t == MAPINFO_TYPE_LMS) return "lms";
681 else if(t == MAPINFO_TYPE_ARENA) return "arena";
682 else if(t == MAPINFO_TYPE_CA) return "ca";
683 else if(t == MAPINFO_TYPE_KEYHUNT) return "kh";
684 else if(t == MAPINFO_TYPE_ASSAULT) return "as";
685 else if(t == MAPINFO_TYPE_ONSLAUGHT) return "ons";
686 else if(t == MAPINFO_TYPE_RACE) return "rc";
687 else if(t == MAPINFO_TYPE_NEXBALL) return "nexball";
688 else if(t == MAPINFO_TYPE_CTS) return "cts";
689 else if(t == MAPINFO_TYPE_FREEZETAG) return "freezetag";
690 else if(t == MAPINFO_TYPE_KEEPAWAY) return "keepaway";
691 else if(t == MAPINFO_TYPE_ALL) return "all";
695 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
699 t = car(s); s = cdr(s);
701 // limited support of "" and comments
702 // remove trailing and leading " of t
703 if(substring(t, 0, 1) == "\"")
705 if(substring(t, -1, 1) == "\"")
706 t = substring(t, 1, -2);
709 // remove leading " of s
710 if(substring(s, 0, 1) == "\"")
712 s = substring(s, 1, -1);
714 // remove trailing " of s, and all that follows (cvar description)
715 o = strstrofs(s, "\"", 0);
717 s = substring(s, 0, o);
719 // remove // comments
720 o = strstrofs(s, "//", 0);
722 s = substring(s, 0, o);
724 // remove trailing spaces
725 while(substring(s, -1, 1) == " ")
726 s = substring(s, 0, -2);
732 fh = fopen(s, FILE_READ);
734 print("Map ", pFilename, " references not existing config file ", s, "\n");
739 if not((s = fgets(fh)))
742 // catch different sorts of comments
743 if(s == "") // empty lines
745 if(substring(s, 0, 1) == "#") // UNIX style
747 if(substring(s, 0, 2) == "//") // C++ style
749 if(substring(s, 0, 1) == "_") // q3map style
752 if(substring(s, 0, 4) == "set ")
753 s = substring(s, 4, -1);
754 if(substring(s, 0, 5) == "seta ")
755 s = substring(s, 5, -1);
757 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
763 print("Map ", pFilename, " uses too many levels of inclusion\n");
766 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
767 else if not(cvar_value_issafe(t))
768 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
769 else if not (cvar_value_issafe(s))
770 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
771 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
772 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
773 else if(matchacl(acl, t) <= 0)
774 print("Map ", pFilename, " contains a denied setting, ignored\n");
777 if(type == 0) // server set
779 dprint("Applying temporary setting ", t, " := ", s, "\n");
780 if(cvar("g_campaign"))
781 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
787 dprint("Applying temporary client setting ", t, " := ", s, "\n");
788 MapInfo_Map_clientstuff = strcat(
789 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
795 // load info about a map by name into the MapInfo_Map_* globals
796 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
804 acl = MAPINFO_SETTEMP_ACL_USER;
806 if(strstrofs(pFilename, "/", 0) >= 0)
808 print("Invalid character in map name, ignored\n");
812 if(pGametypeToSet == 0)
813 if(MapInfo_Cache_Retrieve(pFilename))
818 MapInfo_Map_bspname = pFilename;
820 // default all generic fields so they have "good" values in case something fails
821 fn = strcat("maps/", pFilename, ".mapinfo");
822 fh = fopen(fn, FILE_READ);
825 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
826 fh = fopen(fn, FILE_READ);
831 _MapInfo_Map_Reset();
832 r = _MapInfo_Generate(pFilename);
835 fh = fopen(fn, FILE_WRITE);
836 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
837 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
838 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
839 if(_MapInfo_Map_worldspawn_music != "")
842 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
844 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
846 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
848 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
852 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
853 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
856 i = floor(random() * n);
857 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
860 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
862 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
863 fputs(fh, "has weapons\n");
865 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
866 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
867 fputs(fh, "frustrating\n");
869 for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
870 if(MapInfo_Map_supportedGametypes & i)
871 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
873 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
877 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
880 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
881 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
882 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
883 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
884 fputs(fh, "// optional: hidden\n");
889 fh = fopen(fn, FILE_READ);
891 error("... but I just wrote it!");
894 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
897 _MapInfo_Map_Reset();
900 if not((s = fgets(fh)))
903 // catch different sorts of comments
904 if(s == "") // empty lines
906 if(substring(s, 0, 1) == "#") // UNIX style
908 if(substring(s, 0, 2) == "//") // C++ style
910 if(substring(s, 0, 1) == "_") // q3map style
913 p = strstrofs(s, "//", 0);
915 s = substring(s, 0, p);
917 t = car(s); s = cdr(s);
919 MapInfo_Map_title = s;
920 else if(t == "description")
921 MapInfo_Map_description = s;
922 else if(t == "author")
923 MapInfo_Map_author = s;
926 t = car(s); s = cdr(s);
927 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
928 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
930 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
932 else if(t == "hidden")
934 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
936 else if(t == "forbidden")
938 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
940 else if(t == "frustrating")
942 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
946 t = car(s); s = cdr(s);
947 f = MapInfo_Type_FromString(t);
949 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, TRUE);
951 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
953 else if(t == "gametype")
955 t = car(s); s = cdr(s);
956 f = MapInfo_Type_FromString(t);
958 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
960 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
965 t = car(s); s = cdr(s); a = stof(t);
966 t = car(s); s = cdr(s); b = stof(t);
967 t = car(s); s = cdr(s); c = stof(t);
968 t = car(s); s = cdr(s); d = stof(t);
969 t = car(s); s = cdr(s); e = stof(t);
971 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");
974 t = car(s); s = cdr(s); f = stof(t);
976 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");
979 if(a >= d || b >= e || c >= f)
980 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
983 MapInfo_Map_mins_x = a;
984 MapInfo_Map_mins_y = b;
985 MapInfo_Map_mins_z = c;
986 MapInfo_Map_maxs_x = d;
987 MapInfo_Map_maxs_y = e;
988 MapInfo_Map_maxs_z = f;
993 else if(t == "settemp_for_type")
995 t = car(s); s = cdr(s);
996 if((f = MapInfo_Type_FromString(t)))
998 if(f & pGametypeToSet)
1000 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1005 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1008 else if(t == "clientsettemp_for_type")
1010 t = car(s); s = cdr(s);
1011 if((f = MapInfo_Type_FromString(t)))
1013 if(f & pGametypeToSet)
1015 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1020 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1025 if not(cvar_value_issafe(t))
1026 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1028 MapInfo_Map_fog = s;
1030 else if(t == "cdtrack")
1034 if not(cvar_value_issafe(t))
1035 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1037 MapInfo_Map_clientstuff = strcat(
1038 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
1043 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1049 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1051 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1052 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1057 MapInfo_Cache_Store();
1058 if(MapInfo_Map_supportedGametypes != 0)
1060 dprint("Map ", pFilename, " supports no game types, ignored\n");
1064 float MapInfo_FindName(string s)
1066 // if there is exactly one map of prefix s, return it
1067 // if not, return the null string
1068 // note that DP sorts glob results... so I can use a binary search
1072 // invariants: r is behind s, l-1 is equal or before
1075 m = floor((l + r) / 2);
1076 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1077 cmp = strcasecmp(MapInfo_FindName_match, s);
1079 return m; // found and good
1081 l = m + 1; // l-1 is before s
1085 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1086 MapInfo_FindName_firstResult = l;
1087 // r == l, so: l is behind s, l-1 is before
1088 // SO: if there is any, l is the one with the right prefix
1089 // and l+1 may be one too
1090 if(l == MapInfo_count)
1092 MapInfo_FindName_match = string_null;
1093 MapInfo_FindName_firstResult = -1;
1094 return -1; // no MapInfo_FindName_match, behind last item
1096 if(!startsWithNocase(MapInfo_FindName_match, s))
1098 MapInfo_FindName_match = string_null;
1099 MapInfo_FindName_firstResult = -1;
1100 return -1; // wrong prefix
1102 if(l == MapInfo_count - 1)
1103 return l; // last one, nothing can follow => unique
1104 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1106 MapInfo_FindName_match = string_null;
1107 return -1; // ambigous MapInfo_FindName_match
1112 string MapInfo_FixName(string s)
1114 MapInfo_FindName(s);
1115 return MapInfo_FindName_match;
1118 float MapInfo_CurrentFeatures()
1122 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")))
1123 req |= MAPINFO_FEATURE_WEAPONS;
1127 float MapInfo_CurrentGametype()
1129 if(cvar("g_domination"))
1130 return MAPINFO_TYPE_DOMINATION;
1131 else if(cvar("g_ctf"))
1132 return MAPINFO_TYPE_CTF;
1133 else if(cvar("g_runematch"))
1134 return MAPINFO_TYPE_RUNEMATCH;
1135 else if(cvar("g_tdm"))
1136 return MAPINFO_TYPE_TEAM_DEATHMATCH;
1137 else if(cvar("g_assault"))
1138 return MAPINFO_TYPE_ASSAULT;
1139 else if(cvar("g_lms"))
1140 return MAPINFO_TYPE_LMS;
1141 else if(cvar("g_arena"))
1142 return MAPINFO_TYPE_ARENA;
1143 else if(cvar("g_ca"))
1144 return MAPINFO_TYPE_CA;
1145 else if(cvar("g_keyhunt"))
1146 return MAPINFO_TYPE_KEYHUNT;
1147 else if(cvar("g_onslaught"))
1148 return MAPINFO_TYPE_ONSLAUGHT;
1149 else if(cvar("g_race"))
1150 return MAPINFO_TYPE_RACE;
1151 else if(cvar("g_nexball"))
1152 return MAPINFO_TYPE_NEXBALL;
1153 else if(cvar("g_cts"))
1154 return MAPINFO_TYPE_CTS;
1155 else if(cvar("g_freezetag"))
1156 return MAPINFO_TYPE_FREEZETAG;
1157 else if(cvar("g_keepaway"))
1158 return MAPINFO_TYPE_KEEPAWAY;
1160 return MAPINFO_TYPE_DEATHMATCH;
1163 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1165 if(!MapInfo_Get_ByName(s, 1, 0))
1167 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1169 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1174 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1177 r = _MapInfo_CheckMap(s);
1178 MapInfo_ClearTemps();
1182 string MapInfo_GetGameTypeCvar(float t)
1186 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
1187 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
1188 case MAPINFO_TYPE_DOMINATION: return "g_domination";
1189 case MAPINFO_TYPE_CTF: return "g_ctf";
1190 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
1191 case MAPINFO_TYPE_LMS: return "g_lms";
1192 case MAPINFO_TYPE_ARENA: return "g_arena";
1193 case MAPINFO_TYPE_CA: return "g_ca";
1194 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
1195 case MAPINFO_TYPE_ASSAULT: return "g_assault";
1196 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
1197 case MAPINFO_TYPE_RACE: return "g_race";
1198 case MAPINFO_TYPE_NEXBALL: return "g_nexball";
1199 case MAPINFO_TYPE_FREEZETAG: return "g_freezetag";
1200 case MAPINFO_TYPE_CTS: return "g_cts";
1201 case MAPINFO_TYPE_KEEPAWAY: return "g_keepaway";
1206 void MapInfo_SwitchGameType(float t)
1208 cvar_set("gamecfg", "0");
1209 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "1" : "0");
1210 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
1211 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "1" : "0");
1212 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "1" : "0");
1213 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "1" : "0");
1214 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "1" : "0");
1215 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "1" : "0");
1216 cvar_set("g_ca", (t == MAPINFO_TYPE_CA) ? "1" : "0");
1217 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "1" : "0");
1218 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "1" : "0");
1219 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "1" : "0");
1220 cvar_set("g_race", (t == MAPINFO_TYPE_RACE) ? "1" : "0");
1221 cvar_set("g_nexball", (t == MAPINFO_TYPE_NEXBALL) ? "1" : "0");
1222 cvar_set("g_cts", (t == MAPINFO_TYPE_CTS) ? "1" : "0");
1223 cvar_set("g_freezetag", (t == MAPINFO_TYPE_FREEZETAG) ? "1" : "0");
1224 cvar_set("g_keepaway", (t == MAPINFO_TYPE_KEEPAWAY) ? "1" : "0");
1227 void MapInfo_LoadMap(string s)
1229 MapInfo_Map_supportedGametypes = 0;
1230 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1231 //if(!MapInfo_CheckMap(s))
1233 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1234 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1236 localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
1239 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1244 // to make absolutely sure:
1245 MapInfo_Enumerate();
1246 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1249 for(i = 0; i < MapInfo_count; ++i)
1250 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1251 return substring(out, 1, strlen(out) - 1);
1254 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1257 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1259 if(MapInfo_Map_supportedGametypes == 0)
1261 print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1262 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1263 _MapInfo_Map_ApplyGametypeEx("", t0, t0);
1264 return; // do not call Get_ByName!
1268 while(!(MapInfo_Map_supportedGametypes & 1))
1271 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1273 // t is now a supported mode!
1274 t0 = MapInfo_CurrentGametype();
1275 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1277 print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1278 cvar_settemp_restore();
1279 _MapInfo_Map_ApplyGametypeEx("", t0, t0);
1280 return; // do not call Get_ByName!
1284 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1285 MapInfo_SwitchGameType(t);
1288 cvar_settemp_restore();
1289 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
1292 void MapInfo_ClearTemps()
1294 MapInfo_Map_bspname = string_null;
1295 MapInfo_Map_title = string_null;
1296 MapInfo_Map_description = string_null;
1297 MapInfo_Map_author = string_null;
1298 MapInfo_Map_clientstuff = string_null;
1299 MapInfo_Map_supportedGametypes = 0;
1300 MapInfo_Map_supportedFeatures = 0;
1303 void MapInfo_Shutdown()
1305 MapInfo_ClearTemps();
1306 MapInfo_Filter_Free();
1307 MapInfo_Cache_Destroy();
1308 if(_MapInfo_globopen)
1310 search_end(_MapInfo_globhandle);
1311 _MapInfo_globhandle = -1;
1312 _MapInfo_globopen = FALSE;
1316 float MapInfo_ForbiddenFlags()
1319 f = MAPINFO_FLAG_FORBIDDEN;
1322 if not(cvar("g_maplist_allow_hidden"))
1324 f |= MAPINFO_FLAG_HIDDEN;
1326 if not(cvar("g_maplist_allow_frustrating"))
1327 f |= MAPINFO_FLAG_FRUSTRATING;
1332 float MapInfo_RequiredFlags()
1337 if(cvar("g_maplist_allow_frustrating") > 1)
1338 f |= MAPINFO_FLAG_FRUSTRATING;