Skip to content

Commit

Permalink
Merge pull request #76950 from sparr/optimize_get_dangerous_tile
Browse files Browse the repository at this point in the history
Optimize get_dangerous_tile, also is_ and prompt_
  • Loading branch information
Maleclypse authored Oct 15, 2024
2 parents e3ddac1 + 7fa5c98 commit b7cfa5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
44 changes: 29 additions & 15 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10312,30 +10312,35 @@ 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<std::string> *harmful_stuff ) const
{
if( u.has_effect( effect_stunned ) || u.has_effect( effect_psi_stunned ) ) {
return true;
}

std::vector<std::string> 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;
}
return true;
}

std::vector<std::string> game::get_dangerous_tile( const tripoint &dest_loc ) const
std::vector<std::string> 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
Expand Down Expand Up @@ -10393,16 +10398,25 @@ std::vector<std::string> 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 = {
Expand Down Expand Up @@ -10431,7 +10445,7 @@ std::vector<std::string> 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;
Expand Down Expand Up @@ -10562,22 +10576,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<std::string> harmful_stuff = get_dangerous_tile( dest_loc );
std::vector<std::string> 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<std::string>( "DANGEROUS_TERRAIN_WARNING_PROMPT" ) == "ALWAYS" &&
!prompt_dangerous_tile( dest_loc ) ) {
!prompt_dangerous_tile( dest_loc, &harmful_stuff ) ) {
return true;
} else if( get_option<std::string>( "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<std::string>( "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 ) );
Expand Down
5 changes: 3 additions & 2 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,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<std::string> get_dangerous_tile( const tripoint &dest_loc ) const;
bool prompt_dangerous_tile( const tripoint &dest_loc ) const;
std::vector<std::string> get_dangerous_tile( const tripoint &dest_loc, size_t max = 0 ) const;
bool prompt_dangerous_tile( const tripoint &dest_loc,
std::vector<std::string> *harmful_stuff = nullptr ) const;
// Pick up items from the given point
// TODO: Get rid of untyped overloads.
void pickup( const tripoint &p );
Expand Down

0 comments on commit b7cfa5d

Please sign in to comment.