40 #define ENGINE_DAT_VER 5
41 #define AUDIO_DAT_VER 2
42 #define CHAR_DAT_VER 4
43 #define QUEST_DAT_VER 1
44 #define SAVE_DAT_VER 3
46 vector<gamedata*> gamedata::saves;
47 string gamedata::user_data_dir_;
48 string gamedata::game_data_dir_;
49 string gamedata::game_name;
50 u_int8 gamedata::quick_load;
89 Timestamp = time (NULL);
120 filepath = saves[pos]->directory ();
121 filepath +=
"/character.data";
126 cerr <<
"Couldn't open \"" << filepath <<
"\" - stopping\n" << endl;
138 data::the_player->character_base::get_state (in);
139 data::characters[data::the_player->
get_id ().c_str ()] = data::the_player;
145 mynpc->character_base::get_state (in);
148 data::characters[mynpc->
get_id ().c_str ()] = mynpc;
163 filepath = saves[pos]->directory ();
164 filepath +=
"/quest.data";
169 cerr <<
"Couldn't open \"" << filepath <<
" - stopping\n" << endl;
184 data::quests[myquest->name.c_str ()] = myquest;
199 filepath = saves[pos]->directory();
200 filepath +=
"/mapengine.data";
205 cerr <<
"Couldn't open \"" << filepath <<
" - stopping\n" << endl;
212 if (!data::engine->get_state(in))
214 cerr <<
"Couldn't load \"" << filepath <<
" - stopping\n" << endl;
229 filepath = saves[pos]->directory();
230 filepath +=
"/audio.data";
235 cerr <<
"Couldn't open \"" << filepath <<
" - stopping\n" << endl;
242 if (!audio::get_state (in))
244 cerr <<
"Couldn't load \"" << filepath <<
" - stopping\n" << endl;
258 if (!load_characters (pos))
return false;
259 if (!load_quests (pos))
return false;
260 if (!load_mapengine (pos))
return false;
261 if (!load_audio (pos))
return false;
269 if (!quick_load || saves.size () <= 1)
return false;
275 for (vector<gamedata*>::iterator i = saves.begin (); i != saves.end (); i++)
277 if ((*i)->timestamp () > timestamp)
279 timestamp = (*i)->timestamp ();
286 return load (newest);
298 if (pos == 0)
return false;
301 if (pos >= saves.size ())
309 sprintf(t,
"%03i", pos++);
310 filepath = user_data_dir ();
311 filepath +=
"/" + game_name +
"-save-";
315 success = mkdir (filepath.c_str());
317 success = mkdir (filepath.c_str(), 0700);
323 cerr <<
"Save failed - seems like you have no write permission in\n"
324 << user_data_dir () << endl;
330 gdata =
new gamedata (filepath, desc, time);
341 filepath +=
"/character.data";
342 file.
open (filepath);
346 cerr <<
"Couldn't create \"" << filepath <<
"\" - save failed\n";
353 data::the_player->character_base::put_state (file);
357 for (itc = data::characters.begin (); itc != data::characters.end (); itc++)
360 if (itc->second == (
character*) data::the_player)
continue;
367 itc->second->character_base::put_state (file);
377 filepath +=
"/quest.data";
378 file.open (filepath);
380 if (!file.is_open ())
382 cerr <<
"Couldn't create \"" << filepath <<
"\" - save failed\n";
389 for (itq = data::quests.begin (); itq != data::quests.end (); itq++)
396 itq->second->save (file);
406 filepath +=
"/mapengine.data";
407 file.open (filepath);
409 if (!file.is_open ())
411 cerr <<
"Couldn't create \"" << filepath <<
"\" - save failed\n";
421 filepath +=
"/audio.data";
422 file.open (filepath);
424 if (!file.is_open ())
426 cerr <<
"Couldn't create \"" << filepath <<
"\" - save failed\n";
431 audio::put_state (file);
436 filepath +=
"/save.data";
438 file.open (filepath);
439 if (!file.is_open ())
441 cerr <<
"Couldn't create \"" << filepath <<
"\" - save failed\n";
449 if (pos >= saves.size ()) saves.push_back (gdata);
456 static vector<gamedata*>::iterator i = saves.begin ();
457 static u_int32 size = saves.size ();
460 if (size != saves.size ())
462 size = saves.size ();
467 if (++i == saves.end ())
481 struct dirent *dirent;
485 user_data_dir_ = udir;
486 game_data_dir_ = gdir;
491 if (chdir (game_data_dir ().c_str ()))
493 fprintf (stderr,
"Seems like %s is no valid data directory.\n", game_data_dir ().c_str ());
494 fprintf (stderr,
"Please make sure that your Adonthell installation is correct.\n");
499 gdata =
new gamedata (gdir,
"Start New Game",
"Day 0 - 00:00");
500 saves.push_back (gdata);
504 if ((dir = opendir (user_data_dir ().c_str ())) != NULL)
506 while ((dirent = readdir (dir)) != NULL)
508 string filepath = user_data_dir () +
"/";
509 filepath += dirent->d_name;
511 string name_save = game_name +
"-save-";
513 if (stat (filepath.c_str (), &statbuf) != -1 && S_ISDIR (statbuf.st_mode) &&
514 strncmp (name_save.c_str (), dirent->d_name, name_save.length ()) == 0)
517 filepath +=
"/save.data";
524 filepath = user_data_dir ();
526 filepath += dirent->d_name;
532 saves.push_back (gdata);
547 for (vector<gamedata*>::iterator i = saves.begin (); i != saves.end (); i++)
556 audio::fade_out_background (500);
560 for (itc = data::characters.begin (); itc != data::characters.end (); itc++)
562 itc->second->remove_from_map ();
565 data::characters.clear ();
567 data::the_player = NULL;
571 for (itq = data::quests.begin (); itq != data::quests.end (); itq++)
573 data::quests.clear ();
static bool save(u_int32 pos, string desc, string time)
Save a game.
Class to write data from a Gzip compressed file.
void close()
Close the file that was opened.
void set_description(string)
Sets the description for this game.
Declares the event_handler class.
static void cleanup()
Cleanup the saved game array.
Class to read data from a Gzip compressed file.
static bool load(u_int32 pos)
Loads a previously saved game.
const char * directory()
A bunch of methods to access the private attributes.
Class holding game characters.
static bool load_characters(u_int32 pos)
Load the characters state from a saved game.
gamedata()
Default constructor.
static bool init(string udir, string gdir, string gname, u_int8 qload)
Initialise the saved games array.
string get_id()
Returns an unique identifier of the character.
#define u_int32
32 bits long unsigned integer
#define u_int8
8 bits long unsigned integer
Stores objects of any kind.
static bool load_audio(u_int32 pos)
Load the audio system state from a saved game.
static gamedata * next_save()
Returns a pointer to the next saved game.
bool open(const string &fname)
Opens a file for write access.
bool open(const string &fname)
Opens a file for read access.
static bool get_version(igzstream &file, u_int16 min, u_int16 max, string name)
Declares the gamedata and data classes.
bool is_open()
Returns whether the file is opened or not.
static bool load_newest()
Loads the most recent saved game.
Defines the python class. This file is named this way so it doesn't conflicts with Python...
bool get(igzstream &)
Load a record from an opened file.
static bool load_quests(u_int32 pos)
Load the quests state from a saved game.
static void put_version(ogzstream &file, u_int16 version)
Sets the version number of a file.
s_int8 put_state(ogzstream &file)
Save the engine's state.
void set_gametime(string)
Set the in-game time of the saved game.
static void unload()
Unloads the current game, resetting the engine to it's initial state.
void put(ogzstream &)
Save a record to an opened file.
static bool load_mapengine(u_int32 pos)
Load the mapengine state from a saved game.
void set_directory(string)
Sets the directory for this game.
Contains all the attributes related to a saved game and the high level methods for loading/saving the...