From d03d59796369649f91090e6a6a9f5039a38acf26 Mon Sep 17 00:00:00 2001
From: Lactozilla <jp6781615@gmail.com>
Date: Sun, 6 Aug 2023 01:56:26 -0300
Subject: [PATCH] Minor autobalance fix

---
 src/p_tick.c | 24 ++++++++++--------------
 src/p_user.c |  1 -
 2 files changed, 10 insertions(+), 15 deletions(-)

diff --git a/src/p_tick.c b/src/p_tick.c
index 0b35ed7e30..4e802297fe 100644
--- a/src/p_tick.c
+++ b/src/p_tick.c
@@ -485,24 +485,20 @@ static void P_DoAutobalanceTeams(void)
 		{
 			i = M_RandomKey(count[G_GetTeam(1)]);
 			NetPacket.packet.newteam = 2;
-			NetPacket.packet.playernum = array[1][i];
-			NetPacket.packet.verification = true;
-			NetPacket.packet.autobalance = true;
-
-			usvalue  = SHORT(NetPacket.value.l|NetPacket.value.b);
-			SendNetXCmd(XD_TEAMCHANGE, &usvalue, sizeof(usvalue));
+			NetPacket.packet.playernum = array[G_GetTeam(1)][i];
 		}
 		else
 		{
 			i = M_RandomKey(count[G_GetTeam(2)]);
 			NetPacket.packet.newteam = 1;
-			NetPacket.packet.playernum = array[2][i];
-			NetPacket.packet.verification = true;
-			NetPacket.packet.autobalance = true;
-
-			usvalue  = SHORT(NetPacket.value.l|NetPacket.value.b);
-			SendNetXCmd(XD_TEAMCHANGE, &usvalue, sizeof(usvalue));
+			NetPacket.packet.playernum = array[G_GetTeam(2)][i];
 		}
+
+		NetPacket.packet.verification = true;
+		NetPacket.packet.autobalance = true;
+
+		usvalue = SHORT(NetPacket.value.l|NetPacket.value.b);
+		SendNetXCmd(XD_TEAMCHANGE, &usvalue, sizeof(usvalue));
 	}
 }
 
@@ -651,7 +647,7 @@ static inline void P_DoTagStuff(void)
 	}
 }
 
-static inline void P_DoCTFStuff(void)
+static inline void P_DoTeamGametypeStuff(void)
 {
 	// Automatic team balance for CTF and team match
 	if (leveltime % (TICRATE * 5) == 0) //only check once per five seconds for the sake of CPU conservation.
@@ -803,7 +799,7 @@ void P_Ticker(boolean run)
 		P_DoTagStuff();
 
 	if (G_GametypeHasTeams())
-		P_DoCTFStuff();
+		P_DoTeamGametypeStuff();
 
 	if (run)
 	{
diff --git a/src/p_user.c b/src/p_user.c
index 7e2ef1bf5b..c0d9e2f694 100644
--- a/src/p_user.c
+++ b/src/p_user.c
@@ -10506,7 +10506,6 @@ boolean P_SpectatorJoinGame(player_t *player)
 
 	// Team changing in Team Match and CTF
 	// Pressing fire assigns you to a team that needs players if allowed.
-	// Partial code reproduction from p_tick.c autobalance code.
 	else if (G_GametypeHasTeams())
 	{
 		UINT8 changeto = G_GetMostDisadvantagedTeam();
-- 
GitLab