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"
49 FGEnvironmentMgr::FGEnvironmentMgr () :
50 _environment(new FGEnvironment()),
51 fgClouds(new FGClouds()),
52 _altitudeNode(fgGetNode("/position/altitude-ft", true)),
53 _cloudLayersDirty(true)
55 set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
56 set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
58 set_subsystem("precipitation", new FGPrecipitationMgr);
59 set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
60 set_subsystem("ridgelift", new FGRidgeLift);
63 FGEnvironmentMgr::~FGEnvironmentMgr ()
67 subsys = get_subsystem( "ridgelift" );
68 remove_subsystem( "ridgelift" );
71 subsys = get_subsystem( "terrainsampler" );
72 remove_subsystem( "terrainsampler" );
75 subsys = get_subsystem( "precipitation" );
76 remove_subsystem("precipitation");
79 subsys = get_subsystem("metarfetcher");
80 remove_subsystem("metarfetcher");
83 subsys = get_subsystem("metarcontroller");
84 remove_subsystem("metarcontroller");
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();
103 FGEnvironmentMgr::reinit ()
105 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
106 SGSubsystemGroup::reinit();
110 FGEnvironmentMgr::bind ()
112 SGSubsystemGroup::bind();
113 _environment->Tie( fgGetNode("/environment", true ) );
115 _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
117 _tiedProperties.Tie( "effective-visibility-m", thesky,
118 &SGSky::get_visibility );
120 _tiedProperties.Tie("rebuild-layers", fgClouds,
121 &FGClouds::get_update_event,
122 &FGClouds::set_update_event);
124 _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
125 &SGEnviro::get_turbulence_enable_state,
126 &SGEnviro::set_turbulence_enable_state);
128 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
129 SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
131 _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
132 &FGEnvironmentMgr::get_cloud_layer_span_m,
133 &FGEnvironmentMgr::set_cloud_layer_span_m);
135 _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
136 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
137 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
139 _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
140 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
141 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
143 _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
144 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
145 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
147 _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
148 &FGEnvironmentMgr::get_cloud_layer_coverage,
149 &FGEnvironmentMgr::set_cloud_layer_coverage);
151 _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
152 &FGEnvironmentMgr::get_cloud_layer_coverage_type,
153 &FGEnvironmentMgr::set_cloud_layer_coverage_type);
157 _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
159 _tiedProperties.Tie( "clouds3d-enable", fgClouds,
160 &FGClouds::get_3dClouds,
161 &FGClouds::set_3dClouds);
163 _tiedProperties.Tie( "clouds3d-density", thesky,
164 &SGSky::get_3dCloudDensity,
165 &SGSky::set_3dCloudDensity);
167 _tiedProperties.Tie("clouds3d-vis-range", thesky,
168 &SGSky::get_3dCloudVisRange,
169 &SGSky::set_3dCloudVisRange);
171 _tiedProperties.Tie("precipitation-enable", &sgEnviro,
172 &SGEnviro::get_precipitation_enable_state,
173 &SGEnviro::set_precipitation_enable_state);
175 _tiedProperties.Tie("lightning-enable", &sgEnviro,
176 &SGEnviro::get_lightning_enable_state,
177 &SGEnviro::set_lightning_enable_state);
179 sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
183 FGEnvironmentMgr::unbind ()
185 _tiedProperties.Untie();
186 _environment->Untie();
187 SGSubsystemGroup::unbind();
191 FGEnvironmentMgr::update (double dt)
193 SGSubsystemGroup::update(dt);
195 _environment->set_elevation_ft( _altitudeNode->getDoubleValue() );
197 simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
198 _environment->get_wind_speed_kt() );
199 if( _cloudLayersDirty ) {
200 _cloudLayersDirty = false;
201 fgClouds->set_update_event( fgClouds->get_update_event()+1 );
206 FGEnvironmentMgr::getEnvironment () const
208 return *_environment;
212 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
214 // Always returns the same environment
215 // for now; we'll make it interesting
217 FGEnvironment env = *_environment;
218 env.set_elevation_ft(alt);
223 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
225 // Always returns the same environment
226 // for now; we'll make it interesting
228 FGEnvironment env = *_environment;
229 env.set_elevation_ft(aPos.getElevationFt());
235 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
237 return thesky->get_cloud_layer(index)->getSpan_m();
241 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
243 thesky->get_cloud_layer(index)->setSpan_m(span_m);
247 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
249 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
253 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
255 FGEnvironment env = *_environment;
256 env.set_elevation_ft(elevation_ft);
258 thesky->get_cloud_layer(index)
259 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
261 thesky->get_cloud_layer(index)
262 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
264 thesky->get_cloud_layer(index)
265 ->setDirection(env.get_wind_from_heading_deg());
269 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
271 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
275 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
277 thesky->get_cloud_layer(index)
278 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
282 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
284 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
288 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
289 double transition_ft)
291 thesky->get_cloud_layer(index)
292 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
296 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
298 return thesky->get_cloud_layer(index)->getCoverageString().c_str();
302 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
303 const char * coverage_name)
305 if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
308 thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
309 _cloudLayersDirty = true;
313 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
315 return thesky->get_cloud_layer(index)->getCoverage();
320 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
322 if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
323 SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
327 if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
330 thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
331 _cloudLayersDirty = true;
335 // end of environment-mgr.cxx