SuperTuxKart
achievements_status.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2013-2015 Glenn De Jonghe
4 // (C) 2014-2015 Joerg Henrichs
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 3
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 #ifndef HEADER_ACHIEVEMENTS_SLOT_HPP
21 #define HEADER_ACHIEVEMENTS_SLOT_HPP
22 
23 #include "utils/types.hpp"
24 
25 #include <irrString.h>
26 #include <map>
27 #include <string>
28 #include <vector>
29 
30 class Achievement;
31 class UTFWriter;
32 class XMLNode;
33 
43 {
44 public :
45  // Warning : changing what an existing id does breaks
46  // save-game compatibility. Bump version number if doing so.
47  enum AchievementData {
48  // Won races values share the following properties :
49  // 1. Only races with at least 3 AI count unless otherwise specified.
50  WON_RACES = 0, // Normal, time-trial and FTL
51  WON_NORMAL_RACES = 1, // Normal race only
52  WON_TT_RACES = 2, // Time-trial race only
53  WON_FTL_RACES = 3, // Follow-the-leader race only
54  // Consecutive won race values :
55  // 1. Ignore races with not enough AIs for incrementation
56  // 2. Reset the counter in case of loss against any number of AIs
57  CONS_WON_RACES = 4,
58  CONS_WON_RACES_MAX = 5,
59  // Won races in (at least) hard requires at least 5 AI opponents
60  CONS_WON_RACES_HARD = 6,
61  CONS_WON_RACES_HARD_MAX = 7,
62  // Count how many normal, TT & FTL races were started and finished by difficulty
63  EASY_STARTED = 8,
64  EASY_FINISHED = 9,
65  MEDIUM_STARTED = 10,
66  MEDIUM_FINISHED = 11,
67  HARD_STARTED = 12,
68  HARD_FINISHED = 13,
69  BEST_STARTED = 14,
70  BEST_FINISHED = 15,
71  // Count how many time a race/match was started and finished by game mode.
72  // Races with ghost replays technically belong to TT or egg hunt race mode,
73  // they increment both the with_ghost counter and the relevant mode counter.
74  NORMAL_STARTED = 16,
75  NORMAL_FINISHED = 17,
76  TT_STARTED = 18,
77  TT_FINISHED = 19,
78  FTL_STARTED = 20,
79  FTL_FINISHED = 21,
80  THREE_STRIKES_STARTED = 22,
81  THREE_STRIKES_FINISHED = 23,
82  SOCCER_STARTED = 24,
83  SOCCER_FINISHED = 25,
84  EGG_HUNT_STARTED = 26,
85  EGG_HUNT_FINISHED = 27,
86  WITH_GHOST_STARTED = 28,
87  WITH_GHOST_FINISHED = 29,
88  CTF_STARTED = 30,
89  CTF_FINISHED = 31,
90  FFA_STARTED = 32,
91  FFA_FINISHED = 33,
92 
93  // Count the number of powerups used by the player.
94  POWERUP_USED = 34,
95  POWERUP_USED_1RACE = 35,
96  POWERUP_USED_1RACE_MAX = 36,
97  // Count how many times a bowling ball from the player hit a kart
98  BOWLING_HIT = 37,
99  BOWLING_HIT_1RACE = 38,
100  BOWLING_HIT_1RACE_MAX = 39,
101  // Count how many times a swatter from the player hit a kart
102  SWATTER_HIT = 40,
103  SWATTER_HIT_1RACE = 41,
104  SWATTER_HIT_1RACE_MAX = 42,
105  // Count how many times a swatter, bowling ball or cake from
106  // the player hit a kart (excluding the player's own kart)
107  ALL_HITS = 43,
108  ALL_HITS_1RACE = 44,
109  ALL_HITS_1RACE_MAX = 45,
110  // Count the number of bananas hit
111  BANANA = 46,
112  BANANA_1RACE = 47,
113  BANANA_1RACE_MAX = 48,
114  // Count how many times the player skidded
115  SKIDDING = 49,
116  SKIDDING_1RACE = 50,
117  SKIDDING_1RACE_MAX = 51,
118  SKIDDING_1LAP = 52,
119  SKIDDING_1LAP_MAX = 53,
120 
121 
122  ACHIEVE_DATA_NUM = 54
123  };
124 
125 private:
126  std::map<uint32_t, Achievement *> m_achievements;
127 
128  // Variables used to track achievements progress,
129  // one variable may be used by several achievements.
130  // TODO
131  // Currently this only uses an int counter.
132  // Evaluate if additional data keeping (max achived ?) can be useful,
133  // and either expand the struct or remove it.
135  {
136  int counter;
137  };
138 
139  const int DATA_VERSION = 4;
140 
141  // The tracked values are defined at compile time
142  AchievementVariable m_variables[ACHIEVE_DATA_NUM];
143 
144  // We store the enum name and matching goalTree type
145  // in this table for faster lookup.
146  std::string m_ach_enum_to_xml[ACHIEVE_DATA_NUM];
147 
148 // Switching a few times from public to private
149 // helps here to keep related things next to each other
150 public:
151  enum TrackData {
152  // counters for standard, TT & FTL races
153  TR_STARTED = 0,
154  TR_FINISHED = 1,
155  // doesn't count race without any other AI/player
156  TR_WON = 2,
157  TR_FINISHED_REVERSE = 3,
158  // races against replays are counted, too
159  TR_FINISHED_ALONE = 4,
160  // counters for standard & TT races, apply to finished races only,
161  // lap number compared to track default.
162  TR_LESS_LAPS = 5,
163  TR_MORE_LAPS = 6,
164  TR_MIN_TWICE_LAPS = 7, // at least twice the track's default lap count
165  // counters for egg hunts
166  TR_EGG_HUNT_STARTED = 8,
167  TR_EGG_HUNT_FINISHED = 9,
168 
169  TR_DATA_NUM = 10
170  };
171 
172 private:
173  // To keep track of track-specific data without hardcoding
174  // a list of tracks, we use a special structure.
175  struct TrackStats
176  {
177  std::string ident;
178  int track_data[TR_DATA_NUM];
179  };
180 
181  std::vector<TrackStats> m_track_stats;
182 
183  // We store the enum name and matching goalTree type
184  // in this table for faster lookup.
185  // Each track data value matches 2 xml command
186  std::string m_tr_enum_to_xml[2*TR_DATA_NUM];
187 
188  // TODO : keep track of battle/soccer arenas
189 
190  // Keeps track of hits inflicted to other karts,
191  // identified by their world id.
192  // Reset at the beginning of a race
193  std::vector<int> m_kart_hits;
194 
195  // To avoid updating achievement progress being
196  // too computationally wasteful, we restrain
197  // what is checked on an update
198  enum UpdateType
199  {
200  UP_ACHIEVEMENT_DATA = 0,
201  UP_TRACK_DATA = 1,
202  UP_KART_HITS = 2,
203  };
204 
205  bool m_online;
206  bool m_valid;
207 
208  void setEnumToString();
209  void updateAchievementsProgress(UpdateType type, unsigned int enum_id);
210 
211 public :
214  Achievement * getAchievement(uint32_t id);
215  void load(const XMLNode * input);
216  void save(UTFWriter &out);
217  void add(Achievement *achievement);
218  void sync(const std::vector<uint32_t> & achieved_ids);
219  void increaseDataVar(unsigned int achieve_data_id, int increase);
220  void resetDataVar(unsigned int achieve_data_id);
221  void onRaceEnd(bool aborted=false);
222  void onLapEnd();
223  void trackEvent(std::string track_ident, AchievementsStatus::TrackData event);
224  void resetKartHits(int num_karts);
225  void addKartHit(int kart_id);
226  void updateAllAchievementsProgress();
227  int getNumTracksAboveValue(int value, std::string goal_string);
228  int getNumAchieveTracks();
229  // ------------------------------------------------------------------------
230  std::map<uint32_t, Achievement *>& getAllAchievements()
231  {
232  return m_achievements;
233  }
234  // ------------------------------------------------------------------------
235  bool isOnline() const { return m_online; }
236  // ------------------------------------------------------------------------
237  bool isValid() const { return m_valid; }
238  // ------------------------------------------------------------------------
239 }; // class AchievementsStatus
240 
241 #endif
242 
243 /*EOF*/
Definition: achievement.hpp:42
This class keeps tracks of all achievements of one player.
Definition: achievements_status.hpp:43
AchievementsStatus()
Constructor for an Achievement.
Definition: achievements_status.cpp:44
void load(const XMLNode *input)
Loads the saved state of all achievements from an XML file.
Definition: achievements_status.cpp:175
void save(UTFWriter &out)
Saves the achievement status to a file.
Definition: achievements_status.cpp:295
void sync(const std::vector< uint32_t > &achieved_ids)
Synchronises the achievements between local and online usage.
Definition: achievements_status.cpp:341
void trackEvent(std::string track_ident, AchievementsStatus::TrackData event)
Use the event type to increment the correct track event counter.
Definition: achievements_status.cpp:601
~AchievementsStatus()
Removes all achievements.
Definition: achievements_status.cpp:78
void setEnumToString()
This function loads a table associating an enum identifier with the matching command in achievements....
Definition: achievements_status.cpp:91
utility class used to write wide (UTF-16 or UTF-32, depending of size of wchar_t) XML files
Definition: utf_writer.hpp:35
utility class used to parse XML files
Definition: xml_node.hpp:48
Definition: achievements_status.hpp:135
Definition: achievements_status.hpp:176
Declares the general types that are used by the network.