From 1514c36220e017e5f50f5c4d5aa89b05650fc637 Mon Sep 17 00:00:00 2001
From: Lactozilla <jp6781615@gmail.com>
Date: Sun, 6 Aug 2023 21:48:46 -0300
Subject: [PATCH] Properly replace MT_REDFLAG and MT_BLUEFLAG with the flags of
 the current teams

---
 src/g_game.c | 2 ++
 src/p_mobj.c | 9 ++++++++-
 src/p_spec.c | 8 ++++----
 3 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/g_game.c b/src/g_game.c
index 7ee6bab36f..987a5faccf 100644
--- a/src/g_game.c
+++ b/src/g_game.c
@@ -3935,6 +3935,8 @@ void G_InitTeam(UINT8 team)
 	G_FreeTeamData(team);
 
 	memset(&teams[team], 0, sizeof(team_t));
+
+	teams[team].flag = 1 << (team - 1);
 }
 
 UINT8 G_AddTeam(const char *name)
diff --git a/src/p_mobj.c b/src/p_mobj.c
index 4755583c13..56c7d4cb6e 100644
--- a/src/p_mobj.c
+++ b/src/p_mobj.c
@@ -12083,7 +12083,7 @@ static boolean P_AllowMobjSpawn(mapthing_t* mthing, mobjtype_t i)
 	}
 	else if (i == MT_BLUEFLAG || i == MT_REDFLAG || i == MT_TEAMFLAG)
 	{
-		UINT8 team = i == MT_TEAMFLAG ? mthing->args[0] : mobjinfo[i].mass;
+		UINT8 team = i == MT_TEAMFLAG ? mthing->args[0] : G_GetTeam(mobjinfo[i].mass);
 		if (team == TEAM_NONE || team >= numteams)
 			return false;
 		else if (flagmobjs[team] && !P_MobjWasRemoved(flagmobjs[team]))
@@ -12178,6 +12178,13 @@ static mobjtype_t P_GetMobjtypeSubstitute(mapthing_t *mthing, mobjtype_t i)
 			return MT_NULL;
 		return teams[team].flag_mobj_type;
 	}
+	else if (i == MT_BLUEFLAG || i == MT_REDFLAG)
+	{
+		INT32 team = G_GetTeam(mobjinfo[i].mass);
+		if (team == TEAM_NONE || team >= numteams)
+			return MT_NULL;
+		return teams[team].flag_mobj_type;
+	}
 
 	if (!(gametyperules & GTR_TEAMS))
 	{
diff --git a/src/p_spec.c b/src/p_spec.c
index 36e07d819a..9810ec20f5 100644
--- a/src/p_spec.c
+++ b/src/p_spec.c
@@ -3945,7 +3945,7 @@ void P_SetupSignExit(player_t *player)
 //
 boolean P_TeamHasFlagAtBase(UINT8 team)
 {
-	if (team == TEAM_NONE || team >= teamsingame)
+	if (team == TEAM_NONE || team >= numteams)
 		return false;
 
 	thinker_t *think;
@@ -4738,7 +4738,7 @@ static void P_ProcessExitSector(player_t *player, mtag_t sectag)
 
 static void P_ProcessTeamBase(player_t *player, UINT8 team)
 {
-	if (team == TEAM_NONE || team >= teamsingame)
+	if (team == TEAM_NONE || team >= numteams)
 		return;
 
 	if (!(gametyperules & GTR_TEAMFLAGS))
@@ -4794,14 +4794,14 @@ static void P_ProcessTeamBase(player_t *player, UINT8 team)
 	if (totalcaptured == 1)
 	{
 		UINT8 otherteam = teamscaptured[0];
-		HU_DoCEcho(va(M_GetText("%s%s\200\\captured the %s%s\200.\\\\\\\\"), GetChatColorForSkincolor(G_GetTeamColor(team)), player_names[player-players], GetChatColorForSkincolor(G_GetTeamColor(otherteam)), G_GetTeamFlagName(otherteam)));
+		HU_DoCEcho(va(M_GetText("%s%s\200\\captured the %s%s\200.\\\\\\\\"), GetChatColorForSkincolor(G_GetTeamColor(player->ctfteam)), player_names[player-players], GetChatColorForSkincolor(G_GetTeamColor(otherteam)), G_GetTeamFlagName(otherteam)));
 	}
 	else
 	{
 		char *buffer = NULL;
 		size_t buffer_size = 0;
 
-		const char *text = va(M_GetText("%s%s\200 captured the:\\"), GetChatColorForSkincolor(G_GetTeamColor(team)), player_names[player-players]);
+		const char *text = va(M_GetText("%s%s\200 captured the:\\"), GetChatColorForSkincolor(G_GetTeamColor(player->ctfteam)), player_names[player-players]);
 		buffer_size += strlen(text) + 1;
 		buffer = Z_Realloc(buffer, buffer_size, PU_STATIC, NULL);
 		strcpy(buffer, text);
-- 
GitLab