Skip to content

Commit

Permalink
resources::game_map is const *, move functionality to game_board
Browse files Browse the repository at this point in the history
This commit changes the resources::game_map pointer to a const *,
so that changes to the game_map coming from lua or wml apis must
go through the game_board. Three functions were moved into the
game_board -- although substantively they did not change, there
were minor changes, in terms of the kind of error reporting done,
and also in how gui refresh orders are issued afterwards. In
testing the differences due to this refactor don't seem to be
noticeable.
  • Loading branch information
cbeck88 committed Jun 2, 2014
1 parent a706e05 commit ac59b0d
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/actions/vision.cpp
Expand Up @@ -230,7 +230,7 @@ bool shroud_clearer::clear_loc(team &tm, const map_location &loc,
size_t &enemy_count, size_t &friend_count,
move_unit_spectator * spectator)
{
gamemap &map = *resources::game_map;
const gamemap &map = resources::gameboard->map();
// This counts as clearing a tile for the return value if it is on the
// board and currently fogged under shared vision. (No need to explicitly
// check for shrouded since shrouded implies fogged.)
Expand Down
6 changes: 3 additions & 3 deletions src/ai/contexts.cpp
Expand Up @@ -242,7 +242,7 @@ readonly_context_impl::readonly_context_impl(side_context &context, const config
add_known_aspect("support_villages",support_villages_);
add_known_aspect("village_value",village_value_);
add_known_aspect("villages_per_scout",villages_per_scout_);
keeps_.init(*resources::game_map);
keeps_.init(resources::gameboard->map());

}

Expand Down Expand Up @@ -1002,7 +1002,7 @@ void keeps_cache::clear()
}


void keeps_cache::init(gamemap &map)
void keeps_cache::init(const gamemap &map)
{
map_ = ↦
}
Expand Down Expand Up @@ -1095,7 +1095,7 @@ double readonly_context_impl::power_projection(const map_location& loc, const mo
map_location locs[6];
get_adjacent_tiles(loc,locs);

gamemap& map_ = *resources::game_map;
const gamemap& map_ = resources::gameboard->map();
unit_map& units_ = *resources::units;

int res = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/ai/contexts.hpp
Expand Up @@ -115,9 +115,9 @@ class keeps_cache : public events::observer
void handle_generic_event(const std::string& event_name);
void clear();
const std::set<map_location>& get();
void init(gamemap &map);
void init(const gamemap &map);
private:
gamemap *map_;
const gamemap *map_;
std::set<map_location> keeps_;
};

Expand Down
4 changes: 2 additions & 2 deletions src/ai/default/ai.cpp
Expand Up @@ -530,8 +530,8 @@ ai_default_recruitment_stage::~ai_default_recruitment_stage()

