Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CountActivePlayers,CountHumans,CountZombies + Getter #138

Merged
merged 1 commit into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/engine/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,6 @@ class IServer : public IInterface

virtual int* GetIdMap(int ClientID) = 0;
virtual void SetCustClt(int ClientID) = 0;
// InfClassR spectators vector
std::vector<int> spectators_id;

virtual int GetActivePlayerCount() = 0;
};

class IGameServer : public IInterface
Expand All @@ -384,6 +380,7 @@ class IGameServer : public IInterface

virtual bool IsClientReady(int ClientID) = 0;
virtual bool IsClientPlayer(int ClientID) = 0;
virtual int GetActivePlayerCount() = 0;

virtual const char *GameType() = 0;
virtual const char *Version() = 0;
Expand Down
19 changes: 1 addition & 18 deletions src/engine/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <engine/masterserver.h>
#include <engine/server.h>
#include <engine/storage.h>

#include <engine/shared/compression.h>
#include <engine/shared/config.h>
#include <engine/shared/datafile.h>
Expand Down Expand Up @@ -4386,22 +4385,6 @@ IServer::CClientSession* CServer::GetClientSession(int ClientID)
return &m_aClients[ClientID].m_Session;
}

// returns how many players are currently playing and not spectating
int CServer::GetActivePlayerCount()
{
int PlayerCount = 0;
auto& vec = spectators_id;
for(int i=0; i<MAX_CLIENTS; i++)
{
if(m_aClients[i].m_State == CClient::STATE_INGAME)
{
if (std::find(vec.begin(), vec.end(), i) == vec.end())
PlayerCount++;
}
}
return PlayerCount;
}

