Adonthell  0.4
gamedata.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2001/2002 by 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 /**
21  * @file gamedata.h
22  * @author Kai Sterker <kai.sterker@gmail.com>
23  *
24  * @brief Declares the gamedata and data classes.
25  *
26  *
27  */
28 
29 
30 #ifndef GAMEDATA_H__
31 #define GAMEDATA_H__
32 
33 #include "quest.h"
34 #include "character.h"
35 #include "adonthell.h"
36 
37 /**
38  * Contains all the attributes related to a saved %game and the
39  * high level methods for loading/saving the %game.
40  *
41  * A word about saved games: all games are stored inside
42  * $HOME/.adonthell/ into a individual subdirectory, consisting
43  * of the %game's name (e.g. wastesedge) with the appendix "-save-xxx"
44  * where "xxx" is a number between 001 and 999. All %data that belongs
45  * to a saved %game is contained in that directory, thus allowing
46  * to copy individual games to another machine and/or user.
47  *
48  * The numbering of the %game directories has no special meaning. Saved
49  * games are recognized by the first part of their name, and saving a
50  * new %game will never overwrite an existing.
51  */
52 class gamedata
53 {
54 public:
55  /**
56  * Default constructor.
57  *
58  */
59  gamedata ();
60 
61 #ifndef SWIG
62  /**
63  * Alternate constructor.
64  *
65  * @attention not available from %Python!
66  *
67  * @param desc description of the saved %game.
68  * @param dir directory of the saved %game.
69  * @param time Textual representation of in-game time.
70  */
71  gamedata (string desc, string dir, string time);
72 #endif
73 
74  /**
75  * Destructor.
76  *
77  */
78  ~gamedata ();
79 
80  /**
81  * Save a record to an opened file.
82  *
83  * @param ogzstream& opened file to save to.
84  */
85  void put (ogzstream&);
86 
87  /**
88  * Load a record from an opened file.
89  *
90  * @param igzstream& opened file to load from.
91  *
92  * @return true in case of success, false otherwise.
93  */
94  bool get (igzstream&);
95 
96  /**
97  * A bunch of methods to access the private attributes.
98  *
99  */
100  //@{
101 
102  /**
103  * Returns the directory where the saved %game lies.
104  *
105  * @return Directory where the saved %game lies.
106  */
107  const char* directory () { return Directory.c_str (); }
108 
109  /**
110  * Returns the description of the saved %game.
111  *
112  * @return Description of the saved %game.
113  */
114  const char* description () { return Description.c_str (); }
115 
116  /**
117  * Returns the location of the saved %game.
118  *
119  * @return Location of the saved %game.
120  */
121  const char* location () { return Location.c_str (); }
122 
123  /**
124  * Returns the in-game time of the saved %game.
125  *
126  * @return In-game time of the saved %game.
127  */
128  const char* gametime () { return Gametime.c_str (); }
129 
130  /**
131  * Returns the (real) time when this game has been saved
132  *
133  * @return (Real) time when this game has been saved
134  */
135  u_int32 timestamp () { return Timestamp; }
136 
137  /**
138  * Sets the description for this %game.
139  *
140  * @param string New description for this %game.
141  */
142  void set_description (string);
143 
144  /**
145  * Sets the directory for this %game.
146  *
147  * @param string New directory for this %game.
148  */
149  void set_directory (string);
150 
151  /**
152  * Set the in-game time of the saved %game.
153  *
154  * @param string In-game time of the saved %game.
155  */
156  void set_gametime (string);
157  //@}
158 
159  /**
160  * Initialise the saved games array. Searches the user directory
161  * for available save games and loads their description.
162  *
163  * @param udir The user directory, usually $HOME/.adonthell
164  * @param gdir The %game data directory, usually /usr/local/share/adonthell
165  * @param gname The name of the %game we are running, e.g. wastesedge
166  * @param qload Whether quick-loading should be enabled or disabled
167  *
168  * @return \e true in case of success, false otherwise.
169  */
170  static bool init (string udir, string gdir, string gname, u_int8 qload);
171 
172  /**
173  * Cleanup the saved %game array.
174  *
175  */
176  static void cleanup ();
177 
178  /**
179  * Load the characters state from a saved %game.
180  *
181  * @param pos Slot number to load.
182  *
183  * @return \e true in case of success, \e false otherwise.
184  */
185  static bool load_characters (u_int32 pos);
186 
187  /**
188  * Load the quests state from a saved %game.
189  *
190  * @param pos Slot number to load.
191  *
192  * @return \e true in case of success, \e false otherwise.
193  */
194  static bool load_quests (u_int32 pos);
195 
196  /**
197  * Load the mapengine state from a saved %game.
198  *
199  * @param pos Slot number to load.
200  *
201  * @return \e true in case of success, \e false otherwise.
202  */
203  static bool load_mapengine (u_int32 pos);
204 
205  /**
206  * Load the audio system state from a saved %game.
207  *
208  * @param pos Slot number to load.
209  *
210  * @return \e true in case of success, \e false otherwise.
211  */
212  static bool load_audio (u_int32 pos);
213 
214  static bool load_achievements (u_int32 pos);
215 
216  /**
217  * Loads a previously saved %game. Slot 0 points to the
218  * initial %game %data and needs to be loaded when starting
219  * a fresh %game.
220  *
221  * @param pos Slot number to load.
222  *
223  * @return \e true in case of success, \e false otherwise.
224  */
225  static bool load (u_int32 pos);
226 
227  /**
228  * Loads the most recent saved %game. This method only takes
229  * games created by the player into account, not the initial
230  * saved %game.
231  *
232  * @return \e true in case of success, \e false otherwise.
233  */
234  static bool load_newest ();
235 
236  /**
237  * Save a %game. When given a slot number in the range of
238  * the available saved games, the according %game will be
239  * overwritten, otherwise a new saved %game is created.
240  * Saving to slot 0 is not possible, as it contains the
241  * initial %game %data.
242  *
243  * @param pos Slot number where to save to.
244  * @param desc Description of the %game to be saved.
245  * @param time Textual representation of in-game time.
246  *
247  * @return \e true in case of success, false otherwise.
248  */
249  static bool save (u_int32 pos, string desc, string time);
250 
251  /**
252  * Unloads the current %game, resetting the engine to it's
253  * initial state.
254  *
255  */
256  static void unload ();
257 
258  /**
259  * Returns a pointer to the next saved %game.
260  *
261  *
262  * @return Next saved %game.
263  */
264  static gamedata* next_save ();
265 
266  /**
267  * Returns the user %data directory ($HOME/.adonthell).
268  *
269  *
270  * @return user %data directory.
271  */
272  static string user_data_dir ()
273  {
274  return user_data_dir_;
275  }
276 
277  /**
278  * Returns the %game %data directory.
279  *
280  *
281  * @return %game %data directory.
282  */
283  static string game_data_dir ()
284  {
285  return game_data_dir_;
286  }
287 
288  /**
289  * Returns a pointer to a saved %game.
290  *
291  * @param pos Slot number to return.
292  *
293  * @return Pointer to the saved %game at position \pos.
294  */
296  {
297  return saves[pos];
298  }
299 
300  /**
301  * Returns the global quests dictionary.
302  *
303  *
304  * @return Global quests dictionary.
305  */
307  {
308  return data::quests;
309  }
310 
311  /**
312  * Returns the player %character.
313  *
314  *
315  * @return Player %character.
316  */
317  static character* player ()
318  {
319  return data::the_player;
320  }
321 
322  /**
323  * Returns a certain NPC when given the name. Use player () to get
324  * the player %character, as his/her name will be set at runtime.
325  *
326  * @param name The name of the %character to return
327  *
328  * @return a %character.
329  */
330  static character* get_character (string name)
331  {
332  return data::characters [name];
333  }
334 
335  /**
336  * Returns a certain quest when given the name.
337  *
338  * @param name The name of the %quest to return
339  *
340  * @return a %quest
341  */
342  static quest* get_quest (string name)
343  {
344  return data::quests [name];
345  }
346 
347  /**
348  * Returns the characters dictionary
349  *
350  *
351  * @return Characters dictionary.
352  */
354  {
355  return data::characters;
356  }
357 
358  /**
359  * Returns a pointer to the global game engine.
360  *
361  *
362  * @return Pointer to the global game engine.
363  */
364  static adonthell* engine ()
365  {
366  return data::engine;
367  }
368 
369 private:
370 #ifndef SWIG
371  string Directory; // the game's location on the harddisk
372  string Description; // user supplied description of the game
373  string Location; // the map or area the player is on
374  string Gametime; // the gametime of the saved game
375  u_int32 Timestamp; // time of last save to this file
376 
377  static string game_name;
378  static u_int8 quick_load;
379 
380  /**
381  * Keeps track of available saved games.
382  *
383  */
384  static vector<gamedata*> saves;
385 
386  /**
387  * $HOME/.adonthell
388  *
389  */
390  static string user_data_dir_;
391 
392  /**
393  * Game data directory.
394  *
395  */
396  static string game_data_dir_;
397 #endif
398 };
399 
400 #endif // GAMEDATA_H__
gamedata::set_gametime
void set_gametime(string)
Set the in-game time of the saved game.
Definition: gamedata.cc:113
gamedata::quests
static dictionary< quest * > quests()
Returns the global quests dictionary.
Definition: gamedata.h:306
gamedata::description
const char * description()
Returns the description of the saved game.
Definition: gamedata.h:114
gamedata::save
static bool save(u_int32 pos, string desc, string time)
Save a game.
Definition: gamedata.cc:340
gamedata::load_characters
static bool load_characters(u_int32 pos)
Load the characters state from a saved game.
Definition: gamedata.cc:118
gamedata::directory
const char * directory()
A bunch of methods to access the private attributes.
Definition: gamedata.h:107
gamedata::get
bool get(igzstream &)
Load a record from an opened file.
Definition: gamedata.cc:77
igzstream
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
gamedata::next_save
static gamedata * next_save()
Returns a pointer to the next saved game.
Definition: gamedata.cc:521
dictionary
Stores objects of any kind.
Definition: storage.h:231
gamedata::cleanup
static void cleanup()
Cleanup the saved game array.
Definition: gamedata.cc:612
u_int32
#define u_int32
32 bits long unsigned integer
Definition: types.h:41
gamedata::load
static bool load(u_int32 pos)
Loads a previously saved game.
Definition: gamedata.cc:303
gamedata::init
static bool init(string udir, string gdir, string gname, u_int8 qload)
Initialise the saved games array.
Definition: gamedata.cc:544
gamedata::set_description
void set_description(string)
Sets the description for this game.
Definition: gamedata.cc:103
ogzstream
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
u_int8
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
gamedata::location
const char * location()
Returns the location of the saved game.
Definition: gamedata.h:121
character
Class holding game characters.
Definition: character.h:39
gamedata::gamedata
gamedata()
Default constructor.
Definition: gamedata.cc:61
gamedata
Contains all the attributes related to a saved game and the high level methods for loading/saving the...
Definition: gamedata.h:52
gamedata::timestamp
u_int32 timestamp()
Returns the (real) time when this game has been saved.
Definition: gamedata.h:135
quest
Definition: quest.h:27
character.h
Declares the character class.
adonthell.h
Declares the adonthell class.
gamedata::get_saved_game
static gamedata * get_saved_game(u_int32 pos)
Returns a pointer to a saved game.
Definition: gamedata.h:295
gamedata::engine
static adonthell * engine()
Returns a pointer to the global game engine.
Definition: gamedata.h:364
gamedata::load_audio
static bool load_audio(u_int32 pos)
Load the audio system state from a saved game.
Definition: gamedata.cc:237
gamedata::get_character
static character * get_character(string name)
Returns a certain NPC when given the name.
Definition: gamedata.h:330
gamedata::game_data_dir
static string game_data_dir()
Returns the game data directory.
Definition: gamedata.h:283
gamedata::put
void put(ogzstream &)
Save a record to an opened file.
Definition: gamedata.cc:90
gamedata::load_quests
static bool load_quests(u_int32 pos)
Load the quests state from a saved game.
Definition: gamedata.cc:164
gamedata::player
static character * player()
Returns the player character.
Definition: gamedata.h:317
gamedata::user_data_dir
static string user_data_dir()
Returns the user data directory ($HOME/.adonthell).
Definition: gamedata.h:272
gamedata::load_newest
static bool load_newest()
Loads the most recent saved game.
Definition: gamedata.cc:317
gamedata::get_quest
static quest * get_quest(string name)
Returns a certain quest when given the name.
Definition: gamedata.h:342
gamedata::~gamedata
~gamedata()
Destructor.
Definition: gamedata.cc:73
gamedata::characters
static dictionary< character * > characters()
Returns the characters dictionary.
Definition: gamedata.h:353
gamedata::load_mapengine
static bool load_mapengine(u_int32 pos)
Load the mapengine state from a saved game.
Definition: gamedata.cc:203
gamedata::set_directory
void set_directory(string)
Sets the directory for this game.
Definition: gamedata.cc:108
adonthell
This is the heart of the Adonthell engine.
Definition: adonthell.h:44
gamedata::gametime
const char * gametime()
Returns the in-game time of the saved game.
Definition: gamedata.h:128
gamedata::unload
static void unload()
Unloads the current game, resetting the engine to it's initial state.
Definition: gamedata.cc:620