Autosave now happens at start of every third turn, and placed in menu.
Loading a non-autosave file cleans them up.
This commit is contained in:
Rusty Russell 2006-05-20 11:14:24 +00:00
parent 15e945eeeb
commit 19d075af1e
10 changed files with 87 additions and 16 deletions

View file

@ -79,7 +79,7 @@ height=768
id=menu-main
title= _ "Menu"
image=lite
items=objectives,statistics,unitlist,separator,statustable,save,load,preferences,chatlog,help,quit
items=objectives,statistics,unitlist,separator,statustable,save,load,preferences,chatlog,AUTOSAVES,help,quit
ref=top-panel
rect="=+3,=+1,+100,=-4"
xanchor=fixed

View file

@ -706,6 +706,16 @@ bool game_controller::load_game()
}
}
if (game.compare(0,strlen(_("Auto-Save")),_("Auto-Save")) != 0) {
// Not loading an Auto-Save? Delete all auto-saves.
std::vector<save_info> games = get_saves_list();
for (std::vector<save_info>::iterator i = games.begin(); i != games.end(); i++) {
if (i->name.compare(0,strlen(_("Auto-Save")),_("Auto-Save")) == 0) {
delete_game(i->name);
}
}
}
return true;
}

View file

@ -20,6 +20,7 @@
#include "events.hpp"
#include "hotkeys.hpp"
#include "game_config.hpp"
#include "game_errors.hpp"
#include "gettext.hpp"
#include "menu_events.hpp"
#include "preferences_display.hpp"
@ -800,7 +801,7 @@ void execute_command(display& disp, HOTKEY_COMMAND command, command_executor* ex
}
}
void command_executor::show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool /*context_menu*/, display& gui)
void command_executor::show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool /*context_menu*/, display& gui, const std::vector<std::string>& savenames)
{
std::vector<std::string> items = items_arg;
if (can_execute_command(hotkey::get_hotkey(items.front()).get_id())){
@ -819,6 +820,11 @@ void command_executor::show_menu(const std::vector<std::string>& items_arg, int
return;
const hotkey::HOTKEY_COMMAND cmd = hotkey::get_hotkey(items[res]).get_id();
// Fake entries: they want us to load a specific autosave.
if (cmd == hotkey::HOTKEY_NULL && res < savenames.size() && !savenames[(unsigned)res].empty()) {
throw game::load_game_exception(savenames[(unsigned)res],false);
}
hotkey::execute_command(gui,cmd,this);
}
}
@ -846,7 +852,11 @@ std::vector<std::string> command_executor::get_menu_images(const std::vector<std
str << IMAGE_PREFIX << img << COLUMN_SEPARATOR;
}
str << hk.get_description() << COLUMN_SEPARATOR << hk.get_name();
if (hk.get_id() == hotkey::HOTKEY_NULL) {
str << *i << COLUMN_SEPARATOR;
} else {
str << hk.get_description() << COLUMN_SEPARATOR << hk.get_name();
}
result.push_back(str.str());
}

View file

@ -222,7 +222,7 @@ public:
//Returns a vector of images for a given menu
std::vector<std::string> get_menu_images(const std::vector<std::string>& items_arg);
void show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& gui);
void show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& gui,const std::vector<std::string>& savenames = std::vector<std::string>());
virtual bool can_execute_command(HOTKEY_COMMAND command) const = 0;
};

View file

