Skip to content

Commit

Permalink
Astyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodiologist committed Oct 24, 2019
1 parent 0a6139c commit 7c66f0d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 63 deletions.
13 changes: 7 additions & 6 deletions src/weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,18 @@ double precip_mm_per_hour( precip_class const p )
// the precipitation were rain (rather than snow).
{
return
p == PRECIP_LIGHT ? 1.5 :
p == PRECIP_HEAVY ? 3 :
0;
p == PRECIP_LIGHT ? 1.5 :
p == PRECIP_HEAVY ? 3 :
0;
}

void do_rain( weather_type const w )
{
if (! weather::rains(w) || weather::precip(w) == PRECIP_NONE )
if( ! weather::rains( w ) || weather::precip( w ) == PRECIP_NONE ) {
return;
fill_water_collectors( precip_mm_per_hour(weather::precip(w)), weather::acidic(w) );
bool light = weather::precip(w) == PRECIP_LIGHT;
}
fill_water_collectors( precip_mm_per_hour( weather::precip( w ) ), weather::acidic( w ) );
bool light = weather::precip( w ) == PRECIP_LIGHT;
g->m.decay_fields_and_scent( light ? 15_turns : 45_turns );
wet_player( light ? 30 : 60 );
}
Expand Down
40 changes: 20 additions & 20 deletions src/weather_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ namespace
{
constexpr double tau = 2 * M_PI;
constexpr double coldest_hour = 5;
// Out of 24 hours
// Out of 24 hours
constexpr double daily_magnitude_K = 5;
// Greatest absolute change from a day's average temperature, in kelvins
// Greatest absolute change from a day's average temperature, in kelvins
constexpr double seasonality_magnitude_K = 15;
// Greatest absolute change from the year's average temperature, in kelvins
// Greatest absolute change from the year's average temperature, in kelvins
constexpr double noise_magnitude_K = 8;
// Greatest absolute day-to-day noise, in kelvins
// Greatest absolute day-to-day noise, in kelvins
} //namespace

