bool SaveMode::_PreviewGame(int id) { std::ostringstream f; f << GetUserDataPath(true) + "saved_game_" << id << ".lua"; std::string filename = f.str(); // Check for the file existence, prevents a useless warning if (!hoa_utils::DoesFileExist(filename)) { _ClearSaveData(); return false; } ReadScriptDescriptor file, map_file; if (!file.OpenFile(filename, true)) { _ClearSaveData(); return false; } if (!file.DoesTableExist("save_game1")) { file.CloseFile(); _ClearSaveData(); return false; } // open the namespace that the save game is encapsulated in. file.OpenTable("save_game1"); std::string map_filename = file.ReadString("map_filename"); // using ints to store temp data to populate text boxes int hours, minutes, seconds, drunes; hours = file.ReadInt("play_hours"); minutes = file.ReadInt("play_minutes"); seconds = file.ReadInt("play_seconds"); drunes = file.ReadInt("drunes"); if (!file.DoesTableExist("characters")) { file.CloseFile(); _ClearSaveData(); return false; } file.OpenTable("characters"); std::vector<uint32> char_ids; file.ReadUIntVector("order", char_ids); GlobalCharacter* character[4]; // Loads only up to the first four slots (Visible battle characters) for (uint32 i = 0; i < 4 && i < char_ids.size(); ++i) { // Create a new GlobalCharacter object using the provided id // This loads all of the character's "static" data, such as their name, etc. character[i] = new GlobalCharacter(char_ids[i], false); if (!file.DoesTableExist(char_ids[i])) continue; file.OpenTable(char_ids[i]); // Read in all of the character's stats data character[i]->SetExperienceLevel(file.ReadUInt("experience_level")); character[i]->SetExperiencePoints(file.ReadUInt("experience_points")); character[i]->SetMaxHitPoints(file.ReadUInt("max_hit_points")); character[i]->SetHitPoints(file.ReadUInt("hit_points")); character[i]->SetMaxSkillPoints(file.ReadUInt("max_skill_points")); character[i]->SetSkillPoints(file.ReadUInt("skill_points")); file.CloseTable(); } file.CloseTable(); // Report any errors detected from the previous read operations if (file.IsErrorDetected()) { if (GLOBAL_DEBUG) { PRINT_WARNING << "one or more errors occurred while reading the save game file - they are listed below" << std::endl << file.GetErrorMessages() << std::endl; file.ClearErrors(); } } file.CloseFile(); // Loads the map file to get location name if (!map_file.OpenFile(map_filename)) { _ClearSaveData(); return false; } // Determine the map's tablespacename and then open it. The tablespace is the name of the map file without // file extension or path information (for example, 'dat/maps/demo.lua' has a tablespace name of 'demo'). int32 period = map_filename.find("."); int32 last_slash = map_filename.find_last_of("/"); std::string map_tablespace = map_filename.substr(last_slash + 1, period - (last_slash + 1)); map_file.OpenTable(map_tablespace); // Read the name of the map ustring map_name = MakeUnicodeString(map_file.ReadString("map_name")); map_file.CloseTable(); map_file.CloseFile(); for (uint32 i = 0; i < 4 && i < char_ids.size(); ++i) { _character_window[i].SetCharacter(character[i]); } _map_name_textbox.SetDisplayText(map_name); std::ostringstream time_text; time_text << "Time - "; time_text << (hours < 10 ? "0" : "") << static_cast<uint32>(hours) << ":"; time_text << (minutes < 10 ? "0" : "") << static_cast<uint32>(minutes) << ":"; time_text << (seconds < 10 ? "0" : "") << static_cast<uint32>(seconds); _time_textbox.SetDisplayText(time_text.str()); std::ostringstream drunes_text; drunes_text << "Drunes - " << drunes; _drunes_textbox.SetDisplayText(drunes_text.str()); return true; } // bool SaveMode::_PreviewGame(string& filename)
bool SaveMode::_PreviewGame(const std::string& filename) { // Check for the file existence, prevents a useless warning if(!vt_utils::DoesFileExist(filename)) { _ClearSaveData(false); return false; } ReadScriptDescriptor file; // Clear out the save data namespace to avoid loading false information // when dealing with a save game that has an invalid namespace ScriptManager->DropGlobalTable("save_game1"); if(!file.OpenFile(filename)) { _ClearSaveData(true); return false; } if(!file.DoesTableExist("save_game1")) { file.CloseFile(); _ClearSaveData(true); return false; } // open the namespace that the save game is encapsulated in. file.OpenTable("save_game1"); // The map file, tested after the save game is closed. std::string map_script_filename; std::string map_data_filename; map_script_filename = file.ReadString("map_script_filename"); map_data_filename = file.ReadString("map_data_filename"); // Check whether the map data file is available if (map_data_filename.empty() || !vt_utils::DoesFileExist(map_data_filename)) { file.CloseTable(); // save_game1 file.CloseFile(); _ClearSaveData(true); return false; } // Used to store temp data to populate text boxes int32_t hours = file.ReadInt("play_hours"); int32_t minutes = file.ReadInt("play_minutes"); int32_t seconds = file.ReadInt("play_seconds"); int32_t drunes = file.ReadInt("drunes"); if(!file.DoesTableExist("characters")) { file.CloseTable(); // save_game1 file.CloseFile(); _ClearSaveData(true); return false; } // Read characters table content file.OpenTable("characters"); std::vector<uint32_t> char_ids; file.ReadUIntVector("order", char_ids); // Prereserve characters slots std::vector<GlobalCharacter*> characters; characters.assign(CHARACTERS_SHOWN_SLOTS, nullptr); // Loads only up to the first four slots (Visible battle characters) for(uint32_t i = 0; i < char_ids.size() && i < CHARACTERS_SHOWN_SLOTS; ++i) { // Create a new GlobalCharacter object using the provided id // This loads all of the character's "static" data, such as their name, etc. characters[i] = nullptr; if(!file.DoesTableExist(char_ids[i])) continue; file.OpenTable(char_ids[i]); // Read in all of the character's stats data characters[i] = new GlobalCharacter(char_ids[i], false); characters[i]->SetExperienceLevel(file.ReadUInt("experience_level")); // DEPRECATED: Do not read experience_points anymore in one release uint32_t total_xp = file.ReadUInt("experience_points"); if (total_xp == 0) { total_xp = file.ReadUInt("total_experience_points"); } characters[i]->SetTotalExperiencePoints(total_xp); characters[i]->SetMaxHitPoints(file.ReadUInt("max_hit_points")); characters[i]->SetHitPoints(file.ReadUInt("hit_points")); characters[i]->SetMaxSkillPoints(file.ReadUInt("max_skill_points")); characters[i]->SetSkillPoints(file.ReadUInt("skill_points")); file.CloseTable(); // character id } file.CloseTable(); // characters // Report any errors detected from the previous read operations if(file.IsErrorDetected()) { PRINT_WARNING << "One or more errors occurred while reading the save game file - they are listed below:" << std::endl << file.GetErrorMessages() << std::endl; file.ClearErrors(); } file.CloseTable(); // save_game1 file.CloseFile(); for(uint32_t i = 0; i < CHARACTERS_SHOWN_SLOTS; ++i) { _character_window[i].SetCharacter(characters[i]); } std::ostringstream time_text; time_text << (hours < 10 ? "0" : "") << static_cast<uint32_t>(hours) << ":"; time_text << (minutes < 10 ? "0" : "") << static_cast<uint32_t>(minutes) << ":"; time_text << (seconds < 10 ? "0" : "") << static_cast<uint32_t>(seconds); _time_textbox.SetDisplayText(MakeUnicodeString(time_text.str())); std::ostringstream drunes_amount; drunes_amount << drunes; _drunes_textbox.SetDisplayText(MakeUnicodeString(drunes_amount.str())); // Test the map file // Tests the map file and gets the untranslated map hud name from it. ReadScriptDescriptor map_file; if(!map_file.OpenFile(map_script_filename)) { _ClearSaveData(true); return false; } if (map_file.OpenTablespace().empty()) { _ClearSaveData(true); map_file.CloseFile(); return false; } // Read the in-game location of the save std::string map_hud_name = map_file.ReadString("map_name"); _map_name_textbox.SetDisplayText(UTranslate(map_hud_name)); // Loads the potential location image std::string map_image_filename = map_file.ReadString("map_image_filename"); if (map_image_filename.empty()) { _location_image.Clear(); } else { if (_location_image.Load(map_image_filename)) _location_image.SetWidthKeepRatio(340.0f); } map_file.CloseTable(); // Tablespace map_file.CloseFile(); return true; }
bool SaveMode::_PreviewGame(uint32 id) { std::ostringstream f; f << GetUserDataPath() + "saved_game_" << id << ".lua"; std::string filename = f.str(); // Check for the file existence, prevents a useless warning if(!vt_utils::DoesFileExist(filename)) { _ClearSaveData(false); return false; } ReadScriptDescriptor file; // Clear out the save data namespace to avoid loading false information // when dealing with a save game that has an invalid namespace ScriptManager->DropGlobalTable("save_game1"); if(!file.OpenFile(filename)) { _ClearSaveData(true); return false; } if(!file.DoesTableExist("save_game1")) { file.CloseFile(); _ClearSaveData(true); return false; } // open the namespace that the save game is encapsulated in. file.OpenTable("save_game1"); // The map file, tested after the save game is closed. // DEPRECATED: Old way, will be removed in one release. std::string map_script_filename; std::string map_data_filename; if (file.DoesStringExist("map_filename")) { map_script_filename = file.ReadString("map_filename"); map_data_filename = file.ReadString("map_filename"); } else { map_script_filename = file.ReadString("map_script_filename"); map_data_filename = file.ReadString("map_data_filename"); } // DEPRECATED: Remove in one release // Hack to permit the split of last map data and scripts. if (!map_script_filename.empty() && map_data_filename == map_script_filename) { std::string map_common_name = map_data_filename.substr(0, map_data_filename.length() - 4); map_data_filename = map_common_name + "_map.lua"; map_script_filename = map_common_name + "_script.lua"; } // Used to store temp data to populate text boxes int32 hours = file.ReadInt("play_hours"); int32 minutes = file.ReadInt("play_minutes"); int32 seconds = file.ReadInt("play_seconds"); int32 drunes = file.ReadInt("drunes"); if(!file.DoesTableExist("characters")) { file.CloseTable(); // save_game1 file.CloseFile(); _ClearSaveData(true); return false; } file.OpenTable("characters"); std::vector<uint32> char_ids; file.ReadUIntVector("order", char_ids); GlobalCharacter *character[4]; // Loads only up to the first four slots (Visible battle characters) for(uint32 i = 0; i < 4 && i < char_ids.size(); ++i) { // Create a new GlobalCharacter object using the provided id // This loads all of the character's "static" data, such as their name, etc. character[i] = new GlobalCharacter(char_ids[i], false); if(!file.DoesTableExist(char_ids[i])) continue; file.OpenTable(char_ids[i]); // Read in all of the character's stats data character[i]->SetExperienceLevel(file.ReadUInt("experience_level")); character[i]->SetExperiencePoints(file.ReadUInt("experience_points")); character[i]->SetMaxHitPoints(file.ReadUInt("max_hit_points")); character[i]->SetHitPoints(file.ReadUInt("hit_points")); character[i]->SetMaxSkillPoints(file.ReadUInt("max_skill_points")); character[i]->SetSkillPoints(file.ReadUInt("skill_points")); file.CloseTable(); // character id } file.CloseTable(); // characters // Report any errors detected from the previous read operations if(file.IsErrorDetected()) { PRINT_WARNING << "One or more errors occurred while reading the save game file - they are listed below:" << std::endl << file.GetErrorMessages() << std::endl; file.ClearErrors(); } file.CloseTable(); // save_game1 file.CloseFile(); for(uint32 i = 0; i < 4 && i < char_ids.size(); ++i) { _character_window[i].SetCharacter(character[i]); } std::ostringstream time_text; time_text << (hours < 10 ? "0" : "") << static_cast<uint32>(hours) << ":"; time_text << (minutes < 10 ? "0" : "") << static_cast<uint32>(minutes) << ":"; time_text << (seconds < 10 ? "0" : "") << static_cast<uint32>(seconds); _time_textbox.SetDisplayText(MakeUnicodeString(time_text.str())); std::ostringstream drunes_amount; drunes_amount << drunes; _drunes_textbox.SetDisplayText(MakeUnicodeString(drunes_amount.str())); // Test the map file // Tests the map file and gets the untranslated map hud name from it. ReadScriptDescriptor map_file; if(!map_file.OpenFile(map_script_filename)) { _ClearSaveData(true); return false; } if (map_file.OpenTablespace().empty()) { _ClearSaveData(true); map_file.CloseFile(); return false; } // Read the in-game location of the save std::string map_hud_name = map_file.ReadString("map_name"); _map_name_textbox.SetDisplayText(UTranslate(map_hud_name)); // Loads the potential location image std::string map_image_filename = map_file.ReadString("map_image_filename"); if (map_image_filename.empty()) { _location_image.Clear(); } else { if (_location_image.Load(map_image_filename)) _location_image.SetWidthKeepRatio(340.0f); } map_file.CloseTable(); // Tablespace map_file.CloseFile(); return true; } // bool SaveMode::_PreviewGame(string& filename)
void SaveMode::Update() { if(InputManager->QuitPress()) { ModeManager->Pop(); return; } // Screen is in the process of fading out, in order to load a game if(_current_state == SAVE_MODE_FADING_OUT) { return; } _file_list.Update(); _confirm_save_optionbox.Update(); _load_auto_save_optionbox.Update(); GlobalMedia& media = GlobalManager->Media(); // Otherwise, it's time to start handling events. if(InputManager->ConfirmPress()) { media.PlaySound("confirm"); switch(_current_state) { case SAVE_MODE_SAVING: if(_file_list.GetSelection() > -1) { _current_state = SAVE_MODE_CONFIRMING_SAVE; } break; case SAVE_MODE_CONFIRMING_SAVE: if(_confirm_save_optionbox.GetSelection() == 0) { uint32_t id = static_cast<uint32_t>(_file_list.GetSelection()); uint32_t stamina = MapMode::CurrentInstance() ? MapMode::CurrentInstance()->GetStamina() : 0; GlobalManager->GetMapData().SetSaveStamina(stamina); // Attempt to save the game if(GlobalManager->SaveGame(_BuildSaveFilename(id), id, _x_position, _y_position)) { _current_state = SAVE_MODE_SAVE_COMPLETE; AudioManager->PlaySound("data/sounds/save_successful_nick_bowler_oga.wav"); // Remove the autosave in that case. _DeleteAutoSave(id); } else { _current_state = SAVE_MODE_SAVE_FAILED; AudioManager->PlaySound("data/sounds/cancel.wav"); } } else { _current_state = SAVE_MODE_SAVING; } break; case SAVE_MODE_SAVE_COMPLETE: case SAVE_MODE_SAVE_FAILED: _current_state = SAVE_MODE_SAVING; _PreviewGame(_BuildSaveFilename(_file_list.GetSelection())); break; case SAVE_MODE_CONFIRM_AUTOSAVE: switch (_load_auto_save_optionbox.GetSelection()) { case 0: // Load autosave _LoadGame(_BuildSaveFilename(_file_list.GetSelection(), true)); break; case 1: // Load save _LoadGame(_BuildSaveFilename(_file_list.GetSelection())); break; case 2: // Cancel default: _current_state = SAVE_MODE_LOADING; } break; case SAVE_MODE_LOADING: if(_file_list.GetSelection() > -1) { // Check whether a more recent autosave file exists uint32_t id = static_cast<uint32_t>(_file_list.GetSelection()); if (_IsAutoSaveValid(id)) { _current_state = SAVE_MODE_CONFIRM_AUTOSAVE; } else { _LoadGame(_BuildSaveFilename(id)); } } else { // Leave right away where there is nothing else // to do than loading. ModeManager->Pop(); } break; default: case SAVE_MODE_NO_VALID_SAVES: // Leave right away as there is nothing else to do ModeManager->Pop(); break; } } else if(InputManager->CancelPress()) { media.PlaySound("cancel"); switch(_current_state) { default: case SAVE_MODE_NO_VALID_SAVES: case SAVE_MODE_SAVING: case SAVE_MODE_LOADING: // Leave right away where there is nothing else to do ModeManager->Pop(); break; case SAVE_MODE_CONFIRM_AUTOSAVE: _current_state = SAVE_MODE_LOADING; _PreviewGame(_BuildSaveFilename(_file_list.GetSelection())); break; case SAVE_MODE_CONFIRMING_SAVE: _current_state = SAVE_MODE_SAVING; _PreviewGame(_BuildSaveFilename(_file_list.GetSelection())); break; } } else if(InputManager->UpPress()) { media.PlaySound("bump"); switch(_current_state) { case SAVE_MODE_SAVING: case SAVE_MODE_LOADING: _file_list.InputUp(); if(_file_list.GetSelection() > -1) { _PreviewGame(_BuildSaveFilename(_file_list.GetSelection())); } else { _ClearSaveData(false); } break; case SAVE_MODE_CONFIRMING_SAVE: _confirm_save_optionbox.InputUp(); break; case SAVE_MODE_CONFIRM_AUTOSAVE: _load_auto_save_optionbox.InputUp(); break; } } else if(InputManager->DownPress()) { media.PlaySound("bump"); switch(_current_state) { case SAVE_MODE_SAVING: case SAVE_MODE_LOADING: _file_list.InputDown(); if(_file_list.GetSelection() > -1) { _PreviewGame(_BuildSaveFilename(_file_list.GetSelection())); } else { _ClearSaveData(false); } break; case SAVE_MODE_CONFIRMING_SAVE: _confirm_save_optionbox.InputDown(); break; case SAVE_MODE_CONFIRM_AUTOSAVE: _load_auto_save_optionbox.InputDown(); break; } } }
void SaveMode::Update() { if(InputManager->QuitPress() == true) { ModeManager->Pop(); return; } // Screen is in the process of fading out, in order to load a game if(_current_state == SAVE_MODE_FADING_OUT) { return; } _file_list.Update(); _confirm_save_optionbox.Update(); // Otherwise, it's time to start handling events. if(InputManager->ConfirmPress()) { switch(_current_state) { case SAVE_MODE_SAVING: if(_file_list.GetSelection() > -1) { _current_state = SAVE_MODE_CONFIRMING_SAVE; } break; case SAVE_MODE_CONFIRMING_SAVE: if(_confirm_save_optionbox.GetSelection() == 0) { // note: using int here, because uint8 will NOT work // do not change unless you understand this and can test it properly! uint32 id = (uint32)_file_list.GetSelection(); std::ostringstream f; f << GetUserDataPath() + "saved_game_" << id << ".lua"; std::string filename = f.str(); // now, attempt to save the game. If failure, we need to tell the user that! if(GlobalManager->SaveGame(filename, id, _x_position, _y_position)) { _current_state = SAVE_MODE_SAVE_COMPLETE; AudioManager->PlaySound("snd/save_successful_nick_bowler_oga.wav"); } else { _current_state = SAVE_MODE_SAVE_FAILED; AudioManager->PlaySound("snd/cancel.wav"); } } else { _current_state = SAVE_MODE_SAVING; } break; case SAVE_MODE_SAVE_COMPLETE: case SAVE_MODE_SAVE_FAILED: _current_state = SAVE_MODE_SAVING; _PreviewGame(_file_list.GetSelection()); break; case SAVE_MODE_LOADING: if(_file_list.GetSelection() > -1) { _LoadGame(_file_list.GetSelection()); } else { // Leave right away where there is nothing else // to do than loading. ModeManager->Pop(); } break; default: case SAVE_MODE_NO_VALID_SAVES: // Leave right away as there is nothing else to do ModeManager->Pop(); break; } // end switch (_current_state) } // end if (InputManager->ConfirmPress()) else if(InputManager->CancelPress()) { switch(_current_state) { default: case SAVE_MODE_NO_VALID_SAVES: case SAVE_MODE_SAVING: case SAVE_MODE_LOADING: // Leave right away where there is nothing else to do ModeManager->Pop(); break; case SAVE_MODE_CONFIRMING_SAVE: _current_state = SAVE_MODE_SAVING; _PreviewGame(_file_list.GetSelection()); break; } // end switch (_current_state) } // end if (InputManager->CancelPress()) else if(InputManager->UpPress()) { switch(_current_state) { case SAVE_MODE_SAVING: case SAVE_MODE_LOADING: _file_list.InputUp(); if(_file_list.GetSelection() > -1) { _PreviewGame(_file_list.GetSelection()); } else { _ClearSaveData(false); } break; case SAVE_MODE_CONFIRMING_SAVE: _confirm_save_optionbox.InputUp(); break; } // end switch (_current_state) } // end if (InputManager->UpPress()) else if(InputManager->DownPress()) { switch(_current_state) { case SAVE_MODE_SAVING: case SAVE_MODE_LOADING: _file_list.InputDown(); if(_file_list.GetSelection() > -1) { _PreviewGame(_file_list.GetSelection()); } else { _ClearSaveData(false); } break; case SAVE_MODE_CONFIRMING_SAVE: _confirm_save_optionbox.InputDown(); break; } // end switch (_current_state) } // end if (InputManager->DownPress()) } // void SaveMode::Update()