Skip to content

Commit 720cf6e

Browse files
committed
Implement APlayerSet::FinishUpAddingAIs
1 parent 3e86f23 commit 720cf6e

File tree

2 files changed

+55
-26
lines changed

2 files changed

+55
-26
lines changed

OpenBarnyard/Source/Player/APlayerSet.cpp

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void APlayerSet::Reset()
2626
{
2727
m_strUnk.SetPooledString( TNULL );
2828

29-
m_vecHumanSlots.Clear();
29+
m_vecPlayerSlots.Clear();
3030
m_aPlayerTeams[ 0 ] = 2;
3131
m_aPlayerTeams[ 1 ] = 2;
3232
m_aPlayerTeams[ 2 ] = 2;
@@ -45,7 +45,7 @@ TINT APlayerSet::AddAIPlayer()
4545
}
4646

4747
// $Barnyard: FUNCTION 006206d0
48-
APlayerHumanSlot* APlayerSet::AddHumanPlayer( Toshi::TInputDeviceController* a_pController )
48+
APlayerSlot* APlayerSet::AddHumanPlayer( Toshi::TInputDeviceController* a_pController )
4949
{
5050
// Adjust number of AI players
5151
if ( m_iNumHumanPlayers + m_iNumAIPlayers >= MAX_NUM_PLAYERS )
@@ -57,20 +57,20 @@ APlayerHumanSlot* APlayerSet::AddHumanPlayer( Toshi::TInputDeviceController* a_p
5757
}
5858

5959
// Find free slot
60-
decltype( m_vecHumanSlots )::Iterator itInsertBefore = m_vecHumanSlots.Begin();
61-
T2_FOREACH( m_vecHumanSlots, it )
60+
auto itInsertBefore = m_vecPlayerSlots.Begin();
61+
T2_FOREACH( m_vecPlayerSlots, it )
6262
{
63-
if ( !it->m_bUsed )
63+
if ( it->m_uiFlags == 0 )
6464
{
6565
itInsertBefore = it;
6666
break;
6767
}
6868
}
6969

70-
APlayerHumanSlot oUsedSlot( a_pController, TTRUE );
71-
m_vecHumanSlots.InsertBefore( itInsertBefore, oUsedSlot );
70+
APlayerSlot oUsedSlot( a_pController, APlayerSlot::FLAGS_USED );
71+
m_vecPlayerSlots.InsertBefore( itInsertBefore, oUsedSlot );
7272

73-
return &m_vecHumanSlots[ itInsertBefore.Index() ];
73+
return &m_vecPlayerSlots[ itInsertBefore.Index() ];
7474
}
7575

7676
// $Barnyard: FUNCTION 006205f0
@@ -131,49 +131,68 @@ void APlayerSet::MakeTeamsFair()
131131
}
132132
}
133133

134+
// $Barnyard: FUNCTION 00620820
135+
void APlayerSet::FinishUpAddingAIs()
136+
{
137+
// Count all player slots without the AI flag
138+
TINT iCurrentNumAIPlayers = 0;
139+
for ( TINT i = 0; i < m_vecPlayerSlots.Size(); i++ )
140+
{
141+
if ( m_vecPlayerSlots[ i ].m_uiFlags & APlayerSlot::FLAGS_AI )
142+
iCurrentNumAIPlayers += 1;
143+
}
144+
145+
// Add missing AI players
146+
while ( iCurrentNumAIPlayers < m_iNumAIPlayers )
147+
{
148+
m_vecPlayerSlots.PushBack( APlayerSlot( TNULL, APlayerSlot::FLAGS_AI ) );
149+
iCurrentNumAIPlayers += 1;
150+
}
151+
}
152+
134153
// $Barnyard: FUNCTION 00620400
135154
TBOOL APlayerSet::IsPlayerSlotUsed( TINT a_iSlot ) const
136155
{
137-
return m_vecHumanSlots[ a_iSlot ].m_bUsed & 1;
156+
return m_vecPlayerSlots[ a_iSlot ].m_uiFlags & 1;
138157
}
139158

140159
// $Barnyard: FUNCTION 006206d0
141-
APlayerHumanSlot::APlayerHumanSlot( Toshi::TInputDeviceController* a_pController, TBOOL a_bUsed )
160+
APlayerSlot::APlayerSlot( Toshi::TInputDeviceController* a_pController, FLAGS a_uiFlags )
142161
: m_Unk1( 0 )
143162
, m_pController( a_pController )
144-
, m_bUsed( a_bUsed )
163+
, m_uiFlags( a_uiFlags )
145164
{
146165
}
147166

