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("clouds3d-wrap", thesky,
223 &SGSky::get_3dCloudWrap,
224 &SGSky::set_3dCloudWrap);
226 // _tiedProperties.Tie("lightning-enable", &sgEnviro,
227 // &SGEnviro::get_lightning_enable_state,
228 // &SGEnviro::set_lightning_enable_state);
233 FGEnvironmentMgr::unbind ()
235 _tiedProperties.Untie();
236 _environment->Untie();
237 SGSubsystemGroup::unbind();
241 FGEnvironmentMgr::update (double dt)
243 SGSubsystemGroup::update(dt);
245 _environment->set_elevation_ft( _altitude_n->getDoubleValue() );
247 simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
248 _environment->get_wind_speed_kt() );
249 if( _cloudLayersDirty ) {
250 _cloudLayersDirty = false;
251 fgClouds->set_update_event( fgClouds->get_update_event()+1 );
255 fgSetDouble( "/environment/gravitational-acceleration-mps2",
256 Environment::Gravity::instance()->getGravity(SGGeod::fromDegFt(
257 _longitude_n->getDoubleValue(),
258 _latitude_n->getDoubleValue(),
259 _altitude_n->getDoubleValue()
262 _positionTimeToLive -= dt;
263 if( _positionTimeToLive <= 0.0 )
265 // update closest airport information
266 _positionTimeToLive = 30.0;
268 SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
270 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
271 _latitude_n->getDoubleValue());
273 FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
274 if( nearestAirport == NULL )
276 SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
280 const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
281 if (currentId != nearestAirport->ident())
283 fgSetString("/sim/airport/closest-airport-id",
284 nearestAirport->ident().c_str());
291 FGEnvironmentMgr::getEnvironment () const
293 return *_environment;
297 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
299 // Always returns the same environment
300 // for now; we'll make it interesting
302 FGEnvironment env = *_environment;
303 env.set_elevation_ft(alt);
308 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
310 // Always returns the same environment
311 // for now; we'll make it interesting
313 FGEnvironment env = *_environment;
314 env.set_elevation_ft(aPos.getElevationFt());
320 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
322 return thesky->get_cloud_layer(index)->getSpan_m();
326 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
328 thesky->get_cloud_layer(index)->setSpan_m(span_m);
332 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
334 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
338 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
340 FGEnvironment env = *_environment;
341 env.set_elevation_ft(elevation_ft);
343 thesky->get_cloud_layer(index)
344 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
346 thesky->get_cloud_layer(index)
347 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
349 thesky->get_cloud_layer(index)
350 ->setDirection(env.get_wind_from_heading_deg());
354 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
356 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
360 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
362 thesky->get_cloud_layer(index)
363 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
367 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
369 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
373 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
374 double transition_ft)
376 thesky->get_cloud_layer(index)
377 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
381 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
383 return thesky->get_cloud_layer(index)->getCoverageString().c_str();
387 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
388 const char * coverage_name)
390 if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
393 thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
394 _cloudLayersDirty = true;
398 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
400 return thesky->get_cloud_layer(index)->getCoverage();
404 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
406 return thesky->get_cloud_layer(index)->getVisibility_m();
410 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
412 thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
416 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
418 return thesky->get_cloud_layer(index)->getMaxAlpha();
422 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
424 thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
428 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
430 if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
431 SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
435 if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
438 thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
439 _cloudLayersDirty = true;
443 // end of environment-mgr.cxx