Skip to content

Commit

Permalink
Allow changing Z-level if there is a creature in the way, but we have…
Browse files Browse the repository at this point in the history
… a spot to displace it into
  • Loading branch information
olanti-p authored and kevingranade committed May 8, 2020
1 parent 19a6844 commit ac82610
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10157,6 +10157,23 @@ static cata::optional<tripoint> point_selection_menu( const std::vector<tripoint
return pts[ret];
}

static cata::optional<tripoint> find_empty_spot_nearby( const tripoint &pos )
{
for( const tripoint &p : g->m.points_in_radius( pos, 1 ) ) {
if( p == pos ) {
continue;
}
if( g->m.impassable( p ) ) {
continue;
}
if( g->critter_at( p ) ) {
continue;
}
return p;
}
return cata::nullopt;
}

void game::vertical_move( int movez, bool force )
{
if( u.is_mounted() ) {
Expand Down Expand Up @@ -10445,17 +10462,8 @@ void game::vertical_move( int movez, bool force )
if( critter_at<npc>( u.pos(), true ) || critter_at<monster>( u.pos(), true ) ) {
std::string crit_name;
bool player_displace = false;
tripoint displace;
for( const tripoint &elem : m.points_in_radius( u.pos(), 1 ) ) {
if( elem == u.pos() ) {
continue;
}
if( !m.impassable( elem ) ) {
displace = elem;
break;
}
}
if( displace != tripoint_zero ) {
cata::optional<tripoint> displace = find_empty_spot_nearby( u.pos() );
if( displace.has_value() ) {
npc *guy = g->critter_at<npc>( u.pos(), true );
if( guy ) {
crit_name = guy->get_name();
Expand All @@ -10477,17 +10485,19 @@ void game::vertical_move( int movez, bool force )
if( mon && !mon->mounted_player ) {
crit_name = mon->get_name();
if( mon->friendly == -1 ) {
mon->setpos( displace );
mon->setpos( *displace );
add_msg( _( "Your %s moves out of the way for you." ), mon->get_name() );
} else {
player_displace = true;
}
}
if( player_displace ) {
u.setpos( displace );
u.setpos( *displace );
u.moves -= 20;
add_msg( _( "You push past %s blocking the way." ), crit_name );
}
} else {
debugmsg( "Failed to find a spot to displace into." );
}
}
if( !npcs_to_bring.empty() ) {
Expand Down Expand Up @@ -10626,8 +10636,11 @@ cata::optional<tripoint> game::find_or_make_stairs( map &mp, const int z_after,

if( stairs.has_value() ) {
if( Creature *blocking_creature = critter_at( stairs.value() ) ) {
add_msg( _( "There's a %s in the way!" ), blocking_creature->get_name() );
return cata::nullopt;
bool can_displace = find_empty_spot_nearby( *stairs ).has_value();
if( !can_displace ) {
add_msg( _( "There's a %s in the way!" ), blocking_creature->get_name() );
return cata::nullopt;
}
}
return stairs;
}
Expand Down

0 comments on commit ac82610

Please sign in to comment.