Skip to content

Commit

Permalink
tweaks to play_controller::init
Browse files Browse the repository at this point in the history
moved game_events manager init earlier
moved place_sides_in_preferred_locations to game_state
  • Loading branch information
cbeck88 committed Jun 24, 2014
1 parent e85c37e commit bcb3954
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 76 deletions.
1 change: 1 addition & 0 deletions src/game_board.hpp
Expand Up @@ -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:
Expand Down
80 changes: 80 additions & 0 deletions src/game_state.cpp
Expand Up @@ -14,8 +14,18 @@

#include "game_state.hpp"

#include "log.hpp"
#include "map.hpp"
#include "pathfind/teleport.hpp"

#include <boost/foreach.hpp>

#include <algorithm>
#include <set>

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),
Expand All @@ -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<placing_info> 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<int> placed;
std::set<map_location> positions_taken;

for (std::vector<placing_info>::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;
}
}
}
2 changes: 2 additions & 0 deletions src/game_state.hpp
Expand Up @@ -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
81 changes: 6 additions & 75 deletions src/play_controller.cpp
Expand Up @@ -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")) {
Expand All @@ -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<std::string> seen_save_ids;

Expand Down Expand Up @@ -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<placing_info> 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<int> placed;
std::set<map_location> positions_taken;

for (std::vector<placing_info>::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);
}
Expand Down
1 change: 0 additions & 1 deletion src/play_controller.hpp
Expand Up @@ -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;
Expand Down

0 comments on commit bcb3954

Please sign in to comment.