From b18d218c988a80d1c73bcc11186b5c42ce704968 Mon Sep 17 00:00:00 2001 From: Choupom Date: Fri, 2 Jan 2015 18:26:11 +0100 Subject: [PATCH] refactored CFlag --- src/game/server/entities/flag.cpp | 27 ++++++++++---- src/game/server/entities/flag.h | 32 +++++++++++++---- src/game/server/gamemodes/ctf.cpp | 60 ++++++++++++++----------------- 3 files changed, 73 insertions(+), 46 deletions(-) diff --git a/src/game/server/entities/flag.cpp b/src/game/server/entities/flag.cpp index c66b01944..9d994f7ad 100644 --- a/src/game/server/entities/flag.cpp +++ b/src/game/server/entities/flag.cpp @@ -10,28 +10,43 @@ CFlag::CFlag(CGameWorld *pGameWorld, int Team, vec2 StandPos) : CEntity(pGameWorld, CGameWorld::ENTTYPE_FLAG, StandPos, ms_PhysSize) { m_Team = Team; - m_pCarryingCharacter = NULL; m_StandPos = StandPos; - m_GrabTick = 0; + + GameServer()->m_World.InsertEntity(this); Reset(); } void CFlag::Reset() { - m_pCarryingCharacter = NULL; - m_AtStand = 1; + m_pCarrier = 0; + m_AtStand = true; m_Pos = m_StandPos; m_Vel = vec2(0, 0); m_GrabTick = 0; } +void CFlag::Grab(CCharacter *pChar) +{ + m_AtStand = false; + m_pCarrier = pChar; + if(m_AtStand) + m_GrabTick = Server()->Tick(); +} + +void CFlag::Drop() +{ + m_pCarrier = 0; + m_Vel = vec2(0, 0); + m_DropTick = Server()->Tick(); +} + void CFlag::Tick() { - if(m_pCarryingCharacter) + if(m_pCarrier) { // update flag position - m_Pos = m_pCarryingCharacter->GetPos(); + m_Pos = m_pCarrier->GetPos(); } else { diff --git a/src/game/server/entities/flag.h b/src/game/server/entities/flag.h index 17c8cb7f5..7934fe469 100644 --- a/src/game/server/entities/flag.h +++ b/src/game/server/entities/flag.h @@ -7,23 +7,41 @@ class CFlag : public CEntity { -public: - static const int ms_PhysSize = 14; - CCharacter *m_pCarryingCharacter; - vec2 m_Vel; +private: + /* Identity */ + int m_Team; vec2 m_StandPos; - int m_Team; - int m_AtStand; - int m_DropTick; + /* State */ + bool m_AtStand; + CCharacter *m_pCarrier; + vec2 m_Vel; int m_GrabTick; + int m_DropTick; +public: + /* Constants */ + static int const ms_PhysSize = 14; + + /* Constructor */ CFlag(CGameWorld *pGameWorld, int Team, vec2 StandPos); + /* Getters */ + int GetTeam() const { return m_Team; } + bool IsAtStand() const { return m_AtStand; } + CCharacter *GetCarrier() const { return m_pCarrier; } + int GetGrabTick() const { return m_GrabTick; } + int GetDropTick() const { return m_DropTick; } + + /* CEntity functions */ virtual void Reset(); virtual void TickPaused(); virtual void Snap(int SnappingClient); virtual void Tick(); + + /* Functions */ + void Grab(class CCharacter *pChar); + void Drop(); }; #endif diff --git a/src/game/server/gamemodes/ctf.cpp b/src/game/server/gamemodes/ctf.cpp index fc96cca80..2c2c83610 100644 --- a/src/game/server/gamemodes/ctf.cpp +++ b/src/game/server/gamemodes/ctf.cpp @@ -29,7 +29,7 @@ bool CGameControllerCTF::CanBeMovedOnBalance(int ClientID) const for(int fi = 0; fi < 2; fi++) { CFlag *F = m_apFlags[fi]; - if(F && F->m_pCarryingCharacter == Character) + if(F && F->GetCarrier() == Character) return false; } } @@ -46,14 +46,12 @@ int CGameControllerCTF::OnCharacterDeath(CCharacter *pVictim, CPlayer *pKiller, for(int i = 0; i < 2; i++) { CFlag *F = m_apFlags[i]; - if(F && pKiller && pKiller->GetCharacter() && F->m_pCarryingCharacter == pKiller->GetCharacter()) + if(F && pKiller && pKiller->GetCharacter() && F->GetCarrier() == pKiller->GetCharacter()) HadFlag |= 2; - if(F && F->m_pCarryingCharacter == pVictim) + if(F && F->GetCarrier() == pVictim) { GameServer()->SendGameMsg(GAMEMSG_CTF_DROP, -1); - F->m_DropTick = Server()->Tick(); - F->m_pCarryingCharacter = 0; - F->m_Vel = vec2(0,0); + F->Drop(); if(pKiller && pKiller->GetTeam() != pVictim->GetPlayer()->GetTeam()) pKiller->m_Score++; @@ -84,7 +82,6 @@ bool CGameControllerCTF::OnEntity(int Index, vec2 Pos) CFlag *F = new CFlag(&GameServer()->m_World, Team, Pos); m_apFlags[Team] = F; - GameServer()->m_World.InsertEntity(F); return true; } @@ -122,14 +119,14 @@ void CGameControllerCTF::Snap(int SnappingClient) pGameDataFlag->m_FlagDropTickRed = 0; if(m_apFlags[TEAM_RED]) { - if(m_apFlags[TEAM_RED]->m_AtStand) + if(m_apFlags[TEAM_RED]->IsAtStand()) pGameDataFlag->m_FlagCarrierRed = FLAG_ATSTAND; - else if(m_apFlags[TEAM_RED]->m_pCarryingCharacter && m_apFlags[TEAM_RED]->m_pCarryingCharacter->GetPlayer()) - pGameDataFlag->m_FlagCarrierRed = m_apFlags[TEAM_RED]->m_pCarryingCharacter->GetPlayer()->GetCID(); + else if(m_apFlags[TEAM_RED]->GetCarrier() && m_apFlags[TEAM_RED]->GetCarrier()->GetPlayer()) + pGameDataFlag->m_FlagCarrierRed = m_apFlags[TEAM_RED]->GetCarrier()->GetPlayer()->GetCID(); else { pGameDataFlag->m_FlagCarrierRed = FLAG_TAKEN; - pGameDataFlag->m_FlagDropTickRed = m_apFlags[TEAM_RED]->m_DropTick; + pGameDataFlag->m_FlagDropTickRed = m_apFlags[TEAM_RED]->GetDropTick(); } } else @@ -137,14 +134,14 @@ void CGameControllerCTF::Snap(int SnappingClient) pGameDataFlag->m_FlagDropTickBlue = 0; if(m_apFlags[TEAM_BLUE]) { - if(m_apFlags[TEAM_BLUE]->m_AtStand) + if(m_apFlags[TEAM_BLUE]->IsAtStand()) pGameDataFlag->m_FlagCarrierBlue = FLAG_ATSTAND; - else if(m_apFlags[TEAM_BLUE]->m_pCarryingCharacter && m_apFlags[TEAM_BLUE]->m_pCarryingCharacter->GetPlayer()) - pGameDataFlag->m_FlagCarrierBlue = m_apFlags[TEAM_BLUE]->m_pCarryingCharacter->GetPlayer()->GetCID(); + else if(m_apFlags[TEAM_BLUE]->GetCarrier() && m_apFlags[TEAM_BLUE]->GetCarrier()->GetPlayer()) + pGameDataFlag->m_FlagCarrierBlue = m_apFlags[TEAM_BLUE]->GetCarrier()->GetPlayer()->GetCID(); else { pGameDataFlag->m_FlagCarrierBlue = FLAG_TAKEN; - pGameDataFlag->m_FlagDropTickBlue = m_apFlags[TEAM_BLUE]->m_DropTick; + pGameDataFlag->m_FlagDropTickBlue = m_apFlags[TEAM_BLUE]->GetDropTick(); } } else @@ -166,23 +163,23 @@ void CGameControllerCTF::Tick() continue; // - if(F->m_pCarryingCharacter) + if(F->GetCarrier()) { - if(m_apFlags[fi^1] && m_apFlags[fi^1]->m_AtStand) + if(m_apFlags[fi^1] && m_apFlags[fi^1]->IsAtStand()) { if(distance(F->GetPos(), m_apFlags[fi^1]->GetPos()) < CFlag::ms_PhysSize + CCharacter::ms_PhysSize) { // CAPTURE! \o/ m_aTeamscore[fi^1] += 100; - F->m_pCarryingCharacter->GetPlayer()->m_Score += 5; + F->GetCarrier()->GetPlayer()->m_Score += 5; char aBuf[64]; str_format(aBuf, sizeof(aBuf), "flag_capture player='%d:%s'", - F->m_pCarryingCharacter->GetPlayer()->GetCID(), - Server()->ClientName(F->m_pCarryingCharacter->GetPlayer()->GetCID())); + F->GetCarrier()->GetPlayer()->GetCID(), + Server()->ClientName(F->GetCarrier()->GetPlayer()->GetCID())); GameServer()->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "game", aBuf); - GameServer()->SendGameMsg(GAMEMSG_CTF_CAPTURE, fi, F->m_pCarryingCharacter->GetPlayer()->GetCID(), Server()->Tick()-F->m_GrabTick, -1); + GameServer()->SendGameMsg(GAMEMSG_CTF_CAPTURE, fi, F->GetCarrier()->GetPlayer()->GetCID(), Server()->Tick()-F->GetGrabTick(), -1); for(int i = 0; i < 2; i++) m_apFlags[i]->Reset(); } @@ -197,10 +194,10 @@ void CGameControllerCTF::Tick() if(!apCloseCCharacters[i]->IsAlive() || apCloseCCharacters[i]->GetPlayer()->GetTeam() == TEAM_SPECTATORS || GameServer()->Collision()->IntersectLine(F->GetPos(), apCloseCCharacters[i]->GetPos(), NULL, NULL)) continue; - if(apCloseCCharacters[i]->GetPlayer()->GetTeam() == F->m_Team) + if(apCloseCCharacters[i]->GetPlayer()->GetTeam() == F->GetTeam()) { // return the flag - if(!F->m_AtStand) + if(!F->IsAtStand()) { CCharacter *pChr = apCloseCCharacters[i]; pChr->GetPlayer()->m_Score += 1; @@ -217,20 +214,17 @@ void CGameControllerCTF::Tick() else { // take the flag - if(F->m_AtStand) - { - m_aTeamscore[fi^1]++; - F->m_GrabTick = Server()->Tick(); - } + F->Grab(apCloseCCharacters[i]); - F->m_AtStand = 0; - F->m_pCarryingCharacter = apCloseCCharacters[i]; - F->m_pCarryingCharacter->GetPlayer()->m_Score += 1; + if(F->IsAtStand()) + m_aTeamscore[fi^1]++; + + F->GetCarrier()->GetPlayer()->m_Score += 1; char aBuf[256]; str_format(aBuf, sizeof(aBuf), "flag_grab player='%d:%s'", - F->m_pCarryingCharacter->GetPlayer()->GetCID(), - Server()->ClientName(F->m_pCarryingCharacter->GetPlayer()->GetCID())); + F->GetCarrier()->GetPlayer()->GetCID(), + Server()->ClientName(F->GetCarrier()->GetPlayer()->GetCID())); GameServer()->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "game", aBuf); GameServer()->SendGameMsg(GAMEMSG_CTF_GRAB, fi, -1); break;