Added ChangeTeam function for teamplay

Also removes the TeamLibrary global since all it did was call what should've been static functions.
This commit is contained in:
Boondorl 2024-05-23 12:16:34 -04:00 committed by Ricardo Luís Vaz Silva
parent 3d6e508d67
commit 53270f0bc8
7 changed files with 49 additions and 22 deletions

View file

@ -3359,7 +3359,7 @@ static int D_InitGame(const FIWADInfo* iwad_info, std::vector<std::string>& allw
// [CW] Parse any TEAMINFO lumps.
if (!batchrun) Printf ("ParseTeamInfo: Load team definitions.\n");
TeamLibrary.ParseTeamInfo ();
FTeam::ParseTeamInfo ();
R_ParseTrnslate();
PClassActor::StaticInit ();

View file

@ -197,7 +197,7 @@ void D_GetPlayerColor (int player, float *h, float *s, float *v, FPlayerColorSet
RGBtoHSV (RPART(color)/255.f, GPART(color)/255.f, BPART(color)/255.f,
h, s, v);
if (teamplay && TeamLibrary.IsValidTeam((team = info->GetTeam())) && !Teams[team].GetAllowCustomPlayerColor())
if (teamplay && FTeam::IsValid((team = info->GetTeam())) && !Teams[team].GetAllowCustomPlayerColor())
{
// In team play, force the player to use the team's hue
// and adjust the saturation and value so that the team
@ -264,7 +264,7 @@ int D_PickRandomTeam ()
if (playeringame[i])
{
team = players[i].userinfo.GetTeam();
if (TeamLibrary.IsValidTeam(team))
if (FTeam::IsValid(team))
{
if (Teams[team].m_iPresent++ == 0)
{
@ -319,7 +319,7 @@ static void UpdateTeam (int pnum, int team, bool update)
{
userinfo_t *info = &players[pnum].userinfo;
if ((dmflags2 & DF2_NO_TEAM_SWITCH) && (alwaysapplydmflags || deathmatch) && TeamLibrary.IsValidTeam (info->GetTeam()))
if ((dmflags2 & DF2_NO_TEAM_SWITCH) && (alwaysapplydmflags || deathmatch) && FTeam::IsValid (info->GetTeam()))
{
Printf ("%s\n", GStrings.GetString("TXT_NO_TEAM_CHANGE"));
return;
@ -327,7 +327,7 @@ static void UpdateTeam (int pnum, int team, bool update)
int oldteam;
if (!TeamLibrary.IsValidTeam (team))
if (!FTeam::IsValid (team))
{
team = TEAM_NONE;
}
@ -337,7 +337,7 @@ static void UpdateTeam (int pnum, int team, bool update)
if (update && oldteam != team)
{
FString message;
if (TeamLibrary.IsValidTeam (team))
if (FTeam::IsValid (team))
{
message = GStrings.GetString("TXT_JOINED_TEAM");
message.Substitute("%t", Teams[team].GetName());
@ -356,7 +356,7 @@ static void UpdateTeam (int pnum, int team, bool update)
StatusBar->AttachToPlayer (&players[pnum]);
}
// Double-check
if (!TeamLibrary.IsValidTeam (team))
if (!FTeam::IsValid (team))
{
*static_cast<FIntCVar *>((*info)[NAME_Team]) = TEAM_NONE;
}
@ -365,7 +365,7 @@ static void UpdateTeam (int pnum, int team, bool update)
int D_GetFragCount (player_t *player)
{
const int team = player->userinfo.GetTeam();
if (!teamplay || !TeamLibrary.IsValidTeam(team))
if (!teamplay || !FTeam::IsValid(team))
{
return player->fragcount;
}
@ -479,7 +479,7 @@ void userinfo_t::Reset(int pnum)
int userinfo_t::TeamChanged(int team)
{
if (teamplay && !TeamLibrary.IsValidTeam(team))
if (teamplay && !FTeam::IsValid(team))
{ // Force players onto teams in teamplay mode
team = D_PickRandomTeam();
}

View file

@ -43,6 +43,7 @@
#include "v_video.h"
#include "filesystem.h"
#include "vm.h"
#include "d_player.h"
// MACROS ------------------------------------------------------------------
@ -56,9 +57,11 @@
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern bool playeringame[MAXPLAYERS];
extern player_t players[MAXPLAYERS];
// PUBLIC DATA DEFINITIONS -------------------------------------------------
FTeam TeamLibrary;
TArray<FTeam> Teams;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
@ -245,7 +248,7 @@ void FTeam::ClearTeams ()
//
//==========================================================================
bool FTeam::IsValidTeam (unsigned int uiTeam) const
bool FTeam::IsValid (unsigned int uiTeam)
{
if (uiTeam >= Teams.Size ())
return false;
@ -253,6 +256,15 @@ bool FTeam::IsValidTeam (unsigned int uiTeam) const
return true;
}
bool FTeam::ChangeTeam(unsigned int pNum, unsigned int newTeam)
{
if (!multiplayer || !teamplay || pNum >= MAXPLAYERS || !playeringame[pNum] || !FTeam::IsValid(newTeam) || players[pNum].userinfo.GetTeam() == newTeam)
return false;
players[pNum].userinfo.TeamChanged(newTeam);
return true;
}
//==========================================================================
//
// FTeam :: GetName
@ -342,7 +354,7 @@ DEFINE_FIELD_NAMED(FTeam, m_Name, mName)
static int IsValid(unsigned int id)
{
return TeamLibrary.IsValidTeam(id);
return FTeam::IsValid(id);
}
DEFINE_ACTION_FUNCTION_NATIVE(FTeam, IsValid, IsValid)
@ -350,7 +362,21 @@ DEFINE_ACTION_FUNCTION_NATIVE(FTeam, IsValid, IsValid)
PARAM_PROLOGUE;
PARAM_UINT(id);
ACTION_RETURN_BOOL(TeamLibrary.IsValidTeam(id));
ACTION_RETURN_BOOL(FTeam::IsValid(id));
}
static int ChangeTeam(unsigned int pNum, unsigned int newTeam)
{
return FTeam::ChangeTeam(pNum, newTeam);
}
DEFINE_ACTION_FUNCTION_NATIVE(FTeam, ChangeTeam, ChangeTeam)
{
PARAM_PROLOGUE;
PARAM_UINT(pNum);
PARAM_UINT(newTeam);
ACTION_RETURN_BOOL(FTeam::ChangeTeam(pNum, newTeam));
}
static int GetPlayerColor(FTeam* self)

View file

@ -45,8 +45,9 @@ class FTeam
{
public:
FTeam ();
void ParseTeamInfo ();
bool IsValidTeam (unsigned int uiTeam) const;
static void ParseTeamInfo ();
static bool IsValid (unsigned int uiTeam);
static bool ChangeTeam(unsigned int pNum, unsigned int newTeam);
const char *GetName () const;
int GetPlayerColor () const;
@ -60,8 +61,8 @@ public:
int m_iTies;
private:
void ParseTeamDefinition (FScanner &Scan);
void ClearTeams ();
static void ParseTeamDefinition (FScanner &Scan);
static void ClearTeams ();
public: // needed for script access.
FString m_Name;
@ -72,7 +73,6 @@ private:
bool m_bAllowCustomPlayerColor;
};
extern FTeam TeamLibrary;
extern TArray<FTeam> Teams;
#endif

View file

@ -293,7 +293,7 @@ static void HU_DoDrawScores (player_t *player, player_t *sortedplayers[MAXPLAYER
for (i = 0; i < MAXPLAYERS; ++i)
{
if (playeringame[sortedplayers[i]-players] && TeamLibrary.IsValidTeam (sortedplayers[i]->userinfo.GetTeam()))
if (playeringame[sortedplayers[i]-players] && FTeam::IsValid (sortedplayers[i]->userinfo.GetTeam()))
{
if (Teams[sortedplayers[i]->userinfo.GetTeam()].m_iPlayerCount++ == 0)
{
@ -485,7 +485,7 @@ int HU_GetRowColor(player_t *player, bool highlight)
{
if (teamplay && deathmatch)
{
if (TeamLibrary.IsValidTeam (player->userinfo.GetTeam()))
if (FTeam::IsValid (player->userinfo.GetTeam()))
return Teams[player->userinfo.GetTeam()].GetTextColor();
else
return CR_GREY;

View file

@ -305,7 +305,7 @@ bool FCajunMaster::SpawnBot (const char *name, int color)
{
concat << colors[bot_next_color];
}
if (TeamLibrary.IsValidTeam (thebot->lastteam))
if (FTeam::IsValid (thebot->lastteam))
{ // Keep the bot on the same team when switching levels
concat.AppendFormat("\\team\\%d\n", thebot->lastteam);
}
@ -587,7 +587,7 @@ bool FCajunMaster::LoadBots ()
if (IsNum (sc.String))
{
teamnum = atoi (sc.String);
if (!TeamLibrary.IsValidTeam (teamnum))
if (!FTeam::IsValid (teamnum))
{
teamnum = TEAM_NONE;
}

View file

@ -2989,6 +2989,7 @@ struct Team native
native String mName;
native static bool IsValid(uint teamIndex);
native static bool ChangeTeam(uint playerNumber, uint newTeamIndex);
native Color GetPlayerColor() const;
native int GetTextColor() const;