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));
33 float _MapInfo_Cache_Active;
34 float _MapInfo_Cache_DB_NameToIndex;
35 float _MapInfo_Cache_Buf_IndexToMapData;
37 void MapInfo_Cache_Destroy()
39 if(!_MapInfo_Cache_Active)
42 db_close(_MapInfo_Cache_DB_NameToIndex);
43 buf_del(_MapInfo_Cache_Buf_IndexToMapData);
44 _MapInfo_Cache_Active = 0;
47 void MapInfo_Cache_Create()
49 MapInfo_Cache_Destroy();
50 _MapInfo_Cache_DB_NameToIndex = db_create();
51 _MapInfo_Cache_Buf_IndexToMapData = buf_create();
52 _MapInfo_Cache_Active = 1;
55 void MapInfo_Cache_Invalidate()
57 if(!_MapInfo_Cache_Active)
60 MapInfo_Cache_Create();
63 void MapInfo_Cache_Store()
67 if(!_MapInfo_Cache_Active)
70 s = db_get(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname);
71 if(!s) // empty string is NOT valid here!
73 i = buf_getsize(_MapInfo_Cache_Buf_IndexToMapData);
74 db_put(_MapInfo_Cache_DB_NameToIndex, MapInfo_Map_bspname, ftos(i));
79 // now store all the stuff
80 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, i, MapInfo_Map_bspname);
81 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_title);
82 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_description);
83 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, MapInfo_Map_author);
84 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedGametypes));
85 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_supportedFeatures));
86 bufstr_set(_MapInfo_Cache_Buf_IndexToMapData, ++i, ftos(MapInfo_Map_flags));
89 float MapInfo_Cache_Retrieve(string map)
93 if(!_MapInfo_Cache_Active)
96 s = db_get(_MapInfo_Cache_DB_NameToIndex, map);
101 // now retrieve all the stuff
102 MapInfo_Map_bspname = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, i);
103 MapInfo_Map_title = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
104 MapInfo_Map_description = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
105 MapInfo_Map_author = bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i);
106 MapInfo_Map_supportedGametypes = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
107 MapInfo_Map_supportedFeatures = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
108 MapInfo_Map_flags = stof(bufstr_get(_MapInfo_Cache_Buf_IndexToMapData, ++i));
112 // GLOB HANDLING (for all BSP files)
113 float _MapInfo_globopen;
114 float _MapInfo_globcount;
115 float _MapInfo_globhandle;
116 string _MapInfo_GlobItem(float i)
119 s = search_getfilename(_MapInfo_globhandle, i);
120 return substring(s, 5, strlen(s) - 9); // without maps/ and .bsp
123 void MapInfo_Enumerate()
125 if(_MapInfo_globopen)
126 search_end(_MapInfo_globhandle);
127 MapInfo_Cache_Invalidate();
128 _MapInfo_globhandle = search_begin("maps/*.bsp", TRUE, TRUE);
129 _MapInfo_globcount = search_getsize(_MapInfo_globhandle);
130 _MapInfo_globopen = 1;
133 // filter the info by game type mask (updates MapInfo_count)
135 float _MapInfo_filtered;
136 float _MapInfo_filtered_allocated;
137 float MapInfo_FilterList_Lookup(float i)
139 return stof(bufstr_get(_MapInfo_filtered, i));
142 void _MapInfo_FilterList_swap(float i, float j, entity pass)
145 h = bufstr_get(_MapInfo_filtered, i);
146 bufstr_set(_MapInfo_filtered, i, bufstr_get(_MapInfo_filtered, j));
147 bufstr_set(_MapInfo_filtered, j, h);
150 float _MapInfo_FilterList_cmp(float i, float j, entity pass)
153 a = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, i)));
154 b = _MapInfo_GlobItem(stof(bufstr_get(_MapInfo_filtered, j)));
155 return strcasecmp(a, b);
158 float MapInfo_FilterGametype(float pGametype, float pFeatures, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate)
161 if not(_MapInfo_filtered_allocated)
163 _MapInfo_filtered_allocated = 1;
164 _MapInfo_filtered = buf_create();
167 for(i = 0, j = -1; i < _MapInfo_globcount; ++i)
169 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.
172 dprint("Autogenerated a .mapinfo, doing the rest later.\n");
173 MapInfo_progress = i / _MapInfo_globcount;
176 if((MapInfo_Map_supportedGametypes & pGametype) != 0)
177 if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures)
178 if((MapInfo_Map_flags & pFlagsForbidden) == 0)
179 if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired)
180 bufstr_set(_MapInfo_filtered, ++j, ftos(i));
182 MapInfo_count = j + 1;
183 MapInfo_ClearTemps();
185 // sometimes the glob isn't sorted nicely, so fix it here...
186 heapsort(MapInfo_count, _MapInfo_FilterList_swap, _MapInfo_FilterList_cmp, world);
191 void MapInfo_Filter_Free()
193 if(_MapInfo_filtered_allocated)
195 buf_del(_MapInfo_filtered);
196 _MapInfo_filtered_allocated = 0;
200 // load info about the i-th map into the MapInfo_Map_* globals
201 string MapInfo_BSPName_ByID(float i)
203 return _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i));
206 string unquote(string s)
211 for(i = 0; i < l; ++i)
214 ch = substring(s, i, 1);
215 if(ch != " ") if(ch != "\"")
217 for(j = strlen(s) - i - 1; j > 0; --j)
219 ch = substring(s, i+j, 1);
220 if(ch != " ") if(ch != "\"")
221 return substring(s, i, j+1);
223 return substring(s, i, 1);
229 float MapInfo_Get_ByID(float i)
231 if(MapInfo_Get_ByName(MapInfo_BSPName_ByID(i), 0, 0))
236 string _MapInfo_Map_worldspawn_music;
238 float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
248 float diameter, spawnpoints;
250 vector mapMins, mapMaxs;
253 fn = strcat("maps/", pFilename, ".ent");
254 fh = fopen(fn, FILE_READ);
258 fn = strcat("maps/", pFilename, ".bsp");
259 fh = fopen(fn, FILE_READ);
263 print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
266 MapInfo_Map_supportedGametypes = 0;
268 _MapInfo_Map_worldspawn_music = "";
272 if not((s = fgets(fh)))
274 if(inWorldspawn == 1)
275 if(startsWith(s, "}"))
281 if(k == "classname" && v == "worldspawn")
283 else if(k == "author")
284 MapInfo_Map_author = v;
285 else if(k == "_description")
286 MapInfo_Map_description = v;
287 else if(k == "music")
288 _MapInfo_Map_worldspawn_music = v;
289 else if(k == "noise")
290 _MapInfo_Map_worldspawn_music = v;
291 else if(k == "message")
293 i = strstrofs(v, " by ", 0);
294 if(MapInfo_Map_author == "<AUTHOR>" && i >= 0)
296 MapInfo_Map_title = substring(v, 0, i);
297 MapInfo_Map_author = substring(v, i + 4, strlen(v) - (i + 4));
300 MapInfo_Map_title = v;
307 o = stov(strcat("'", v, "'"));
308 mapMins_x = min(mapMins_x, o_x);
309 mapMins_y = min(mapMins_y, o_y);
310 mapMins_z = min(mapMins_z, o_z);
311 mapMaxs_x = max(mapMaxs_x, o_x);
312 mapMaxs_y = max(mapMaxs_y, o_y);
313 mapMaxs_z = max(mapMaxs_z, o_z);
315 else if(k == "classname")
317 if(v == "dom_controlpoint")
318 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DOMINATION;
319 else if(v == "item_flag_team2")
320 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
321 else if(v == "team_CTF_blueflag")
322 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CTF;
323 else if(v == "runematch_spawn_point")
324 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;
325 else if(v == "target_assault_roundend")
326 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ASSAULT;
327 else if(v == "onslaught_generator")
328 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ONSLAUGHT;
329 else if(substring(v, 0, 8) == "nexball_" || substring(v, 0, 4) == "ball")
330 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_NEXBALL;
331 else if(v == "info_player_team1")
333 else if(v == "info_player_team2")
335 else if(v == "info_player_start")
337 else if(v == "info_player_deathmatch")
339 else if(v == "trigger_race_checkpoint")
340 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RACE;
341 else if(v == "weapon_nex")
343 else if(v == "weapon_railgun")
345 else if(startsWith(v, "weapon_"))
346 MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
352 print(fn, " ended still in worldspawn, BUG\n");
355 diameter = vlen(mapMaxs - mapMins);
357 twoBaseModes = MapInfo_Map_supportedGametypes & (MAPINFO_TYPE_CTF | MAPINFO_TYPE_ASSAULT | MAPINFO_TYPE_RACE);
358 if(twoBaseModes && (MapInfo_Map_supportedGametypes == twoBaseModes))
360 // we have a CTF-only or Assault-only map. Don't add other modes then,
361 // as the map is too symmetric for them.
365 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH; // DM always works
366 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH; // Rune always works
367 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS; // LMS always works
369 if(spawnpoints >= 8 && diameter > 4096)
370 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
372 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_ARENA;
373 if(spawnpoints >= 12 && diameter > 5120)
374 MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEYHUNT;
377 dprint("-> diameter ", ftos(diameter));
378 dprint("; spawnpoints ", ftos(spawnpoints));
379 dprint("; modes ", ftos(MapInfo_Map_supportedGametypes), "\n");
386 void _MapInfo_Map_Reset()
388 MapInfo_Map_title = "<TITLE>";
389 MapInfo_Map_description = "<DESCRIPTION>";
390 MapInfo_Map_author = "<AUTHOR>";
391 MapInfo_Map_supportedGametypes = 0;
392 MapInfo_Map_supportedFeatures = 0;
393 MapInfo_Map_flags = 0;
394 MapInfo_Map_clientstuff = "";
395 MapInfo_Map_fog = "";
396 MapInfo_Map_mins = '0 0 0';
397 MapInfo_Map_maxs = '0 0 0';
400 void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType)
403 MapInfo_Map_supportedGametypes |= pThisType;
404 if(!(pThisType & pWantedType))
407 if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT) // these modes don't use fraglimit
409 cvar_set("fraglimit", "0");
413 cvar_set("fraglimit", car(s));
417 cvar_set("timelimit", car(s));
420 if(pWantedType == MAPINFO_TYPE_TEAM_DEATHMATCH)
422 sa = car(s); if(sa == "") sa = "2";
423 cvar_set("g_tdm_teams", sa);
427 if(pWantedType == MAPINFO_TYPE_KEYHUNT)
429 sa = car(s); if(sa == "") sa = "3";
430 cvar_set("g_keyhunt_teams", sa);
434 if(pWantedType == MAPINFO_TYPE_CTF)
436 sa = car(s); if(sa == "") sa = "10";
437 if(cvar("g_ctf_win_mode") < 2)
438 cvar_set("fraglimit", sa);
442 if(pWantedType == MAPINFO_TYPE_RACE)
444 sa = car(s); if(sa == "") sa = cvar_string("fraglimit");
445 if(cvar("g_race_teams"))
446 cvar_set("fraglimit", sa);
450 sa = car(s); if(sa == "") sa = "0";
451 cvar_set("leadlimit", sa);
455 float MapInfo_Type_FromString(string t)
457 if (t == "dm") return MAPINFO_TYPE_DEATHMATCH;
458 else if(t == "tdm") return MAPINFO_TYPE_TEAM_DEATHMATCH;
459 else if(t == "dom") return MAPINFO_TYPE_DOMINATION;
460 else if(t == "ctf") return MAPINFO_TYPE_CTF;
461 else if(t == "rune") return MAPINFO_TYPE_RUNEMATCH;
462 else if(t == "lms") return MAPINFO_TYPE_LMS;
463 else if(t == "arena") return MAPINFO_TYPE_ARENA;
464 else if(t == "kh") return MAPINFO_TYPE_KEYHUNT;
465 else if(t == "as") return MAPINFO_TYPE_ASSAULT;
466 else if(t == "ons") return MAPINFO_TYPE_ONSLAUGHT;
467 else if(t == "race") return MAPINFO_TYPE_RACE;
468 else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
469 else if(t == "all") return MAPINFO_TYPE_ALL;
473 // load info about a map by name into the MapInfo_Map_* globals
474 float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametypeToSet)
481 if(strstrofs(pFilename, "/", 0) >= 0)
483 print("Invalid character in map name, ignored\n");
487 if(pGametypeToSet == 0)
488 if(MapInfo_Cache_Retrieve(pFilename))
493 MapInfo_Map_bspname = pFilename;
495 // default all generic fields so they have "good" values in case something fails
496 fn = strcat("maps/", pFilename, ".mapinfo");
497 fh = fopen(fn, FILE_READ);
502 _MapInfo_Map_Reset();
503 r = _MapInfo_Generate(pFilename);
506 fh = fopen(fn, FILE_WRITE);
507 fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
508 fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
509 fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
510 if(_MapInfo_Map_worldspawn_music != "")
513 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
515 substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
517 fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
519 fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
523 n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
524 s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
527 i = floor(random() * n);
528 if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
531 fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
533 if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
534 fputs(fh, "has weapons\n");
536 fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
537 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DEATHMATCH) fputs(fh, "type dm 30 20\n");
538 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_TEAM_DEATHMATCH) fputs(fh, "type tdm 50 20 2\n");
539 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_DOMINATION) fputs(fh, "type dom 200 20\n");
540 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_CTF) fputs(fh, "type ctf 300 20 10\n");
541 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RUNEMATCH) fputs(fh, "type rune 200 20\n");
542 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_LMS) fputs(fh, "type lms 9 20\n");
543 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ARENA) fputs(fh, "type arena 10 20\n");
544 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_KEYHUNT) fputs(fh, "type kh 1000 20 3\n");
545 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ASSAULT) fputs(fh, "type as 20\n");
546 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_RACE) fputs(fh, "type race 5 20 15\n");
547 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_ONSLAUGHT) fputs(fh, "type ons 20\n");
548 if(MapInfo_Map_supportedGametypes & MAPINFO_TYPE_NEXBALL) fputs(fh, "type nexball 5 20\n");
550 fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
554 fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
557 fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
558 fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
559 fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
560 fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
561 fputs(fh, "// optional: hidden\n");
566 fh = fopen(fn, FILE_READ);
568 error("... but I just wrote it!");
571 _MapInfo_Map_Reset();
574 if not((s = fgets(fh)))
577 // catch different sorts of comments
578 if(s == "") // empty lines
580 if(substring(s, 0, 1) == "#") // UNIX style
582 if(substring(s, 0, 2) == "//") // C++ style
584 if(substring(s, 0, 1) == "_") // q3map style
587 t = car(s); s = cdr(s);
589 MapInfo_Map_title = s;
590 else if(t == "description")
591 MapInfo_Map_description = s;
592 else if(t == "author")
593 MapInfo_Map_author = s;
596 t = car(s); s = cdr(s);
597 if (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
599 dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
601 else if(t == "hidden")
603 MapInfo_Map_flags |= MAPINFO_FLAG_HIDDEN;
605 else if(t == "forbidden")
607 MapInfo_Map_flags |= MAPINFO_FLAG_FORBIDDEN;
611 t = car(s); s = cdr(s);
612 f = MapInfo_Type_FromString(t);
614 _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f);
616 dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
621 t = car(s); s = cdr(s); a = stof(t);
622 t = car(s); s = cdr(s); b = stof(t);
623 t = car(s); s = cdr(s); c = stof(t);
624 t = car(s); s = cdr(s); d = stof(t);
625 t = car(s); s = cdr(s); e = stof(t);
627 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");
630 t = car(s); s = cdr(s); f = stof(t);
632 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");
635 if(a >= d || b >= e || c >= f)
636 print("Map ", pFilename, " contains an incorrect size line, mins have to be < maxs\n");
639 MapInfo_Map_mins_x = a;
640 MapInfo_Map_mins_y = b;
641 MapInfo_Map_mins_z = c;
642 MapInfo_Map_maxs_x = d;
643 MapInfo_Map_maxs_y = e;
644 MapInfo_Map_maxs_z = f;
649 else if(t == "settemp_for_type")
651 t = car(s); s = cdr(s);
652 if((f = MapInfo_Type_FromString(t)))
654 if(f & pGametypeToSet)
656 t = car(s); s = cdr(s);
657 if not(cvar_value_issafe(t))
658 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
659 else if not (cvar_value_issafe(s))
660 print("Map ", pFilename, " contains a potentially harmful setting, ignored\n");
663 dprint("Applying temporary setting ", t, " := ", s, "\n");
670 dprint("Map ", pFilename, " has a setting for unknown game type ", t, ", ignored\n");
673 else if(t == "clientsettemp_for_type")
675 t = car(s); s = cdr(s);
676 if((f = MapInfo_Type_FromString(t)))
678 if(f & pGametypeToSet)
680 t = car(s); s = cdr(s);
681 if not(cvar_value_issafe(t))
682 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
683 else if not (cvar_value_issafe(s))
684 print("Map ", pFilename, " contains a potentially harmful client setting, ignored\n");
687 dprint("Applying temporary client setting ", t, " := ", s, "\n");
688 MapInfo_Map_clientstuff = strcat(
689 MapInfo_Map_clientstuff, "cl_cmd settemp \"", t, "\" \"", s, "\"\n"
696 dprint("Map ", pFilename, " has a client setting for unknown game type ", t, ", ignored\n");
701 if not(cvar_value_issafe(t))
702 print("Map ", pFilename, " contains a potentially harmful fog setting, ignored\n");
706 else if(t == "cdtrack")
710 if not(cvar_value_issafe(t))
711 print("Map ", pFilename, " contains a potentially harmful cdtrack, ignored\n");
713 MapInfo_Map_clientstuff = strcat(
714 MapInfo_Map_clientstuff, "cd loop \"", s, "\"\n"
719 dprint("Map ", pFilename, " provides unknown info item ", t, ", ignored\n");
723 if(!MapInfo_Map_supportedGametypes)
724 _MapInfo_Map_ApplyGametype("30 20", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
727 if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
728 error("Can't select the requested game type. Bailing out.");
729 MapInfo_Cache_Store();
730 if(MapInfo_Map_supportedGametypes != 0)
732 dprint("Map ", pFilename, " supports no game types, ignored\n");
736 float MapInfo_FindName(string s)
738 // if there is exactly one map of prefix s, return it
739 // if not, return the null string
740 // note that DP sorts glob results... so I can use a binary search
744 // invariants: r is behind s, l-1 is equal or before
747 m = floor((l + r) / 2);
748 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(m));
749 cmp = strcasecmp(MapInfo_FindName_match, s);
751 return m; // found and good
753 l = m + 1; // l-1 is before s
757 MapInfo_FindName_match = _MapInfo_GlobItem(MapInfo_FilterList_Lookup(l));
758 MapInfo_FindName_firstResult = l;
759 // r == l, so: l is behind s, l-1 is before
760 // SO: if there is any, l is the one with the right prefix
761 // and l+1 may be one too
762 if(l == MapInfo_count)
764 MapInfo_FindName_match = string_null;
765 MapInfo_FindName_firstResult = -1;
766 return -1; // no MapInfo_FindName_match, behind last item
768 if(!startsWithNocase(MapInfo_FindName_match, s))
770 MapInfo_FindName_match = string_null;
771 MapInfo_FindName_firstResult = -1;
772 return -1; // wrong prefix
774 if(l == MapInfo_count - 1)
775 return l; // last one, nothing can follow => unique
776 if(startsWithNocase(_MapInfo_GlobItem(MapInfo_FilterList_Lookup(l + 1)), s))
778 MapInfo_FindName_match = string_null;
779 return -1; // ambigous MapInfo_FindName_match
784 string MapInfo_FixName(string s)
787 return MapInfo_FindName_match;
790 float MapInfo_CurrentFeatures()
794 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")))
795 req |= MAPINFO_FEATURE_WEAPONS;
799 float MapInfo_CurrentGametype()
801 if(cvar("g_domination"))
802 return MAPINFO_TYPE_DOMINATION;
803 else if(cvar("g_ctf"))
804 return MAPINFO_TYPE_CTF;
805 else if(cvar("g_runematch"))
806 return MAPINFO_TYPE_RUNEMATCH;
807 else if(cvar("g_tdm"))
808 return MAPINFO_TYPE_TEAM_DEATHMATCH;
809 else if(cvar("g_assault"))
810 return MAPINFO_TYPE_ASSAULT;
811 else if(cvar("g_lms"))
812 return MAPINFO_TYPE_LMS;
813 else if(cvar("g_arena"))
814 return MAPINFO_TYPE_ARENA;
815 else if(cvar("g_keyhunt"))
816 return MAPINFO_TYPE_KEYHUNT;
817 else if(cvar("g_onslaught"))
818 return MAPINFO_TYPE_ONSLAUGHT;
819 else if(cvar("g_race"))
820 return MAPINFO_TYPE_RACE;
821 else if(cvar("g_nexball"))
822 return MAPINFO_TYPE_NEXBALL;
824 return MAPINFO_TYPE_DEATHMATCH;
827 float _MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
829 if(!MapInfo_Get_ByName(s, 1, 0))
831 if((MapInfo_Map_supportedGametypes & MapInfo_CurrentGametype()) == 0)
833 if((MapInfo_Map_supportedFeatures & MapInfo_CurrentFeatures()) != MapInfo_CurrentFeatures())
838 float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with the current settings, 1 otherwise
841 r = _MapInfo_CheckMap(s);
842 MapInfo_ClearTemps();
846 string MapInfo_GetGameTypeCvar(float t)
850 case MAPINFO_TYPE_DEATHMATCH: return "g_dm";
851 case MAPINFO_TYPE_TEAM_DEATHMATCH: return "g_tdm";
852 case MAPINFO_TYPE_DOMINATION: return "g_domination";
853 case MAPINFO_TYPE_CTF: return "g_ctf";
854 case MAPINFO_TYPE_RUNEMATCH: return "g_runematch";
855 case MAPINFO_TYPE_LMS: return "g_lms";
856 case MAPINFO_TYPE_ARENA: return "g_arena";
857 case MAPINFO_TYPE_KEYHUNT: return "g_kh";
858 case MAPINFO_TYPE_ASSAULT: return "g_assault";
859 case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
860 case MAPINFO_TYPE_RACE: return "g_race";
865 void MapInfo_SwitchGameType(float t)
867 cvar_set("gamecfg", "0");
868 cvar_set("g_dm", (t == MAPINFO_TYPE_DEATHMATCH) ? "1" : "0");
869 cvar_set("g_tdm", (t == MAPINFO_TYPE_TEAM_DEATHMATCH) ? "1" : "0");
870 cvar_set("g_domination", (t == MAPINFO_TYPE_DOMINATION) ? "1" : "0");
871 cvar_set("g_ctf", (t == MAPINFO_TYPE_CTF) ? "1" : "0");
872 cvar_set("g_runematch", (t == MAPINFO_TYPE_RUNEMATCH) ? "1" : "0");
873 cvar_set("g_lms", (t == MAPINFO_TYPE_LMS) ? "1" : "0");
874 cvar_set("g_arena", (t == MAPINFO_TYPE_ARENA) ? "1" : "0");
875 cvar_set("g_keyhunt", (t == MAPINFO_TYPE_KEYHUNT) ? "1" : "0");
876 cvar_set("g_assault", (t == MAPINFO_TYPE_ASSAULT) ? "1" : "0");
877 cvar_set("g_onslaught", (t == MAPINFO_TYPE_ONSLAUGHT) ? "1" : "0");
878 cvar_set("g_race", (t == MAPINFO_TYPE_RACE) ? "1" : "0");
879 cvar_set("g_nexball", (t == MAPINFO_TYPE_NEXBALL) ? "1" : "0");
882 void MapInfo_LoadMap(string s)
884 MapInfo_Map_supportedGametypes = 0;
885 if(!MapInfo_CheckMap(s))
887 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to DM.\n");
888 MapInfo_SwitchGameType(MAPINFO_TYPE_DEATHMATCH);
890 localcmd(strcat("\nsettemp_restore\nchangelevel ", s, "\n"));
893 string MapInfo_ListAllowedMaps(float pRequiredFlags, float pForbiddenFlags)
898 // to make absolutely sure:
900 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), pRequiredFlags, pForbiddenFlags, 0);
903 for(i = 0; i < MapInfo_count; ++i)
904 out = strcat(out, " ", _MapInfo_GlobItem(MapInfo_FilterList_Lookup(i)));
905 return substring(out, 1, strlen(out) - 1);
908 void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
911 if(!_MapInfo_CheckMap(s)) // with underscore, it keeps temps
913 if(MapInfo_Map_supportedGametypes <= 0)
914 error("Mapinfo system is not functional at all. BAILED OUT.\n");
917 while(!(MapInfo_Map_supportedGametypes & 1))
920 MapInfo_Map_supportedGametypes = floor(MapInfo_Map_supportedGametypes / 2);
922 // t is now a supported mode!
923 print("EMERGENCY: can't play the selected map in the given game mode. Falling back to a supported mode.\n");
924 MapInfo_SwitchGameType(t);
926 cvar_settemp_restore();
927 MapInfo_Get_ByName(s, 1, MapInfo_CurrentGametype());
930 void MapInfo_ClearTemps()
932 MapInfo_Map_bspname = string_null;
933 MapInfo_Map_title = string_null;
934 MapInfo_Map_description = string_null;
935 MapInfo_Map_author = string_null;
936 MapInfo_Map_clientstuff = string_null;
937 MapInfo_Map_supportedGametypes = 0;
938 MapInfo_Map_supportedFeatures = 0;
941 void MapInfo_Shutdown()
943 MapInfo_ClearTemps();
944 MapInfo_Filter_Free();
945 MapInfo_Cache_Destroy();
946 if(_MapInfo_globopen)
948 search_end(_MapInfo_globhandle);
949 _MapInfo_globhandle = -1;
950 _MapInfo_globopen = FALSE;