]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/mapinfo.qc
Move fexists from server/miscfunctions.qc to common/util.qc so that it's available...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapinfo.qc
index 1fd2cf8093422d3cafd2b3760e8db48f16d28391..a6b0788088216c93b53f2a0dfa96aabc5ed5e820 100644 (file)
@@ -231,7 +231,7 @@ float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
        }
        if(fh < 0)
                return 0;
-       print("Analyzing ", fn, " to generate initial mapinfo; please edit that file later\n");
+       print("Analyzing ", fn, " to generate initial mapinfo\n");
 
        inWorldspawn = 2;
        MapInfo_Map_flags = 0;
@@ -347,9 +347,11 @@ float _MapInfo_Generate(string pFilename) // 0: failure, 1: ok ent, 2: ok bsp
                MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_DEATHMATCH;      // DM always works
                MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_RUNEMATCH;       // Rune always works
                MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_LMS;             // LMS always works
+               MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_KEEPAWAY;                // Keepaway always works
 
                if(spawnpoints >= 8  && diameter > 4096) {
                        MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_TEAM_DEATHMATCH;
+                       MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_FREEZETAG;
                        MapInfo_Map_supportedGametypes |= MAPINFO_TYPE_CA;
                }
                if(                     diameter < 4096)
@@ -388,7 +390,7 @@ void _MapInfo_Map_Reset()
        MapInfo_Map_maxs = '0 0 0';
 }
 
-string MapInfo_GetDefault(float t)
+string _MapInfo_GetDefault(float t)
 {
        switch(t)
        {
@@ -406,6 +408,11 @@ string MapInfo_GetDefault(float t)
                case MAPINFO_TYPE_ONSLAUGHT:       return "20 0";
                case MAPINFO_TYPE_NEXBALL:         return "5 20 0";
                case MAPINFO_TYPE_CTS:             return "20 0 0";
+               case MAPINFO_TYPE_FREEZETAG:       return "10 20 0";
+               // NOTE: DO NOT ADD ANY MORE GAME TYPES HERE
+               // THIS IS JUST LEGACY SUPPORT FOR NEXUIZ MAPS
+               // ONLY ADD NEW STUFF TO _MapInfo_GetDefaultEx
+               // THIS FUNCTION WILL EVENTUALLY BE REMOVED
                default:                           return "";
        }
 }
@@ -418,7 +425,7 @@ void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, fl
                return;
 
        if(load_default)
-               _MapInfo_Map_ApplyGametype(MapInfo_GetDefault(pThisType), pWantedType, pThisType, FALSE);
+               _MapInfo_Map_ApplyGametype(_MapInfo_GetDefault(pThisType), pWantedType, pThisType, FALSE);
        
        if(pWantedType == MAPINFO_TYPE_ASSAULT || pWantedType == MAPINFO_TYPE_ONSLAUGHT || pWantedType == MAPINFO_TYPE_RACE || pWantedType == MAPINFO_TYPE_CTS) // these modes don't use fraglimit
        {
@@ -462,6 +469,16 @@ void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, fl
                s = cdr(s);
        }
 
+       /* keepaway wuz here
+       if(pWantedType == MAPINFO_TYPE_KEEPAWAY)
+       {
+               sa = car(s);
+               if(sa != "")
+                       cvar_set("fraglimit", sa);
+               s = cdr(s);
+       }
+       */
+       
        // rc = timelimit timelimit_qualification laps laps_teamplay
        if(pWantedType == MAPINFO_TYPE_RACE)
        {
@@ -508,6 +525,134 @@ void _MapInfo_Map_ApplyGametype(string s, float pWantedType, float pThisType, fl
        }
 }
 
