SuperTuxKart
replay_play.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2012-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_REPLAY__PLAY_HPP
20 #define HEADER_REPLAY__PLAY_HPP
21 
22 #include "replay/replay_base.hpp"
23 #include "tracks/track.hpp"
24 
25 #include "irrString.h"
26 #include <algorithm>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 
31 using namespace irr;
32 
33 class GhostKart;
34 
38 class ReplayPlay : public ReplayBase
39 {
40 
41 public:
43  enum SortOrder
44  {
45  SO_DEFAULT,
46  SO_TRACK = SO_DEFAULT,
47  SO_REV,
48  SO_KART_NUM,
49  SO_DIFF,
50  SO_LAPS,
51  SO_TIME,
52  SO_USER,
53  SO_VERSION
54  };
55 
56  class ReplayData
57  {
58  public:
59  std::string m_filename;
60  std::string m_track_name;
61  Track* m_track;
62  std::string m_minor_mode;
63  core::stringw m_stk_version;
64  core::stringw m_user_name;
65  core::stringw m_info;
66  std::vector<std::string> m_kart_list;
67  std::vector<core::stringw> m_name_list;
68  std::vector<float> m_kart_color; //no sorting for this
69  bool m_reverse;
70  bool m_custom_replay_file;
71  unsigned int m_difficulty;
72  unsigned int m_laps;
73  unsigned int m_replay_version; //no sorting for this
74  uint64_t m_replay_uid; //no sorting for this
75  float m_min_time;
76 
77  bool operator < (const ReplayData& r) const
78  {
79  switch (m_sort_order)
80  {
81  case SO_TRACK:
82  return m_track->getSortName() < r.m_track->getSortName();
83  break;
84  case SO_KART_NUM:
85  return m_kart_list.size() < r.m_kart_list.size();
86  break;
87  case SO_REV:
88  return m_reverse < r.m_reverse;
89  break;
90  case SO_DIFF:
91  return m_difficulty < r.m_difficulty;
92  break;
93  case SO_LAPS:
94  return m_laps < r.m_laps;
95  break;
96  case SO_TIME:
97  return m_min_time < r.m_min_time;
98  break;
99  case SO_USER:
100  return m_user_name < r.m_user_name;
101  break;
102  case SO_VERSION:
103  return m_stk_version < r.m_stk_version;
104  break;
105  } // switch
106  return true;
107  } // operator <
108  }; // ReplayData
109 
110 private:
111  static ReplayPlay *m_replay_play;
112 
113  static SortOrder m_sort_order;
114 
115  unsigned int m_current_replay_file;
116 
117  unsigned int m_second_replay_file;
118 
119  bool m_second_replay_enabled;
120 
121  std::vector<ReplayData> m_replay_file_list;
122 
124  std::vector<std::shared_ptr<GhostKart> > m_ghost_karts;
125 
126  ReplayPlay();
127  ~ReplayPlay();
128  void readKartData(FILE *fd, char *next_line, bool second_replay);
129 public:
130  void reset();
131  void load();
132  void loadFile(bool second_replay);
133  void loadAllReplayFile();
134  // ------------------------------------------------------------------------
135  static void setSortOrder(SortOrder so) { m_sort_order = so; }
136  // ------------------------------------------------------------------------
137  void sortReplay(bool reverse)
138  {
139  (reverse ? std::stable_sort(m_replay_file_list.rbegin(),
140  m_replay_file_list.rend()) : std::stable_sort(m_replay_file_list.begin(),
141  m_replay_file_list.end()));
142  }
143  // ------------------------------------------------------------------------
144  void setReplayFile(unsigned int n)
145  { m_current_replay_file = n; }
146 
147  // ------------------------------------------------------------------------
148  void setSecondReplayFile(unsigned int n, bool second_replay_enabled)
149  { m_second_replay_file = n;
150  m_second_replay_enabled = second_replay_enabled;}
151 
152  // ------------------------------------------------------------------------
153  void setReplayFileByUID(uint64_t uid);
154  // ------------------------------------------------------------------------
155  unsigned int getReplayIdByUID(uint64_t uid);
156 
157  // ------------------------------------------------------------------------
158  bool addReplayFile(const std::string& fn,
159  bool custom_replay = false,
160  int call_index = 0);
161  // ------------------------------------------------------------------------
162  const ReplayData& getReplayData(unsigned int n) const
163  { return m_replay_file_list.at(n); }
164  // ------------------------------------------------------------------------
165  const ReplayData& getCurrentReplayData() const
166  { return m_replay_file_list.at(m_current_replay_file); }
167  // ------------------------------------------------------------------------
168  const unsigned int getNumReplayFile() const
169  { return (unsigned int)m_replay_file_list.size(); }
170  // ------------------------------------------------------------------------
171  std::shared_ptr<GhostKart> getGhostKart(int n) { return m_ghost_karts[n]; }
172  // ------------------------------------------------------------------------
173  const unsigned int getNumGhostKart() const
174  {
175  assert(m_replay_file_list.size() > 0);
176  unsigned int num =
177  (unsigned int)m_replay_file_list.at(m_current_replay_file)
178  .m_kart_list.size();
179  unsigned int second_file_num =
180  (unsigned int)m_replay_file_list.at(m_second_replay_file)
181  .m_kart_list.size();
182 
183  num = (m_second_replay_enabled) ? num + second_file_num : num;
184 
185  return num;
186  } // getNumGhostKart
187  // ------------------------------------------------------------------------
188  const std::string& getGhostKartName(unsigned int n) const
189  {
190  assert(m_replay_file_list.size() > 0);
191 
192  unsigned int fkn =
193  (unsigned int)m_replay_file_list.at(m_current_replay_file)
194  .m_kart_list.size();
195  if (n < fkn)
196  return m_replay_file_list.at(m_current_replay_file).m_kart_list.at(n);
197  else
198  return m_replay_file_list.at(m_second_replay_file).m_kart_list.at(n-fkn);
199  }
200  // ------------------------------------------------------------------------
202  static void create() { m_replay_play = new ReplayPlay(); }
203  // ------------------------------------------------------------------------
205  static ReplayPlay *get() { return m_replay_play; }
206  // ------------------------------------------------------------------------
208  static void destroy()
209  { delete m_replay_play; m_replay_play = NULL; }
210  // ------------------------------------------------------------------------
212  virtual const std::string& getReplayFilename(int replay_file_number = 1) const
213  {
214  assert(m_replay_file_list.size() > 0);
215  if (replay_file_number == 2)
216  return m_replay_file_list.at(m_second_replay_file).m_filename;
217  else
218  return m_replay_file_list.at(m_current_replay_file).m_filename;
219  }
220  // ------------------------------------------------------------------------
221  unsigned int getCurrentReplayFileIndex() const
222  { return m_current_replay_file; }
223  // ------------------------------------------------------------------------
224  unsigned int getSecondReplayFileIndex() const
225  { return m_second_replay_file; }
226  // ------------------------------------------------------------------------
227  bool isSecondReplayEnabled() const { return m_second_replay_enabled; }
228 }; // Replay
229 
230 #endif
A ghost kart.
Definition: ghost_kart.hpp:38
Definition: replay_base.hpp:33
Definition: replay_play.hpp:57
Definition: replay_play.hpp:39
static void create()
Creates a new instance of the replay object.
Definition: replay_play.hpp:202
static void destroy()
Delete the instance of the replay object.
Definition: replay_play.hpp:208
virtual const std::string & getReplayFilename(int replay_file_number=1) const
Returns the filename that was opened.
Definition: replay_play.hpp:212
SortOrder
Order of sort for ReplayData.
Definition: replay_play.hpp:44
std::vector< std::shared_ptr< GhostKart > > m_ghost_karts
All ghost karts.
Definition: replay_play.hpp:124
static ReplayPlay * get()
Returns the instance of the replay object.
Definition: replay_play.hpp:205
Definition: track.hpp:115
core::stringw getSortName() const
Returns the name of the track used to sort the tracks alphabetically.
Definition: track.cpp:249