From dd243eaa0b5befb96bab8f113f43bc79e9bba098 Mon Sep 17 00:00:00 2001 From: Juice Date: Thu, 14 Oct 2021 13:54:09 +0300 Subject: [PATCH] Update player counts (#684) * Update player counts in CheckWinConditions and SwitchTeam * Add UTIL_SpectatorsInGame and update MaintainBotQuota --- regamedll/dlls/bot/cs_bot_manager.cpp | 9 +++---- regamedll/dlls/multiplay_gamerules.cpp | 6 +++++ regamedll/dlls/player.cpp | 6 +++++ regamedll/game_shared/bot/bot_util.cpp | 33 ++++++++++++++++++++++++++ regamedll/game_shared/bot/bot_util.h | 1 + 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/regamedll/dlls/bot/cs_bot_manager.cpp b/regamedll/dlls/bot/cs_bot_manager.cpp index 7a57339a..786a3f08 100644 --- a/regamedll/dlls/bot/cs_bot_manager.cpp +++ b/regamedll/dlls/bot/cs_bot_manager.cpp @@ -852,6 +852,7 @@ void CCSBotManager::MaintainBotQuota() int totalHumansInGame = UTIL_HumansInGame(); int humanPlayersInGame = UTIL_HumansInGame(IGNORE_SPECTATORS); + int spectatorPlayersInGame = UTIL_SpectatorsInGame(); // don't add bots until local player has been registered, to make sure he's player ID #1 if (!IS_DEDICATED_SERVER() && totalHumansInGame == 0) @@ -872,7 +873,7 @@ void CCSBotManager::MaintainBotQuota() // unless the round is already in progress, in which case we play with what we've been dealt if (!isRoundInProgress) { - desiredBotCount = Q_max(0, desiredBotCount - humanPlayersInGame); + desiredBotCount = Q_max(0, desiredBotCount - humanPlayersInGame + spectatorPlayersInGame); } else { @@ -902,7 +903,7 @@ void CCSBotManager::MaintainBotQuota() // wait for a player to join, if necessary if (cv_bot_join_after_player.value > 0.0) { - if (humanPlayersInGame == 0) + if (humanPlayersInGame == 0 && spectatorPlayersInGame == 0) desiredBotCount = 0; } @@ -917,9 +918,9 @@ void CCSBotManager::MaintainBotQuota() // if bots will auto-vacate, we need to keep one slot open to allow players to join if (cv_bot_auto_vacate.value > 0.0) - desiredBotCount = Q_min(desiredBotCount, gpGlobals->maxClients - (totalHumansInGame + 1)); + desiredBotCount = Q_min(desiredBotCount, gpGlobals->maxClients - (humanPlayersInGame + 1)); else - desiredBotCount = Q_min(desiredBotCount, gpGlobals->maxClients - totalHumansInGame); + desiredBotCount = Q_min(desiredBotCount, gpGlobals->maxClients - humanPlayersInGame + spectatorPlayersInGame); #ifdef REGAMEDLL_FIXES // Try to balance teams, if we are in the first specified seconds of a round and bots can join either team. diff --git a/regamedll/dlls/multiplay_gamerules.cpp b/regamedll/dlls/multiplay_gamerules.cpp index 5fb33f8d..7824ef51 100644 --- a/regamedll/dlls/multiplay_gamerules.cpp +++ b/regamedll/dlls/multiplay_gamerules.cpp @@ -880,7 +880,13 @@ void EXT_FUNC CHalfLifeMultiplay::__API_HOOK(CheckWinConditions)() #ifdef REGAMEDLL_FIXES // If a winner has already been determined.. then get the heck out of here if (m_iRoundWinStatus != WINSTATUS_NONE) + { + // still check if we lost players to where we need to do a full reset next round... + int NumDeadCT, NumDeadTerrorist, NumAliveTerrorist, NumAliveCT; + InitializePlayerCounts(NumAliveTerrorist, NumAliveCT, NumDeadTerrorist, NumDeadCT); + return; + } #else // If a winner has already been determined and game of started.. then get the heck out of here if (m_bGameStarted && m_iRoundWinStatus != WINSTATUS_NONE) diff --git a/regamedll/dlls/player.cpp b/regamedll/dlls/player.cpp index 8c595776..6a4b569a 100644 --- a/regamedll/dlls/player.cpp +++ b/regamedll/dlls/player.cpp @@ -8216,6 +8216,12 @@ void CBasePlayer::__API_HOOK(SwitchTeam)() } } } + +#ifdef REGAMEDLL_FIXES + // Initialize the player counts now that a player has switched teams + int NumDeadCT, NumDeadTerrorist, NumAliveTerrorist, NumAliveCT; + CSGameRules()->InitializePlayerCounts(NumAliveTerrorist, NumAliveCT, NumDeadTerrorist, NumDeadCT); +#endif } void CBasePlayer::UpdateShieldCrosshair(bool draw) diff --git a/regamedll/game_shared/bot/bot_util.cpp b/regamedll/game_shared/bot/bot_util.cpp index 2cbc59cf..d14aaaeb 100644 --- a/regamedll/game_shared/bot/bot_util.cpp +++ b/regamedll/game_shared/bot/bot_util.cpp @@ -131,6 +131,39 @@ int UTIL_HumansInGame(bool ignoreSpectators) return iCount; } +// Returns the number of human spectators in the game +int UTIL_SpectatorsInGame() +{ + int iCount = 0; + + for (int iIndex = 1; iIndex <= gpGlobals->maxClients; iIndex++) + { + CBasePlayer* pPlayer = UTIL_PlayerByIndex(iIndex); + + if (!pPlayer) + continue; + + if (FNullEnt(pPlayer->pev)) + continue; + + if (FStrEq(STRING(pPlayer->pev->netname), "")) + continue; + + if (pPlayer->IsProxy()) + continue; + + if (pPlayer->IsBot()) + continue; + + if (pPlayer->m_iTeam != SPECTATOR) + continue; + + iCount++; + } + + return iCount; +} + int UTIL_HumansOnTeam(int teamID, bool isAlive) { int iCount = 0; diff --git a/regamedll/game_shared/bot/bot_util.h b/regamedll/game_shared/bot/bot_util.h index 45fc271a..e7fbd71b 100644 --- a/regamedll/game_shared/bot/bot_util.h +++ b/regamedll/game_shared/bot/bot_util.h @@ -167,6 +167,7 @@ int UTIL_HumansInGame(bool ignoreSpectators = false); bool UTIL_IsNameTaken(const char *name, bool ignoreHumans = false); int UTIL_ClientsInGame(); +int UTIL_SpectatorsInGame(); int UTIL_ActivePlayersInGame(); int UTIL_BotsInGame(); bool UTIL_KickBotFromTeam(TeamName kickTeam);