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.
29 #include <simgear/constants.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/scene/sky/sky.hxx>
32 #include <simgear/environment/visual_enviro.hxx>
33 #include <simgear/scene/model/particles.hxx>
35 #include <Main/main.hxx>
36 #include <Main/fg_props.hxx>
37 #include <FDM/flight.hxx>
39 #include "environment.hxx"
40 #include "environment_mgr.hxx"
41 #include "environment_ctrl.hxx"
42 #include "fgclouds.hxx"
43 #include "precipitation_mgr.hxx"
50 FGEnvironmentMgr::FGEnvironmentMgr ()
51 : _environment(new FGEnvironment)
54 _controller = new FGInterpolateEnvironmentCtrl;
55 _controller->setEnvironment(_environment);
56 set_subsystem("controller", _controller, 0.1 );
58 fgClouds = new FGClouds();
60 _metarcontroller = new FGMetarCtrl(_controller );
61 set_subsystem("metarcontroller", _metarcontroller, 0.1 );
63 _metarfetcher = new FGMetarFetcher();
64 set_subsystem("metarfetcher", _metarfetcher, 1.0 );
66 _precipitationManager = new FGPrecipitationMgr;
67 set_subsystem("precipitation", _precipitationManager);
70 FGEnvironmentMgr::~FGEnvironmentMgr ()
72 remove_subsystem("precipitation");
73 delete _precipitationManager;
75 remove_subsystem("metarcontroller");
78 remove_subsystem("metarfetcher");
79 delete _metarcontroller;
83 remove_subsystem("controller");
90 FGEnvironmentMgr::init ()
92 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
93 SGSubsystemGroup::init();
98 FGEnvironmentMgr::reinit ()
100 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
101 SGSubsystemGroup::reinit();
106 FGEnvironmentMgr::bind ()
108 SGSubsystemGroup::bind();
109 fgTie("/environment/visibility-m", _environment,
110 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
111 fgSetArchivable("/environment/visibility-m");
112 fgTie("/environment/effective-visibility-m", thesky,
113 &SGSky::get_visibility );
114 fgTie("/environment/temperature-sea-level-degc", _environment,
115 &FGEnvironment::get_temperature_sea_level_degc,
116 &FGEnvironment::set_temperature_sea_level_degc);
117 fgSetArchivable("/environment/temperature-sea-level-degc");
118 fgTie("/environment/temperature-degc", _environment,
119 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
120 fgTie("/environment/temperature-degf", _environment,
121 &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
122 fgTie("/environment/dewpoint-sea-level-degc", _environment,
123 &FGEnvironment::get_dewpoint_sea_level_degc,
124 &FGEnvironment::set_dewpoint_sea_level_degc);
125 fgSetArchivable("/environment/dewpoint-sea-level-degc");
126 fgTie("/environment/dewpoint-degc", _environment,
127 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
128 fgTie("/environment/pressure-sea-level-inhg", _environment,
129 &FGEnvironment::get_pressure_sea_level_inhg,
130 &FGEnvironment::set_pressure_sea_level_inhg);
131 fgSetArchivable("/environment/pressure-sea-level-inhg");
132 fgTie("/environment/pressure-inhg", _environment,
133 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
134 fgTie("/environment/density-slugft3", _environment,
135 &FGEnvironment::get_density_slugft3); // read-only
136 fgTie("/environment/relative-humidity", _environment,
137 &FGEnvironment::get_relative_humidity); //ro
138 fgTie("/environment/atmosphere/density-tropo-avg", _environment,
139 &FGEnvironment::get_density_tropo_avg_kgm3); //ro
140 fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
141 &FGEnvironment::get_altitude_half_to_sun_m,
142 &FGEnvironment::set_altitude_half_to_sun_m);
143 fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
144 &FGEnvironment::get_altitude_tropo_top_m,
145 &FGEnvironment::set_altitude_tropo_top_m);
146 fgTie("/environment/wind-from-heading-deg", _environment,
147 &FGEnvironment::get_wind_from_heading_deg,
148 &FGEnvironment::set_wind_from_heading_deg);
149 fgTie("/environment/wind-speed-kt", _environment,
150 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
151 fgTie("/environment/wind-from-north-fps", _environment,
152 &FGEnvironment::get_wind_from_north_fps,
153 &FGEnvironment::set_wind_from_north_fps);
154 fgSetArchivable("/environment/wind-from-north-fps");
155 fgTie("/environment/wind-from-east-fps", _environment,
156 &FGEnvironment::get_wind_from_east_fps,
157 &FGEnvironment::set_wind_from_east_fps);
158 fgSetArchivable("/environment/wind-from-east-fps");
159 fgTie("/environment/wind-from-down-fps", _environment,
160 &FGEnvironment::get_wind_from_down_fps,
161 &FGEnvironment::set_wind_from_down_fps);
162 fgSetArchivable("/environment/wind-from-down-fps");
164 fgTie("/environment/thermal-lift-fps", _environment,
165 &FGEnvironment::get_thermal_lift_fps,
166 &FGEnvironment::set_thermal_lift_fps);
167 fgSetArchivable("/environment/thermal-lift-fps");
168 fgTie("/environment/ridge-lift-fps", _environment,
169 &FGEnvironment::get_ridge_lift_fps,
170 &FGEnvironment::set_ridge_lift_fps);
171 fgSetArchivable("/environment/ridge-lift-fps");
173 fgTie("/environment/local-weather-lift", _environment,
174 &FGEnvironment::get_local_weather_lift_fps); //read-only
176 fgTie("/environment/turbulence/magnitude-norm", _environment,
177 &FGEnvironment::get_turbulence_magnitude_norm,
178 &FGEnvironment::set_turbulence_magnitude_norm);
179 fgSetArchivable("/environment/turbulence/magnitude-norm");
180 fgTie("/environment/turbulence/rate-hz", _environment,
181 &FGEnvironment::get_turbulence_rate_hz,
182 &FGEnvironment::set_turbulence_rate_hz);
183 fgSetArchivable("/environment/turbulence/rate-hz");
185 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
187 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
189 &FGEnvironmentMgr::get_cloud_layer_span_m,
190 &FGEnvironmentMgr::set_cloud_layer_span_m);
191 fgSetArchivable(buf);
192 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
194 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
195 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
196 fgSetArchivable(buf);
197 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
199 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
200 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
201 fgSetArchivable(buf);
202 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
204 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
205 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
206 fgSetArchivable(buf);
207 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
209 &FGEnvironmentMgr::get_cloud_layer_coverage,
210 &FGEnvironmentMgr::set_cloud_layer_coverage);
211 fgSetArchivable(buf);
214 fgTie("/environment/metar/data", _metarcontroller,
215 &FGMetarCtrl::get_metar, &FGMetarCtrl::set_metar );
217 fgTie("/sim/rendering/clouds3d-enable", fgClouds,
218 &FGClouds::get_3dClouds,
219 &FGClouds::set_3dClouds);
220 fgTie("/sim/rendering/clouds3d-density", thesky,
221 &SGSky::get_3dCloudDensity,
222 &SGSky::set_3dCloudDensity);
223 fgTie("/sim/rendering/clouds3d-vis-range", thesky,
224 &SGSky::get_3dCloudVisRange,
225 &SGSky::set_3dCloudVisRange);
227 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
228 &SGEnviro::get_precipitation_enable_state,
229 &SGEnviro::set_precipitation_enable_state);
230 fgTie("/environment/rebuild-layers", fgClouds,
231 &FGClouds::get_update_event,
232 &FGClouds::set_update_event);
233 fgTie("/sim/rendering/lightning-enable", &sgEnviro,
234 &SGEnviro::get_lightning_enable_state,
235 &SGEnviro::set_lightning_enable_state);
236 fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
237 &SGEnviro::get_turbulence_enable_state,
238 &SGEnviro::set_turbulence_enable_state);
239 sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
243 FGEnvironmentMgr::unbind ()
245 fgUntie("/environment/visibility-m");
246 fgUntie("/environment/effective-visibility-m");
247 fgUntie("/environment/temperature-sea-level-degc");
248 fgUntie("/environment/temperature-degc");
249 fgUntie("/environment/dewpoint-sea-level-degc");
250 fgUntie("/environment/dewpoint-degc");
251 fgUntie("/environment/pressure-sea-level-inhg");
252 fgUntie("/environment/pressure-inhg");
253 fgUntie("/environment/density-inhg");
254 fgUntie("/environment/relative-humidity");
255 fgUntie("/environment/atmosphere/density-tropo-avg");
256 fgUntie("/environment/wind-from-north-fps");
257 fgUntie("/environment/wind-from-east-fps");
258 fgUntie("/environment/wind-from-down-fps");
260 fgUntie("/environment/thermal-lift-fps");
261 fgUntie("/environment/ridge-lift-fps");
262 fgUntie("/environment/local-weather-lift");
264 fgUntie("/environment/atmosphere/altitude-half-to-sun");
265 fgUntie("/environment/atmosphere/altitude-troposphere-top");
266 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
268 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
270 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
272 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
274 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
276 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
279 fgUntie("/sim/rendering/clouds3d-enable");
280 fgUntie("/sim/rendering/clouds3d-vis-range");
281 fgUntie("/sim/rendering/clouds3d-density");
282 fgUntie("/sim/rendering/precipitation-enable");
283 fgUntie("/environment/rebuild-layers");
284 fgUntie("/environment/weather-scenario");
285 fgUntie("/sim/rendering/lightning-enable");
286 fgUntie("/environment/turbulence/use-cloud-turbulence");
287 SGSubsystemGroup::unbind();
291 FGEnvironmentMgr::update (double dt)
293 SGSubsystemGroup::update(dt);
295 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
296 _environment->set_local_weather_lift_fps(fgGetDouble("/local-weather/current/thermal-lift"));
297 osg::Vec3 windVec(-_environment->get_wind_from_north_fps(),
298 -_environment->get_wind_from_east_fps(),
299 _environment->get_wind_from_down_fps());
300 // simgear::Particles::setWindVector(windVec * SG_FEET_TO_METER);
301 simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
302 _environment->get_wind_speed_kt() );
306 FGEnvironmentMgr::getEnvironment () const
308 return *_environment;
312 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
314 // Always returns the same environment
315 // for now; we'll make it interesting
317 FGEnvironment env = *_environment;
318 env.set_elevation_ft(alt);
323 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
325 // Always returns the same environment
326 // for now; we'll make it interesting
328 FGEnvironment env = *_environment;
329 env.set_elevation_ft(aPos.getElevationFt());
335 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
337 return thesky->get_cloud_layer(index)->getSpan_m();
341 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
343 thesky->get_cloud_layer(index)->setSpan_m(span_m);
347 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
349 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
353 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
355 FGEnvironment env = *_environment;
356 env.set_elevation_ft(elevation_ft);
358 thesky->get_cloud_layer(index)
359 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
361 thesky->get_cloud_layer(index)
362 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
364 thesky->get_cloud_layer(index)
365 ->setDirection(env.get_wind_from_heading_deg());
369 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
371 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
375 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
377 thesky->get_cloud_layer(index)
378 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
382 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
384 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
388 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
389 double transition_ft)
391 thesky->get_cloud_layer(index)
392 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
396 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
398 switch (thesky->get_cloud_layer(index)->getCoverage()) {
399 case SGCloudLayer::SG_CLOUD_OVERCAST:
401 case SGCloudLayer::SG_CLOUD_BROKEN:
403 case SGCloudLayer::SG_CLOUD_SCATTERED:
405 case SGCloudLayer::SG_CLOUD_FEW:
407 case SGCloudLayer::SG_CLOUD_CIRRUS:
409 case SGCloudLayer::SG_CLOUD_CLEAR:
417 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
418 const char * coverage_name)
420 SGCloudLayer::Coverage coverage;
421 if (!strcmp(coverage_name, "overcast"))
422 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
423 else if (!strcmp(coverage_name, "broken"))
424 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
425 else if (!strcmp(coverage_name, "scattered"))
426 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
427 else if (!strcmp(coverage_name, "few"))
428 coverage = SGCloudLayer::SG_CLOUD_FEW;
429 else if (!strcmp(coverage_name, "cirrus"))
430 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
431 else if (!strcmp(coverage_name, "clear"))
432 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
434 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
435 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
437 thesky->get_cloud_layer(index)->setCoverage(coverage);
442 // end of environment-mgr.cxx