Skip to content

Commit

Permalink
move village_owner function to display_context
Browse files Browse the repository at this point in the history
I would have liked to put it in game_board, but it is needed for
reports so it can't go there
  • Loading branch information
cbeck88 committed Jun 26, 2014
1 parent 221863b commit f5a9de4
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/actions/move.cpp
Expand Up @@ -1051,7 +1051,7 @@ namespace { // Private helpers for move_unit()
// Village capturing.
if ( resources::gameboard->map().is_village(final_loc) ) {
// Is this a capture?
orig_village_owner = village_owner(final_loc);
orig_village_owner = resources::gameboard->village_owner(final_loc);
if ( orig_village_owner != current_side_-1 ) {
// Captured. Zap movement and take over the village.
move_it_->set_movement(0, true);
Expand Down
2 changes: 1 addition & 1 deletion src/ai/default/ai.cpp
Expand Up @@ -831,7 +831,7 @@ bool ai_default_recruitment_stage::do_play_stage()
// that are closer to us than to other keeps.
const std::vector<map_location>& villages = resources::gameboard->map().villages();
for(std::vector<map_location>::const_iterator v = villages.begin(); v != villages.end(); ++v) {
const int owner = village_owner(*v);
const int owner = resources::gameboard->village_owner(*v);
if(owner == -1) {
const size_t distance = distance_between(start_pos,*v);

Expand Down
2 changes: 1 addition & 1 deletion src/ai/default/contexts.cpp
Expand Up @@ -114,7 +114,7 @@ int default_ai_context_impl::rate_terrain(const unit& u, const map_location& loc
}

if(map_.is_village(terrain)) {
int owner = village_owner(loc) + 1;
int owner = resources::gameboard->village_owner(loc) + 1;

if(owner == get_side()) {
rating += friendly_village_value;
Expand Down
4 changes: 2 additions & 2 deletions src/ai/recruitment/recruitment.cpp
Expand Up @@ -1445,7 +1445,7 @@ double recruitment::get_estimated_village_gain() const {
const gamemap& map = resources::gameboard->map();
int neutral_villages = 0;
BOOST_FOREACH(const map_location& village, map.villages()) {
if (village_owner(village) == -1) {
if (resources::gameboard->village_owner(village) == -1) {
++neutral_villages;
}
}
Expand Down Expand Up @@ -1710,7 +1710,7 @@ void recruitment::update_scouts_wanted() {
// We recruit the initial allocation of scouts
// based on how many neutral villages there are.
BOOST_FOREACH(const map_location& village, resources::gameboard->map().villages()) {
if (village_owner(village) == -1) {
if (resources::gameboard->village_owner(village) == -1) {
++neutral_villages;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ai/testing/aspect_attacks.cpp
Expand Up @@ -375,7 +375,7 @@ int aspect_attacks::rate_terrain(const unit& u, const map_location& loc)
}

if(map_.is_village(terrain)) {
int owner = village_owner(loc) + 1;
int owner = resources::gameboard->village_owner(loc) + 1;

if(owner == u.side()) {
rating += friendly_village_value;
Expand Down
2 changes: 1 addition & 1 deletion src/ai/testing/ca.cpp
Expand Up @@ -246,7 +246,7 @@ void recruitment_phase::execute()
// that are closer to us than to other keeps.
const std::vector<map_location>& villages = map_.villages();
for(std::vector<map_location>::const_iterator v = villages.begin(); v != villages.end(); ++v) {
const int owner = village_owner(*v);
const int owner = resources::gameboard->village_owner(*v);
if(owner == -1) {
const size_t distance = distance_between(start_pos,*v);

Expand Down
2 changes: 1 addition & 1 deletion src/display.cpp
Expand Up @@ -3058,7 +3058,7 @@ bool display::invalidate_locations_in_rect(const SDL_Rect& rect)

void display::invalidate_animations_location(const map_location& loc) {
if (get_map().is_village(loc)) {
const int owner = village_owner(loc);
const int owner = dc_->village_owner(loc);
if (owner >= 0 && flags_[owner].need_update()
&& (!fogged(loc) || !dc_->teams()[currentTeam_].is_enemy(owner+1))) {
invalidate(loc);
Expand Down
15 changes: 15 additions & 0 deletions src/display_context.cpp
Expand Up @@ -67,3 +67,18 @@ bool display_context::unit_can_move(const unit &u) const
return false;
}


/**
* Given the location of a village, will return the 0-based index
* of the team that currently owns it, and -1 if it is unowned.
*/
int display_context::village_owner(const map_location& loc) const
{
const std::vector<team> & t = teams();
for(size_t i = 0; i != t.size(); ++i) {
if(t[i].owns_village(loc))
return i;
}
return -1;
}

8 changes: 8 additions & 0 deletions src/display_context.hpp
Expand Up @@ -45,6 +45,14 @@ class display_context {

bool unit_can_move(const unit & u) const;

// From class team

/**
* Given the location of a village, will return the 0-based index
* of the team that currently owns it, and -1 if it is unowned.
*/
int village_owner(const map_location & loc) const;

// Dtor

virtual ~display_context() {}
Expand Down
2 changes: 1 addition & 1 deletion src/menu_events.cpp
Expand Up @@ -1130,7 +1130,7 @@ void menu_handler::change_side(mouse_handler& mousehandler)
return;

// village_owner returns -1 for free village, so team 0 will get it
int team = village_owner(loc) + 1;
int team = resources::gameboard->village_owner(loc) + 1;
// team is 0-based so team=team::nteams() is not a team
// but this will make get_village free it
if(team > team::nteams()) {
Expand Down
3 changes: 2 additions & 1 deletion src/minimap.cpp
Expand Up @@ -17,6 +17,7 @@
#include "global.hpp"
#include "minimap.hpp"

#include "game_board.hpp"
#include "gettext.hpp"
#include "image.hpp"
#include "log.hpp"
Expand Down Expand Up @@ -233,7 +234,7 @@ surface getMinimap(int w, int h, const gamemap &map, const team *vw, const std::

if (terrain_info.is_village() && preferences_minimap_draw_villages) {

int side = village_owner(loc);
int side = resources::gameboard->village_owner(loc);

SDL_Color col = int_to_color(game_config::team_rgb_range.find("white")->second.min());

Expand Down
2 changes: 1 addition & 1 deletion src/reports.cpp
Expand Up @@ -1407,7 +1407,7 @@ REPORT_GENERATOR(terrain, rc)
std::ostringstream str;
if (map.is_village(mouseover_hex))
{
int owner = village_owner(mouseover_hex) + 1;
int owner = rc.dc().village_owner(mouseover_hex) + 1;
if (owner == 0 || viewing_team.fogged(mouseover_hex)) {
str << map.get_terrain_info(terrain).income_description();
} else if (owner == viewing_side) {
Expand Down
4 changes: 2 additions & 2 deletions src/scripting/lua.cpp
Expand Up @@ -1440,7 +1440,7 @@ static int intf_get_village_owner(lua_State *L)
if (!resources::gameboard->map().is_village(loc))
return 0;

int side = village_owner(loc) + 1;
int side = resources::gameboard->village_owner(loc) + 1;
if (!side) return 0;
lua_pushinteger(L, side);
return 1;
Expand All @@ -1462,7 +1462,7 @@ static int intf_set_village_owner(lua_State *L)
if (!resources::gameboard->map().is_village(loc))
return 0;

int old_side = village_owner(loc) + 1;
int old_side = resources::gameboard->village_owner(loc) + 1;
if (new_side == old_side
|| new_side < 0
|| new_side > static_cast<int>(teams.size())
Expand Down
17 changes: 0 additions & 17 deletions src/team.cpp
Expand Up @@ -842,20 +842,3 @@ config team::to_config() const
write(result);
return result;
}

/**
* Given the location of a village, will return the 0-based index
* of the team that currently owns it, and -1 if it is unowned.
*/
int village_owner(const map_location& loc)
{
if(! teams) {
return -1;
}
for(size_t i = 0; i != teams->size(); ++i) {
if((*teams)[i].owns_village(loc))
return i;
}
return -1;
}

6 changes: 0 additions & 6 deletions src/team.hpp
Expand Up @@ -381,12 +381,6 @@ namespace teams_manager {
const std::vector<team> &get_teams();
}

/**
* Given the location of a village, will return the 0-based index
* of the team that currently owns it, and -1 if it is unowned.
*/
int village_owner(const map_location& loc);

//FIXME: this global method really needs to be moved into play_controller,
//or somewhere else that makes sense.
bool is_observer();
Expand Down
4 changes: 2 additions & 2 deletions src/terrain_filter.cpp
Expand Up @@ -289,7 +289,7 @@ bool terrain_filter::match_internal(const map_location& loc, const bool ignore_x
side_filter ssf(filter_owner);
const std::vector<int>& sides = ssf.get_teams();
bool found = false;
if(sides.empty() && village_owner(loc) == -1)
if(sides.empty() && resources::gameboard->village_owner(loc) == -1)
found = true;
BOOST_FOREACH(const int side, sides) {
if(resources::teams->at(side - 1).owns_village(loc)) {
Expand All @@ -302,7 +302,7 @@ bool terrain_filter::match_internal(const map_location& loc, const bool ignore_x
}
else if(!owner_side.empty()) {
const int side_index = owner_side.to_int(0) - 1;
if(village_owner(loc) != side_index) {
if(resources::gameboard->village_owner(loc) != side_index) {
return false;
}
}
Expand Down

0 comments on commit f5a9de4

Please sign in to comment.