]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/teamplay.qc
Improved LogTeamchange.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
index 3efab4cf5d37a20827e99dd9129c0886e78b47d2..4cc9ff960ee37a0f074585d916ff9781bee338a7 100644 (file)
@@ -34,17 +34,19 @@ const int TEAM_NOT_ALLOWED = -1;
 .float m_team_score; ///< The score of the team.
 .int m_num_players; ///< Number of players (both humans and bots) in a team.
 .int m_num_bots; ///< Number of bots in a team.
+.int m_num_players_alive; ///< Number of alive players in a team.
+.int m_num_control_points; ///< Number of control points owned by a team.
 .entity m_lowest_human; ///< Human with the lowest score in a team.
 .entity m_lowest_bot; ///< Bot with the lowest score in a team.
 
-entity g_team_entities[4]; ///< Holds global team entities.
+entity g_team_entities[NUM_TEAMS]; ///< Holds global team entities.
 
 STATIC_INIT(g_team_entities)
 {
-       g_team_entities[0] = spawn();
-       g_team_entities[1] = spawn();
-       g_team_entities[2] = spawn();
-       g_team_entities[3] = spawn();
+       for (int i = 0; i < NUM_TEAMS; ++i)
+       {
+               g_team_entities[i] = spawn();
+       }
 }
 
 entity Team_GetTeamFromIndex(int index)
@@ -62,7 +64,7 @@ entity Team_GetTeam(int team_num)
        {
                LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
        }
-       return g_team_entities[Team_TeamToNumber(team_num) - 1];
+       return g_team_entities[Team_TeamToIndex(team_num) - 1];
 }
 
 float Team_GetTeamScore(entity team_)
@@ -75,145 +77,50 @@ void Team_SetTeamScore(entity team_, float score)
        team_.m_team_score = score;
 }
 
-void TeamchangeFrags(entity e)
+int Team_GetNumberOfAlivePlayers(entity team_)
 {
-       PlayerScore_Clear(e);
+       return team_.m_num_players_alive;
 }
 
-void LogTeamchange(float player_id, float team_number, float type)
+void Team_SetNumberOfAlivePlayers(entity team_, int number)
 {
-       if(!autocvar_sv_eventlog)
-               return;
-
-       if(player_id < 1)
-               return;
-
-       GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
+       team_.m_num_players_alive = number;
 }
 
-void default_delayedinit(entity this)
+int Team_GetNumberOfAliveTeams()
 {
-       if(!scores_initialized)
-               ScoreRules_generic();
+       int result = 0;
+       for (int i = 0; i < NUM_TEAMS; ++i)
+       {
+               if (g_team_entities[i].m_num_players_alive > 0)
+               {
+                       ++result;
+               }
+       }
+       return result;
 }
 
