Skip to content

Commit

Permalink
Merge pull request #39951 from wapcaplet/cloudy-moon
Browse files Browse the repository at this point in the history
Add sunrise, sunshine, sunset, and daylight tests
  • Loading branch information
ifreund authored Apr 29, 2020
2 parents e5aae8e + d4bccca commit 12f3da5
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 16 deletions.
28 changes: 19 additions & 9 deletions src/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ moon_phase get_moon_phase( const time_point &p )
return static_cast<moon_phase>( current_phase );
}

// TODO: Refactor sunrise / sunset
// The only difference between them is the start_hours array
time_point sunrise( const time_point &p )
{
static_assert( static_cast<int>( SPRING ) == 0,
Expand Down Expand Up @@ -131,23 +133,32 @@ bool is_night( const time_point &p )
const time_duration sunrise = time_past_midnight( ::sunrise( p ) );
const time_duration sunset = time_past_midnight( ::sunset( p ) );

return now > sunset + twilight_duration || now < sunrise;
return now >= sunset + twilight_duration || now <= sunrise;
}

bool is_sunset_now( const time_point &p )
bool is_day( const time_point &p )
{
const time_duration now = time_past_midnight( p );
const time_duration sunrise = time_past_midnight( ::sunrise( p ) );
const time_duration sunset = time_past_midnight( ::sunset( p ) );

return now >= sunrise + twilight_duration && now <= sunset;
}

bool is_dusk( const time_point &p )
{
const time_duration now = time_past_midnight( p );
const time_duration sunset = time_past_midnight( ::sunset( p ) );

return now > sunset && now < sunset + twilight_duration;
return now >= sunset && now <= sunset + twilight_duration;
}

bool is_sunrise_now( const time_point &p )
bool is_dawn( const time_point &p )
{
const time_duration now = time_past_midnight( p );
const time_duration sunrise = time_past_midnight( ::sunrise( p ) );

return now > sunrise && now < sunrise + twilight_duration;
return now >= sunrise && now <= sunrise + twilight_duration;
}

double current_daylight_level( const time_point &p )
Expand Down Expand Up @@ -194,13 +205,12 @@ float sunlight( const time_point &p, const bool vision )
const int moonlight = vision ? 1 + static_cast<int>( current_phase * moonlight_per_quarter ) :
0;

if( now > sunset + twilight_duration || now < sunrise ) {
// Night
if( is_night( p ) ) {
return moonlight;
} else if( now >= sunrise && now <= sunrise + twilight_duration ) {
} else if( is_dawn( p ) ) {
const double percent = ( now - sunrise ) / twilight_duration;
return static_cast<double>( moonlight ) * ( 1. - percent ) + daylight_level * percent;
} else if( now >= sunset && now <= sunset + twilight_duration ) {
} else if( is_dusk( p ) ) {
const double percent = ( now - sunset ) / twilight_duration;
return daylight_level * ( 1. - percent ) + static_cast<double>( moonlight ) * percent;
} else {
Expand Down
12 changes: 7 additions & 5 deletions src/calendar.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,14 @@ time_point sunset( const time_point &p );
time_point daylight_time( const time_point &p );
/** Returns the time it gets dark based on sunset */
time_point night_time( const time_point &p );
/** Returns whether it's currently after sunset + TWILIGHT_SECONDS or before sunrise - TWILIGHT_SECONDS. */
/** Returns true if it's currently night time - after dusk and before dawn. */
bool is_night( const time_point &p );
/** Returns true if it's currently after sunset and before sunset + TWILIGHT_SECONDS. */
bool is_sunset_now( const time_point &p );
/** Returns true if it's currently after sunrise and before sunrise + TWILIGHT_SECONDS. */
bool is_sunrise_now( const time_point &p );
/** Returns true if it's currently day time - after dawn and before dusk. */
bool is_day( const time_point &p );
/** Returns true if it's currently dusk - between sunset and and twilight_duration after sunset. */
bool is_dusk( const time_point &p );
/** Returns true if it's currently dawn - between sunrise and twilight_duration after sunrise. */
bool is_dawn( const time_point &p );
/** Returns the current seasonally-adjusted maximum daylight level */
double current_daylight_level( const time_point &p );
/** How much light is provided in full daylight */
Expand Down
4 changes: 2 additions & 2 deletions src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7441,9 +7441,9 @@ static extended_photo_def photo_def_for_camera_point( const tripoint &aim_point,

if( g->get_levz() >= 0 && need_store_weather ) {
photo_text += "\n\n";
if( is_sunrise_now( calendar::turn ) ) {
if( is_dawn( calendar::turn ) ) {
photo_text += _( "It is <color_yellow>sunrise</color>. " );
} else if( is_sunset_now( calendar::turn ) ) {
} else if( is_dusk( calendar::turn ) ) {
photo_text += _( "It is <color_light_red>sunset</color>. " );
} else if( is_night( calendar::turn ) ) {
photo_text += _( "It is <color_dark_gray>night</color>. " );
Expand Down
Loading

0 comments on commit 12f3da5

Please sign in to comment.