Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only give message about sleeping aid once #37404

Merged
merged 8 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3956,7 +3956,7 @@ void Character::update_needs( int rate_multiplier )
rest_modifier += 1;
}

comfort_level comfort = base_comfort_value( pos() );
const comfort_level comfort = base_comfort_value( pos() ).level;

if( comfort >= comfort_level::very_comfortable ) {
rest_modifier *= 3;
Expand Down Expand Up @@ -4878,14 +4878,16 @@ void Character::temp_equalizer( body_part bp1, body_part bp2 )
temp_cur[bp1] += diff;
}

Character::comfort_level Character::base_comfort_value( const tripoint &p ) const
Character::comfort_response_t Character::base_comfort_value( const tripoint &p ) const
{
// Comfort of sleeping spots is "objective", while sleep_spot( p ) is "subjective"
// As in the latter also checks for fatigue and other variables while this function
// only looks at the base comfyness of something. It's still subjective, in a sense,
// as arachnids who sleep in webs will find most places comfortable for instance.
int comfort = 0;

comfort_response_t comfort_response;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the pointer is uninitialised. Reading it is undefined behaviour.


bool plantsleep = has_trait( trait_CHLOROMORPH );
bool fungaloid_cosplay = has_trait( trait_M_SKIN3 );
bool websleep = has_trait( trait_WEB_WALKER );
Expand All @@ -4908,16 +4910,15 @@ Character::comfort_level Character::base_comfort_value( const tripoint &p ) cons
comfort += 1 + static_cast<int>( comfort_level::slightly_comfortable );
// Note: shelled individuals can still use sleeping aids!
} else if( vp ) {
vehicle &veh = vp->vehicle();
const cata::optional<vpart_reference> carg = vp.part_with_feature( "CARGO", false );
const cata::optional<vpart_reference> board = vp.part_with_feature( "BOARDABLE", true );
if( carg ) {
vehicle_stack items = veh.get_items( carg->part_index() );
for( auto &items_it : items ) {
const vehicle_stack items = vp->vehicle().get_items( carg->part_index() );
for( const item &items_it : items ) {
if( items_it.has_flag( "SLEEP_AID" ) ) {
// Note: BED + SLEEP_AID = 9 pts, or 1 pt below very_comfortable
comfort += 1 + static_cast<int>( comfort_level::slightly_comfortable );
add_msg_if_player( m_info, _( "You use your %s for comfort." ), items_it.tname() );
comfort_response.aid = &items_it;
break; // prevents using more than 1 sleep aid
}
}
Expand Down Expand Up @@ -4948,16 +4949,17 @@ Character::comfort_level Character::base_comfort_value( const tripoint &p ) cons
comfort -= g->m.move_cost( p );
}

auto items = g->m.i_at( p );
for( auto &items_it : items ) {
if( items_it.has_flag( "SLEEP_AID" ) ) {
// Note: BED + SLEEP_AID = 9 pts, or 1 pt below very_comfortable
comfort += 1 + static_cast<int>( comfort_level::slightly_comfortable );
add_msg_if_player( m_info, _( "You use your %s for comfort." ), items_it.tname() );
break; // prevents using more than 1 sleep aid
if( comfort_response.aid == nullptr ) {
const map_stack items = g->m.i_at( p );
for( const item &items_it : items ) {
if( items_it.has_flag( "SLEEP_AID" ) ) {
// Note: BED + SLEEP_AID = 9 pts, or 1 pt below very_comfortable
comfort += 1 + static_cast<int>( comfort_level::slightly_comfortable );
comfort_response.aid = &items_it;
break; // prevents using more than 1 sleep aid
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to also deduplicate this snippet.

}
}

if( fungaloid_cosplay && g->m.has_flag_ter_or_furn( "FUNGUS", pos() ) ) {
comfort += static_cast<int>( comfort_level::very_comfortable );
} else if( watersleep && g->m.has_flag_ter( "SWIMMABLE", pos() ) ) {
Expand Down Expand Up @@ -4993,16 +4995,17 @@ Character::comfort_level Character::base_comfort_value( const tripoint &p ) cons
}

if( comfort > static_cast<int>( comfort_level::comfortable ) ) {
return comfort_level::very_comfortable;
comfort_response.level = comfort_level::very_comfortable;
} else if( comfort > static_cast<int>( comfort_level::slightly_comfortable ) ) {
return comfort_level::comfortable;
comfort_response.level = comfort_level::comfortable;
} else if( comfort > static_cast<int>( comfort_level::neutral ) ) {
return comfort_level::slightly_comfortable;
comfort_response.level = comfort_level::slightly_comfortable;
} else if( comfort == static_cast<int>( comfort_level::neutral ) ) {
return comfort_level::neutral;
comfort_response.level = comfort_level::neutral;
} else {
return comfort_level::uncomfortable;
comfort_response.level = comfort_level::uncomfortable;
}
return comfort_response;
}

int Character::blood_loss( body_part bp ) const
Expand Down
6 changes: 5 additions & 1 deletion src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,12 @@ class Character : public Creature, public visitable<Character>
/** Equalizes heat between body parts */
void temp_equalizer( body_part bp1, body_part bp2 );

struct comfort_response_t {
comfort_level level;
const item *aid;
};
/** Rate point's ability to serve as a bed. Only takes certain mutations into account, and not fatigue nor stimulants. */
comfort_level base_comfort_value( const tripoint &p ) const;
comfort_response_t base_comfort_value( const tripoint &p ) const;

/** Define blood loss (in percents) */
int blood_loss( body_part bp ) const;
Expand Down
8 changes: 6 additions & 2 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4466,8 +4466,12 @@ void player::try_to_sleep( const time_duration &dur )
int player::sleep_spot( const tripoint &p ) const
{
const int current_stim = get_stim();
comfort_level base_level = base_comfort_value( p );
int sleepy = static_cast<int>( base_level );
const comfort_response_t comfort_info = base_comfort_value( p );
if( comfort_info.aid != nullptr ) {
add_msg_if_player( m_info, _( "You use your %s for comfort." ), comfort_info.aid->tname() );
}

int sleepy = static_cast<int>( comfort_info.level );
bool watersleep = has_trait( trait_WATERSLEEP );

if( has_addiction( ADD_SLEEP ) ) {
Expand Down