Skip to content

Commit

Permalink
Extract game::find_stairs from game::find_or_make_stairs (#75963)
Browse files Browse the repository at this point in the history
Part of #74945. Sometimes we want to find stairs without spontaneously
generating them.
  • Loading branch information
CLIDragon authored Aug 27, 2024
1 parent 871d063 commit 2430ee4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12307,31 +12307,29 @@ void game::start_hauling( const tripoint &pos )
u.assign_activity( actor );
}

std::optional<tripoint> game::find_or_make_stairs( map &mp, const int z_after, bool &rope_ladder,
bool peeking, const tripoint &pos )
std::optional<tripoint> game::find_stairs( const map &mp, int z_after, const tripoint &pos )
{
const bool is_avatar = u.pos() == pos;
const int omtilesz = SEEX * 2;
real_coords rc( mp.getabs( pos.xy() ) );
tripoint omtile_align_start( mp.getlocal( rc.begin_om_pos() ), z_after );
tripoint omtile_align_end( omtile_align_start + point( -1 + omtilesz, -1 + omtilesz ) );

// Try to find the stairs.
std::optional<tripoint> stairs;
int best = INT_MAX;
const int movez = z_after - pos.z;
const bool going_down_1 = movez == -1;
const bool going_up_1 = movez == 1;

// If there are stairs on the same x and y as we currently are, use those
if( going_down_1 && mp.has_flag( ter_furn_flag::TFLAG_GOES_UP, pos + tripoint_below ) ) {
stairs.emplace( pos + tripoint_below );
return pos + tripoint_below;
}
if( going_up_1 && mp.has_flag( ter_furn_flag::TFLAG_GOES_DOWN, pos + tripoint_above ) ) {
stairs.emplace( pos + tripoint_above );
return pos + tripoint_above;
}
// We did not find stairs directly above or below, so search the map for them
// If there's empty space right below us, we can just go down that way.
if( !stairs.has_value() && get_map().tr_at( u.pos() ) != tr_ledge ) {
int best = INT_MAX;
std::optional<tripoint> stairs;
const int omtilesz = SEEX * 2 - 1;
real_coords rc( mp.getabs( pos.xy() ) );
tripoint omtile_align_start( mp.getlocal( rc.begin_om_pos() ), z_after );
tripoint omtile_align_end( omtile_align_start + point( omtilesz, omtilesz ) );

if( get_map().tr_at( u.pos() ) != tr_ledge ) {
for( const tripoint &dest : mp.points_in_rectangle( omtile_align_start, omtile_align_end ) ) {
if( rl_dist( u.pos(), dest ) <= best &&
( ( going_down_1 && mp.has_flag( ter_furn_flag::TFLAG_GOES_UP, dest ) ) ||
Expand All @@ -12344,6 +12342,18 @@ std::optional<tripoint> game::find_or_make_stairs( map &mp, const int z_after, b
}
}

return stairs;
}

std::optional<tripoint> game::find_or_make_stairs( map &mp, const int z_after, bool &rope_ladder,
bool peeking, const tripoint &pos )
{
const bool is_avatar = u.pos() == pos;
const int movez = z_after - pos.z;

// Try to find the stairs.
std::optional<tripoint> stairs = find_stairs( mp, z_after, pos );

creature_tracker &creatures = get_creature_tracker();
if( stairs.has_value() ) {
if( !is_avatar ) {
Expand Down
1 change: 1 addition & 0 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ class game
/** Returns the other end of the stairs (if any). May query, affect u etc.
* @param pos Disable queries and msgs if not the same position as player.
*/
std::optional<tripoint> find_stairs( const map &mp, int z_after, const tripoint &pos );
std::optional<tripoint> find_or_make_stairs( map &mp, int z_after, bool &rope_ladder,
bool peeking, const tripoint &pos );
/*
Expand Down

0 comments on commit 2430ee4

Please sign in to comment.