-void InitGameplayMode()
+int Team_GetNumberOfControlPoints(entity team_)
 {
-       VoteReset();
-
-       // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
-       get_mi_min_max(1);
-       // assign reflectively to avoid "assignment to world" warning
-       int done = 0; for (int i = 0, n = numentityfields(); i < n; ++i) {
-           string k = entityfieldname(i); vector v = (k == "mins") ? mi_min : (k == "maxs") ? mi_max : '0 0 0';
-           if (v) {
-            putentityfieldstring(i, world, sprintf("%v", v));
-            if (++done == 2) break;
-        }
-       }
-       // currently, NetRadiant's limit is 131072 qu for each side
-       // distance from one corner of a 131072qu cube to the opposite corner is approx. 227023 qu
-       // set the distance according to map size but don't go over the limit to avoid issues with float precision
-       // in case somebody makes extremely large maps
-       max_shot_distance = min(230000, vlen(world.maxs - world.mins));
-
-       MapInfo_LoadMapSettings(mapname);
-       GameRules_teams(false);
-
-       if (!cvar_value_issafe(world.fog))
-       {
-               LOG_INFO("The current map contains a potentially harmful fog setting, ignored");
-               world.fog = string_null;
-       }
-       if(MapInfo_Map_fog != "")
-               if(MapInfo_Map_fog == "none")
-                       world.fog = string_null;
-               else
-                       world.fog = strzone(MapInfo_Map_fog);
-       clientstuff = strzone(MapInfo_Map_clientstuff);
-
-       MapInfo_ClearTemps();
-
-       gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
-
-       cache_mutatormsg = strzone("");
-       cache_lastmutatormsg = strzone("");
-
-       InitializeEntity(NULL, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
+       return team_.m_num_control_points;
 }
 
-string GetClientVersionMessage(entity this)
+void Team_SetNumberOfControlPoints(entity team_, int number)
 {
-       if (CS(this).version_mismatch) {
-               if(CS(this).version < autocvar_gameversion) {
-                       return strcat("This is Xonotic ", autocvar_g_xonoticversion,
-                               "\n^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8");
-               } else {
-                       return strcat("This is Xonotic ", autocvar_g_xonoticversion,
-                               "\n^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8");
-               }
-       } else {
-               return strcat("Welcome to Xonotic ", autocvar_g_xonoticversion);
-       }
+       team_.m_num_control_points = number;
 }
 
-string getwelcomemessage(entity this)
+int Team_GetNumberOfTeamsWithControlPoints()
 {
-       MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
-       string modifications = M_ARGV(0, string);
-
-       if(g_weaponarena)
-       {
-               if(g_weaponarena_random)
-                       modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
-               else
-                       modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
-       }
-       else if(cvar("g_balance_blaster_weaponstartoverride") == 0)
-               modifications = strcat(modifications, ", No start weapons");
-       if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
-               modifications = strcat(modifications, ", Low gravity");
-       if(g_weapon_stay && !g_cts)
-               modifications = strcat(modifications, ", Weapons stay");
-       if(g_jetpack)
-               modifications = strcat(modifications, ", Jet pack");
-       if(autocvar_g_powerups == 0)
-               modifications = strcat(modifications, ", No powerups");
-       if(autocvar_g_powerups > 0)
-               modifications = strcat(modifications, ", Powerups");
-       modifications = substring(modifications, 2, strlen(modifications) - 2);
-
-       string versionmessage = GetClientVersionMessage(this);
-       string s = strcat(versionmessage, "^8\n^8\nmatch type is ^1", gamemode_name, "^8\n");
-
-       if(modifications != "")
-               s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
-
-       if(cache_lastmutatormsg != autocvar_g_mutatormsg)
+       int result = 0;
+       for (int i = 0; i < NUM_TEAMS; ++i)
        {
-               if(cache_lastmutatormsg)
-                       strunzone(cache_lastmutatormsg);
-               if(cache_mutatormsg)
-                       strunzone(cache_mutatormsg);
-               cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
-               cache_mutatormsg = strzone(cache_lastmutatormsg);
-       }
-
-       if (cache_mutatormsg != "") {
-               s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
-       }
-
-       string mutator_msg = "";
-       MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
-       mutator_msg = M_ARGV(0, string);
-
-       s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
-
-       string motd = autocvar_sv_motd;
-       if (motd != "") {
-               s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
+               if (g_team_entities[i].m_num_control_points > 0)
+               {
+                       ++result;
+               }
        }
-       return s;
+       return result;
 }
 
 void setcolor(entity this, int clr)
@@ -226,6 +133,26 @@ void setcolor(entity this, int clr)
 #endif
 }
 
+bool Entity_HasValidTeam(entity this)
+{
+       return Team_IsValidTeam(this.team);
+}
+
+int Entity_GetTeamIndex(entity this)
+{
+       return Team_TeamToIndex(this.team);
+}
+
+entity Entity_GetTeam(entity this)
+{
+       int index = Entity_GetTeamIndex(this);
+       if (!Team_IsValidIndex(index))
+       {
+               return NULL;
+       }
+       return Team_GetTeamFromIndex(index);
+}
+
 void SetPlayerColors(entity player, float _color)
 {
        float pants = _color & 0x0F;
@@ -240,67 +167,92 @@ void SetPlayerColors(entity player, float _color)
        }
 }
 
-void KillPlayerForTeamChange(entity player)
+bool Player_SetTeamIndex(entity player, int index)
 {
-       if (IS_DEAD(player))
-       {
-               return;
-       }
-       if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
-       {
-               return;
-       }
-       Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
-               player.origin, '0 0 0');
-}
-
-bool SetPlayerTeamSimple(entity player, int team_num)
-{
-       if (player.team == team_num)
+       int new_team = Team_IndexToTeam(index);
+       if (player.team == new_team)
        {
                // This is important when players join the game and one of their color
                // matches the team color while other doesn't. For example [BOT]Lion.
-               SetPlayerColors(player, team_num - 1);
+               SetPlayerColors(player, new_team - 1);
                return true;
        }
-       if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, Team_TeamToNumber(
-               player.team), Team_TeamToNumber(team_num)) == true)
+       int old_index = Team_TeamToIndex(player.team);
+       if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
        {
                // Mutator has blocked team change.
                return false;
        }