148167
// $Barnyard: FUNCTION 006206e0
149-
APlayerHumanSlot::APlayerHumanSlot( const APlayerHumanSlot& a_rcOther )
168+
APlayerSlot::APlayerSlot( const APlayerSlot& a_rcOther )
150169
{
151170
m_Unk1 = a_rcOther.m_Unk1;
152171
m_pController = a_rcOther.m_pController;
153172
m_strUnk = a_rcOther.m_strUnk;
154-
m_bUsed = a_rcOther.m_bUsed;
173+
m_uiFlags = a_rcOther.m_uiFlags;
155174
}
156175

157176
// $Barnyard: FUNCTION 00620740
158-
APlayerHumanSlot::~APlayerHumanSlot()
177+
APlayerSlot::~APlayerSlot()
159178
{
160179
Reset();
161180
}
162181

163182
// $Barnyard: FUNCTION 006207c0
164-
APlayerHumanSlot& APlayerHumanSlot::operator=( const APlayerHumanSlot& a_rcOther )
183+
APlayerSlot& APlayerSlot::operator=( const APlayerSlot& a_rcOther )
165184
{
166185
Reset();
167186

168187
m_Unk1 = a_rcOther.m_Unk1;
169188
m_pController = a_rcOther.m_pController;
170189
m_strUnk = a_rcOther.m_strUnk;
171-
m_bUsed = a_rcOther.m_bUsed;
190+
m_uiFlags = a_rcOther.m_uiFlags;
172191

173192
return *this;
174193
}
175194

176-
void APlayerHumanSlot::Reset()
195+
void APlayerSlot::Reset()
177196
{
178197
TASSERT( TFALSE && "Destroy Unk1" );
179198
m_strUnk.SetPooledString( TNULL );

OpenBarnyard/Source/Player/APlayerSet.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,31 @@ enum APLAYERTEAM_ : APLAYERTEAM
1515
APLAYERTEAM_NONE
1616
};
1717

18-
class APlayerHumanSlot
18+
class APlayerSlot
1919
{
2020
public:
21-
APlayerHumanSlot( Toshi::TInputDeviceController* a_pController = TNULL, TBOOL a_bUsed = TFALSE );
22-
APlayerHumanSlot( const APlayerHumanSlot& a_rcOther );
23-
~APlayerHumanSlot();
21+
using FLAGS = TUINT8;
22+
enum FLAGS_ : FLAGS
23+
{
24+
FLAGS_NONE = 0,
25+
FLAGS_USED = BITFLAG( 0 ),
26+
FLAGS_AI = BITFLAG( 1 ),
27+
};
28+
29+
public:
30+
APlayerSlot( Toshi::TInputDeviceController* a_pController = TNULL, FLAGS a_uiFlags = FLAGS_NONE );
31+
APlayerSlot( const APlayerSlot& a_rcOther );
32+
~APlayerSlot();
2433

2534
void Reset();
2635

27-
APlayerHumanSlot& operator=( const APlayerHumanSlot& a_rcOther );
36+
APlayerSlot& operator=( const APlayerSlot& a_rcOther );
2837

2938
public:
3039
TINT m_Unk1;
3140
Toshi::TInputDeviceController* m_pController;
3241
Toshi::TPString8 m_strUnk;
33-
TBOOL m_bUsed;
42+
TUINT8 m_uiFlags;
3443
};
3544

3645
class APlayerSet
@@ -41,16 +50,17 @@ class APlayerSet
4150

4251
void Reset();
4352

44-
TINT AddAIPlayer();
45-
APlayerHumanSlot* AddHumanPlayer( Toshi::TInputDeviceController* a_pController );
53+
TINT AddAIPlayer();
54+
APlayerSlot* AddHumanPlayer( Toshi::TInputDeviceController* a_pController );
4655

4756
void MakeTeamsFair();
57+
void FinishUpAddingAIs();
4858

4959
TBOOL IsPlayerSlotUsed( TINT a_iSlot ) const;
5060

5161
private:
5262
Toshi::TPString8 m_strUnk;
53-
Toshi::T2Vector<APlayerHumanSlot, MAX_NUM_PLAYERS> m_vecHumanSlots;
63+
Toshi::T2Vector<APlayerSlot, MAX_NUM_PLAYERS> m_vecPlayerSlots;
5464
APLAYERTEAM m_aPlayerTeams[ MAX_NUM_PLAYERS ];
5565
TINT m_iNumAIPlayers;
5666
TINT m_iNumHumanPlayers;

0 commit comments

Comments
 (0)