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