ddnet/src/game/server/player.cpp

267 lines
7.6 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <new>
2011-01-18 18:09:53 +00:00
#include <engine/shared/config.h>
2010-05-29 07:25:38 +00:00
#include "player.h"
2010-05-29 07:25:38 +00:00
MACRO_ALLOC_POOL_ID_IMPL(CPlayer, MAX_CLIENTS)
2010-05-29 07:25:38 +00:00
IServer *CPlayer::Server() const { return m_pGameServer->Server(); }
CPlayer::CPlayer(CGameContext *pGameServer, int ClientID, int Team)
{
2010-05-29 07:25:38 +00:00
m_pGameServer = pGameServer;
m_RespawnTick = Server()->Tick();
m_DieTick = Server()->Tick();
m_ScoreStartTick = Server()->Tick();
2011-04-19 08:42:48 +00:00
m_pCharacter = 0;
m_ClientID = ClientID;
2010-05-29 07:25:38 +00:00
m_Team = GameServer()->m_pController->ClampTeam(Team);
m_SpectatorID = SPEC_FREEVIEW;
m_LastActionTick = Server()->Tick();
}
2010-05-29 07:25:38 +00:00
CPlayer::~CPlayer()
{
2011-04-19 08:42:48 +00:00
delete m_pCharacter;
m_pCharacter = 0;
}
2010-05-29 07:25:38 +00:00
void CPlayer::Tick()
{
2011-01-18 18:09:53 +00:00
#ifdef CONF_DEBUG
if(!g_Config.m_DbgDummies || m_ClientID < MAX_CLIENTS-g_Config.m_DbgDummies)
#endif
if(!Server()->ClientIngame(m_ClientID))
return;
2010-05-29 07:25:38 +00:00
Server()->SetClientScore(m_ClientID, m_Score);
// do latency stuff
{
2010-05-29 07:25:38 +00:00
IServer::CClientInfo Info;
if(Server()->GetClientInfo(m_ClientID, &Info))
{
2010-05-29 07:25:38 +00:00
m_Latency.m_Accum += Info.m_Latency;
m_Latency.m_AccumMax = max(m_Latency.m_AccumMax, Info.m_Latency);
m_Latency.m_AccumMin = min(m_Latency.m_AccumMin, Info.m_Latency);
}
2010-05-29 07:25:38 +00:00
// each second
if(Server()->Tick()%Server()->TickSpeed() == 0)
{
2010-05-29 07:25:38 +00:00
m_Latency.m_Avg = m_Latency.m_Accum/Server()->TickSpeed();
m_Latency.m_Max = m_Latency.m_AccumMax;
m_Latency.m_Min = m_Latency.m_AccumMin;
m_Latency.m_Accum = 0;
m_Latency.m_AccumMin = 1000;
m_Latency.m_AccumMax = 0;
}
}
2011-04-19 08:42:48 +00:00
if(!m_pCharacter && m_DieTick+Server()->TickSpeed()*3 <= Server()->Tick())
2010-05-29 07:25:38 +00:00
m_Spawning = true;
2008-09-24 14:54:51 +00:00
2011-04-19 08:42:48 +00:00
if(m_pCharacter)
2008-09-24 14:54:51 +00:00
{
2011-04-19 08:42:48 +00:00
if(m_pCharacter->IsAlive())
2008-09-24 14:54:51 +00:00
{
2011-04-19 08:42:48 +00:00
m_ViewPos = m_pCharacter->m_Pos;
2008-09-24 14:54:51 +00:00
}
else
{
2011-04-19 08:42:48 +00:00
delete m_pCharacter;
m_pCharacter = 0;
2008-09-24 14:54:51 +00:00
}
}
2010-05-29 07:25:38 +00:00
else if(m_Spawning && m_RespawnTick <= Server()->Tick())
TryRespawn();
}
void CPlayer::PostTick()
{
// update latency value
if(m_PlayerFlags&PLAYERFLAG_SCOREBOARD)
{
for(int i = 0; i < MAX_CLIENTS; ++i)
{
if(GameServer()->m_apPlayers[i] && GameServer()->m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS)
m_aActLatency[i] = GameServer()->m_apPlayers[i]->m_Latency.m_Min;
}
}
// update view pos for spectators
if(m_Team == TEAM_SPECTATORS && m_SpectatorID != SPEC_FREEVIEW && GameServer()->m_apPlayers[m_SpectatorID])
m_ViewPos = GameServer()->m_apPlayers[m_SpectatorID]->m_ViewPos;
}
2010-05-29 07:25:38 +00:00
void CPlayer::Snap(int SnappingClient)
{
2011-01-18 18:09:53 +00:00
#ifdef CONF_DEBUG
if(!g_Config.m_DbgDummies || m_ClientID < MAX_CLIENTS-g_Config.m_DbgDummies)
#endif
if(!Server()->ClientIngame(m_ClientID))
return;
CNetObj_ClientInfo *pClientInfo = static_cast<CNetObj_ClientInfo *>(Server()->SnapNewItem(NETOBJTYPE_CLIENTINFO, m_ClientID, sizeof(CNetObj_ClientInfo)));
if(!pClientInfo)
return;
StrToInts(&pClientInfo->m_Name0, 4, Server()->ClientName(m_ClientID));
StrToInts(&pClientInfo->m_Clan0, 3, Server()->ClientClan(m_ClientID));
pClientInfo->m_Country = Server()->ClientCountry(m_ClientID);
StrToInts(&pClientInfo->m_Skin0, 6, m_TeeInfos.m_SkinName);
pClientInfo->m_UseCustomColor = m_TeeInfos.m_UseCustomColor;
pClientInfo->m_ColorBody = m_TeeInfos.m_ColorBody;
pClientInfo->m_ColorFeet = m_TeeInfos.m_ColorFeet;
CNetObj_PlayerInfo *pPlayerInfo = static_cast<CNetObj_PlayerInfo *>(Server()->SnapNewItem(NETOBJTYPE_PLAYERINFO, m_ClientID, sizeof(CNetObj_PlayerInfo)));
if(!pPlayerInfo)
return;
pPlayerInfo->m_Latency = SnappingClient == -1 ? m_Latency.m_Min : GameServer()->m_apPlayers[SnappingClient]->m_aActLatency[m_ClientID];
pPlayerInfo->m_Local = 0;
pPlayerInfo->m_ClientID = m_ClientID;
pPlayerInfo->m_Score = m_Score;
pPlayerInfo->m_Team = m_Team;
2010-05-29 07:25:38 +00:00
if(m_ClientID == SnappingClient)
pPlayerInfo->m_Local = 1;
if(m_ClientID == SnappingClient && m_Team == TEAM_SPECTATORS)
{
CNetObj_SpectatorInfo *pSpectatorInfo = static_cast<CNetObj_SpectatorInfo *>(Server()->SnapNewItem(NETOBJTYPE_SPECTATORINFO, m_ClientID, sizeof(CNetObj_SpectatorInfo)));
if(!pSpectatorInfo)
return;
pSpectatorInfo->m_SpectatorID = m_SpectatorID;
pSpectatorInfo->m_X = m_ViewPos.x;
pSpectatorInfo->m_Y = m_ViewPos.y;
}
}
void CPlayer::OnDisconnect(const char *pReason)
{
2010-05-29 07:25:38 +00:00
KillCharacter();
if(Server()->ClientIngame(m_ClientID))
{
char aBuf[512];
if(pReason && *pReason)
str_format(aBuf, sizeof(aBuf), "'%s' has left the game (%s)", Server()->ClientName(m_ClientID), pReason);
else
str_format(aBuf, sizeof(aBuf), "'%s' has left the game", Server()->ClientName(m_ClientID));
GameServer()->SendChat(-1, CGameContext::CHAT_ALL, aBuf);
str_format(aBuf, sizeof(aBuf), "leave player='%d:%s'", m_ClientID, Server()->ClientName(m_ClientID));
GameServer()->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "game", aBuf);
2010-05-29 07:25:38 +00:00
}
}
2010-05-29 07:25:38 +00:00
void CPlayer::OnPredictedInput(CNetObj_PlayerInput *NewInput)
{
// skip the input if chat is active
if((m_PlayerFlags&PLAYERFLAG_CHATTING) && (NewInput->m_PlayerFlags&PLAYERFLAG_CHATTING))
return;
2011-04-19 08:42:48 +00:00
if(m_pCharacter)
m_pCharacter->OnPredictedInput(NewInput);
}
2010-05-29 07:25:38 +00:00
void CPlayer::OnDirectInput(CNetObj_PlayerInput *NewInput)
{
// skip the input if chat is active
if((m_PlayerFlags&PLAYERFLAG_CHATTING) && (NewInput->m_PlayerFlags&PLAYERFLAG_CHATTING))
return;
m_PlayerFlags = NewInput->m_PlayerFlags;
2011-04-19 08:42:48 +00:00
if(m_pCharacter)
m_pCharacter->OnDirectInput(NewInput);
2011-04-19 08:42:48 +00:00
if(!m_pCharacter && m_Team != TEAM_SPECTATORS && (NewInput->m_Fire&1))
2010-05-29 07:25:38 +00:00
m_Spawning = true;
2011-04-19 08:42:48 +00:00
if(!m_pCharacter && m_Team == TEAM_SPECTATORS && m_SpectatorID == SPEC_FREEVIEW)
2010-05-29 07:25:38 +00:00
m_ViewPos = vec2(NewInput->m_TargetX, NewInput->m_TargetY);
// check for activity
if(NewInput->m_Direction || m_LatestActivity.m_TargetX != NewInput->m_TargetX ||
m_LatestActivity.m_TargetY != NewInput->m_TargetY || NewInput->m_Jump ||
NewInput->m_Fire&1 || NewInput->m_Hook)
{
m_LatestActivity.m_TargetX = NewInput->m_TargetX;
m_LatestActivity.m_TargetY = NewInput->m_TargetY;
m_LastActionTick = Server()->Tick();
}
}
2010-05-29 07:25:38 +00:00
CCharacter *CPlayer::GetCharacter()
{
2011-04-19 08:42:48 +00:00
if(m_pCharacter && m_pCharacter->IsAlive())
return m_pCharacter;
return 0;
}
2010-05-29 07:25:38 +00:00
void CPlayer::KillCharacter(int Weapon)
{
2011-04-19 08:42:48 +00:00
if(m_pCharacter)
{
2011-04-19 08:42:48 +00:00
m_pCharacter->Die(m_ClientID, Weapon);
delete m_pCharacter;
m_pCharacter = 0;
}
}
2010-05-29 07:25:38 +00:00
void CPlayer::Respawn()
{
2011-01-03 11:50:38 +00:00
if(m_Team != TEAM_SPECTATORS)
2010-05-29 07:25:38 +00:00
m_Spawning = true;
}
2010-05-29 07:25:38 +00:00
void CPlayer::SetTeam(int Team)
{
// clamp the team
2010-05-29 07:25:38 +00:00
Team = GameServer()->m_pController->ClampTeam(Team);
if(m_Team == Team)
return;
char aBuf[512];
str_format(aBuf, sizeof(aBuf), "'%s' joined the %s", Server()->ClientName(m_ClientID), GameServer()->m_pController->GetTeamName(Team));
GameServer()->SendChat(-1, CGameContext::CHAT_ALL, aBuf);
2010-05-29 07:25:38 +00:00
KillCharacter();
2010-05-29 07:25:38 +00:00
m_Team = Team;
m_LastActionTick = Server()->Tick();
2010-05-29 07:25:38 +00:00
// we got to wait 0.5 secs before respawning
m_RespawnTick = Server()->Tick()+Server()->TickSpeed()/2;
str_format(aBuf, sizeof(aBuf), "team_join player='%d:%s' m_Team=%d", m_ClientID, Server()->ClientName(m_ClientID), m_Team);
GameServer()->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "game", aBuf);
2010-05-29 07:25:38 +00:00
GameServer()->m_pController->OnPlayerInfoChange(GameServer()->m_apPlayers[m_ClientID]);
if(Team == TEAM_SPECTATORS)
{
// update spectator modes
for(int i = 0; i < MAX_CLIENTS; ++i)
{
if(GameServer()->m_apPlayers[i] && GameServer()->m_apPlayers[i]->m_SpectatorID == m_ClientID)
GameServer()->m_apPlayers[i]->m_SpectatorID = SPEC_FREEVIEW;
}
}
}
2010-05-29 07:25:38 +00:00
void CPlayer::TryRespawn()
{
vec2 SpawnPos;
if(!GameServer()->m_pController->CanSpawn(m_Team, &SpawnPos))
return;
m_Spawning = false;
2011-04-19 08:42:48 +00:00
m_pCharacter = new(m_ClientID) CCharacter(&GameServer()->m_World);
m_pCharacter->Spawn(this, SpawnPos);
GameServer()->CreatePlayerSpawn(SpawnPos);
}