1 // environment-mgr.cxx -- manager for natural environment information.
3 // Written by David Megginson, started February 2002.
5 // Copyright (C) 2002 David Megginson - david@megginson.com
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.
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.
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.
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>
33 #include <Main/main.hxx>
34 #include <Main/fg_props.hxx>
35 #include <FDM/flight.hxx>
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"
51 class FG3DCloudsListener : public SGPropertyChangeListener {
53 FG3DCloudsListener( FGClouds * fgClouds );
54 virtual ~FG3DCloudsListener();
56 virtual void valueChanged (SGPropertyNode * node);
60 SGPropertyNode_ptr _shaderNode;
61 SGPropertyNode_ptr _enableNode;
64 FG3DCloudsListener::FG3DCloudsListener( FGClouds * fgClouds ) :
67 _shaderNode = fgGetNode( "/sim/rendering/shader-effects", true );
68 _shaderNode->addChangeListener( this );
70 _enableNode = fgGetNode( "/sim/rendering/clouds3d-enable", true );
71 _enableNode->addChangeListener( this );
73 valueChanged( _enableNode );
76 FG3DCloudsListener::~FG3DCloudsListener()
78 _enableNode->removeChangeListener( this );
79 _shaderNode->removeChangeListener( this );
82 void FG3DCloudsListener::valueChanged( SGPropertyNode * node )
84 _fgClouds->set_3dClouds( _enableNode->getBoolValue() && _shaderNode->getBoolValue() );
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) )
96 set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
97 set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
99 set_subsystem("precipitation", new FGPrecipitationMgr);
100 set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
101 set_subsystem("ridgelift", new FGRidgeLift);
104 FGEnvironmentMgr::~FGEnvironmentMgr ()
106 SGSubsystem * subsys;
108 subsys = get_subsystem( "ridgelift" );
109 remove_subsystem( "ridgelift" );
112 subsys = get_subsystem( "terrainsampler" );
113 remove_subsystem( "terrainsampler" );
116 subsys = get_subsystem( "precipitation" );
117 remove_subsystem("precipitation");
120 subsys = get_subsystem("realwx");
121 remove_subsystem("realwx");
124 subsys = get_subsystem("controller");
125 remove_subsystem("controller");
131 delete _3dCloudsEnableListener;
135 FGEnvironmentMgr::init ()
137 SG_LOG( SG_ENVIRONMENT, SG_INFO, "Initializing environment subsystem");
138 SGSubsystemGroup::init();
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"));
149 globals->get_event_mgr()->addTask("updateClosestAirport", this,
150 &FGEnvironmentMgr::updateClosestAirport, 30 );
154 FGEnvironmentMgr::shutdown()
156 globals->get_event_mgr()->removeTask("updateClosestAirport");
160 FGEnvironmentMgr::reinit ()
162 SG_LOG( SG_ENVIRONMENT, SG_INFO, "Reinitializing environment subsystem");
163 SGSubsystemGroup::reinit();
167 FGEnvironmentMgr::bind ()
169 SGSubsystemGroup::bind();
170 _environment->Tie( fgGetNode("/environment", true ) );
172 _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
174 _tiedProperties.Tie( "effective-visibility-m", thesky,
175 &SGSky::get_visibility );
177 _tiedProperties.Tie("rebuild-layers", fgClouds,
178 &FGClouds::get_update_event,
179 &FGClouds::set_update_event);
181 // _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
182 // &SGEnviro::get_turbulence_enable_state,
183 // &SGEnviro::set_turbulence_enable_state);
185 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
186 SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
188 _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
189 &FGEnvironmentMgr::get_cloud_layer_span_m,
190 &FGEnvironmentMgr::set_cloud_layer_span_m);
192 _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
193 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
194 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
196 _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
197 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
198 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
200 _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
201 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
202 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
204 _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
205 &FGEnvironmentMgr::get_cloud_layer_coverage,
206 &FGEnvironmentMgr::set_cloud_layer_coverage);
208 _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
209 &FGEnvironmentMgr::get_cloud_layer_coverage_type,
210 &FGEnvironmentMgr::set_cloud_layer_coverage_type);
212 _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
213 &FGEnvironmentMgr::get_cloud_layer_visibility_m,
214 &FGEnvironmentMgr::set_cloud_layer_visibility_m);
216 _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
217 &FGEnvironmentMgr::get_cloud_layer_maxalpha,
218 &FGEnvironmentMgr::set_cloud_layer_maxalpha);
221 _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
223 _tiedProperties.Tie( "clouds3d-density", thesky,
224 &SGSky::get_3dCloudDensity,
225 &SGSky::set_3dCloudDensity);
227 _tiedProperties.Tie("clouds3d-vis-range", thesky,
228 &SGSky::get_3dCloudVisRange,
229 &SGSky::set_3dCloudVisRange);
231 _tiedProperties.Tie("clouds3d-wrap", thesky,
232 &SGSky::get_3dCloudWrap,
233 &SGSky::set_3dCloudWrap);
235 // _tiedProperties.Tie("lightning-enable", &sgEnviro,
236 // &SGEnviro::get_lightning_enable_state,
237 // &SGEnviro::set_lightning_enable_state);
242 FGEnvironmentMgr::unbind ()
244 _tiedProperties.Untie();
245 _environment->Untie();
246 SGSubsystemGroup::unbind();
250 FGEnvironmentMgr::update (double dt)
252 SGSubsystemGroup::update(dt);
254 _environment->set_elevation_ft( _altitude_n->getDoubleValue() );
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 );
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()
273 FGEnvironmentMgr::updateClosestAirport()
275 SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "FGEnvironmentMgr::update: updating closest airport");
277 SGGeod pos = globals->get_aircraft_position();
278 FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
279 if( nearestAirport == NULL )
281 SG_LOG(SG_ENVIRONMENT,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
285 const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
286 if (currentId != nearestAirport->ident())
288 SG_LOG(SG_ENVIRONMENT, SG_INFO, "FGEnvironmentMgr::updateClosestAirport: selected:" << nearestAirport->ident());
289 fgSetString("/sim/airport/closest-airport-id",
290 nearestAirport->ident().c_str());
297 FGEnvironmentMgr::getEnvironment () const
299 return *_environment;
303 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
305 // Always returns the same environment
306 // for now; we'll make it interesting
308 FGEnvironment env = *_environment;
309 env.set_elevation_ft(alt);
314 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
316 // Always returns the same environment
317 // for now; we'll make it interesting
319 FGEnvironment env = *_environment;
320 env.set_elevation_ft(aPos.getElevationFt());
326 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
328 return thesky->get_cloud_layer(index)->getSpan_m();
332 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
334 thesky->get_cloud_layer(index)->setSpan_m(span_m);
338 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
340 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
344 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
346 FGEnvironment env = *_environment;
347 env.set_elevation_ft(elevation_ft);
349 thesky->get_cloud_layer(index)
350 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
352 thesky->get_cloud_layer(index)
353 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
355 thesky->get_cloud_layer(index)
356 ->setDirection(env.get_wind_from_heading_deg());
360 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
362 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
366 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
368 thesky->get_cloud_layer(index)
369 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
373 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
375 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
379 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
380 double transition_ft)
382 thesky->get_cloud_layer(index)
383 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
387 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
389 return thesky->get_cloud_layer(index)->getCoverageString().c_str();
393 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
394 const char * coverage_name)
396 if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
399 thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
400 _cloudLayersDirty = true;
404 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
406 return thesky->get_cloud_layer(index)->getCoverage();
410 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
412 return thesky->get_cloud_layer(index)->getVisibility_m();
416 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
418 thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
422 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
424 return thesky->get_cloud_layer(index)->getMaxAlpha();
428 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
430 thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
434 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
436 if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
437 SG_LOG(SG_ENVIRONMENT,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
441 if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
444 thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
445 _cloudLayersDirty = true;
449 // end of environment-mgr.cxx