Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* (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. */
- /* Modified by Blindhero for 64 Client System */
- #include <new>
- #include <engine/shared/config.h>
- #include "player.h"
- MACRO_ALLOC_POOL_ID_IMPL(CPlayer, MAX_CLIENTS)
- IServer *CPlayer::Server() const { return m_pGameServer->Server(); }
- CPlayer::CPlayer(CGameContext *pGameServer, int ClientID, int Team)
- {
- m_pGameServer = pGameServer;
- m_RespawnTick = Server()->Tick();
- m_DieTick = Server()->Tick();
- m_ScoreStartTick = Server()->Tick();
- m_pCharacter = 0;
- m_ClientID = ClientID;
- m_Team = GameServer()->m_pController->ClampTeam(Team);
- m_SpectatorID = SPEC_FREEVIEW;
- m_LastActionTick = Server()->Tick();
- m_TeamChangeTick = Server()->Tick();
- //zCatch
- m_CaughtBy = ZCATCH_NOT_CAUGHT;
- m_SpecExplicit = false;
- m_Kills = 0;
- m_Deaths = 0;
- m_LastKillTry = Server()->Tick();
- m_TicksSpec = 0;
- m_TicksIngame = 0;
- m_ChatTicks = 0;
- // zCatch/TeeVi
- m_ZCatchVictims = NULL;
- m_zCatchNumVictims = 0;
- m_zCatchNumKillsInARow = 0;
- // bot detection
- m_IsAimBot = 0;
- m_AimBotIndex = 0;
- m_AimBotRange = 0;
- m_AimBotLastDetection = 0;
- m_AimBotTargetSpeed = .0;
- m_CurrentTarget.x = 0;
- m_CurrentTarget.y = 0;
- m_LastTarget.x = 0;
- m_LastTarget.y = 0;
- //64 System
- int* idMap = Server()->GetIdMap(ClientID);
- for (int i = 1;i < VANILLA_MAX_CLIENTS;i++)
- {
- //jxsl13 idMap[i] = -1;
- idMap[i] = 0;
- }
- idMap[0] = ClientID;
- zTeam = 0;
- m_pCommand = new CCommand(this);
- replyID = -1;
- m_TeamChangeDelay = 0;
- }
- CPlayer::~CPlayer()
- {
- while(m_ZCatchVictims != NULL)
- {
- CZCatchVictim *tmp = m_ZCatchVictims;
- m_ZCatchVictims = tmp->prev;
- delete tmp;
- }
- delete m_pCharacter;
- m_pCharacter = 0;
- }
- void CPlayer::Tick()
- {
- #ifdef CONF_DEBUG
- if(!g_Config.m_DbgDummies || m_ClientID < MAX_CLIENTS-g_Config.m_DbgDummies)
- #endif
- if(!Server()->ClientIngame(m_ClientID))
- return;
- Server()->SetClientScore(m_ClientID, m_Score);
- /* begin zCatch*/
- if(m_Team == TEAM_SPECTATORS)
- m_TicksSpec++;
- else
- m_TicksIngame++;
- if(m_ChatTicks > 0)
- m_ChatTicks--;
- if((g_Config.m_SvAnticamper == 2 && g_Config.m_SvMode == 1) || (g_Config.m_SvAnticamper == 1))
- Anticamper();
- /* end zCatch*/
- // do latency stuff
- {
- IServer::CClientInfo Info;
- //dbg_msg("PlayerTick", "ClientID: %d", m_ClientID);
- if(Server()->GetClientInfo(m_ClientID, &Info))
- {
- 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);
- }
- // each second
- if(Server()->Tick()%Server()->TickSpeed() == 0)
- {
- 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;
- }
- }
- if(!GameServer()->m_World.m_Paused)
- {
- if(!m_pCharacter && m_Team == TEAM_SPECTATORS && m_SpectatorID == SPEC_FREEVIEW)
- m_ViewPos -= vec2(clamp(m_ViewPos.x-m_LatestActivity.m_TargetX, -500.0f, 500.0f), clamp(m_ViewPos.y-m_LatestActivity.m_TargetY, -400.0f, 400.0f));
- if(!m_pCharacter && m_DieTick+Server()->TickSpeed()*3 <= Server()->Tick())
- m_Spawning = true;
- if(m_pCharacter)
- {
- if(m_pCharacter->IsAlive())
- {
- m_ViewPos = m_pCharacter->m_Pos;
- }
- else
- {
- delete m_pCharacter;
- m_pCharacter = 0;
- }
- }
- else if(m_Spawning && m_RespawnTick <= Server()->Tick())
- TryRespawn();
- }
- else
- {
- ++m_RespawnTick;
- ++m_DieTick;
- ++m_ScoreStartTick;
- ++m_LastActionTick;
- ++m_TeamChangeTick;
- }
- // bot detection
- m_LastTarget = m_CurrentTarget;
- m_CurrentTarget.x = m_LatestActivity.m_TargetX;
- m_CurrentTarget.y = m_LatestActivity.m_TargetY;
- m_AimBotTargetSpeed = abs(distance(m_CurrentTarget, m_LastTarget));
- }
- 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;
- }
- void CPlayer::Snap(int SnappingClient)
- {
- #ifdef CONF_DEBUG
- if(!g_Config.m_DbgDummies || m_ClientID < MAX_CLIENTS-g_Config.m_DbgDummies)
- #endif
- if(!Server()->ClientIngame(m_ClientID))
- return;
- int id = m_ClientID;
- if (!Server()->Translate(id, SnappingClient)) return;
- CNetObj_ClientInfo *pClientInfo = static_cast<CNetObj_ClientInfo *>(Server()->SnapNewItem(NETOBJTYPE_CLIENTINFO, id, sizeof(CNetObj_ClientInfo)));
- if(!pClientInfo)
- return;
- pClientInfo->m_Country = Server()->ClientCountry(m_ClientID);
- StrToInts(&pClientInfo->m_Skin0, 6, m_TeeInfos.m_SkinName);
- if(g_Config.m_SvUseTeamColors) {
- pClientInfo->m_UseCustomColor = 1;
- if(zTeam == 0) {
- pClientInfo->m_ColorBody = 16777215;
- } else {
- int TeamColor = (255/(g_Config.m_SvMaxClients / GameServer()->max_teamsize) * zTeam)%256;
- pClientInfo->m_ColorBody = TeamColor * 0x010000 + 0xff00;
- }
- pClientInfo->m_ColorFeet = m_TeeInfos.m_ColorFeet;
- } else {
- pClientInfo->m_UseCustomColor = m_TeeInfos.m_UseCustomColor;
- pClientInfo->m_ColorBody = m_TeeInfos.m_ColorBody;
- pClientInfo->m_ColorFeet = m_TeeInfos.m_ColorFeet;
- }
- char clientname[MAX_NAME_LENGTH], clanname[MAX_CLAN_LENGTH];;
- if(g_Config.m_SvShowTeam && zTeam > 0)
- str_format(clientname, sizeof(clientname), "[%d]%s", zTeam, Server()->ClientName(m_ClientID));
- else
- str_copy(clientname, Server()->ClientName(m_ClientID), sizeof(clientname));
- StrToInts(&pClientInfo->m_Name0, 4, clientname);
- if(zTeam > 0 && GameServer()->m_zTeamNames[zTeam] && str_length(GameServer()->m_zTeamNames[zTeam]) > 0)
- str_copy(clanname, GameServer()->m_zTeamNames[zTeam], sizeof(clanname));
- else
- str_copy(clanname, Server()->ClientClan(m_ClientID), sizeof(clanname));
- StrToInts(&pClientInfo->m_Clan0, 3, clanname);
- CNetObj_PlayerInfo *pPlayerInfo = static_cast<CNetObj_PlayerInfo *>(Server()->SnapNewItem(NETOBJTYPE_PLAYERINFO, id, 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 = id;
- pPlayerInfo->m_Score = m_Score;
- pPlayerInfo->m_Team = m_Team;
- 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::FakeSnap(int SnappingClient)
- {
- IServer::CClientInfo info;
- //dbg_msg("fakesnap", "SnappingClient: %d", SnappingClient);
- Server()->GetClientInfo(SnappingClient, &info);
- if (info.m_CustClt)
- return;
- int id = VANILLA_MAX_CLIENTS - 1;
- CNetObj_ClientInfo *pClientInfo = static_cast<CNetObj_ClientInfo *>(Server()->SnapNewItem(NETOBJTYPE_CLIENTINFO, id, sizeof(CNetObj_ClientInfo)));
- if(!pClientInfo)
- return;
- StrToInts(&pClientInfo->m_Name0, 4, " ");
- StrToInts(&pClientInfo->m_Clan0, 3, Server()->ClientClan(m_ClientID));
- StrToInts(&pClientInfo->m_Skin0, 6, m_TeeInfos.m_SkinName);
- }
- void CPlayer::OnDisconnect(const char *pReason)
- {
- 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_ADDINFO, "game", aBuf);
- }
- }
- void CPlayer::OnPredictedInput(CNetObj_PlayerInput *NewInput)
- {
- // skip the input if chat is active
- if((m_PlayerFlags&PLAYERFLAG_CHATTING) && (NewInput->m_PlayerFlags&PLAYERFLAG_CHATTING))
- return;
- if(m_pCharacter && m_pCharacter->m_FreezeTicks)
- return;
- if(m_pCharacter)
- m_pCharacter->OnPredictedInput(NewInput);
- }
- void CPlayer::OnDirectInput(CNetObj_PlayerInput *NewInput)
- {
- if(NewInput->m_PlayerFlags&PLAYERFLAG_CHATTING)
- {
- // skip the input if chat is active
- if(m_PlayerFlags&PLAYERFLAG_CHATTING)
- return;
- // reset input
- if(m_pCharacter)
- m_pCharacter->ResetInput();
- m_PlayerFlags = NewInput->m_PlayerFlags;
- return;
- }
- m_PlayerFlags = NewInput->m_PlayerFlags;
- if(m_pCharacter)
- m_pCharacter->OnDirectInput(NewInput);
- if(m_pCharacter && m_pCharacter->m_FreezeTicks)
- return;
- if(!m_pCharacter && m_Team != TEAM_SPECTATORS && (NewInput->m_Fire&1))
- m_Spawning = true;
- // 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();
- }
- }
- CCharacter *CPlayer::GetCharacter()
- {
- if(m_pCharacter && m_pCharacter->IsAlive())
- return m_pCharacter;
- return 0;
- }
- void CPlayer::KillCharacter(int Weapon)
- {
- if(m_pCharacter)
- {
- m_pCharacter->Die(m_ClientID, Weapon);
- delete m_pCharacter;
- m_pCharacter = 0;
- }
- }
- void CPlayer::Respawn()
- {
- if(m_Team != TEAM_SPECTATORS)
- m_Spawning = true;
- }
- void CPlayer::SetTeam(int Team, bool DoChatMsg)
- {
- // clamp the team
- Team = GameServer()->m_pController->ClampTeam(Team);
- if(m_Team == Team)
- return;
- char aBuf[512];
- if(DoChatMsg)
- {
- 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);
- }
- KillCharacter();
- m_Team = Team;
- m_LastActionTick = Server()->Tick();
- m_SpectatorID = SPEC_FREEVIEW;
- // 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);
- 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;
- }
- m_SpecExplicit = true;
- }
- else
- m_SpecExplicit = false;
- }
- void CPlayer::SetTeamDirect(int Team)
- {
- m_Team = Team;
- }
- void CPlayer::TryRespawn()
- {
- vec2 SpawnPos;
- if(!GameServer()->m_pController->CanSpawn(m_Team, &SpawnPos))
- return;
- m_Spawning = false;
- m_pCharacter = new(m_ClientID) CCharacter(&GameServer()->m_World);
- m_pCharacter->Spawn(this, SpawnPos);
- GameServer()->CreatePlayerSpawn(SpawnPos);
- }
- int CPlayer::Anticamper()
- {
- if(GameServer()->m_World.m_Paused || !m_pCharacter || m_Team == TEAM_SPECTATORS || m_pCharacter->m_FreezeTicks)
- {
- m_CampTick = -1;
- m_SentCampMsg = false;
- return 0;
- }
- int AnticamperTime = g_Config.m_SvAnticamperTime;
- int AnticamperRange = g_Config.m_SvAnticamperRange;
- if(m_CampTick == -1)
- {
- m_CampPos = m_pCharacter->m_Pos;
- m_CampTick = Server()->Tick() + Server()->TickSpeed()*AnticamperTime;
- }
- // Check if the player is moving
- if((m_CampPos.x - m_pCharacter->m_Pos.x >= (float)AnticamperRange || m_CampPos.x - m_pCharacter->m_Pos.x <= -(float)AnticamperRange)
- || (m_CampPos.y - m_pCharacter->m_Pos.y >= (float)AnticamperRange || m_CampPos.y - m_pCharacter->m_Pos.y <= -(float)AnticamperRange))
- {
- m_CampTick = -1;
- }
- // Send warning to the player
- if(m_CampTick <= Server()->Tick() + Server()->TickSpeed() * AnticamperTime/2 && m_CampTick != -1 && !m_SentCampMsg)
- {
- GameServer()->SendBroadcast("ANTICAMPER: Move or die", m_ClientID);
- m_SentCampMsg = true;
- }
- // Kill him
- if((m_CampTick <= Server()->Tick()) && (m_CampTick > 0))
- {
- if(g_Config.m_SvAnticamperFreeze)
- {
- m_pCharacter->Freeze(Server()->TickSpeed()*g_Config.m_SvAnticamperFreeze);
- GameServer()->SendBroadcast("You have been freezed due camping", m_ClientID);
- }
- else
- m_pCharacter->Die(m_ClientID, WEAPON_GAME);
- m_CampTick = -1;
- m_SentCampMsg = false;
- return 1;
- }
- return 0;
- }
- // catch another player
- void CPlayer::AddZCatchVictim(int ClientID, int reason)
- {
- CPlayer *victim = GameServer()->m_apPlayers[ClientID];
- if(victim)
- {
- // add to list of victims
- CZCatchVictim *v = new CZCatchVictim;
- v->ClientID = ClientID;
- v->Reason = reason;
- v->prev = m_ZCatchVictims;
- m_ZCatchVictims = v;
- ++m_zCatchNumVictims;
- // set victim's status
- victim->m_CaughtBy = m_ClientID;
- victim->m_SpecExplicit = false;
- victim->m_zCatchJoinSpecWhenReleased = false;
- victim->SetTeamDirect(TEAM_SPECTATORS);
- victim->m_SpectatorID = m_ClientID;
- }
- }
- // release one or more of the victims
- void CPlayer::ReleaseZCatchVictim(int ClientID, int limit)
- {
- CZCatchVictim **v = &m_ZCatchVictims;
- CZCatchVictim *tmp;
- CPlayer *victim;
- int count = 0;
- while(*v != NULL)
- {
- if(ClientID == ZCATCH_RELEASE_ALL || (*v)->ClientID == ClientID)
- {
- victim = GameServer()->m_apPlayers[(*v)->ClientID];
- if(victim)
- {
- victim->m_CaughtBy = ZCATCH_NOT_CAUGHT;
- victim->SetTeamDirect(GameServer()->m_pController->ClampTeam(1));
- victim->m_SpectatorID = SPEC_FREEVIEW;
- // SetTeam after SetTeamDirect, otherwise it would skip the message for joining the spectators
- if(victim->m_zCatchJoinSpecWhenReleased)
- victim->SetTeam(GameServer()->m_pController->ClampTeam(TEAM_SPECTATORS));
- }
- // delete from list
- tmp = (*v)->prev;
- delete *v;
- *v = tmp;
- --m_zCatchNumVictims;
- if (limit && ++count >= limit)
- return;
- }
- else
- v = &(*v)->prev;
- }
- }
- bool CPlayer::CanAttack(int ID) {
- if(zTeam == 0)
- return true;
- else
- if(GameServer()->m_apPlayers[ID] && GameServer()->m_apPlayers[ID]->zTeam == zTeam)
- return false;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement