Skip to content

Commit

Permalink
Consolidate waking up code
Browse files Browse the repository at this point in the history
Almost the same code was present in avatar::wake_up and
player_hardcoded_effects.  Consolidate the two into one implementation.
  • Loading branch information
jbytheway committed May 2, 2020
1 parent cb7d243 commit ecbdf12
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
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 );
}
wake_up();
}
} else if( id == effect_alarm_clock ) {
if( in_sleep_state() ) {
Expand Down

0 comments on commit ecbdf12

Please sign in to comment.