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