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