]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Merge branch 'next' into durk-atc
[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 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <cstring>
26
27 #include <simgear/constants.h>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/scene/sky/sky.hxx>
30 #include <simgear/scene/model/particles.hxx>
31
32 #include <Main/main.hxx>
33 #include <Main/fg_props.hxx>
34 #include <FDM/flight.hxx>
35
36 #include "environment.hxx"
37 #include "environment_mgr.hxx"
38 #include "environment_ctrl.hxx"
39 #include "realwx_ctrl.hxx"
40 #include "fgclouds.hxx"
41 #include "precipitation_mgr.hxx"
42 #include "ridge_lift.hxx"
43 #include "terrainsampler.hxx"
44 #include "Airports/simple.hxx"
45 #include "gravity.hxx"
46
47 class SGSky;
48 extern SGSky *thesky;
49
50 class FG3DCloudsListener : public SGPropertyChangeListener {
51 public:
52   FG3DCloudsListener( FGClouds * fgClouds );
53   virtual ~FG3DCloudsListener();
54
55   virtual void valueChanged (SGPropertyNode * node);
56
57 private:
58   FGClouds * _fgClouds;
59   SGPropertyNode_ptr _shaderNode;
60   SGPropertyNode_ptr _enableNode;
61 };
62
63 FG3DCloudsListener::FG3DCloudsListener( FGClouds * fgClouds ) :
64     _fgClouds( fgClouds ) 
65 {
66   _shaderNode = fgGetNode( "/sim/rendering/shader-effects", true );
67   _shaderNode->addChangeListener( this );
68
69   _enableNode = fgGetNode( "/sim/rendering/clouds3d-enable", true );
70   _enableNode->addChangeListener( this );
71
72   valueChanged( _enableNode );
73 }
74
75 FG3DCloudsListener::~FG3DCloudsListener()
76 {
77   _enableNode->removeChangeListener( this );
78   _shaderNode->removeChangeListener( this );
79 }
80
81 void FG3DCloudsListener::valueChanged( SGPropertyNode * node )
82 {
83   _fgClouds->set_3dClouds( _enableNode->getBoolValue() && _shaderNode->getBoolValue() );
84 }
85
86 FGEnvironmentMgr::FGEnvironmentMgr () :
87   _environment(new FGEnvironment()),
88   fgClouds(new FGClouds()),
89   _cloudLayersDirty(true),
90   _altitude_n(fgGetNode("/position/altitude-ft", true)),
91   _longitude_n(fgGetNode( "/position/longitude-deg", true )),
92   _latitude_n( fgGetNode( "/position/latitude-deg", true )),
93   _positionTimeToLive(0.0),
94   _3dCloudsEnableListener(new FG3DCloudsListener(fgClouds) )
95 {
96   set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
97   set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
98
99   set_subsystem("precipitation", new FGPrecipitationMgr);
100   set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
101   set_subsystem("ridgelift", new FGRidgeLift);
102 }
103
104 FGEnvironmentMgr::~FGEnvironmentMgr ()
105 {
106   SGSubsystem * subsys;
107
108   subsys = get_subsystem( "ridgelift" );
109   remove_subsystem( "ridgelift" );
110   delete subsys;
111
112   subsys = get_subsystem( "terrainsampler" );
113   remove_subsystem( "terrainsampler" );
114   delete subsys;
115
116   subsys = get_subsystem( "precipitation" );
117   remove_subsystem("precipitation");
118   delete subsys;
119
120   subsys = get_subsystem("realwx");
121   remove_subsystem("realwx");
122   delete subsys;
123
124   subsys = get_subsystem("controller");
125   remove_subsystem("controller");
126   delete subsys;
127
128   delete fgClouds;
129   delete _environment;
130
131   delete _3dCloudsEnableListener;
132 }
133
134 void
135 FGEnvironmentMgr::init ()
136 {
137   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
138   SGSubsystemGroup::init();
139   fgClouds->Init();
140
141   // FIXME: is this really part of the environment_mgr?
142   // Initialize the longitude, latitude and altitude to the initial position
143   // of the aircraft so that the atmospheric properties (pressure, temperature
144   // and density) can be initialized accordingly.
145   _altitude_n->setDoubleValue(fgGetDouble("/sim/presets/altitude-ft"));
146   _longitude_n->setDoubleValue(fgGetDouble("/sim/presets/longitude-deg"));
147   _latitude_n->setDoubleValue(fgGetDouble("/sim/presets/latitude-deg"));
148 }
149
150 void
151 FGEnvironmentMgr::reinit ()
152 {
153   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
154   SGSubsystemGroup::reinit();
155 }
156
157 void
158 FGEnvironmentMgr::bind ()
159 {
160   SGSubsystemGroup::bind();
161   _environment->Tie( fgGetNode("/environment", true ) );
162
163   _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
164
165   _tiedProperties.Tie( "effective-visibility-m", thesky,
166           &SGSky::get_visibility );
167
168   _tiedProperties.Tie("rebuild-layers", fgClouds,
169           &FGClouds::get_update_event,
170           &FGClouds::set_update_event);
171
172 //  _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
173 //          &SGEnviro::get_turbulence_enable_state,
174 //          &SGEnviro::set_turbulence_enable_state);
175
176   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
177       SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
178
179       _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
180               &FGEnvironmentMgr::get_cloud_layer_span_m,
181               &FGEnvironmentMgr::set_cloud_layer_span_m);
182
183       _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
184               &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
185               &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
186
187       _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
188               &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
189               &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
190
191       _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
192               &FGEnvironmentMgr::get_cloud_layer_transition_ft,
193               &FGEnvironmentMgr::set_cloud_layer_transition_ft);
194
195       _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
196               &FGEnvironmentMgr::get_cloud_layer_coverage,
197               &FGEnvironmentMgr::set_cloud_layer_coverage);
198
199       _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
200               &FGEnvironmentMgr::get_cloud_layer_coverage_type,
201               &FGEnvironmentMgr::set_cloud_layer_coverage_type);
202
203       _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
204               &FGEnvironmentMgr::get_cloud_layer_visibility_m,
205               &FGEnvironmentMgr::set_cloud_layer_visibility_m);
206
207       _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
208               &FGEnvironmentMgr::get_cloud_layer_maxalpha,
209               &FGEnvironmentMgr::set_cloud_layer_maxalpha);
210   }
211
212   _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
213
214   _tiedProperties.Tie( "clouds3d-density", thesky,
215           &SGSky::get_3dCloudDensity,
216           &SGSky::set_3dCloudDensity);
217
218   _tiedProperties.Tie("clouds3d-vis-range", thesky,
219           &SGSky::get_3dCloudVisRange,
220           &SGSky::set_3dCloudVisRange);
221
222 //  _tiedProperties.Tie("lightning-enable", &sgEnviro,
223 //          &SGEnviro::get_lightning_enable_state,
224 //          &SGEnviro::set_lightning_enable_state);
225
226 }
227
228 void
229 FGEnvironmentMgr::unbind ()
230 {
231   _tiedProperties.Untie();
232   _environment->Untie();
233   SGSubsystemGroup::unbind();
234 }
235
236 void
237 FGEnvironmentMgr::update (double dt)
238 {
239   SGSubsystemGroup::update(dt);
240
241   _environment->set_elevation_ft( _altitude_n->getDoubleValue() );
242
243   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
244                                    _environment->get_wind_speed_kt() );
245   if( _cloudLayersDirty ) {
246     _cloudLayersDirty = false;
247     fgClouds->set_update_event( fgClouds->get_update_event()+1 );
248   }
249
250
251   fgSetDouble( "/environment/gravitational-acceleration-mps2", 
252     Environment::Gravity::instance()->getGravity(SGGeod::fromDegFt(
253       _longitude_n->getDoubleValue(),
254       _latitude_n->getDoubleValue(),
255       _altitude_n->getDoubleValue()
256   )));
257
258   _positionTimeToLive -= dt;
259   if( _positionTimeToLive <= 0.0 )
260   {
261       // update closest airport information
262       _positionTimeToLive = 30.0;
263
264       SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
265
266       SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
267                                    _latitude_n->getDoubleValue());
268
269       FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
270       if( nearestAirport == NULL )
271       {
272           SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
273       }
274       else
275       {
276           const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
277           if (currentId != nearestAirport->ident())
278           {
279               fgSetString("/sim/airport/closest-airport-id",
280                       nearestAirport->ident().c_str());
281           }
282       }
283   }
284 }
285
286 FGEnvironment
287 FGEnvironmentMgr::getEnvironment () const
288 {
289   return *_environment;
290 }
291
292 FGEnvironment
293 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
294 {
295   // Always returns the same environment
296   // for now; we'll make it interesting
297   // later.
298   FGEnvironment env = *_environment;
299   env.set_elevation_ft(alt);
300   return env;
301 }
302
303 FGEnvironment
304 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
305 {
306   // Always returns the same environment
307   // for now; we'll make it interesting
308   // later.
309   FGEnvironment env = *_environment;
310   env.set_elevation_ft(aPos.getElevationFt());
311   return env;
312
313 }
314
315 double
316 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
317 {
318   return thesky->get_cloud_layer(index)->getSpan_m();
319 }
320
321 void
322 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
323 {
324   thesky->get_cloud_layer(index)->setSpan_m(span_m);
325 }
326
327 double
328 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
329 {
330   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
331 }
332
333 void
334 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
335 {
336   FGEnvironment env = *_environment;
337   env.set_elevation_ft(elevation_ft);
338
339   thesky->get_cloud_layer(index)
340     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
341
342   thesky->get_cloud_layer(index)
343     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
344
345   thesky->get_cloud_layer(index)
346     ->setDirection(env.get_wind_from_heading_deg());
347 }
348
349 double
350 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
351 {
352   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
353 }
354
355 void
356 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
357 {
358   thesky->get_cloud_layer(index)
359     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
360 }
361
362 double
363 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
364 {
365   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
366 }
367
368 void
369 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
370                                                  double transition_ft)
371 {
372   thesky->get_cloud_layer(index)
373     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
374 }
375
376 const char *
377 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
378 {
379   return thesky->get_cloud_layer(index)->getCoverageString().c_str();
380 }
381
382 void
383 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
384                                             const char * coverage_name)
385 {
386   if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
387     return;
388
389   thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
390   _cloudLayersDirty = true;
391 }
392
393 int
394 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
395 {
396   return thesky->get_cloud_layer(index)->getCoverage();
397 }
398
399 double
400 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
401 {
402     return thesky->get_cloud_layer(index)->getVisibility_m();
403 }
404
405 void
406 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
407 {
408     thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
409 }
410
411 double
412 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
413 {
414     return thesky->get_cloud_layer(index)->getMaxAlpha();
415 }
416
417 void
418 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
419 {
420     thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
421 }
422
423 void
424 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
425 {
426   if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
427     SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
428     return;
429   }
430
431   if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
432     return;
433
434   thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
435   _cloudLayersDirty = true;
436 }
437
438
439 // end of environment-mgr.cxx