weather_generator::weather_generator() = default;
Expand Down Expand Up @@ -55,11 +55,11 @@ static weather_gen_common get_common_data( const tripoint &location, const time_
const double year_fraction( time_past_new_year( t ) /
calendar::year_length() ); // [0,1)

result.cyf = cos( tau * (year_fraction + .125) ); // [-1, 1]
// We add one-eighth to line up `cyf` so that 1 is at
// midwinter and -1 at midsummer. (Cataclsym DDA years
// start when spring starts. Gregorian years start when
// winter starts.)
result.cyf = cos( tau * ( year_fraction + .125 ) ); // [-1, 1]
// We add one-eighth to line up `cyf` so that 1 is at
// midwinter and -1 at midsummer. (Cataclsym DDA years
// start when spring starts. Gregorian years start when
// winter starts.)
result.season = season_of_year( t );

return result;
Expand All @@ -74,19 +74,19 @@ static double weather_temperature_from_common_data( const weather_generator &wg,

const unsigned modSEED = common.modSEED;
const double seasonality = -common.cyf;
// -1 in midwinter, +1 in midsummer
// -1 in midwinter, +1 in midsummer
const season_type season = common.season;
const double dayFraction = time_past_midnight( t ) / 1_days;
const double dayv = cos( tau * (dayFraction + .5 - coldest_hour / 24) );
// -1 at coldest_hour, +1 twelve hours later
const double dayv = cos( tau * ( dayFraction + .5 - coldest_hour / 24 ) );
// -1 at coldest_hour, +1 twelve hours later

// manually specified seasonal temp variation from region_settings.json
const int seasonal_temp_mod[4] = { wg.spring_temp_manual_mod, wg.summer_temp_manual_mod, wg.autumn_temp_manual_mod, wg.winter_temp_manual_mod };
const double baseline(
wg.base_temperature +
seasonal_temp_mod[ season ] +
dayv * daily_magnitude_K +
seasonality * seasonality_magnitude_K);
seasonality * seasonality_magnitude_K );

const double T = baseline + raw_noise_4d( x, y, z, modSEED ) * noise_magnitude_K;

Expand All @@ -111,7 +111,7 @@ w_point weather_generator::get_weather( const tripoint &location, const time_poi
const unsigned modSEED = common.modSEED;
const double cyf( common.cyf );
const double seasonality = -common.cyf;
// -1 in midwinter, +1 in midsummer
// -1 in midwinter, +1 in midsummer
const season_type season = common.season;

// Noise factors
Expand All @@ -131,17 +131,17 @@ w_point weather_generator::get_weather( const tripoint &location, const time_poi
mod_h += autumn_humidity_manual_mod;
}
// Relative humidity, a percentage.
double H = std::min(100., std::max(0.,
base_humidity + mod_h + 100 * (
.15 * seasonality +
raw_noise_4d( x, y, z, modSEED + 101 ) *
.2 * (-seasonality + 2))));
double H = std::min( 100., std::max( 0.,
base_humidity + mod_h + 100 * (
.15 * seasonality +
raw_noise_4d( x, y, z, modSEED + 101 ) *
.2 * ( -seasonality + 2 ) ) ) );

// Pressure
double P =
base_pressure +
raw_noise_4d( x, y, z, modSEED + 211 ) *
10 * (-seasonality + 2);
10 * ( -seasonality + 2 );

// Wind power
W = std::max( 0, static_cast<int>( base_wind / pow( P / 1014.78, rng( 9,
Expand Down
81 changes: 44 additions & 37 deletions tests/weather_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,33 @@
#include "weather_gen.h"
#include "game.h"

static double mean_abs_running_diff(std::vector<double> const &v) {
static double mean_abs_running_diff( std::vector<double> const &v )
{
double x = 0;
int n = v.size() - 1;
for (int i = 0 ; i < n ; ++i)
x += abs(v[i + 1] - v[i]);
for( int i = 0 ; i < n ; ++i ) {
x += abs( v[i + 1] - v[i] );
}
return x / n;
}

static double mean_pairwise_diffs(std::vector<double> const &a, std::vector<double> const &b) {
static double mean_pairwise_diffs( std::vector<double> const &a, std::vector<double> const &b )
{
double x = 0;
int n = a.size();
for (int i = 0 ; i < n ; ++i)
for( int i = 0 ; i < n ; ++i ) {
x += a[i] - b[i];
}
return x / n;
}

static double proportion_gteq_x(std::vector<double> const &v, double x) {
static double proportion_gteq_x( std::vector<double> const &v, double x )
{
int count = 0;
for (auto i : v)
count += (i >= x);
return static_cast<double>(count) / v.size();
for( auto i : v ) {
count += ( i >= x );
}
return static_cast<double>( count ) / v.size();
}

TEST_CASE( "weather realism" )
Expand All @@ -43,63 +49,64 @@ TEST_CASE( "weather realism" )
const weather_generator &wgen = g->weather.get_cur_weather_gen();
const time_point begin = 0;
const time_point end = begin + calendar::year_length();
const int n_days = to_days<int>(end - begin);
const int n_hours = to_hours<int>(1_days);
const int n_minutes = to_minutes<int>(1_days);
const int n_days = to_days<int>( end - begin );
const int n_hours = to_hours<int>( 1_days );
const int n_minutes = to_minutes<int>( 1_days );

for (auto seed = seeds.begin() ; seed != seeds.end() ; ++seed) {
for( auto seed = seeds.begin() ; seed != seeds.end() ; ++seed ) {
std::vector<std::vector<double>> temperature;
temperature.resize(n_days, std::vector<double>(n_minutes, 0));
temperature.resize( n_days, std::vector<double>( n_minutes, 0 ) );
std::vector<double> hourly_precip;
hourly_precip.resize(n_days * n_hours, 0);
hourly_precip.resize( n_days * n_hours, 0 );

// Collect generated weather data for a single year.
for( time_point i = begin ; i < end ; i += 1_minutes ) {
w_point w = wgen.get_weather(tripoint_zero, to_turn<int>(i), *seed);
int day = to_days<int>(time_past_new_year(i));
int minute = to_minutes<int>(time_past_midnight(i));
w_point w = wgen.get_weather( tripoint_zero, to_turn<int>( i ), *seed );
int day = to_days<int>( time_past_new_year( i ) );
int minute = to_minutes<int>( time_past_midnight( i ) );
temperature[day][minute] = w.temperature;
int hour = to_hours<int>(time_past_new_year(i));
int hour = to_hours<int>( time_past_new_year( i ) );
hourly_precip[hour] +=
precip_mm_per_hour(
weather::precip(wgen.get_weather_conditions(w)))
/ 60;
precip_mm_per_hour(
weather::precip( wgen.get_weather_conditions( w ) ) )
/ 60;
}

// Collect daily highs and lows.
std::vector<double> highs(n_days);
std::vector<double> lows(n_days);
for (int do_highs = 0 ; do_highs < 2 ; ++do_highs) {
std::vector<double> highs( n_days );
std::vector<double> lows( n_days );
for( int do_highs = 0 ; do_highs < 2 ; ++do_highs ) {
std::vector<double> &t = do_highs ? highs : lows;
std::transform(temperature.begin(), temperature.end(), t.begin(),
[&] (std::vector<double> const &day)
{return do_highs
? *std::max_element(day.begin(), day.end())
: *std::min_element(day.begin(), day.end());});
std::transform( temperature.begin(), temperature.end(), t.begin(),
[&]( std::vector<double> const & day ) {
return do_highs
? *std::max_element( day.begin(), day.end() )
: *std::min_element( day.begin(), day.end() );
} );

// Check the mean absolute difference between the highs or lows
// of adjacent days (Fahrenheit).
const double d = mean_abs_running_diff(t);
CHECK(d >= (do_highs ? 5.5 : 4));
CHECK(d <= (do_highs ? 7.5 : 7));
const double d = mean_abs_running_diff( t );
CHECK( d >= ( do_highs ? 5.5 : 4 ) );
CHECK( d <= ( do_highs ? 7.5 : 7 ) );
}

// Check the daily mean of the range in temperatures (Fahrenheit).
const double mean_of_ranges = mean_pairwise_diffs(highs, lows);
const double mean_of_ranges = mean_pairwise_diffs( highs, lows );
CHECK( mean_of_ranges >= 14 );
CHECK( mean_of_ranges <= 25 );

// Check the proportion of hours with light precipitation
// or more, counting snow (mm of rain equivalent per hour).
const double at_least_light_precip = proportion_gteq_x(
hourly_precip, 1);
hourly_precip, 1 );
CHECK( at_least_light_precip >= .025 );
CHECK( at_least_light_precip <= .05 );

// Likewise for heavy precipitation.
const double heavy_precip = proportion_gteq_x(
hourly_precip, 2.5);
hourly_precip, 2.5 );
CHECK( heavy_precip >= .005 );
CHECK( heavy_precip <= .02 );
}
}
}

0 comments on commit 7c66f0d

Please sign in to comment.