void CServer::AddAccusation(int From, int To, const char* pReason)
{
if(From < 0 || From >= MAX_CLIENTS || To < 0 || To >= MAX_CLIENTS)
Expand Down Expand Up @@ -4547,7 +4530,7 @@ IServer::CMapVote* CServer::GetMapVote()
if (m_MapVotesCounter <= 0)
return 0;

float PlayerCount = GetActivePlayerCount();
float PlayerCount = GameServer()->GetActivePlayerCount();

int HighestNum = -1;
int HighestNumIndex = -1;
Expand Down
3 changes: 0 additions & 3 deletions src/engine/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,6 @@ class CServer : public IServer
virtual void ResetMapVotes();
virtual IServer::CMapVote* GetMapVote();
virtual int GetMinPlayersForMap(const char* pMapName);

virtual int GetActivePlayerCount();

virtual int GetTimeShiftUnit() const { return m_TimeShiftUnit; } //In ms
/* INFECTION MODIFICATION END *****************************************/

Expand Down
4 changes: 2 additions & 2 deletions src/game/server/entities/hero-flag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void CHeroFlag::FindPosition()
void CHeroFlag::SetCoolDown()
{
// Set cooldown for next flag depending on how many players are online
int PlayerCount = Server()->GetActivePlayerCount();
int PlayerCount = GameServer()->GetActivePlayerCount();
if (PlayerCount <= 1)
{
// only 1 player on, let him find as many flags as he wants
Expand All @@ -70,7 +70,7 @@ void CHeroFlag::GiveGift(CCharacter* pHero)

if (g_Config.m_InfTurretEnable)
{
if (Server()->GetActivePlayerCount() > 2)
if (GameServer()->GetActivePlayerCount() > 2)
{
if (pHero->m_TurretCount == 0)
pHero->GiveWeapon(WEAPON_HAMMER, -1);
Expand Down
173 changes: 128 additions & 45 deletions src/game/server/gamecontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ void CGameContext::Construct(int Resetting)
m_TargetToKill = -1;
m_TargetToKillCoolDown = 0;
m_HeroGiftCooldown = 0;

m_NbActivePlayers = 0;
m_NbSpectators = 0;
m_NbHumans = 0;
m_NbZombies = 0;
m_ChatResponseTargetID = -1;

if(Resetting==NO_RESET)
Expand Down Expand Up @@ -133,31 +136,118 @@ class CCharacter *CGameContext::GetPlayerChar(int ClientID)
return m_apPlayers[ClientID]->GetCharacter();
}

int CGameContext::GetZombieCount() {
int count = 0;
void CGameContext::CountActivePlayers(){

// returns how many players are currently playing and not spectating
int PlayerCount = 0;
for(int i=0; i<MAX_CLIENTS; i++)
{
if(m_apPlayers[i] && m_apPlayers[i]->m_IsInGame && !m_apPlayers[i]->IsSpectator())
PlayerCount++;
}

m_NbActivePlayers = PlayerCount;

//console output
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "Active Players: %d", m_NbActivePlayers);
Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "game", aBuf);
}


int CGameContext::GetActivePlayerCount()
{
return m_NbActivePlayers;
}

void CGameContext::CountSpectators(){

int SpecCount = 0;
for(int i=0; i<MAX_CLIENTS; i++)
{
if(m_apPlayers[i] && m_apPlayers[i]->m_IsInGame && m_apPlayers[i]->IsSpectator())
SpecCount++;
}

m_NbSpectators = SpecCount;

//console output
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "Spectators: %d", m_NbSpectators);
Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "game", aBuf);

}

int CGameContext::GetSpectatorCount()
{
return m_NbSpectators;
}


void CGameContext::CountHumans()
{
int humanCounter = 0;
int zombieCounter = 0;
for(int i = 0; i < MAX_CLIENTS; i++)
{
if (!m_apPlayers[i])
continue;
if (m_apPlayers[i]->IsHuman())
humanCounter++;
else
zombieCounter++;
}
m_NbHumans = humanCounter;
m_NbZombies = zombieCounter;

dbg_msg("Game", "#humans: %d -- #zombies: %d", m_NbHumans, m_NbZombies);

}

int CGameContext::GetHumanCount()
{
return m_NbHumans;
}

void CGameContext::CountZombies()
{
int humanCounter = 0;
int zombieCounter = 0;
for(int i = 0; i < MAX_CLIENTS; i++)
{
if (!m_apPlayers[i])
continue;
if (m_apPlayers[i]->IsZombie())
count++;
zombieCounter++;
else
humanCounter++;
}
return count;
m_NbZombies = zombieCounter;
m_NbHumans = humanCounter;

dbg_msg("Server_Player_Info", "#humans: %d -- #zombies: %d", m_NbHumans, m_NbZombies);
}

int CGameContext::GetZombieCount()
{
return m_NbZombies;
}

int CGameContext::GetZombieCount(int zombie_class) {
int CGameContext::GetIsOfClassCount(int player_class)
{
int count = 0;
for(int i = 0; i < MAX_CLIENTS; i++)
{
if (!m_apPlayers[i])
continue;
if (m_apPlayers[i]->IsZombie() && m_apPlayers[i]->GetClass() == zombie_class)
if (m_apPlayers[i]->GetClass() == player_class)
count++;
}
return count;
}

int CGameContext::RandomZombieToWitch() {
int CGameContext::RandomZombieToWitch()
{
std::vector<int> zombies_id;

m_WitchCallers.clear();
Expand Down Expand Up @@ -1036,14 +1126,10 @@ void CGameContext::OnTick()
//if(world.paused) // make sure that the game object always updates
m_pController->Tick();

int NumActivePlayers = 0;
for(int i = 0; i < MAX_CLIENTS; i++)
{
if(m_apPlayers[i])
{
if(m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS)
NumActivePlayers++;

{
Server()->RoundStatistics()->UpdatePlayer(i, m_apPlayers[i]->GetTeam() == TEAM_SPECTATORS);

m_apPlayers[i]->Tick();
Expand Down Expand Up @@ -1149,7 +1235,7 @@ void CGameContext::OnTick()
m_aHitSoundState[i] = 0;
}

Server()->RoundStatistics()->UpdateNumberOfPlayers(NumActivePlayers);
Server()->RoundStatistics()->UpdateNumberOfPlayers(GetActivePlayerCount());

/* INFECTION MODIFICATION START ***************************************/
//Clean old dots
Expand Down Expand Up @@ -1338,6 +1424,12 @@ void CGameContext::OnClientEnter(int ClientID)
Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "game", aBuf);

m_VoteUpdate = true;

//Count players
CountActivePlayers();
CountSpectators();
CountHumans(); //updates also zombies

}

void CGameContext::OnClientConnected(int ClientID)
Expand Down Expand Up @@ -1389,6 +1481,11 @@ void CGameContext::OnClientConnected(int ClientID)
m_BroadcastStates[ClientID].m_Priority = BROADCAST_PRIORITY_LOWEST;
m_BroadcastStates[ClientID].m_PrevMessage[0] = 0;
m_BroadcastStates[ClientID].m_NextMessage[0] = 0;

CountActivePlayers();
CountSpectators();
CountHumans(); //updates also zombies

}

void CGameContext::OnClientDrop(int ClientID, int Type, const char *pReason)
Expand Down Expand Up @@ -1418,6 +1515,12 @@ void CGameContext::OnClientDrop(int ClientID, int Type, const char *pReason)
str_format(aBuf, sizeof(aBuf), "leave player='%d:%s'", ClientID, Server()->ClientName(ClientID));
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);

//Count players
CountActivePlayers();
CountSpectators();
CountHumans(); //updates also zombies


// InfClassR end
}

Expand Down Expand Up @@ -1556,7 +1659,7 @@ void CGameContext::OnCallVote(void *pRawMsg, int ClientID)
int RoundCount = m_pController->GetRoundCount();
if (m_pController->IsRoundEndTime())
RoundCount++;
if (g_Config.m_InfMinRoundsForMapVote > RoundCount && Server()->GetActivePlayerCount() > 1)
if (g_Config.m_InfMinRoundsForMapVote > RoundCount && GetActivePlayerCount() > 1)
{
char aBufVoteMap[128];
str_format(aBufVoteMap, sizeof(aBufVoteMap), "Each map must be played at least %i rounds before calling a map vote", g_Config.m_InfMinRoundsForMapVote);
Expand Down Expand Up @@ -1839,15 +1942,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID)
/* INFECTION MODIFICATION START ***************************************/
if(m_apPlayers[ClientID]->IsZombie() && pMsg->m_Team == TEAM_SPECTATORS)
{
int InfectedCount = 0;
CPlayerIterator<PLAYERITER_INGAME> Iter(m_apPlayers);
while(Iter.Next())
{
if(Iter.Player()->IsZombie())
InfectedCount++;
}

if(InfectedCount <= 2)
if(GetZombieCount() <= 2)
{
SendBroadcast_Localization(ClientID, BROADCAST_PRIORITY_GAMEANNOUNCE, BROADCAST_DURATION_GAMEANNOUNCE, _("You can't join the spectators right now"), NULL);
return;
Expand All @@ -1864,13 +1959,6 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID)
if(pPlayer->GetTeam() == TEAM_SPECTATORS || pMsg->m_Team == TEAM_SPECTATORS)
m_VoteUpdate = true;
pPlayer->SetTeam(pMsg->m_Team);
if (pPlayer->GetTeam() == TEAM_SPECTATORS) {
AddSpectatorCID(ClientID);
} else {
RemoveSpectatorCID(ClientID);
}
(void)m_pController->CheckTeamBalance();
pPlayer->m_TeamChangeTick = Server()->Tick();
}
else
SendBroadcast(ClientID, "Teams must be balanced, please join other team", BROADCAST_PRIORITY_GAMEANNOUNCE, BROADCAST_DURATION_GAMEANNOUNCE);
Expand Down Expand Up @@ -4028,8 +4116,9 @@ bool CGameContext::ConWitch(IConsole::IResult *pResult, void *pUserData)
str_format(aBuf, sizeof(aBuf), "ConWitch() called");
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "conwitch", aBuf);

