From 74fe722119fcdbf16e98b63201c0147aab339836 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Wed, 2 Jul 2014 19:04:31 -0400 Subject: [PATCH] move a helper function for unit invisibility to display_context --- src/display_context.cpp | 26 ++++++++++++++++++++++++++ src/display_context.hpp | 9 +++++++++ src/unit.cpp | 27 ++------------------------- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/display_context.cpp b/src/display_context.cpp index cedea5e1f75f..018861e4008e 100644 --- a/src/display_context.cpp +++ b/src/display_context.cpp @@ -22,6 +22,32 @@ #include +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 ¤t_team, bool see_all) const { if (!map().on_board(loc)) return NULL; diff --git a/src/display_context.hpp b/src/display_context.hpp index 73b304874fc4..75f6b499c6eb 100644 --- a/src/display_context.hpp +++ b/src/display_context.hpp @@ -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 ¤t_team, bool see_all = false) const; diff --git a/src/unit.cpp b/src/unit.cpp index 6e4762c92645..51321090b62b 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -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& 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) {