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>
32 #include <Main/main.hxx>
33 #include <Main/fg_props.hxx>
34 #include <FDM/flight.hxx>
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"
50 class FG3DCloudsListener : public SGPropertyChangeListener {
52 FG3DCloudsListener( FGClouds * fgClouds );
53 virtual ~FG3DCloudsListener();
55 virtual void valueChanged (SGPropertyNode * node);
59 SGPropertyNode_ptr _shaderNode;
60 SGPropertyNode_ptr _enableNode;
63 FG3DCloudsListener::FG3DCloudsListener( FGClouds * fgClouds ) :
66 _shaderNode = fgGetNode( "/sim/rendering/shader-effects", true );
67 _shaderNode->addChangeListener( this );
69 _enableNode = fgGetNode( "/sim/rendering/clouds3d-enable", true );
70 _enableNode->addChangeListener( this );
72 valueChanged( _enableNode );
75 FG3DCloudsListener::~FG3DCloudsListener()
77 _enableNode->removeChangeListener( this );
78 _shaderNode->removeChangeListener( this );
81 void FG3DCloudsListener::valueChanged( SGPropertyNode * node )
83 _fgClouds->set_3dClouds( _enableNode->getBoolValue() && _shaderNode->getBoolValue() );
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) )
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_GENERAL, 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"));
151 FGEnvironmentMgr::reinit ()
153 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
154 SGSubsystemGroup::reinit();
158 FGEnvironmentMgr::bind ()
160 SGSubsystemGroup::bind();
161 _environment->Tie( fgGetNode("/environment", true ) );
163 _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
165 _tiedProperties.Tie( "effective-visibility-m", thesky,
166 &SGSky::get_visibility );
168 _tiedProperties.Tie("rebuild-layers", fgClouds,
169 &FGClouds::get_update_event,
170 &FGClouds::set_update_event);
172 // _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
173 // &SGEnviro::get_turbulence_enable_state,
174 // &SGEnviro::set_turbulence_enable_state);
176 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
177 SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
179 _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
180 &FGEnvironmentMgr::get_cloud_layer_span_m,
181 &FGEnvironmentMgr::set_cloud_layer_span_m);
183 _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
184 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
185 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
187 _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
188 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
189 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
191 _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
192 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
193 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
195 _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
196 &FGEnvironmentMgr::get_cloud_layer_coverage,
197 &FGEnvironmentMgr::set_cloud_layer_coverage);
199 _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
200 &FGEnvironmentMgr::get_cloud_layer_coverage_type,
201 &FGEnvironmentMgr::set_cloud_layer_coverage_type);
203 _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
204 &FGEnvironmentMgr::get_cloud_layer_visibility_m,
205 &FGEnvironmentMgr::set_cloud_layer_visibility_m);
207 _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
208 &FGEnvironmentMgr::get_cloud_layer_maxalpha,
209 &FGEnvironmentMgr::set_cloud_layer_maxalpha);
212 _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
214 _tiedProperties.Tie( "clouds3d-density", thesky,
215 &SGSky::get_3dCloudDensity,
216 &SGSky::set_3dCloudDensity);
218 _tiedProperties.Tie("clouds3d-vis-range", thesky,
219 &SGSky::get_3dCloudVisRange,
220 &SGSky::set_3dCloudVisRange);
222 // _tiedProperties.Tie("lightning-enable", &sgEnviro,
223 // &SGEnviro::get_lightning_enable_state,
224 // &SGEnviro::set_lightning_enable_state);
229 FGEnvironmentMgr::unbind ()
231 _tiedProperties.Untie();
232 _environment->Untie();
233 SGSubsystemGroup::unbind();
237 FGEnvironmentMgr::update (double dt)
239 SGSubsystemGroup::update(dt);
241 _environment->set_elevation_ft( _altitude_n->getDoubleValue() );
243 simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
244 _environment->get_wind_speed_kt() );
245 if( _cloudLayersDirty ) {
246 _cloudLayersDirty = false;
247 fgClouds->set_update_event( fgClouds->get_update_event()+1 );
251 fgSetDouble( "/environment/gravitational-acceleration-mps2",
252 Environment::Gravity::instance()->getGravity(SGGeod::fromDegFt(
253 _longitude_n->getDoubleValue(),
254 _latitude_n->getDoubleValue(),
255 _altitude_n->getDoubleValue()
258 _positionTimeToLive -= dt;
259 if( _positionTimeToLive <= 0.0 )
261 // update closest airport information
262 _positionTimeToLive = 30.0;
264 SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
266 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
267 _latitude_n->getDoubleValue());
269 FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
270 if( nearestAirport == NULL )
272 SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
276 const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
277 if (currentId != nearestAirport->ident())
279 fgSetString("/sim/airport/closest-airport-id",
280 nearestAirport->ident().c_str());
287 FGEnvironmentMgr::getEnvironment () const
289 return *_environment;
293 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
295 // Always returns the same environment
296 // for now; we'll make it interesting
298 FGEnvironment env = *_environment;
299 env.set_elevation_ft(alt);
304 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
306 // Always returns the same environment
307 // for now; we'll make it interesting
309 FGEnvironment env = *_environment;
310 env.set_elevation_ft(aPos.getElevationFt());
316 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
318 return thesky->get_cloud_layer(index)->getSpan_m();
322 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
324 thesky->get_cloud_layer(index)->setSpan_m(span_m);
328 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
330 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
334 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
336 FGEnvironment env = *_environment;
337 env.set_elevation_ft(elevation_ft);
339 thesky->get_cloud_layer(index)
340 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
342 thesky->get_cloud_layer(index)
343 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
345 thesky->get_cloud_layer(index)
346 ->setDirection(env.get_wind_from_heading_deg());
350 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
352 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
356 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
358 thesky->get_cloud_layer(index)
359 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
363 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
365 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
369 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
370 double transition_ft)
372 thesky->get_cloud_layer(index)
373 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
377 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
379 return thesky->get_cloud_layer(index)->getCoverageString().c_str();
383 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
384 const char * coverage_name)
386 if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
389 thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
390 _cloudLayersDirty = true;
394 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
396 return thesky->get_cloud_layer(index)->getCoverage();
400 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
402 return thesky->get_cloud_layer(index)->getVisibility_m();
406 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
408 thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
412 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
414 return thesky->get_cloud_layer(index)->getMaxAlpha();
418 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
420 thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
424 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
426 if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
427 SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
431 if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
434 thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
435 _cloudLayersDirty = true;
439 // end of environment-mgr.cxx