if (pSelf->GetZombieCount(PLAYERCLASS_WITCH) >= MAX_WITCHES) {
pSelf->SendChatTarget_Localization(ClientID, CHATCATEGORY_DEFAULT, _("All witches are already here"));
if (pSelf->GetIsOfClassCount(PLAYERCLASS_WITCH) >= MAX_WITCHES) {
str_format(aBuf, sizeof(aBuf), "All witches are already here", MAX_WITCHES);
pSelf->SendChatTarget(ClientID, aBuf);
return true;
}
if (pSelf->GetZombieCount() <= MIN_ZOMBIES) {
Expand Down Expand Up @@ -4329,19 +4418,13 @@ int CGameContext::GetTargetToKill()
}
void CGameContext::TargetKilled()
{
m_TargetToKill = -1;

int PlayerCounter = 0;
CPlayerIterator<PLAYERITER_INGAME> Iter(m_apPlayers);
while(Iter.Next())
PlayerCounter++;

m_TargetToKillCoolDown = Server()->TickSpeed()*(10 + 3*max(0, 16 - PlayerCounter));
m_TargetToKill = -1;
m_TargetToKillCoolDown = Server()->TickSpeed()*(10 + 3*max(0, 16 - GetActivePlayerCount() ));
}

void CGameContext::FlagCollected()
{
float t = (8-Server()->GetActivePlayerCount()) / 8.0f;
float t = (8-GetActivePlayerCount()) / 8.0f;
if (t < 0.0f)
t = 0.0f;

Expand Down Expand Up @@ -4416,17 +4499,17 @@ void CGameContext::List(int ClientID, const char* filter)
void CGameContext::AddSpectatorCID(int ClientID)
{
Server()->RemoveMapVotesForID(ClientID);
auto& vec = Server()->spectators_id;
auto& vec = spectators_id;
if(!(std::find(vec.begin(), vec.end(), ClientID) != vec.end()))
vec.push_back(ClientID);
}

void CGameContext::RemoveSpectatorCID(int ClientID) {
auto& vec = Server()->spectators_id;
auto& vec = spectators_id;
vec.erase(std::remove(vec.begin(), vec.end(), ClientID), vec.end());
}

bool CGameContext::IsSpectatorCID(int ClientID) {
auto& vec = Server()->spectators_id;
auto& vec = spectators_id;
return std::find(vec.begin(), vec.end(), ClientID) != vec.end();
}
Loading