void ai_default_recruitment_stage::analyze_potential_recruit_movements()
{
unit_map &units_ = *resources::units;
gamemap &map_ = *resources::game_map;
const unit_map &units_ = *resources::units;
const gamemap &map_ = *resources::game_map;

if(unit_movement_scores_.empty() == false ||
get_recruitment_ignore_bad_movement()) {
Expand Down
4 changes: 2 additions & 2 deletions src/ai/default/contexts.cpp
Expand Up @@ -97,7 +97,7 @@ default_ai_context& default_ai_context_impl::get_default_ai_context(){

int default_ai_context_impl::rate_terrain(const unit& u, const map_location& loc) const
{
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;
const t_translation::t_terrain terrain = map_.get_terrain(loc);
const int defense = u.defense_modifier(terrain);
int rating = 100 - defense;
Expand Down Expand Up @@ -132,7 +132,7 @@ std::vector<target> default_ai_context_impl::find_targets(const move_map& enemy_
log_scope2(log_ai, "finding targets...");
unit_map &units_ = *resources::units;
unit_map::iterator leader = units_.find_leader(get_side());
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;
std::vector<team> teams_ = *resources::teams;
const bool has_leader = leader != units_.end();

Expand Down
4 changes: 2 additions & 2 deletions src/ai/testing/aspect_attacks.cpp
Expand Up @@ -136,7 +136,7 @@ void aspect_attacks::do_attack_analysis(
//std::cerr << "ANALYSIS " << cur_analysis.movements.size() << " >= " << get_attack_depth() << "\n";
return;
}
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;
unit_map &units_ = *resources::units;
std::vector<team> &teams_ = *resources::teams;

Expand Down Expand Up @@ -359,7 +359,7 @@ void aspect_attacks::do_attack_analysis(

int aspect_attacks::rate_terrain(const unit& u, const map_location& loc)
{
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;
const t_translation::t_terrain terrain = map_.get_terrain(loc);
const int defense = u.defense_modifier(terrain);
int rating = 100 - defense;
Expand Down
10 changes: 5 additions & 5 deletions src/ai/testing/ca.cpp
Expand Up @@ -64,7 +64,7 @@ double goto_phase::evaluate()
// Execute goto-movements - first collect gotos in a list
std::vector<map_location> gotos;
unit_map &units_ = *resources::units;
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;

for(unit_map::iterator ui = units_.begin(); ui != units_.end(); ++ui) {
if (ui->get_goto() == ui->get_location()) {
Expand Down Expand Up @@ -222,9 +222,9 @@ void recruitment_phase::execute()
unit_combat_scores_.clear();
unit_movement_scores_.clear();

unit_map &units_ = *resources::units;
gamemap &map_ = *resources::game_map;
std::vector<team> &teams_ = *resources::teams;
const unit_map &units_ = *resources::units;
const gamemap &map_ = *resources::game_map;
const std::vector<team> &teams_ = *resources::teams;

map_location start_pos = units_.find_leader(get_side())->get_location();

Expand Down Expand Up @@ -1046,7 +1046,7 @@ void get_villages_phase::find_villages(
const bool passive_leader = get_passive_leader();

size_t min_distance = 100000;
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;
std::vector<team> &teams_ = *resources::teams;

// When a unit is dispatched we need to make sure we don't
Expand Down
8 changes: 4 additions & 4 deletions src/ai/testing/ca_testing_move_to_targets.cpp
Expand Up @@ -273,7 +273,7 @@ std::pair<map_location,map_location> testing_move_to_targets_phase::choose_move(

raise_user_interact();
unit_map &units_ = *resources::units;
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;

unit_map::iterator u;

Expand Down Expand Up @@ -620,7 +620,7 @@ std::pair<map_location,map_location> testing_move_to_targets_phase::choose_move(
void testing_move_to_targets_phase::access_points(const move_map& srcdst, const map_location& u, const map_location& dst, std::vector<map_location>& out)
{
unit_map &units_ = *resources::units;
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;
const unit_map::const_iterator u_it = units_.find(u);
if(u_it == units_.end()) {
return;
Expand Down Expand Up @@ -711,7 +711,7 @@ map_location testing_move_to_targets_phase::form_group(const std::vector<map_loc
bool testing_move_to_targets_phase::move_group(const map_location& dst, const std::vector<map_location>& route, const std::set<map_location>& units)
{
unit_map &units_ = *resources::units;
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;

const std::vector<map_location>::const_iterator itor = std::find(route.begin(),route.end(),dst);
if(itor == route.end()) {
Expand Down Expand Up @@ -813,7 +813,7 @@ bool testing_move_to_targets_phase::move_group(const map_location& dst, const st
double testing_move_to_targets_phase::rate_group(const std::set<map_location>& group, const std::vector<map_location>& battlefield) const
{
unit_map &units_ = *resources::units;
gamemap &map_ = *resources::game_map;
const gamemap &map_ = *resources::game_map;

double strength = 0.0;
for(std::set<map_location>::const_iterator i = group.begin(); i != group.end(); ++i) {
Expand Down
95 changes: 95 additions & 0 deletions src/game_board.cpp
Expand Up @@ -14,10 +14,20 @@

#include "config.hpp"
#include "game_board.hpp"
#include "game_preferences.hpp"
#include "log.hpp"
#include "unit.hpp"

#include "utils/foreach.tpp"

#include <boost/foreach.hpp>

static lg::log_domain log_engine("enginerefac");
#define DBG_RG LOG_STREAM(debug, log_engine)
#define LOG_RG LOG_STREAM(info, log_engine)
#define WRN_RG LOG_STREAM(warn, log_engine)
#define ERR_RG LOG_STREAM(err, log_engine)


void game_board::new_turn(int player_num) {
BOOST_FOREACH (unit & i, units_) {
Expand Down Expand Up @@ -87,6 +97,91 @@ void game_board::side_change_controller(int side_num, team::CONTROLLER ctrl, con
}
}

bool game_board::try_add_unit_to_recall_list(const map_location& loc, const unit& u)
{
if(teams_[u.side()-1].persistent()) {
teams_[u.side()-1].recall_list().push_back(u);
return true;
} else {
ERR_RG << "unit with id " << u.id() << ": location (" << loc.x << "," << loc.y <<") is not on the map, and player "
<< u.side() << " has no recall list.\n";
return false;
}
}


boost::optional<std::string> game_board::replace_map(const gamemap & newmap) {
boost::optional<std::string> ret = boost::optional<std::string> ();

/* Remember the locations where a village is owned by a side. */
std::map<map_location, int> villages;
FOREACH(const AUTO& village, map_.villages()) {
const int owner = village_owner(village);
if(owner != -1) {
villages[village] = owner;
}
}

for (unit_map::iterator itor = units_.begin(); itor != units_.end(); ) {
if (!newmap.on_board(itor->get_location())) {
if (!try_add_unit_to_recall_list(itor->get_location(), *itor)) {
*ret = std::string("replace_map: Cannot add a unit that would become off-map to the recall list\n");
}
units_.erase(itor++);
} else {
++itor;
}
}

/* Disown villages that are no longer villages. */
FOREACH(const AUTO& village, villages) {
if(!newmap.is_village(village.first)) {
teams_[village.second].lose_village(village.first);
}
}

map_ = newmap;
return ret;
}



void game_board::overlay_map(const gamemap & mask_map, const config & cfg, map_location loc, bool border) {
map_.overlay(mask_map, cfg, loc.x, loc.y, border);
}

bool game_board::change_terrain(const map_location &loc, const t_translation::t_terrain &t,
gamemap::tmerge_mode mode, bool replace_if_failed)
{
/*
* When a hex changes from a village terrain to a non-village terrain, and
* a team owned that village it loses that village. When a hex changes from
* a non-village terrain to a village terrain and there is a unit on that
* hex it does not automatically capture the village. The reason for not
* capturing villages it that there are too many choices to make; should a
* unit loose its movement points, should capture events be fired. It is
* easier to do this as wanted by the author in WML.
*/

t_translation::t_terrain
old_t = map_.get_terrain(loc),
new_t = map_.merge_terrains(old_t, t, mode, replace_if_failed);
if (new_t == t_translation::NONE_TERRAIN) return false;
preferences::encountered_terrains().insert(new_t);

if (map_.is_village(old_t) && !map_.is_village(new_t)) {
int owner = village_owner(loc);
if (owner != -1)
teams_[owner].lose_village(loc);
}

map_.set_terrain(loc, new_t);

BOOST_FOREACH(const t_translation::t_terrain &ut, map_.underlying_union_terrain(loc)) {
preferences::encountered_terrains().insert(ut);
}
return true;
}

void game_board::write_config(config & cfg) const {
for(std::vector<team>::const_iterator t = teams_.begin(); t != teams_.end(); ++t) {
Expand Down
11 changes: 11 additions & 0 deletions src/game_board.hpp
Expand Up @@ -21,6 +21,7 @@
#include "team.hpp"
#include "unit_map.hpp"

#include <boost/optional.hpp>
#include <vector>

class config;
Expand All @@ -29,6 +30,7 @@ namespace events {
class mouse_handler;
}


class game_board {

std::vector<team> teams_;
Expand Down Expand Up @@ -68,6 +70,15 @@ class game_board {
void side_drop_to (int side_num, team::CONTROLLER ctrl);
void side_change_controller (int side_num, team::CONTROLLER ctrl, const std::string pname = "");

// Manipulator from actionwml

bool try_add_unit_to_recall_list(const map_location& loc, const unit& u);
boost::optional<std::string> replace_map (const gamemap & r);
void overlay_map (const gamemap & o, const config & cfg, map_location loc, bool border);

bool change_terrain(const map_location &loc, const t_translation::t_terrain &t,
gamemap::tmerge_mode mode, bool replace_if_failed); //used only by lua

// Global accessor from unit.hpp

unit_map::iterator find_visible_unit(const map_location &loc, const team& current_team, bool see_all = false);
Expand Down

0 comments on commit ac59b0d

Please sign in to comment.