Skip to content

Commit

Permalink
move some initializing from play_controller to game_state
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jun 24, 2014
1 parent bcb3954 commit 15352a2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 84 deletions.
84 changes: 83 additions & 1 deletion src/game_state.cpp
Expand Up @@ -14,11 +14,16 @@

#include "game_state.hpp"

#include "game_data.hpp"
#include "loadscreen.hpp"
#include "log.hpp"
#include "map.hpp"
#include "pathfind/teleport.hpp"
#include "game_preferences.hpp"
#include "random_new_deterministic.hpp"

#include <boost/foreach.hpp>
#include <SDL_timer.h>

#include <algorithm>
#include <set>
Expand All @@ -32,7 +37,8 @@ game_state::game_state(const config & level, const config & game_config) :
gamedata_(level_),
board_(game_config,level_),
tod_manager_(level_),
pathfind_manager_()
pathfind_manager_(),
first_human_team_(-1)
{}

game_state::~game_state() {}
Expand Down Expand Up @@ -107,3 +113,79 @@ void game_state::place_sides_in_preferred_locations()
}
}
}

static std::string get_unique_saveid(const config& cfg, std::set<std::string>& seen_save_ids)
{
std::string save_id = cfg["save_id"];

if(save_id.empty()) {
save_id = cfg["id"].str();
}

if(save_id.empty()) {
save_id="Unknown";
}

// Make sure the 'save_id' is unique
while(seen_save_ids.count(save_id)) {
save_id += "_";
}

return save_id;
}

void game_state::init(const int ticks, const config & replay_start)
{
if (level_["modify_placing"].to_bool()) {
LOG_NG << "modifying placing..." << std::endl;
place_sides_in_preferred_locations();
}

LOG_NG << "initialized time of day regions... " << (SDL_GetTicks() - ticks) << std::endl;
BOOST_FOREACH(const config &t, level_.child_range("time_area")) {
tod_manager_.add_time_area(board_.map(),t);
}

LOG_NG << "initialized teams... " << (SDL_GetTicks() - ticks) << std::endl;
loadscreen::start_stage("init teams");

board_.teams_.resize(level_.child_count("side"));

std::set<std::string> seen_save_ids;

std::vector<team_builder_ptr> team_builders;

int team_num = 0;
BOOST_FOREACH(const config &side, level_.child_range("side"))
{
std::string save_id = get_unique_saveid(side, seen_save_ids);
seen_save_ids.insert(save_id);
if (first_human_team_ == -1) {
const std::string &controller = side["controller"];
if (controller == "human" &&
side["id"] == preferences::login()) {
first_human_team_ = team_num;
} else if (controller == "human") {
first_human_team_ = team_num;
}
}
team_builder_ptr tb_ptr = gamedata_.create_team_builder(side,
save_id, board_.teams_, level_, *board_.map_, board_.units_, replay_start);
++team_num;
gamedata_.build_team_stage_one(tb_ptr);
team_builders.push_back(tb_ptr);
}
{
//sync traits of start units and the random start time.
random_new::set_random_determinstic deterministic(gamedata_.rng());

tod_manager_.resolve_random(*random_new::generator);

BOOST_FOREACH(team_builder_ptr tb_ptr, team_builders)
{
gamedata_.build_team_stage_two(tb_ptr);
}
}

pathfind_manager_.reset(new pathfind::manager(level_));
}
4 changes: 4 additions & 0 deletions src/game_state.hpp
Expand Up @@ -32,11 +32,15 @@ struct game_state {
tod_manager tod_manager_;
boost::scoped_ptr<pathfind::manager> pathfind_manager_;

int first_human_team_; //needed to initialize the viewpoint during setup

game_state(const config & level, const config & game_config);

~game_state();

void place_sides_in_preferred_locations();

void init(int ticks, const config & replay_start);
};

