]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Add aubmodels to AI objects
[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   simgear::Particles::setWindVector(windVec * SG_FEET_TO_METER);
301   //simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
302   //                       _environment->get_wind_speed_kt() );
303 }
304
305 FGEnvironment
306 FGEnvironmentMgr::getEnvironment () const
307 {
308   return *_environment;
309 }
310
311 FGEnvironment
312 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
313 {
314                                 // Always returns the same environment
315                                 // for now; we'll make it interesting
316                                 // later.
317   FGEnvironment env = *_environment;
318   env.set_elevation_ft(alt);
319   return env;
320 }
321
322 FGEnvironment
323 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
324 {
325   // Always returns the same environment
326   // for now; we'll make it interesting
327   // later.
328   FGEnvironment env = *_environment;
329   env.set_elevation_ft(aPos.getElevationFt());
330   return env;
331
332 }
333
334 double
335 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
336 {
337   return thesky->get_cloud_layer(index)->getSpan_m();
338 }
339
340 void
341 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
342 {
343   thesky->get_cloud_layer(index)->setSpan_m(span_m);
344 }
345
346 double
347 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
348 {
349   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
350 }
351
352 void
353 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
354 {
355   FGEnvironment env = *_environment;
356   env.set_elevation_ft(elevation_ft);
357
358   thesky->get_cloud_layer(index)
359     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
360
361   thesky->get_cloud_layer(index)
362     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
363
364   thesky->get_cloud_layer(index)
365     ->setDirection(env.get_wind_from_heading_deg());
366 }
367
368 double
369 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
370 {
371   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
372 }
373
374 void
375 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
376 {
377   thesky->get_cloud_layer(index)
378     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
379 }
380
381 double
382 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
383 {
384   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
385 }
386
387 void
388 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
389                                                  double transition_ft)
390 {
391   thesky->get_cloud_layer(index)
392     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
393 }
394
395 const char *
396 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
397 {
398   switch (thesky->get_cloud_layer(index)->getCoverage()) {
399   case SGCloudLayer::SG_CLOUD_OVERCAST:
400     return "overcast";
401   case SGCloudLayer::SG_CLOUD_BROKEN:
402     return "broken";
403   case SGCloudLayer::SG_CLOUD_SCATTERED:
404     return "scattered";
405   case SGCloudLayer::SG_CLOUD_FEW:
406     return "few";
407   case SGCloudLayer::SG_CLOUD_CIRRUS:
408     return "cirrus";
409   case SGCloudLayer::SG_CLOUD_CLEAR:
410     return "clear";
411   default:
412     return "unknown";
413   }
414 }
415
416 void
417 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
418                                             const char * coverage_name)
419 {
420   SGCloudLayer::Coverage coverage;
421   if (!strcmp(coverage_name, "overcast"))
422     coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
423   else if (!strcmp(coverage_name, "broken"))
424     coverage = SGCloudLayer::SG_CLOUD_BROKEN;
425   else if (!strcmp(coverage_name, "scattered"))
426     coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
427   else if (!strcmp(coverage_name, "few"))
428     coverage = SGCloudLayer::SG_CLOUD_FEW;
429   else if (!strcmp(coverage_name, "cirrus"))
430     coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
431   else if (!strcmp(coverage_name, "clear"))
432     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
433   else {
434     SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
435     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
436   }
437   thesky->get_cloud_layer(index)->setCoverage(coverage);
438 }
439
440
441
442 // end of environment-mgr.cxx