Skip to content

Commit 3e86f23

Browse files
committed
Implement APlayerSet::MakeTeamsFair
1 parent 46b8285 commit 3e86f23

File tree

2 files changed

+84
-19
lines changed

2 files changed

+84
-19
lines changed

OpenBarnyard/Source/Player/APlayerSet.cpp

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ void APlayerSet::Reset()
2727
m_strUnk.SetPooledString( TNULL );
2828

2929
m_vecHumanSlots.Clear();
30-
m_iUnk1 = 2;
31-
m_iUnk2 = 2;
32-
m_iUnk3 = 2;
33-
m_iUnk4 = 2;
34-
m_iNumAIPlayers = 0;
35-
m_iNumHumanPlayers = 0;
30+
m_aPlayerTeams[ 0 ] = 2;
31+
m_aPlayerTeams[ 1 ] = 2;
32+
m_aPlayerTeams[ 2 ] = 2;
33+
m_aPlayerTeams[ 3 ] = 2;
34+
m_iNumAIPlayers = 0;
35+
m_iNumHumanPlayers = 0;
3636
}
3737

3838
// $Barnyard: FUNCTION 00620310
@@ -69,10 +69,68 @@ APlayerHumanSlot* APlayerSet::AddHumanPlayer( Toshi::TInputDeviceController* a_p
6969

7070
APlayerHumanSlot oUsedSlot( a_pController, TTRUE );
7171
m_vecHumanSlots.InsertBefore( itInsertBefore, oUsedSlot );
72-
72+
7373
return &m_vecHumanSlots[ itInsertBefore.Index() ];
7474
}
7575

76+
// $Barnyard: FUNCTION 006205f0
77+
void APlayerSet::MakeTeamsFair()
78+
{
79+
if ( m_aPlayerTeams[ 0 ] == APLAYERTEAM_NONE ) return;
80+
81+
const TINT iTotalNumPlayers = m_iNumAIPlayers + m_iNumHumanPlayers;
82+
if ( iTotalNumPlayers > 1 )
83+
{
84+
// Check if there's at least one player in different team
85+
for ( TINT i = 1; i < iTotalNumPlayers; i++ )
86+
{
87+
if ( m_aPlayerTeams[ 0 ] != m_aPlayerTeams[ i ] ) break;
88+
89+
// Every player is in a single team
90+
if ( i + 1 >= iTotalNumPlayers ) return;
91+
}
92+
93+
if ( m_iNumHumanPlayers < iTotalNumPlayers )
94+
{
95+
// There are some AI players, so need to fill flags for them
96+
for ( TINT i = m_iNumHumanPlayers; i < iTotalNumPlayers; i++ )
97+
m_aPlayerTeams[ i ] = APLAYERTEAM_NONE;
98+
}
99+
100+
// Count Team 1 players
101+
TINT iNumTeam1 = 0;
102+
for ( TINT i = 0; i < iTotalNumPlayers; i++ )
103+
{
104+
if ( m_aPlayerTeams[ i ] == APLAYERTEAM_TEAM1 ) iNumTeam1++;
105+
}
106+
107+
// Count Team 2 players
108+
TINT iNumTeam2 = 0;
109+
for ( TINT i = 0; i < iTotalNumPlayers; i++ )
110+
{
111+
if ( m_aPlayerTeams[ i ] == APLAYERTEAM_TEAM2 ) iNumTeam2++;
112+
}
113+
114+
// Balance teams
115+
for ( TINT i = 0; i < iTotalNumPlayers; i++ )
116+
{
117+
if ( m_aPlayerTeams[ i ] == APLAYERTEAM_NONE )
118+
{
119+
if ( iNumTeam1 < iNumTeam2 )
120+
{
121+
m_aPlayerTeams[ i ] = APLAYERTEAM_TEAM1;
122+
iNumTeam1 += 1;
123+
}
124+
else
125+
{
126+
m_aPlayerTeams[ i ] = APLAYERTEAM_TEAM2;
127+
iNumTeam2 += 1;
128+
}
129+
}
130+
}
131+
}
132+
}
133+
76134
// $Barnyard: FUNCTION 00620400
77135
TBOOL APlayerSet::IsPlayerSlotUsed( TINT a_iSlot ) const
78136
{
@@ -90,10 +148,10 @@ APlayerHumanSlot::APlayerHumanSlot( Toshi::TInputDeviceController* a_pController
90148
// $Barnyard: FUNCTION 006206e0
91149
APlayerHumanSlot::APlayerHumanSlot( const APlayerHumanSlot& a_rcOther )
92150
{
93-
m_Unk1 = a_rcOther.m_Unk1;
94-
m_pController = a_rcOther.m_pController;
95-
m_strUnk = a_rcOther.m_strUnk;
96-
m_bUsed = a_rcOther.m_bUsed;
151+
m_Unk1 = a_rcOther.m_Unk1;
152+
m_pController = a_rcOther.m_pController;
153+
m_strUnk = a_rcOther.m_strUnk;
154+
m_bUsed = a_rcOther.m_bUsed;
97155
}
98156

99157
// $Barnyard: FUNCTION 00620740
@@ -107,10 +165,10 @@ APlayerHumanSlot& APlayerHumanSlot::operator=( const APlayerHumanSlot& a_rcOther
107165
{
108166
Reset();
109167

110-
m_Unk1 = a_rcOther.m_Unk1;
111-
m_pController = a_rcOther.m_pController;
112-
m_strUnk = a_rcOther.m_strUnk;
113-
m_bUsed = a_rcOther.m_bUsed;
168+
m_Unk1 = a_rcOther.m_Unk1;
169+
m_pController = a_rcOther.m_pController;
170+
m_strUnk = a_rcOther.m_strUnk;
171+
m_bUsed = a_rcOther.m_bUsed;
114172

115173
return *this;
116174
}

OpenBarnyard/Source/Player/APlayerSet.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
#define MAX_NUM_PLAYERS SPLITSCREEN_MAX_NUM_PLAYERS
99

10+
using APLAYERTEAM = TUINT32;
11+
enum APLAYERTEAM_ : APLAYERTEAM
12+
{
13+
APLAYERTEAM_TEAM1,
14+
APLAYERTEAM_TEAM2,
15+
APLAYERTEAM_NONE
16+
};
17+
1018
class APlayerHumanSlot
1119
{
1220
public:
@@ -36,15 +44,14 @@ class APlayerSet
3644
TINT AddAIPlayer();
3745
APlayerHumanSlot* AddHumanPlayer( Toshi::TInputDeviceController* a_pController );
3846

47+
void MakeTeamsFair();
48+
3949
TBOOL IsPlayerSlotUsed( TINT a_iSlot ) const;
4050

4151
private:
4252
Toshi::TPString8 m_strUnk;
4353
Toshi::T2Vector<APlayerHumanSlot, MAX_NUM_PLAYERS> m_vecHumanSlots;
44-
TINT m_iUnk1;
45-
TINT m_iUnk2;
46-
TINT m_iUnk3;
47-
TINT m_iUnk4;
54+
APLAYERTEAM m_aPlayerTeams[ MAX_NUM_PLAYERS ];
4855
TINT m_iNumAIPlayers;
4956
TINT m_iNumHumanPlayers;
5057
};

0 commit comments

Comments
 (0)