Skip to content

Commit

Permalink
move a helper function for unit invisibility to display_context
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jul 24, 2014
1 parent 9f76d90 commit 74fe722
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
26 changes: 26 additions & 0 deletions src/display_context.cpp
Expand Up @@ -22,6 +22,32 @@

#include <boost/foreach.hpp>

bool display_context::would_be_discovered(const map_location & loc, int side_num, bool see_all)
{
map_location adjs[6];
get_adjacent_tiles(loc,adjs);

BOOST_FOREACH(const map_location &u_loc, adjs)
{
unit_map::const_iterator u_it = units().find(u_loc);
if (!u_it.valid()) {
continue;
}
const unit & u = *u_it;
if (teams()[side_num-1].is_enemy(u.side()) && !u.incapacitated()) {
// Enemy spotted in adjacent tiles, check if we can see him.
// Watch out to call invisible with see_all=true to avoid infinite recursive calls!
if(see_all) {
return true;
} else if (!teams()[side_num-1].fogged(u_loc)
&& !u.invisible(u_loc, true)) {
return true;
}
}
}
return false;
}

const unit * display_context::get_visible_unit(const map_location & loc, const team &current_team, bool see_all) const
{
if (!map().on_board(loc)) return NULL;
Expand Down
9 changes: 9 additions & 0 deletions src/display_context.hpp
Expand Up @@ -55,6 +55,15 @@ class display_context {
virtual const gamemap & map() const = 0;
virtual const unit_map & units() const = 0;

// Helper for is_visible_to_team

/**
* Given a location and a side number, indicates whether an invisible unit of that side at that
* location would be revealed (perhaps ambushed), based on what team side_num can see.
* If see_all is true then the calculation ignores fog, and enemy ambushers.
*/
bool would_be_discovered(const map_location & loc, int side_num, bool see_all = true);

// Needed for reports

const unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all = false) const;
Expand Down
27 changes: 2 additions & 25 deletions src/unit.cpp
Expand Up @@ -20,6 +20,7 @@
#include "unit.hpp"
#include "global.hpp"

#include "display_context.hpp"
#include "formula_string_utils.hpp" // for vgettext
#include "game_board.hpp" // for game_board
#include "game_data.hpp"
Expand Down Expand Up @@ -2012,31 +2013,7 @@ bool unit::invisible(const map_location& loc, bool see_all) const
static const std::string hides("hides");
bool is_inv = get_ability_bool(hides,loc);
if(is_inv){
const std::vector<team>& teams = *resources::teams;

map_location adjs[6];
get_adjacent_tiles(loc,adjs);

BOOST_FOREACH(const map_location &u_loc, adjs)
{
unit_map::iterator u_it = resources::units->find(u_loc);
if (!u_it.valid()) {
continue;
}
unit & u = *u_it;
if (teams[side_-1].is_enemy(u.side()) && !u.incapacitated()) {
// Enemy spotted in adjacent tiles, check if we can see him.
// Watch out to call invisible with see_all=true to avoid infinite recursive calls!
if(see_all) {
is_inv = false;
break;
} else if (!teams[side_-1].fogged(u_loc)
&& !u.invisible(u_loc, true)) {
is_inv = false;
break;
}
}
}
is_inv = (resources::gameboard ? !resources::gameboard->would_be_discovered(loc, side_,see_all) : true);
}

if(see_all) {
Expand Down

0 comments on commit 74fe722

Please sign in to comment.