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