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 FGEnvironmentMgr::FGEnvironmentMgr () :
51 _environment(new FGEnvironment()),
52 fgClouds(new FGClouds()),
53 _cloudLayersDirty(true),
54 _altitude_n(fgGetNode("/position/altitude-ft", true)),
55 _longitude_n(fgGetNode( "/position/longitude-deg", true )),
56 _latitude_n( fgGetNode( "/position/latitude-deg", true )),
57 _positionTimeToLive(0.0)
59 set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
60 set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
62 set_subsystem("precipitation", new FGPrecipitationMgr);
63 set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
64 set_subsystem("ridgelift", new FGRidgeLift);
67 FGEnvironmentMgr::~FGEnvironmentMgr ()
71 subsys = get_subsystem( "ridgelift" );
72 remove_subsystem( "ridgelift" );
75 subsys = get_subsystem( "terrainsampler" );
76 remove_subsystem( "terrainsampler" );
79 subsys = get_subsystem( "precipitation" );
80 remove_subsystem("precipitation");
83 subsys = get_subsystem("realwx");
84 remove_subsystem("realwx");
87 subsys = get_subsystem("controller");
88 remove_subsystem("controller");
96 FGEnvironmentMgr::init ()
98 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
99 SGSubsystemGroup::init();
102 // FIXME: is this really part of the environment_mgr?
103 // Initialize the longitude, latitude and altitude to the initial position
104 // of the aircraft so that the atmospheric properties (pressure, temperature
105 // and density) can be initialized accordingly.
106 _altitude_n->setDoubleValue(fgGetDouble("/sim/presets/altitude-ft"));
107 _longitude_n->setDoubleValue(fgGetDouble("/sim/presets/longitude-deg"));
108 _latitude_n->setDoubleValue(fgGetDouble("/sim/presets/latitude-deg"));
112 FGEnvironmentMgr::reinit ()
114 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
115 SGSubsystemGroup::reinit();
119 FGEnvironmentMgr::bind ()
121 SGSubsystemGroup::bind();
122 _environment->Tie( fgGetNode("/environment", true ) );
124 _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
126 _tiedProperties.Tie( "effective-visibility-m", thesky,
127 &SGSky::get_visibility );
129 _tiedProperties.Tie("rebuild-layers", fgClouds,
130 &FGClouds::get_update_event,
131 &FGClouds::set_update_event);
133 // _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
134 // &SGEnviro::get_turbulence_enable_state,
135 // &SGEnviro::set_turbulence_enable_state);
137 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
138 SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
140 _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
141 &FGEnvironmentMgr::get_cloud_layer_span_m,
142 &FGEnvironmentMgr::set_cloud_layer_span_m);
144 _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
145 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
146 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
148 _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
149 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
150 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
152 _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
153 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
154 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
156 _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
157 &FGEnvironmentMgr::get_cloud_layer_coverage,
158 &FGEnvironmentMgr::set_cloud_layer_coverage);
160 _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
161 &FGEnvironmentMgr::get_cloud_layer_coverage_type,
162 &FGEnvironmentMgr::set_cloud_layer_coverage_type);
164 _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
165 &FGEnvironmentMgr::get_cloud_layer_visibility_m,
166 &FGEnvironmentMgr::set_cloud_layer_visibility_m);
168 _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
169 &FGEnvironmentMgr::get_cloud_layer_maxalpha,
170 &FGEnvironmentMgr::set_cloud_layer_maxalpha);
173 _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
175 _tiedProperties.Tie( "clouds3d-enable", fgClouds,
176 &FGClouds::get_3dClouds,
177 &FGClouds::set_3dClouds);
179 _tiedProperties.Tie( "clouds3d-density", thesky,
180 &SGSky::get_3dCloudDensity,
181 &SGSky::set_3dCloudDensity);
183 _tiedProperties.Tie("clouds3d-vis-range", thesky,
184 &SGSky::get_3dCloudVisRange,
185 &SGSky::set_3dCloudVisRange);
187 // _tiedProperties.Tie("lightning-enable", &sgEnviro,
188 // &SGEnviro::get_lightning_enable_state,
189 // &SGEnviro::set_lightning_enable_state);
194 FGEnvironmentMgr::unbind ()
196 _tiedProperties.Untie();
197 _environment->Untie();
198 SGSubsystemGroup::unbind();
202 FGEnvironmentMgr::update (double dt)
204 SGSubsystemGroup::update(dt);
206 _environment->set_elevation_ft( _altitude_n->getDoubleValue() );
208 simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
209 _environment->get_wind_speed_kt() );
210 if( _cloudLayersDirty ) {
211 _cloudLayersDirty = false;
212 fgClouds->set_update_event( fgClouds->get_update_event()+1 );
216 fgSetDouble( "/environment/gravitational-acceleration-mps2",
217 Environment::Gravity::instance()->getGravity(SGGeod::fromDegFt(
218 _longitude_n->getDoubleValue(),
219 _latitude_n->getDoubleValue(),
220 _altitude_n->getDoubleValue()
223 _positionTimeToLive -= dt;
224 if( _positionTimeToLive <= 0.0 )
226 // update closest airport information
227 _positionTimeToLive = 30.0;
229 SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
231 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
232 _latitude_n->getDoubleValue());
234 FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
235 if( nearestAirport == NULL )
237 SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
241 const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
242 if (currentId != nearestAirport->ident())
244 fgSetString("/sim/airport/closest-airport-id",
245 nearestAirport->ident().c_str());
252 FGEnvironmentMgr::getEnvironment () const
254 return *_environment;
258 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
260 // Always returns the same environment
261 // for now; we'll make it interesting
263 FGEnvironment env = *_environment;
264 env.set_elevation_ft(alt);
269 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
271 // Always returns the same environment
272 // for now; we'll make it interesting
274 FGEnvironment env = *_environment;
275 env.set_elevation_ft(aPos.getElevationFt());
281 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
283 return thesky->get_cloud_layer(index)->getSpan_m();
287 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
289 thesky->get_cloud_layer(index)->setSpan_m(span_m);
293 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
295 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
299 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
301 FGEnvironment env = *_environment;
302 env.set_elevation_ft(elevation_ft);
304 thesky->get_cloud_layer(index)
305 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
307 thesky->get_cloud_layer(index)
308 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
310 thesky->get_cloud_layer(index)
311 ->setDirection(env.get_wind_from_heading_deg());
315 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
317 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
321 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
323 thesky->get_cloud_layer(index)
324 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
328 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
330 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
334 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
335 double transition_ft)
337 thesky->get_cloud_layer(index)
338 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
342 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
344 return thesky->get_cloud_layer(index)->getCoverageString().c_str();
348 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
349 const char * coverage_name)
351 if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
354 thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
355 _cloudLayersDirty = true;
359 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
361 return thesky->get_cloud_layer(index)->getCoverage();
365 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
367 return thesky->get_cloud_layer(index)->getVisibility_m();
371 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
373 thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
377 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
379 return thesky->get_cloud_layer(index)->getMaxAlpha();
383 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
385 thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
389 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
391 if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
392 SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
396 if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
399 thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
400 _cloudLayersDirty = true;
404 // end of environment-mgr.cxx