From ecbdf12c77d031c7a6703a3f537a0459792b4b40 Mon Sep 17 00:00:00 2001 From: John Bytheway <jbytheway@gmail.com> Date: Fri, 1 May 2020 21:54:02 -0400 Subject: [PATCH] Consolidate waking up code Almost the same code was present in avatar::wake_up and player_hardcoded_effects. Consolidate the two into one implementation. --- src/avatar.cpp | 7 ++++++- src/character.cpp | 12 +++++++++--- src/player_hardcoded_effects.cpp | 22 +++------------------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/avatar.cpp b/src/avatar.cpp index 546f339c6a5ab..1c8edb81e7ea9 100644 --- a/src/avatar.cpp +++ b/src/avatar.cpp @@ -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" ); @@ -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 { diff --git a/src/character.cpp b/src/character.cpp index 8db5df5dc0720..1d1b1a9172319 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -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 ); } diff --git a/src/player_hardcoded_effects.cpp b/src/player_hardcoded_effects.cpp index 98b40396bec51..90e745e920536 100644 --- a/src/player_hardcoded_effects.cpp +++ b/src/player_hardcoded_effects.cpp @@ -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(); @@ -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 ); - } + wake_up(); } } else if( id == effect_alarm_clock ) { if( in_sleep_state() ) {