Adonthell  0.4
achievements.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell 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 Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /**
20  * @file achievements.h
21  *
22  * @author Kai Sterker
23  * @brief Manages in-game achievements
24  */
25 
26 #ifndef ACHIEVEMENTS_H_
27 #define ACHIEVEMENTS_H_
28 
29 #include "py_callback.h"
30 #include "fileops.h"
31 #include <vector>
32 
33 using namespace std;
34 
35 #ifndef SWIG
36 
37 /**
38  * Data for a single achievement. This contains of a unique id,
39  * the state that will unlock the achievement, whether the
40  * achievement has already been unlocked and the state of the
41  * achievement in the current game.
42  */
44 {
45 public:
46  /**
47  * Create a new achievement with the given id and the bitmask
48  * that will eventually unlock it.
49 
50  * @param id the unique achievement id
51  * @param expected the value that will unlock the achievement
52  */
53  achievement_data(const u_int8 & id, const u_int32 & expected);
54  virtual ~achievement_data();
55 
56  /**
57  * Get the unique id of the achievement
58  * @return the unique id of the achievement
59  */
60  u_int8 id() const { return _id; }
61 
62  /**
63  * Set the nth bit of the given achievement to 1.
64  * @param bit the index of the bit to set to 1
65  * @return \e true if the new achievement value matches the expected
66  * bitmask to permanently unlock the achievement. \e False otherwise.
67  */
68  bool update(const u_int8 & bit);
69 
70  /**
71  * Check whether the achievement is permanently unlocked
72  * @return \e true if the achievement is unlocked, \e false otherwise.
73  */
74  bool is_unlocked() const { return _persistent == _expected; }
75 
76  friend class achievements;
77 
78 private:
79  /// the unique id
80  u_int8 _id;
81  /// expected value to unlock this achievement
82  u_int32 _expected;
83  /// value of the achievement for the current game
84  u_int32 _current;
85  /// value of the achievement for all games
86  u_int32 _persistent;
87 };
88 
89 #endif
90 
91 /**
92  * Class that keeps track of all available achievements,
93  * their permanent unlocked state and state in the current game.
94  */
96 {
97 public:
98  /**
99  * Create a new achievement with the given id and the bitmask
100  * that will eventually unlock it. If an achievement with the
101  * given id already exists, it will be updated.
102  *
103  * Note that ids 0 and 255 are reserved.
104  *
105  * @param achievement the unique achievement id
106  * @param bitmask the value that will unlock the achievement
107  *
108  * @return \e true on success, \e false otherwise
109  */
110  static bool create(const u_int8 & achievement, const u_int32 & bitmask);
111 
112  /**
113  * Set the nth bit of the given achievement to 1.
114  * If the current value of the achievement afterwards matches
115  * the bitmask given when creating the achievement, the achievement
116  * will be unlocked permanently (only once).
117  *
118  * @param achievement the unique achievement id
119  * @param bit the index of the bit to set to 1.
120  */
121  static void update(const u_int8 & achievement, const u_int8 & bit);
122 
123  /**
124  * Return the total number of available achievements.
125  * @returns the number of achievements.
126  */
127  static int num_achievements() { return _achievements.size(); }
128 
129  /**
130  * Returns how many achievements have been permanently unlocked.
131  * @returns the number of unlocked achievements.
132  */
133  static int num_unlocked ();
134 
135  /**
136  * Checks whether the achievement at the given index is unlocked.
137  * @param index a number between 0 and num_achievements
138  * @return \e true if the achievement has been unlocked, \e false if not.
139  */
140  static bool is_unlocked (const u_int32 & index);
141 
142  /**
143  * Get the achievement id at the given index.
144  * @param index a number between 0 and num_achievements
145  * @return the id of the achievement stored at the given index.
146  */
147  static u_int8 achievement_id(const u_int32 & index);
148 
149  /**
150  * Allow to connect a python callback to get notified when a new
151  * achievement was unlocked. Callback will receive the achievement id
152  * along with any additional arguments given when connecting the callback
153  *
154  * @param pyfunc the callback to register
155  * @param args the optional arguments to use when executing the callback
156  */
157  static void py_signal_connect (PyObject *pyfunc, PyObject *args = NULL);
158 
159 #ifndef SWIG
160  /**
161  * Load achievement data from stream.
162  * @param file the stream to read data from.
163  * @return \e true on success, \e false otherwise.
164  */
165  static bool get_state (igzstream& file);
166 
167  /**
168  * Save achievement data to stream.
169  * @param file the stream to write data to.
170  */
171  static void put_state (ogzstream& file);
172 
173  /**
174  * Initialize achievements by loading all available
175  * achievements and their permanent unlocked status.
176  */
177  static void init();
178 
179  /**
180  * Write permanent unlock status of available achievements.
181  */
182  static void make_persistent();
183 #endif
184 
185 private:
186  achievements();
187  virtual ~achievements();
188 
189 #ifndef SWIG
190  /// a callback executed when an achievement gets unlocked
191  static py_callback *_callback;
192 
193  /// list of available achievements
194  static vector<achievement_data> _achievements;
195 #endif
196 };
197 
198 #endif
Class that keeps track of all available achievements, their permanent unlocked state and state in the...
Definition: achievements.h:95
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
u_int8 id() const
Get the unique id of the achievement.
Definition: achievements.h:60
Declares the py_callback class.
Definition: str_hash.h:71
#define u_int32
32 bits long unsigned integer
Definition: types.h:41
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
static int num_achievements()
Return the total number of available achievements.
Definition: achievements.h:127
bool is_unlocked() const
Check whether the achievement is permanently unlocked.
Definition: achievements.h:74
Data for a single achievement.
Definition: achievements.h:43
Stores the C++ <-> Python callback binding.
Definition: py_callback.h:41
Declares the igzstream, ogzstream and fileops classes.