Skip to content

Commit

Permalink
remove resources::state_of_game
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gfgtdf committed Jun 1, 2014
1 parent 83acdf2 commit afbf5c6
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/ai/default/ai.cpp
Expand Up @@ -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 '" +
Expand Down
2 changes: 1 addition & 1 deletion src/ai/testing/ca.cpp
Expand Up @@ -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 '" +
Expand Down
4 changes: 3 additions & 1 deletion src/editor/editor_controller.cpp
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion src/editor/map/context_manager.cpp
Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions src/game_events/action_wml.cpp
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
}


Expand Down
33 changes: 3 additions & 30 deletions src/gamestatus.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -764,44 +763,18 @@ void convert_old_saves(config& cfg){
LOG_RG<<"cfg after conversion "<<cfg<<"\n";
}

void game_state::write_snapshot(config& cfg, game_display* gui) const
void game_state::write_snapshot(config& cfg) const
{
log_scope("write_game");
if(gui != NULL){
cfg["playing_team"] = str_cast(gui->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<std::string> (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<unsigned int>(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)
Expand Down
2 changes: 1 addition & 1 deletion src/gamestatus.hpp
Expand Up @@ -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;

Expand Down
31 changes: 28 additions & 3 deletions src/play_controller.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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;

}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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);
}


2 changes: 2 additions & 0 deletions src/play_controller.hpp
Expand Up @@ -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_; }

Expand Down
2 changes: 1 addition & 1 deletion src/playmp_controller.cpp
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/playsingle_controller.cpp
Expand Up @@ -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();

Expand Down
5 changes: 2 additions & 3 deletions src/playturn.cpp
Expand Up @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/replay_controller.cpp
Expand Up @@ -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
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/resources.cpp
Expand Up @@ -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;
}
36 changes: 19 additions & 17 deletions src/resources.hpp
Expand Up @@ -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; }
Expand All @@ -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<team> *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<team> *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
6 changes: 3 additions & 3 deletions src/savegame.cpp
Expand Up @@ -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*/)
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/savegame.hpp
Expand Up @@ -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. */
Expand Down
12 changes: 7 additions & 5 deletions src/scripting/lua.cpp
Expand Up @@ -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<std::string>(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<std::string>(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.
Expand Down
2 changes: 1 addition & 1 deletion src/synced_checkup.hpp
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/synced_context.cpp
Expand Up @@ -210,7 +210,7 @@ void synced_context::pull_remote_user_input()

boost::shared_ptr<random_new::rng> 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<random_new::rng>(new random_new::rng_deterministic(resources::gamedata->rng()));
Expand Down

0 comments on commit afbf5c6

Please sign in to comment.