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"
49 FGEnvironmentMgr::FGEnvironmentMgr () :
50 _environment(new FGEnvironment()),
51 fgClouds(new FGClouds()),
52 _cloudLayersDirty(true),
53 _altitudeNode(fgGetNode("/position/altitude-ft", true)),
54 _longitude_n(fgGetNode( "/position/longitude-deg", true )),
55 _latitude_n( fgGetNode( "/position/latitude-deg", true )),
56 _positionTimeToLive(0.0)
58 set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
59 set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
61 set_subsystem("precipitation", new FGPrecipitationMgr);
62 set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
63 set_subsystem("ridgelift", new FGRidgeLift);
66 FGEnvironmentMgr::~FGEnvironmentMgr ()
70 subsys = get_subsystem( "ridgelift" );
71 remove_subsystem( "ridgelift" );
74 subsys = get_subsystem( "terrainsampler" );
75 remove_subsystem( "terrainsampler" );
78 subsys = get_subsystem( "precipitation" );
79 remove_subsystem("precipitation");
82 subsys = get_subsystem("realwx");
83 remove_subsystem("realwx");
86 subsys = get_subsystem("controller");
87 remove_subsystem("controller");
95 FGEnvironmentMgr::init ()
97 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
98 SGSubsystemGroup::init();
101 // Initialize the longitude, latitude and altitude to the initial position
102 // of the aircraft so that the atmospheric properties (pressure, temperature
103 // and density) can be initialized accordingly.
104 _altitudeNode->setDoubleValue(fgGetDouble("/sim/presets/altitude-ft"));
105 _longitude_n->setDoubleValue(fgGetDouble("/sim/presets/longitude-deg"));
106 _latitude_n->setDoubleValue(fgGetDouble("/sim/presets/latitude-deg"));
110 FGEnvironmentMgr::reinit ()
112 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
113 SGSubsystemGroup::reinit();
117 FGEnvironmentMgr::bind ()
119 SGSubsystemGroup::bind();
120 _environment->Tie( fgGetNode("/environment", true ) );
122 _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
124 _tiedProperties.Tie( "effective-visibility-m", thesky,
125 &SGSky::get_visibility );
127 _tiedProperties.Tie("rebuild-layers", fgClouds,
128 &FGClouds::get_update_event,
129 &FGClouds::set_update_event);
131 // _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
132 // &SGEnviro::get_turbulence_enable_state,
133 // &SGEnviro::set_turbulence_enable_state);
135 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
136 SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
138 _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
139 &FGEnvironmentMgr::get_cloud_layer_span_m,
140 &FGEnvironmentMgr::set_cloud_layer_span_m);
142 _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
143 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
144 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
146 _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
147 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
148 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
150 _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
151 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
152 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
154 _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
155 &FGEnvironmentMgr::get_cloud_layer_coverage,
156 &FGEnvironmentMgr::set_cloud_layer_coverage);
158 _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
159 &FGEnvironmentMgr::get_cloud_layer_coverage_type,
160 &FGEnvironmentMgr::set_cloud_layer_coverage_type);
162 _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
163 &FGEnvironmentMgr::get_cloud_layer_visibility_m,
164 &FGEnvironmentMgr::set_cloud_layer_visibility_m);
166 _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
167 &FGEnvironmentMgr::get_cloud_layer_maxalpha,
168 &FGEnvironmentMgr::set_cloud_layer_maxalpha);
171 _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
173 _tiedProperties.Tie( "clouds3d-enable", fgClouds,
174 &FGClouds::get_3dClouds,
175 &FGClouds::set_3dClouds);
177 _tiedProperties.Tie( "clouds3d-density", thesky,
178 &SGSky::get_3dCloudDensity,
179 &SGSky::set_3dCloudDensity);
181 _tiedProperties.Tie("clouds3d-vis-range", thesky,
182 &SGSky::get_3dCloudVisRange,
183 &SGSky::set_3dCloudVisRange);
185 // _tiedProperties.Tie("lightning-enable", &sgEnviro,
186 // &SGEnviro::get_lightning_enable_state,
187 // &SGEnviro::set_lightning_enable_state);
192 FGEnvironmentMgr::unbind ()
194 _tiedProperties.Untie();
195 _environment->Untie();
196 SGSubsystemGroup::unbind();
200 FGEnvironmentMgr::update (double dt)
202 SGSubsystemGroup::update(dt);
204 _environment->set_elevation_ft( _altitudeNode->getDoubleValue() );
206 simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
207 _environment->get_wind_speed_kt() );
208 if( _cloudLayersDirty ) {
209 _cloudLayersDirty = false;
210 fgClouds->set_update_event( fgClouds->get_update_event()+1 );
213 _positionTimeToLive -= dt;
214 if( _positionTimeToLive <= 0.0 )
216 // update closest airport information
217 _positionTimeToLive = 30.0;
219 SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
221 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
222 _latitude_n->getDoubleValue());
224 FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
225 if( nearestAirport == NULL )
227 SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
231 const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
232 if (currentId != nearestAirport->ident())
234 fgSetString("/sim/airport/closest-airport-id",
235 nearestAirport->ident().c_str());
242 FGEnvironmentMgr::getEnvironment () const
244 return *_environment;
248 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
250 // Always returns the same environment
251 // for now; we'll make it interesting
253 FGEnvironment env = *_environment;
254 env.set_elevation_ft(alt);
259 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
261 // Always returns the same environment
262 // for now; we'll make it interesting
264 FGEnvironment env = *_environment;
265 env.set_elevation_ft(aPos.getElevationFt());
271 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
273 return thesky->get_cloud_layer(index)->getSpan_m();
277 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
279 thesky->get_cloud_layer(index)->setSpan_m(span_m);
283 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
285 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
289 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
291 FGEnvironment env = *_environment;
292 env.set_elevation_ft(elevation_ft);
294 thesky->get_cloud_layer(index)
295 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
297 thesky->get_cloud_layer(index)
298 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
300 thesky->get_cloud_layer(index)
301 ->setDirection(env.get_wind_from_heading_deg());
305 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
307 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
311 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
313 thesky->get_cloud_layer(index)
314 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
318 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
320 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
324 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
325 double transition_ft)
327 thesky->get_cloud_layer(index)
328 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
332 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
334 return thesky->get_cloud_layer(index)->getCoverageString().c_str();
338 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
339 const char * coverage_name)
341 if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
344 thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
345 _cloudLayersDirty = true;
349 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
351 return thesky->get_cloud_layer(index)->getCoverage();
355 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
357 return thesky->get_cloud_layer(index)->getVisibility_m();
361 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
363 thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
367 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
369 return thesky->get_cloud_layer(index)->getMaxAlpha();
373 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
375 thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
379 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
381 if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
382 SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
386 if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
389 thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
390 _cloudLayersDirty = true;
394 // end of environment-mgr.cxx