+string _MapInfo_GetDefaultEx(float t)
+{
+       switch(t)
+       {
+               case MAPINFO_TYPE_DEATHMATCH:      return "timelimit=20 pointlimit=30 leadlimit=0";
+               case MAPINFO_TYPE_TEAM_DEATHMATCH: return "timelimit=20 pointlimit=50 teams=2 leadlimit=0";
+               case MAPINFO_TYPE_DOMINATION:      return "timelimit=20 pointlimit=200 teams=2 leadlimit=0";
+               case MAPINFO_TYPE_CTF:             return "timelimit=20 pointlimit=300 caplimit=10 leadlimit=0";
+               case MAPINFO_TYPE_RUNEMATCH:       return "timelimit=20 pointlimit=200 leadlimit=0";
+               case MAPINFO_TYPE_LMS:             return "timelimit=20 lives=9 leadlimit=0";
+               case MAPINFO_TYPE_ARENA:           return "timelimit=20 pointlimit=10 leadlimit=0";
+               case MAPINFO_TYPE_CA:              return "timelimit=20 pointlimit=10 leadlimit=0";
+               case MAPINFO_TYPE_KEYHUNT:         return "timelimit=20 pointlimit=1000 teams=3 leadlimit=0";
+               case MAPINFO_TYPE_ASSAULT:         return "timelimit=20";
+               case MAPINFO_TYPE_RACE:            return "timelimit=20 qualifying_timelimit=5 laplimit=7 teamlaplimit=15 leadlimit=0";
+               case MAPINFO_TYPE_ONSLAUGHT:       return "timelimit=20";
+               case MAPINFO_TYPE_NEXBALL:         return "timelimit=20 pointlimit=5 leadlimit=0";
+               case MAPINFO_TYPE_CTS:             return "timelimit=20 skill=-1";
+               case MAPINFO_TYPE_FREEZETAG:       return "timelimit=20 pointlimit=10 teams=2 leadlimit=0";
+               case MAPINFO_TYPE_KEEPAWAY:        return "timelimit=20 pointlimit=30";
+               default:                           return "";
+       }
+}
+
+void _MapInfo_Map_ApplyGametypeEx(string s, float pWantedType, float pThisType)
+{
+       string sa, k, v;
+       float p;
+       string fraglimit_normal;
+       string fraglimit_caps;
+       string fraglimit_teams;
+
+       MapInfo_Map_supportedGametypes |= pThisType;
+       if(!(pThisType & pWantedType))
+               return;
+
+       // reset all the cvars to their defaults
+
+       cvar_set("timelimit", cvar_defstring("timelimit"));
+       cvar_set("leadlimit", cvar_defstring("leadlimit"));
+       cvar_set("fraglimit", cvar_defstring("fraglimit"));
+       cvar_set("g_tdm_teams", cvar_defstring("g_tdm_teams"));
+       cvar_set("g_keyhunt_teams", cvar_defstring("g_keyhunt_teams"));
+       cvar_set("g_domination_default_teams", cvar_defstring("g_domination_default_teams"));
+       cvar_set("g_race_qualifying_timelimit", cvar_defstring("g_race_qualifying_timelimit"));
+
+       fraglimit_normal = string_null;
+       fraglimit_caps = string_null;
+       fraglimit_teams = string_null;
+
+       s = strcat(_MapInfo_GetDefaultEx(pWantedType), " ", s);
+       while(s != "")
+       {
+               sa = car(s);
+               s = cdr(s);
+
+               if(sa == "")
+                       continue;
+
+               p = strstrofs(sa, "=", 0);
+               if(p < 0)
+               {
+                       k = "timelimit";
+                       v = s;
+               }
+               else
+               {
+                       k = substring(sa, 0, p);
+                       v = substring(sa, p+1, -1);
+               }
+
+               if(k == "timelimit")
+               {
+                       cvar_set("timelimit", v);
+               }
+               else if(k == "leadlimit")
+               {
+                       cvar_set("leadlimit", v);
+               }
+               else if(k == "pointlimit" || k == "fraglimit" || k == "lives" || k == "laplimit")
+               {
+                       fraglimit_normal = v;
+               }
+               else if(k == "caplimit")
+               {
+                       fraglimit_caps = v;
+               }
+               else if(k == "teampointlimit" || k == "teamlaplimit")
+               {
+                       fraglimit_teams = v;
+               }
+               else if(k == "teams")
+               {
+                       cvar_set("g_tdm_teams", v);
+                       cvar_set("g_keyhunt_teams", v);
+                       cvar_set("g_domination_default_teams", v);
+               }
+               else if(k == "qualifying_timelimit")
+               {
+                       cvar_set("g_race_qualifying_timelimit", v);
+               }
+               else if(k == "skill")
+               {
+                       // ignore
+               }
+               else
+               {
+                       print("Invalid gametype key in mapinfo: ", k, "\n");
+               }
+       }
+
+       if(pWantedType == MAPINFO_TYPE_CTF && cvar("g_ctf_win_mode") < 2)
+       {
+               if(fraglimit_caps)
+                       cvar_set("fraglimit", fraglimit_caps);
+       }
+       else if(pWantedType == MAPINFO_TYPE_RACE && cvar("g_race_teams") >= 2)
+       {
+               if(fraglimit_teams)
+                       cvar_set("fraglimit", fraglimit_teams);
+       }
+       else
+       {
+               if(fraglimit_normal)
+                       cvar_set("fraglimit", fraglimit_normal);
+       }
+}
+
 float MapInfo_Type_FromString(string t)
 {
        if     (t == "dm")      return MAPINFO_TYPE_DEATHMATCH;
@@ -524,6 +669,8 @@ float MapInfo_Type_FromString(string t)
        else if(t == "rc")      return MAPINFO_TYPE_RACE;
        else if(t == "nexball") return MAPINFO_TYPE_NEXBALL;
        else if(t == "cts")     return MAPINFO_TYPE_CTS;
+       else if(t == "freezetag")       return MAPINFO_TYPE_FREEZETAG;
+       else if(t == "keepaway") return MAPINFO_TYPE_KEEPAWAY;
        else if(t == "all")     return MAPINFO_TYPE_ALL;
        else                    return 0;
 }
