#include "../common/gamemodes/_mod.qh"
#include "../common/teams.qh"
-void TeamchangeFrags(entity e)
+/// \brief Describes a state of team balance entity.
+enum
{
- PlayerScore_Clear(e);
+ TEAM_BALANCE_UNINITIALIZED, ///< The team balance has not been initialized.
+ /// \brief TeamBalance_CheckAllowedTeams has been called.
+ TEAM_BALANCE_TEAMS_CHECKED,
+ /// \brief TeamBalance_GetTeamCounts has been called.
+ TEAM_BALANCE_TEAM_COUNTS_FILLED
+};
+
+/// \brief Indicates that the player is not allowed to join a team.
+const int TEAM_NOT_ALLOWED = -1;
+
+.int m_team_balance_state; ///< Holds the state of the team balance entity.
+.entity m_team_balance_team[NUM_TEAMS]; ///< ???
+
+.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[NUM_TEAMS]; ///< Holds global team entities.
+
+STATIC_INIT(g_team_entities)
+{
+ for (int i = 0; i < NUM_TEAMS; ++i)
+ {
+ g_team_entities[i] = spawn();
+ }
}
-void LogTeamchange(float player_id, float team_number, float type)
+entity Team_GetTeamFromIndex(int index)
{
- if(!autocvar_sv_eventlog)
- return;
-
- if(player_id < 1)
- return;
-
- GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
+ if (!Team_IsValidIndex(index))
+ {
+ LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
+ }
+ return g_team_entities[index - 1];
}
-void default_delayedinit(entity this)
+entity Team_GetTeam(int team_num)
{
- if(!scores_initialized)
- ScoreRules_generic();
+ if (!Team_IsValidTeam(team_num))
+ {
+ LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
+ }
+ return g_team_entities[Team_TeamToIndex(team_num) - 1];
}
-void ActivateTeamplay()
+float Team_GetTeamScore(entity team_)
{
- serverflags |= SERVERFLAG_TEAMPLAY;
- teamplay = 1;
- cvar_set("teamplay", "2"); // DP needs this for sending proper getstatus replies.
+ return team_.m_team_score;
}
-void InitGameplayMode()
+void Team_SetTeamScore(entity team_, float score)
{
- 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);
- world.mins = mi_min;
- world.maxs = mi_max;
- // 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);
- serverflags &= ~SERVERFLAG_TEAMPLAY;
- teamplay = 0;
- cvar_set("teamplay", "0"); // DP needs this for sending proper getstatus replies.
-
- if (!cvar_value_issafe(world.fog))
- {
- LOG_INFO("The current map contains a potentially harmful fog setting, ignored\n");
- 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);
+ team_.m_team_score = score;
}
-string GetClientVersionMessage(entity this)
+int Team_GetNumberOfAlivePlayers(entity team_)
{
- 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);
- }
+ return team_.m_num_players_alive;
}
-string getwelcomemessage(entity this)
+void Team_SetNumberOfAlivePlayers(entity team_, int number)
{
- MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
- string modifications = M_ARGV(0, string);
+ team_.m_num_players_alive = number;
+}
- if(g_weaponarena)
+int Team_GetNumberOfAliveTeams()
+{
+ int result = 0;
+ for (int i = 0; i < NUM_TEAMS; ++i)
{
- if(g_weaponarena_random)
- modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
- else
- modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
+ if (g_team_entities[i].m_num_players_alive > 0)
+ {
+ ++result;
+ }
}
- 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);
+ return result;
+}
- string versionmessage = GetClientVersionMessage(this);
- string s = strcat(versionmessage, "^8\n^8\nmatch type is ^1", gamemode_name, "^8\n");
+int Team_GetNumberOfControlPoints(entity team_)
+{
+ return team_.m_num_control_points;
+}
- if(modifications != "")
- s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
+void Team_SetNumberOfControlPoints(entity team_, int number)
+{
+ team_.m_num_control_points = number;
+}
- if(cache_lastmutatormsg != autocvar_g_mutatormsg)
+int Team_GetNumberOfTeamsWithControlPoints()
+{
+ 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)
#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;
}
}
-bool SetPlayerTeamSimple(entity player, int teamnum)
+bool Player_SetTeamIndex(entity player, int index)
{
- if (player.team == teamnum)
+ 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, teamnum - 1);
+ SetPlayerColors(player, new_team - 1);
return true;
}
- if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, Team_TeamToNumber(
- player.team), Team_TeamToNumber(teamnum)) == 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 oldteam = player.team;
- SetPlayerColors(player, teamnum - 1);
- MUTATOR_CALLHOOK(Player_ChangedTeam, player, oldteam, player.team);
+ SetPlayerColors(player, new_team - 1);
+ MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
return true;
}
-void SetPlayerTeam(entity player, int destinationteam, int sourceteam, bool noprint)
+bool SetPlayerTeam(entity player, int destination_team_index,
+ int source_team_index, bool no_print)
{
- int teamnum = Team_NumberToTeam(destinationteam);
- if (!SetPlayerTeamSimple(player, teamnum))
+ if (!Player_SetTeamIndex(player, destination_team_index))
+ {
+ return false;
+ }
+ LogTeamchange(player.playerid, player.team, TEAM_CHANGE_MANUAL);
+ if (no_print)
+ {
+ return true;
+ }
+ bprint(playername(player, false), "^7 has changed from ",
+ 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;
}
- LogTeamchange(player.playerid, player.team, 3); // log manual team join
- if (noprint)
+ if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
{
return;
}
- bprint(playername(player, false), "^7 has changed from ", Team_NumberToColoredFullName(sourceteam), "^7 to ", Team_NumberToColoredFullName(destinationteam), "\n");
+ Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
+ player.origin, '0 0 0');
}
-// set c1...c4 to show what teams are allowed
-void CheckAllowedTeams (entity for_whom)
+void LogTeamchange(float player_id, float team_number, int type)
{
- int teams_mask = 0;
+ if(!autocvar_sv_eventlog)
+ return;
- c1 = c2 = c3 = c4 = -1;
- numbotsteam1 = numbotsteam2 = numbotsteam3 = numbotsteam4 = 0;
+ if(player_id < 1)
+ return;
- string teament_name = string_null;
+ GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
+}
- bool mutator_returnvalue = MUTATOR_CALLHOOK(CheckAllowedTeams, teams_mask, teament_name, for_whom);
+entity TeamBalance_CheckAllowedTeams(entity for_whom)
+{
+ entity balance = spawn();
+ for (int i = 0; i < NUM_TEAMS; ++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;
+ team_.m_lowest_human = NULL;
+ team_.m_lowest_bot = NULL;
+ }
+
+ int teams_mask = 0;
+ string teament_name = string_null;
+ bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
+ teams_mask, teament_name, for_whom);
teams_mask = M_ARGV(0, float);
teament_name = M_ARGV(1, string);
-
- if(!mutator_returnvalue)
+ if (mutator_returnvalue)
{
- if(teams_mask & BIT(0)) c1 = 0;
- if(teams_mask & BIT(1)) c2 = 0;
- if(teams_mask & BIT(2)) c3 = 0;
- if(teams_mask & BIT(3)) c4 = 0;
+ for (int i = 0; i < NUM_TEAMS; ++i)
+ {
+ if (teams_mask & BIT(i))
+ {
+ balance.m_team_balance_team[i].m_num_players = 0;
+ }
+ }
}
- // find out what teams are allowed if necessary
- if(teament_name)
+ if (teament_name)
{
entity head = find(NULL, classname, teament_name);
- while(head)
+ while (head)
{
- switch(head.team)
+ if (Team_IsValidTeam(head.team))
{
- case NUM_TEAM_1: c1 = 0; break;
- case NUM_TEAM_2: c2 = 0; break;
- case NUM_TEAM_3: c3 = 0; break;
- case NUM_TEAM_4: c4 = 0; break;
+ TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
}
-
head = find(head, classname, teament_name);
}
}
// TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
- if(AvailableTeams() == 2)
- if(autocvar_bot_vs_human && for_whom)
+ if (AvailableTeams() == 2)
+ if (autocvar_bot_vs_human && for_whom)
{
- if(autocvar_bot_vs_human > 0)
+ if (autocvar_bot_vs_human > 0)
{
// find last team available
-
- if(IS_BOT_CLIENT(for_whom))
+ if (IS_BOT_CLIENT(for_whom))
{
- if(c4 >= 0) { c3 = c2 = c1 = -1; }
- else if(c3 >= 0) { c4 = c2 = c1 = -1; }
- else { c4 = c3 = c1 = -1; }
+ if (TeamBalance_IsTeamAllowedInternal(balance, 4))
+ {
+ TeamBalance_BanTeamsExcept(balance, 4);
+ }
+ else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
+ {
+ TeamBalance_BanTeamsExcept(balance, 3);
+ }
+ else
+ {
+ TeamBalance_BanTeamsExcept(balance, 2);
+ }
// no further cases, we know at least 2 teams exist
}
else
{
- if(c1 >= 0) { c2 = c3 = c4 = -1; }
- else if(c2 >= 0) { c1 = c3 = c4 = -1; }
- else { c1 = c2 = c4 = -1; }
+ if (TeamBalance_IsTeamAllowedInternal(balance, 1))
+ {
+ TeamBalance_BanTeamsExcept(balance, 1);
+ }
+ else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
+ {
+ TeamBalance_BanTeamsExcept(balance, 2);
+ }
+ else
+ {
+ TeamBalance_BanTeamsExcept(balance, 3);
+ }
// no further cases, bots have one of the teams
}
}
else
{
// find first team available
-
- if(IS_BOT_CLIENT(for_whom))
+ if (IS_BOT_CLIENT(for_whom))
{
- if(c1 >= 0) { c2 = c3 = c4 = -1; }
- else if(c2 >= 0) { c1 = c3 = c4 = -1; }
- else { c1 = c2 = c4 = -1; }
+ if (TeamBalance_IsTeamAllowedInternal(balance, 1))
+ {
+ TeamBalance_BanTeamsExcept(balance, 1);
+ }
+ else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
+ {
+ TeamBalance_BanTeamsExcept(balance, 2);
+ }
+ else
+ {
+ TeamBalance_BanTeamsExcept(balance, 3);
+ }
// no further cases, we know at least 2 teams exist
}
else
{
- if(c4 >= 0) { c3 = c2 = c1 = -1; }
- else if(c3 >= 0) { c4 = c2 = c1 = -1; }
- else { c4 = c3 = c1 = -1; }
+ if (TeamBalance_IsTeamAllowedInternal(balance, 4))
+ {
+ TeamBalance_BanTeamsExcept(balance, 4);
+ }
+ else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
+ {
+ TeamBalance_BanTeamsExcept(balance, 3);
+ }
+ else
+ {
+ TeamBalance_BanTeamsExcept(balance, 2);
+ }
// no further cases, bots have one of the teams
}
}
}
- if(!for_whom)
- return;
+ if (!for_whom)
+ {
+ balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
+ return balance;
+ }
// if player has a forced team, ONLY allow that one
- if(for_whom.team_forced == NUM_TEAM_1 && c1 >= 0)
- c2 = c3 = c4 = -1;
- else if(for_whom.team_forced == NUM_TEAM_2 && c2 >= 0)
- c1 = c3 = c4 = -1;
- else if(for_whom.team_forced == NUM_TEAM_3 && c3 >= 0)
- c1 = c2 = c4 = -1;
- else if(for_whom.team_forced == NUM_TEAM_4 && c4 >= 0)
- c1 = c2 = c3 = -1;
+ for (int i = 1; i <= NUM_TEAMS; ++i)
+ {
+ if (for_whom.team_forced == Team_IndexToTeam(i) &&
+ TeamBalance_IsTeamAllowedInternal(balance, i))
+ {
+ TeamBalance_BanTeamsExcept(balance, i);
+ }
+ break;
+ }
+ balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
+ return balance;
}
-float PlayerValue(entity p)
+void TeamBalance_Destroy(entity balance)
{
- return 1;
- // FIXME: it always returns 1...
+ if (balance == NULL)
+ {
+ return;
+ }
+ for (int i = 0; i < NUM_TEAMS; ++i)
+ {
+ remove(balance.(m_team_balance_team[i]));
+ }
+ remove(balance);
}
-// c1...c4 should be set to -1 (not allowed) or 0 (allowed).
-// teams that are allowed will now have their player counts stored in c1...c4
-void GetTeamCounts(entity ignore)
+int TeamBalance_GetAllowedTeams(entity balance)
{
- if (MUTATOR_CALLHOOK(GetTeamCounts) == true)
+ if (balance == NULL)
{
- if (c1 >= 0)
- {
- MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_1, ignore, c1, numbotsteam1,
- lowestplayerteam1, lowestbotteam1);
- c1 = M_ARGV(2, float);
- numbotsteam1 = M_ARGV(3, float);
- lowestplayerteam1 = M_ARGV(4, entity);
- lowestbotteam1 = M_ARGV(5, entity);
- }
- if (c2 >= 0)
- {
- MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_2, ignore, c2, numbotsteam2,
- lowestplayerteam2, lowestbotteam2);
- c2 = M_ARGV(2, float);
- numbotsteam2 = M_ARGV(3, float);
- lowestplayerteam2 = M_ARGV(4, entity);
- lowestbotteam2 = M_ARGV(5, entity);
- }
- if (c3 >= 0)
+ LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
+ }
+ if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
+ {
+ LOG_FATAL("TeamBalance_GetAllowedTeams: "
+ "Team balance entity is not initialized.");
+ }
+ int result = 0;
+ for (int i = 1; i <= NUM_TEAMS; ++i)
+ {
+ if (TeamBalance_IsTeamAllowedInternal(balance, i))
{
- MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_3, ignore, c3, numbotsteam3,
- lowestplayerteam3, lowestbotteam3);
- c3 = M_ARGV(2, float);
- numbotsteam3 = M_ARGV(3, float);
- lowestplayerteam3 = M_ARGV(4, entity);
- lowestbotteam3 = M_ARGV(5, entity);
+ result |= Team_IndexToBit(i);
}
- if (c4 >= 0)
+ }
+ return result;
+}
+
+bool TeamBalance_IsTeamAllowed(entity balance, int index)
+{
+ if (balance == NULL)
+ {
+ LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
+ }
+ if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
+ {
+ LOG_FATAL("TeamBalance_IsTeamAllowed: "
+ "Team balance entity is not initialized.");
+ }
+ if (!Team_IsValidIndex(index))
+ {
+ LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
+ index);
+ }
+ return TeamBalance_IsTeamAllowedInternal(balance, index);
+}
+
+void TeamBalance_GetTeamCounts(entity balance, entity ignore)
+{
+ if (balance == NULL)
+ {
+ LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
+ }
+ if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
+ {
+ LOG_FATAL("TeamBalance_GetTeamCounts: "
+ "Team balance entity is not initialized.");
+ }
+ if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
+ {
+ // Mutator has overriden the configuration.
+ for (int i = 1; i <= NUM_TEAMS; ++i)
{
- MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_4, ignore, c4, numbotsteam4,
- lowestplayerteam4, lowestbotteam4);
- c4 = M_ARGV(2, float);
- numbotsteam4 = M_ARGV(3, float);
- lowestplayerteam4 = M_ARGV(4, entity);
- lowestbotteam4 = M_ARGV(5, entity);
+ entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
+ if (TeamBalanceTeam_IsAllowed(team_))
+ {
+ 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);
+ team_.m_lowest_bot = M_ARGV(5, entity);
+ }
}
}
else
{
- float value, bvalue;
- // now count how many players are on each team already
- float lowestplayerscore1 = FLOAT_MAX;
- float lowestbotscore1 = FLOAT_MAX;
- float lowestplayerscore2 = FLOAT_MAX;
- float lowestbotscore2 = FLOAT_MAX;
- float lowestplayerscore3 = FLOAT_MAX;
- float lowestbotscore3 = FLOAT_MAX;
- float lowestplayerscore4 = FLOAT_MAX;
- float lowestbotscore4 = FLOAT_MAX;
- FOREACH_CLIENT(true, LAMBDA(
- float t;
+ // Manually count all players.
+ FOREACH_CLIENT(true,
+ {
+ if (it == ignore)
+ {
+ continue;
+ }
+ int team_num;
if (IS_PLAYER(it) || it.caplayer)
{
- t = it.team;
+ team_num = it.team;
}
else if (it.team_forced > 0)
{
- t = it.team_forced; // reserve the spot
+ team_num = it.team_forced; // reserve the spot
}
else
{
continue;
}
- if (it == ignore)
+ if (!Team_IsValidTeam(team_num))
{
continue;
}
- value = PlayerValue(it);
- if (IS_BOT_CLIENT(it))
- {
- bvalue = value;
- }
- else
+ entity team_ = TeamBalance_GetTeam(balance, team_num);
+ if (!TeamBalanceTeam_IsAllowed(team_))
{
- bvalue = 0;
+ continue;
}
- if (value == 0)
+ ++team_.m_num_players;
+ if (IS_BOT_CLIENT(it))
{
- continue;
+ ++team_.m_num_bots;
}
- switch (t)
+ float temp_score = PlayerScore_Get(it, SP_SCORE);
+ if (!IS_BOT_CLIENT(it))
{
- case NUM_TEAM_1:
- {
- if (c1 < 0)
- {
- break;
- }
- c1 += value;
- numbotsteam1 += bvalue;
- float tempscore = PlayerScore_Get(it, SP_SCORE);
- if (!bvalue)
- {
- if (tempscore < lowestplayerscore1)
- {
- lowestplayerteam1 = it;
- lowestplayerscore1 = tempscore;
- }
- break;
- }
- if (tempscore < lowestbotscore1)
- {
- lowestbotteam1 = it;
- lowestbotscore1 = tempscore;
- }
- break;
- }
- case NUM_TEAM_2:
- {
- if (c2 < 0)
- {
- break;
- }
- c2 += value;
- numbotsteam2 += bvalue;
- float tempscore = PlayerScore_Get(it, SP_SCORE);
- if (!bvalue)
- {
- if (tempscore < lowestplayerscore2)
- {
- lowestplayerteam2 = it;
- lowestplayerscore2 = tempscore;
- }
- break;
- }
- if (tempscore < lowestbotscore2)
- {
- lowestbotteam2 = it;
- lowestbotscore2 = tempscore;
- }
- break;
- }
- case NUM_TEAM_3:
+ if (team_.m_lowest_human == NULL)
{
- if (c3 < 0)
- {
- break;
- }
- c3 += value;
- numbotsteam3 += bvalue;
- float tempscore = PlayerScore_Get(it, SP_SCORE);
- if (!bvalue)
- {
- if (tempscore < lowestplayerscore3)
- {
- lowestplayerteam3 = it;
- lowestplayerscore3 = tempscore;
- }
- break;
- }
- if (tempscore < lowestbotscore3)
- {
- lowestbotteam3 = it;
- lowestbotscore3 = tempscore;
- }
- break;
+ team_.m_lowest_human = it;
+ continue;
}
- case NUM_TEAM_4:
+ if (temp_score < PlayerScore_Get(team_.m_lowest_human,
+ SP_SCORE))
{
- if (c4 < 0)
- {
- break;
- }
- c4 += value;
- numbotsteam4 += bvalue;
- float tempscore = PlayerScore_Get(it, SP_SCORE);
- if (!bvalue)
- {
- if (tempscore < lowestplayerscore4)
- {
- lowestplayerteam4 = it;
- lowestplayerscore4 = tempscore;
- }
- break;
- }
- if (tempscore < lowestbotscore4)
- {
- lowestbotteam4 = it;
- lowestbotscore4 = tempscore;
- }
- break;
+ team_.m_lowest_human = it;
}
+ continue;
+ }
+ if (team_.m_lowest_bot == NULL)
+ {
+ team_.m_lowest_bot = it;
+ continue;
}
- ));
+ if (temp_score < PlayerScore_Get(team_.m_lowest_bot, SP_SCORE))
+ {
+ team_.m_lowest_bot = it;
+ }
+ });
}
// if the player who has a forced team has not joined yet, reserve the spot
- if(autocvar_g_campaign)
+ if (autocvar_g_campaign)
{
- switch(autocvar_g_campaign_forceteam)
+ if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
{
- case 1: if(c1 == numbotsteam1) ++c1; break;
- case 2: if(c2 == numbotsteam2) ++c2; break;
- case 3: if(c3 == numbotsteam3) ++c3; break;
- case 4: if(c4 == numbotsteam4) ++c4; break;
+ entity team_ = TeamBalance_GetTeamFromIndex(balance,
+ autocvar_g_campaign_forceteam);
+ if (team_.m_num_players == team_.m_num_bots)
+ {
+ ++team_.m_num_players;
+ }
}
}
+ balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
}
-bool IsTeamSmallerThanTeam(int teama, int teamb, entity e, bool usescore)
+int TeamBalance_GetNumberOfPlayers(entity balance, int index)
{
- // equal
- if (teama == teamb)
- {
- return false;
- }
- // we assume that CheckAllowedTeams and GetTeamCounts have already been called
- float numplayersteama = -1, numplayersteamb = -1;
- float numbotsteama = 0, numbotsteamb = 0;
- float scoreteama = 0, scoreteamb = 0;
-
- switch (teama)
- {
- case 1: numplayersteama = c1; numbotsteama = numbotsteam1; scoreteama = team1_score; break;
- case 2: numplayersteama = c2; numbotsteama = numbotsteam2; scoreteama = team2_score; break;
- case 3: numplayersteama = c3; numbotsteama = numbotsteam3; scoreteama = team3_score; break;
- case 4: numplayersteama = c4; numbotsteama = numbotsteam4; scoreteama = team4_score; break;
- }
- switch (teamb)
- {
- case 1: numplayersteamb = c1; numbotsteamb = numbotsteam1; scoreteamb = team1_score; break;
- case 2: numplayersteamb = c2; numbotsteamb = numbotsteam2; scoreteamb = team2_score; break;
- case 3: numplayersteamb = c3; numbotsteamb = numbotsteam3; scoreteamb = team3_score; break;
- case 4: numplayersteamb = c4; numbotsteamb = numbotsteam4; scoreteamb = team4_score; break;
- }
-
- // invalid
- if (numplayersteama < 0 || numplayersteamb < 0)
- return false;
-
- if ((IS_REAL_CLIENT(e) && bots_would_leave))
- {
- numplayersteama -= numbotsteama;
- numplayersteamb -= numbotsteamb;
- }
- if (!usescore)
+ if (balance == NULL)
{
- return numplayersteama < numplayersteamb;
+ LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
+ "Team balance entity is NULL.");
}
- if (numplayersteama < numplayersteamb)
+ if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
{
- return true;
+ LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
+ "TeamBalance_GetTeamCounts has not been called.");
}
- if (numplayersteama > numplayersteamb)
+ if (!Team_IsValidIndex(index))
{
- return false;
+ LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
+ index);
}
- return scoreteama < scoreteamb;
+ return balance.m_team_balance_team[index - 1].m_num_players;
}
-bool IsTeamEqualToTeam(int teama, int teamb, entity e, bool usescore)
+int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
{
- // equal
- if (teama == teamb)
+ if (balance == NULL)
{
- return true;
- }
- // we assume that CheckAllowedTeams and GetTeamCounts have already been called
- float numplayersteama = -1, numplayersteamb = -1;
- float numbotsteama = 0, numbotsteamb = 0;
- float scoreteama = 0, scoreteamb = 0;
-
- switch (teama)
- {
- case 1: numplayersteama = c1; numbotsteama = numbotsteam1; scoreteama = team1_score; break;
- case 2: numplayersteama = c2; numbotsteama = numbotsteam2; scoreteama = team2_score; break;
- case 3: numplayersteama = c3; numbotsteama = numbotsteam3; scoreteama = team3_score; break;
- case 4: numplayersteama = c4; numbotsteama = numbotsteam4; scoreteama = team4_score; break;
+ LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
}
- switch (teamb)
+ if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
{
- case 1: numplayersteamb = c1; numbotsteamb = numbotsteam1; scoreteamb = team1_score; break;
- case 2: numplayersteamb = c2; numbotsteamb = numbotsteam2; scoreteamb = team2_score; break;
- case 3: numplayersteamb = c3; numbotsteamb = numbotsteam3; scoreteamb = team3_score; break;
- case 4: numplayersteamb = c4; numbotsteamb = numbotsteam4; scoreteamb = team4_score; break;
+ LOG_FATAL("TeamBalance_FindBestTeam: "
+ "Team balance entity is not initialized.");
}
-
- // invalid
- if (numplayersteama < 0 || numplayersteamb < 0)
- return false;
-
- if ((IS_REAL_CLIENT(e) && bots_would_leave))
+ // count how many players are in each team
+ if (ignore_player)
{
- numplayersteama -= numbotsteama;
- numplayersteamb -= numbotsteamb;
+ TeamBalance_GetTeamCounts(balance, player);
}
- if (!usescore)
+ else
{
- return numplayersteama == numplayersteamb;
+ TeamBalance_GetTeamCounts(balance, NULL);
}
- if (numplayersteama < numplayersteamb)
+ int team_bits = TeamBalance_FindBestTeams(balance, player, true);
+ if (team_bits == 0)
{
- return false;
+ LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
+ MapInfo_Type_ToString(MapInfo_CurrentGametype()));
}
- if (numplayersteama > numplayersteamb)
+ RandomSelection_Init();
+ for (int i = 1; i <= NUM_TEAMS; ++i)
{
- return false;
+ if (team_bits & Team_IndexToBit(i))
+ {
+ RandomSelection_AddFloat(i, 1, 1);
+ }
}
- return scoreteama == scoreteamb;
+ return RandomSelection_chosen_float;
}
-int FindBestTeams(entity player, bool usescore)
+int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
{
- if (MUTATOR_CALLHOOK(FindBestTeams, player) == true)
+ if (balance == NULL)
{
- return M_ARGV(1, float);
+ LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
}
- int teambits = 0;
- int previousteam = 0;
- if (c1 >= 0)
+ if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
{
- teambits = BIT(0);
- previousteam = 1;
+ LOG_FATAL("TeamBalance_FindBestTeams: "
+ "TeamBalance_GetTeamCounts has not been called.");
}
- if (c2 >= 0)
+ if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
{
- if (previousteam == 0)
- {
- teambits = BIT(1);
- previousteam = 2;
- }
- else if (IsTeamSmallerThanTeam(2, previousteam, player, usescore))
- {
- teambits = BIT(1);
- previousteam = 2;
- }
- else if (IsTeamEqualToTeam(2, previousteam, player, usescore))
- {
- teambits |= BIT(1);
- previousteam = 2;
- }
+ return M_ARGV(1, float);
}
- if (c3 >= 0)
+ int team_bits = 0;
+ int previous_team = 0;
+ for (int i = 1; i <= NUM_TEAMS; ++i)
{
- if (previousteam == 0)
- {
- teambits = BIT(2);
- previousteam = 3;
- }
- else if (IsTeamSmallerThanTeam(3, previousteam, player, usescore))
+ if (!TeamBalance_IsTeamAllowedInternal(balance, i))
{
- teambits = BIT(2);
- previousteam = 3;
+ continue;
}
- else if (IsTeamEqualToTeam(3, previousteam, player, usescore))
+ if (previous_team == 0)
{
- teambits |= BIT(2);
- previousteam = 3;
+ team_bits = Team_IndexToBit(i);
+ previous_team = i;
+ continue;
}
- }
- if (c4 >= 0)
- {
- if (previousteam == 0)
- {
- teambits = BIT(3);
- }
- else if (IsTeamSmallerThanTeam(4, previousteam, player, usescore))
+ int compare = TeamBalance_CompareTeams(balance, i, previous_team,
+ player, use_score);
+ if (compare == TEAMS_COMPARE_LESS)
{
- teambits = BIT(3);
+ team_bits = Team_IndexToBit(i);
+ previous_team = i;
+ continue;
}
- else if (IsTeamEqualToTeam(4, previousteam, player, usescore))
+ if (compare == TEAMS_COMPARE_EQUAL)
{
- teambits |= BIT(3);
+ team_bits |= Team_IndexToBit(i);
+ previous_team = i;
}
}
- return teambits;
-}
-
-// returns # of smallest team (1, 2, 3, 4)
-// NOTE: Assumes CheckAllowedTeams has already been called!
-float FindSmallestTeam(entity pl, float ignore_pl)
-{
- // count how many players are in each team
- if (ignore_pl)
- {
- GetTeamCounts(pl);
- }
- else
- {
- GetTeamCounts(NULL);
- }
- int teambits = FindBestTeams(pl, true);
- if (teambits == 0)
- {
- error(sprintf("No teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
- }
- RandomSelection_Init();
- if ((teambits & BIT(0)) != 0)
- {
- RandomSelection_AddFloat(1, 1, 1);
- }
- if ((teambits & BIT(1)) != 0)
- {
- RandomSelection_AddFloat(2, 1, 1);
- }
- if ((teambits & BIT(2)) != 0)
- {
- RandomSelection_AddFloat(3, 1, 1);
- }
- if ((teambits & BIT(3)) != 0)
- {
- RandomSelection_AddFloat(4, 1, 1);
- }
- return RandomSelection_chosen_float;
+ return team_bits;
}
-int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
+void TeamBalance_JoinBestTeam(entity this, bool force_best_team)
{
// don't join a team if we're not playing a team game
if (!teamplay)
{
- return 0;
+ return;
}
// find out what teams are available
- CheckAllowedTeams(this);
-
- float selectedteam;
-
- // if we don't care what team he ends up on, put him on whatever team he entered as.
- // if he's not on a valid team, then let other code put him on the smallest team
- if (!forcebestteam)
- {
- if( c1 >= 0 && this.team == NUM_TEAM_1)
- selectedteam = this.team;
- else if(c2 >= 0 && this.team == NUM_TEAM_2)
- selectedteam = this.team;
- else if(c3 >= 0 && this.team == NUM_TEAM_3)
- selectedteam = this.team;
- else if(c4 >= 0 && this.team == NUM_TEAM_4)
- selectedteam = this.team;
- else
- selectedteam = -1;
+ entity balance = TeamBalance_CheckAllowedTeams(this);
- if (selectedteam > 0)
+ // if we don't care what team they end up on, put them on whatever team they entered as.
+ // 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_index = -1;
+ for (int i = 1; i <= NUM_TEAMS; ++i)
{
- if (!only_return_best)
+ if (TeamBalance_IsTeamAllowedInternal(balance, i) &&
+ (Team_TeamToIndex(this.team) == i))
{
- SetPlayerTeamSimple(this, selectedteam);
-
- // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
- // when JoinBestTeam is called by client.qc/ClientConnect the player_id is 0 the log attempt is rejected
- LogTeamchange(this.playerid, this.team, 99);
+ selected_team_index = i;
+ break;
}
- return selectedteam;
}
- // otherwise end up on the smallest team (handled below)
+
+ if (Team_IsValidIndex(selected_team_index))
+ {
+ Player_SetTeamIndex(this, selected_team_index);
+ LogTeamchange(this.playerid, this.team, TEAM_CHANGE_AUTO_RELAXED);
+ TeamBalance_Destroy(balance);
+ return;
+ }
}
-
- float bestteam = FindSmallestTeam(this, true);
- if (only_return_best || this.bot_forced_team)
+ // otherwise end up on the smallest team (handled below)
+ if (this.bot_forced_team)
{
- return bestteam;
+ TeamBalance_Destroy(balance);
+ return;
}
- bestteam = Team_NumberToTeam(bestteam);
- if (bestteam == -1)
+ int best_team_index = TeamBalance_FindBestTeam(balance, this, true);
+ 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))
{
- error("JoinBestTeam: invalid team\n");
+ TeamBalance_AutoBalanceBots(balance, old_team_index, best_team_index);
}
- int oldteam = Team_TeamToNumber(this.team);
- TeamchangeFrags(this);
- SetPlayerTeamSimple(this, bestteam);
- LogTeamchange(this.playerid, this.team, 2); // log auto join
- if (!IS_BOT_CLIENT(this))
+ KillPlayerForTeamChange(this);
+ TeamBalance_Destroy(balance);
+}
+
+int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
+ entity player, bool use_score)
+{
+ if (balance == NULL)
{
- AutoBalanceBots(oldteam, Team_TeamToNumber(bestteam));
+ LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
}
- if (!IS_DEAD(this) && (MUTATOR_CALLHOOK(Player_ChangeTeamKill, this) ==
- false))
+ if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
{
- Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
+ LOG_FATAL("TeamBalance_CompareTeams: "
+ "TeamBalance_GetTeamCounts has not been called.");
}
- return bestteam;
-}
-
-void SV_ChangeTeam(entity this, float _color)
-{
- float sourcecolor, destinationcolor, sourceteam, destinationteam;
-
- // in normal deathmatch we can just apply the color and we're done
- if(!teamplay)
- SetPlayerColors(this, _color);
-
- if(!IS_CLIENT(this))
+ if (!Team_IsValidIndex(team_index_a))
{
- // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
- Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
- return;
+ LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
+ team_index_a);
}
-
- if(!teamplay)
- return;
-
- sourcecolor = this.clientcolors & 0x0F;
- destinationcolor = _color & 0x0F;
-
- sourceteam = Team_TeamToNumber(sourcecolor + 1);
- destinationteam = Team_TeamToNumber(destinationcolor + 1);
-
- CheckAllowedTeams(this);
-
- if (destinationteam == 1 && c1 < 0) destinationteam = 4;
- if (destinationteam == 4 && c4 < 0) destinationteam = 3;
- if (destinationteam == 3 && c3 < 0) destinationteam = 2;
- if (destinationteam == 2 && c2 < 0) destinationteam = 1;
-
- // not changing teams
- if (sourcecolor == destinationcolor)
+ if (!Team_IsValidIndex(team_index_b))
{
- SetPlayerTeam(this, destinationteam, sourceteam, true);
- return;
+ LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
+ team_index_b);
}
-
- if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
- Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
- return; // changing teams is not allowed
+ if (team_index_a == team_index_b)
+ {
+ return TEAMS_COMPARE_EQUAL;
}
+ entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
+ entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
+ return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
+}
- // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
- if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
+void TeamBalance_AutoBalanceBots(entity balance, int source_team_index,
+ int destination_team_index)
+{
+ if (balance == NULL)
{
- GetTeamCounts(this);
- if ((BIT(destinationteam - 1) & FindBestTeams(this, false)) == 0)
- {
- Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
- return;
- }
+ LOG_FATAL("TeamBalance_AutoBalanceBots: Team balance entity is NULL.");
}
- if(IS_PLAYER(this) && sourceteam != destinationteam)
+ if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
{
- // reduce frags during a team change
- TeamchangeFrags(this);
+ LOG_FATAL("TeamBalance_AutoBalanceBots: "
+ "TeamBalance_GetTeamCounts has not been called.");
}
- SetPlayerTeam(this, destinationteam, sourceteam, !IS_CLIENT(this));
- AutoBalanceBots(sourceteam, destinationteam);
- if (!IS_PLAYER(this) || (sourceteam == destinationteam))
+ if (!Team_IsValidIndex(source_team_index))
{
+ LOG_WARNF("AutoBalanceBots: Source team index is invalid: %f",
+ source_team_index);
return;
}
- // kill player when changing teams
- if (IS_DEAD(this) || (MUTATOR_CALLHOOK(Player_ChangeTeamKill, this) == true))
+ if (!Team_IsValidIndex(destination_team_index))
{
+ LOG_WARNF("AutoBalanceBots: Destination team index is invalid: %f",
+ destination_team_index);
return;
}
- Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
-}
-
-void AutoBalanceBots(int sourceteam, int destinationteam)
-{
- if ((sourceteam == -1) || (destinationteam == -1))
+ if (!autocvar_g_balance_teams ||
+ !autocvar_g_balance_teams_prevent_imbalance)
{
return;
}
- if (!autocvar_g_balance_teams ||
- !autocvar_g_balance_teams_prevent_imbalance)
+ entity source_team = TeamBalance_GetTeamFromIndex(balance,
+ source_team_index);
+ if (!TeamBalanceTeam_IsAllowed(source_team))
{
return;
}
- int numplayerssourceteam = 0;
- int numplayersdestinationteam = 0;
- entity lowestbotdestinationteam = NULL;
- switch (sourceteam)
+ entity destination_team = TeamBalance_GetTeamFromIndex(balance,
+ destination_team_index);
+ if ((destination_team.m_num_players <= source_team.m_num_players) ||
+ (destination_team.m_lowest_bot == NULL))
{
- case 1:
- {
- numplayerssourceteam = c1;
- break;
- }
- case 2:
- {
- numplayerssourceteam = c2;
- break;
- }
- case 3:
- {
- numplayerssourceteam = c3;
- break;
- }
- case 4:
- {
- numplayerssourceteam = c4;
- break;
- }
+ return;
}
- switch (destinationteam)
+ 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 !=
+ TEAM_NOT_ALLOWED;
+}
+
+void TeamBalance_BanTeamsExcept(entity balance, int index)
+{
+ for (int i = 1; i <= NUM_TEAMS; ++i)
{
- case 1:
- {
- numplayersdestinationteam = c1;
- lowestbotdestinationteam = lowestbotteam1;
- break;
- }
- case 2:
- {
- numplayersdestinationteam = c2;
- lowestbotdestinationteam = lowestbotteam2;
- break;
- }
- case 3:
- {
- numplayersdestinationteam = c3;
- lowestbotdestinationteam = lowestbotteam3;
- break;
- }
- case 4:
+ if (i != index)
{
- numplayersdestinationteam = c4;
- lowestbotdestinationteam = lowestbotteam4;
- break;
+ balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
}
}
- if ((numplayersdestinationteam <= numplayerssourceteam) ||
- (lowestbotdestinationteam == NULL))
- {
- return;
- }
- SetPlayerTeamSimple(lowestbotdestinationteam, Team_NumberToTeam(sourceteam));
- if (IS_DEAD(lowestbotdestinationteam) || (MUTATOR_CALLHOOK(
- Player_ChangeTeamKill, lowestbotdestinationteam) == true))
+}
+
+entity TeamBalance_GetTeamFromIndex(entity balance, int index)
+{
+ if (!Team_IsValidIndex(index))
{
- return;
+ LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
}
- Damage(lowestbotdestinationteam, lowestbotdestinationteam,
- lowestbotdestinationteam, 100000, DEATH_TEAMCHANGE.m_id,
- lowestbotdestinationteam.origin, '0 0 0');
+ return balance.m_team_balance_team[index - 1];
+}
+
+entity TeamBalance_GetTeam(entity balance, int team_num)
+{
+ return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
+}
+
+bool TeamBalanceTeam_IsAllowed(entity team_)
+{
+ return team_.m_num_players != TEAM_NOT_ALLOWED;
+}
+
+int TeamBalanceTeam_GetNumberOfPlayers(entity team_)
+{
+ return team_.m_num_players;
+}
+
+int TeamBalanceTeam_GetNumberOfBots(entity team_)
+{
+ return team_.m_num_bots;
}
-void ShufflePlayerOutOfTeam (float source_team)
+entity TeamBalanceTeam_GetLowestHuman(entity team_)
{
- float smallestteam, smallestteam_count, steam;
- float lowest_bot_score, lowest_player_score;
- entity lowest_bot, lowest_player, selected;
+ return team_.m_lowest_human;
+}
- smallestteam = 0;
- smallestteam_count = 999999999;
+entity TeamBalanceTeam_GetLowestBot(entity team_)
+{
+ return team_.m_lowest_bot;
+}
- if(c1 >= 0 && c1 < smallestteam_count)
+int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
+ entity player, bool use_score)
+{
+ if (team_a == team_b)
+ {
+ return TEAMS_COMPARE_EQUAL;
+ }
+ if (!TeamBalanceTeam_IsAllowed(team_a) ||
+ !TeamBalanceTeam_IsAllowed(team_b))
+ {
+ return TEAMS_COMPARE_INVALID;
+ }
+ int num_players_team_a = team_a.m_num_players;
+ int num_players_team_b = team_b.m_num_players;
+ if (IS_REAL_CLIENT(player) && bots_would_leave)
{
- smallestteam = 1;
- smallestteam_count = c1;
+ num_players_team_a -= team_a.m_num_bots;
+ num_players_team_b -= team_b.m_num_bots;
}
- if(c2 >= 0 && c2 < smallestteam_count)
+ if (num_players_team_a < num_players_team_b)
{
- smallestteam = 2;
- smallestteam_count = c2;
+ return TEAMS_COMPARE_LESS;
}
- if(c3 >= 0 && c3 < smallestteam_count)
+ if (num_players_team_a > num_players_team_b)
{
- smallestteam = 3;
- smallestteam_count = c3;
+ return TEAMS_COMPARE_GREATER;
}
- if(c4 >= 0 && c4 < smallestteam_count)
+ if (!use_score)
{
- smallestteam = 4;
- smallestteam_count = c4;
+ return TEAMS_COMPARE_EQUAL;
}
+ if (team_a.m_team_score < team_b.m_team_score)
+ {
+ return TEAMS_COMPARE_LESS;
+ }
+ if (team_a.m_team_score > team_b.m_team_score)
+ {
+ return TEAMS_COMPARE_GREATER;
+ }
+ return TEAMS_COMPARE_EQUAL;
+}
- if(!smallestteam)
+void SV_ChangeTeam(entity this, float _color)
+{
+ int source_color, destination_color;
+ int source_team_index, destination_team_index;
+
+ // in normal deathmatch we can just apply the color and we're done
+ if(!teamplay)
+ SetPlayerColors(this, _color);
+
+ if(!IS_CLIENT(this))
{
- bprint("warning: no smallest team\n");
+ // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
+ Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
return;
}
- if(source_team == 1)
- steam = NUM_TEAM_1;
- else if(source_team == 2)
- steam = NUM_TEAM_2;
- else if(source_team == 3)
- steam = NUM_TEAM_3;
- else // if(source_team == 4)
- steam = NUM_TEAM_4;
+ if(!teamplay)
+ return;
- lowest_bot = NULL;
- lowest_bot_score = 999999999;
- lowest_player = NULL;
- lowest_player_score = 999999999;
+ source_color = this.clientcolors & 0x0F;
+ destination_color = _color & 0x0F;
- // find the lowest-scoring player & bot of that team
- FOREACH_CLIENT(IS_PLAYER(it) && it.team == steam, LAMBDA(
- if(it.isbot)
- {
- if(it.totalfrags < lowest_bot_score)
- {
- lowest_bot = it;
- lowest_bot_score = it.totalfrags;
- }
- }
- else
- {
- if(it.totalfrags < lowest_player_score)
- {
- lowest_player = it;
- lowest_player_score = it.totalfrags;
- }
- }
- ));
+ source_team_index = Team_TeamToIndex(source_color + 1);
+ destination_team_index = Team_TeamToIndex(destination_color + 1);
- // prefers to move a bot...
- if(lowest_bot != NULL)
- selected = lowest_bot;
- // but it will move a player if it has to
- else
- selected = lowest_player;
- // don't do anything if it couldn't find anyone
- if(!selected)
+ if (destination_team_index == -1)
{
- bprint("warning: couldn't find a player to move from team\n");
return;
}
- // smallest team gains a member
- if(smallestteam == 1)
+ entity balance = TeamBalance_CheckAllowedTeams(this);
+
+ if (destination_team_index == 1 && !TeamBalance_IsTeamAllowedInternal(
+ balance, 1))
{
- c1 = c1 + 1;
+ destination_team_index = 4;
}
- else if(smallestteam == 2)
+ if (destination_team_index == 4 && !TeamBalance_IsTeamAllowedInternal(
+ balance, 4))
{
- c2 = c2 + 1;
+ destination_team_index = 3;
}
- else if(smallestteam == 3)
+ if (destination_team_index == 3 && !TeamBalance_IsTeamAllowedInternal(
+ balance, 3))
{
- c3 = c3 + 1;
+ destination_team_index = 2;
}
- else if(smallestteam == 4)
+ if (destination_team_index == 2 && !TeamBalance_IsTeamAllowedInternal(
+ balance, 2))
{
- c4 = c4 + 1;
+ destination_team_index = 1;
}
- else
+
+ // not changing teams
+ if (source_color == destination_color)
{
- bprint("warning: destination team invalid\n");
+ SetPlayerTeam(this, destination_team_index, source_team_index, true);
+ TeamBalance_Destroy(balance);
return;
}
- // source team loses a member
- if(source_team == 1)
- {
- c1 = c1 + 1;
- }
- else if(source_team == 2)
- {
- c2 = c2 + 2;
+
+ if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
+ Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
+ return; // changing teams is not allowed
}
- else if(source_team == 3)
+
+ // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
+ if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
{
- c3 = c3 + 3;
+ TeamBalance_GetTeamCounts(balance, this);
+ if ((Team_IndexToBit(destination_team_index) &
+ TeamBalance_FindBestTeams(balance, this, false)) == 0)
+ {
+ Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
+ TeamBalance_Destroy(balance);
+ return;
+ }
}
- else if(source_team == 4)
+ if (IS_PLAYER(this) && source_team_index != destination_team_index)
{
- c4 = c4 + 4;
+ // reduce frags during a team change
+ PlayerScore_Clear(this);
}
- else
+ if (!SetPlayerTeam(this, destination_team_index, source_team_index,
+ !IS_CLIENT(this)))
{
- bprint("warning: source team invalid\n");
+ TeamBalance_Destroy(balance);
return;
}
-
- // move the player to the new team
- TeamchangeFrags(selected);
- SetPlayerTeam(selected, smallestteam, source_team, false);
-
- if (IS_DEAD(selected) || MUTATOR_CALLHOOK(Player_ChangeTeamKill, selected) == true)
+ TeamBalance_AutoBalanceBots(balance, source_team_index,
+ destination_team_index);
+ if (!IS_PLAYER(this) || (source_team_index == destination_team_index))
{
+ TeamBalance_Destroy(balance);
return;
}
- Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
- Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);
+ KillPlayerForTeamChange(this);
+ TeamBalance_Destroy(balance);
}