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

Consolidate waking up code #40069

Merged
merged 1 commit into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static const bionic_id bio_eye_optic( "bio_eye_optic" );
static const bionic_id bio_memory( "bio_memory" );
static const bionic_id bio_watch( "bio_watch" );

static const efftype_id effect_alarm_clock( "alarm_clock" );
static const efftype_id effect_contacts( "contacts" );
static const efftype_id effect_depressants( "depressants" );
static const efftype_id effect_happy( "happy" );
Expand Down Expand Up @@ -941,7 +942,11 @@ void avatar::wake_up()
if( calendar::turn - get_effect( effect_sleep ).get_start_time() > 2_hours ) {
print_health();
}
if( has_effect( effect_slept_through_alarm ) ) {
// alarm was set and player hasn't slept through the alarm.
if( has_effect( effect_alarm_clock ) && !has_effect( effect_slept_through_alarm ) ) {
add_msg( _( "It looks like you woke up before your alarm." ) );
remove_effect( effect_alarm_clock );
} else if( has_effect( effect_slept_through_alarm ) ) {
if( has_bionic( bio_watch ) ) {
add_msg( m_warning, _( "It looks like you've slept through your internal alarm…" ) );
} else {
Expand Down
12 changes: 9 additions & 3 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7679,11 +7679,17 @@ void Character::cough( bool harmful, int loudness )

void Character::wake_up()
{
remove_effect( effect_sleep );
// Do not remove effect_sleep or effect_alarm_clock now otherwise it invalidates an effect
// iterator in player::process_effects().
// We just set it for later removal (also happening in player::process_effects(), so no side
// effects) with a duration of 0 turns.

if( has_effect( effect_sleep ) ) {
g->events().send<event_type::character_wakes_up>( getID() );
get_effect( effect_sleep ).set_duration( 0_turns );
}
remove_effect( effect_slept_through_alarm );
remove_effect( effect_lying_down );
// Do not remove effect_alarm_clock now otherwise it invalidates an effect iterator in player::process_effects().
// We just set it for later removal (also happening in player::process_effects(), so no side effects) with a duration of 0 turns.
if( has_effect( effect_alarm_clock ) ) {
get_effect( effect_alarm_clock ).set_duration( 0_turns );
}
Expand Down
22 changes: 3 additions & 19 deletions src/player_hardcoded_effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ void player::hardcoded_effects( effect &it )
return;
}

const time_point start = it.get_start_time();
const time_duration dur = it.get_duration();
int intense = it.get_intensity();
body_part bp = it.get_bp();
Expand Down Expand Up @@ -1235,25 +1234,10 @@ void player::hardcoded_effects( effect &it )
}
}

// A bit of a hack: check if we are about to wake up for any reason, including regular timing out of sleep
// A bit of a hack: check if we are about to wake up for any reason, including regular
// timing out of sleep
if( dur == 1_turns || woke_up ) {
g->events().send<event_type::character_wakes_up>( getID() );
if( calendar::turn - start > 2_hours ) {
print_health();
}
// alarm was set and player hasn't slept through the alarm.
if( has_effect( effect_alarm_clock ) && !has_effect( effect_slept_through_alarm ) ) {
add_msg_if_player( _( "It looks like you woke up just before your alarm." ) );
remove_effect( effect_alarm_clock );
} else if( has_effect( effect_slept_through_alarm ) ) { // slept though the alarm.
if( has_bionic( bio_watch ) ) {
add_msg_if_player( m_warning, _( "It looks like you've slept through your internal alarm…" ) );
} else {
add_msg_if_player( m_warning, _( "It looks like you've slept through the alarm…" ) );
}
get_effect( effect_slept_through_alarm ).set_duration( 0_turns );
remove_effect( effect_alarm_clock );
Comment on lines -1254 to -1255
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see these two lines present after the change. Is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

avatar::wake_up calls Character::wake_up, and that removes these two effects (albeit with slightly different code).

}
wake_up();
}
} else if( id == effect_alarm_clock ) {
if( in_sleep_state() ) {
Expand Down