Skip to content

Commit

Permalink
Merge pull request #200 from gfgtdf/gamestate_refactor
Browse files Browse the repository at this point in the history
move next_underlying_unit_id + game_state refactor
  • Loading branch information
gfgtdf committed Jun 10, 2014
2 parents 469eabb + 42a28bf commit 99feabf
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 27 deletions.
50 changes: 37 additions & 13 deletions src/carryover.cpp
Expand Up @@ -142,6 +142,7 @@ carryover_info::carryover_info(const config& cfg)
, rng_(cfg)
, wml_menu_items_()
, next_scenario_(cfg["next_scenario"])
, next_underlying_unit_id_(cfg["next_underlying_unit_id"].to_int(0))
{
end_level_.read(cfg.child_or_empty("end_level_data"));
BOOST_FOREACH(const config& side, cfg.child_range("side")){
Expand Down Expand Up @@ -185,22 +186,42 @@ void carryover_info::transfer_from(const team& t, int carryover_gold){
carryover_sides_.push_back(carryover(t, carryover_gold, end_level_.carryover_add));
}


struct save_id_equals
{
save_id_equals(std::string val) : value (val) {}
bool operator () (carryover& v2)
{
return value == v2.get_save_id();
}

std::string value;
};

void carryover_info::transfer_all_to(config& side_cfg){
if(side_cfg["save_id"].empty()){
side_cfg["save_id"] = side_cfg["id"];
}
BOOST_FOREACH(carryover& side, carryover_sides_){
if(side.get_save_id() == side_cfg["save_id"]){
side.transfer_all_gold_to(side_cfg);
side.transfer_all_recalls_to(side_cfg);
side.transfer_all_recruits_to(side_cfg);
return;
}
std::vector<carryover>::iterator iside = std::find_if(
carryover_sides_.begin(),
carryover_sides_.end(),
save_id_equals(side_cfg["save_id"])
);
if(iside != carryover_sides_.end())
{
iside->transfer_all_gold_to(side_cfg);
iside->transfer_all_recalls_to(side_cfg);
iside->transfer_all_recruits_to(side_cfg);
//TODO: enable the following line after some testing.
//carryover_sides_.erase(iside);
return;
}

//if no carryover was found for this side, check if starting gold is defined
if(!side_cfg.has_attribute("gold") || side_cfg["gold"].empty()){
side_cfg["gold"] = default_gold_qty;
else
{
//if no carryover was found for this side, check if starting gold is defined
if(!side_cfg.has_attribute("gold") || side_cfg["gold"].empty()){
side_cfg["gold"] = default_gold_qty;
}
}
}

Expand All @@ -212,7 +233,10 @@ void carryover_info::transfer_from(game_data& gamedata){
}

void carryover_info::transfer_to(config& level){

if(!level.has_attribute("next_underlying_unit_id"))
{
level["next_underlying_unit_id"] = next_underlying_unit_id_;
}

if(!end_level_.next_scenario_settings.empty()) {
level.merge_with(end_level_.next_scenario_settings);
Expand Down Expand Up @@ -245,7 +269,7 @@ void carryover_info::transfer_to(config& level){
const config carryover_info::to_config()
{
config cfg;

cfg["next_underlying_unit_id"] = next_underlying_unit_id_;
cfg["next_scenario"] = next_scenario_;

BOOST_FOREACH(carryover& c, carryover_sides_){
Expand Down
2 changes: 2 additions & 0 deletions src/carryover.hpp
Expand Up @@ -63,6 +63,7 @@ class carryover_info{
, rng_()
, wml_menu_items_()
, next_scenario_()
, next_underlying_unit_id_()
{}
// Turns config from a loaded savegame into carryover_info
explicit carryover_info(const config& cfg);
Expand Down Expand Up @@ -100,6 +101,7 @@ class carryover_info{
rand_rng::simple_rng rng_;
game_events::wmi_container wml_menu_items_;
std::string next_scenario_; /**< the scenario coming next (for campaigns) */
int next_underlying_unit_id_;
};


Expand Down
11 changes: 10 additions & 1 deletion src/gamestatus.cpp
Expand Up @@ -617,7 +617,6 @@ game_state::game_state(const config& cfg, bool show_replay) :
classification_(cfg),
mp_settings_(cfg)
{
n_unit::id_manager::instance().set_save_id(cfg["next_underlying_unit_id"]);
log_scope("read_game");

if(cfg.has_child("carryover_sides")){
Expand Down Expand Up @@ -759,7 +758,17 @@ void convert_old_saves(config& cfg){
LOG_RG<<"removing snapshot \n";
cfg.remove_child("snapshot", 0);
}
//?-1.11.? end
//1.12-1.13 begin

if(config& carryover_sides_start = cfg.child("carryover_sides_start"))
{
if(!carryover_sides_start.has_attribute("next_underlying_unit_id"))
{
carryover_sides_start["next_underlying_unit_id"] = cfg["next_underlying_unit_id"];
}
}
//1.12-1.13 end
LOG_RG<<"cfg after conversion "<<cfg<<"\n";
}

Expand Down
3 changes: 2 additions & 1 deletion src/play_controller.cpp
Expand Up @@ -155,7 +155,8 @@ play_controller::play_controller(const config& level, game_state& state_of_game,
resources::mp_settings = &gamestate_.mp_settings();

persist_.start_transaction();

n_unit::id_manager::instance().set_save_id(level_["next_underlying_unit_id"]);

// Setup victory and defeat music
set_victory_music_list(level_["victory_music"]);
set_defeat_music_list(level_["defeat_music"]);
Expand Down
11 changes: 6 additions & 5 deletions src/playcampaign.cpp
Expand Up @@ -189,16 +189,15 @@ LEVEL_RESULT play_replay(display& disp, game_state& gamestate, const config& gam
{
const std::string campaign_type_str = lexical_cast<std::string> (gamestate.classification().campaign_type);

// 'starting_pos' will contain the position we start the game from.
config starting_pos;

if (gamestate.replay_start().empty()){
// Backwards compatibility code for 1.2 and 1.2.1
const config &scenario = game_config.find_child(campaign_type_str,"id",gamestate.carryover_sides_start["next_scenario"]);
assert(scenario);
gamestate.replay_start() = scenario;
}
starting_pos = gamestate.replay_start();

// 'starting_pos' will contain the position we start the game from.
const config& starting_pos = gamestate.replay_start();

//for replays, use the variables specified in starting_pos
if (const config &vars = starting_pos.child("variables")) {
Expand All @@ -212,7 +211,7 @@ LEVEL_RESULT play_replay(display& disp, game_state& gamestate, const config& gam
//if (gamestate.abbrev.empty())
// gamestate.abbrev = (*scenario)["abbrev"];

LEVEL_RESULT res = play_replay_level(game_config, &starting_pos, video, gamestate, is_unit_test);
LEVEL_RESULT res = play_replay_level(game_config, video, gamestate, is_unit_test);

gamestate.snapshot = config();
recorder.clear();
Expand Down Expand Up @@ -270,6 +269,7 @@ static LEVEL_RESULT playsingle_scenario(const config& game_config,

end_level = playcontroller.get_end_level_data_const();
config& cfg_end_level = state_of_game.carryover_sides.child_or_add("end_level_data");
state_of_game.carryover_sides["next_underlying_unit_id"] = int(n_unit::id_manager::instance().get_save_id());
end_level.write(cfg_end_level);

if (res != QUIT)
Expand Down Expand Up @@ -312,6 +312,7 @@ static LEVEL_RESULT playmp_scenario(const config& game_config,
end_level = playcontroller.get_end_level_data_const();
config& cfg_end_level = state_of_game.carryover_sides.child_or_add("end_level_data");
end_level.write(cfg_end_level);
state_of_game.carryover_sides["next_underlying_unit_id"] = int(n_unit::id_manager::instance().get_save_id());

//Check if the player started as mp client and changed to host
if (io_type == IO_CLIENT && playcontroller.is_host())
Expand Down
7 changes: 2 additions & 5 deletions src/replay_controller.cpp
Expand Up @@ -45,14 +45,11 @@ static lg::log_domain log_replay("replay");
possible_end_play_signal play_replay_level_main_loop(replay_controller & replaycontroller, bool & is_unit_test);

LEVEL_RESULT play_replay_level(const config& game_config,
const config* level, CVideo& video, game_state& state_of_game, bool is_unit_test)
CVideo& video, game_state& state_of_game, bool is_unit_test)
{
const int ticks = SDL_GetTicks();

config init_level = *level;
carryover_info sides(state_of_game.carryover_sides);
sides.transfer_to(init_level);
state_of_game.carryover_sides = sides.to_config();
config init_level = state_of_game.replay_start();

DBG_NG << "creating objects... " << (SDL_GetTicks() - ticks) << std::endl;

Expand Down
3 changes: 1 addition & 2 deletions src/replay_controller.hpp
Expand Up @@ -98,8 +98,7 @@ class replay_controller : public play_controller
};


LEVEL_RESULT play_replay_level(const config& terrain_config,
const config* level, CVideo& video,
LEVEL_RESULT play_replay_level(const config& terrain_config, CVideo& video,
game_state& state_of_game, bool is_unit_test = false);

#endif

0 comments on commit 99feabf

Please sign in to comment.