#endif
84 changes: 6 additions & 78 deletions src/play_controller.cpp
Expand Up @@ -41,7 +41,6 @@
#include "pathfind/teleport.hpp"
#include "preferences_display.hpp"
#include "replay.hpp"
#include "random_new_deterministic.hpp"
#include "resources.hpp"
#include "savegame.hpp"
#include "saved_game.hpp"
Expand Down Expand Up @@ -117,7 +116,6 @@ play_controller::play_controller(const config& level, saved_game& state_of_game,
undo_stack_(new actions::undo_list(level.child("undo_stack"))),
whiteboard_manager_(),
loading_game_(level["playing_team"].empty() == false),
first_human_team_(-1),
player_number_(1),
first_player_(level["playing_team"].to_int() + 1),
start_turn_(gamestate_.tod_manager_.turn()), // gamestate_.tod_manager_ constructed above
Expand Down Expand Up @@ -187,56 +185,9 @@ void play_controller::init(CVideo& video){
// Has to be done before registering any events!
events_manager_.reset(new game_events::manager(level_));

if (level_["modify_placing"].to_bool()) {
LOG_NG << "modifying placing..." << std::endl;
gamestate_.place_sides_in_preferred_locations();
}

BOOST_FOREACH(const config &t, level_.child_range("time_area")) {
gamestate_.tod_manager_.add_time_area(gamestate_.board_.map(),t);
}

LOG_NG << "initialized teams... " << (SDL_GetTicks() - ticks_) << std::endl;
loadscreen::start_stage("init teams");

resources::teams->resize(level_.child_count("side"));


std::set<std::string> seen_save_ids;

std::vector<team_builder_ptr> team_builders;

int team_num = 0;
BOOST_FOREACH(const config &side, level_.child_range("side"))
{
std::string save_id = get_unique_saveid(side, seen_save_ids);
seen_save_ids.insert(save_id);
if (first_human_team_ == -1) {
const std::string &controller = side["controller"];
if (controller == "human" &&
side["id"] == preferences::login()) {
first_human_team_ = team_num;
} else if (controller == "human") {
first_human_team_ = team_num;
}
}
team_builder_ptr tb_ptr = gamestate_.gamedata_.create_team_builder(side,
save_id, gamestate_.board_.teams_, level_, *gamestate_.board_.map_, gamestate_.board_.units_, saved_game_.replay_start());
++team_num;
gamestate_.gamedata_.build_team_stage_one(tb_ptr);
team_builders.push_back(tb_ptr);
}
{
//sync traits of start units and the random start time.
random_new::set_random_determinstic deterministic(gamestate_.gamedata_.rng());

gamestate_.tod_manager_.resolve_random(*random_new::generator);

BOOST_FOREACH(team_builder_ptr tb_ptr, team_builders)
{
gamestate_.gamedata_.build_team_stage_two(tb_ptr);
}
}
LOG_NG << "initializing game_state..." << (SDL_GetTicks() - ticks_) << std::endl;
gamestate_.init(ticks_, saved_game_.replay_start());
resources::tunnels = gamestate_.pathfind_manager_.get();

// mouse_handler expects at least one team for linger mode to work.
if (gamestate_.board_.teams().empty()) end_level_data_.transient.linger_mode = false;
Expand All @@ -249,10 +200,8 @@ void play_controller::init(CVideo& video){
loadscreen::start_stage("init theme");
const config &theme_cfg = get_theme(game_config_, level_["theme"]);

LOG_NG << "initializing pathfinding and whiteboard..." << (SDL_GetTicks() - ticks_) << std::endl;
gamestate_.pathfind_manager_.reset(new pathfind::manager(level_));
LOG_NG << "initializing whiteboard..." << (SDL_GetTicks() - ticks_) << std::endl;
whiteboard_manager_.reset(new wb::manager());
resources::tunnels = gamestate_.pathfind_manager_.get();
resources::whiteboard = whiteboard_manager_;

LOG_NG << "building terrain rules... " << (SDL_GetTicks() - ticks_) << std::endl;
Expand All @@ -272,8 +221,8 @@ void play_controller::init(CVideo& video){

LOG_NG << "done initializing display... " << (SDL_GetTicks() - ticks_) << std::endl;

if(first_human_team_ != -1) {
gui_->set_team(first_human_team_);
if(gamestate_.first_human_team_ != -1) {
gui_->set_team(gamestate_.first_human_team_);
}
else if (is_observer())
{
Expand Down Expand Up @@ -969,27 +918,6 @@ void play_controller::tab()
menu_handler_.get_textbox().tab(dictionary);
}


std::string play_controller::get_unique_saveid(const config& cfg, std::set<std::string>& seen_save_ids)
{
std::string save_id = cfg["save_id"];

if(save_id.empty()) {
save_id = cfg["id"].str();
}

if(save_id.empty()) {
save_id="Unknown";
}

// Make sure the 'save_id' is unique
while(seen_save_ids.count(save_id)) {
save_id += "_";
}

return save_id;
}

team& play_controller::current_team()
{
assert(player_number_ > 0 && player_number_ <= int(gamestate_.board_.teams().size()));
Expand Down
3 changes: 0 additions & 3 deletions src/play_controller.hpp
Expand Up @@ -209,8 +209,6 @@ class play_controller : public controller_base, public events::observer, public
void enter_textbox();
void tab();

std::string get_unique_saveid(const config& cfg, std::set<std::string>& seen_save_ids);

team& current_team();
const team& current_team() const;

Expand Down Expand Up @@ -251,7 +249,6 @@ class play_controller : public controller_base, public events::observer, public
//instead of starting a fresh one. Gets reset to false after init_side
bool loading_game_;

int first_human_team_;
int player_number_;
int first_player_;
unsigned int start_turn_;
Expand Down
4 changes: 2 additions & 2 deletions src/playsingle_controller.cpp
Expand Up @@ -101,8 +101,8 @@ void playsingle_controller::init_gui(){
LOG_NG << "Initializing GUI... " << (SDL_GetTicks() - ticks_) << "\n";
play_controller::init_gui();

if(first_human_team_ != -1) {
gui_->scroll_to_tile(gamestate_.board_.map().starting_position(first_human_team_ + 1), game_display::WARP);
if(gamestate_.first_human_team_ != -1) {
gui_->scroll_to_tile(gamestate_.board_.map().starting_position(gamestate_.first_human_team_ + 1), game_display::WARP);
}
gui_->scroll_to_tile(gamestate_.board_.map().starting_position(1), game_display::WARP);

Expand Down

0 comments on commit 15352a2

Please sign in to comment.