@ -464,6 +464,21 @@ namespace events{
gui_->labels().write(start);
}
void menu_handler::autosave(unsigned turn, const config &starting_pos) const
{
// We save at the start of every third turn.
if (turn % 3 == 1) {
config snapshot;
write_game_snapshot(snapshot);
try {
recorder.save_game(_("Auto-Save") + lexical_cast<std::string>(turn), snapshot, starting_pos);
} catch(game::save_game_failed&) {
gui::show_dialog(*gui_,NULL,"",_("Could not auto save the game. Please save the game manually."),gui::MESSAGE);
//do not bother retrying, since the user can just save the game
}
}
}
void menu_handler::load_game(){
bool show_replay = false;
const std::string game = dialogs::load_game_dialog(*gui_, game_config_, gameinfo_, &show_replay);
@ -1064,18 +1079,8 @@ namespace events{
clear_undo_stack(team_num);
gui_->set_route(NULL);
//auto-save
config snapshot;
write_game_snapshot(snapshot);
try {
recorder.save_game(_("Auto-Save"), snapshot, gamestate_.starting_pos);
} catch(game::save_game_failed&) {
gui::show_dialog(*gui_,NULL,"",_("Could not auto save the game. Please save the game manually."),gui::MESSAGE);
//do not bother retrying, since the user can just save the game
}
recorder.end_turn();
recorder.end_turn();
return true;
}

View file

@ -108,6 +108,8 @@ public:
void do_search(const std::string& new_search);
void do_command(const std::string& str, const unsigned int team_num, mouse_handler& mousehandler);
void clear_undo_stack(const unsigned int team_num);
void autosave(unsigned turn, const config &starting_pos) const;
protected:
void add_chat_message(const std::string& speaker, int side, const std::string& message, display::MESSAGE_TYPE type=display::MESSAGE_PRIVATE);
void send_chat_message(const std::string& message, bool allies_only=false);

View file

@ -608,11 +608,44 @@ void play_controller::play_slice()
}
}
std::vector<std::string> play_controller::expand_menu(std::vector<std::string>& items)
{
std::vector<std::string> savenames;
for (unsigned int i = 0; i < items.size(); i++) {
if (items[i] == "AUTOSAVES") {
items.erase(items.begin() + i);
std::vector<std::string> newitems;
for (unsigned int turn = status_.turn(); turn != 0; turn--) {
std::string name = _("Auto-Save") + lexical_cast<std::string>(turn);
if (save_game_exists(name)) {
savenames.push_back(name);
if (turn == 1) {
newitems.push_back(_("Back to start"));
} else {
newitems.push_back(_("Back to turn ") + lexical_cast<std::string>(turn));
}
if (newitems.size() == 5)
break;
}
}
items.insert(items.begin()+i, newitems.begin(), newitems.end());
break;
}
savenames.push_back("");
}
return savenames;
}
void play_controller::show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu)
{
std::vector<std::string> items = items_arg;
hotkey::HOTKEY_COMMAND command;
for(std::vector<std::string>::iterator i = items.begin(); i != items.end();){
if (*i == "AUTOSAVES") {
i++;
continue;
}
command = hotkey::get_hotkey(*i).get_id();
if (!can_execute_command(command) || (context_menu && !in_context_menu(command))){
i = items.erase(i);
@ -620,10 +653,11 @@ void play_controller::show_menu(const std::vector<std::string>& items_arg, int x
else{ i++; }
}
std::vector<std::string> savenames = expand_menu(items);
if(items.empty())
return;
command_executor::show_menu(items, xloc, yloc, context_menu, *gui_);
command_executor::show_menu(items, xloc, yloc, context_menu, *gui_, savenames);
}
// Indicates whether the command should be in the context menu or not.

View file

@ -136,6 +136,10 @@ protected:
unsigned int start_turn_;
bool skip_replay_;
bool browse_;
private:
// Expand AUTOSAVES in the menu items, returning real names.
std::vector<std::string> expand_menu(std::vector<std::string>& items);
};

View file

@ -13,6 +13,8 @@
#include "playmp_controller.hpp"
#include "game_errors.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "sound.hpp"
@ -65,6 +67,8 @@ void playmp_controller::before_human_turn(){
turn_data_ = new turn_info(gameinfo_,gamestate_,status_,
*gui_,map_,teams_,player_number_,units_,replay_sender_);
menu_handler_.autosave(status_.turn(), gamestate_.starting_pos);
}
void playmp_controller::play_human_turn(){

View file

@ -416,6 +416,8 @@ void playsingle_controller::before_human_turn(){
gui_->draw();
gui_->update_display();
menu_handler_.autosave(status_.turn(), gamestate_.starting_pos);
if(preferences::turn_bell()) {
sound::play_sound(game_config::sounds::turn_bell);
}