Skip to content

Commit

Permalink
Merge remote-tracking branch 'jostephd/invisible-teleport' into 1.14
Browse files Browse the repository at this point in the history
* jostephd/invisible-teleport:
  Units: Add is_visible_to_team() overload and use it to improve the previous commit.
  Unit Display: When a unit teleports, don't scroll to the source or destination hex if the unit is invisible there.
  • Loading branch information
jostephd committed Oct 14, 2018
2 parents a926195 + 5234355 commit c3da7b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/units/udisplay.cpp
Expand Up @@ -71,23 +71,32 @@ static void teleport_unit_between(const map_location& a, const map_location& b,
if ( disp.fogged(a) && disp.fogged(b) ) {
return;
}
const display_context& dc = disp.get_disp_context();
const team& viewing_team = dc.get_team(disp.viewing_side());

temp_unit.set_location(a);
if ( !disp.fogged(a) ) { // teleport
const bool a_visible = temp_unit.is_visible_to_team(a, viewing_team, dc, false);
const bool b_visible = temp_unit.is_visible_to_team(b, viewing_team, dc, false);

if ( a_visible ) { // teleport
disp.invalidate(a);
temp_unit.set_facing(a.get_relative_dir(b));
disp.scroll_to_tiles(a, b, game_display::ONSCREEN, true, 0.0, false);
if ( b_visible )
disp.scroll_to_tiles(a, b, game_display::ONSCREEN, true, 0.0, false);
else
disp.scroll_to_tile(a, game_display::ONSCREEN, true, false);
unit_animator animator;
animator.add_animation(&temp_unit,"pre_teleport",a);
animator.start_animations();
animator.wait_for_end();
}

temp_unit.set_location(b);
if ( !disp.fogged(b) ) { // teleport
if ( b_visible ) { // teleport
disp.invalidate(b);
temp_unit.set_facing(a.get_relative_dir(b));
disp.scroll_to_tiles(b, a, game_display::ONSCREEN, true, 0.0, false);
if ( a_visible )
disp.scroll_to_tiles(b, a, game_display::ONSCREEN, true, 0.0, false);
else
disp.scroll_to_tile(b, game_display::ONSCREEN, true, false);
unit_animator animator;
animator.add_animation(&temp_unit,"post_teleport",b);
animator.start_animations();
Expand Down Expand Up @@ -316,10 +325,17 @@ void unit_mover::proceed_to(unit_ptr u, size_t path_index, bool update, bool wai
// Safety check.
path_index = std::min(path_index, path_.size()-1);

for ( ; current_ < path_index; ++current_ )
for ( ; current_ < path_index; ++current_ ) {
// It is possible for path_[current_] and path_[current_+1] not to be adjacent.
// When that is the case, and the unit is invisible at path_[current_], we shouldn't
// scroll to that hex.
std::vector<map_location> locs;
if (!temp_unit_ptr_->invisible(path_[current_], disp_->get_disp_context()))
locs.push_back(path_[current_]);
if (!temp_unit_ptr_->invisible(path_[current_+1], disp_->get_disp_context()))
locs.push_back(path_[current_+1]);
// If the unit can be seen by the viewing side while making this step:
if ( !is_enemy_ || !temp_unit_ptr_->invisible(path_[current_], disp_->get_disp_context()) ||
!temp_unit_ptr_->invisible(path_[current_+1], disp_->get_disp_context()) )
if ( !is_enemy_ || !locs.empty() )
{
// Wait for the previous step to complete before drawing the next one.
wait_for_anims();
Expand All @@ -333,8 +349,7 @@ void unit_mover::proceed_to(unit_ptr u, size_t path_index, bool update, bool wai
// scroll in as much of the remaining path as possible
if ( temp_unit_ptr_->anim_comp().get_animation() )
temp_unit_ptr_->anim_comp().get_animation()->pause_animation();
disp_->scroll_to_tiles(path_.begin() + current_,
path_.end(), game_display::ONSCREEN,
disp_->scroll_to_tiles(locs, game_display::ONSCREEN,
true, false, 0.0, force_scroll_);
if ( temp_unit_ptr_->anim_comp().get_animation() )
temp_unit_ptr_->anim_comp().get_animation()->restart_animation();
Expand All @@ -350,6 +365,7 @@ void unit_mover::proceed_to(unit_ptr u, size_t path_index, bool update, bool wai
teleport_unit_between(path_[current_], path_[current_+1],
*temp_unit_ptr_, *disp_);
}
}

// Update the unit's facing.
u->set_facing(temp_unit_ptr_->facing());
Expand Down
5 changes: 5 additions & 0 deletions src/units/unit.cpp
Expand Up @@ -2358,6 +2358,11 @@ bool unit::invisible(const map_location& loc, const display_context& dc, bool se
bool unit::is_visible_to_team(const team& team,const display_context& dc, bool const see_all) const
{
const map_location& loc = get_location();
return is_visible_to_team(loc, team, dc, see_all);
}

bool unit::is_visible_to_team(const map_location& loc, const team& team, const display_context& dc, bool const see_all) const
{
if(!dc.map().on_board(loc)) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/units/unit.hpp
Expand Up @@ -1571,6 +1571,8 @@ class unit
bool invisible(const map_location& loc, const display_context& dc, bool see_all = true) const;

bool is_visible_to_team(const team& team, const display_context& dc, bool const see_all = true) const;
/// Return true if the unit would be visible to team if its location were loc.
bool is_visible_to_team(const map_location& loc, const team& team, const display_context& dc, bool const see_all = true) const;

/**
* Serializes the current unit metadata values.
Expand Down

0 comments on commit c3da7b8

Please sign in to comment.