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 "fgclouds.hxx"
41 #include "precipitation_mgr.hxx"
48 FGEnvironmentMgr::FGEnvironmentMgr ()
49 : _environment(new FGEnvironment)
52 _controller = new FGInterpolateEnvironmentCtrl;
53 _controller->setEnvironment(_environment);
54 set_subsystem("controller", _controller, 0.1 );
56 fgClouds = new FGClouds();
58 _metarcontroller = new FGMetarCtrl(_controller );
59 set_subsystem("metarcontroller", _metarcontroller, 0.1 );
61 _metarfetcher = new FGMetarFetcher();
62 set_subsystem("metarfetcher", _metarfetcher, 1.0 );
64 _precipitationManager = new FGPrecipitationMgr;
65 set_subsystem("precipitation", _precipitationManager);
68 FGEnvironmentMgr::~FGEnvironmentMgr ()
70 remove_subsystem("precipitation");
71 delete _precipitationManager;
73 remove_subsystem("metarcontroller");
76 remove_subsystem("metarfetcher");
77 delete _metarcontroller;
81 remove_subsystem("controller");
88 FGEnvironmentMgr::init ()
90 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
91 SGSubsystemGroup::init();
96 FGEnvironmentMgr::reinit ()
98 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
99 SGSubsystemGroup::reinit();
104 FGEnvironmentMgr::bind ()
106 SGSubsystemGroup::bind();
107 fgTie("/environment/visibility-m", _environment,
108 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
109 fgSetArchivable("/environment/visibility-m");
110 fgTie("/environment/effective-visibility-m", thesky,
111 &SGSky::get_visibility );
112 fgTie("/environment/temperature-sea-level-degc", _environment,
113 &FGEnvironment::get_temperature_sea_level_degc,
114 &FGEnvironment::set_temperature_sea_level_degc);
115 fgSetArchivable("/environment/temperature-sea-level-degc");
116 fgTie("/environment/temperature-degc", _environment,
117 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
118 fgTie("/environment/temperature-degf", _environment,
119 &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
120 fgTie("/environment/dewpoint-sea-level-degc", _environment,
121 &FGEnvironment::get_dewpoint_sea_level_degc,
122 &FGEnvironment::set_dewpoint_sea_level_degc);
123 fgSetArchivable("/environment/dewpoint-sea-level-degc");
124 fgTie("/environment/dewpoint-degc", _environment,
125 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
126 fgTie("/environment/pressure-sea-level-inhg", _environment,
127 &FGEnvironment::get_pressure_sea_level_inhg,
128 &FGEnvironment::set_pressure_sea_level_inhg);
129 fgSetArchivable("/environment/pressure-sea-level-inhg");
130 fgTie("/environment/pressure-inhg", _environment,
131 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
132 fgTie("/environment/density-slugft3", _environment,
133 &FGEnvironment::get_density_slugft3); // read-only
134 fgTie("/environment/relative-humidity", _environment,
135 &FGEnvironment::get_relative_humidity); //ro
136 fgTie("/environment/atmosphere/density-tropo-avg", _environment,
137 &FGEnvironment::get_density_tropo_avg_kgm3); //ro
138 fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
139 &FGEnvironment::get_altitude_half_to_sun_m,
140 &FGEnvironment::set_altitude_half_to_sun_m);
141 fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
142 &FGEnvironment::get_altitude_tropo_top_m,
143 &FGEnvironment::set_altitude_tropo_top_m);
144 fgTie("/environment/wind-from-heading-deg", _environment,
145 &FGEnvironment::get_wind_from_heading_deg,
146 &FGEnvironment::set_wind_from_heading_deg);
147 fgTie("/environment/wind-speed-kt", _environment,
148 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
149 fgTie("/environment/wind-from-north-fps", _environment,
150 &FGEnvironment::get_wind_from_north_fps,
151 &FGEnvironment::set_wind_from_north_fps);
152 fgSetArchivable("/environment/wind-from-north-fps");
153 fgTie("/environment/wind-from-east-fps", _environment,
154 &FGEnvironment::get_wind_from_east_fps,
155 &FGEnvironment::set_wind_from_east_fps);
156 fgSetArchivable("/environment/wind-from-east-fps");
157 fgTie("/environment/wind-from-down-fps", _environment,
158 &FGEnvironment::get_wind_from_down_fps,
159 &FGEnvironment::set_wind_from_down_fps);
160 fgSetArchivable("/environment/wind-from-down-fps");
162 fgTie("/environment/thermal-lift-fps", _environment,
163 &FGEnvironment::get_thermal_lift_fps,
164 &FGEnvironment::set_thermal_lift_fps);
165 fgSetArchivable("/environment/thermal-lift-fps");
166 fgTie("/environment/ridge-lift-fps", _environment,
167 &FGEnvironment::get_ridge_lift_fps,
168 &FGEnvironment::set_ridge_lift_fps);
169 fgSetArchivable("/environment/ridge-lift-fps");
171 fgTie("/environment/local-weather-lift", _environment,
172 &FGEnvironment::get_local_weather_lift_fps); //read-only
174 fgTie("/environment/turbulence/magnitude-norm", _environment,
175 &FGEnvironment::get_turbulence_magnitude_norm,
176 &FGEnvironment::set_turbulence_magnitude_norm);
177 fgSetArchivable("/environment/turbulence/magnitude-norm");
178 fgTie("/environment/turbulence/rate-hz", _environment,
179 &FGEnvironment::get_turbulence_rate_hz,
180 &FGEnvironment::set_turbulence_rate_hz);
181 fgSetArchivable("/environment/turbulence/rate-hz");
183 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
185 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
187 &FGEnvironmentMgr::get_cloud_layer_span_m,
188 &FGEnvironmentMgr::set_cloud_layer_span_m);
189 fgSetArchivable(buf);
190 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
192 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
193 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
194 fgSetArchivable(buf);
195 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
197 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
198 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
199 fgSetArchivable(buf);
200 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
202 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
203 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
204 fgSetArchivable(buf);
205 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
207 &FGEnvironmentMgr::get_cloud_layer_coverage,
208 &FGEnvironmentMgr::set_cloud_layer_coverage);
209 fgSetArchivable(buf);
212 fgTie("/environment/metar/data", _metarcontroller,
213 &FGMetarCtrl::get_metar, &FGMetarCtrl::set_metar );
215 fgTie("/sim/rendering/clouds3d-enable", fgClouds,
216 &FGClouds::get_3dClouds,
217 &FGClouds::set_3dClouds);
218 fgTie("/sim/rendering/clouds3d-density", thesky,
219 &SGSky::get_3dCloudDensity,
220 &SGSky::set_3dCloudDensity);
221 fgTie("/sim/rendering/clouds3d-vis-range", thesky,
222 &SGSky::get_3dCloudVisRange,
223 &SGSky::set_3dCloudVisRange);
225 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
226 &SGEnviro::get_precipitation_enable_state,
227 &SGEnviro::set_precipitation_enable_state);
228 fgTie("/environment/rebuild-layers", fgClouds,
229 &FGClouds::get_update_event,
230 &FGClouds::set_update_event);
231 fgTie("/sim/rendering/lightning-enable", &sgEnviro,
232 &SGEnviro::get_lightning_enable_state,
233 &SGEnviro::set_lightning_enable_state);
234 fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
235 &SGEnviro::get_turbulence_enable_state,
236 &SGEnviro::set_turbulence_enable_state);
237 sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
241 FGEnvironmentMgr::unbind ()
243 fgUntie("/environment/visibility-m");
244 fgUntie("/environment/effective-visibility-m");
245 fgUntie("/environment/temperature-sea-level-degc");
246 fgUntie("/environment/temperature-degc");
247 fgUntie("/environment/dewpoint-sea-level-degc");
248 fgUntie("/environment/dewpoint-degc");
249 fgUntie("/environment/pressure-sea-level-inhg");
250 fgUntie("/environment/pressure-inhg");
251 fgUntie("/environment/density-inhg");
252 fgUntie("/environment/relative-humidity");
253 fgUntie("/environment/atmosphere/density-tropo-avg");
254 fgUntie("/environment/wind-from-north-fps");
255 fgUntie("/environment/wind-from-east-fps");
256 fgUntie("/environment/wind-from-down-fps");
258 fgUntie("/environment/thermal-lift-fps");
259 fgUntie("/environment/ridge-lift-fps");
260 fgUntie("/environment/local-weather-lift");
262 fgUntie("/environment/atmosphere/altitude-half-to-sun");
263 fgUntie("/environment/atmosphere/altitude-troposphere-top");
264 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
266 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
268 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
270 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
272 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
274 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
277 fgUntie("/sim/rendering/clouds3d-enable");
278 fgUntie("/sim/rendering/clouds3d-vis-range");
279 fgUntie("/sim/rendering/clouds3d-density");
280 fgUntie("/sim/rendering/precipitation-enable");
281 fgUntie("/environment/rebuild-layers");
282 fgUntie("/environment/weather-scenario");
283 fgUntie("/sim/rendering/lightning-enable");
284 fgUntie("/environment/turbulence/use-cloud-turbulence");
285 SGSubsystemGroup::unbind();
289 FGEnvironmentMgr::update (double dt)
291 SGSubsystemGroup::update(dt);
293 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
294 _environment->set_local_weather_lift_fps(fgGetDouble("/local-weather/current/thermal-lift"));
295 osg::Vec3 windVec(-_environment->get_wind_from_north_fps(),
296 -_environment->get_wind_from_east_fps(),
297 _environment->get_wind_from_down_fps());
298 // simgear::Particles::setWindVector(windVec * SG_FEET_TO_METER);
299 simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
300 _environment->get_wind_speed_kt() );
304 FGEnvironmentMgr::getEnvironment () const
306 return *_environment;
310 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
312 // Always returns the same environment
313 // for now; we'll make it interesting
315 FGEnvironment env = *_environment;
316 env.set_elevation_ft(alt);
321 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
323 // Always returns the same environment
324 // for now; we'll make it interesting
326 FGEnvironment env = *_environment;
327 env.set_elevation_ft(aPos.getElevationFt());
333 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
335 return thesky->get_cloud_layer(index)->getSpan_m();
339 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
341 thesky->get_cloud_layer(index)->setSpan_m(span_m);
345 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
347 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
351 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
353 FGEnvironment env = *_environment;
354 env.set_elevation_ft(elevation_ft);
356 thesky->get_cloud_layer(index)
357 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
359 thesky->get_cloud_layer(index)
360 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
362 thesky->get_cloud_layer(index)
363 ->setDirection(env.get_wind_from_heading_deg());
367 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
369 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
373 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
375 thesky->get_cloud_layer(index)
376 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
380 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
382 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
386 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
387 double transition_ft)
389 thesky->get_cloud_layer(index)
390 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
394 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
396 switch (thesky->get_cloud_layer(index)->getCoverage()) {
397 case SGCloudLayer::SG_CLOUD_OVERCAST:
399 case SGCloudLayer::SG_CLOUD_BROKEN:
401 case SGCloudLayer::SG_CLOUD_SCATTERED:
403 case SGCloudLayer::SG_CLOUD_FEW:
405 case SGCloudLayer::SG_CLOUD_CIRRUS:
407 case SGCloudLayer::SG_CLOUD_CLEAR:
415 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
416 const char * coverage_name)
418 SGCloudLayer::Coverage coverage;
419 if (!strcmp(coverage_name, "overcast"))
420 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
421 else if (!strcmp(coverage_name, "broken"))
422 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
423 else if (!strcmp(coverage_name, "scattered"))
424 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
425 else if (!strcmp(coverage_name, "few"))
426 coverage = SGCloudLayer::SG_CLOUD_FEW;
427 else if (!strcmp(coverage_name, "cirrus"))
428 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
429 else if (!strcmp(coverage_name, "clear"))
430 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
432 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
433 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
435 thesky->get_cloud_layer(index)->setCoverage(coverage);
440 // end of environment-mgr.cxx