From 7fa5c98fd5871ac02888bb1626e245a72035d677 Mon Sep 17 00:00:00 2001 From: "Clarence \"Sparr\" Risher" Date: Thu, 10 Oct 2024 14:56:06 -0400 Subject: [PATCH] Optimize get_dangerous_tile, also is_ and prompt_ --- src/game.cpp | 44 +++++++++++++++++++++++++++++--------------- src/game.h | 5 +++-- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index e932ff268b8f7..f376f113e2f39 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -10267,22 +10267,26 @@ bool game::disable_robot( const tripoint_bub_ms &p ) bool game::is_dangerous_tile( const tripoint &dest_loc ) const { - return !get_dangerous_tile( dest_loc ).empty(); + return !get_dangerous_tile( dest_loc, 1 ).empty(); } -bool game::prompt_dangerous_tile( const tripoint &dest_loc ) const +bool game::prompt_dangerous_tile( const tripoint &dest_loc, + std::vector *harmful_stuff ) const { if( u.has_effect( effect_stunned ) || u.has_effect( effect_psi_stunned ) ) { return true; } - std::vector harmful_stuff = get_dangerous_tile( dest_loc ); + if( harmful_stuff == nullptr ) { + auto dangerous_tile = get_dangerous_tile( dest_loc ); + harmful_stuff = &dangerous_tile; + } - if( !harmful_stuff.empty() && - !query_yn( _( "Really step into %s?" ), enumerate_as_string( harmful_stuff ) ) ) { + if( !harmful_stuff->empty() && + !query_yn( _( "Really step into %s?" ), enumerate_as_string( *harmful_stuff ) ) ) { return false; } - if( !harmful_stuff.empty() && u.is_mounted() && m.tr_at( dest_loc ) == tr_ledge ) { + if( !harmful_stuff->empty() && u.is_mounted() && m.tr_at( dest_loc ) == tr_ledge ) { add_msg( m_warning, _( "Your %s refuses to move over that ledge!" ), u.mounted_creature->get_name() ); return false; @@ -10290,7 +10294,8 @@ bool game::prompt_dangerous_tile( const tripoint &dest_loc ) const return true; } -std::vector game::get_dangerous_tile( const tripoint &dest_loc ) const +std::vector game::get_dangerous_tile( const tripoint &dest_loc, + const size_t max ) const { if( u.is_blind() ) { return {}; // blinded players don't see dangerous tiles @@ -10348,16 +10353,25 @@ std::vector game::get_dangerous_tile( const tripoint &dest_loc ) co } harmful_stuff.push_back( e.second.name() ); + if( harmful_stuff.size() == max ) { + return harmful_stuff; + } } const trap &tr = m.tr_at( dest_loc ); // HACK: Hack for now, later ledge should stop being a trap if( tr == tr_ledge ) { if( !veh_dest && !u.has_effect_with_flag( json_flag_LEVITATION ) ) { - harmful_stuff.emplace_back( tr.name() ); + harmful_stuff.push_back( tr.name() ); + if( harmful_stuff.size() == max ) { + return harmful_stuff; + } } } else if( tr.can_see( dest_loc, u ) && !tr.is_benign() && !veh_dest ) { - harmful_stuff.emplace_back( tr.name() ); + harmful_stuff.push_back( tr.name() ); + if( harmful_stuff.size() == max ) { + return harmful_stuff; + } } static const std::set< bodypart_str_id > sharp_bps = { @@ -10386,7 +10400,7 @@ std::vector game::get_dangerous_tile( const tripoint &dest_loc ) co !( u.is_mounted() && u.mounted_creature->get_armor_type( damage_cut, bodypart_id( "torso" ) ) >= 10 ) && !std::all_of( sharp_bps.begin(), sharp_bps.end(), sharp_bp_check ) ) { - harmful_stuff.emplace_back( m.name( dest_loc ) ); + harmful_stuff.push_back( m.name( dest_loc ) ); } return harmful_stuff; @@ -10517,22 +10531,22 @@ bool game::walk_move( const tripoint &dest_loc, const bool via_ramp, const bool } u.set_underwater( false ); - if( !shifting_furniture && !pushing && is_dangerous_tile( dest_loc ) ) { - std::vector harmful_stuff = get_dangerous_tile( dest_loc ); + std::vector harmful_stuff = get_dangerous_tile( dest_loc ); + if( !shifting_furniture && !pushing && !harmful_stuff.empty() ) { if( harmful_stuff.size() == 1 && harmful_stuff[0] == "ledge" ) { iexamine::ledge( u, tripoint_bub_ms( dest_loc ) ); return true; } else if( get_option( "DANGEROUS_TERRAIN_WARNING_PROMPT" ) == "ALWAYS" && - !prompt_dangerous_tile( dest_loc ) ) { + !prompt_dangerous_tile( dest_loc, &harmful_stuff ) ) { return true; } else if( get_option( "DANGEROUS_TERRAIN_WARNING_PROMPT" ) == "RUNNING" && - ( !u.is_running() || !prompt_dangerous_tile( dest_loc ) ) ) { + ( !u.is_running() || !prompt_dangerous_tile( dest_loc, &harmful_stuff ) ) ) { add_msg( m_warning, _( "Stepping into that %1$s looks risky. Run into it if you wish to enter anyway." ), enumerate_as_string( harmful_stuff ) ); return true; } else if( get_option( "DANGEROUS_TERRAIN_WARNING_PROMPT" ) == "CROUCHING" && - ( !u.is_crouching() || !prompt_dangerous_tile( dest_loc ) ) ) { + ( !u.is_crouching() || !prompt_dangerous_tile( dest_loc, &harmful_stuff ) ) ) { add_msg( m_warning, _( "Stepping into that %1$s looks risky. Crouch and move into it if you wish to enter anyway." ), enumerate_as_string( harmful_stuff ) ); diff --git a/src/game.h b/src/game.h index 77e7af867e190..b43d6f572e9b9 100644 --- a/src/game.h +++ b/src/game.h @@ -955,8 +955,9 @@ class game void mon_info_update( ); //Update seen monsters information void cleanup_dead(); // Delete any dead NPCs/monsters bool is_dangerous_tile( const tripoint &dest_loc ) const; - std::vector get_dangerous_tile( const tripoint &dest_loc ) const; - bool prompt_dangerous_tile( const tripoint &dest_loc ) const; + std::vector get_dangerous_tile( const tripoint &dest_loc, size_t max = 0 ) const; + bool prompt_dangerous_tile( const tripoint &dest_loc, + std::vector *harmful_stuff = nullptr ) const; // Pick up items from the given point // TODO: Get rid of untyped overloads. void pickup( const tripoint &p );