@@ -544,6 +691,8 @@ string MapInfo_Type_ToString(float t)
        else if(t == MAPINFO_TYPE_RACE)            return "rc";
        else if(t == MAPINFO_TYPE_NEXBALL)         return "nexball";
        else if(t == MAPINFO_TYPE_CTS)             return "cts";
+       else if(t == MAPINFO_TYPE_FREEZETAG)       return "freezetag";
+       else if(t == MAPINFO_TYPE_KEEPAWAY)        return "keepaway";
        else if(t == MAPINFO_TYPE_ALL)             return "all";
        else                                       return "";
 }
@@ -654,7 +803,7 @@ float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametype
        string fn;
        string s, t;
        float fh, fh2;
-       float r, f, n, i;
+       float r, f, n, i, p;
        string acl;
 
        acl = MAPINFO_SETTEMP_ACL_USER;
@@ -678,69 +827,72 @@ float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametype
        fh = fopen(fn, FILE_READ);
        if(fh < 0)
        {
-               if(!pAllowGenerate)
-                       return 0;
-               _MapInfo_Map_Reset();
-               r = _MapInfo_Generate(pFilename);
-               if(!r)
-                       return 0;
-               fh = fopen(fn, FILE_WRITE);
-               fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
-               fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
-               fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
-               if(_MapInfo_Map_worldspawn_music != "")
+               fn = strcat("maps/autogenerated/", pFilename, ".mapinfo");
+               fh = fopen(fn, FILE_READ);
+               if(fh < 0)
                {
-                       if(
-                               substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
-                               ||
-                               substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
-                       )
-                               fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
+                       if(!pAllowGenerate)
+                               return 0;
+                       _MapInfo_Map_Reset();
+                       r = _MapInfo_Generate(pFilename);
+                       if(!r)
+                               return 0;
+                       fh = fopen(fn, FILE_WRITE);
+                       fputs(fh, strcat("title ", MapInfo_Map_title, "\n"));
+                       fputs(fh, strcat("description ", MapInfo_Map_description, "\n"));
+                       fputs(fh, strcat("author ", MapInfo_Map_author, "\n"));
+                       if(_MapInfo_Map_worldspawn_music != "")
+                       {
+                               if(
+                                       substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".wav"
+                                       ||
+                                       substring(_MapInfo_Map_worldspawn_music, strlen(_MapInfo_Map_worldspawn_music) - 4, 4) == ".ogg"
+                               )
+                                       fputs(fh, strcat("cdtrack ", substring(_MapInfo_Map_worldspawn_music, 0, strlen(_MapInfo_Map_worldspawn_music) - 4), "\n"));
+                               else
+                                       fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
+                       }
                        else
-                               fputs(fh, strcat("cdtrack ", _MapInfo_Map_worldspawn_music, "\n"));
-               }
-               else if(_MapInfo_Map_worldspawn_music)
-               {
-                       n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
-                       s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
-                       for(;;)
                        {
-                               i = floor(random() * n);
-                               if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
-                                       break;
+                               n = tokenize_console(cvar_string("g_cdtracks_remaplist"));
+                               s = strcat(" ", cvar_string("g_cdtracks_dontusebydefault"), " ");
+                               for(;;)
+                               {
+                                       i = floor(random() * n);
+                                       if(strstrofs(s, strcat(" ", argv(i), " "), 0) < 0)
+                                               break;
+                               }
+                               fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
                        }
-                       fputs(fh, strcat("cdtrack ", ftos(i + 1), "\n"));
-               }
-               if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
-                       fputs(fh, "has weapons\n");
-               else
-                       fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
-               if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
-                       fputs(fh, "frustrating\n");
-
-               for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
-                       if(MapInfo_Map_supportedGametypes & i)
-                               fputs(fh, sprintf("type %s %s\n", i, MapInfo_GetDefault(i)));
-
-               fh2 = fopen(strcat("scripts/", pFilename, ".arena"), FILE_READ);
-               if(fh2 >= 0)
-               {
-                       fclose(fh2);
-                       fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
+                       if(MapInfo_Map_supportedFeatures & MAPINFO_FEATURE_WEAPONS)
+                               fputs(fh, "has weapons\n");
+                       else
+                               fputs(fh, "// uncomment this if you added weapon pickups: has weapons\n");
+                       if(MapInfo_Map_flags & MAPINFO_FLAG_FRUSTRATING)
+                               fputs(fh, "frustrating\n");
+
+                       for(i = 1; i <= MapInfo_Map_supportedGametypes; i *= 2)
+                               if(MapInfo_Map_supportedGametypes & i)
+                                       fputs(fh, sprintf("gametype %s // defaults: %s\n", MapInfo_Type_ToString(i), _MapInfo_GetDefaultEx(i)));
+
+                       if(fexists(strcat("scripts/", pFilename, ".arena")))
+                               fputs(fh, "settemp_for_type all sv_q3acompat_machineshotgunswap 1\n");
+
+                       fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
+                       fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
+                       fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
+                       fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
+                       fputs(fh, "// optional: hidden\n");
+
+                       fclose(fh);
+                       r = 2;
+                       // return r;
+                       fh = fopen(fn, FILE_READ);
+                       if(fh < 0)
+                               error("... but I just wrote it!");
                }
 
-               fputs(fh, "// optional: fog density red green blue alpha mindist maxdist\n");
-               fputs(fh, "// optional: settemp_for_type (all|gametypename) cvarname value\n");
-               fputs(fh, "// optional: clientsettemp_for_type (all|gametypename) cvarname value\n");
-               fputs(fh, "// optional: size mins_x mins_y mins_z maxs_x maxs_y maxs_z\n");
-               fputs(fh, "// optional: hidden\n");
-
-               fclose(fh);
-               r = 2;
-               // return r;
-               fh = fopen(fn, FILE_READ);
-               if(fh < 0)
-                       error("... but I just wrote it!");
+               print("WARNING: autogenerated mapinfo file ", fn, " has been loaded; please edit that file and move it to maps/", pFilename, ".mapinfo\n");
        }
 
        _MapInfo_Map_Reset();