-       int old_team_num = player.team;
-       SetPlayerColors(player, team_num - 1);
-       MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_team_num, player.team);
+       SetPlayerColors(player, new_team - 1);
+       MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
        return true;
 }
 
 bool SetPlayerTeam(entity player, int destination_team_index,
        int source_team_index, bool no_print)
 {
-       int team_num = Team_NumberToTeam(destination_team_index);
-       if (!SetPlayerTeamSimple(player, team_num))
+       if (!Player_SetTeamIndex(player, destination_team_index))
        {
                return false;
        }
-       LogTeamchange(player.playerid, player.team, 3);  // log manual team join
+       LogTeamchange(player.playerid, player.team, TEAM_CHANGE_MANUAL);
        if (no_print)
        {
                return true;
        }
        bprint(playername(player, false), "^7 has changed from ",
-               Team_NumberToColoredFullName(source_team_index), "^7 to ",
-               Team_NumberToColoredFullName(destination_team_index), "\n");
+               Team_IndexToColoredFullName(source_team_index), "^7 to ",
+               Team_IndexToColoredFullName(destination_team_index), "\n");
+       return true;
+}
+
+bool MoveToTeam(entity client, int team_index, int type)
+{
+       int lockteams_backup = lockteams;  // backup any team lock
+       lockteams = 0;  // disable locked teams
+       PlayerScore_Clear(client);
+       if (!Player_SetTeamIndex(client, team_index))
+       {
+               lockteams = lockteams_backup;  // restore the team lock
+               return false;
+       }
+       KillPlayerForTeamChange(client);
+       lockteams = lockteams_backup;  // restore the team lock
+       LogTeamchange(client.playerid, client.team, type);
        return true;
 }
 
+void KillPlayerForTeamChange(entity player)
+{
+       if (IS_DEAD(player))
+       {
+               return;
+       }
+       if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
+       {
+               return;
+       }
+       Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
+               player.origin, '0 0 0');
+}
+
+void LogTeamchange(float player_id, float team_number, int type)
+{
+       if(!autocvar_sv_eventlog)
+               return;
+
+       if(player_id < 1)
+               return;
+
+       GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
+}
+
 entity TeamBalance_CheckAllowedTeams(entity for_whom)
 {
        entity balance = spawn();
        for (int i = 0; i < NUM_TEAMS; ++i)
        {
-               balance.(m_team_balance_team[i]) = spawn();
-               entity team_ = balance.(m_team_balance_team[i]);
+               entity team_ = balance.m_team_balance_team[i] = spawn();
                team_.m_team_score = g_team_entities[i].m_team_score;
                team_.m_num_players = TEAM_NOT_ALLOWED;
                team_.m_num_bots = 0;
@@ -320,7 +272,7 @@ entity TeamBalance_CheckAllowedTeams(entity for_whom)
                {
                        if (teams_mask & BIT(i))
                        {
-                               balance.(m_team_balance_team[i]).m_num_players = 0;
+                               balance.m_team_balance_team[i].m_num_players = 0;
                        }
                }
        }
@@ -425,7 +377,7 @@ entity TeamBalance_CheckAllowedTeams(entity for_whom)
        // if player has a forced team, ONLY allow that one
        for (int i = 1; i <= NUM_TEAMS; ++i)
        {
-               if (for_whom.team_forced == Team_NumberToTeam(i) &&
+               if (for_whom.team_forced == Team_IndexToTeam(i) &&
                        TeamBalance_IsTeamAllowedInternal(balance, i))
                {
                        TeamBalance_BanTeamsExcept(balance, i);
@@ -509,9 +461,9 @@ void TeamBalance_GetTeamCounts(entity balance, entity ignore)
                        entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
                        if (TeamBalanceTeam_IsAllowed(team_))
                        {
-                               MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, Team_NumberToTeam(i),
-                                       ignore, team_.m_num_players, team_.m_num_bots,
-                                       team_.m_lowest_human, team_.m_lowest_bot);
+                               MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore,
+                                       team_.m_num_players, team_.m_num_bots, team_.m_lowest_human,
+                                       team_.m_lowest_bot);
                                team_.m_num_players = M_ARGV(2, float);
                                team_.m_num_bots = M_ARGV(3, float);
                                team_.m_lowest_human = M_ARGV(4, entity);
@@ -615,7 +567,7 @@ int TeamBalance_GetNumberOfPlayers(entity balance, int index)
                LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
                        index);
        }
-       return balance.(m_team_balance_team[index - 1]).m_num_players;
+       return balance.m_team_balance_team[index - 1].m_num_players;
 }
 
 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
