]> 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("clouds3d-wrap", thesky,
223           &SGSky::get_3dCloudWrap,
224           &SGSky::set_3dCloudWrap);
225
226 //  _tiedProperties.Tie("lightning-enable", &sgEnviro,
227 //          &SGEnviro::get_lightning_enable_state,
228 //          &SGEnviro::set_lightning_enable_state);
229
230 }
231
232 void
233 FGEnvironmentMgr::unbind ()
234 {
235   _tiedProperties.Untie();
236   _environment->Untie();
237   SGSubsystemGroup::unbind();
238 }
239
240 void
241 FGEnvironmentMgr::update (double dt)
242 {
243   SGSubsystemGroup::update(dt);
244
245   _environment->set_elevation_ft( _altitude_n->getDoubleValue() );
246
247   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
248                                    _environment->get_wind_speed_kt() );
249   if( _cloudLayersDirty ) {
250     _cloudLayersDirty = false;
251     fgClouds->set_update_event( fgClouds->get_update_event()+1 );
252   }
253
254
255   fgSetDouble( "/environment/gravitational-acceleration-mps2", 
256     Environment::Gravity::instance()->getGravity(SGGeod::fromDegFt(
257       _longitude_n->getDoubleValue(),
258       _latitude_n->getDoubleValue(),
259       _altitude_n->getDoubleValue()
260   )));
261
262   _positionTimeToLive -= dt;
263   if( _positionTimeToLive <= 0.0 )
264   {
265       // update closest airport information
266       _positionTimeToLive = 30.0;
267
268       SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
269
270       SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
271                                    _latitude_n->getDoubleValue());
272
273       FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
274       if( nearestAirport == NULL )
275       {
276           SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
277       }
278       else
279       {
280           const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
281           if (currentId != nearestAirport->ident())
282           {
283               fgSetString("/sim/airport/closest-airport-id",
284                       nearestAirport->ident().c_str());
285           }
286       }
287   }
288 }
289
290 FGEnvironment
291 FGEnvironmentMgr::getEnvironment () const
292 {
293   return *_environment;
294 }
295
296 FGEnvironment
297 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
298 {
299   // Always returns the same environment
300   // for now; we'll make it interesting
301   // later.
302   FGEnvironment env = *_environment;
303   env.set_elevation_ft(alt);
304   return env;
305 }
306
307 FGEnvironment
308 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
309 {
310   // Always returns the same environment
311   // for now; we'll make it interesting
312   // later.
313   FGEnvironment env = *_environment;
314   env.set_elevation_ft(aPos.getElevationFt());
315   return env;
316
317 }
318
319 double
320 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
321 {
322   return thesky->get_cloud_layer(index)->getSpan_m();
323 }
324
325 void
326 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
327 {
328   thesky->get_cloud_layer(index)->setSpan_m(span_m);
329 }
330
331 double
332 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
333 {
334   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
335 }
336
337 void
338 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
339 {
340   FGEnvironment env = *_environment;
341   env.set_elevation_ft(elevation_ft);
342
343   thesky->get_cloud_layer(index)
344     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
345
346   thesky->get_cloud_layer(index)
347     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
348
349   thesky->get_cloud_layer(index)
350     ->setDirection(env.get_wind_from_heading_deg());
351 }
352
353 double
354 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
355 {
356   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
357 }
358
359 void
360 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
361 {
362   thesky->get_cloud_layer(index)
363     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
364 }
365
366 double
367 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
368 {
369   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
370 }
371
372 void
373 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
374                                                  double transition_ft)
375 {
376   thesky->get_cloud_layer(index)
377     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
378 }
379
380 const char *
381 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
382 {
383   return thesky->get_cloud_layer(index)->getCoverageString().c_str();
384 }
385
386 void
387 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
388                                             const char * coverage_name)
389 {
390   if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
391     return;
392
393   thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
394   _cloudLayersDirty = true;
395 }
396
397 int
398 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
399 {
400   return thesky->get_cloud_layer(index)->getCoverage();
401 }
402
403 double
404 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
405 {
406     return thesky->get_cloud_layer(index)->getVisibility_m();
407 }
408
409 void
410 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
411 {
412     thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
413 }
414
415 double
416 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
417 {
418     return thesky->get_cloud_layer(index)->getMaxAlpha();
419 }
420
421 void
422 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
423 {
424     thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
425 }
426
427 void
428 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
429 {
430   if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
431     SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
432     return;
433   }
434
435   if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
436     return;
437
438   thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
439   _cloudLayersDirty = true;
440 }
441
442
443 // end of environment-mgr.cxx