]> git.mxchange.org Git - flightgear.git/blob - src/Environment/fgclouds.cxx
022a1a4bc8da4cdae30fac5f704937819704d9b6
[flightgear.git] / src / Environment / fgclouds.cxx
1 // Build a cloud layer based on metar
2 //
3 // Written by Harald JOHNSEN, started April 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN - hjohnsen@evc.net
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20 //
21 //
22
23 #include <Main/fg_props.hxx>
24
25 #include <simgear/constants.h>
26 #include <simgear/sound/soundmgr_openal.hxx>
27 #include <simgear/scene/sky/sky.hxx>
28 #include <simgear/environment/visual_enviro.hxx>
29 #include <simgear/scene/sky/cloudfield.hxx>
30 #include <simgear/scene/sky/newcloud.hxx>
31 #include <simgear/math/sg_random.h>
32 #include <Main/globals.hxx>
33 #include <Airports/simple.hxx>
34 #include <Main/util.hxx>
35
36 #include "environment_mgr.hxx"
37 #include "fgmetar.hxx"
38 #include "fgclouds.hxx"
39
40 extern SGSky *thesky;
41
42
43 FGClouds::FGClouds() :
44         station_elevation_ft(0.0),
45         snd_lightning(NULL)
46 {
47         update_event = 0;
48         fgSetString("/environment/weather-scenario", "METAR");
49 }
50 FGClouds::~FGClouds() {
51 }
52
53 int FGClouds::get_update_event(void) const {
54         return update_event;
55 }
56 void FGClouds::set_update_event(int count) {
57         update_event = count;
58         build();
59 }
60
61 void FGClouds::init(void) {
62         if( snd_lightning == NULL ) {
63                 snd_lightning = new SGSoundSample(globals->get_fg_root().c_str(), "Sounds/thunder.wav", true);
64                 snd_lightning->set_max_dist(7000.0f);
65                 snd_lightning->set_reference_dist(3000.0f);
66                 SGSoundMgr *soundMgr = globals->get_soundmgr();
67                 soundMgr->add( snd_lightning, "thunder" );
68                 sgEnviro.set_soundMgr( soundMgr );
69         }
70 }
71
72 SGNewCloud *FGClouds::buildCloud(SGPropertyNode *cloud_def_root, string name) {
73         SGPropertyNode *cld_def=NULL;
74
75         cld_def = cloud_def_root->getChild(name.c_str());
76         string base_name = name.substr(0,2);
77         if( !cld_def ) {
78                 if( name[2] == '-' ) {
79                         cld_def = cloud_def_root->getChild(base_name.c_str());
80                 }
81                 if( !cld_def )
82                         return NULL;
83         }
84         string familly = cld_def->getStringValue("familly", base_name.c_str());
85         SGNewCloud *cld = new SGNewCloud(familly);
86         for(int i = 0; i < cld_def->nChildren() ; i++) {
87                 SGPropertyNode *abox = cld_def->getChild(i);
88                 if( strcmp(abox->getName(), "box") == 0) {
89                         double x = abox->getDoubleValue("x");
90                         double y = abox->getDoubleValue("y");
91                         double z = abox->getDoubleValue("z");
92                         double size = abox->getDoubleValue("size");
93                         int type = abox->getIntValue("type", SGNewCloud::CLbox_standard);
94                         cld->addContainer(x, y, z, size, (SGNewCloud::CLbox_type) type);
95                 }
96         }
97         cld->genSprites();
98         return cld;
99 }
100
101 void FGClouds::buildLayer(SGCloudField *layer, string name, double alt, double coverage) {
102         struct {
103                 string name;
104                 double count;
105         } tCloudVariety[20];
106         int CloudVarietyCount = 0;
107         double totalCount = 0.0;
108
109         SGPropertyNode *cloud_def_root = fgGetNode("/environment/config/cloudlayers/clouds", false);
110         SGPropertyNode *layer_def_root = fgGetNode("/environment/config/cloudlayers/layers", false);
111
112         layer->clear();
113         // when we don't generate clouds the layer is rendered in 2D
114         if( coverage == 0.0 )
115                 return;
116         if( layer_def_root == NULL || cloud_def_root == NULL)
117                 return;
118         if( name == "ci" || name == "sc" || name == "st")
119                 return;
120
121         SGPropertyNode *layer_def=NULL;
122
123         layer_def = layer_def_root->getChild(name.c_str());
124         if( !layer_def ) {
125                 if( name[2] == '-' ) {
126                         string base_name = name.substr(0,2);
127                         layer_def = layer_def_root->getChild(base_name.c_str());
128                 }
129                 if( !layer_def )
130                         return;
131         }
132
133         double grid_x_size = layer_def->getDoubleValue("grid-x-size", 1000.0);
134         double grid_y_size = layer_def->getDoubleValue("grid-y-size", 1000.0);
135         double grid_x_rand = layer_def->getDoubleValue("grid-x-rand", grid_x_size);
136         double grid_y_rand = layer_def->getDoubleValue("grid-y-rand", grid_y_size);
137         double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
138
139         for(int i = 0; i < layer_def->nChildren() ; i++) {
140                 SGPropertyNode *acloud = layer_def->getChild(i);
141                 if( strcmp(acloud->getName(), "cloud") == 0) {
142                         string cloud_name = acloud->getStringValue("name");
143                         tCloudVariety[CloudVarietyCount].name = cloud_name;
144                         double count = acloud->getDoubleValue("count", 1.0);
145                         tCloudVariety[CloudVarietyCount].count = count;
146                         int variety = 0;
147                         cloud_name = cloud_name + "-%d";
148                         char variety_name[50];
149                         do {
150                                 variety++;
151                                 snprintf(variety_name, sizeof(variety_name), cloud_name.c_str(), variety);
152                         } while( cloud_def_root->getChild(variety_name, 0, false) );
153
154                         totalCount += count;
155                         if( CloudVarietyCount < 20 )
156                                 CloudVarietyCount++;
157                 }
158         }
159         totalCount = 1.0 / totalCount;
160         double currCoverage = 0.0;
161
162         for(double px = 0.0; px < SGCloudField::fieldSize; px += grid_x_size) {
163                 for(double py = 0.0; py < SGCloudField::fieldSize; py += grid_y_size) {
164                         double x = px + grid_x_rand * (sg_random() - 0.5);
165                         double y = py + grid_y_rand * (sg_random() - 0.5);
166                         double z = alt + grid_z_rand * (sg_random() - 0.5);
167                         double choice = sg_random();
168                         currCoverage += coverage;
169                         if( currCoverage < 1.0 )
170                                 continue;
171                         currCoverage -= 1.0;
172
173                         for(int i = 0; i < CloudVarietyCount ; i ++) {
174                                 choice -= tCloudVariety[i].count * totalCount;
175                                 if( choice <= 0.0 ) {
176                                         SGNewCloud *cld = buildCloud(cloud_def_root, tCloudVariety[i].name);
177                                         sgVec3 pos={x,z,y};
178                                         if( cld )
179                                                 layer->addCloud(pos, cld);
180
181                                         break;
182                                 }
183                         }
184                 }
185         }
186
187 }
188
189 // TODO:call this after real metar updates
190 void FGClouds::buildMETAR(void) {
191         SGPropertyNode *metar_root = fgGetNode("/environment", true);
192
193         double wind_speed_kt     = metar_root->getDoubleValue("wind-speed-kt");
194         double temperature_degc  = metar_root->getDoubleValue("temperature-sea-level-degc");
195         double dewpoint_degc     = metar_root->getDoubleValue("dewpoint-sea-level-degc");
196         double pressure_mb              = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
197
198         double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
199         double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
200         double rel_humidity = dewp * 100 / temp;
201
202         // formule d'Epsy, base d'un cumulus
203         double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
204         double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
205
206         bool cu_seen = false;
207
208         for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
209                 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
210
211                 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
212                 double alt_m = alt_ft * SG_FEET_TO_METER;
213                 string coverage = cloud_root->getStringValue("coverage");
214                 double coverage_norm = 0.0;
215                 if( coverage == "few" )
216                         coverage_norm = 2.0/8.0;        // <1-2
217                 else if( coverage == "scattered" )
218                         coverage_norm = 4.0/8.0;        // 3-4
219                 else if( coverage == "broken" )
220                         coverage_norm = 6.0/8.0;        // 5-7
221                 else if( coverage == "overcast" )
222                         coverage_norm = 8.0/8.0;        // 8
223
224                 string layer_type = "nn";
225                 if( coverage == "cirrus" ) {
226                         layer_type = "ci";
227                 } else if( alt_ft > 16500 ) {
228 //                      layer_type = "ci|cs|cc";
229                         layer_type = "ci";
230                 } else if( alt_ft > 6500 ) {
231 //                      layer_type = "as|ac|ns";
232                         layer_type = "ac";
233                         if( pressure_mb < 1005.0 && coverage_norm >= 5.5 )
234                                 layer_type = "ns";
235                 } else {
236 //                      layer_type = "st|cu|cb|sc";
237                         // +/- 20% from stratus probable base
238                         if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m )
239                                 layer_type = "st";
240                         // +/- 20% from cumulus probable base
241                         else if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m )
242                                 layer_type = "cu";
243                         else {
244                                 // above formulae is far from perfect
245                                 if ( alt_ft < 2000 )
246                                         layer_type = "st";
247                                 else if( alt_ft < 4500 )
248                                         layer_type = "cu";
249                                 else
250                                         layer_type = "sc";
251                         }
252                 }
253
254                 SGCloudField *layer3D = thesky->get_cloud_layer(iLayer)->get_layer3D();
255                 buildLayer(layer3D, layer_type, alt_m, coverage_norm);
256         }
257 }
258
259 // copy from FGMetarEnvironmentCtrl until better
260 void
261 FGClouds::update_metar_properties( FGMetar *m )
262 {
263     int i;
264     double d;
265     char s[128];
266
267     fgSetString("/environment/metar/station-id", m->getId());
268     fgSetDouble("/environment/metar/min-visibility-m",
269                 m->getMinVisibility().getVisibility_m() );
270     fgSetDouble("/environment/metar/max-visibility-m",
271                 m->getMaxVisibility().getVisibility_m() );
272
273     SGMetarVisibility *dirvis = m->getDirVisibility();
274     for (i = 0; i < 8; i++, dirvis++) {
275         const char *min = "/environment/metar/visibility[%d]/min-m";
276         const char *max = "/environment/metar/visibility[%d]/max-m";
277
278         d = dirvis->getVisibility_m();
279
280         snprintf(s, 128, min, i);
281         fgSetDouble(s, d);
282         snprintf(s, 128, max, i);
283         fgSetDouble(s, d);
284     }
285
286     fgSetInt("/environment/metar/base-wind-range-from",
287              m->getWindRangeFrom() );
288     fgSetInt("/environment/metar/base-wind-range-to",
289              m->getWindRangeTo() );
290     fgSetDouble("/environment/metar/base-wind-speed-kt",
291                 m->getWindSpeed_kt() );
292     fgSetDouble("/environment/metar/gust-wind-speed-kt",
293                 m->getGustSpeed_kt() );
294     fgSetDouble("/environment/metar/temperature-degc",
295                 m->getTemperature_C() );
296     fgSetDouble("/environment/metar/dewpoint-degc",
297                 m->getDewpoint_C() );
298     fgSetDouble("/environment/metar/rel-humidity-norm",
299                 m->getRelHumidity() );
300     fgSetDouble("/environment/metar/pressure-inhg",
301                 m->getPressure_inHg() );
302
303     vector<SGMetarCloud> cv = m->getClouds();
304     vector<SGMetarCloud>::iterator cloud;
305
306     const char *cl = "/environment/clouds/layer[%i]";
307     for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
308         const char *coverage_string[5] = 
309             { "clear", "few", "scattered", "broken", "overcast" };
310         const double thickness[5] = { 0, 65, 600,750, 1000};
311         int q;
312
313         snprintf(s, 128, cl, i);
314         strncat(s, "/coverage", 128);
315         q = cloud->getCoverage();
316         fgSetString(s, coverage_string[q] );
317
318         snprintf(s, 128, cl, i);
319         strncat(s, "/elevation-ft", 128);
320         fgSetDouble(s, cloud->getAltitude_ft() + station_elevation_ft);
321
322         snprintf(s, 128, cl, i);
323         strncat(s, "/thickness-ft", 128);
324         fgSetDouble(s, thickness[q]);
325
326         snprintf(s, 128, cl, i);
327         strncat(s, "/span-m", 128);
328         fgSetDouble(s, 40000.0);
329     }
330
331     for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
332         snprintf(s, 128, cl, i);
333         strncat(s, "/coverage", 128);
334         fgSetString(s, "clear");
335
336         snprintf(s, 128, cl, i);
337         strncat(s, "/elevation-ft", 128);
338         fgSetDouble(s, -9999);
339
340         snprintf(s, 128, cl, i);
341         strncat(s, "/thickness-ft", 128);
342         fgSetDouble(s, 0);
343
344         snprintf(s, 128, cl, i);
345         strncat(s, "/span-m", 128);
346         fgSetDouble(s, 40000.0);
347     }
348
349     fgSetDouble("/environment/metar/rain-norm", m->getRain());
350     fgSetDouble("/environment/metar/hail-norm", m->getHail());
351     fgSetDouble("/environment/metar/snow-norm", m->getSnow());
352     fgSetBool("/environment/metar/snow-cover", m->getSnowCover());
353 }
354
355 void
356 FGClouds::update_env_config ()
357 {
358     fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
359                  fgGetDouble("/environment/metar/base-wind-range-to"),
360                  fgGetDouble("/environment/metar/base-wind-speed-kt"),
361                  fgGetDouble("/environment/metar/gust-wind-speed-kt") );
362
363     fgDefaultWeatherValue( "visibility-m",
364                            fgGetDouble("/environment/metar/min-visibility-m") );
365 #if 0
366     set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
367                           station_elevation_ft );
368     set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
369                               station_elevation_ft );
370 #endif
371     fgDefaultWeatherValue( "pressure-sea-level-inhg",
372                            fgGetDouble("/environment/metar/pressure-inhg") );
373 }
374
375
376 void FGClouds::setLayer( int iLayer, float alt_ft, string coverage, string layer_type ) {
377         double coverage_norm = 0.0;
378         if( coverage == "few" )
379                 coverage_norm = 2.0/8.0;        // <1-2
380         else if( coverage == "scattered" )
381                 coverage_norm = 4.0/8.0;        // 3-4
382         else if( coverage == "broken" )
383                 coverage_norm = 6.0/8.0;        // 5-7
384         else if( coverage == "overcast" )
385                 coverage_norm = 8.0/8.0;        // 8
386
387         SGCloudField *layer3D = thesky->get_cloud_layer(iLayer)->get_layer3D();
388         buildLayer(layer3D, layer_type, station_elevation_ft + alt_ft * SG_FEET_TO_METER, coverage_norm);
389 }
390
391 void FGClouds::buildScenario( string scenario ) {
392         string fakeMetar="";
393         string station = fgGetString("/environment/metar/station-id", "XXXX");
394
395         // fetch station elevation if exists
396     FGAirport a = globals->get_airports()->search( station );
397     station_elevation_ft = a.getElevation();
398
399         for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
400                 thesky->get_cloud_layer(iLayer)->get_layer3D()->clear();
401         }
402
403         station += " 011000Z ";
404         if( scenario == "Fair weather" ) {
405                 fakeMetar = "15003KT 12SM SCT033 FEW200 20/08 Q1015 NOSIG";
406                 setLayer(0, 3300.0, "scattered", "cu");
407         } else if( scenario == "Thunderstorm" ) {
408                 fakeMetar = "15012KT 08SM TSRA SCT040 BKN070 20/12 Q0995";
409                 setLayer(0, 4000.0, "scattered", "cb");
410                 setLayer(1, 7000.0, "scattered", "ns");
411         } else 
412                 return;
413         FGMetar *m = new FGMetar( station + fakeMetar );
414         update_metar_properties( m );
415         update_env_config();
416         fgSetString("/environment/metar/last-metar", m->getData());
417         // TODO:desactivate real metar updates
418         if( scenario == "Fair weather" ) {
419                 fgSetString("/environment/clouds/layer[1]/coverage", "cirrus");
420         }
421 }
422
423
424 void FGClouds::build(void) {
425         string scenario = fgGetString("/environment/weather-scenario", "METAR");
426
427         if( scenario == "METAR" ) {
428                 string realMetar = fgGetString("/environment/metar/last-metar", "");
429                 if( realMetar != "" ) {
430                         FGMetar *m = new FGMetar( realMetar );
431                         update_metar_properties( m );
432                         update_env_config();
433                 }
434                 buildMETAR();
435         }
436         else
437                 buildScenario( scenario );
438
439         // ...
440         if( snd_lightning == NULL )
441                 init();
442 }