Skip to content

Commit

Permalink
refactor tod_manager::set_turn, next_turn, accept game_data arg
Browse files Browse the repository at this point in the history
This improves encapsulation and almost allows to push resources
out of the tod_manager
  • Loading branch information
cbeck88 committed Jun 13, 2014
1 parent 80be563 commit fe00d72
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/game_events/action_wml.cpp
Expand Up @@ -1345,7 +1345,7 @@ WML_HANDLER_FUNCTION(modify_turns, /*event_info*/, cfg)
if(new_turn_number_u < 1 || (new_turn_number > tod_man.number_of_turns() && tod_man.number_of_turns() != -1)) {
ERR_NG << "attempted to change current turn number to one out of range (" << new_turn_number << ")" << std::endl;
} else if(new_turn_number_u != current_turn_number) {
tod_man.set_turn(new_turn_number_u);
tod_man.set_turn(new_turn_number_u, *resources::gamedata);
resources::screen->new_turn();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/menu_events.cpp
Expand Up @@ -3008,7 +3008,7 @@ void console_handler::do_turn()
if (!data.empty()) {
turn = lexical_cast_default<int>(data, 1);
}
resources::tod_manager->set_turn(turn);
resources::tod_manager->set_turn(turn, *resources::gamedata);

menu_handler_.gui_->new_turn();
menu_handler_.gui_->redraw_everything();
Expand Down
2 changes: 1 addition & 1 deletion src/playsingle_controller.cpp
Expand Up @@ -1012,7 +1012,7 @@ void playsingle_controller::handle_generic_event(const std::string& name){
}

possible_end_play_signal playsingle_controller::check_time_over(){
bool b = tod_manager_.next_turn();
bool b = tod_manager_.next_turn(*resources::gamedata);
it_is_a_new_turn_ = true;
if(!b) {

Expand Down
2 changes: 1 addition & 1 deletion src/replay_controller.cpp
Expand Up @@ -552,7 +552,7 @@ possible_end_play_signal replay_controller::play_side() {
//during the orginal game player_number_ would also be gameboard_.teams().size(),
player_number_ = gameboard_.teams().size();
finish_turn();
tod_manager_.next_turn();
tod_manager_.next_turn(*resources::gamedata);
it_is_a_new_turn_ = true;
player_number_ = 1;
current_turn_++;
Expand Down
10 changes: 5 additions & 5 deletions src/tod_manager.cpp
Expand Up @@ -378,7 +378,7 @@ void tod_manager::set_number_of_turns(int num)
num_turns_ = std::max<int>(num, -1);
}

void tod_manager::set_turn(const int num, const bool increase_limit_if_needed)
void tod_manager::set_turn(const int num, boost::optional<game_data &> vars, const bool increase_limit_if_needed)
{
const int new_turn = std::max<int>(num, 1);
LOG_NG << "changing current turn number from " << turn_ << " to " << new_turn << '\n';
Expand All @@ -389,8 +389,8 @@ void tod_manager::set_turn(const int num, const bool increase_limit_if_needed)
set_number_of_turns(new_turn);
}
turn_ = new_turn;
if (resources::gamedata)
resources::gamedata->get_variable("turn_number") = new_turn;
if (vars)
vars->get_variable("turn_number") = new_turn;
}

void tod_manager::set_new_current_times(const int new_current_turn_number)
Expand Down Expand Up @@ -418,9 +418,9 @@ int tod_manager::calculate_current_time(
return new_current_time;
}

bool tod_manager::next_turn()
bool tod_manager::next_turn(boost::optional<game_data&> vars)
{
set_turn(turn_ + 1, false);
set_turn(turn_ + 1, vars, false);
return is_time_left();
}

Expand Down
7 changes: 5 additions & 2 deletions src/tod_manager.hpp
Expand Up @@ -19,8 +19,11 @@
#include "time_of_day.hpp"
#include "savegame_config.hpp"

#include <boost/optional.hpp>

class gamemap;
class unit_map;
class game_data;

namespace random_new
{
Expand Down Expand Up @@ -156,14 +159,14 @@ class tod_manager : public savegame::savegame_config
void set_number_of_turns(int num);

/** Dynamically change the current turn number. */
void set_turn(const int num, const bool increase_limit_if_needed = true);
void set_turn(const int num, boost::optional<game_data&> vars = boost::none, const bool increase_limit_if_needed = true);

/**
* Function to move to the next turn.
*
* @returns True if time has not expired.
*/
bool next_turn();
bool next_turn(boost::optional<game_data&> vars);

/**
* Function to check the end of turns.
Expand Down

0 comments on commit fe00d72

Please sign in to comment.