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 */ directory()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 */ description()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 */ location()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 */ gametime()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 */ timestamp()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 */ user_data_dir()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 */ game_data_dir()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 */ get_saved_game(u_int32 pos)295 static gamedata * get_saved_game (u_int32 pos) 296 { 297 return saves[pos]; 298 } 299 300 /** 301 * Returns the global quests dictionary. 302 * 303 * 304 * @return Global quests dictionary. 305 */ quests()306 static dictionary <quest *> quests () 307 { 308 return data::quests; 309 } 310 311 /** 312 * Returns the player %character. 313 * 314 * 315 * @return Player %character. 316 */ player()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 */ get_character(string name)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 */ get_quest(string name)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 */ characters()353 static dictionary<character*> characters () 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 */ engine()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__ 401