@@ -759,6 +911,10 @@ float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametype
                if(substring(s, 0, 1) == "_")  // q3map style
                        continue;
 
+               p = strstrofs(s, "//", 0);
+               if(p >= 0)
+                       s = substring(s, 0, p);
+
                t = car(s); s = cdr(s);
                if(t == "title")
                        MapInfo_Map_title = s;
@@ -770,6 +926,7 @@ float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametype
                {
                        t = car(s); s = cdr(s);
                        if     (t == "weapons") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
+                       else if(t == "new_toys") MapInfo_Map_supportedFeatures |= MAPINFO_FEATURE_WEAPONS;
                        else
                                dprint("Map ", pFilename, " supports unknown feature ", t, ", ignored\n");
                }
@@ -789,11 +946,21 @@ float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametype
                {
                        t = car(s); s = cdr(s);
                        f = MapInfo_Type_FromString(t);
+                       print("Map ", pFilename, " contains the legacy 'type' keyword which is deprecated and will be removed in the future. Please migrate the mapinfo file to 'gametype'.\n");
                        if(f)
                                _MapInfo_Map_ApplyGametype (s, pGametypeToSet, f, TRUE);
                        else
                                dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
                }
+               else if(t == "gametype")
+               {
+                       t = car(s); s = cdr(s);
+                       f = MapInfo_Type_FromString(t);
+                       if(f)
+                               _MapInfo_Map_ApplyGametypeEx (s, pGametypeToSet, f);
+                       else
+                               dprint("Map ", pFilename, " supports unknown game type ", t, ", ignored\n");
+               }
                else if(t == "size")
                {
                        float a, b, c, d, e;
@@ -883,8 +1050,9 @@ float MapInfo_Get_ByName(string pFilename, float pAllowGenerate, float pGametype
        {
                if(!(MapInfo_Map_supportedGametypes & pGametypeToSet))
                {
-                       print("Can't select the requested game type. Trying anyway with stupid settings.\n");
-                       _MapInfo_Map_ApplyGametype(MapInfo_GetDefault(MAPINFO_TYPE_DEATHMATCH), pGametypeToSet, MAPINFO_TYPE_DEATHMATCH, FALSE);
+                       error("Can't select the requested game type. This should never happen as the caller should prevent it!\n");
+                       //_MapInfo_Map_ApplyGametypeEx("", pGametypeToSet, MAPINFO_TYPE_DEATHMATCH);
+                       //return;
                }
        }
 
@@ -986,6 +1154,10 @@ float MapInfo_CurrentGametype()
                return MAPINFO_TYPE_NEXBALL;
        else if(cvar("g_cts"))
                return MAPINFO_TYPE_CTS;
+       else if(cvar("g_freezetag"))
+               return MAPINFO_TYPE_FREEZETAG;
+       else if(cvar("g_keepaway"))
+               return MAPINFO_TYPE_KEEPAWAY;
        else
                return MAPINFO_TYPE_DEATHMATCH;
 }
@@ -1026,7 +1198,9 @@ string MapInfo_GetGameTypeCvar(float t)
                case MAPINFO_TYPE_ONSLAUGHT: return "g_onslaught";
                case MAPINFO_TYPE_RACE: return "g_race";
                case MAPINFO_TYPE_NEXBALL: return "g_nexball";
+               case MAPINFO_TYPE_FREEZETAG: return "g_freezetag";
                case MAPINFO_TYPE_CTS: return "g_cts";
+               case MAPINFO_TYPE_KEEPAWAY:             return "g_keepaway";
                default: return "";
        }
 }
