]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Merge branch 'next' of git://gitorious.org/fg/flightgear into next
[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 #include <simgear/structure/event_mgr.hxx>
32
33 #include <Main/main.hxx>
34 #include <Main/fg_props.hxx>
35 #include <FDM/flight.hxx>
36
37 #include "environment.hxx"
38 #include "environment_mgr.hxx"
39 #include "environment_ctrl.hxx"
40 #include "realwx_ctrl.hxx"
41 #include "fgclouds.hxx"
42 #include "precipitation_mgr.hxx"
43 #include "ridge_lift.hxx"
44 #include "terrainsampler.hxx"
45 #include "Airports/simple.hxx"
46 #include "gravity.hxx"
47
48 class SGSky;
49 extern SGSky *thesky;
50
51 class FG3DCloudsListener : public SGPropertyChangeListener {
52 public:
53   FG3DCloudsListener( FGClouds * fgClouds );
54   virtual ~FG3DCloudsListener();
55
56   virtual void valueChanged (SGPropertyNode * node);
57
58 private:
59   FGClouds * _fgClouds;
60   SGPropertyNode_ptr _shaderNode;
61   SGPropertyNode_ptr _enableNode;
62 };
63
64 FG3DCloudsListener::FG3DCloudsListener( FGClouds * fgClouds ) :
65     _fgClouds( fgClouds ) 
66 {
67   _shaderNode = fgGetNode( "/sim/rendering/shader-effects", true );
68   _shaderNode->addChangeListener( this );
69
70   _enableNode = fgGetNode( "/sim/rendering/clouds3d-enable", true );
71   _enableNode->addChangeListener( this );
72
73   valueChanged( _enableNode );
74 }
75
76 FG3DCloudsListener::~FG3DCloudsListener()
77 {
78   _enableNode->removeChangeListener( this );
79   _shaderNode->removeChangeListener( this );
80 }
81
82 void FG3DCloudsListener::valueChanged( SGPropertyNode * node )
83 {
84   _fgClouds->set_3dClouds( _enableNode->getBoolValue() && _shaderNode->getBoolValue() );
85 }
86
87 FGEnvironmentMgr::FGEnvironmentMgr () :
88   _environment(new FGEnvironment()),
89   fgClouds(new FGClouds()),
90   _cloudLayersDirty(true),
91   _altitude_n(fgGetNode("/position/altitude-ft", true)),
92   _longitude_n(fgGetNode( "/position/longitude-deg", true )),
93   _latitude_n( fgGetNode( "/position/latitude-deg", true )),
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_ENVIRONMENT, 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   globals->get_event_mgr()->addTask("updateClosestAirport", this,
150                                     &FGEnvironmentMgr::updateClosestAirport, 30 );
151 }
152
153 void
154 FGEnvironmentMgr::shutdown()
155 {
156   globals->get_event_mgr()->removeTask("updateClosestAirport");
157 }
158
159 void
160 FGEnvironmentMgr::reinit ()
161 {
162   SG_LOG( SG_ENVIRONMENT, SG_INFO, "Reinitializing environment subsystem");
163   SGSubsystemGroup::reinit();
164 }
165
166 void
167 FGEnvironmentMgr::bind ()
168 {
169   SGSubsystemGroup::bind();
170   _environment->Tie( fgGetNode("/environment", true ) );
171
172   _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
173
174   _tiedProperties.Tie( "effective-visibility-m", thesky,
175           &SGSky::get_visibility );
176
177   _tiedProperties.Tie("rebuild-layers", fgClouds,
178           &FGClouds::get_update_event,
179           &FGClouds::set_update_event);
180
181 //  _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
182 //          &SGEnviro::get_turbulence_enable_state,
183 //          &SGEnviro::set_turbulence_enable_state);
184
185   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
186       SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
187
188       _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
189               &FGEnvironmentMgr::get_cloud_layer_span_m,
190               &FGEnvironmentMgr::set_cloud_layer_span_m);
191
192       _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
193               &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
194               &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
195
196       _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
197               &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
198               &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
199
200       _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
201               &FGEnvironmentMgr::get_cloud_layer_transition_ft,
202               &FGEnvironmentMgr::set_cloud_layer_transition_ft);
203
204       _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
205               &FGEnvironmentMgr::get_cloud_layer_coverage,
206               &FGEnvironmentMgr::set_cloud_layer_coverage);
207
208       _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
209               &FGEnvironmentMgr::get_cloud_layer_coverage_type,
210               &FGEnvironmentMgr::set_cloud_layer_coverage_type);
211
212       _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
213               &FGEnvironmentMgr::get_cloud_layer_visibility_m,
214               &FGEnvironmentMgr::set_cloud_layer_visibility_m);
215
216       _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
217               &FGEnvironmentMgr::get_cloud_layer_maxalpha,
218               &FGEnvironmentMgr::set_cloud_layer_maxalpha);
219   }
220
221   _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
222
223   _tiedProperties.Tie( "clouds3d-density", thesky,
224           &SGSky::get_3dCloudDensity,
225           &SGSky::set_3dCloudDensity);
226
227   _tiedProperties.Tie("clouds3d-vis-range", thesky,
228           &SGSky::get_3dCloudVisRange,
229           &SGSky::set_3dCloudVisRange);
230
231   _tiedProperties.Tie("clouds3d-wrap", thesky,
232           &SGSky::get_3dCloudWrap,
233           &SGSky::set_3dCloudWrap);
234
235 //  _tiedProperties.Tie("lightning-enable", &sgEnviro,
236 //          &SGEnviro::get_lightning_enable_state,
237 //          &SGEnviro::set_lightning_enable_state);
238
239 }
240
241 void
242 FGEnvironmentMgr::unbind ()
243 {
244   _tiedProperties.Untie();
245   _environment->Untie();
246   SGSubsystemGroup::unbind();
247 }
248
249 void
250 FGEnvironmentMgr::update (double dt)
251 {
252   SGSubsystemGroup::update(dt);
253
254   _environment->set_elevation_ft( _altitude_n->getDoubleValue() );
255
256   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
257                                    _environment->get_wind_speed_kt() );
258   if( _cloudLayersDirty ) {
259     _cloudLayersDirty = false;
260     fgClouds->set_update_event( fgClouds->get_update_event()+1 );
261   }
262
263
264   fgSetDouble( "/environment/gravitational-acceleration-mps2", 
265     Environment::Gravity::instance()->getGravity(SGGeod::fromDegFt(
266       _longitude_n->getDoubleValue(),
267       _latitude_n->getDoubleValue(),
268       _altitude_n->getDoubleValue()
269   )));
270 }
271
272 void
273 FGEnvironmentMgr::updateClosestAirport()
274 {
275   SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "FGEnvironmentMgr::update: updating closest airport");
276   
277   SGGeod pos = globals->get_aircraft_position();
278   FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
279   if( nearestAirport == NULL )
280   {
281     SG_LOG(SG_ENVIRONMENT,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
282   }
283   else
284   {
285     const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
286     if (currentId != nearestAirport->ident())
287     {
288       SG_LOG(SG_ENVIRONMENT, SG_INFO, "FGEnvironmentMgr::updateClosestAirport: selected:" << nearestAirport->ident());
289       fgSetString("/sim/airport/closest-airport-id",
290                   nearestAirport->ident().c_str());
291     }
292   }
293
294 }
295
296 FGEnvironment
297 FGEnvironmentMgr::getEnvironment () const
298 {
299   return *_environment;
300 }
301
302 FGEnvironment
303 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
304 {
305   // Always returns the same environment
306   // for now; we'll make it interesting
307   // later.
308   FGEnvironment env = *_environment;
309   env.set_elevation_ft(alt);
310   return env;
311 }
312
313 FGEnvironment
314 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
315 {
316   // Always returns the same environment
317   // for now; we'll make it interesting
318   // later.
319   FGEnvironment env = *_environment;
320   env.set_elevation_ft(aPos.getElevationFt());
321   return env;
322
323 }
324
325 double
326 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
327 {
328   return thesky->get_cloud_layer(index)->getSpan_m();
329 }
330
331 void
332 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
333 {
334   thesky->get_cloud_layer(index)->setSpan_m(span_m);
335 }
336
337 double
338 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
339 {
340   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
341 }
342
343 void
344 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
345 {
346   FGEnvironment env = *_environment;
347   env.set_elevation_ft(elevation_ft);
348
349   thesky->get_cloud_layer(index)
350     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
351
352   thesky->get_cloud_layer(index)
353     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
354
355   thesky->get_cloud_layer(index)
356     ->setDirection(env.get_wind_from_heading_deg());
357 }
358
359 double
360 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
361 {
362   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
363 }
364
365 void
366 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
367 {
368   thesky->get_cloud_layer(index)
369     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
370 }
371
372 double
373 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
374 {
375   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
376 }
377
378 void
379 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
380                                                  double transition_ft)
381 {
382   thesky->get_cloud_layer(index)
383     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
384 }
385
386 const char *
387 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
388 {
389   return thesky->get_cloud_layer(index)->getCoverageString().c_str();
390 }
391
392 void
393 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
394                                             const char * coverage_name)
395 {
396   if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
397     return;
398
399   thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
400   _cloudLayersDirty = true;
401 }
402
403 int
404 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
405 {
406   return thesky->get_cloud_layer(index)->getCoverage();
407 }
408
409 double
410 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
411 {
412     return thesky->get_cloud_layer(index)->getVisibility_m();
413 }
414
415 void
416 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
417 {
418     thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
419 }
420
421 double
422 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
423 {
424     return thesky->get_cloud_layer(index)->getMaxAlpha();
425 }
426
427 void
428 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
429 {
430     thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
431 }
432
433 void
434 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
435 {
436   if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
437     SG_LOG(SG_ENVIRONMENT,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
438     return;
439   }
440
441   if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
442     return;
443
444   thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
445   _cloudLayersDirty = true;
446 }
447
448
449 // end of environment-mgr.cxx