From 2430ee4ec3581b9ab988f5ef38d179eeb0d80086 Mon Sep 17 00:00:00 2001 From: Joseph Chung <84266961+CLIDragon@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:04:26 +1000 Subject: [PATCH] Extract game::find_stairs from game::find_or_make_stairs (#75963) Part of #74945. Sometimes we want to find stairs without spontaneously generating them. --- src/game.cpp | 38 ++++++++++++++++++++++++-------------- src/game.h | 1 + 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index dbece2bb4850d..82af9c1a5ade3 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -12307,31 +12307,29 @@ void game::start_hauling( const tripoint &pos ) u.assign_activity( actor ); } -std::optional game::find_or_make_stairs( map &mp, const int z_after, bool &rope_ladder, - bool peeking, const tripoint &pos ) +std::optional 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 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 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 ) ) || @@ -12344,6 +12342,18 @@ std::optional game::find_or_make_stairs( map &mp, const int z_after, b } } + return stairs; +} + +std::optional 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 stairs = find_stairs( mp, z_after, pos ); + creature_tracker &creatures = get_creature_tracker(); if( stairs.has_value() ) { if( !is_avatar ) { diff --git a/src/game.h b/src/game.h index 517fb0520e85c..92244afa65774 100644 --- a/src/game.h +++ b/src/game.h @@ -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 find_stairs( const map &mp, int z_after, const tripoint &pos ); std::optional find_or_make_stairs( map &mp, int z_after, bool &rope_ladder, bool peeking, const tripoint &pos ); /*