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