From bcb39542c5d7bb5befbf38e2a2ae0412917e0895 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Tue, 24 Jun 2014 16:06:46 -0400 Subject: [PATCH] tweaks to play_controller::init moved game_events manager init earlier moved place_sides_in_preferred_locations to game_state --- src/game_board.hpp | 1 + src/game_state.cpp | 80 ++++++++++++++++++++++++++++++++++++++++ src/game_state.hpp | 2 + src/play_controller.cpp | 81 +++-------------------------------------- src/play_controller.hpp | 1 - 5 files changed, 89 insertions(+), 76 deletions(-) diff --git a/src/game_board.hpp b/src/game_board.hpp index db84bd3b5437..672066fe0e44 100644 --- a/src/game_board.hpp +++ b/src/game_board.hpp @@ -60,6 +60,7 @@ class game_board : public display_context { friend class play_controller; friend class events::mouse_handler; friend class events::menu_handler; + friend struct game_state; /** * Temporary unit move structs: diff --git a/src/game_state.cpp b/src/game_state.cpp index 48d9e3dd124d..f5f2d87a9954 100644 --- a/src/game_state.cpp +++ b/src/game_state.cpp @@ -14,8 +14,18 @@ #include "game_state.hpp" +#include "log.hpp" +#include "map.hpp" #include "pathfind/teleport.hpp" +#include + +#include +#include + +static lg::log_domain log_engine("engine"); +#define LOG_NG LOG_STREAM(info, log_engine) +#define DBG_NG LOG_STREAM(debug, log_engine) game_state::game_state(const config & level, const config & game_config) : level_(level), @@ -27,3 +37,73 @@ game_state::game_state(const config & level, const config & game_config) : game_state::~game_state() {} +static int placing_score(const config& side, const gamemap& map, const map_location& pos) +{ + int positions = 0, liked = 0; + const t_translation::t_list terrain = t_translation::read_list(side["terrain_liked"]); + + for(int i = pos.x-8; i != pos.x+8; ++i) { + for(int j = pos.y-8; j != pos.y+8; ++j) { + const map_location pos(i,j); + if(map.on_board(pos)) { + ++positions; + if(std::count(terrain.begin(),terrain.end(),map[pos])) { + ++liked; + } + } + } + } + + return (100*liked)/positions; +} + +struct placing_info { + + placing_info() : + side(0), + score(0), + pos() + { + } + + int side, score; + map_location pos; +}; + +static bool operator<(const placing_info& a, const placing_info& b) { return a.score > b.score; } + + +void game_state::place_sides_in_preferred_locations() +{ + std::vector placings; + + int num_pos = board_.map().num_valid_starting_positions(); + + int side_num = 1; + BOOST_FOREACH(const config &side, level_.child_range("side")) + { + for(int p = 1; p <= num_pos; ++p) { + const map_location& pos = board_.map().starting_position(p); + int score = placing_score(side, board_.map(), pos); + placing_info obj; + obj.side = side_num; + obj.score = score; + obj.pos = pos; + placings.push_back(obj); + } + ++side_num; + } + + std::sort(placings.begin(),placings.end()); + std::set placed; + std::set positions_taken; + + for (std::vector::const_iterator i = placings.begin(); i != placings.end() && int(placed.size()) != side_num - 1; ++i) { + if(placed.count(i->side) == 0 && positions_taken.count(i->pos) == 0) { + placed.insert(i->side); + positions_taken.insert(i->pos); + board_.map_->set_starting_position(i->side,i->pos); + LOG_NG << "placing side " << i->side << " at " << i->pos << std::endl; + } + } +} diff --git a/src/game_state.hpp b/src/game_state.hpp index a6e93d1b1646..7db21f64bcfa 100644 --- a/src/game_state.hpp +++ b/src/game_state.hpp @@ -35,6 +35,8 @@ struct game_state { game_state(const config & level, const config & game_config); ~game_state(); + + void place_sides_in_preferred_locations(); }; #endif diff --git a/src/play_controller.cpp b/src/play_controller.cpp index 46044540fe62..faedee458b1c 100644 --- a/src/play_controller.cpp +++ b/src/play_controller.cpp @@ -182,9 +182,14 @@ void play_controller::init(CVideo& video){ loadscreen::start_stage("load level"); recorder.set_skip(false); + // This *needs* to be created before the show_intro and show_map_scene + // as that functions use the manager state_of_game + // 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; - place_sides_in_preferred_locations(); + gamestate_.place_sides_in_preferred_locations(); } BOOST_FOREACH(const config &t, level_.child_range("time_area")) { @@ -196,10 +201,6 @@ void play_controller::init(CVideo& video){ resources::teams->resize(level_.child_count("side")); - // This *needs* to be created before the show_intro and show_map_scene - // as that functions use the manager state_of_game - // Has to be done before registering any events! - events_manager_.reset(new game_events::manager(level_)); std::set seen_save_ids; @@ -322,76 +323,6 @@ void play_controller::init_managers(){ LOG_NG << "done initializing managers... " << (SDL_GetTicks() - ticks_) << std::endl; } -static int placing_score(const config& side, const gamemap& map, const map_location& pos) -{ - int positions = 0, liked = 0; - const t_translation::t_list terrain = t_translation::read_list(side["terrain_liked"]); - - for(int i = pos.x-8; i != pos.x+8; ++i) { - for(int j = pos.y-8; j != pos.y+8; ++j) { - const map_location pos(i,j); - if(map.on_board(pos)) { - ++positions; - if(std::count(terrain.begin(),terrain.end(),map[pos])) { - ++liked; - } - } - } - } - - return (100*liked)/positions; -} - -struct placing_info { - - placing_info() : - side(0), - score(0), - pos() - { - } - - int side, score; - map_location pos; -}; - -static bool operator<(const placing_info& a, const placing_info& b) { return a.score > b.score; } - -void play_controller::place_sides_in_preferred_locations() -{ - std::vector placings; - - int num_pos = gamestate_.board_.map().num_valid_starting_positions(); - - int side_num = 1; - BOOST_FOREACH(const config &side, level_.child_range("side")) - { - for(int p = 1; p <= num_pos; ++p) { - const map_location& pos = gamestate_.board_.map().starting_position(p); - int score = placing_score(side, gamestate_.board_.map(), pos); - placing_info obj; - obj.side = side_num; - obj.score = score; - obj.pos = pos; - placings.push_back(obj); - } - ++side_num; - } - - std::sort(placings.begin(),placings.end()); - std::set placed; - std::set positions_taken; - - for (std::vector::const_iterator i = placings.begin(); i != placings.end() && int(placed.size()) != side_num - 1; ++i) { - if(placed.count(i->side) == 0 && positions_taken.count(i->pos) == 0) { - placed.insert(i->side); - positions_taken.insert(i->pos); - gamestate_.board_.map_->set_starting_position(i->side,i->pos); - LOG_NG << "placing side " << i->side << " at " << i->pos << std::endl; - } - } -} - void play_controller::objectives(){ menu_handler_.objectives(gui_->viewing_team()+1); } diff --git a/src/play_controller.hpp b/src/play_controller.hpp index 7597848054d9..6564252178ae 100644 --- a/src/play_controller.hpp +++ b/src/play_controller.hpp @@ -202,7 +202,6 @@ class play_controller : public controller_base, public events::observer, public void fire_start(bool execute); virtual void init_gui(); possible_end_play_signal init_side(bool is_replay = false); - void place_sides_in_preferred_locations(); virtual void finish_side_turn(); void finish_turn(); //this should not throw an end turn or end level exception bool enemies_visible() const;