forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_gen.cpp
336 lines (296 loc) · 13.2 KB
/
weather_gen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "weather_gen.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <ostream>
#include <random>
#include <string>
#include <utility>
#include "avatar.h"
#include "cata_utility.h"
#include "condition.h"
#include "dialogue.h"
#include "game_constants.h"
#include "json.h"
#include "math_defines.h"
#include "point.h"
#include "rng.h"
#include "simplexnoise.h"
#include "translations.h"
#include "weather.h"
#include "weather_type.h"
namespace
{
constexpr double tau = 2 * M_PI;
constexpr double coldest_hour = 5;
// Out of 24 hours
constexpr double daily_magnitude_K = 5;
// 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
constexpr double noise_magnitude_K = 8;
// Greatest absolute day-to-day noise, in kelvins
} //namespace
weather_generator::weather_generator() = default;
int weather_generator::current_winddir = 1000;
struct weather_gen_common {
double x = 0;
double y = 0;
double z = 0;
double cyf = 0;
unsigned modSEED = 0u;
season_type season = season_type::SPRING;
};
static weather_gen_common get_common_data( const tripoint &location, const time_point &real_t,
unsigned seed )
{
season_effective_time t( real_t );
weather_gen_common result;
// Integer x position / widening factor of the Perlin function.
result.x = location.x / 2000.0;
// Integer y position / widening factor of the Perlin function.
result.y = location.y / 2000.0;
// Integer turn / widening factor of the Perlin function.
result.z = to_days<double>( real_t - calendar::turn_zero );
// Limit the random seed during noise calculation, a large value flattens the noise generator to zero
// Windows has a rand limit of 32768, other operating systems can have higher limits
result.modSEED = seed % SIMPLEX_NOISE_RANDOM_SEED_LIMIT;
const double year_fraction( time_past_new_year( t.t ) /
calendar::year_length() ); // [0,1)
result.cyf = std::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.t );
return result;
}
static double weather_temperature_from_common_data( const weather_generator &wg,
const weather_gen_common &common, const season_effective_time &t )
{
const double x( common.x );
const double y( common.y );
const double z( common.z );
const unsigned modSEED = common.modSEED;
const double seasonality = -common.cyf;
// -1 in midwinter, +1 in midsummer
const season_type season = common.season;
const double dayFraction = time_past_midnight( t.t ) / 1_days;
const double dayv = std::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 );
const double T = baseline + raw_noise_4d( x, y, z, modSEED ) * noise_magnitude_K;
// Convert from Celsius to Fahrenheit
return T * 9 / 5 + 32;
}
double weather_generator::get_weather_temperature(
const tripoint &location, const time_point &real_t, unsigned seed ) const
{
return weather_temperature_from_common_data( *this, get_common_data( location, real_t, seed ),
season_effective_time( real_t ) );
}
w_point weather_generator::get_weather( const tripoint &location, const time_point &real_t,
unsigned seed ) const
{
season_effective_time t( real_t );
const weather_gen_common common = get_common_data( location, real_t, seed );
const double x( common.x );
const double y( common.y );
const double z( common.z );
const unsigned modSEED = common.modSEED;
const double cyf( common.cyf );
const double seasonality = -common.cyf;
// -1 in midwinter, +1 in midsummer
const season_type season = common.season;
// Noise factors
const double T( weather_temperature_from_common_data( *this, common, t ) );
double W( raw_noise_4d( x / 2.5, y / 2.5, z / 200, modSEED ) * 10.0 );
// Humidity variation
double mod_h( 0 );
if( season == WINTER ) {
mod_h += winter_humidity_manual_mod;
} else if( season == SPRING ) {
mod_h += spring_humidity_manual_mod;
} else if( season == SUMMER ) {
mod_h += summer_humidity_manual_mod;
} else if( season == AUTUMN ) {
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 ) ) ) );
// Pressure
double P =
base_pressure +
raw_noise_4d( x, y, z, modSEED + 211 ) *
10 * ( -seasonality + 2 );
// Wind power
W = std::max( 0, static_cast<int>( base_wind * rng( 1, 2 ) / std::pow( ( P + W ) / 1014.78, rng( 9,
base_wind_distrib_peaks ) ) +
-cyf / base_wind_season_variation * rng( 1, 2 ) ) );
// Initial static variable
if( current_winddir == 1000 ) {
current_winddir = get_wind_direction( season );
current_winddir = convert_winddir( current_winddir );
} else {
// When wind strength is low, wind direction is more variable
bool changedir = one_in( W * 2160 );
if( changedir ) {
current_winddir = get_wind_direction( season );
current_winddir = convert_winddir( current_winddir );
}
}
std::string wind_desc = get_wind_desc( W );
return w_point{ T, H, P, W, wind_desc, current_winddir, t };
}
weather_type_id weather_generator::get_weather_conditions( const tripoint &location,
const time_point &t, unsigned seed ) const
{
w_point w( get_weather( location, t, seed ) );
weather_type_id wt = get_weather_conditions( w );
return wt;
}
weather_type_id weather_generator::get_weather_conditions( const w_point &w ) const
{
// We're being asked for the weather condition given a set of parameters (humidity, pressure, etc),
// but the dialogue condition system which drives that logic has no way for us to provide them
// directly; it can only reference the current game state. Until it's overhauled, we'll just hack
// the current game state while checking the conditions.
const weather_manager &game_weather = get_weather_const();
w_point original_weather_precise = *game_weather.weather_precise;
*game_weather.weather_precise = w;
weather_type_id current_conditions = WEATHER_CLEAR;
dialogue d( get_talker_for( get_avatar() ), nullptr );
for( const std::string &weather_type : weather_types ) {
weather_type_id type = weather_type_id( weather_type );
bool required_weather = type->required_weathers.empty();
if( !required_weather ) {
for( const weather_type_id &weather : type->required_weathers ) {
if( weather == current_conditions ) {
required_weather = true;
break;
}
}
}
if( required_weather && type->condition( d ) ) {
current_conditions = type;
continue;
}
}
// Cleanup our conditional hack.
*game_weather.weather_precise = original_weather_precise;
return current_conditions;
}
int weather_generator::get_wind_direction( const season_type season ) const
{
cata_default_random_engine &wind_dir_gen = rng_get_engine();
// Assign chance to angle direction
if( season == SPRING ) {
std::discrete_distribution<int> distribution {3, 3, 5, 8, 11, 10, 5, 2, 5, 6, 6, 5, 8, 10, 8, 6};
return distribution( wind_dir_gen );
} else if( season == SUMMER ) {
std::discrete_distribution<int> distribution {3, 4, 4, 8, 8, 9, 8, 3, 7, 8, 10, 7, 7, 7, 5, 3};
return distribution( wind_dir_gen );
} else if( season == AUTUMN ) {
std::discrete_distribution<int> distribution {4, 6, 6, 7, 6, 5, 4, 3, 5, 6, 8, 8, 10, 10, 8, 5};
return distribution( wind_dir_gen );
} else if( season == WINTER ) {
std::discrete_distribution<int> distribution {5, 3, 2, 3, 2, 2, 2, 2, 4, 6, 10, 8, 12, 19, 13, 9};
return distribution( wind_dir_gen );
} else {
return 0;
}
}
int weather_generator::convert_winddir( const int inputdir ) const
{
// Convert from discrete distribution output to angle
float finputdir = inputdir * 22.5f;
return static_cast<int>( finputdir );
}
int weather_generator::get_water_temperature() const
{
/**
WATER TEMPERATURE
source : http://echo2.epfl.ch/VICAIRE/mod_2/chapt_5/main.htm
source : http://www.grandriver.ca/index/document.cfm?Sec=2&Sub1=7&sub2=1
**/
season_effective_time t( calendar::turn );
int season_length = to_days<int>( calendar::season_length() );
int day = to_days<int>( time_past_new_year( t.t ) );
int hour = hour_of_day<int>( t.t );
int water_temperature = 0;
if( season_length == 0 ) {
season_length = 1;
}
// Temperature varies between 33.8F and 75.2F depending on the time of year. Day = 0 corresponds to the start of spring.
int annual_mean_water_temperature = 54.5 + 20.7 * std::sin( tau * ( day - season_length * 0.5 ) /
( season_length * 4.0 ) );
// Temperature varies between +2F and -2F depending on the time of day. Hour = 0 corresponds to midnight.
int daily_water_temperature_variation = 2.0 + 2.0 * std::sin( tau * ( hour - 6.0 ) / 24.0 );
water_temperature = annual_mean_water_temperature + daily_water_temperature_variation;
return water_temperature;
}
void weather_generator::test_weather( unsigned seed ) const
{
// Outputs a Cata year's worth of weather data to a CSV file.
// Usage:
// weather_generator WEATHERGEN; // Instantiate the class.
// WEATHERGEN.test_weather(); // Runs this test.
write_to_file( "weather.output", [&]( std::ostream & testfile ) {
testfile <<
"|;year;season;day;hour;minute;temperature(F);humidity(%);pressure(mB);weatherdesc;windspeed(mph);winddirection"
<< std::endl;
const time_point begin = calendar::turn;
const time_point end = begin + 2 * calendar::year_length();
for( time_point i = begin; i < end; i += 20_minutes ) {
w_point w = get_weather( tripoint_zero, i, seed );
weather_type_id conditions = get_weather_conditions( w );
int year = to_turns<int>( i - calendar::turn_zero ) / to_turns<int>
( calendar::year_length() ) + 1;
const int hour = hour_of_day<int>( i );
const int minute = minute_of_hour<int>( i );
int day;
if( calendar::eternal_season() ) {
day = to_days<int>( time_past_new_year( i ) );
} else {
day = day_of_season<int>( i );
}
testfile << "|;" << year << ";" << season_of_year( i ) << ";" << day << ";" << hour << ";" << minute
<< ";" << w.temperature << ";" << w.humidity << ";" << w.pressure << ";" << conditions->name << ";"
<<
w.windpower << ";" << w.winddirection << std::endl;
}
}, "weather test file" );
}
weather_generator weather_generator::load( const JsonObject &jo )
{
weather_generator ret;
ret.base_temperature = jo.get_float( "base_temperature", 0.0 );
ret.base_humidity = jo.get_float( "base_humidity", 50.0 );
ret.base_pressure = jo.get_float( "base_pressure", 0.0 );
ret.base_wind = jo.get_float( "base_wind", 0.0 );
ret.base_wind_distrib_peaks = jo.get_int( "base_wind_distrib_peaks", 0 );
ret.base_wind_season_variation = jo.get_int( "base_wind_season_variation", 0 );
ret.summer_temp_manual_mod = jo.get_int( "summer_temp_manual_mod", 0 );
ret.spring_temp_manual_mod = jo.get_int( "spring_temp_manual_mod", 0 );
ret.autumn_temp_manual_mod = jo.get_int( "autumn_temp_manual_mod", 0 );
ret.winter_temp_manual_mod = jo.get_int( "winter_temp_manual_mod", 0 );
ret.spring_humidity_manual_mod = jo.get_int( "spring_humidity_manual_mod", 0 );
ret.summer_humidity_manual_mod = jo.get_int( "summer_humidity_manual_mod", 0 );
ret.autumn_humidity_manual_mod = jo.get_int( "autumn_humidity_manual_mod", 0 );
ret.winter_humidity_manual_mod = jo.get_int( "winter_humidity_manual_mod", 0 );
ret.weather_types = jo.get_string_array( "weather_types" );
if( ret.weather_types.size() < 2 ) {
jo.throw_error( "Need at least 2 weather types per region for null and default." );
}
return ret;
}