From afbf5c6e5804ef5a31a40b4bcb59a41f389e5a40 Mon Sep 17 00:00:00 2001 From: gfgtdf Date: Sun, 1 Jun 2014 23:45:12 +0200 Subject: [PATCH] remove resources::state_of_game we don't want to expose the fileds "snapshot", "carryover_sides". instead we only add classification, and mp_game_setting to resources which are members of game_state. in order to do that, we have to add a do_autosave method to play_controller. this commit also moves some code from game_state::write_snapshot to play_controller::to_config which was intended to be in a different commit. --- src/ai/default/ai.cpp | 2 +- src/ai/testing/ca.cpp | 2 +- src/editor/editor_controller.cpp | 4 +++- src/editor/map/context_manager.cpp | 4 +++- src/game_events/action_wml.cpp | 10 ++++----- src/gamestatus.cpp | 33 +++------------------------ src/gamestatus.hpp | 2 +- src/play_controller.cpp | 31 ++++++++++++++++++++++--- src/play_controller.hpp | 2 ++ src/playmp_controller.cpp | 2 +- src/playsingle_controller.cpp | 2 +- src/playturn.cpp | 5 ++--- src/replay_controller.cpp | 2 +- src/resources.cpp | 2 ++ src/resources.hpp | 36 ++++++++++++++++-------------- src/savegame.cpp | 6 ++--- src/savegame.hpp | 2 +- src/scripting/lua.cpp | 12 +++++----- src/synced_checkup.hpp | 2 +- src/synced_context.cpp | 2 +- 20 files changed, 86 insertions(+), 77 deletions(-) diff --git a/src/ai/default/ai.cpp b/src/ai/default/ai.cpp index 5fc5e7afbee9..f7d1f46f7838 100644 --- a/src/ai/default/ai.cpp +++ b/src/ai/default/ai.cpp @@ -250,7 +250,7 @@ bool ai_default_recruitment_stage::recruit_usage(const std::string& usage) //FIXME: This message should be suppressed when WML author //chooses the default recruitment pattern. const std::string warning = "At difficulty level " + - resources::state_of_game->classification().difficulty + ", trying to recruit a:" + + resources::classification->difficulty + ", trying to recruit a:" + usage + " but no unit of that type (usage=) is" " available. Check the recruit and [ai]" " recruitment_pattern keys for team '" + diff --git a/src/ai/testing/ca.cpp b/src/ai/testing/ca.cpp index eb42ba96f939..038eaf2112ac 100644 --- a/src/ai/testing/ca.cpp +++ b/src/ai/testing/ca.cpp @@ -362,7 +362,7 @@ bool recruitment_phase::recruit_usage(const std::string& usage) //FIXME: This message should be suppressed when WML author //chooses the default recruitment pattern. const std::string warning = "At difficulty level " + - resources::state_of_game->classification().difficulty + ", trying to recruit a:" + + resources::classification->difficulty + ", trying to recruit a:" + usage + " but no unit of that type (usage=) is" " available. Check the recruit and [ai]" " recruitment_pattern keys for team '" + diff --git a/src/editor/editor_controller.cpp b/src/editor/editor_controller.cpp index 34c1b8d1861c..6008d57366ac 100644 --- a/src/editor/editor_controller.cpp +++ b/src/editor/editor_controller.cpp @@ -159,7 +159,9 @@ editor_controller::~editor_controller() resources::units = NULL; resources::tod_manager = NULL; resources::teams = NULL; - resources::state_of_game = NULL; + + resources::classification = NULL; + resources::mp_settings = NULL; } EXIT_STATUS editor_controller::main_loop() diff --git a/src/editor/map/context_manager.cpp b/src/editor/map/context_manager.cpp index bebef090f146..b0ccda3f74d2 100644 --- a/src/editor/map/context_manager.cpp +++ b/src/editor/map/context_manager.cpp @@ -86,8 +86,10 @@ class map_context_refresher context_manager_.gui().replace_overlay_map(&context_manager_.get_map_context().get_overlays()); resources::teams = &context_manager_.get_map_context().get_teams(); + + resources::classification = &context_manager_.get_map_context().get_game_state().classification(); + resources::mp_settings = &context_manager_.get_map_context().get_game_state().mp_settings(); - resources::state_of_game = &context_manager_.get_map_context().get_game_state(); context_manager_.gui().init_flags(); context_manager_.reload_map(); diff --git a/src/game_events/action_wml.cpp b/src/game_events/action_wml.cpp index aee477670b58..1bd1c3db1609 100644 --- a/src/game_events/action_wml.cpp +++ b/src/game_events/action_wml.cpp @@ -749,7 +749,7 @@ WML_HANDLER_FUNCTION(endlevel, /*event_info*/, cfg) } data.transient.disabled = true; - game_state *state_of_game = resources::state_of_game; + game_classification &classification = *resources::classification; unit_map *units = resources::units; // Remove 0-hp units from the unit map to avoid the following problem: @@ -781,17 +781,17 @@ WML_HANDLER_FUNCTION(endlevel, /*event_info*/, cfg) std::string end_of_campaign_text = cfg["end_text"]; if (!end_of_campaign_text.empty()) { - state_of_game->classification().end_text = end_of_campaign_text; + classification.end_text = end_of_campaign_text; } config::attribute_value end_of_campaign_text_delay = cfg["end_text_duration"]; if (!end_of_campaign_text_delay.empty()) { - state_of_game->classification().end_text_duration = - end_of_campaign_text_delay.to_int(state_of_game->classification().end_text_duration); + classification.end_text_duration = + end_of_campaign_text_delay.to_int(classification.end_text_duration); } if(cfg.has_attribute("end_credits")) { - state_of_game->classification().end_credits = cfg["end_credits"].to_bool(true); + classification.end_credits = cfg["end_credits"].to_bool(true); } diff --git a/src/gamestatus.cpp b/src/gamestatus.cpp index 77446618b224..5d39608096ef 100644 --- a/src/gamestatus.cpp +++ b/src/gamestatus.cpp @@ -26,7 +26,6 @@ #include "filesystem.hpp" #include "formula_string_utils.hpp" #include "game_config.hpp" -#include "game_events/handlers.hpp" #include "game_preferences.hpp" #include "gettext.hpp" #include "log.hpp" @@ -764,44 +763,18 @@ void convert_old_saves(config& cfg){ LOG_RG<<"cfg after conversion "<playing_team()); + cfg.merge_attributes(classification_.to_config()); - game_events::write_events(cfg); - - sound::write_music_play_list(cfg); - } - - cfg["label"] = classification_.label; - cfg["abbrev"] = classification_.abbrev; - cfg["version"] = game_config::version; - - cfg["completion"] = classification_.completion; - - cfg["campaign"] = classification_.campaign; - cfg["campaign_type"] = lexical_cast (classification_.campaign_type); - - cfg["campaign_define"] = classification_.campaign_define; - cfg["campaign_extra_defines"] = utils::join(classification_.campaign_xtra_defines); + //TODO: move id_manager handling to play_controller cfg["next_underlying_unit_id"] = str_cast(n_unit::id_manager::instance().get_save_id()); - cfg["end_credits"] = classification_.end_credits; - cfg["end_text"] = classification_.end_text; - cfg["end_text_duration"] = str_cast(classification_.end_text_duration); - - cfg["difficulty"] = classification_.difficulty; - cfg["random_mode"] = classification_.random_mode; - if(resources::gamedata != NULL){ resources::gamedata->write_snapshot(cfg); } - if(gui != NULL){ - gui->labels().write(cfg); - } } void extract_summary_from_config(config& cfg_save, config& cfg_summary) diff --git a/src/gamestatus.hpp b/src/gamestatus.hpp index 58aa5341ded1..b76a32343f2b 100644 --- a/src/gamestatus.hpp +++ b/src/gamestatus.hpp @@ -174,7 +174,7 @@ class game_state game_state& operator=(const game_state& state); //write the gamestate into a config object - void write_snapshot(config& cfg, game_display* gui = NULL) const; + void write_snapshot(config& cfg) const; //write the config information into a stream (file) void write_config(config_writer& out) const; diff --git a/src/play_controller.cpp b/src/play_controller.cpp index 212b2d380124..1f4775aa1643 100644 --- a/src/play_controller.cpp +++ b/src/play_controller.cpp @@ -30,6 +30,7 @@ #include "game_events/menu_item.hpp" #include "game_events/pump.hpp" #include "game_preferences.hpp" +#include "map_label.hpp" #include "gettext.hpp" #include "halo.hpp" #include "loadscreen.hpp" @@ -78,13 +79,17 @@ static void clear_resources() resources::persist = NULL; resources::screen = NULL; resources::soundsources = NULL; - resources::state_of_game = NULL; resources::teams = NULL; resources::tod_manager = NULL; resources::tunnels = NULL; resources::undo_stack = NULL; resources::units = NULL; resources::whiteboard = NULL; + + + resources::classification = NULL; + resources::mp_settings = NULL; + } @@ -140,12 +145,15 @@ play_controller::play_controller(const config& level, game_state& state_of_game, resources::gamedata = &gamedata_; resources::game_map = &gameboard_.map_; resources::persist = &persist_; - resources::state_of_game = &gamestate_; resources::teams = &gameboard_.teams_; resources::tod_manager = &tod_manager_; resources::undo_stack = undo_stack_.get(); resources::units = &gameboard_.units_; + + resources::classification = &gamestate_.classification(); + resources::mp_settings = &gamestate_.mp_settings(); + persist_.start_transaction(); // Setup victory and defeat music @@ -762,6 +770,15 @@ config play_controller::to_config() const // Preserve the undo stack so that fog/shroud clearing is kept accurate. undo_stack_->write(cfg.add_child("undo_stack")); + //Write the game events. + game_events::write_events(cfg); + + + if(gui_.get() != NULL){ + cfg["playing_team"] = str_cast(gui_->playing_team()); + gui_->labels().write(cfg); + sound::write_music_play_list(cfg); + } return cfg; } @@ -1464,7 +1481,7 @@ void play_controller::process_oos(const std::string& msg) const message << _("The game is out of sync. It might not make much sense to continue. Do you want to save your game?"); message << "\n\n" << _("Error details:") << "\n\n" << msg; - savegame::oos_savegame save(to_config()); + savegame::oos_savegame save(gamestate_, *gui_, to_config()); save.save_game_interactive(resources::screen->video(), message.str(), gui::YES_NO); // can throw end_level_exception } @@ -1496,3 +1513,11 @@ void play_controller::toggle_accelerated_speed() gui_->announce(_("Accelerated speed disabled!"), font::NORMAL_COLOR); } } + +void play_controller::do_autosave() +{ + savegame::autosave_savegame save(gamestate_, *gui_, to_config(), preferences::save_compression_format()); + save.autosave(false, preferences::autosavemax(), preferences::INFINITE_AUTO_SAVES); +} + + diff --git a/src/play_controller.hpp b/src/play_controller.hpp index 6798f0416247..1d5673493847 100644 --- a/src/play_controller.hpp +++ b/src/play_controller.hpp @@ -163,6 +163,8 @@ class play_controller : public controller_base, public events::observer, public bool is_skipping_replay() const { return skip_replay_;} bool is_linger_mode() const { return linger_; } + void do_autosave(); + events::mouse_handler& get_mouse_handler_base(); events::menu_handler& get_menu_handler() { return menu_handler_; } diff --git a/src/playmp_controller.cpp b/src/playmp_controller.cpp index 603920f034e3..dc06fd58efcf 100644 --- a/src/playmp_controller.cpp +++ b/src/playmp_controller.cpp @@ -505,7 +505,7 @@ void playmp_controller::process_oos(const std::string& err_msg) const { temp_buf << " \n"; } - savegame::oos_savegame save(to_config()); + savegame::oos_savegame save(gamestate_, *gui_, to_config()); save.save_game_interactive(resources::screen->video(), temp_buf.str(), gui::YES_NO); } diff --git a/src/playsingle_controller.cpp b/src/playsingle_controller.cpp index 6f0534456874..7518d4335937 100644 --- a/src/playsingle_controller.cpp +++ b/src/playsingle_controller.cpp @@ -383,7 +383,7 @@ LEVEL_RESULT playsingle_controller::play_scenario( // At the beginning of the scenario, save a snapshot as replay_start if(gamestate_.snapshot.child_or_empty("variables")["turn_number"].to_int(-1)<1){ gamestate_.replay_start() = to_config(); - gamestate_.write_snapshot(gamestate_.replay_start(), gui_.get()); + gamestate_.write_snapshot(gamestate_.replay_start()); } fire_preload(); diff --git a/src/playturn.cpp b/src/playturn.cpp index 8b00bad2f62b..ce679ba37721 100644 --- a/src/playturn.cpp +++ b/src/playturn.cpp @@ -97,9 +97,8 @@ turn_info::PROCESS_DATA_RESULT turn_info::handle_turn( void turn_info::do_save() { - if ((resources::state_of_game != NULL) && (resources::screen != NULL) && (resources::controller != NULL)) { - savegame::autosave_savegame save(*resources::state_of_game, *resources::screen, resources::controller->to_config(), preferences::save_compression_format()); - save.autosave(false, preferences::autosavemax(), preferences::INFINITE_AUTO_SAVES); + if (resources::controller != NULL) { + resources::controller->do_autosave(); } } diff --git a/src/replay_controller.cpp b/src/replay_controller.cpp index 1dc42df430ac..c4b329acf6a1 100644 --- a/src/replay_controller.cpp +++ b/src/replay_controller.cpp @@ -367,7 +367,7 @@ void replay_controller::process_oos(const std::string& msg) const if (non_interactive()) { throw game::game_error(message.str()); //throw end_level_exception(DEFEAT); } else { - savegame::oos_savegame save(to_config()); + savegame::oos_savegame save(gamestate_, *gui_, to_config()); save.save_game_interactive(resources::screen->video(), message.str(), gui::YES_NO); // can throw end_level_exception } } diff --git a/src/resources.cpp b/src/resources.cpp index ae8c8d34d12f..da7357a1ec0e 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -33,4 +33,6 @@ namespace resources actions::undo_list *undo_stack = NULL; unit_map *units = NULL; wb::manager *whiteboard = NULL; + game_classification *classification = NULL; + const mp_game_settings *mp_settings = NULL; } diff --git a/src/resources.hpp b/src/resources.hpp index 4192482f4d84..f7230b623c1e 100644 --- a/src/resources.hpp +++ b/src/resources.hpp @@ -29,7 +29,8 @@ class team; class tod_manager; class unit_map; class persist_manager; - +class game_classification; +struct mp_game_settings; namespace actions { class undo_list; } namespace soundsource { class manager; } @@ -40,22 +41,23 @@ namespace wb { class manager; } //whiteboard manager namespace resources { - extern game_config_manager *config_manager; - extern play_controller *controller; - extern game_board *gameboard; - extern game_data *gamedata; - extern gamemap *game_map; - extern LuaKernel *lua_kernel; // Set by game_events::manager. - extern persist_manager *persist; - extern game_display *screen; - extern soundsource::manager *soundsources; - extern game_state *state_of_game; - extern std::vector *teams; - extern ::tod_manager *tod_manager; - extern pathfind::manager *tunnels; - extern actions::undo_list *undo_stack; - extern unit_map *units; - extern wb::manager *whiteboard; + extern game_config_manager *config_manager; + extern play_controller *controller; + extern game_board *gameboard; + extern game_data *gamedata; + extern gamemap *game_map; + extern LuaKernel *lua_kernel; // Set by game_events::manager. + extern persist_manager *persist; + extern game_classification *classification; + extern game_display *screen; + extern const mp_game_settings *mp_settings; + extern soundsource::manager *soundsources; + extern std::vector *teams; + extern ::tod_manager *tod_manager; + extern pathfind::manager *tunnels; + extern actions::undo_list *undo_stack; + extern unit_map *units; + extern wb::manager *whiteboard; } #endif diff --git a/src/savegame.cpp b/src/savegame.cpp index 878e136875fe..7f0bdb11b039 100644 --- a/src/savegame.cpp +++ b/src/savegame.cpp @@ -1041,8 +1041,8 @@ void autosave_savegame::create_filename() set_filename(filename); } -oos_savegame::oos_savegame(const config& snapshot_cfg) - : ingame_savegame(*resources::state_of_game, *resources::screen, snapshot_cfg, preferences::save_compression_format()) +oos_savegame::oos_savegame(game_state& gamestate, game_display& gui, const config& snapshot_cfg) + : ingame_savegame(gamestate, gui, snapshot_cfg, preferences::save_compression_format()) {} int oos_savegame::show_save_dialog(CVideo& video, const std::string& message, const gui::DIALOG_TYPE /*dialog_type*/) @@ -1085,7 +1085,7 @@ void ingame_savegame::create_filename() void ingame_savegame::before_save() { savegame::before_save(); - gamestate().write_snapshot(snapshot(), &gui_); + gamestate().write_snapshot(snapshot()); } void ingame_savegame::write_game(config_writer &out) { diff --git a/src/savegame.hpp b/src/savegame.hpp index d21dcff0796a..cbc128c0f38a 100644 --- a/src/savegame.hpp +++ b/src/savegame.hpp @@ -280,7 +280,7 @@ class autosave_savegame : public ingame_savegame class oos_savegame : public ingame_savegame { public: - oos_savegame(const config& snapshot_cfg); + oos_savegame(game_state& gamestate, game_display& gui, const config& snapshot_cfg); private: /** Display the save game dialog. */ diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 1bd645caa7e0..a9c6891746f0 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -1537,12 +1537,14 @@ static int impl_game_config_get(lua_State *L) return_bool_attrib("debug", game_config::debug); return_bool_attrib("debug_lua", game_config::debug_lua); return_bool_attrib("mp_debug", game_config::mp_debug); + + const mp_game_settings& mp_settings = *resources::mp_settings; + const game_classification & classification = *resources::classification; - const game_state & game_state_ = *resources::state_of_game; - return_string_attrib("campaign_type", lexical_cast(game_state_.classification().campaign_type)); - if(game_state_.classification().campaign_type==game_classification::MULTIPLAYER) { - return_cfgref_attrib("mp_settings", game_state_.mp_settings().to_config()); - return_cfgref_attrib("era", resources::config_manager->game_config().find_child("era","id",game_state_.mp_settings().mp_era)); + return_string_attrib("campaign_type", lexical_cast(classification.campaign_type)); + if(classification.campaign_type==game_classification::MULTIPLAYER) { + return_cfgref_attrib("mp_settings", mp_settings.to_config()); + return_cfgref_attrib("era", resources::config_manager->game_config().find_child("era","id",mp_settings.mp_era)); //^ finds the era with name matching mp_era, and creates a lua reference from the config of that era. //This code for SigurdFD, not the cleanest implementation but seems to work just fine. diff --git a/src/synced_checkup.hpp b/src/synced_checkup.hpp index fc27a30d8e04..f767106c5b29 100644 --- a/src/synced_checkup.hpp +++ b/src/synced_checkup.hpp @@ -20,7 +20,7 @@ struct map_location; a class to check whether calculated ingame results match the results calculated during the original game. note, that you shouldn't add new checkups to existent user actions or you might break replay compability by bringing the [checkups] tag of older saves in unorder. - so if you really want to add new checkups, you should wrap your checkup_instance->... call in a if(resources::state_of_game->classification().version ....) or similar. + so if you really want to add new checkups, you should wrap your checkup_instance->... call in a if(resources::state_of_game->classification.version ....) or similar. */ class checkup { diff --git a/src/synced_context.cpp b/src/synced_context.cpp index 0411e7ecb669..dd2a979d6ef4 100644 --- a/src/synced_context.cpp +++ b/src/synced_context.cpp @@ -210,7 +210,7 @@ void synced_context::pull_remote_user_input() boost::shared_ptr synced_context::get_rng_for_action() { - const std::string& mode = resources::state_of_game->classification().random_mode; + const std::string& mode = resources::classification->random_mode; if(mode == "deterministic") { return boost::shared_ptr(new random_new::rng_deterministic(resources::gamedata->rng()));