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; please edit that file later\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;
332 print(fn, " ended still in worldspawn, BUG\n");
335 diameter = vlen(mapMaxs - mapMins);
337 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
338 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
340 // we have a CTF-only or Assault-only map. Don't add other modes then,
341 // as the map is too symmetric for them.
345 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
346 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH; // Rune always works
347 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
349 if(spawnpoints >= 8 && diameter > 4096) {
350 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
351 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
354 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
355 if(spawnpoints >= 12 && diameter > 5120)
356 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
359 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
362 MapInfo_Map_supportedGametypes &~= MAPINFO_TYPE_RACE;
363 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
366 dprint("-> diameter ", ftos(diameter));
367 dprint("; spawnpoints ", ftos(spawnpoints));
368 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
375 void _MapInfo_Map_Reset()
377 MapInfo_Map_title = "<TITLE>";
378 MapInfo_Map_description = "<DESCRIPTION>";
379 MapInfo_Map_author = "<AUTHOR>";
380 MapInfo_Map_supportedGametypes = 0;
381 MapInfo_Map_supportedFeatures = 0;
382 MapInfo_Map_flags = 0;
383 MapInfo_Map_clientstuff = "";
384 MapInfo_Map_fog = "";
385 MapInfo_Map_mins = '0 0 0';
386 MapInfo_Map_maxs = '0 0 0';
389 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
392 MapInfo_Map_supportedGametypes |= pThisType;
393 if(!(pThisType & pWantedType))
396 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
398 cvar_set("fraglimit", "0");
402 cvar_set("fraglimit", car(s));
406 cvar_set("timelimit", car(s));
409 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
411 sa = car(s); if(sa == "") sa = "2";
412 cvar_set("g_tdm_teams", sa);
416 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
418 sa = car(s); if(sa == "") sa = "3";
419 cvar_set("g_keyhunt_teams", sa);
423 if(pWantedType == MAPINFO_TYPE_CTF)
425 sa = car(s); if(sa == "") sa = "10";
426 if(cvar("g_ctf_win_mode") < 2)
427 cvar_set("fraglimit", sa);
431 // rc = timelimit timelimit_qualification laps laps_teamplay
432 if(pWantedType == MAPINFO_TYPE_RACE)
434 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
435 cvar_set("g_race_qualifying_timelimit", sa);
438 sa = car(s); if(sa == "") sa = "10";
439 if(cvar("g_race_teams") < 2)
440 cvar_set("fraglimit", sa);
443 sa = car(s); if(sa == "") sa = "20";
444 if(cvar("g_race_teams") >= 2)
445 cvar_set("fraglimit", sa);
449 if(pWantedType == MAPINFO_TYPE_CTS)
451 sa = car(s); if(sa == "") sa = cvar_string("fraglimit");
452 if(cvar("g_race_teams"))
453 cvar_set("fraglimit", sa);
457 sa = car(s); if(sa == "") sa = "0";
458 cvar_set("leadlimit", sa);
462 float MapInfo_Type_FromString(string t)
464 if (t == "dm") return MAPINFO_TYPE_DEATHMATCH;
465 else if(t == "tdm") return MAPINFO_TYPE_TEAM_DEATHMATCH;
466 else if(t == "dom") return MAPINFO_TYPE_DOMINATION;
467 else if(t == "ctf") return MAPINFO_TYPE_CTF;
468 else if(t == "rune") return MAPINFO_TYPE_RUNEMATCH;
469 else if(t == "lms") return MAPINFO_TYPE_LMS;
470 else if(t == "arena") return MAPINFO_TYPE_ARENA;
471 else if(t == "ca") return MAPINFO_TYPE_CA;
472 else if(t == "kh") return MAPINFO_TYPE_KEYHUNT;
473 else if(t == "as") return MAPINFO_TYPE_ASSAULT;
474 else if(t == "ons") return MAPINFO_TYPE_ONSLAUGHT;
475 else if(t == "rc") return MAPINFO_TYPE_RACE;
476 else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
477 else if(t == "cts") return MAPINFO_TYPE_CTS;
478 else if(t == "all") return MAPINFO_TYPE_ALL;
482 void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
486 t = car(s); s = cdr(s);
488 // limited support of "" and comments
489 // remove trailing and leading " of t
490 if(substring(t, 0, 1) == "\"")
492 if(substring(t, -1, 1) == "\"")
493 t = substring(t, 1, -2);
496 // remove leading " of s
497 if(substring(s, 0, 1) == "\"")
499 s = substring(s, 1, -1);
501 // remove trailing " of s, and all that follows (cvar description)
502 o = strstrofs(s, "\"", 0);
504 s = substring(s, 0, o);
506 // remove // comments
507 o = strstrofs(s, "//", 0);
509 s = substring(s, 0, o);
511 // remove trailing spaces
512 while(substring(s, -1, 1) == " ")
513 s = substring(s, 0, -2);
519 fh = fopen(s, FILE_READ);
521 print("Map ", pFilename, " references not existing config file ", s, "\n");
526 if not((s = fgets(fh)))
529 // catch different sorts of comments
530 if(s == "") // empty lines
532 if(substring(s, 0, 1) == "#") // UNIX style
534 if(substring(s, 0, 2) == "//") // C++ style
536 if(substring(s, 0, 1) == "_") // q3map style
539 if(substring(s, 0, 4) == "set ")
540 s = substring(s, 4, -1);
541 if(substring(s, 0, 5) == "seta ")
542 s = substring(s, 5, -1);
544 _MapInfo_Parse_Settemp(pFilename, acl, type, s, recurse - 1);
550 print("Map ", pFilename, " uses too many levels of inclusion\n");
552 else if not(cvar_value_issafe(t))
553 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
554 else if not (cvar_value_issafe(s))
555 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
556 else if(matchacl(MAPINFO_SETTEMP_ACL_SYSTEM, t) <= 0)
557 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
558 else if(matchacl(acl, t) <= 0)
559 print("Map ", pFilename, " contains a denied setting, ignored\n");
562 if(type == 0) // server set
564 dprint("Applying temporary setting ", t, " := ", s, "\n");
565 if(cvar("g_campaign"))
566 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
572 dprint("Applying temporary client setting ", t, " := ", s, "\n");
573 MapInfo_Map_clientstuff = strcat(
574 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
580 // load info about a map by name into the MapInfo_Map_* globals
581 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
589 acl = MAPINFO_SETTEMP_ACL_USER;
591 if(strstrofs(pFilename, "/", 0) >= 0)
593 print("Invalid character in map name, ignored\n");
597 if(pGametypeToSet == 0)
598 if(MapInfo_Cache_Retrieve(pFilename))
603 MapInfo_Map_bspname = pFilename;
605 // default all generic fields so they have "good" values in case something fails
606 fn = strcat("maps/", pFilename, ".mapinfo");
607 fh = fopen(fn, FILE_READ);
612 _MapInfo_Map_Reset();
613 r = _MapInfo_Generate(pFilename);
616 fh = fopen(fn, FILE_WRITE);
617 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
618 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
619 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
620 if(_MapInfo_Map_worldspawn_music != "")
623 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
625 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
627 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
629 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
633 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
634 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
637 i = floor(random() * n);
638 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
641 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
643 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
644 fputs(fh, "has weapons\n");
646 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
647 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
648 fputs(fh, "frustrating\n");
649 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH) fputs(fh, "type dm 30 20\n");
650 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
651 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION) fputs(fh, "type dom 200 20\n");
652 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF) fputs(fh, "type ctf 300 20 10\n");
653 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH) fputs(fh, "type rune 200 20\n");
654 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS) fputs(fh, "type lms 9 20\n");
655 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA) fputs(fh, "type arena 10 20\n");
656 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CA) fputs(fh, "type ca 10 20\n");
657 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT) fputs(fh, "type kh 1000 20 3\n");
658 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT) fputs(fh, "type as 20\n");
659 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE) fputs(fh, "type rc 20 5 7 15\n");
660 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT) fputs(fh, "type ons 20\n");
661 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_NEXBALL) fputs(fh, "type nexball 5 20\n");
662 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTS) fputs(fh, "type cts 20 -1\n");
664 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
668 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
671 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
672 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
673 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
674 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
675 fputs(fh, "// optional: hidden\n");
680 fh = fopen(fn, FILE_READ);
682 error("... but I just wrote it!");
685 _MapInfo_Map_Reset();
688 if not((s = fgets(fh)))
691 // catch different sorts of comments
692 if(s == "") // empty lines
694 if(substring(s, 0, 1) == "#") // UNIX style
696 if(substring(s, 0, 2) == "//") // C++ style
698 if(substring(s, 0, 1) == "_") // q3map style
701 t = car(s); s = cdr(s);
703 MapInfo_Map_title = s;
704 else if(t == "description")
705 MapInfo_Map_description = s;
706 else if(t == "author")
707 MapInfo_Map_author = s;
710 t = car(s); s = cdr(s);
711 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
713 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
715 else if(t == "hidden")
717 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
719 else if(t == "forbidden")
721 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
723 else if(t == "frustrating")
725 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
729 t = car(s); s = cdr(s);
730 f = MapInfo_Type_FromString(t);
732 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
734 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
739 t = car(s); s = cdr(s); a = stof(t);
740 t = car(s); s = cdr(s); b = stof(t);
741 t = car(s); s = cdr(s); c = stof(t);
742 t = car(s); s = cdr(s); d = stof(t);
743 t = car(s); s = cdr(s); e = stof(t);
745 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");
748 t = car(s); s = cdr(s); f = stof(t);
750 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");
753 if(a >= d || b >= e || c >= f)
754 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
757 MapInfo_Map_mins_x = a;
758 MapInfo_Map_mins_y = b;
759 MapInfo_Map_mins_z = c;
760 MapInfo_Map_maxs_x = d;
761 MapInfo_Map_maxs_y = e;
762 MapInfo_Map_maxs_z = f;
767 else if(t == "settemp_for_type")
769 t = car(s); s = cdr(s);
770 if((f = MapInfo_Type_FromString(t)))
772 if(f & pGametypeToSet)
774 _MapInfo_Parse_Settemp(pFilename, acl, 0, s, 1);
779 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
782 else if(t == "clientsettemp_for_type")
784 t = car(s); s = cdr(s);
785 if((f = MapInfo_Type_FromString(t)))
787 if(f & pGametypeToSet)
789 _MapInfo_Parse_Settemp(pFilename, acl, 1, s, 1);
794 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
799 if not(cvar_value_issafe(t))
800 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
804 else if(t == "cdtrack")
808 if not(cvar_value_issafe(t))
809 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
811 MapInfo_Map_clientstuff = strcat(
812 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
817 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
823 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
825 print("Can't select the requested game type. Trying anyway with stupid settings.\n");
826 _MapInfo_Map_ApplyGametype("0 0 0", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
830 MapInfo_Cache_Store();
831 if(MapInfo_Map_supportedGametypes != 0)
833 dprint("Map ", pFilename, " supports no game types, ignored\n");
837 float MapInfo_FindName(string s)
839 // if there is exactly one map of prefix s, return it
840 // if not, return the null string
841 // note that DP sorts glob results... so I can use a binary search
845 // invariants: r is behind s, l-1 is equal or before
848 m = floor((l + r) / 2);
849 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
850 cmp = strcasecmp(MapInfo_FindName_match, s);
852 return m; // found and good
854 l = m + 1; // l-1 is before s
858 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
859 MapInfo_FindName_firstResult = l;
860 // r == l, so: l is behind s, l-1 is before
861 // SO: if there is any, l is the one with the right prefix
862 // and l+1 may be one too
863 if(l == MapInfo_count)
865 MapInfo_FindName_match = string_null;
866 MapInfo_FindName_firstResult = -1;
867 return -1; // no MapInfo_FindName_match, behind last item
869 if(!startsWithNocase(MapInfo_FindName_match, s))
871 MapInfo_FindName_match = string_null;
872 MapInfo_FindName_firstResult = -1;
873 return -1; // wrong prefix
875 if(l == MapInfo_count - 1)
876 return l; // last one, nothing can follow => unique
877 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
879 MapInfo_FindName_match = string_null;
880 return -1; // ambigous MapInfo_FindName_match
885 string MapInfo_FixName(string s)
888 return MapInfo_FindName_match;
891 float MapInfo_CurrentFeatures()
895 if(!(cvar("g_lms") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_weaponarena") || !cvar("g_pickup_items") || cvar("g_race") || cvar("g_cts") || cvar("g_nexball")))
896 req |= MAPINFO_FEATURE_WEAPONS;
900 float MapInfo_CurrentGametype()
902 if(cvar("g_domination"))
903 return MAPINFO_TYPE_DOMINATION;
904 else if(cvar("g_ctf"))
905 return MAPINFO_TYPE_CTF;
906 else if(cvar("g_runematch"))
907 return MAPINFO_TYPE_RUNEMATCH;
908 else if(cvar("g_tdm"))
909 return MAPINFO_TYPE_TEAM_DEATHMATCH;
910 else if(cvar("g_assault"))
911 return MAPINFO_TYPE_ASSAULT;
912 else if(cvar("g_lms"))
913 return MAPINFO_TYPE_LMS;
914 else if(cvar("g_arena"))
915 return MAPINFO_TYPE_ARENA;
916 else if(cvar("g_ca"))
917 return MAPINFO_TYPE_CA;
918 else if(cvar("g_keyhunt"))
919 return MAPINFO_TYPE_KEYHUNT;
920 else if(cvar("g_onslaught"))
921 return MAPINFO_TYPE_ONSLAUGHT;
922 else if(cvar("g_race"))
923 return MAPINFO_TYPE_RACE;
924 else if(cvar("g_nexball"))
925 return MAPINFO_TYPE_NEXBALL;
926 else if(cvar("g_cts"))
927 return MAPINFO_TYPE_CTS;
929 return MAPINFO_TYPE_DEATHMATCH;
932 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
934 if(!MapInfo_Get_ByName(s, 1, 0))
936 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
938 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
943 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
946 r = _MapInfo_CheckMap(s);
947 MapInfo_ClearTemps();
951 string MapInfo_GetGameTypeCvar(float t)
955 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
956 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
957 case MAPINFO_TYPE_DOMINATION: return "g_domination";
958 case MAPINFO_TYPE_CTF: return "g_ctf";
959 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
960 case MAPINFO_TYPE_LMS: return "g_lms";
961 case MAPINFO_TYPE_ARENA: return "g_arena";
962 case MAPINFO_TYPE_CA: return "g_ca";
963 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
964 case MAPINFO_TYPE_ASSAULT: return "g_assault";
965 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
966 case MAPINFO_TYPE_RACE: return "g_race";
967 case MAPINFO_TYPE_NEXBALL: return "g_nexball";
968 case MAPINFO_TYPE_CTS: return "g_cts";
973 void MapInfo_SwitchGameType(float t)
975 cvar_set("gamecfg", "0");
976 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "1" : "0");
977 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
978 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "1" : "0");
979 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "1" : "0");
980 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "1" : "0");
981 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "1" : "0");
982 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "1" : "0");
983 cvar_set("g_ca", (t == MAPINFO_TYPE_CA) ? "1" : "0");
984 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "1" : "0");
985 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "1" : "0");
986 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "1" : "0");
987 cvar_set("g_race", (t == MAPINFO_TYPE_RACE) ? "1" : "0");
988 cvar_set("g_nexball", (t == MAPINFO_TYPE_NEXBALL) ? "1" : "0");
989 cvar_set("g_cts", (t == MAPINFO_TYPE_CTS) ? "1" : "0");
992 void MapInfo_LoadMap(string s)
994 MapInfo_Map_supportedGametypes = 0;
995 if(!MapInfo_CheckMap(s))
997 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
998 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1000 localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
1003 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1008 // to make absolutely sure:
1009 MapInfo_Enumerate();
1010 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1013 for(i = 0; i < MapInfo_count; ++i)
1014 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1015 return substring(out, 1, strlen(out) - 1);
1018 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1021 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1023 if(MapInfo_Map_supportedGametypes <= 0)
1024 error("Mapinfo system is not functional at all. BAILED OUT.\n");
1027 while(!(MapInfo_Map_supportedGametypes & 1))
1030 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1032 // t is now a supported mode!
1033 t0 = MapInfo_CurrentGametype();
1034 if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
1036 print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1037 cvar_set("timelimit", "0");
1038 cvar_set("fraglimit", "0");
1039 cvar_set("g_tdm_teams", "2");
1040 cvar_set("g_keyhunt_teams", "3");
1041 cvar_set("g_race_qualifying_timelimit", "0");
1042 cvar_set("leadlimit", "0");
1046 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1047 MapInfo_SwitchGameType(t);
1050 cvar_settemp_restore();
1051 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
1054 void MapInfo_ClearTemps()
1056 MapInfo_Map_bspname = string_null;
1057 MapInfo_Map_title = string_null;
1058 MapInfo_Map_description = string_null;
1059 MapInfo_Map_author = string_null;
1060 MapInfo_Map_clientstuff = string_null;
1061 MapInfo_Map_supportedGametypes = 0;
1062 MapInfo_Map_supportedFeatures = 0;
1065 void MapInfo_Shutdown()
1067 MapInfo_ClearTemps();
1068 MapInfo_Filter_Free();
1069 MapInfo_Cache_Destroy();
1070 if(_MapInfo_globopen)
1072 search_end(_MapInfo_globhandle);
1073 _MapInfo_globhandle = -1;
1074 _MapInfo_globopen = FALSE;
1078 float MapInfo_ForbiddenFlags()
1081 f = MAPINFO_FLAG_FORBIDDEN;
1084 if not(cvar("g_maplist_allow_hidden"))
1086 f |= MAPINFO_FLAG_HIDDEN;
1088 if not(cvar("g_maplist_allow_frustrating"))
1089 f |= MAPINFO_FLAG_FRUSTRATING;
1094 float MapInfo_RequiredFlags()
1099 if(cvar("g_maplist_allow_frustrating") > 1)
1100 f |= MAPINFO_FLAG_FRUSTRATING;