2 // HUGE SET - stored in a string
3 string HugeSetOfIntegers_empty()
7 float HugeSetOfIntegers_get(string pArr, float i)
9 return stof(substring(pArr, i * 4, 4));
11 float HugeSetOfIntegers_length(string pArr)
13 return strlen(pArr) / 4;
15 string HugeSetOfIntegers_concat(string a1, string a2)
17 return strcat(a1, a2);
19 string HugeSetOfIntegers_insert(string a1, float n, string a2)
20 // special concat function to build up large lists in less time by binary concatenation
23 s = strcat(" ", ftos(n));
24 return strcat(a1, substring(s, strlen(s) - 4, 4), a2);
28 // generic string stuff
29 float startsWith(string haystack, string needle)
31 return substring(haystack, 0, strlen(needle)) == needle;
33 float startsWithNocase(string haystack, string needle)
35 return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
37 string extractRestOfLine(string haystack, string needle)
39 if(startsWith(haystack, needle))
40 return substring(haystack, strlen(needle), strlen(haystack) - strlen(needle));
46 o = strstrofs(s, " ", 0);
49 return substring(s, 0, o);
54 o = strstrofs(s, " ", 0);
57 return substring(s, o + 1, strlen(s) - (o + 1));
60 float _MapInfo_Cache_Active;
61 float _MapInfo_Cache_DB_NameToIndex;
62 float _MapInfo_Cache_Buf_IndexToMapData;
64 void MapInfo_Cache_Destroy()
66 if(!_MapInfo_Cache_Active)
69 db_close(_MapInfo_Cache_DB_NameToIndex);
70 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
71 _MapInfo_Cache_Active = 0;
74 void MapInfo_Cache_Create()
76 MapInfo_Cache_Destroy();
77 _MapInfo_Cache_DB_NameToIndex = db_create();
78 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
79 _MapInfo_Cache_Active = 1;
82 void MapInfo_Cache_Invalidate()
84 if(!_MapInfo_Cache_Active)
87 MapInfo_Cache_Create();
90 void MapInfo_Cache_Store()
94 if(!_MapInfo_Cache_Active)
97 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
98 if(!s) // empty string is NOT valid here!
100 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
101 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
106 // now store all the stuff
107 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
108 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
109 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
110 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
111 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
112 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
113 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
116 float MapInfo_Cache_Retrieve(string map)
120 if(!_MapInfo_Cache_Active)
123 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
128 // now retrieve all the stuff
129 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
130 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
131 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
132 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
133 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
134 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
135 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
139 // GLOB HANDLING (for all BSP files)
140 float _MapInfo_globopen;
141 float _MapInfo_globcount;
142 float _MapInfo_globhandle;
143 string _MapInfo_GlobItem(float i)
146 s = search_getfilename(_MapInfo_globhandle, i);
147 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
150 void MapInfo_Enumerate()
152 if(_MapInfo_globopen)
153 search_end(_MapInfo_globhandle);
154 MapInfo_Cache_Invalidate();
155 _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
156 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
157 _MapInfo_globopen = 1;
160 // filter the info by game type mask (updates MapInfo_count)
163 string _MapInfo_filtered;
164 float MapInfo_FilterList_Lookup(float i)
166 return MapInfo_FilterList_Lookup(i);
169 string MapInfo_FilterGametype_Recursive(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pBegin, float pEnd, float pAbortOnGenerate)
175 return HugeSetOfIntegers_empty();
177 m = floor((pBegin + pEnd) / 2);
179 l = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pBegin, m, pAbortOnGenerate);
181 return string_null; // BAIL OUT
182 if(MapInfo_Get_ByName(_MapInfo_GlobItem(m), 1, 0) == 2) // if we generated one... BAIL OUT and let the caller continue in the next frame.
185 MapInfo_progress = m / _MapInfo_globcount;
186 return string_null; // BAIL OUT
188 valid = (((MapInfo_Map_supportedGametypes & pGametype) != 0);
189 valid = valid && ((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures));
190 valid = valid && (MapInfo_Map_flags & pFlagsForbidden == 0);
191 valid = valid && (MapInfo_Map_flags & pFlagsRequired == pFlagsRequired);
192 r = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pFlagsRequired, pFlagsForbidden, m + 1, pEnd, pAbortOnGenerate);
194 return string_null; // BAIL OUT
197 return HugeSetOfIntegers_insert(l, m, r);
199 return HugeSetOfIntegers_concat(l, r);
202 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
204 if(_MapInfo_filtered)
205 strunzone(_MapInfo_filtered);
206 _MapInfo_filtered = MapInfo_FilterGametype_Recursive(pGametype, pFeatures, pFlagsRequired, pFlagsForbidden, 0, _MapInfo_globcount, pAbortOnGenerate);
207 if not(_MapInfo_filtered)
209 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
212 _MapInfo_filtered = strzone(_MapInfo_filtered);
213 MapInfo_count = HugeSetOfIntegers_length(_MapInfo_filtered);
214 MapInfo_ClearTemps();
218 void MapInfo_Filter_Free()
220 if(_MapInfo_filtered)
222 strunzone(_MapInfo_filtered);
223 _MapInfo_filtered = string_null;
227 float _MapInfo_filtered;
228 float _MapInfo_filtered_allocated;
229 float MapInfo_FilterList_Lookup(float i)
231 return stof(bufstr_get(_MapInfo_filtered, i));
234 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
237 if not(_MapInfo_filtered_allocated)
239 _MapInfo_filtered_allocated = 1;
240 _MapInfo_filtered = buf_create();
243 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
245 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.
248 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
249 MapInfo_progress = i / _MapInfo_globcount;
252 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
253 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
254 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
255 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
256 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
258 MapInfo_count = j + 1;
259 MapInfo_ClearTemps();
263 void MapInfo_Filter_Free()
265 if(_MapInfo_filtered_allocated)
267 buf_del(_MapInfo_filtered);
268 _MapInfo_filtered_allocated = 0;
273 // load info about the i-th map into the MapInfo_Map_* globals
274 string MapInfo_BSPName_ByID(float i)
276 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
279 string unquote(string s)
284 for(i = 0; i < l; ++i)
287 ch = substring(s, i, 1);
288 if(ch != " ") if(ch != "\"")
290 for(j = strlen(s) - i - 1; j > 0; --j)
292 ch = substring(s, i+j, 1);
293 if(ch != " ") if(ch != "\"")
294 return substring(s, i, j+1);
296 return substring(s, i, 1);
302 float MapInfo_Get_ByID(float i)
304 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
309 string _MapInfo_Map_worldspawn_music;
311 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
321 float diameter, spawnpoints;
323 vector mapMins, mapMaxs;
326 fn = strcat("maps/", pFilename, ".ent");
327 fh = fopen(fn, FILE_READ);
331 fn = strcat("maps/", pFilename, ".bsp");
332 fh = fopen(fn, FILE_READ);
336 print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
339 MapInfo_Map_supportedGametypes = 0;
341 _MapInfo_Map_worldspawn_music = "";
345 if not((s = fgets(fh)))
347 if(inWorldspawn == 1)
348 if(startsWith(s, "}"))
354 if(k == "classname" && v == "worldspawn")
356 else if(k == "author")
357 MapInfo_Map_author = v;
358 else if(k == "_description")
359 MapInfo_Map_description = v;
360 else if(k == "music")
361 _MapInfo_Map_worldspawn_music = v;
362 else if(k == "noise")
363 _MapInfo_Map_worldspawn_music = v;
364 else if(k == "message")
366 i = strstrofs(v, " by ", 0);
367 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
369 MapInfo_Map_title = substring(v, 0, i);
370 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
373 MapInfo_Map_title = v;
380 o = stov(strcat("'", v, "'"));
381 mapMins_x = min(mapMins_x, o_x);
382 mapMins_y = min(mapMins_y, o_y);
383 mapMins_z = min(mapMins_z, o_z);
384 mapMaxs_x = max(mapMaxs_x, o_x);
385 mapMaxs_y = max(mapMaxs_y, o_y);
386 mapMaxs_z = max(mapMaxs_z, o_z);
388 else if(k == "classname")
390 if(v == "dom_controlpoint")
391 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
392 else if(v == "item_flag_team2")
393 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
394 else if(v == "team_CTF_blueflag")
395 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
396 else if(v == "runematch_spawn_point")
397 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
398 else if(v == "target_assault_roundend")
399 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
400 else if(v == "onslaught_generator")
401 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
402 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
403 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
404 else if(v == "info_player_team1")
406 else if(v == "info_player_team2")
408 else if(v == "info_player_start")
410 else if(v == "info_player_deathmatch")
412 else if(v == "trigger_race_checkpoint")
413 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
414 else if(v == "weapon_nex")
416 else if(v == "weapon_railgun")
418 else if(startsWith(v, "weapon_"))
419 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
425 print(fn, " ended still in worldspawn, BUG\n");
428 diameter = vlen(mapMaxs - mapMins);
430 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE);
431 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
433 // we have a CTF-only or Assault-only map. Don't add other modes then,
434 // as the map is too symmetric for them.
438 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
439 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH; // Rune always works
440 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
442 if(spawnpoints >= 8 && diameter > 4096)
443 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
445 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
446 if(spawnpoints >= 12 && diameter > 5120)
447 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
450 dprint("-> diameter ", ftos(diameter));
451 dprint("; spawnpoints ", ftos(spawnpoints));
452 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
459 void _MapInfo_Map_Reset()
461 MapInfo_Map_title = "<TITLE>";
462 MapInfo_Map_description = "<DESCRIPTION>";
463 MapInfo_Map_author = "<AUTHOR>";
464 MapInfo_Map_supportedGametypes = 0;
465 MapInfo_Map_supportedFeatures = 0;
466 MapInfo_Map_flags = 0;
467 MapInfo_Map_clientstuff = "";
468 MapInfo_Map_fog = "";
469 MapInfo_Map_mins = '0 0 0';
470 MapInfo_Map_maxs = '0 0 0';
473 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
476 MapInfo_Map_supportedGametypes |= pThisType;
477 if(!(pThisType & pWantedType))
480 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT) // these modes don't use fraglimit
482 cvar_set("fraglimit", "0");
486 cvar_set("fraglimit", car(s));
490 cvar_set("timelimit", car(s));
493 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
495 sa = car(s); if(sa == "") sa = "2";
496 cvar_set("g_tdm_teams", sa);
500 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
502 sa = car(s); if(sa == "") sa = "3";
503 cvar_set("g_keyhunt_teams", sa);
507 if(pWantedType == MAPINFO_TYPE_CTF)
509 sa = car(s); if(sa == "") sa = "10";
510 if(cvar("g_ctf_win_mode") < 2)
511 cvar_set("fraglimit", sa);
515 if(pWantedType == MAPINFO_TYPE_RACE)
517 sa = car(s); if(sa == "") sa = cvar_string("fraglimit");
518 if(cvar("g_race_teams"))
519 cvar_set("fraglimit", sa);
524 float MapInfo_Type_FromString(string t)
526 if (t == "dm") return MAPINFO_TYPE_DEATHMATCH;
527 else if(t == "tdm") return MAPINFO_TYPE_TEAM_DEATHMATCH;
528 else if(t == "dom") return MAPINFO_TYPE_DOMINATION;
529 else if(t == "ctf") return MAPINFO_TYPE_CTF;
530 else if(t == "rune") return MAPINFO_TYPE_RUNEMATCH;
531 else if(t == "lms") return MAPINFO_TYPE_LMS;
532 else if(t == "arena") return MAPINFO_TYPE_ARENA;
533 else if(t == "kh") return MAPINFO_TYPE_KEYHUNT;
534 else if(t == "as") return MAPINFO_TYPE_ASSAULT;
535 else if(t == "ons") return MAPINFO_TYPE_ONSLAUGHT;
536 else if(t == "race") return MAPINFO_TYPE_RACE;
537 else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
538 else if(t == "all") return MAPINFO_TYPE_ALL;
542 // load info about a map by name into the MapInfo_Map_* globals
543 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
550 if(strstrofs(pFilename, "/", 0) >= 0)
552 print("Invalid character in map name, ignored\n");
556 if(pGametypeToSet == 0)
557 if(MapInfo_Cache_Retrieve(pFilename))
562 MapInfo_Map_bspname = pFilename;
564 // default all generic fields so they have "good" values in case something fails
565 fn = strcat("maps/", pFilename, ".mapinfo");
566 fh = fopen(fn, FILE_READ);
571 _MapInfo_Map_Reset();
572 r = _MapInfo_Generate(pFilename);
575 fh = fopen(fn, FILE_WRITE);
576 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
577 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
578 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
579 if(_MapInfo_Map_worldspawn_music != "")
582 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
584 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
586 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
588 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
592 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
593 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
596 i = floor(random() * n);
597 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
600 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
602 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
603 fputs(fh, "has weapons\n");
605 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
606 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH) fputs(fh, "type dm 30 20\n");
607 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
608 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION) fputs(fh, "type dom 200 20\n");
609 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF) fputs(fh, "type ctf 300 20 10\n");
610 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH) fputs(fh, "type rune 200 20\n");
611 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS) fputs(fh, "type lms 9 20\n");
612 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA) fputs(fh, "type arena 10 20\n");
613 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT) fputs(fh, "type kh 1000 20 3\n");
614 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT) fputs(fh, "type as 20\n");
615 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE) fputs(fh, "type race 5 20 15\n");
616 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT) fputs(fh, "type ons 20\n");
617 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_NEXBALL) fputs(fh, "type nexball 5 20\n");
619 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
623 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
626 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
627 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
628 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
629 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
630 fputs(fh, "// optional: hidden\n");
635 fh = fopen(fn, FILE_READ);
637 error("... but I just wrote it!");
640 _MapInfo_Map_Reset();
643 if not((s = fgets(fh)))
646 // catch different sorts of comments
647 if(s == "") // empty lines
649 if(substring(s, 0, 1) == "#") // UNIX style
651 if(substring(s, 0, 2) == "//") // C++ style
653 if(substring(s, 0, 1) == "_") // q3map style
656 t = car(s); s = cdr(s);
658 MapInfo_Map_title = s;
659 else if(t == "description")
660 MapInfo_Map_description = s;
661 else if(t == "author")
662 MapInfo_Map_author = s;
665 t = car(s); s = cdr(s);
666 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
668 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
670 else if(t == "hidden")
672 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
674 else if(t == "forbidden")
676 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
680 t = car(s); s = cdr(s);
681 f = MapInfo_Type_FromString(t);
683 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
685 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
690 t = car(s); s = cdr(s); a = stof(t);
691 t = car(s); s = cdr(s); b = stof(t);
692 t = car(s); s = cdr(s); c = stof(t);
693 t = car(s); s = cdr(s); d = stof(t);
694 t = car(s); s = cdr(s); e = stof(t);
696 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");
699 t = car(s); s = cdr(s); f = stof(t);
701 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");
704 if(a >= d || b >= e || c >= f)
705 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
708 MapInfo_Map_mins_x = a;
709 MapInfo_Map_mins_y = b;
710 MapInfo_Map_mins_z = c;
711 MapInfo_Map_maxs_x = d;
712 MapInfo_Map_maxs_y = e;
713 MapInfo_Map_maxs_z = f;
718 else if(t == "settemp_for_type")
720 t = car(s); s = cdr(s);
721 if((f = MapInfo_Type_FromString(t)))
723 if(f & pGametypeToSet)
725 t = car(s); s = cdr(s);
726 if not(cvar_value_issafe(t))
727 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
728 else if not (cvar_value_issafe(s))
729 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
732 dprint("Applying temporary setting ", t, " := ", s, "\n");
739 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
742 else if(t == "clientsettemp_for_type")
744 t = car(s); s = cdr(s);
745 if((f = MapInfo_Type_FromString(t)))
747 if(f & pGametypeToSet)
749 t = car(s); s = cdr(s);
750 if not(cvar_value_issafe(t))
751 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
752 else if not (cvar_value_issafe(s))
753 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
756 dprint("Applying temporary client setting ", t, " := ", s, "\n");
757 MapInfo_Map_clientstuff = strcat(
758 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
765 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
770 if not(cvar_value_issafe(t))
771 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
775 else if(t == "cdtrack")
779 if not(cvar_value_issafe(t))
780 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
782 MapInfo_Map_clientstuff = strcat(
783 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
788 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
792 if(!MapInfo_Map_supportedGametypes)
793 _MapInfo_Map_ApplyGametype("30 20", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
796 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
797 error("Can't select the requested game type. Bailing out.");
798 MapInfo_Cache_Store();
799 if(MapInfo_Map_supportedGametypes != 0)
801 dprint("Map ", pFilename, " supports no game types, ignored\n");
805 float MapInfo_FindName(string s)
807 // if there is exactly one map of prefix s, return it
808 // if not, return the null string
809 // note that DP sorts glob results... so I can use a binary search
813 // invariants: r is behind s, l-1 is equal or before
816 m = floor((l + r) / 2);
817 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
818 cmp = strcasecmp(MapInfo_FindName_match, s);
820 return m; // found and good
822 l = m + 1; // l-1 is before s
826 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
827 MapInfo_FindName_firstResult = l;
828 // r == l, so: l is behind s, l-1 is before
829 // SO: if there is any, l is the one with the right prefix
830 // and l+1 may be one too
831 if(l == MapInfo_count)
833 MapInfo_FindName_match = string_null;
834 MapInfo_FindName_firstResult = -1;
835 return -1; // no MapInfo_FindName_match, behind last item
837 if(!startsWithNocase(MapInfo_FindName_match, s))
839 MapInfo_FindName_match = string_null;
840 MapInfo_FindName_firstResult = -1;
841 return -1; // wrong prefix
843 if(l == MapInfo_count - 1)
844 return l; // last one, nothing can follow => unique
845 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
847 MapInfo_FindName_match = string_null;
848 return -1; // ambigous MapInfo_FindName_match
853 string MapInfo_FixName(string s)
856 return MapInfo_FindName_match;
859 float MapInfo_CurrentFeatures()
863 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_nexball")))
864 req |= MAPINFO_FEATURE_WEAPONS;
868 float MapInfo_CurrentGametype()
870 if(cvar("g_domination"))
871 return MAPINFO_TYPE_DOMINATION;
872 else if(cvar("g_ctf"))
873 return MAPINFO_TYPE_CTF;
874 else if(cvar("g_runematch"))
875 return MAPINFO_TYPE_RUNEMATCH;
876 else if(cvar("g_tdm"))
877 return MAPINFO_TYPE_TEAM_DEATHMATCH;
878 else if(cvar("g_assault"))
879 return MAPINFO_TYPE_ASSAULT;
880 else if(cvar("g_lms"))
881 return MAPINFO_TYPE_LMS;
882 else if(cvar("g_arena"))
883 return MAPINFO_TYPE_ARENA;
884 else if(cvar("g_keyhunt"))
885 return MAPINFO_TYPE_KEYHUNT;
886 else if(cvar("g_onslaught"))
887 return MAPINFO_TYPE_ONSLAUGHT;
888 else if(cvar("g_race"))
889 return MAPINFO_TYPE_RACE;
890 else if(cvar("g_nexball"))
891 return MAPINFO_TYPE_NEXBALL;
893 return MAPINFO_TYPE_DEATHMATCH;
896 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
898 if(!MapInfo_Get_ByName(s, 1, 0))
900 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
902 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
907 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
910 r = _MapInfo_CheckMap(s);
911 MapInfo_ClearTemps();
915 string MapInfo_GetGameTypeCvar(float t)
919 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
920 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
921 case MAPINFO_TYPE_DOMINATION: return "g_domination";
922 case MAPINFO_TYPE_CTF: return "g_ctf";
923 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
924 case MAPINFO_TYPE_LMS: return "g_lms";
925 case MAPINFO_TYPE_ARENA: return "g_arena";
926 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
927 case MAPINFO_TYPE_ASSAULT: return "g_assault";
928 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
929 case MAPINFO_TYPE_RACE: return "g_race";
934 void MapInfo_SwitchGameType(float t)
936 cvar_set("gamecfg", "0");
937 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "1" : "0");
938 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
939 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "1" : "0");
940 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "1" : "0");
941 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "1" : "0");
942 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "1" : "0");
943 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "1" : "0");
944 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "1" : "0");
945 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "1" : "0");
946 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "1" : "0");
947 cvar_set("g_race", (t == MAPINFO_TYPE_RACE) ? "1" : "0");
948 cvar_set("g_nexball", (t == MAPINFO_TYPE_NEXBALL) ? "1" : "0");
951 void MapInfo_LoadMap(string s)
953 MapInfo_Map_supportedGametypes = 0;
954 if(!MapInfo_CheckMap(s))
956 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
957 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
959 localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
962 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
967 // to make absolutely sure:
969 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
972 for(i = 0; i < MapInfo_count; ++i)
973 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
974 return substring(out, 1, strlen(out) - 1);
977 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
980 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
982 if(MapInfo_Map_supportedGametypes <= 0)
983 error("Mapinfo system is not functional at all. BAILED OUT.\n");
986 while(!(MapInfo_Map_supportedGametypes & 1))
989 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
991 // t is now a supported mode!
992 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
993 MapInfo_SwitchGameType(t);
995 cvar_settemp_restore();
996 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
999 void MapInfo_ClearTemps()
1001 MapInfo_Map_bspname = string_null;
1002 MapInfo_Map_title = string_null;
1003 MapInfo_Map_description = string_null;
1004 MapInfo_Map_author = string_null;
1005 MapInfo_Map_clientstuff = string_null;
1006 MapInfo_Map_supportedGametypes = 0;
1007 MapInfo_Map_supportedFeatures = 0;
1010 void MapInfo_Shutdown()
1012 MapInfo_ClearTemps();
1013 MapInfo_Filter_Free();
1014 MapInfo_Cache_Destroy();
1015 if(_MapInfo_globopen)
1017 search_end(_MapInfo_globhandle);
1018 _MapInfo_globhandle = -1;
1019 _MapInfo_globopen = FALSE;