OpenClonk
C4RoundResults.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2008-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // Round result information to be displayed in game over dialog
17 // Collects information from:
18 // -Instances of C4Player as they are evaluated (EvaluatePlayer)
19 // -Game evaluation (EvaluateGame)
20 // -League evaluation (EvaluateLeague)
21 // -Script calls to AddCustomEvaluationString
22 
23 #ifndef INC_C4RoundResults
24 #define INC_C4RoundResults
25 
26 #include "c4group/C4Components.h"
27 #include "graphics/C4FacetEx.h"
28 #include "network/C4PacketBase.h"
29 #include "object/C4IDList.h"
30 
31 // Contains additional data not present in C4PlayerInfo
33 {
34 private:
35  // player ID of linked structure in Game.PlayerInfos
36  int32_t id{0};
37 
38  // player icon
39  // not compiled, so it will be lost for eliminated players after savegame resume,
40  // or for runtime joiners
41  C4FacetSurface fctBigIcon;
42 
43  // game data
44  uint32_t iTotalPlayingTime{0}; // total playing time in seconds
45  int32_t iScoreOld{-1}, iScoreNew{-1};
46  StdCopyStrBuf sCustomEvaluationStrings; // scenario specific
47 
48  // league data
49  int32_t iLeagueScoreNew{-1}; // score on league server after this round - -1 for unknown
50  int32_t iLeagueScoreGain{0}; // league score gained by this round - -1 for unknown
51  int32_t iLeagueRankNew{0}; // rank on league server after this round
52  int32_t iLeagueRankSymbolNew{0}; // rank symbol on league server after this round
53  int32_t iLeaguePerformance{0}; // script-set performance value, effect league-dependent
54  StdCopyStrBuf sLeagueProgressData; // scenario-specific data to store more proigress info (which levels were done, etc.)
55  enum LeagueStatus
56  {
57  RRPLS_Unknown=0, RRPLS_Lost, RRPLS_Won
58  } eLeagueStatus{RRPLS_Unknown}; // whether player lost or won
59 
60 public:
61  C4RoundResultsPlayer() : sLeagueProgressData() {}
62  C4RoundResultsPlayer(const C4RoundResultsPlayer &cpy) { *this=cpy; }
63 
64  void CompileFunc(StdCompiler *pComp);
65 
66  int32_t GetID() const { return id; }
67  C4Facet &GetBigIcon() { return fctBigIcon; }
68  uint32_t GetTotalPlayingTime() const { return iTotalPlayingTime; }
69  bool IsScoreOldValid() const { return iScoreOld>=0; }
70  int32_t GetScoreOld() const { return iScoreOld; }
71  bool IsScoreNewValid() const { return iScoreNew>=0; }
72  int32_t GetScoreNew() const { return iScoreNew; }
73  const char *GetCustomEvaluationStrings() { return sCustomEvaluationStrings.getData(); }
74  int32_t GetLeagueScoreNew() const { return iLeagueScoreNew; } // returns score number on league server after round evaluation (0 for not assigned)
75  bool IsLeagueScoreNewValid() const { return iLeagueScoreNew>=0; }
76  int32_t GetLeagueScoreGain() const { return iLeagueScoreGain; }
77  int32_t GetLeagueRankNew() const { return iLeagueRankNew; } // returns rank on league server after round evaluation (0 for not assigned)
78  int32_t GetLeagueRankSymbolNew() const { return iLeagueRankSymbolNew; }
79  int32_t GetLeaguePerformance() const { return iLeaguePerformance; }
80 
81  void EvaluateLeague(C4RoundResultsPlayer *pLeaguePlayer); // called from league evaluation; set league fields
82  void EvaluatePlayer(C4Player *pPlr); // called from C4Player::Evaluate; set fields by player
83 
84  void SetID(int32_t idNew) { this->id=idNew; }
85  void AddCustomEvaluationString(const char *szCustomString);
86  void SetLeaguePerformance(int32_t iNewPerf) { iLeaguePerformance = iNewPerf; }
87 
88  bool operator ==(const C4RoundResultsPlayer &cmp);
90 };
91 
92 // player list in round results (std::vector<C4RoundResultsPlayer>...)
94 {
95 private:
96  // players
97  C4RoundResultsPlayer **ppPlayers{nullptr};
98  int32_t iPlayerCount{0}, iPlayerCapacity{0};
99 
100 public:
102  C4RoundResultsPlayers(const C4RoundResultsPlayers &cpy) { *this=cpy; }
104 
105  void Clear();
106 
107  void CompileFunc(StdCompiler *pComp);
108 
109 private:
110  void GrowList(size_t iByVal);
111 
112 public:
113  C4RoundResultsPlayer *GetByIndex(int32_t idx) const;
114  C4RoundResultsPlayer *GetByID(int32_t id) const;
115  int32_t GetCount() const { return iPlayerCount; }
116 
117  void Add(C4RoundResultsPlayer *pNewPlayer);
118  C4RoundResultsPlayer *GetCreateByID(int32_t id);
119 
120  bool operator ==(const C4RoundResultsPlayers &cmp);
122 };
123 
125 {
126 public:
128  {
129  NR_None=0, // undefined
130  NR_LeagueOK, // league evaluated
131  NR_LeagueError, // league evaluation error
132  NR_NetError // network disconnect
133  };
134 
135 private:
136  // player list
137  C4RoundResultsPlayers Players;
138 
139  // game data
140  C4IDList Goals; // Goals at time of evaluation
141  C4IDList FulfilledGoals; // only those goals that are fulfilled
142  uint32_t iPlayingTime{0}; // game time in seconds
143  int32_t iLeaguePerformance; // settlement league performance points
144  bool fHideSettlementScore; // to hide the score in the evaluation dialogue (for melees)
145 
146  // league/network result
147  StdCopyStrBuf sNetResult;
148  NetResult eNetResult;
149 
150  // scenario-specific
151  StdCopyStrBuf sCustomEvaluationStrings;
152  StdCopyStrBuf Statistics; // stats collected by script at the end of the round
153 
154 public:
155  C4RoundResults() = default;
157 
158  void Clear();
159  void Init();
160 
161  void CompileFunc(StdCompiler *pComp);
162 
163 public:
164  // fill GoalList with current goal status - also called by goal menu!
165  static void EvaluateGoals(C4IDList &GoalList, C4IDList &FulfilledGoalList, int32_t iPlayerNumber);
166 
167  // Evaluation called by C4Game::Evaluate
168  // Caution: This does script callbacks for goal fulfillment check and must be called in sync,
169  // i.e. even for dedicated server
170  void EvaluateGame();
171 
172  // Evaluation called by league: Sets new league scores and ranks
173  void EvaluateLeague(const char *szResultMsg, bool fSuccess, const C4RoundResultsPlayers &rLeagueInfo);
174 
175  // Evaluation called by player when it's evaluated
176  void EvaluatePlayer(C4Player *pPlr);
177 
178  // Evaluation by network: Disconnect or league info
179  void EvaluateNetwork(NetResult eResult, const char *szResultsString);
180 
181  // Set custom string to be shown in game over dialog
182  // idPlayer==0 for global strings
183  void AddCustomEvaluationString(const char *szCustomString, int32_t idPlayer);
184 
185  // to hide the settlement score in melees
186  void HideSettlementScore(bool fHide=true);
188 
189  // Used for special league scenarios, e.g. settlement scenarios that wish to use a
190  // measure different from the elapsed game time
191  void SetLeaguePerformance(int32_t iNewPerf, int32_t idPlayer = 0);
192  int32_t GetLeaguePerformance(int32_t idPlayer = 0) const;
193 
194  StdCopyStrBuf GetStatistics() const { return Statistics; }
195 
196  const C4RoundResultsPlayers &GetPlayers() const { return Players; }
197  const char *GetCustomEvaluationStrings() const { return sCustomEvaluationStrings.getData(); }
198  NetResult GetNetResult() const { return eNetResult; }
199  const char *GetNetResultString() const { return sNetResult.getData(); }
200  bool HasNetResult() const { return eNetResult != NR_None; }
201 
202  bool Load(C4Group &hGroup, const char *szFilename = C4CFN_RoundResults);
203  bool Save(C4Group &hGroup, const char *szFilename = C4CFN_RoundResults);
204 
205  const C4IDList &GetGoals() const { return Goals; }
206  const C4IDList &GetFulfilledGoals() const { return FulfilledGoals; }
207 
208 };
209 
210 // * PID_LeagueRoundResults
211 // packet containing league round results for all participating players
212 // sent from host to client after league evaluation
214 {
215 public:
216  C4RoundResultsPlayers Players; // league info for players
217  StdCopyStrBuf sResultsString; // league result string - or error message
218  bool fSuccess{false}; // whether result was successful or not
219 
220  C4PacketLeagueRoundResults() = default; // std ctor
221  C4PacketLeagueRoundResults(const char *szResultsString, bool fSuccess, const C4RoundResultsPlayers &Players) : Players(Players), sResultsString(szResultsString), fSuccess(fSuccess) {} // ctor
222  C4PacketLeagueRoundResults(const char *szResultsString, bool fSuccess) : sResultsString(szResultsString), fSuccess(fSuccess) {} // ctor
223 
224  void CompileFunc(StdCompiler *pComp) override;
225 };
226 
227 
228 #endif // INC_C4RoundResults
#define C4CFN_RoundResults
Definition: C4Components.h:129
C4PlayerList Players
C4PacketLeagueRoundResults(const char *szResultsString, bool fSuccess)
C4PacketLeagueRoundResults(const char *szResultsString, bool fSuccess, const C4RoundResultsPlayers &Players)
void CompileFunc(StdCompiler *pComp) override
C4PacketLeagueRoundResults()=default
C4RoundResultsPlayers Players
void EvaluatePlayer(C4Player *pPlr)
void EvaluateLeague(const char *szResultMsg, bool fSuccess, const C4RoundResultsPlayers &rLeagueInfo)
const char * GetNetResultString() const
bool Save(C4Group &hGroup, const char *szFilename=C4CFN_RoundResults)
C4RoundResults()=default
void EvaluateNetwork(NetResult eResult, const char *szResultsString)
void CompileFunc(StdCompiler *pComp)
bool Load(C4Group &hGroup, const char *szFilename=C4CFN_RoundResults)
bool HasNetResult() const
const C4RoundResultsPlayers & GetPlayers() const
const char * GetCustomEvaluationStrings() const
static void EvaluateGoals(C4IDList &GoalList, C4IDList &FulfilledGoalList, int32_t iPlayerNumber)
void HideSettlementScore(bool fHide=true)
void AddCustomEvaluationString(const char *szCustomString, int32_t idPlayer)
void SetLeaguePerformance(int32_t iNewPerf, int32_t idPlayer=0)
const C4IDList & GetFulfilledGoals() const
StdCopyStrBuf GetStatistics() const
int32_t GetLeaguePerformance(int32_t idPlayer=0) const
const C4IDList & GetGoals() const
NetResult GetNetResult() const
bool SettlementScoreIsHidden()
void SetLeaguePerformance(int32_t iNewPerf)
void EvaluatePlayer(C4Player *pPlr)
int32_t GetScoreNew() const
C4RoundResultsPlayer(const C4RoundResultsPlayer &cpy)
bool IsLeagueScoreNewValid() const
C4Facet & GetBigIcon()
int32_t GetLeagueRankNew() const
int32_t GetLeagueScoreGain() const
int32_t GetScoreOld() const
bool IsScoreOldValid() const
uint32_t GetTotalPlayingTime() const
bool operator==(const C4RoundResultsPlayer &cmp)
void AddCustomEvaluationString(const char *szCustomString)
const char * GetCustomEvaluationStrings()
int32_t GetID() const
bool IsScoreNewValid() const
C4RoundResultsPlayer & operator=(const C4RoundResultsPlayer &cpy)
void EvaluateLeague(C4RoundResultsPlayer *pLeaguePlayer)
int32_t GetLeagueRankSymbolNew() const
int32_t GetLeagueScoreNew() const
int32_t GetLeaguePerformance() const
void SetID(int32_t idNew)
void CompileFunc(StdCompiler *pComp)
C4RoundResultsPlayer * GetCreateByID(int32_t id)
int32_t GetCount() const
C4RoundResultsPlayers & operator=(const C4RoundResultsPlayers &cpy)
C4RoundResultsPlayers()=default
C4RoundResultsPlayers(const C4RoundResultsPlayers &cpy)
bool operator==(const C4RoundResultsPlayers &cmp)
C4RoundResultsPlayer * GetByID(int32_t id) const
void Add(C4RoundResultsPlayer *pNewPlayer)
C4RoundResultsPlayer * GetByIndex(int32_t idx) const
void CompileFunc(StdCompiler *pComp)
const char * getData() const
Definition: StdBuf.h:442