std::shuffle players list for randomized starting position

This commit is contained in:
Benau 2018-11-25 16:12:39 +08:00
parent 4c534dc28c
commit 950695769c
3 changed files with 11 additions and 5 deletions

View File

@ -265,19 +265,25 @@ void GameSetup::sortPlayersForGrandPrix()
} // sortPlayersForGrandPrix
//-----------------------------------------------------------------------------
void GameSetup::sortPlayersForTeamGame()
void GameSetup::sortPlayersForGame()
{
std::lock_guard<std::mutex> lock(m_players_mutex);
if (!isGrandPrix())
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(m_players.begin(), m_players.end(), g);
}
if (!race_manager->teamEnabled() ||
ServerConfig::m_team_choosing)
return;
std::lock_guard<std::mutex> lock(m_players_mutex);
for (unsigned i = 0; i < m_players.size(); i++)
{
auto player = m_players[i].lock();
assert(player);
player->setTeam((KartTeam)(i % 2));
}
} // sortPlayersForTeamGame
} // sortPlayersForGame
// ----------------------------------------------------------------------------
std::pair<int, int> GameSetup::getPlayerTeamInfo() const

View File

@ -165,7 +165,7 @@ public:
// ------------------------------------------------------------------------
void sortPlayersForGrandPrix();
// ------------------------------------------------------------------------
void sortPlayersForTeamGame();
void sortPlayersForGame();
// ------------------------------------------------------------------------
void setHitCaptureTime(int hc, float time)
{

View File

@ -509,7 +509,7 @@ void ServerLobby::asynchronousUpdate()
// Remove disconnected player (if any) one last time
m_game_setup->update(true);
m_game_setup->sortPlayersForGrandPrix();
m_game_setup->sortPlayersForTeamGame();
m_game_setup->sortPlayersForGame();
auto players = m_game_setup->getConnectedPlayers();
for (auto& player : players)
player->getPeer()->clearAvailableKartIDs();