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);
813 t = strreplace("'", "-", t);
815 if(!strcasecmp(fn, t))
818 // we allow the visible title to have punctuation the file name does
819 // not, but not vice versa
820 t = strreplace("-", "", t);
822 if(!strcasecmp(fn, t))
828 // load info about a map by name into the MapInfo_Map_* globals
829 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
837 acl = MAPINFO_SETTEMP_ACL_USER;
839 if(strstrofs(pFilename, "/", 0) >= 0)
841 print("Invalid character in map name, ignored\n");
845 if(pGametypeToSet == 0)
846 if(MapInfo_Cache_Retrieve(pFilename))
851 MapInfo_Map_bspname = pFilename;
853 // default all generic fields so they have "good" values in case something fails
854 fn = strcat("maps/", pFilename, ".mapinfo");
855 fh = fopen(fn, FILE_READ);
858 fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
859 fh = fopen(fn, FILE_READ);
864 _MapInfo_Map_Reset();
865 r = _MapInfo_Generate(pFilename);
868 fh = fopen(fn, FILE_WRITE);
869 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
870 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
871 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
872 if(_MapInfo_Map_worldspawn_music != "")
875 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
877 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
879 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
881 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
885 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
886 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
889 i = floor(random() * n);
890 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
893 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
895 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
896 fputs(fh, "has weapons\n");
898 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
899 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
900 fputs(fh, "frustrating\n");
902 for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
903 if(MapInfo_Map_supportedGametypes & i)
904 fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
906 if(fexists(strcat("scripts/", pFilename, ".arena")))
907 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
909 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
910 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
911 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
912 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
913 fputs(fh, "// optional: hidden\n");
918 fh = fopen(fn, FILE_READ);
920 error("... but I just wrote it!");
923 print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
926 _MapInfo_Map_Reset();
929 if not((s = fgets(fh)))
932 // catch different sorts of comments
933 if(s == "") // empty lines
935 if(substring(s, 0, 1) == "#") // UNIX style
937 if(substring(s, 0, 2) == "//") // C++ style
939 if(substring(s, 0, 1) == "_") // q3map style
942 p = strstrofs(s, "//", 0);
944 s = substring(s, 0, p);
946 t = car(s); s = cdr(s);
948 MapInfo_Map_title = s;
949 else if(t == "description")
950 MapInfo_Map_description = s;
951 else if(t == "author")
952 MapInfo_Map_author = s;
955 t = car(s); s = cdr(s);
956 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
957 else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
959 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
961 else if(t == "hidden")
963 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
965 else if(t == "forbidden")
967 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
969 else if(t == "frustrating")
971 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
975 t = car(s); s = cdr(s);
976 f = MapInfo_Type_FromString(t);
977 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");
979 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, TRUE);
981 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
983 else if(t == "gametype")
985 t = car(s); s = cdr(s);
986 f = MapInfo_Type_FromString(t);
988 _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
990 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
995 t = car(s); s = cdr(s); a = stof(t);
996 t = car(s); s = cdr(s); b = stof(t);
997 t = car(s); s = cdr(s); c = stof(t);
998 t = car(s); s = cdr(s); d = stof(t);
999 t = car(s); s = cdr(s); e = stof(t);
1001 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");
1004 t = car(s); s = cdr(s); f = stof(t);
1006 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");
1009 if(a >= d || b >= e || c >= f)
1010 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
1013 MapInfo_Map_mins_x = a;
1014 MapInfo_Map_mins_y = b;
1015 MapInfo_Map_mins_z = c;
1016 MapInfo_Map_maxs_x = d;
1017 MapInfo_Map_maxs_y = e;
1018 MapInfo_Map_maxs_z = f;
1023 else if(t == "settemp_for_type")
1025 t = car(s); s = cdr(s);
1026 if((f = MapInfo_Type_FromString(t)))
1028 if(f & pGametypeToSet)
1030 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
1035 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
1038 else if(t == "clientsettemp_for_type")
1040 t = car(s); s = cdr(s);
1041 if((f = MapInfo_Type_FromString(t)))
1043 if(f & pGametypeToSet)
1045 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
1050 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
1055 if not(cvar_value_issafe(t))
1056 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
1058 MapInfo_Map_fog = s;
1060 else if(t == "cdtrack")
1064 if not(cvar_value_issafe(t))
1065 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
1067 MapInfo_Map_clientstuff = strcat(
1068 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
1073 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
1079 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
1081 error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
1082 //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
1087 if(MapInfo_Map_title == "<TITLE>")
1088 MapInfo_Map_titlestring = MapInfo_Map_bspname;
1089 else if(MapInfo_isRedundant(MapInfo_Map_bspname, MapInfo_Map_title))
1090 MapInfo_Map_titlestring = MapInfo_Map_title;
1092 MapInfo_Map_titlestring = sprintf(_("%s: %s"), MapInfo_Map_bspname, MapInfo_Map_title);
1094 MapInfo_Cache_Store();
1095 if(MapInfo_Map_supportedGametypes != 0)
1097 dprint("Map ", pFilename, " supports no game types, ignored\n");
1101 float MapInfo_FindName(string s)
1103 // if there is exactly one map of prefix s, return it
1104 // if not, return the null string
1105 // note that DP sorts glob results... so I can use a binary search
1109 // invariants: r is behind s, l-1 is equal or before
1112 m = floor((l + r) / 2);
1113 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
1114 cmp = strcasecmp(MapInfo_FindName_match, s);
1116 return m; // found and good
1118 l = m + 1; // l-1 is before s
1122 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
1123 MapInfo_FindName_firstResult = l;
1124 // r == l, so: l is behind s, l-1 is before
1125 // SO: if there is any, l is the one with the right prefix
1126 // and l+1 may be one too
1127 if(l == MapInfo_count)
1129 MapInfo_FindName_match = string_null;
1130 MapInfo_FindName_firstResult = -1;
1131 return -1; // no MapInfo_FindName_match, behind last item
1133 if(!startsWithNocase(MapInfo_FindName_match, s))
1135 MapInfo_FindName_match = string_null;
1136 MapInfo_FindName_firstResult = -1;
1137 return -1; // wrong prefix
1139 if(l == MapInfo_count - 1)
1140 return l; // last one, nothing can follow => unique
1141 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
1143 MapInfo_FindName_match = string_null;
1144 return -1; // ambigous MapInfo_FindName_match
1149 string MapInfo_FixName(string s)
1151 MapInfo_FindName(s);
1152 return MapInfo_FindName_match;
1155 float MapInfo_CurrentFeatures()
1159 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")))
1160 req |= MAPINFO_FEATURE_WEAPONS;
1164 float MapInfo_CurrentGametype()
1166 if(cvar("g_domination"))
1167 return MAPINFO_TYPE_DOMINATION;
1168 else if(cvar("g_ctf"))
1169 return MAPINFO_TYPE_CTF;
1170 else if(cvar("g_runematch"))
1171 return MAPINFO_TYPE_RUNEMATCH;
1172 else if(cvar("g_tdm"))
1173 return MAPINFO_TYPE_TEAM_DEATHMATCH;
1174 else if(cvar("g_assault"))
1175 return MAPINFO_TYPE_ASSAULT;
1176 else if(cvar("g_lms"))
1177 return MAPINFO_TYPE_LMS;
1178 else if(cvar("g_arena"))
1179 return MAPINFO_TYPE_ARENA;
1180 else if(cvar("g_ca"))
1181 return MAPINFO_TYPE_CA;
1182 else if(cvar("g_keyhunt"))
1183 return MAPINFO_TYPE_KEYHUNT;
1184 else if(cvar("g_onslaught"))
1185 return MAPINFO_TYPE_ONSLAUGHT;
1186 else if(cvar("g_race"))
1187 return MAPINFO_TYPE_RACE;
1188 else if(cvar("g_nexball"))
1189 return MAPINFO_TYPE_NEXBALL;
1190 else if(cvar("g_cts"))
1191 return MAPINFO_TYPE_CTS;
1192 else if(cvar("g_freezetag"))
1193 return MAPINFO_TYPE_FREEZETAG;
1194 else if(cvar("g_keepaway"))
1195 return MAPINFO_TYPE_KEEPAWAY;
1197 return MAPINFO_TYPE_DEATHMATCH;
1200 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1202 if(!MapInfo_Get_ByName(s, 1, 0))
1204 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
1206 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1211 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1214 r = _MapInfo_CheckMap(s);
1215 MapInfo_ClearTemps();
1219 string MapInfo_GetGameTypeCvar(float t)
1223 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
1224 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
1225 case MAPINFO_TYPE_DOMINATION: return "g_domination";
1226 case MAPINFO_TYPE_CTF: return "g_ctf";
1227 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
1228 case MAPINFO_TYPE_LMS: return "g_lms";
1229 case MAPINFO_TYPE_ARENA: return "g_arena";
1230 case MAPINFO_TYPE_CA: return "g_ca";
1231 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
1232 case MAPINFO_TYPE_ASSAULT: return "g_assault";
1233 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
1234 case MAPINFO_TYPE_RACE: return "g_race";
1235 case MAPINFO_TYPE_NEXBALL: return "g_nexball";
1236 case MAPINFO_TYPE_FREEZETAG: return "g_freezetag";
1237 case MAPINFO_TYPE_CTS: return "g_cts";
1238 case MAPINFO_TYPE_KEEPAWAY: return "g_keepaway";
1243 void MapInfo_SwitchGameType(float t)
1245 cvar_set("gamecfg", "0");
1246 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "1" : "0");
1247 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
1248 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "1" : "0");
1249 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "1" : "0");
1250 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "1" : "0");
1251 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "1" : "0");
1252 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "1" : "0");
1253 cvar_set("g_ca", (t == MAPINFO_TYPE_CA) ? "1" : "0");
1254 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "1" : "0");
1255 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "1" : "0");
1256 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "1" : "0");
1257 cvar_set("g_race", (t == MAPINFO_TYPE_RACE) ? "1" : "0");
1258 cvar_set("g_nexball", (t == MAPINFO_TYPE_NEXBALL) ? "1" : "0");
1259 cvar_set("g_cts", (t == MAPINFO_TYPE_CTS) ? "1" : "0");
1260 cvar_set("g_freezetag", (t == MAPINFO_TYPE_FREEZETAG) ? "1" : "0");
1261 cvar_set("g_keepaway", (t == MAPINFO_TYPE_KEEPAWAY) ? "1" : "0");
1264 void MapInfo_LoadMap(string s)
1266 MapInfo_Map_supportedGametypes = 0;
1267 // we shouldn't need this, as LoadMapSettings already fixes the gametype
1268 //if(!MapInfo_CheckMap(s))
1270 // print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1271 // MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1273 localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
1276 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1281 // to make absolutely sure:
1282 MapInfo_Enumerate();
1283 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1286 for(i = 0; i < MapInfo_count; ++i)
1287 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1288 return substring(out, 1, strlen(out) - 1);
1291 string MapInfo_ListAllAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1296 // to make absolutely sure:
1297 MapInfo_Enumerate();
1298 MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, pRequiredFlags, pForbiddenFlags, 0);
1301 for(i = 0; i < MapInfo_count; ++i)
1302 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1304 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1306 return substring(out, 1, strlen(out) - 1);
1309 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1312 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1314 if(MapInfo_Map_supportedGametypes == 0)
1316 print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
1317 MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
1318 _MapInfo_Map_ApplyGametypeEx("", t0, t0);
1319 return; // do not call Get_ByName!
1323 while(!(MapInfo_Map_supportedGametypes & 1))
1326 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1328 // t is now a supported mode!
1329 t0 = MapInfo_CurrentGametype();
1330 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1332 print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1333 cvar_settemp_restore();
1334 _MapInfo_Map_ApplyGametypeEx("", t0, t0);
1335 return; // do not call Get_ByName!
1339 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1340 MapInfo_SwitchGameType(t);
1343 cvar_settemp_restore();
1344 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
1347 void MapInfo_ClearTemps()
1349 MapInfo_Map_bspname = string_null;
1350 MapInfo_Map_title = string_null;
1351 MapInfo_Map_titlestring = string_null;
1352 MapInfo_Map_description = string_null;
1353 MapInfo_Map_author = string_null;
1354 MapInfo_Map_clientstuff = string_null;
1355 MapInfo_Map_supportedGametypes = 0;
1356 MapInfo_Map_supportedFeatures = 0;
1359 void MapInfo_Shutdown()
1361 MapInfo_ClearTemps();
1362 MapInfo_Filter_Free();
1363 MapInfo_Cache_Destroy();
1364 if(_MapInfo_globopen)
1366 search_end(_MapInfo_globhandle);
1367 _MapInfo_globhandle = -1;
1368 _MapInfo_globopen = FALSE;
1372 float MapInfo_ForbiddenFlags()
1375 f = MAPINFO_FLAG_FORBIDDEN;
1378 if not(cvar("g_maplist_allow_hidden"))
1380 f |= MAPINFO_FLAG_HIDDEN;
1382 if not(cvar("g_maplist_allow_frustrating"))
1383 f |= MAPINFO_FLAG_FRUSTRATING;
1388 float MapInfo_RequiredFlags()
1393 if(cvar("g_maplist_allow_frustrating") > 1)
1394 f |= MAPINFO_FLAG_FRUSTRATING;