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

Conversation

Fris0uman
Copy link
Contributor

Summary

SUMMARY: Bugfixes "Only give message about sleeping aid once"

Purpose of change

Fixes #37121

Describe the solution

If base_comfort_value is called from update_needs don't print a message

Describe alternatives you've considered

Testing

Try to sleep on a pillow
Before fix get tons of message
After fix get only one message

Additional context

@esotericist esotericist added <Bugfix> This is a fix for a bug (or closes open issue) Mechanics: Character / Player Character / Player mechanics Info / User Interface Game - player communication, menus, etc. labels Jan 26, 2020
@esotericist
Copy link
Contributor

I'll give this a spin when I get back from errands.

good work on murdering more auto.

@esotericist esotericist added the [C++] Changes (can be) made in C++. Previously named `Code` label Jan 26, 2020
@esotericist esotericist self-assigned this Jan 26, 2020
Copy link
Contributor

@codemime codemime left a comment

Choose a reason for hiding this comment

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

Prior to this change, the function base_comfort_value did two things: calculating the level of comfort (its main purpose) and printing the message (a non-obvious side effect). Now it does three: the side effect starts depending on the flag.

I'd suggest a slightly cleaner approach. You can return a plain struct with the result of the "query" instead. Something along these lines:

struct comfort_response_t {
    comfort_level level;
    const item* aid;
};

So that the function becomes pure, and you can describe the result of the query in the calling code as needed (for example, print different descriptions for different callers, or do something fancy with the acquired information).

add_msg_if_player( m_info, _( "You use your %s for comfort." ), items_it.tname() );
if( message ) {
add_msg_if_player( m_info, _( "You use your %s for comfort." ), items_it.tname() );
}
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.

auto items = g->m.i_at( p );
for( auto &items_it : items ) {
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 );
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't test it right now, but I'm pretty sure there's a bug in here. One can put one pillow inside a vehicle and yet another under it, thus getting the bonus from both pillows (and two messages as a symptom).

{
// 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;
comfort_response.aid = &item( "null" );
Copy link
Contributor

Choose a reason for hiding this comment

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

This stores a pointer to a temporary object. This pointer becomes invalid right after this statement. I suggest you initialise it with a nullptr and check for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, it should be fixed now.

@esotericist esotericist removed their assignment Jan 27, 2020
{
// 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
<Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code` Info / User Interface Game - player communication, menus, etc. Mechanics: Character / Player Character / Player mechanics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Redundant messages while sleeping
6 participants