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