Skip to content

Commit

Permalink
Merge pull request #36549 from jbytheway/non_synchronous_moon_phases
Browse files Browse the repository at this point in the history
Non synchronous moon phases
  • Loading branch information
kevingranade committed Jan 2, 2020
2 parents d2e8c25 + 9080d34 commit 0918b4e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ double default_daylight_level()

moon_phase get_moon_phase( const time_point &p )
{
//One full phase every 1 rl months = 1/3 season length
const time_duration moon_phase_duration = calendar::season_length() / 3.0;
//Switch moon phase at noon so it stays the same all night
const time_duration current_day = ( p - calendar::turn_zero ) + 1_days / 2;
const double phase_change = current_day / moon_phase_duration;
static constexpr time_duration synodic_month = 29.530588853 * 1_days;
const time_duration moon_phase_duration =
calendar::season_from_default_ratio() * synodic_month;
// Switch moon phase at noon so it stays the same all night
const int num_middays = to_days<int>( p - calendar::turn_zero + 1_days / 2 );
const time_duration nearest_midnight = num_middays * 1_days;
const double phase_change = nearest_midnight / moon_phase_duration;
const int current_phase = static_cast<int>( round( phase_change * MOON_PHASE_MAX ) ) %
static_cast<int>( MOON_PHASE_MAX );
return static_cast<moon_phase>( current_phase );
Expand Down Expand Up @@ -448,15 +450,16 @@ void calendar::set_season_length( const int dur )
cur_season_length = dur;
}

static constexpr int real_world_season_length = 91;
static constexpr int default_season_length = real_world_season_length;

float calendar::season_ratio()
{
static const int real_world_season_length = 91;
return to_days<float>( season_length() ) / real_world_season_length;
}

float calendar::season_from_default_ratio()
{
static const int default_season_length = 91;
return to_days<float>( season_length() ) / default_season_length;
}

Expand Down
38 changes: 38 additions & 0 deletions tests/calendar_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "catch/catch.hpp"

#include "calendar.h"
#include "rng.h"
#include "stringmaker.h"

TEST_CASE( "moon_phases_take_28_days", "[calendar]" )
{
CAPTURE( calendar::season_length() );
// This test only makes sense if the seasons are set to the default length
REQUIRE( calendar::season_from_default_ratio() == Approx( 1.0f ) );

const int num_days = GENERATE( take( 100, random( 0, 1000 ) ) );
const time_point first_time = calendar::turn_zero + time_duration::from_days( num_days );
const time_point later_14_days = first_time + 14_days;
const time_point later_29_days = first_time + 29_days;
const time_point later_30_days = first_time + 30_days;

CAPTURE( num_days );
CHECK( get_moon_phase( first_time ) != get_moon_phase( later_14_days ) );
// Phase should match either 29 or 30 days later
CHECK( ( get_moon_phase( first_time ) == get_moon_phase( later_29_days ) ||
get_moon_phase( first_time ) == get_moon_phase( later_30_days ) ) );
}

TEST_CASE( "moon_phase_changes_at_noon", "[calendar]" )
{
// This test only makes sense if the seasons are set to the default length
REQUIRE( calendar::season_from_default_ratio() == Approx( 1.0f ) );

const int num_days = GENERATE( take( 100, random( 0, 1000 ) ) );
const time_point midnight = calendar::turn_zero + time_duration::from_days( num_days );
const time_point earlier_11_hours = midnight - 11_hours;
const time_point later_11_hours = midnight + 11_hours;

CAPTURE( num_days );
CHECK( get_moon_phase( earlier_11_hours ) == get_moon_phase( later_11_hours ) );
}
1 change: 1 addition & 0 deletions tests/catch/catch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4238,6 +4238,7 @@ namespace Catch {
{
if( !IMutableContext::currentContext )
IMutableContext::createContext();
// NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn)
return *IMutableContext::currentContext;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/stringmaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ struct StringMaker<cata_variant> {
}
};

template<>
struct StringMaker<time_duration> {
static std::string convert( const time_duration &d ) {
return string_format( "time_duration( %d ) [%s]", to_turns<int>( d ), to_string( d ) );
}
};

} // namespace Catch

#endif // CATA_TESTS_STRINGMAKER_H
2 changes: 1 addition & 1 deletion tests/vision_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ TEST_CASE( "vision_wall_can_be_lit_by_player", "[shadowcasting][vision]" )

TEST_CASE( "vision_see_wall_in_moonlight", "[shadowcasting][vision]" )
{
const time_point full_moon = calendar::turn_zero + calendar::season_length() / 3 / 2;
const time_point full_moon = calendar::turn_zero + calendar::season_length() / 6;
// Verify that I've picked the full_moon time correctly.
CHECK( get_moon_phase( full_moon ) == MOON_FULL );

Expand Down

0 comments on commit 0918b4e

Please sign in to comment.