move a helper function for unit invisibility to display_context

This commit is contained in:
Chris Beck 2014-07-02 19:04:31 -04:00
parent 9f76d9000f
commit 74fe722119
3 changed files with 37 additions and 25 deletions

View file

@ -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;

View file

@ -55,6 +55,15 @@ public:
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;

View file

@ -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"
@ -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) {