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