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