@@ -716,21 +668,21 @@ void TeamBalance_JoinBestTeam(entity this, bool force_best_team)
        // if they're not on a valid team, then let other code put them on the smallest team
        if (!force_best_team)
        {
-               int selected_team_num = -1;
+               int selected_team_index = -1;
                for (int i = 1; i <= NUM_TEAMS; ++i)
                {
-                       if (TeamBalance_IsTeamAllowedInternal(balance, i) && (this.team ==
-                               Team_NumberToTeam(i)))
+                       if (TeamBalance_IsTeamAllowedInternal(balance, i) &&
+                               (Team_TeamToIndex(this.team) == i))
                        {
-                               selected_team_num = this.team;
+                               selected_team_index = i;
                                break;
                        }
                }
                
-               if (Team_IsValidTeam(selected_team_num))
+               if (Team_IsValidIndex(selected_team_index))
                {
-                       SetPlayerTeamSimple(this, selected_team_num);
-                       LogTeamchange(this.playerid, this.team, 99);
+                       Player_SetTeamIndex(this, selected_team_index);
+                       LogTeamchange(this.playerid, this.team, TEAM_CHANGE_AUTO_RELAXED);
                        TeamBalance_Destroy(balance);
                        return;
                }
@@ -742,11 +694,10 @@ void TeamBalance_JoinBestTeam(entity this, bool force_best_team)
                return;
        }
        int best_team_index = TeamBalance_FindBestTeam(balance, this, true);
-       int best_team_num = Team_NumberToTeam(best_team_index);
-       int old_team_index = Team_TeamToNumber(this.team);
-       TeamchangeFrags(this);
-       SetPlayerTeamSimple(this, best_team_num);
-       LogTeamchange(this.playerid, this.team, 2); // log auto join
+       int old_team_index = Team_TeamToIndex(this.team);
+       PlayerScore_Clear(this);
+       Player_SetTeamIndex(this, best_team_index);
+       LogTeamchange(this.playerid, this.team, TEAM_CHANGE_AUTO);
        if ((old_team_index != -1) && !IS_BOT_CLIENT(this))
        {
                TeamBalance_AutoBalanceBots(balance, old_team_index, best_team_index);
@@ -828,14 +779,13 @@ void TeamBalance_AutoBalanceBots(entity balance, int source_team_index,
        {
                return;
        }
-       SetPlayerTeamSimple(destination_team.m_lowest_bot,
-               Team_NumberToTeam(source_team_index));
+       Player_SetTeamIndex(destination_team.m_lowest_bot, source_team_index);
        KillPlayerForTeamChange(destination_team.m_lowest_bot);
 }
 
 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
 {
-       return balance.(m_team_balance_team[index - 1]).m_num_players !=
+       return balance.m_team_balance_team[index - 1].m_num_players !=
                TEAM_NOT_ALLOWED;
 }
 
@@ -845,8 +795,7 @@ void TeamBalance_BanTeamsExcept(entity balance, int index)
        {
                if (i != index)
                {
-                       balance.(m_team_balance_team[i - 1]).m_num_players =
-                               TEAM_NOT_ALLOWED;
+                       balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
                }
        }
 }
@@ -862,7 +811,7 @@ entity TeamBalance_GetTeamFromIndex(entity balance, int index)
 
 entity TeamBalance_GetTeam(entity balance, int team_num)
 {
-       return TeamBalance_GetTeamFromIndex(balance, Team_TeamToNumber(team_num));
+       return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
 }
 
 bool TeamBalanceTeam_IsAllowed(entity team_)
@@ -954,8 +903,8 @@ void SV_ChangeTeam(entity this, float _color)
        source_color = this.clientcolors & 0x0F;
        destination_color = _color & 0x0F;
 
-       source_team_index = Team_TeamToNumber(source_color + 1);
-       destination_team_index = Team_TeamToNumber(destination_color + 1);
+       source_team_index = Team_TeamToIndex(source_color + 1);
+       destination_team_index = Team_TeamToIndex(destination_color + 1);
 
        if (destination_team_index == -1)
        {
@@ -1013,7 +962,7 @@ void SV_ChangeTeam(entity this, float _color)
        if (IS_PLAYER(this) && source_team_index != destination_team_index)
        {
                // reduce frags during a team change
-               TeamchangeFrags(this);
+               PlayerScore_Clear(this);
        }
        if (!SetPlayerTeam(this, destination_team_index, source_team_index,
                !IS_CLIENT(this)))