Skip to content

Commit

Permalink
move actions::unit_can_move to be a method of display_context::
Browse files Browse the repository at this point in the history
I would have put it in game board but it needs to be available
to display / drawing code... would be good to refactor to simplify
this.
  • Loading branch information
cbeck88 committed Jun 17, 2014
1 parent 1c2d254 commit ab23b03
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 43 deletions.
32 changes: 0 additions & 32 deletions src/actions/move.cpp
Expand Up @@ -1288,37 +1288,5 @@ size_t move_unit_from_replay(const std::vector<map_location> &steps,
return move_unit_internal(undo_stack, show_move, NULL, mover);
}

bool unit_can_move(const unit &u)
{
const team &current_team = (*resources::teams)[u.side() - 1];

if(!u.attacks_left() && u.movement_left()==0)
return false;

// Units with goto commands that have already done their gotos this turn
// (i.e. don't have full movement left) should have red globes.
if(u.has_moved() && u.has_goto()) {
return false;
}

map_location locs[6];
get_adjacent_tiles(u.get_location(), locs);
for(int n = 0; n != 6; ++n) {
if (resources::gameboard->map().on_board(locs[n])) {
const unit_map::const_iterator i = resources::units->find(locs[n]);
if (i.valid() && !i->incapacitated() &&
current_team.is_enemy(i->side())) {
return true;
}

if (u.movement_cost((resources::gameboard->map())[locs[n]]) <= u.movement_left()) {
return true;
}
}
}

return false;
}


}//namespace actions
7 changes: 0 additions & 7 deletions src/actions/move.hpp
Expand Up @@ -114,13 +114,6 @@ size_t move_unit_from_replay(const std::vector<map_location> &steps,
bool continued_move, bool skip_ally_sighted,
bool show_move = true);

/**
* Will return true iff the unit @a u has any possible moves
* it can do (including attacking etc).
*/
bool unit_can_move(const unit &u);


}//namespace actions


Expand Down
38 changes: 38 additions & 0 deletions src/display_context.cpp
Expand Up @@ -29,3 +29,41 @@ const unit * display_context::get_visible_unit(const map_location & loc, const t
}
return &*u;
}

/**
* Will return true iff the unit @a u has any possible moves
* it can do (including attacking etc).
*/

bool display_context::unit_can_move(const unit &u)
{
if(!u.attacks_left() && u.movement_left()==0)
return false;

// Units with goto commands that have already done their gotos this turn
// (i.e. don't have full movement left) should have red globes.
if(u.has_moved() && u.has_goto()) {
return false;
}

const team &current_team = teams()[u.side() - 1];

map_location locs[6];
get_adjacent_tiles(u.get_location(), locs);
for(int n = 0; n != 6; ++n) {
if (map().on_board(locs[n])) {
const unit_map::const_iterator i = units().find(locs[n]);
if (i.valid() && !i->incapacitated() &&
current_team.is_enemy(i->side())) {
return true;
}

if (u.movement_cost(map()[locs[n]]) <= u.movement_left()) {
return true;
}
}
}

return false;
}

8 changes: 8 additions & 0 deletions src/display_context.hpp
Expand Up @@ -37,8 +37,16 @@ class display_context {
virtual const gamemap & map() const = 0;
virtual const unit_map & units() const = 0;

// Needed for reports

const unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all = false) const;

// From actions:: namespace

bool unit_can_move(const unit & u);

// Dtor

virtual ~display_context() {}
};

Expand Down
4 changes: 2 additions & 2 deletions src/menu_events.cpp
Expand Up @@ -810,7 +810,7 @@ namespace { // Helpers for menu_handler::end_turn()
if ( un->side() == side_num ) {
// @todo whiteboard should take into consideration units that have
// a planned move but can still plan more movement in the same turn
if ( actions::unit_can_move(*un) && !un->user_end_turn()
if ( resources::gameboard->unit_can_move(*un) && !un->user_end_turn()
&& !resources::whiteboard->unit_has_actions(&*un) )
return true;
}
Expand All @@ -825,7 +825,7 @@ namespace { // Helpers for menu_handler::end_turn()
{
for ( unit_map::const_iterator un = units.begin(); un != units.end(); ++un ) {
if ( un->side() == side_num ) {
if ( actions::unit_can_move(*un) && !un->has_moved() && !un->user_end_turn()
if ( resources::gameboard->unit_can_move(*un) && !un->has_moved() && !un->user_end_turn()
&& !resources::whiteboard->unit_has_actions(&*un) )
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mouse_events.cpp
Expand Up @@ -1192,7 +1192,7 @@ bool mouse_handler::unit_in_cycle(unit_map::const_iterator it)
return false;

if (it->side() != side_num_ || it->user_end_turn()
|| gui().fogged(it->get_location()) || !actions::unit_can_move(*it))
|| gui().fogged(it->get_location()) || !board_.unit_can_move(*it))
return false;

if (current_team().is_enemy(int(gui().viewing_team()+1)) &&
Expand Down
2 changes: 1 addition & 1 deletion src/unit.cpp
Expand Up @@ -2032,7 +2032,7 @@ void unit::redraw_unit () const
if (preferences::show_unmoved_orb())
orb_img = &unmoved_orb;
else orb_img = NULL;
} else if ( actions::unit_can_move(*this) ) {
} else if ( resources::gameboard->unit_can_move(*this) ) {
if (preferences::show_partial_orb())
orb_img = &partmoved_orb;
else orb_img = NULL;
Expand Down

0 comments on commit ab23b03

Please sign in to comment.