1 // generic string stuff
2 float startsWith(string haystack, string needle)
4 return substring(haystack, 0, strlen(needle)) == needle;
6 float startsWithNocase(string haystack, string needle)
8 return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
10 string extractRestOfLine(string haystack, string needle)
12 if(startsWith(haystack, needle))
13 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
19 o = strstrofs(s, " ", 0);
22 return substring(s, 0, o);
27 o = strstrofs(s, " ", 0);
30 return substring(s, o + 1, strlen(s) - (o + 1));
32 float matchacl(string acl, string str)
39 t = car(acl); acl = cdr(acl);
41 if(substring(t, 0, 1) == "-")
44 t = substring(t, 1, strlen(t) - 1);
46 else if(substring(t, 0, 1) == "+")
47 t = substring(t, 1, strlen(t) - 1);
48 if(substring(t, -1, 1) == "*")
50 t = substring(t, 0, strlen(t) - 1);
51 s = substring(s, 0, strlen(t));
64 float _MapInfo_Cache_Active;
65 float _MapInfo_Cache_DB_NameToIndex;
66 float _MapInfo_Cache_Buf_IndexToMapData;
68 void MapInfo_Cache_Destroy()
70 if(!_MapInfo_Cache_Active)
73 db_close(_MapInfo_Cache_DB_NameToIndex);
74 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
75 _MapInfo_Cache_Active = 0;
78 void MapInfo_Cache_Create()
80 MapInfo_Cache_Destroy();
81 _MapInfo_Cache_DB_NameToIndex = db_create();
82 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
83 _MapInfo_Cache_Active = 1;
86 void MapInfo_Cache_Invalidate()
88 if(!_MapInfo_Cache_Active)
91 MapInfo_Cache_Create();
94 void MapInfo_Cache_Store()
98 if(!_MapInfo_Cache_Active)
101 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
102 if(!s) // empty string is NOT valid here!
104 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
105 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
110 // now store all the stuff
111 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
112 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
113 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
114 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
115 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
116 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
117 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
120 float MapInfo_Cache_Retrieve(string map)
124 if(!_MapInfo_Cache_Active)
127 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
132 // now retrieve all the stuff
133 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
134 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
135 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
136 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
137 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
138 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
139 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
143 // GLOB HANDLING (for all BSP files)
144 float _MapInfo_globopen;
145 float _MapInfo_globcount;
146 float _MapInfo_globhandle;
147 string _MapInfo_GlobItem(float i)
150 s = search_getfilename(_MapInfo_globhandle, i);
151 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
154 void MapInfo_Enumerate()
156 if(_MapInfo_globopen)
157 search_end(_MapInfo_globhandle);
158 MapInfo_Cache_Invalidate();
159 _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
160 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
161 _MapInfo_globopen = 1;
164 // filter the info by game type mask (updates MapInfo_count)
166 float _MapInfo_filtered;
167 float _MapInfo_filtered_allocated;
168 float MapInfo_FilterList_Lookup(float i)
170 return stof(bufstr_get(_MapInfo_filtered, i));
173 void _MapInfo_FilterList_swap(float i, float j, entity pass)
176 h = bufstr_get(_MapInfo_filtered, i);
177 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
178 bufstr_set(_MapInfo_filtered, j, h);
181 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
184 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
185 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
186 return strcasecmp(a, b);
189 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
192 if not(_MapInfo_filtered_allocated)
194 _MapInfo_filtered_allocated = 1;
195 _MapInfo_filtered = buf_create();
198 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
200 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.
203 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
204 MapInfo_progress = i / _MapInfo_globcount;
207 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
208 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
209 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
210 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
211 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
213 MapInfo_count = j + 1;
214 MapInfo_ClearTemps();
216 // sometimes the glob isn't sorted nicely, so fix it here...
217 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
222 void MapInfo_Filter_Free()
224 if(_MapInfo_filtered_allocated)
226 buf_del(_MapInfo_filtered);
227 _MapInfo_filtered_allocated = 0;
231 // load info about the i-th map into the MapInfo_Map_* globals
232 string MapInfo_BSPName_ByID(float i)
234 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
237 string unquote(string s)
242 for(i = 0; i < l; ++i)
245 ch = substring(s, i, 1);
246 if(ch != " ") if(ch != "\"")
248 for(j = strlen(s) - i - 1; j > 0; --j)
250 ch = substring(s, i+j, 1);
251 if(ch != " ") if(ch != "\"")
252 return substring(s, i, j+1);
254 return substring(s, i, 1);
260 float MapInfo_Get_ByID(float i)
262 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
267 string _MapInfo_Map_worldspawn_music;
269 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
279 float diameter, spawnpoints;
282 vector mapMins, mapMaxs;
285 fn = strcat("maps/", pFilename, ".ent");
286 fh = fopen(fn, FILE_READ);
290 fn = strcat("maps/", pFilename, ".bsp");
291 fh = fopen(fn, FILE_READ);
295 print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
298 MapInfo_Map_flags = 0;
299 MapInfo_Map_supportedGametypes = 0;
302 _MapInfo_Map_worldspawn_music = "";
306 if not((s = fgets(fh)))
308 if(inWorldspawn == 1)
309 if(startsWith(s, "}"))
315 if(k == "classname" && v == "worldspawn")
317 else if(k == "author")
318 MapInfo_Map_author = v;
319 else if(k == "_description")
320 MapInfo_Map_description = v;
321 else if(k == "music")
322 _MapInfo_Map_worldspawn_music = v;
323 else if(k == "noise")
324 _MapInfo_Map_worldspawn_music = v;
325 else if(k == "message")
327 i = strstrofs(v, " by ", 0);
328 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
330 MapInfo_Map_title = substring(v, 0, i);
331 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
334 MapInfo_Map_title = v;
341 o = stov(strcat("'", v, "'"));
342 mapMins_x = min(mapMins_x, o_x);
343 mapMins_y = min(mapMins_y, o_y);
344 mapMins_z = min(mapMins_z, o_z);
345 mapMaxs_x = max(mapMaxs_x, o_x);
346 mapMaxs_y = max(mapMaxs_y, o_y);
347 mapMaxs_z = max(mapMaxs_z, o_z);
349 else if(k == "race_place")
354 else if(k == "classname")
356 if(v == "dom_controlpoint")
357 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
358 else if(v == "item_flag_team2")
359 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
360 else if(v == "team_CTF_blueflag")
361 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
362 else if(v == "runematch_spawn_point")
363 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
364 else if(v == "target_assault_roundend")
365 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
366 else if(v == "onslaught_generator")
367 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
368 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
369 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
370 else if(v == "info_player_team1")
372 else if(v == "info_player_team2")
374 else if(v == "info_player_start")
376 else if(v == "info_player_deathmatch")
378 else if(v == "trigger_race_checkpoint")
379 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
380 else if(v == "weapon_nex")
382 else if(v == "weapon_railgun")
384 else if(startsWith(v, "weapon_"))
385 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
391 print(fn, " ended still in worldspawn, BUG\n");
394 diameter = vlen(mapMaxs - mapMins);
396 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE | MAPINFO_TYPE_NEXBALL);
397 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
399 // we have a CTF-only or Assault-only map. Don't add other modes then,
400 // as the map is too symmetric for them.
404 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
405 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH; // Rune always works
406 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
408 if(spawnpoints >= 8 && diameter > 4096) {
409 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
410 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
413 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
414 if(spawnpoints >= 12 && diameter > 5120)
415 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
418 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE)
421 MapInfo_Map_supportedGametypes &~= MAPINFO_TYPE_RACE;
422 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTS;
425 dprint("-> diameter ", ftos(diameter));
426 dprint("; spawnpoints ", ftos(spawnpoints));
427 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
434 void _MapInfo_Map_Reset()
436 MapInfo_Map_title = "<TITLE>";
437 MapInfo_Map_description = "<DESCRIPTION>";
438 MapInfo_Map_author = "<AUTHOR>";
439 MapInfo_Map_supportedGametypes = 0;
440 MapInfo_Map_supportedFeatures = 0;
441 MapInfo_Map_flags = 0;
442 MapInfo_Map_clientstuff = "";
443 MapInfo_Map_fog = "";
444 MapInfo_Map_mins = '0 0 0';
445 MapInfo_Map_maxs = '0 0 0';
448 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
451 MapInfo_Map_supportedGametypes |= pThisType;
452 if(!(pThisType & pWantedType))
455 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
457 cvar_set("fraglimit", "0");
461 cvar_set("fraglimit", car(s));
465 cvar_set("timelimit", car(s));
468 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
470 sa = car(s); if(sa == "") sa = "2";
471 cvar_set("g_tdm_teams", sa);
475 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
477 sa = car(s); if(sa == "") sa = "3";
478 cvar_set("g_keyhunt_teams", sa);
482 if(pWantedType == MAPINFO_TYPE_CTF)
484 sa = car(s); if(sa == "") sa = "10";
485 if(cvar("g_ctf_win_mode") < 2)
486 cvar_set("fraglimit", sa);
490 // rc = timelimit timelimit_qualification laps laps_teamplay
491 if(pWantedType == MAPINFO_TYPE_RACE)
493 sa = car(s); if(sa == "") sa = cvar_string("timelimit");
494 cvar_set("g_race_qualifying_timelimit", sa);
497 sa = car(s); if(sa == "") sa = "10";
498 if(cvar("g_race_teams") < 2)
499 cvar_set("fraglimit", sa);
502 sa = car(s); if(sa == "") sa = "20";
503 if(cvar("g_race_teams") >= 2)
504 cvar_set("fraglimit", sa);
508 if(pWantedType == MAPINFO_TYPE_CTS)
510 sa = car(s); if(sa == "") sa = cvar_string("fraglimit");
511 if(cvar("g_race_teams"))
512 cvar_set("fraglimit", sa);
516 sa = car(s); if(sa == "") sa = "0";
517 cvar_set("leadlimit", sa);
521 float MapInfo_Type_FromString(string t)
523 if (t == "dm") return MAPINFO_TYPE_DEATHMATCH;
524 else if(t == "tdm") return MAPINFO_TYPE_TEAM_DEATHMATCH;
525 else if(t == "dom") return MAPINFO_TYPE_DOMINATION;
526 else if(t == "ctf") return MAPINFO_TYPE_CTF;
527 else if(t == "rune") return MAPINFO_TYPE_RUNEMATCH;
528 else if(t == "lms") return MAPINFO_TYPE_LMS;
529 else if(t == "arena") return MAPINFO_TYPE_ARENA;
530 else if(t == "ca") return MAPINFO_TYPE_CA;
531 else if(t == "kh") return MAPINFO_TYPE_KEYHUNT;
532 else if(t == "as") return MAPINFO_TYPE_ASSAULT;
533 else if(t == "ons") return MAPINFO_TYPE_ONSLAUGHT;
534 else if(t == "rc") return MAPINFO_TYPE_RACE;
535 else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
536 else if(t == "cts") return MAPINFO_TYPE_CTS;
537 else if(t == "all") return MAPINFO_TYPE_ALL;
541 void _MapInfo_Parse_Settemp(string pFilename, string acl, string s, float recurse)
545 t = car(s); s = cdr(s);
547 // limited support of "" and comments
548 // remove trailing and leading " of t
549 if(substring(t, 0, 1) == "\"")
551 if(substring(t, -1, 1) == "\"")
552 t = substring(t, 1, -2);
555 // remove leading " of s
556 if(substring(s, 0, 1) == "\"")
558 s = substring(s, 1, -1);
560 // remove trailing " of s, and all that follows (cvar description)
561 o = strstrofs(s, "\"", 0);
563 s = substring(s, 0, o);
565 // remove // comments
566 o = strstrofs(s, "//", 0);
568 s = substring(s, 0, o);
570 // remove trailing spaces
571 while(substring(s, -1, 1) == " ")
572 s = substring(s, 0, -2);
578 fh = fopen(s, FILE_READ);
580 print("Map ", pFilename, " references not existing config file ", s, "\n");
585 if not((s = fgets(fh)))
588 // catch different sorts of comments
589 if(s == "") // empty lines
591 if(substring(s, 0, 1) == "#") // UNIX style
593 if(substring(s, 0, 2) == "//") // C++ style
595 if(substring(s, 0, 1) == "_") // q3map style
598 if(substring(s, 0, 4) == "set ")
599 s = substring(s, 4, -1);
600 if(substring(s, 0, 5) == "seta ")
601 s = substring(s, 5, -1);
603 _MapInfo_Parse_Settemp(pFilename, acl, s, recurse - 1);
609 print("Map ", pFilename, " uses too many levels of inclusion\n");
611 else if not(cvar_value_issafe(t))
612 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
613 else if not (cvar_value_issafe(s))
614 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
615 else if(substring(t, 0, 10) == "g_mapinfo_")
616 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
617 else if(matchacl(acl, t) <= 0)
618 print("Map ", pFilename, " contains a denied setting, ignored\n");
621 dprint("Applying temporary setting ", t, " := ", s, "\n");
622 if(cvar("g_campaign"))
623 cvar_set(t, s); // this is a wrapper and is always temporary anyway; no need to backup old values then
629 // load info about a map by name into the MapInfo_Map_* globals
630 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
638 acl = cvar_string("g_mapinfo_settemp_acl");
640 if(strstrofs(pFilename, "/", 0) >= 0)
642 print("Invalid character in map name, ignored\n");
646 if(pGametypeToSet == 0)
647 if(MapInfo_Cache_Retrieve(pFilename))
652 MapInfo_Map_bspname = pFilename;
654 // default all generic fields so they have "good" values in case something fails
655 fn = strcat("maps/", pFilename, ".mapinfo");
656 fh = fopen(fn, FILE_READ);
661 _MapInfo_Map_Reset();
662 r = _MapInfo_Generate(pFilename);
665 fh = fopen(fn, FILE_WRITE);
666 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
667 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
668 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
669 if(_MapInfo_Map_worldspawn_music != "")
672 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
674 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
676 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
678 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
682 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
683 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
686 i = floor(random() * n);
687 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
690 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
692 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
693 fputs(fh, "has weapons\n");
695 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
696 if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
697 fputs(fh, "frustrating\n");
698 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH) fputs(fh, "type dm 30 20\n");
699 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
700 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION) fputs(fh, "type dom 200 20\n");
701 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF) fputs(fh, "type ctf 300 20 10\n");
702 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH) fputs(fh, "type rune 200 20\n");
703 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS) fputs(fh, "type lms 9 20\n");
704 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA) fputs(fh, "type arena 10 20\n");
705 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CA) fputs(fh, "type ca 10 20\n");
706 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT) fputs(fh, "type kh 1000 20 3\n");
707 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT) fputs(fh, "type as 20\n");
708 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE) fputs(fh, "type rc 20 5 7 15\n");
709 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT) fputs(fh, "type ons 20\n");
710 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_NEXBALL) fputs(fh, "type nexball 5 20\n");
711 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTS) fputs(fh, "type cts 20 -1\n");
713 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
717 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
720 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
721 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
722 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
723 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
724 fputs(fh, "// optional: hidden\n");
729 fh = fopen(fn, FILE_READ);
731 error("... but I just wrote it!");
734 _MapInfo_Map_Reset();
737 if not((s = fgets(fh)))
740 // catch different sorts of comments
741 if(s == "") // empty lines
743 if(substring(s, 0, 1) == "#") // UNIX style
745 if(substring(s, 0, 2) == "//") // C++ style
747 if(substring(s, 0, 1) == "_") // q3map style
750 t = car(s); s = cdr(s);
752 MapInfo_Map_title = s;
753 else if(t == "description")
754 MapInfo_Map_description = s;
755 else if(t == "author")
756 MapInfo_Map_author = s;
759 t = car(s); s = cdr(s);
760 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
762 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
764 else if(t == "hidden")
766 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
768 else if(t == "forbidden")
770 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
772 else if(t == "frustrating")
774 MapInfo_Map_flags |= MAPINFO_FLAG_FRUSTRATING;
778 t = car(s); s = cdr(s);
779 f = MapInfo_Type_FromString(t);
781 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
783 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
788 t = car(s); s = cdr(s); a = stof(t);
789 t = car(s); s = cdr(s); b = stof(t);
790 t = car(s); s = cdr(s); c = stof(t);
791 t = car(s); s = cdr(s); d = stof(t);
792 t = car(s); s = cdr(s); e = stof(t);
794 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");
797 t = car(s); s = cdr(s); f = stof(t);
799 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");
802 if(a >= d || b >= e || c >= f)
803 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
806 MapInfo_Map_mins_x = a;
807 MapInfo_Map_mins_y = b;
808 MapInfo_Map_mins_z = c;
809 MapInfo_Map_maxs_x = d;
810 MapInfo_Map_maxs_y = e;
811 MapInfo_Map_maxs_z = f;
816 else if(t == "settemp_for_type")
818 t = car(s); s = cdr(s);
819 if((f = MapInfo_Type_FromString(t)))
821 if(f & pGametypeToSet)
823 _MapInfo_Parse_Settemp(pFilename, acl, s, 1);
828 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
831 else if(t == "clientsettemp_for_type")
833 t = car(s); s = cdr(s);
834 if((f = MapInfo_Type_FromString(t)))
836 if(f & pGametypeToSet)
838 t = car(s); s = cdr(s);
839 if not(cvar_value_issafe(t))
840 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
841 else if not (cvar_value_issafe(s))
842 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
845 dprint("Applying temporary client setting ", t, " := ", s, "\n");
846 MapInfo_Map_clientstuff = strcat(
847 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
854 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
859 if not(cvar_value_issafe(t))
860 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
864 else if(t == "cdtrack")
868 if not(cvar_value_issafe(t))
869 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
871 MapInfo_Map_clientstuff = strcat(
872 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
877 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
881 if(!MapInfo_Map_supportedGametypes)
882 _MapInfo_Map_ApplyGametype("30 20", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
885 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
886 error("Can't select the requested game type. Bailing out.");
887 MapInfo_Cache_Store();
888 if(MapInfo_Map_supportedGametypes != 0)
890 dprint("Map ", pFilename, " supports no game types, ignored\n");
894 float MapInfo_FindName(string s)
896 // if there is exactly one map of prefix s, return it
897 // if not, return the null string
898 // note that DP sorts glob results... so I can use a binary search
902 // invariants: r is behind s, l-1 is equal or before
905 m = floor((l + r) / 2);
906 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
907 cmp = strcasecmp(MapInfo_FindName_match, s);
909 return m; // found and good
911 l = m + 1; // l-1 is before s
915 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
916 MapInfo_FindName_firstResult = l;
917 // r == l, so: l is behind s, l-1 is before
918 // SO: if there is any, l is the one with the right prefix
919 // and l+1 may be one too
920 if(l == MapInfo_count)
922 MapInfo_FindName_match = string_null;
923 MapInfo_FindName_firstResult = -1;
924 return -1; // no MapInfo_FindName_match, behind last item
926 if(!startsWithNocase(MapInfo_FindName_match, s))
928 MapInfo_FindName_match = string_null;
929 MapInfo_FindName_firstResult = -1;
930 return -1; // wrong prefix
932 if(l == MapInfo_count - 1)
933 return l; // last one, nothing can follow => unique
934 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
936 MapInfo_FindName_match = string_null;
937 return -1; // ambigous MapInfo_FindName_match
942 string MapInfo_FixName(string s)
945 return MapInfo_FindName_match;
948 float MapInfo_CurrentFeatures()
952 if(!(cvar("g_lms") || cvar("g_instagib") || cvar("g_minstagib") || cvar("g_nixnex") || cvar("g_rocketarena") || !cvar("g_pickup_items") || cvar("g_race") || cvar("g_cts") || cvar("g_nexball")))
953 req |= MAPINFO_FEATURE_WEAPONS;
957 float MapInfo_CurrentGametype()
959 if(cvar("g_domination"))
960 return MAPINFO_TYPE_DOMINATION;
961 else if(cvar("g_ctf"))
962 return MAPINFO_TYPE_CTF;
963 else if(cvar("g_runematch"))
964 return MAPINFO_TYPE_RUNEMATCH;
965 else if(cvar("g_tdm"))
966 return MAPINFO_TYPE_TEAM_DEATHMATCH;
967 else if(cvar("g_assault"))
968 return MAPINFO_TYPE_ASSAULT;
969 else if(cvar("g_lms"))
970 return MAPINFO_TYPE_LMS;
971 else if(cvar("g_arena"))
972 return MAPINFO_TYPE_ARENA;
973 else if(cvar("g_ca"))
974 return MAPINFO_TYPE_CA;
975 else if(cvar("g_keyhunt"))
976 return MAPINFO_TYPE_KEYHUNT;
977 else if(cvar("g_onslaught"))
978 return MAPINFO_TYPE_ONSLAUGHT;
979 else if(cvar("g_race"))
980 return MAPINFO_TYPE_RACE;
981 else if(cvar("g_nexball"))
982 return MAPINFO_TYPE_NEXBALL;
983 else if(cvar("g_cts"))
984 return MAPINFO_TYPE_CTS;
986 return MAPINFO_TYPE_DEATHMATCH;
989 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
991 if(!MapInfo_Get_ByName(s, 1, 0))
993 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
995 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
1000 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
1003 r = _MapInfo_CheckMap(s);
1004 MapInfo_ClearTemps();
1008 string MapInfo_GetGameTypeCvar(float t)
1012 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
1013 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
1014 case MAPINFO_TYPE_DOMINATION: return "g_domination";
1015 case MAPINFO_TYPE_CTF: return "g_ctf";
1016 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
1017 case MAPINFO_TYPE_LMS: return "g_lms";
1018 case MAPINFO_TYPE_ARENA: return "g_arena";
1019 case MAPINFO_TYPE_CA: return "g_ca";
1020 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
1021 case MAPINFO_TYPE_ASSAULT: return "g_assault";
1022 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
1023 case MAPINFO_TYPE_RACE: return "g_race";
1024 case MAPINFO_TYPE_NEXBALL: return "g_nexball";
1025 case MAPINFO_TYPE_CTS: return "g_cts";
1030 void MapInfo_SwitchGameType(float t)
1032 cvar_set("gamecfg", "0");
1033 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "1" : "0");
1034 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
1035 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "1" : "0");
1036 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "1" : "0");
1037 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "1" : "0");
1038 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "1" : "0");
1039 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "1" : "0");
1040 cvar_set("g_ca", (t == MAPINFO_TYPE_CA) ? "1" : "0");
1041 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "1" : "0");
1042 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "1" : "0");
1043 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "1" : "0");
1044 cvar_set("g_race", (t == MAPINFO_TYPE_RACE) ? "1" : "0");
1045 cvar_set("g_nexball", (t == MAPINFO_TYPE_NEXBALL) ? "1" : "0");
1046 cvar_set("g_cts", (t == MAPINFO_TYPE_CTS) ? "1" : "0");
1049 void MapInfo_LoadMap(string s)
1051 MapInfo_Map_supportedGametypes = 0;
1052 if(!MapInfo_CheckMap(s))
1054 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
1055 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
1057 localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
1060 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
1065 // to make absolutely sure:
1066 MapInfo_Enumerate();
1067 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
1070 for(i = 0; i < MapInfo_count; ++i)
1071 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
1072 return substring(out, 1, strlen(out) - 1);
1075 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
1078 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
1080 if(MapInfo_Map_supportedGametypes <= 0)
1081 error("Mapinfo system is not functional at all. BAILED OUT.\n");
1084 while(!(MapInfo_Map_supportedGametypes & 1))
1087 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
1089 // t is now a supported mode!
1090 t0 = MapInfo_CurrentGametype();
1093 case MAPINFO_TYPE_RUNEMATCH:
1094 case MAPINFO_TYPE_TEAM_DEATHMATCH:
1095 case MAPINFO_TYPE_LMS:
1096 case MAPINFO_TYPE_ARENA:
1097 case MAPINFO_TYPE_CA:
1098 case MAPINFO_TYPE_KEYHUNT:
1099 case MAPINFO_TYPE_DEATHMATCH:
1100 print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
1101 cvar_set("timelimit", "0");
1102 cvar_set("fraglimit", "0");
1103 cvar_set("g_keyhunt_teams", "3");
1106 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
1107 MapInfo_SwitchGameType(t);
1111 cvar_settemp_restore();
1112 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
1115 void MapInfo_ClearTemps()
1117 MapInfo_Map_bspname = string_null;
1118 MapInfo_Map_title = string_null;
1119 MapInfo_Map_description = string_null;
1120 MapInfo_Map_author = string_null;
1121 MapInfo_Map_clientstuff = string_null;
1122 MapInfo_Map_supportedGametypes = 0;
1123 MapInfo_Map_supportedFeatures = 0;
1126 void MapInfo_Shutdown()
1128 MapInfo_ClearTemps();
1129 MapInfo_Filter_Free();
1130 MapInfo_Cache_Destroy();
1131 if(_MapInfo_globopen)
1133 search_end(_MapInfo_globhandle);
1134 _MapInfo_globhandle = -1;
1135 _MapInfo_globopen = FALSE;
1139 float MapInfo_ForbiddenFlags()
1142 f = MAPINFO_FLAG_FORBIDDEN;
1145 if not(cvar("g_maplist_allow_hidden"))
1147 f |= MAPINFO_FLAG_HIDDEN;
1149 if not(cvar("g_maplist_allow_frustrating"))
1150 f |= MAPINFO_FLAG_FRUSTRATING;
1155 float MapInfo_RequiredFlags()
1160 if(cvar("g_maplist_allow_frustrating") > 1)
1161 f |= MAPINFO_FLAG_FRUSTRATING;