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