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/environment/visual_enviro.hxx>
31 #include <simgear/scene/model/particles.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"
50 FGEnvironmentMgr::FGEnvironmentMgr () :
51 _environment(new FGEnvironment()),
52 fgClouds(new FGClouds()),
53 _cloudLayersDirty(true),
54 _altitudeNode(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();
104 FGEnvironmentMgr::reinit ()
106 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
107 SGSubsystemGroup::reinit();
111 FGEnvironmentMgr::bind ()
113 SGSubsystemGroup::bind();
114 _environment->Tie( fgGetNode("/environment", true ) );
116 _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
118 _tiedProperties.Tie( "effective-visibility-m", thesky,
119 &SGSky::get_visibility );
121 _tiedProperties.Tie("rebuild-layers", fgClouds,
122 &FGClouds::get_update_event,
123 &FGClouds::set_update_event);
125 _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
126 &SGEnviro::get_turbulence_enable_state,
127 &SGEnviro::set_turbulence_enable_state);
129 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
130 SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
132 _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
133 &FGEnvironmentMgr::get_cloud_layer_span_m,
134 &FGEnvironmentMgr::set_cloud_layer_span_m);
136 _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
137 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
138 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
140 _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
141 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
142 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
144 _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
145 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
146 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
148 _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
149 &FGEnvironmentMgr::get_cloud_layer_coverage,
150 &FGEnvironmentMgr::set_cloud_layer_coverage);
152 _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
153 &FGEnvironmentMgr::get_cloud_layer_coverage_type,
154 &FGEnvironmentMgr::set_cloud_layer_coverage_type);
156 _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
157 &FGEnvironmentMgr::get_cloud_layer_visibility_m,
158 &FGEnvironmentMgr::set_cloud_layer_visibility_m);
160 _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
161 &FGEnvironmentMgr::get_cloud_layer_maxalpha,
162 &FGEnvironmentMgr::set_cloud_layer_maxalpha);
165 _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
167 _tiedProperties.Tie( "clouds3d-enable", fgClouds,
168 &FGClouds::get_3dClouds,
169 &FGClouds::set_3dClouds);
171 _tiedProperties.Tie( "clouds3d-density", thesky,
172 &SGSky::get_3dCloudDensity,
173 &SGSky::set_3dCloudDensity);
175 _tiedProperties.Tie("clouds3d-vis-range", thesky,
176 &SGSky::get_3dCloudVisRange,
177 &SGSky::set_3dCloudVisRange);
179 _tiedProperties.Tie("precipitation-enable", &sgEnviro,
180 &SGEnviro::get_precipitation_enable_state,
181 &SGEnviro::set_precipitation_enable_state);
183 _tiedProperties.Tie("lightning-enable", &sgEnviro,
184 &SGEnviro::get_lightning_enable_state,
185 &SGEnviro::set_lightning_enable_state);
187 sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
191 FGEnvironmentMgr::unbind ()
193 _tiedProperties.Untie();
194 _environment->Untie();
195 SGSubsystemGroup::unbind();
199 FGEnvironmentMgr::update (double dt)
201 SGSubsystemGroup::update(dt);
203 _environment->set_elevation_ft( _altitudeNode->getDoubleValue() );
205 simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
206 _environment->get_wind_speed_kt() );
207 if( _cloudLayersDirty ) {
208 _cloudLayersDirty = false;
209 fgClouds->set_update_event( fgClouds->get_update_event()+1 );
212 _positionTimeToLive -= dt;
213 if( _positionTimeToLive <= 0.0 )
215 // update closest airport information
216 _positionTimeToLive = 30.0;
218 SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
220 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
221 _latitude_n->getDoubleValue());
223 FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
224 if( nearestAirport == NULL )
226 SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
230 const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
231 if (currentId != nearestAirport->ident())
233 fgSetString("/sim/airport/closest-airport-id",
234 nearestAirport->ident().c_str());
241 FGEnvironmentMgr::getEnvironment () const
243 return *_environment;
247 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
249 // Always returns the same environment
250 // for now; we'll make it interesting
252 FGEnvironment env = *_environment;
253 env.set_elevation_ft(alt);
258 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
260 // Always returns the same environment
261 // for now; we'll make it interesting
263 FGEnvironment env = *_environment;
264 env.set_elevation_ft(aPos.getElevationFt());
270 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
272 return thesky->get_cloud_layer(index)->getSpan_m();
276 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
278 thesky->get_cloud_layer(index)->setSpan_m(span_m);
282 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
284 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
288 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
290 FGEnvironment env = *_environment;
291 env.set_elevation_ft(elevation_ft);
293 thesky->get_cloud_layer(index)
294 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
296 thesky->get_cloud_layer(index)
297 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
299 thesky->get_cloud_layer(index)
300 ->setDirection(env.get_wind_from_heading_deg());
304 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
306 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
310 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
312 thesky->get_cloud_layer(index)
313 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
317 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
319 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
323 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
324 double transition_ft)
326 thesky->get_cloud_layer(index)
327 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
331 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
333 return thesky->get_cloud_layer(index)->getCoverageString().c_str();
337 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
338 const char * coverage_name)
340 if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
343 thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
344 _cloudLayersDirty = true;
348 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
350 return thesky->get_cloud_layer(index)->getCoverage();
354 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
356 return thesky->get_cloud_layer(index)->getVisibility_m();
360 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
362 thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
366 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
368 return thesky->get_cloud_layer(index)->getMaxAlpha();
372 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
374 thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
378 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
380 if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
381 SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
385 if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
388 thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
389 _cloudLayersDirty = true;
393 // end of environment-mgr.cxx