]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
4a3fc96061ea8fb9be0e1a56c9df259ca5334c75
[flightgear.git] / src / Environment / environment_mgr.cxx
1 // environment-mgr.cxx -- manager for natural environment information.
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <cstring>
28
29 #include <simgear/constants.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/scene/sky/sky.hxx>
32 #include <simgear/environment/visual_enviro.hxx>
33 #include <simgear/scene/model/particles.hxx>
34
35 #include <Main/main.hxx>
36 #include <Main/fg_props.hxx>
37 #include <FDM/flight.hxx>
38
39 #include "environment.hxx"
40 #include "environment_mgr.hxx"
41 #include "environment_ctrl.hxx"
42 #include "fgclouds.hxx"
43 #include "precipitation_mgr.hxx"
44
45 class SGSky;
46 extern SGSky *thesky;
47
48
49
50 FGEnvironmentMgr::FGEnvironmentMgr ()
51   : _environment(new FGEnvironment)
52 {
53
54   _controller = new FGInterpolateEnvironmentCtrl;
55   _controller->setEnvironment(_environment);
56   set_subsystem("controller", _controller, 0.1 );
57
58   fgClouds = new FGClouds();
59
60   _metarcontroller = new FGMetarCtrl(_controller );
61   set_subsystem("metarcontroller", _metarcontroller, 0.1 );
62
63   _metarfetcher = new FGMetarFetcher();
64   set_subsystem("metarfetcher", _metarfetcher, 1.0 );
65
66   _precipitationManager = new FGPrecipitationMgr;
67   set_subsystem("precipitation", _precipitationManager);
68 }
69
70 FGEnvironmentMgr::~FGEnvironmentMgr ()
71 {
72   remove_subsystem("precipitation");
73   delete _precipitationManager;
74
75   remove_subsystem("metarcontroller");
76   delete _metarfetcher;
77
78   remove_subsystem("metarfetcher");
79   delete _metarcontroller;
80
81   delete fgClouds;
82
83   remove_subsystem("controller");
84   delete _controller;
85
86   delete _environment;
87 }
88
89 void
90 FGEnvironmentMgr::init ()
91 {
92   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
93   SGSubsystemGroup::init();
94   //_update_fdm();
95 }
96
97 void
98 FGEnvironmentMgr::reinit ()
99 {
100   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
101   SGSubsystemGroup::reinit();
102   //_update_fdm();
103 }
104
105 void
106 FGEnvironmentMgr::bind ()
107 {
108   SGSubsystemGroup::bind();
109   fgTie("/environment/visibility-m", _environment,
110         &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
111   fgSetArchivable("/environment/visibility-m");
112   fgTie("/environment/effective-visibility-m", thesky,
113        &SGSky::get_visibility );
114   fgTie("/environment/temperature-sea-level-degc", _environment,
115         &FGEnvironment::get_temperature_sea_level_degc,
116         &FGEnvironment::set_temperature_sea_level_degc);
117   fgSetArchivable("/environment/temperature-sea-level-degc");
118   fgTie("/environment/temperature-degc", _environment,
119         &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
120   fgTie("/environment/temperature-degf", _environment,
121         &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
122   fgTie("/environment/dewpoint-sea-level-degc", _environment,
123         &FGEnvironment::get_dewpoint_sea_level_degc,
124         &FGEnvironment::set_dewpoint_sea_level_degc);
125   fgSetArchivable("/environment/dewpoint-sea-level-degc");
126   fgTie("/environment/dewpoint-degc", _environment,
127         &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
128   fgTie("/environment/pressure-sea-level-inhg", _environment,
129         &FGEnvironment::get_pressure_sea_level_inhg,
130         &FGEnvironment::set_pressure_sea_level_inhg);
131   fgSetArchivable("/environment/pressure-sea-level-inhg");
132   fgTie("/environment/pressure-inhg", _environment,
133         &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
134   fgTie("/environment/density-slugft3", _environment,
135         &FGEnvironment::get_density_slugft3); // read-only
136   fgTie("/environment/relative-humidity", _environment,
137         &FGEnvironment::get_relative_humidity); //ro
138   fgTie("/environment/atmosphere/density-tropo-avg", _environment,
139         &FGEnvironment::get_density_tropo_avg_kgm3); //ro
140   fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
141         &FGEnvironment::get_altitude_half_to_sun_m, 
142         &FGEnvironment::set_altitude_half_to_sun_m);
143   fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
144         &FGEnvironment::get_altitude_tropo_top_m,
145         &FGEnvironment::set_altitude_tropo_top_m);
146   fgTie("/environment/wind-from-heading-deg", _environment,
147         &FGEnvironment::get_wind_from_heading_deg,
148         &FGEnvironment::set_wind_from_heading_deg);
149   fgTie("/environment/wind-speed-kt", _environment,
150         &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
151   fgTie("/environment/wind-from-north-fps", _environment,
152         &FGEnvironment::get_wind_from_north_fps,
153         &FGEnvironment::set_wind_from_north_fps);
154   fgSetArchivable("/environment/wind-from-north-fps");
155   fgTie("/environment/wind-from-east-fps", _environment,
156         &FGEnvironment::get_wind_from_east_fps,
157         &FGEnvironment::set_wind_from_east_fps);
158   fgSetArchivable("/environment/wind-from-east-fps");
159   fgTie("/environment/wind-from-down-fps", _environment,
160         &FGEnvironment::get_wind_from_down_fps,
161         &FGEnvironment::set_wind_from_down_fps);
162   fgSetArchivable("/environment/wind-from-down-fps");
163
164   fgTie("/environment/thermal-lift-fps", _environment,
165         &FGEnvironment::get_thermal_lift_fps,
166         &FGEnvironment::set_thermal_lift_fps);
167   fgSetArchivable("/environment/thermal-lift-fps");
168   fgTie("/environment/ridge-lift-fps", _environment,
169         &FGEnvironment::get_ridge_lift_fps,
170         &FGEnvironment::set_ridge_lift_fps);    
171   fgSetArchivable("/environment/ridge-lift-fps");
172   
173     fgTie("/environment/local-weather-lift", _environment,
174         &FGEnvironment::get_local_weather_lift_fps); //read-only
175      
176   fgTie("/environment/turbulence/magnitude-norm", _environment,
177         &FGEnvironment::get_turbulence_magnitude_norm,
178         &FGEnvironment::set_turbulence_magnitude_norm);
179   fgSetArchivable("/environment/turbulence/magnitude-norm");
180   fgTie("/environment/turbulence/rate-hz", _environment,
181         &FGEnvironment::get_turbulence_rate_hz,
182         &FGEnvironment::set_turbulence_rate_hz);
183   fgSetArchivable("/environment/turbulence/rate-hz");
184
185   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
186     char buf[128];
187     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
188     fgTie(buf, this, i,
189           &FGEnvironmentMgr::get_cloud_layer_span_m,
190           &FGEnvironmentMgr::set_cloud_layer_span_m);
191     fgSetArchivable(buf);
192     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
193     fgTie(buf, this, i,
194           &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
195           &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
196     fgSetArchivable(buf);
197     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
198     fgTie(buf, this, i,
199           &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
200           &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
201     fgSetArchivable(buf);
202     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
203     fgTie(buf, this, i,
204           &FGEnvironmentMgr::get_cloud_layer_transition_ft,
205           &FGEnvironmentMgr::set_cloud_layer_transition_ft);
206     fgSetArchivable(buf);
207     sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
208     fgTie(buf, this, i,
209           &FGEnvironmentMgr::get_cloud_layer_coverage,
210           &FGEnvironmentMgr::set_cloud_layer_coverage);
211     fgSetArchivable(buf);
212   }
213
214   fgTie("/environment/metar/data", _metarcontroller,
215           &FGMetarCtrl::get_metar, &FGMetarCtrl::set_metar );
216
217   fgTie("/sim/rendering/clouds3d-enable", fgClouds,
218           &FGClouds::get_3dClouds,
219           &FGClouds::set_3dClouds);
220   fgTie("/sim/rendering/clouds3d-density", thesky,
221           &SGSky::get_3dCloudDensity,
222           &SGSky::set_3dCloudDensity);
223   fgTie("/sim/rendering/clouds3d-vis-range", thesky,
224         &SGSky::get_3dCloudVisRange,
225         &SGSky::set_3dCloudVisRange);
226   
227   fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
228           &SGEnviro::get_precipitation_enable_state, 
229           &SGEnviro::set_precipitation_enable_state);
230   fgTie("/environment/rebuild-layers", fgClouds,
231       &FGClouds::get_update_event,
232       &FGClouds::set_update_event);
233   fgTie("/sim/rendering/lightning-enable", &sgEnviro,
234       &SGEnviro::get_lightning_enable_state,
235       &SGEnviro::set_lightning_enable_state);
236   fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
237       &SGEnviro::get_turbulence_enable_state,
238       &SGEnviro::set_turbulence_enable_state);
239   sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
240 }
241
242 void
243 FGEnvironmentMgr::unbind ()
244 {
245   fgUntie("/environment/visibility-m");
246   fgUntie("/environment/effective-visibility-m");
247   fgUntie("/environment/temperature-sea-level-degc");
248   fgUntie("/environment/temperature-degc");
249   fgUntie("/environment/dewpoint-sea-level-degc");
250   fgUntie("/environment/dewpoint-degc");
251   fgUntie("/environment/pressure-sea-level-inhg");
252   fgUntie("/environment/pressure-inhg");
253   fgUntie("/environment/density-inhg");
254   fgUntie("/environment/relative-humidity");
255   fgUntie("/environment/atmosphere/density-tropo-avg");
256   fgUntie("/environment/wind-from-north-fps");
257   fgUntie("/environment/wind-from-east-fps");
258   fgUntie("/environment/wind-from-down-fps");
259
260   fgUntie("/environment/thermal-lift-fps");
261   fgUntie("/environment/ridge-lift-fps");
262   fgUntie("/environment/local-weather-lift");
263
264   fgUntie("/environment/atmosphere/altitude-half-to-sun");
265   fgUntie("/environment/atmosphere/altitude-troposphere-top");
266   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
267     char buf[128];
268     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
269     fgUntie(buf);
270     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
271     fgUntie(buf);
272     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
273     fgUntie(buf);
274     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
275     fgUntie(buf);
276     sprintf(buf, "/environment/clouds/layer[%d]/type", i);
277     fgUntie(buf);
278   }
279   fgUntie("/sim/rendering/clouds3d-enable");
280   fgUntie("/sim/rendering/clouds3d-vis-range");
281   fgUntie("/sim/rendering/clouds3d-density");
282   fgUntie("/sim/rendering/precipitation-enable");
283   fgUntie("/environment/rebuild-layers");
284   fgUntie("/environment/weather-scenario");
285   fgUntie("/sim/rendering/lightning-enable");
286   fgUntie("/environment/turbulence/use-cloud-turbulence");
287   SGSubsystemGroup::unbind();
288 }
289
290 void
291 FGEnvironmentMgr::update (double dt)
292 {
293   SGSubsystemGroup::update(dt);
294   
295   _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
296   _environment->set_local_weather_lift_fps(fgGetDouble("/local-weather/current/thermal-lift"));
297   osg::Vec3 windVec(_environment->get_wind_from_north_fps(),
298                     -_environment->get_wind_from_east_fps(),
299                     0);
300           // SG_LOG(SG_GENERAL, SG_ALERT, "-_environment->get_wind_from_north_mps() " <<
301                    //_environment->get_wind_from_north_fps() * SG_FEET_TO_METER
302                    //<< " -_environment->get_wind_from_east_mps() " 
303                    //<< -_environment->get_wind_from_east_fps() * SG_FEET_TO_METER
304                    //);
305
306   // simgear::Particles::setWindVector(windVec * SG_FEET_TO_METER);
307   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
308                                    _environment->get_wind_speed_kt() );
309   //double wind_true_deg = _environment->get_wind_from_heading_deg();
310   //simgear::Particles::setWindFrom( wind_true_deg,
311   //                                _environment->get_wind_speed_kt() );
312
313 }
314
315 FGEnvironment
316 FGEnvironmentMgr::getEnvironment () const
317 {
318   return *_environment;
319 }
320
321 FGEnvironment
322 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
323 {
324                                 // Always returns the same environment
325                                 // for now; we'll make it interesting
326                                 // later.
327   FGEnvironment env = *_environment;
328   env.set_elevation_ft(alt);
329   return env;
330 }
331
332 FGEnvironment
333 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
334 {
335   // Always returns the same environment
336   // for now; we'll make it interesting
337   // later.
338   FGEnvironment env = *_environment;
339   env.set_elevation_ft(aPos.getElevationFt());
340   return env;
341
342 }
343
344 double
345 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
346 {
347   return thesky->get_cloud_layer(index)->getSpan_m();
348 }
349
350 void
351 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
352 {
353   thesky->get_cloud_layer(index)->setSpan_m(span_m);
354 }
355
356 double
357 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
358 {
359   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
360 }
361
362 void
363 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
364 {
365   FGEnvironment env = *_environment;
366   env.set_elevation_ft(elevation_ft);
367
368   thesky->get_cloud_layer(index)
369     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
370
371   thesky->get_cloud_layer(index)
372     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
373
374   thesky->get_cloud_layer(index)
375     ->setDirection(env.get_wind_from_heading_deg());
376 }
377
378 double
379 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
380 {
381   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
382 }
383
384 void
385 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
386 {
387   thesky->get_cloud_layer(index)
388     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
389 }
390
391 double
392 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
393 {
394   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
395 }
396
397 void
398 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
399                                                  double transition_ft)
400 {
401   thesky->get_cloud_layer(index)
402     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
403 }
404
405 const char *
406 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
407 {
408   switch (thesky->get_cloud_layer(index)->getCoverage()) {
409   case SGCloudLayer::SG_CLOUD_OVERCAST:
410     return "overcast";
411   case SGCloudLayer::SG_CLOUD_BROKEN:
412     return "broken";
413   case SGCloudLayer::SG_CLOUD_SCATTERED:
414     return "scattered";
415   case SGCloudLayer::SG_CLOUD_FEW:
416     return "few";
417   case SGCloudLayer::SG_CLOUD_CIRRUS:
418     return "cirrus";
419   case SGCloudLayer::SG_CLOUD_CLEAR:
420     return "clear";
421   default:
422     return "unknown";
423   }
424 }
425
426 void
427 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
428                                             const char * coverage_name)
429 {
430   SGCloudLayer::Coverage coverage;
431   if (!strcmp(coverage_name, "overcast"))
432     coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
433   else if (!strcmp(coverage_name, "broken"))
434     coverage = SGCloudLayer::SG_CLOUD_BROKEN;
435   else if (!strcmp(coverage_name, "scattered"))
436     coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
437   else if (!strcmp(coverage_name, "few"))
438     coverage = SGCloudLayer::SG_CLOUD_FEW;
439   else if (!strcmp(coverage_name, "cirrus"))
440     coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
441   else if (!strcmp(coverage_name, "clear"))
442     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
443   else {
444     SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
445     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
446   }
447   thesky->get_cloud_layer(index)->setCoverage(coverage);
448 }
449
450
451
452 // end of environment-mgr.cxx