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));
84 // GLOB HANDLING (for all BSP files)
85 float _MapInfo_globopen;
86 float _MapInfo_globcount;
87 float _MapInfo_globhandle;
88 string _MapInfo_GlobItem(float i)
91 s = search_getfilename(_MapInfo_globhandle, i);
92 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
95 void MapInfo_Enumerate()
98 search_end(_MapInfo_globhandle);
99 MapInfo_Cache_Invalidate();
100 _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
101 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
102 _MapInfo_globopen = 1;
105 // filter the info by game type mask (updates MapInfo_count)
107 float _MapInfo_filtered;
108 float _MapInfo_filtered_allocated;
109 float MapInfo_FilterList_Lookup(float i)
111 return stof(bufstr_get(_MapInfo_filtered, i));
114 void _MapInfo_FilterList_swap(float i, float j, entity pass)
117 h = bufstr_get(_MapInfo_filtered, i);
118 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
119 bufstr_set(_MapInfo_filtered, j, h);
122 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
125 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
126 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
127 return strcasecmp(a, b);
130 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
133 if not(_MapInfo_filtered_allocated)
135 _MapInfo_filtered_allocated = 1;
136 _MapInfo_filtered = buf_create();
139 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
141 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.
144 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
145 MapInfo_progress = i / _MapInfo_globcount;
148 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
149 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
150 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
151 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
152 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
154 MapInfo_count = j + 1;
155 MapInfo_ClearTemps();
157 // sometimes the glob isn't sorted nicely, so fix it here...
158 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
163 void MapInfo_Filter_Free()
165 if(_MapInfo_filtered_allocated)
167 buf_del(_MapInfo_filtered);
168 _MapInfo_filtered_allocated = 0;
172 // load info about the i-th map into the MapInfo_Map_* globals
173 string MapInfo_BSPName_ByID(float i)
175 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
178 string unquote(string s)
183 for(i = 0; i < l; ++i)
186 ch = substring(s, i, 1);
187 if(ch != " ") if(ch != "\"")
189 for(j = strlen(s) - i - 1; j > 0; --j)
191 ch = substring(s, i+j, 1);
192 if(ch != " ") if(ch != "\"")
193 return substring(s, i, j+1);
195 return substring(s, i, 1);
201 float MapInfo_Get_ByID(float i)
203 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
208 string _MapInfo_Map_worldspawn_music;
210 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
220 float diameter, spawnpoints;
223 vector mapMins, mapMaxs;
226 fn = strcat("maps/", pFilename, ".ent");
227 fh = fopen(fn, FILE_READ);
231 fn = strcat("maps/", pFilename, ".bsp");
232 fh = fopen(fn, FILE_READ);
236 print("Analyzing ", fn, " to generate initial mapinfo\n");
239 MapInfo_Map_flags = 0;
240 MapInfo_Map_supportedGametypes = 0;
243 _MapInfo_Map_worldspawn_music = "";
247 if not((s = fgets(fh)))
249 if(inWorldspawn == 1)
250 if(startsWith(s, "}"))
256 if(k == "classname" && v == "worldspawn")
258 else if(k == "author")
259 MapInfo_Map_author = v;
260 else if(k == "_description")
261 MapInfo_Map_description = v;
262 else if(k == "music")
263 _MapInfo_Map_worldspawn_music = v;
264 else if(k == "noise")
265 _MapInfo_Map_worldspawn_music = v;
266 else if(k == "message")
268 i = strstrofs(v, " by ", 0);
269 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
271 MapInfo_Map_title = substring(v, 0, i);
272 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
275 MapInfo_Map_title = v;
282 o = stov(strcat("'", v, "'"));
283 mapMins_x = min(mapMins_x, o_x);
284 mapMins_y = min(mapMins_y, o_y);
285 mapMins_z = min(mapMins_z, o_z);
286 mapMaxs_x = max(mapMaxs_x, o_x);
287 mapMaxs_y = max(mapMaxs_y, o_y);
288 mapMaxs_z = max(mapMaxs_z, o_z);
290 else if(k == "race_place")
295 else if(k == "classname")
297 if(v == "dom_controlpoint")
298 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
299 else if(v == "item_flag_team2")
300 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
301 else if(v == "team_CTF_blueflag")
302 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
303 else if(v == "runematch_spawn_point")
304 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
305 else if(v == "target_assault_roundend")
306 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
307 else if(v == "onslaught_generator")
308 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
309 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
310 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
311 else if(v == "info_player_team1")
313 else if(v == "info_player_team2")
315 else if(v == "info_player_start")
317 else if(v == "info_player_deathmatch")
319 else if(v == "trigger_race_checkpoint")
320 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
321 else if(v == "target_startTimer")
322 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
323 else if(v == "weapon_nex")
325 else if(v == "weapon_railgun")
327 else if(startsWith(v, "weapon_"))
328 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
329 else if(v == "target_music" || v == "trigger_music")
330 _MapInfo_Map_worldspawn_music = string_null; // don't use regular BGM
336 print(fn, " ended still in worldspawn, BUG\n");
339 diameter = vlen(mapMaxs - mapMins);
341 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
342 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
344 // we have a CTF-only or Assault-only map. Don't add other modes then,
345 // as the map is too symmetric for them.
349 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
350 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH; // Rune always works
351 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
352 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY; // Keepaway always works
354 if(spawnpoints >= 8 && diameter > 4096) {
355 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
356 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
357 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
360 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
361 if(spawnpoints >= 12 && diameter > 5120)
362 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
365 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
368 MapInfo_Map_supportedGametypes &~= MAPINFO_TYPE_RACE;
369 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
372 dprint("-> diameter ", ftos(diameter));
373 dprint("; spawnpoints ", ftos(spawnpoints));
374 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
381 void _MapInfo_Map_Reset()
383 MapInfo_Map_title = "<TITLE>";
384 MapInfo_Map_titlestring = "<TITLE>";
385 MapInfo_Map_description = "<DESCRIPTION>";
386 MapInfo_Map_author = "<AUTHOR>";
387 MapInfo_Map_supportedGametypes = 0;
388 MapInfo_Map_supportedFeatures = 0;
389 MapInfo_Map_flags = 0;
390 MapInfo_Map_clientstuff = "";
391 MapInfo_Map_fog = "";
392 MapInfo_Map_mins = '0 0 0';
393 MapInfo_Map_maxs = '0 0 0';
396 string _MapInfo_GetDefault(float t)
400 case MAPINFO_TYPE_DEATHMATCH: return "30 20 0";
401 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "50 20 2 0";
402 case MAPINFO_TYPE_DOMINATION: return "200 20 0";
403 case MAPINFO_TYPE_CTF: return "300 20 10 0";
404 case MAPINFO_TYPE_RUNEMATCH: return "200 20 0";
405 case MAPINFO_TYPE_LMS: return "9 20 0";
406 case MAPINFO_TYPE_ARENA: return "10 20 0";
407 case MAPINFO_TYPE_CA: return "10 20 0";
408 case MAPINFO_TYPE_KEYHUNT: return "1000 20 3 0";
409 case MAPINFO_TYPE_ASSAULT: return "20 0";
410 case MAPINFO_TYPE_RACE: return "20 5 7 15 0";
411 case MAPINFO_TYPE_ONSLAUGHT: return "20 0";
412 case MAPINFO_TYPE_NEXBALL: return "5 20 0";
413 case MAPINFO_TYPE_CTS: return "20 0 0";
414 case MAPINFO_TYPE_FREEZETAG: return "10 20 0";
415 // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
416 // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
417 // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
418 // THIS FUNCTION WILL EVENTUALLY BE REMOVED
423 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, float load_default)
426 MapInfo_Map_supportedGametypes |= pThisType;
427 if(!(pThisType & pWantedType))
431 _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, FALSE);
433 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
435 cvar_set("fraglimit", "0");
441 cvar_set("fraglimit", sa);
447 cvar_set("timelimit", sa);
450 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
454 cvar_set("g_tdm_teams", sa);
458 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
462 cvar_set("g_keyhunt_teams", sa);
466 if(pWantedType == MAPINFO_TYPE_CTF)
470 if(cvar("g_ctf_win_mode") < 2)
471 cvar_set("fraglimit", sa);
476 if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
480 cvar_set("fraglimit", sa);
485 // rc = timelimit timelimit_qualification laps laps_teamplay
486 if(pWantedType == MAPINFO_TYPE_RACE)
488 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
489 cvar_set("g_race_qualifying_timelimit", sa);
494 if(cvar("g_race_teams") < 2)
495 cvar_set("fraglimit", sa);
500 if(cvar("g_race_teams") >= 2)
501 cvar_set("fraglimit", sa);
505 if(pWantedType == MAPINFO_TYPE_CTS)
509 // this is the skill of the map
510 // not parsed by anything yet
513 // cvar_set("fraglimit", sa);
518 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
520 cvar_set("leadlimit", "0");
526 cvar_set("leadlimit", sa);
531 string _MapInfo_GetDefaultEx(float t)
535 case MAPINFO_TYPE_DEATHMATCH: return "timelimit=20 pointlimit=30 leadlimit=0";
536 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "timelimit=20 pointlimit=50 teams=2 leadlimit=0";
537 case MAPINFO_TYPE_DOMINATION: return "timelimit=20 pointlimit=200 teams=2 leadlimit=0";
538 case MAPINFO_TYPE_CTF: return "timelimit=20 pointlimit=300 caplimit=10 leadlimit=0";
539 case MAPINFO_TYPE_RUNEMATCH: return "timelimit=20 pointlimit=200 leadlimit=0";
540 case MAPINFO_TYPE_LMS: return "timelimit=20 lives=9 leadlimit=0";
541 case MAPINFO_TYPE_ARENA: return "timelimit=20 pointlimit=10 leadlimit=0";
542 case MAPINFO_TYPE_CA: return "timelimit=20 pointlimit=10 leadlimit=0";
543 case MAPINFO_TYPE_KEYHUNT: return "timelimit=20 pointlimit=1000 teams=3 leadlimit=0";
544 case MAPINFO_TYPE_ASSAULT: return "timelimit=20";
545 case MAPINFO_TYPE_RACE: return "timelimit=20 qualifying_timelimit=5 laplimit=7 teamlaplimit=15 leadlimit=0";
546 case MAPINFO_TYPE_ONSLAUGHT: return "timelimit=20";
547 case MAPINFO_TYPE_NEXBALL: return "timelimit=20 pointlimit=5 leadlimit=0";
548 case MAPINFO_TYPE_CTS: return "timelimit=20 skill=-1";
549 case MAPINFO_TYPE_FREEZETAG: return "timelimit=20 pointlimit=10 teams=2 leadlimit=0";
550 case MAPINFO_TYPE_KEEPAWAY: return "timelimit=20 pointlimit=30";
555 void _MapInfo_Map_ApplyGametypeEx(string s, float pWantedType, float pThisType)
559 string fraglimit_normal;
560 string fraglimit_caps;
561 string fraglimit_teams;
563 MapInfo_Map_supportedGametypes |= pThisType;
564 if(!(pThisType & pWantedType))
567 // reset all the cvars to their defaults
569 cvar_set("timelimit", cvar_defstring("timelimit"));
570 cvar_set("leadlimit", cvar_defstring("leadlimit"));
571 cvar_set("fraglimit", cvar_defstring("fraglimit"));
572 cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
573 cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
574 cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
575 cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
577 fraglimit_normal = string_null;
578 fraglimit_caps = string_null;
579 fraglimit_teams = string_null;
581 s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
590 p = strstrofs(sa, "=", 0);
598 k = substring(sa, 0, p);
599 v = substring(sa, p+1, -1);
604 cvar_set("timelimit", v);
606 else if(k == "leadlimit")
608 cvar_set("leadlimit", v);
610 else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit")
612 fraglimit_normal = v;
614 else if(k == "caplimit")
618 else if(k == "teampointlimit" || k == "teamlaplimit")
622 else if(k == "teams")
624 cvar_set("g_tdm_teams", v);
625 cvar_set("g_keyhunt_teams", v);
626 cvar_set("g_domination_default_teams", v);
628 else if(k == "qualifying_timelimit")
630 cvar_set("g_race_qualifying_timelimit", v);
632 else if(k == "skill")
638 print("Invalid gametype key in mapinfo: ", k, "\n");
642 if(pWantedType == MAPINFO_TYPE_CTF && cvar("g_ctf_win_mode") < 2)
645 cvar_set("fraglimit", fraglimit_caps);
647 else if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
650 cvar_set("fraglimit", fraglimit_teams);
655 cvar_set("fraglimit", fraglimit_normal);
659 float MapInfo_Type_FromString(string t)
661 if (t == "dm") return MAPINFO_TYPE_DEATHMATCH;
662 else if(t == "tdm") return MAPINFO_TYPE_TEAM_DEATHMATCH;
663 else if(t == "dom") return MAPINFO_TYPE_DOMINATION;
664 else if(t == "ctf") return MAPINFO_TYPE_CTF;
665 else if(t == "rune") return MAPINFO_TYPE_RUNEMATCH;
666 else if(t == "lms") return MAPINFO_TYPE_LMS;
667 else if(t == "arena") return MAPINFO_TYPE_ARENA;
668 else if(t == "ca") return MAPINFO_TYPE_CA;
669 else if(t == "kh") return MAPINFO_TYPE_KEYHUNT;
670 else if(t == "as") return MAPINFO_TYPE_ASSAULT;
671 else if(t == "ons") return MAPINFO_TYPE_ONSLAUGHT;
672 else if(t == "rc") return MAPINFO_TYPE_RACE;
673 else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
674 else if(t == "cts") return MAPINFO_TYPE_CTS;
675 else if(t == "freezetag") return MAPINFO_TYPE_FREEZETAG;
676 else if(t == "keepaway") return MAPINFO_TYPE_KEEPAWAY;
677 else if(t == "all") return MAPINFO_TYPE_ALL;
681 string MapInfo_Type_ToString(float t)
683 if (t == MAPINFO_TYPE_DEATHMATCH) return "dm";
684 else if(t == MAPINFO_TYPE_TEAM_DEATHMATCH) return "tdm";
685 else if(t == MAPINFO_TYPE_DOMINATION) return "dom";
686 else if(t == MAPINFO_TYPE_CTF) return "ctf";
687 else if(t == MAPINFO_TYPE_RUNEMATCH) return "rune";
688 else if(t == MAPINFO_TYPE_LMS) return "lms";
689 else if(t == MAPINFO_TYPE_ARENA) return "arena";
690 else if(t == MAPINFO_TYPE_CA) return "ca";
691 else if(t == MAPINFO_TYPE_KEYHUNT) return "kh";
692 else if(t == MAPINFO_TYPE_ASSAULT) return "as";
693 else if(t == MAPINFO_TYPE_ONSLAUGHT) return "ons";
694 else if(t == MAPINFO_TYPE_RACE) return "rc";
695 else if(t == MAPINFO_TYPE_NEXBALL) return "nexball";
696 else if(t == MAPINFO_TYPE_CTS) return "cts";
697 else if(t == MAPINFO_TYPE_FREEZETAG) return "freezetag";
698 else if(t == MAPINFO_TYPE_KEEPAWAY) return "keepaway";
699 else if(t == MAPINFO_TYPE_ALL) return "all";
703 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
707 t = car(s); s = cdr(s);
709 // limited support of "" and comments
710 // remove trailing and leading " of t
711 if(substring(t, 0, 1) == "\"")
713 if(substring(t, -1, 1) == "\"")
714 t = substring(t, 1, -2);
717 // remove leading " of s
718 if(substring(s, 0, 1) == "\"")
720 s = substring(s, 1, -1);
722 // remove trailing " of s, and all that follows (cvar description)
723 o = strstrofs(s, "\"", 0);
725 s = substring(s, 0, o);
727 // remove // comments
728 o = strstrofs(s, "//", 0);
730 s = substring(s, 0, o);
732 // remove trailing spaces
733 while(substring(s, -1, 1) == " ")
734 s = substring(s, 0, -2);
740 fh = fopen(s, FILE_READ);
742 print("Map ", pFilename, " references not existing config file ", s, "\n");
747 if not((s = fgets(fh)))
750 // catch different sorts of comments
751 if(s == "") // empty lines
753 if(substring(s, 0, 1) == "#") // UNIX style
755 if(substring(s, 0, 2) == "//") // C++ style
757 if(substring(s, 0, 1) == "_") // q3map style
760 if(substring(s, 0, 4) == "set ")
761 s = substring(s, 4, -1);
762 if(substring(s, 0, 5) == "seta ")
763 s = substring(s, 5, -1);
765 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
771 print("Map ", pFilename, " uses too many levels of inclusion\n");
774 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
775 else if not(cvar_value_issafe(t))
776 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
777 else if not (cvar_value_issafe(s))
778 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
779 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
780 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
781 else if(matchacl(acl, t) <= 0)
782 print("Map ", pFilename, " contains a denied setting, ignored\n");
785 if(type == 0) // server set
787 dprint("Applying temporary setting ", t, " := ", s, "\n");
788 if(cvar("g_campaign"))
789 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
795 dprint("Applying temporary client setting ", t, " := ", s, "\n");
796 MapInfo_Map_clientstuff = strcat(
797 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
803 float MapInfo_isRedundant(string fn, string t)
805 // normalize file name
806 fn = strreplace("_", "-", fn);
808 // normalize visible title
809 t = strreplace(": ", "-", t);
810 t = strreplace(":", "-", t);
811 t = strreplace(" ", "-", t);
812 t = strreplace("_", "-", t);
814 if(!strcasecmp(fn, t))
817 // we allow the visible title to have punctuation the file name does
818 // not, but not vice versa
819 t = strreplace("-", "", t);
821 if(!strcasecmp(fn, t))
827 // load info about a map by name into the MapInfo_Map_* globals
828 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
836 acl = MAPINFO_SETTEMP_ACL_USER;
838 if(strstrofs(pFilename, "/", 0) >= 0)
840 print("Invalid character in map name, ignored\n");
844 if(pGametypeToSet == 0)
845 if(MapInfo_Cache_Retrieve(pFilename))
850 MapInfo_Map_bspname = pFilename;
852 // default all generic fields so they have "good" values in case something fails
853 fn = strcat("maps/", pFilename, ".mapinfo");
854 fh = fopen(fn, FILE_READ);
857 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
858 fh = fopen(fn, FILE_READ);
863 _MapInfo_Map_Reset();
864 r = _MapInfo_Generate(pFilename);
867 fh = fopen(fn, FILE_WRITE);
868 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
869 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
870 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
871 if(_MapInfo_Map_worldspawn_music != "")
874 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
876 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
878 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
880 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
884 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
885 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
888 i = floor(random() * n);
889 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
892 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
894 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
895 fputs(fh, "has weapons\n");
897 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
898 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
899 fputs(fh, "frustrating\n");
901 for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
902 if(MapInfo_Map_supportedGametypes & i)
903 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
905 if(fexists(strcat("scripts/", pFilename, ".arena")))
906 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
908 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
909 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
910 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
911 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
912 fputs(fh, "// optional: hidden\n");
917 fh = fopen(fn, FILE_READ);
919 error("... but I just wrote it!");
922 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
925 _MapInfo_Map_Reset();
928 if not((s = fgets(fh)))
931 // catch different sorts of comments
932 if(s == "") // empty lines
934 if(substring(s, 0, 1) == "#") // UNIX style
936 if(substring(s, 0, 2) == "//") // C++ style
938 if(substring(s, 0, 1) == "_") // q3map style
941 p = strstrofs(s, "//", 0);
943 s = substring(s, 0, p);
945 t = car(s); s = cdr(s);
947 MapInfo_Map_title = s;
948 else if(t == "description")
949 MapInfo_Map_description = s;
950 else if(t == "author")
951 MapInfo_Map_author = s;
954 t = car(s); s = cdr(s);
955 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
956 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
958 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
960 else if(t == "hidden")
962 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
964 else if(t == "forbidden")
966 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
968 else if(t == "frustrating")
970 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
974 t = car(s); s = cdr(s);
975 f = MapInfo_Type_FromString(t);
976 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");
978 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, TRUE);
980 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
982 else if(t == "gametype")
984 t = car(s); s = cdr(s);
985 f = MapInfo_Type_FromString(t);
987 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
989 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
994 t = car(s); s = cdr(s); a = stof(t);
995 t = car(s); s = cdr(s); b = stof(t);
996 t = car(s); s = cdr(s); c = stof(t);
997 t = car(s); s = cdr(s); d = stof(t);
998 t = car(s); s = cdr(s); e = stof(t);
1000 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");
1003 t = car(s); s = cdr(s); f = stof(t);
1005 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");
1008 if(a >= d || b >= e || c >= f)
1009 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
1012 MapInfo_Map_mins_x = a;
1013 MapInfo_Map_mins_y = b;
1014 MapInfo_Map_mins_z = c;
1015 MapInfo_Map_maxs_x = d;
1016 MapInfo_Map_maxs_y = e;
1017 MapInfo_Map_maxs_z = f;
1022 else if(t == "settemp_for_type")
1024 t = car(s); s = cdr(s);
1025 if((f = MapInfo_Type_FromString(t)))
1027 if(f & pGametypeToSet)
1029 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1034 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1037 else if(t == "clientsettemp_for_type")
1039 t = car(s); s = cdr(s);
1040 if((f = MapInfo_Type_FromString(t)))
1042 if(f & pGametypeToSet)
1044 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1049 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1054 if not(cvar_value_issafe(t))
1055 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1057 MapInfo_Map_fog = s;
1059 else if(t == "cdtrack")
1063 if not(cvar_value_issafe(t))
1064 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1066 MapInfo_Map_clientstuff = strcat(
1067 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
1072 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1078 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1080 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1081 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1086 if(MapInfo_Map_titlestring == "<TITLE>")
1087 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1088 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1089 MapInfo_Map_titlestring = MapInfo_Map_title;
1091 MapInfo_Map_titlestring = sprintf(_("%s: %s"), MapInfo_Map_bspname, MapInfo_Map_title);
1093 MapInfo_Cache_Store();
1094 if(MapInfo_Map_supportedGametypes != 0)
1096 dprint("Map ", pFilename, " supports no game types, ignored\n");
1100 float MapInfo_FindName(string s)
1102 // if there is exactly one map of prefix s, return it
1103 // if not, return the null string
1104 // note that DP sorts glob results... so I can use a binary search
1108 // invariants: r is behind s, l-1 is equal or before
1111 m = floor((l + r) / 2);
1112 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1113 cmp = strcasecmp(MapInfo_FindName_match, s);
1115 return m; // found and good
1117 l = m + 1; // l-1 is before s
1121 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1122 MapInfo_FindName_firstResult = l;
1123 // r == l, so: l is behind s, l-1 is before
1124 // SO: if there is any, l is the one with the right prefix
1125 // and l+1 may be one too
1126 if(l == MapInfo_count)
1128 MapInfo_FindName_match = string_null;
1129 MapInfo_FindName_firstResult = -1;
1130 return -1; // no MapInfo_FindName_match, behind last item
1132 if(!startsWithNocase(MapInfo_FindName_match, s))
1134 MapInfo_FindName_match = string_null;
1135 MapInfo_FindName_firstResult = -1;
1136 return -1; // wrong prefix
1138 if(l == MapInfo_count - 1)
1139 return l; // last one, nothing can follow => unique
1140 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1142 MapInfo_FindName_match = string_null;
1143 return -1; // ambigous MapInfo_FindName_match
1148 string MapInfo_FixName(string s)
1150 MapInfo_FindName(s);
1151 return MapInfo_FindName_match;
1154 float MapInfo_CurrentFeatures()
1158 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")))
1159 req |= MAPINFO_FEATURE_WEAPONS;
1163 float MapInfo_CurrentGametype()
1165 if(cvar("g_domination"))
1166 return MAPINFO_TYPE_DOMINATION;
1167 else if(cvar("g_ctf"))
1168 return MAPINFO_TYPE_CTF;
1169 else if(cvar("g_runematch"))
1170 return MAPINFO_TYPE_RUNEMATCH;
1171 else if(cvar("g_tdm"))
1172 return MAPINFO_TYPE_TEAM_DEATHMATCH;
1173 else if(cvar("g_assault"))
1174 return MAPINFO_TYPE_ASSAULT;
1175 else if(cvar("g_lms"))
1176 return MAPINFO_TYPE_LMS;
1177 else if(cvar("g_arena"))
1178 return MAPINFO_TYPE_ARENA;
1179 else if(cvar("g_ca"))
1180 return MAPINFO_TYPE_CA;
1181 else if(cvar("g_keyhunt"))
1182 return MAPINFO_TYPE_KEYHUNT;
1183 else if(cvar("g_onslaught"))
1184 return MAPINFO_TYPE_ONSLAUGHT;
1185 else if(cvar("g_race"))
1186 return MAPINFO_TYPE_RACE;
1187 else if(cvar("g_nexball"))
1188 return MAPINFO_TYPE_NEXBALL;
1189 else if(cvar("g_cts"))
1190 return MAPINFO_TYPE_CTS;
1191 else if(cvar("g_freezetag"))
1192 return MAPINFO_TYPE_FREEZETAG;
1193 else if(cvar("g_keepaway"))
1194 return MAPINFO_TYPE_KEEPAWAY;
1196 return MAPINFO_TYPE_DEATHMATCH;
1199 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1201 if(!MapInfo_Get_ByName(s, 1, 0))
1203 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1205 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1210 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1213 r = _MapInfo_CheckMap(s);
1214 MapInfo_ClearTemps();
1218 string MapInfo_GetGameTypeCvar(float t)
1222 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
1223 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
1224 case MAPINFO_TYPE_DOMINATION: return "g_domination";
1225 case MAPINFO_TYPE_CTF: return "g_ctf";
1226 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
1227 case MAPINFO_TYPE_LMS: return "g_lms";
1228 case MAPINFO_TYPE_ARENA: return "g_arena";
1229 case MAPINFO_TYPE_CA: return "g_ca";
1230 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
1231 case MAPINFO_TYPE_ASSAULT: return "g_assault";
1232 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
1233 case MAPINFO_TYPE_RACE: return "g_race";
1234 case MAPINFO_TYPE_NEXBALL: return "g_nexball";
1235 case MAPINFO_TYPE_FREEZETAG: return "g_freezetag";
1236 case MAPINFO_TYPE_CTS: return "g_cts";
1237 case MAPINFO_TYPE_KEEPAWAY: return "g_keepaway";
1242 void MapInfo_SwitchGameType(float t)
1244 cvar_set("gamecfg", "0");
1245 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "1" : "0");
1246 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
1247 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "1" : "0");
1248 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "1" : "0");
1249 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "1" : "0");
1250 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "1" : "0");
1251 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "1" : "0");
1252 cvar_set("g_ca", (t == MAPINFO_TYPE_CA) ? "1" : "0");
1253 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "1" : "0");
1254 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "1" : "0");
1255 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "1" : "0");
1256 cvar_set("g_race", (t == MAPINFO_TYPE_RACE) ? "1" : "0");
1257 cvar_set("g_nexball", (t == MAPINFO_TYPE_NEXBALL) ? "1" : "0");
1258 cvar_set("g_cts", (t == MAPINFO_TYPE_CTS) ? "1" : "0");
1259 cvar_set("g_freezetag", (t == MAPINFO_TYPE_FREEZETAG) ? "1" : "0");
1260 cvar_set("g_keepaway", (t == MAPINFO_TYPE_KEEPAWAY) ? "1" : "0");
1263 void MapInfo_LoadMap(string s)
1265 MapInfo_Map_supportedGametypes = 0;
1266 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1267 //if(!MapInfo_CheckMap(s))
1269 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1270 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1272 localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
1275 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1280 // to make absolutely sure:
1281 MapInfo_Enumerate();
1282 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1285 for(i = 0; i < MapInfo_count; ++i)
1286 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1287 return substring(out, 1, strlen(out) - 1);
1290 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1295 // to make absolutely sure:
1296 MapInfo_Enumerate();
1297 MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1300 for(i = 0; i < MapInfo_count; ++i)
1301 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1303 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1305 return substring(out, 1, strlen(out) - 1);
1308 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1311 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1313 if(MapInfo_Map_supportedGametypes == 0)
1315 print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1316 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1317 _MapInfo_Map_ApplyGametypeEx("", t0, t0);
1318 return; // do not call Get_ByName!
1322 while(!(MapInfo_Map_supportedGametypes & 1))
1325 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1327 // t is now a supported mode!
1328 t0 = MapInfo_CurrentGametype();
1329 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1331 print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1332 cvar_settemp_restore();
1333 _MapInfo_Map_ApplyGametypeEx("", t0, t0);
1334 return; // do not call Get_ByName!
1338 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1339 MapInfo_SwitchGameType(t);
1342 cvar_settemp_restore();
1343 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
1346 void MapInfo_ClearTemps()
1348 MapInfo_Map_bspname = string_null;
1349 MapInfo_Map_title = string_null;
1350 MapInfo_Map_titlestring = string_null;
1351 MapInfo_Map_description = string_null;
1352 MapInfo_Map_author = string_null;
1353 MapInfo_Map_clientstuff = string_null;
1354 MapInfo_Map_supportedGametypes = 0;
1355 MapInfo_Map_supportedFeatures = 0;
1358 void MapInfo_Shutdown()
1360 MapInfo_ClearTemps();
1361 MapInfo_Filter_Free();
1362 MapInfo_Cache_Destroy();
1363 if(_MapInfo_globopen)
1365 search_end(_MapInfo_globhandle);
1366 _MapInfo_globhandle = -1;
1367 _MapInfo_globopen = FALSE;
1371 float MapInfo_ForbiddenFlags()
1374 f = MAPINFO_FLAG_FORBIDDEN;
1377 if not(cvar("g_maplist_allow_hidden"))
1379 f |= MAPINFO_FLAG_HIDDEN;
1381 if not(cvar("g_maplist_allow_frustrating"))
1382 f |= MAPINFO_FLAG_FRUSTRATING;
1387 float MapInfo_RequiredFlags()
1392 if(cvar("g_maplist_allow_frustrating") > 1)
1393 f |= MAPINFO_FLAG_FRUSTRATING;