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