From f321b5d224cb4a6d562cfb32850bf752ddd69f61 Mon Sep 17 00:00:00 2001 From: andrew Date: Sun, 19 Jan 2014 16:02:37 +0200 Subject: [PATCH] Scoreboard improvements --- src/Entities/Player.cpp | 8 +-- src/Scoreboard.cpp | 79 +++++++++------------------ src/Scoreboard.h | 116 +++++++++++++++++++--------------------- 3 files changed, 83 insertions(+), 120 deletions(-) diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 4f3c6138b..d2fdba909 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -798,7 +798,7 @@ void cPlayer::DoTakeDamage(TakeDamageInfo & a_TDI) if ((m_Team != NULL) && (m_Team == Attacker->m_Team)) { - if (!m_Team->GetFriendlyFire()) + if (!m_Team->AllowsFriendlyFire()) { // Friendly fire is disabled return; @@ -868,7 +868,7 @@ void cPlayer::KilledBy(cEntity * a_Killer) cScoreboard* Scoreboard = m_World->GetScoreBoard(); // Update scoreboard objectives - Scoreboard->ForEachObjectiveWith(E_OBJECTIVE_DEATH_COUNT, IncrementCounter); + Scoreboard->ForEachObjectiveWith(cObjective::E_TYPE_DEATH_COUNT, IncrementCounter); } @@ -953,14 +953,14 @@ void cPlayer::SetTeam(cTeam* a_Team) { if (m_Team) { - m_Team->RemovePlayer(this); + m_Team->RemovePlayer(GetName()); } m_Team = a_Team; if (m_Team) { - m_Team->AddPlayer(this); + m_Team->AddPlayer(GetName()); } } diff --git a/src/Scoreboard.cpp b/src/Scoreboard.cpp index 1d2711ca0..539316356 100644 --- a/src/Scoreboard.cpp +++ b/src/Scoreboard.cpp @@ -11,14 +11,14 @@ -cObjective::cObjective(eObjectiveType a_Type) : m_Type(a_Type) +cObjective::cObjective(cObjective::eType a_Type) : m_Type(a_Type) {} -void cObjective::SetDisplaySlot(eDisplaySlot a_Display) +void cObjective::SetDisplaySlot(cObjective::eDisplaySlot a_Display) { m_Display = a_Display; } @@ -102,8 +102,8 @@ cObjective::Score cObjective::SubScore(const AString & a_Name, cObjective::Score cTeam::cTeam(const AString & a_Name, const AString & a_DisplayName, const AString & a_Prefix, const AString & a_Suffix) - : m_FriendlyFire(true) - , m_SeeFriendlyInvisible(false) + : m_AllowsFriendlyFire(true) + , m_CanSeeFriendlyInvisible(false) , m_Name(a_Name) , m_DisplayName(a_DisplayName) , m_Prefix(a_Prefix) @@ -114,18 +114,18 @@ cTeam::cTeam(const AString & a_Name, const AString & a_DisplayName, -bool cTeam::AddPlayer(cPlayer * a_Player) +bool cTeam::AddPlayer(const AString & a_Name) { - return m_Players.insert(a_Player).second; + return m_Players.insert(a_Name).second; } -bool cTeam::RemovePlayer(cPlayer * a_Player) +bool cTeam::RemovePlayer(const AString & a_Name) { - return m_Players.erase(a_Player) > 0; + return m_Players.erase(a_Name) > 0; } @@ -149,38 +149,13 @@ unsigned int cTeam::GetNumPlayers(void) const -cScoreboard::~cScoreboard() +cObjective* cScoreboard::RegisterObjective(const AString & a_Name, cObjective::eType a_Type) { - for (ObjectiveMap::iterator it = m_Objectives.begin(); it != m_Objectives.end(); ++it) - { - delete it->second; - } + cObjective Objective(a_Type); - for (TeamMap::iterator it = m_Teams.begin(); it != m_Teams.end(); ++it) - { - delete it->second; - } -} + std::pair Status = m_Objectives.insert(NamedObjective(a_Name, Objective)); - - - - -cObjective* cScoreboard::RegisterObjective(const AString & a_Name, eObjectiveType a_Type) -{ - cObjective* Objective = new cObjective(a_Type); - - bool Status = m_Objectives.insert(NamedObjective(a_Name, Objective)).second; - - if (Status) - { - return Objective; - } - else - { - delete Objective; - return NULL; - } + return Status.second ? &Status.first->second : NULL; } @@ -215,7 +190,7 @@ cObjective* cScoreboard::GetObjective(const AString & a_Name) } else { - return it->second; + return &it->second; } } @@ -223,22 +198,16 @@ cObjective* cScoreboard::GetObjective(const AString & a_Name) -cTeam* cScoreboard::RegisterTeam(const AString & a_Name, const AString & a_DisplayName, - const AString & a_Prefix, const AString & a_Suffix) +cTeam* cScoreboard::RegisterTeam( + const AString & a_Name, const AString & a_DisplayName, + const AString & a_Prefix, const AString & a_Suffix +) { - cTeam* Team = new cTeam(a_Name, a_DisplayName, a_Prefix, a_Suffix); + cTeam Team(a_Name, a_DisplayName, a_Prefix, a_Suffix); - bool Status = m_Teams.insert(NamedTeam(a_Name, Team)).second; + std::pair Status = m_Teams.insert(NamedTeam(a_Name, Team)); - if (Status) - { - return Team; - } - else - { - delete Team; - return NULL; - } + return Status.second ? &Status.first->second : NULL; } @@ -273,7 +242,7 @@ cTeam* cScoreboard::GetTeam(const AString & a_Name) } else { - return it->second; + return &it->second; } } @@ -281,14 +250,14 @@ cTeam* cScoreboard::GetTeam(const AString & a_Name) -void cScoreboard::ForEachObjectiveWith(eObjectiveType a_Type, cObjectiveCallback& a_Callback) +void cScoreboard::ForEachObjectiveWith(cObjective::eType a_Type, cObjectiveCallback& a_Callback) { for (ObjectiveMap::iterator it = m_Objectives.begin(); it != m_Objectives.end(); ++it) { - if (it->second->GetType() == a_Type) + if (it->second.GetType() == a_Type) { // Call callback - if (a_Callback.Item(it->second)) + if (a_Callback.Item(&it->second)) { return; } diff --git a/src/Scoreboard.h b/src/Scoreboard.h index 8ab298a07..7993b1333 100644 --- a/src/Scoreboard.h +++ b/src/Scoreboard.h @@ -13,61 +13,51 @@ -class cPlayer; class cObjective; -typedef std::set< cPlayer * > cPlayerSet; typedef cItemCallback cObjectiveCallback; -enum eObjectiveType -{ - E_OBJECTIVE_DUMMY, - - E_OBJECTIVE_DEATH_COUNT, - E_OBJECTIVE_PLAYER_KILL_COUNT, - E_OBJECTIVE_TOTAL_KILL_COUNT, - E_OBJECTIVE_HEALTH, - - E_OBJECTIVE_ACHIEVEMENT, - - E_OBJECTIVE_STAT, - E_OBJECTIVE_STAT_ITEM_CRAFT, - E_OBJECTIVE_STAT_ITEM_USE, - E_OBJECTIVE_STAT_ITEM_BREAK, - - E_OBJECTIVE_STAT_BLOCK_MINE, - E_OBJECTIVE_STAT_ENTITY_KILL, - E_OBJECTIVE_STAT_ENTITY_KILLED_BY -}; - - - - - -enum eDisplaySlot -{ - E_DISPLAY_SLOT_LIST, - E_DISPLAY_SLOT_SIDEBAR, - E_DISPLAY_SLOT_NAME -}; - - - - - class cObjective { public: typedef int Score; -public: - cObjective(eObjectiveType a_Type); + enum eType + { + E_TYPE_DUMMY, - eObjectiveType GetType(void) const { return m_Type; } + E_TYPE_DEATH_COUNT, + E_TYPE_PLAYER_KILL_COUNT, + E_TYPE_TOTAL_KILL_COUNT, + E_TYPE_HEALTH, + + E_TYPE_ACHIEVEMENT, + + E_TYPE_STAT, + E_TYPE_STAT_ITEM_CRAFT, + E_TYPE_STAT_ITEM_USE, + E_TYPE_STAT_ITEM_BREAK, + + E_TYPE_STAT_BLOCK_MINE, + E_TYPE_STAT_ENTITY_KILL, + E_TYPE_STAT_ENTITY_KILLED_BY + }; + + enum eDisplaySlot + { + E_DISPLAY_SLOT_LIST, + E_DISPLAY_SLOT_SIDEBAR, + E_DISPLAY_SLOT_NAME + }; + +public: + cObjective(eType a_Type); + + eType GetType(void) const { return m_Type; } eDisplaySlot GetDisplaySlot(void) const { return m_Display; } @@ -98,7 +88,7 @@ private: ScoreMap m_Scores; - eObjectiveType m_Type; + eType m_Type; eDisplaySlot m_Display; }; @@ -110,14 +100,17 @@ private: class cTeam { public: - cTeam(const AString & a_Name, const AString & a_DisplayName, - const AString & a_Prefix, const AString & a_Suffix); + + cTeam( + const AString & a_Name, const AString & a_DisplayName, + const AString & a_Prefix, const AString & a_Suffix + ); /// Adds a new player to the team - bool AddPlayer(cPlayer * a_Player); + bool AddPlayer(const AString & a_Name); /// Removes a player from the team - bool RemovePlayer(cPlayer * a_Player); + bool RemovePlayer(const AString & a_Name); /// Removes all registered players void Reset(void); @@ -125,10 +118,10 @@ public: /// Returns the number of registered players unsigned int GetNumPlayers(void) const; - bool GetFriendlyFire(void) const { return m_FriendlyFire; } - bool GetCanSeeFriendlyInvisible(void) const { return m_SeeFriendlyInvisible; } + bool AllowsFriendlyFire(void) const { return m_AllowsFriendlyFire; } + bool CanSeeFriendlyInvisible(void) const { return m_CanSeeFriendlyInvisible; } - const AString & GetDisplayName(void) const { return m_Name; } + const AString & GetDisplayName(void) const { return m_DisplayName; } const AString & GetName(void) const { return m_DisplayName; } const AString & GetPrefix(void) const { return m_Prefix; } @@ -144,8 +137,8 @@ public: private: - bool m_FriendlyFire; - bool m_SeeFriendlyInvisible; + bool m_AllowsFriendlyFire; + bool m_CanSeeFriendlyInvisible; AString m_DisplayName; AString m_Name; @@ -154,7 +147,9 @@ private: AString m_Suffix; // TODO 2014-01-19 xdot: Potential optimization - vector/list - cPlayerSet m_Players; + typedef std::set PlayerNameSet; + + PlayerNameSet m_Players; }; @@ -165,10 +160,9 @@ class cScoreboard { public: cScoreboard() {} - virtual ~cScoreboard(); - /// Registers a new scoreboard objective, returns the cObjective instance - cObjective* RegisterObjective(const AString & a_Name, eObjectiveType a_Type); + /// Registers a new scoreboard objective, returns the cObjective instance, NULL on name collision + cObjective* RegisterObjective(const AString & a_Name, cObjective::eType a_Type); /// Removes a registered objective, returns true if operation was successful bool RemoveObjective(const AString & a_Name); @@ -176,7 +170,7 @@ public: /// Retrieves the objective with the specified name, NULL if not found cObjective* GetObjective(const AString & a_Name); - /// Registers a new team, returns the cTeam instance + /// Registers a new team, returns the cTeam instance, NULL on name collision cTeam* RegisterTeam(const AString & a_Name, const AString & a_DisplayName, const AString & a_Prefix, const AString & a_Suffix); @@ -187,14 +181,14 @@ public: cTeam* GetTeam(const AString & a_Name); /// Execute callback for each objective with the specified type - void ForEachObjectiveWith(eObjectiveType a_Type, cObjectiveCallback& a_Callback); + void ForEachObjectiveWith(cObjective::eType a_Type, cObjectiveCallback& a_Callback); private: - typedef std::pair NamedObjective; - typedef std::pair NamedTeam; + typedef std::pair NamedObjective; + typedef std::pair NamedTeam; - typedef std::map ObjectiveMap; - typedef std::map TeamMap; + typedef std::map ObjectiveMap; + typedef std::map TeamMap; // TODO 2014-01-19 xdot: Potential optimization - Sort objectives based on type ObjectiveMap m_Objectives;