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