@@ -1048,6 +1222,8 @@ void MapInfo_SwitchGameType(float t)
        cvar_set("g_race",       (t == MAPINFO_TYPE_RACE)            ? "1" : "0");
        cvar_set("g_nexball",    (t == MAPINFO_TYPE_NEXBALL)         ? "1" : "0");
        cvar_set("g_cts",        (t == MAPINFO_TYPE_CTS)             ? "1" : "0");
+       cvar_set("g_freezetag",  (t == MAPINFO_TYPE_FREEZETAG)       ? "1" : "0");
+       cvar_set("g_keepaway",   (t == MAPINFO_TYPE_KEEPAWAY)        ? "1" : "0");
 }
 
 void MapInfo_LoadMap(string s)
@@ -1086,6 +1262,8 @@ void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
                {
                        print("Mapinfo system is not functional at all. Assuming deathmatch.\n");
                        MapInfo_Map_supportedGametypes = MAPINFO_TYPE_DEATHMATCH;
+                       _MapInfo_Map_ApplyGametypeEx("", t0, t0);
+                       return; // do not call Get_ByName!
                }
 
                t = 1;
@@ -1099,12 +1277,9 @@ void MapInfo_LoadMapSettings(string s) // to be called from worldspawn
                if(cvar("g_mapinfo_allow_unsupported_modes_and_let_stuff_break"))
                {
                        print("EMERGENCY: can't play the selected map in the given game mode. Working with only the override settings.\n");
-                       cvar_set("timelimit", "0");
-                       cvar_set("fraglimit", "0");
-                       cvar_set("g_tdm_teams", "2");
-                       cvar_set("g_keyhunt_teams", "3");
-                       cvar_set("g_race_qualifying_timelimit", "0");
-                       cvar_set("leadlimit", "0");
+                       cvar_settemp_restore();
+                       _MapInfo_Map_ApplyGametypeEx("", t0, t0);
+                       return; // do not call Get_ByName!
                }
                else
                {