]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Expose a radio function (receiveBeacon) to the Nasal subsystem
[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/renderer.hxx>
35 #include <Main/fg_props.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/simple.hxx"
47 #include "gravity.hxx"
48
49 class FG3DCloudsListener : public SGPropertyChangeListener {
50 public:
51   FG3DCloudsListener( FGClouds * fgClouds );
52   virtual ~FG3DCloudsListener();
53
54   virtual void valueChanged (SGPropertyNode * node);
55
56 private:
57   FGClouds * _fgClouds;
58   SGPropertyNode_ptr _enableNode;
59 };
60
61 FG3DCloudsListener::FG3DCloudsListener( FGClouds * fgClouds ) :
62     _fgClouds( fgClouds ) 
63 {
64   _enableNode = fgGetNode( "/sim/rendering/clouds3d-enable", true );
65   _enableNode->addChangeListener( this );
66
67   valueChanged( _enableNode );
68 }
69
70 FG3DCloudsListener::~FG3DCloudsListener()
71 {
72   _enableNode->removeChangeListener( this );
73 }
74
75 void FG3DCloudsListener::valueChanged( SGPropertyNode * node )
76 {
77   _fgClouds->set_3dClouds( _enableNode->getBoolValue() );
78 }
79
80 FGEnvironmentMgr::FGEnvironmentMgr () :
81   _environment(new FGEnvironment()),
82   fgClouds(new FGClouds()),
83   _cloudLayersDirty(true),
84   _altitude_n(fgGetNode("/position/altitude-ft", true)),
85   _longitude_n(fgGetNode( "/position/longitude-deg", true )),
86   _latitude_n( fgGetNode( "/position/latitude-deg", true )),
87   _3dCloudsEnableListener(new FG3DCloudsListener(fgClouds) ),
88   _sky(globals->get_renderer()->getSky())
89 {
90   set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
91   set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
92
93   set_subsystem("precipitation", new FGPrecipitationMgr);
94   set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
95   set_subsystem("ridgelift", new FGRidgeLift);
96 }
97
98 FGEnvironmentMgr::~FGEnvironmentMgr ()
99 {
100   SGSubsystem * subsys;
101
102   subsys = get_subsystem( "ridgelift" );
103   remove_subsystem( "ridgelift" );
104   delete subsys;
105
106   subsys = get_subsystem( "terrainsampler" );
107   remove_subsystem( "terrainsampler" );
108   delete subsys;
109
110   subsys = get_subsystem( "precipitation" );
111   remove_subsystem("precipitation");
112   delete subsys;
113
114   subsys = get_subsystem("realwx");
115   remove_subsystem("realwx");
116   delete subsys;
117
118   subsys = get_subsystem("controller");
119   remove_subsystem("controller");
120   delete subsys;
121
122   delete fgClouds;
123   delete _environment;
124
125   delete _3dCloudsEnableListener;
126 }
127
128 void
129 FGEnvironmentMgr::init ()
130 {
131   SG_LOG( SG_ENVIRONMENT, SG_INFO, "Initializing environment subsystem");
132   SGSubsystemGroup::init();
133   fgClouds->Init();
134
135   // FIXME: is this really part of the environment_mgr?
136   // Initialize the longitude, latitude and altitude to the initial position
137   // of the aircraft so that the atmospheric properties (pressure, temperature
138   // and density) can be initialized accordingly.
139   _altitude_n->setDoubleValue(fgGetDouble("/sim/presets/altitude-ft"));
140   _longitude_n->setDoubleValue(fgGetDouble("/sim/presets/longitude-deg"));
141   _latitude_n->setDoubleValue(fgGetDouble("/sim/presets/latitude-deg"));
142   
143   globals->get_event_mgr()->addTask("updateClosestAirport", this,
144                                     &FGEnvironmentMgr::updateClosestAirport, 30 );
145 }
146
147 void
148 FGEnvironmentMgr::shutdown()
149 {
150   globals->get_event_mgr()->removeTask("updateClosestAirport");
151 }
152
153 void
154 FGEnvironmentMgr::reinit ()
155 {
156   SG_LOG( SG_ENVIRONMENT, SG_INFO, "Reinitializing environment subsystem");
157   SGSubsystemGroup::reinit();
158 }
159
160 void
161 FGEnvironmentMgr::bind ()
162 {
163   SGSubsystemGroup::bind();
164   _environment->Tie( fgGetNode("/environment", true ) );
165
166   _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
167
168   _tiedProperties.Tie( "effective-visibility-m", _sky,
169           &SGSky::get_visibility );
170
171   _tiedProperties.Tie("rebuild-layers", fgClouds,
172           &FGClouds::get_update_event,
173           &FGClouds::set_update_event);
174
175 //  _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
176 //          &SGEnviro::get_turbulence_enable_state,
177 //          &SGEnviro::set_turbulence_enable_state);
178
179   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
180       SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
181
182       _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
183               &FGEnvironmentMgr::get_cloud_layer_span_m,
184               &FGEnvironmentMgr::set_cloud_layer_span_m);
185
186       _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
187               &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
188               &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
189
190       _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
191               &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
192               &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
193
194       _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
195               &FGEnvironmentMgr::get_cloud_layer_transition_ft,
196               &FGEnvironmentMgr::set_cloud_layer_transition_ft);
197
198       _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
199               &FGEnvironmentMgr::get_cloud_layer_coverage,
200               &FGEnvironmentMgr::set_cloud_layer_coverage);
201
202       _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
203               &FGEnvironmentMgr::get_cloud_layer_coverage_type,
204               &FGEnvironmentMgr::set_cloud_layer_coverage_type);
205
206       _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
207               &FGEnvironmentMgr::get_cloud_layer_visibility_m,
208               &FGEnvironmentMgr::set_cloud_layer_visibility_m);
209
210       _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
211               &FGEnvironmentMgr::get_cloud_layer_maxalpha,
212               &FGEnvironmentMgr::set_cloud_layer_maxalpha);
213   }
214
215   _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
216
217   _tiedProperties.Tie( "clouds3d-density", _sky,
218           &SGSky::get_3dCloudDensity,
219           &SGSky::set_3dCloudDensity);
220
221   _tiedProperties.Tie("clouds3d-vis-range", _sky,
222           &SGSky::get_3dCloudVisRange,
223           &SGSky::set_3dCloudVisRange);
224
225   _tiedProperties.Tie("clouds3d-wrap", _sky,
226           &SGSky::get_3dCloudWrap,
227           &SGSky::set_3dCloudWrap);
228
229 //  _tiedProperties.Tie("lightning-enable", &sgEnviro,
230 //          &SGEnviro::get_lightning_enable_state,
231 //          &SGEnviro::set_lightning_enable_state);
232
233 }
234
235 void
236 FGEnvironmentMgr::unbind ()
237 {
238   _tiedProperties.Untie();
239   _environment->Untie();
240   SGSubsystemGroup::unbind();
241 }
242
243 void
244 FGEnvironmentMgr::update (double dt)
245 {
246   SGSubsystemGroup::update(dt);
247
248   _environment->set_elevation_ft( _altitude_n->getDoubleValue() );
249
250   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
251                                    _environment->get_wind_speed_kt() );
252   if( _cloudLayersDirty ) {
253     _cloudLayersDirty = false;
254     fgClouds->set_update_event( fgClouds->get_update_event()+1 );
255   }
256
257
258   fgSetDouble( "/environment/gravitational-acceleration-mps2", 
259     Environment::Gravity::instance()->getGravity(SGGeod::fromDegFt(
260       _longitude_n->getDoubleValue(),
261       _latitude_n->getDoubleValue(),
262       _altitude_n->getDoubleValue()
263   )));
264 }
265
266 void
267 FGEnvironmentMgr::updateClosestAirport()
268 {
269   SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "FGEnvironmentMgr::update: updating closest airport");
270   
271   SGGeod pos = globals->get_aircraft_position();
272   FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
273   if( nearestAirport == NULL )
274   {
275     SG_LOG(SG_ENVIRONMENT,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
276   }
277   else
278   {
279     const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
280     if (currentId != nearestAirport->ident())
281     {
282       SG_LOG(SG_ENVIRONMENT, SG_INFO, "FGEnvironmentMgr::updateClosestAirport: selected:" << nearestAirport->ident());
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 _sky->get_cloud_layer(index)->getSpan_m();
323 }
324
325 void
326 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
327 {
328   _sky->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 _sky->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   _sky->get_cloud_layer(index)
344     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
345
346   _sky->get_cloud_layer(index)
347     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
348
349   _sky->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 _sky->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   _sky->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 _sky->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   _sky->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 _sky->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( _sky->get_cloud_layer(index)->getCoverageString() == coverage_name )
391     return;
392
393   _sky->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 _sky->get_cloud_layer(index)->getCoverage();
401 }
402
403 double
404 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
405 {
406     return _sky->get_cloud_layer(index)->getVisibility_m();
407 }
408
409 void
410 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
411 {
412     _sky->get_cloud_layer(index)->setVisibility_m(visibility_m);
413 }
414
415 double
416 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
417 {
418     return _sky->get_cloud_layer(index)->getMaxAlpha();
419 }
420
421 void
422 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
423 {
424     _sky->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_ENVIRONMENT,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
432     return;
433   }
434
435   if( static_cast<SGCloudLayer::Coverage>(type) == _sky->get_cloud_layer(index)->getCoverage() )
436     return;
437
438   _sky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
439   _cloudLayersDirty = true;
440 }
441
442
443 // end of environment-mgr.cxx