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 <Aircraft/aircraft.hxx>
36 #include <FDM/flight.hxx>
38 #include "environment.hxx"
39 #include "environment_mgr.hxx"
40 #include "environment_ctrl.hxx"
41 #include "fgclouds.hxx"
42 #include "precipitation_mgr.hxx"
49 FGEnvironmentMgr::FGEnvironmentMgr ()
50 : _environment(new FGEnvironment)
53 _controller = new FGInterpolateEnvironmentCtrl;
54 _controller->setEnvironment(_environment);
55 set_subsystem("controller", _controller, 0.1 );
57 fgClouds = new FGClouds();
59 _metarcontroller = new FGMetarCtrl(_controller );
60 set_subsystem("metarcontroller", _metarcontroller, 0.1 );
62 _metarfetcher = new FGMetarFetcher();
63 set_subsystem("metarfetcher", _metarfetcher, 1.0 );
65 _precipitationManager = new FGPrecipitationMgr;
66 set_subsystem("precipitation", _precipitationManager);
69 FGEnvironmentMgr::~FGEnvironmentMgr ()
71 remove_subsystem("precipitation");
72 delete _precipitationManager;
74 remove_subsystem("metarcontroller");
77 remove_subsystem("metarfetcher");
78 delete _metarcontroller;
82 remove_subsystem("controller");
89 FGEnvironmentMgr::init ()
91 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
92 SGSubsystemGroup::init();
97 FGEnvironmentMgr::reinit ()
99 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
100 SGSubsystemGroup::reinit();
105 FGEnvironmentMgr::bind ()
107 SGSubsystemGroup::bind();
108 fgTie("/environment/visibility-m", _environment,
109 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
110 fgSetArchivable("/environment/visibility-m");
111 fgTie("/environment/effective-visibility-m", thesky,
112 &SGSky::get_visibility );
113 fgTie("/environment/temperature-sea-level-degc", _environment,
114 &FGEnvironment::get_temperature_sea_level_degc,
115 &FGEnvironment::set_temperature_sea_level_degc);
116 fgSetArchivable("/environment/temperature-sea-level-degc");
117 fgTie("/environment/temperature-degc", _environment,
118 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
119 fgTie("/environment/temperature-degf", _environment,
120 &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
121 fgTie("/environment/dewpoint-sea-level-degc", _environment,
122 &FGEnvironment::get_dewpoint_sea_level_degc,
123 &FGEnvironment::set_dewpoint_sea_level_degc);
124 fgSetArchivable("/environment/dewpoint-sea-level-degc");
125 fgTie("/environment/dewpoint-degc", _environment,
126 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
127 fgTie("/environment/pressure-sea-level-inhg", _environment,
128 &FGEnvironment::get_pressure_sea_level_inhg,
129 &FGEnvironment::set_pressure_sea_level_inhg);
130 fgSetArchivable("/environment/pressure-sea-level-inhg");
131 fgTie("/environment/pressure-inhg", _environment,
132 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
133 fgTie("/environment/density-slugft3", _environment,
134 &FGEnvironment::get_density_slugft3); // read-only
135 fgTie("/environment/relative-humidity", _environment,
136 &FGEnvironment::get_relative_humidity); //ro
137 fgTie("/environment/atmosphere/density-tropo-avg", _environment,
138 &FGEnvironment::get_density_tropo_avg_kgm3); //ro
139 fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
140 &FGEnvironment::get_altitude_half_to_sun_m,
141 &FGEnvironment::set_altitude_half_to_sun_m);
142 fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
143 &FGEnvironment::get_altitude_tropo_top_m,
144 &FGEnvironment::set_altitude_tropo_top_m);
145 fgTie("/environment/wind-from-heading-deg", _environment,
146 &FGEnvironment::get_wind_from_heading_deg,
147 &FGEnvironment::set_wind_from_heading_deg);
148 fgTie("/environment/wind-speed-kt", _environment,
149 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
150 fgTie("/environment/wind-from-north-fps", _environment,
151 &FGEnvironment::get_wind_from_north_fps,
152 &FGEnvironment::set_wind_from_north_fps);
153 fgSetArchivable("/environment/wind-from-north-fps");
154 fgTie("/environment/wind-from-east-fps", _environment,
155 &FGEnvironment::get_wind_from_east_fps,
156 &FGEnvironment::set_wind_from_east_fps);
157 fgSetArchivable("/environment/wind-from-east-fps");
158 fgTie("/environment/wind-from-down-fps", _environment,
159 &FGEnvironment::get_wind_from_down_fps,
160 &FGEnvironment::set_wind_from_down_fps);
161 fgSetArchivable("/environment/wind-from-down-fps");
163 fgTie("/environment/thermal-lift-fps", _environment,
164 &FGEnvironment::get_thermal_lift_fps,
165 &FGEnvironment::set_thermal_lift_fps);
166 fgSetArchivable("/environment/thermal-lift-fps");
167 fgTie("/environment/ridge-lift-fps", _environment,
168 &FGEnvironment::get_ridge_lift_fps,
169 &FGEnvironment::set_ridge_lift_fps);
170 fgSetArchivable("/environment/ridge-lift-fps");
172 fgTie("/environment/local-weather-lift", _environment,
173 &FGEnvironment::get_local_weather_lift_fps); //read-only
175 fgTie("/environment/turbulence/magnitude-norm", _environment,
176 &FGEnvironment::get_turbulence_magnitude_norm,
177 &FGEnvironment::set_turbulence_magnitude_norm);
178 fgSetArchivable("/environment/turbulence/magnitude-norm");
179 fgTie("/environment/turbulence/rate-hz", _environment,
180 &FGEnvironment::get_turbulence_rate_hz,
181 &FGEnvironment::set_turbulence_rate_hz);
182 fgSetArchivable("/environment/turbulence/rate-hz");
184 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
186 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
188 &FGEnvironmentMgr::get_cloud_layer_span_m,
189 &FGEnvironmentMgr::set_cloud_layer_span_m);
190 fgSetArchivable(buf);
191 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
193 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
194 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
195 fgSetArchivable(buf);
196 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
198 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
199 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
200 fgSetArchivable(buf);
201 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
203 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
204 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
205 fgSetArchivable(buf);
206 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
208 &FGEnvironmentMgr::get_cloud_layer_coverage,
209 &FGEnvironmentMgr::set_cloud_layer_coverage);
210 fgSetArchivable(buf);
213 fgTie("/environment/metar/data", _metarcontroller,
214 &FGMetarCtrl::get_metar, &FGMetarCtrl::set_metar );
216 fgTie("/sim/rendering/clouds3d-enable", fgClouds,
217 &FGClouds::get_3dClouds,
218 &FGClouds::set_3dClouds);
219 fgTie("/sim/rendering/clouds3d-density", thesky,
220 &SGSky::get_3dCloudDensity,
221 &SGSky::set_3dCloudDensity);
222 fgTie("/sim/rendering/clouds3d-vis-range", thesky,
223 &SGSky::get_3dCloudVisRange,
224 &SGSky::set_3dCloudVisRange);
226 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
227 &SGEnviro::get_precipitation_enable_state,
228 &SGEnviro::set_precipitation_enable_state);
229 fgTie("/environment/rebuild-layers", fgClouds,
230 &FGClouds::get_update_event,
231 &FGClouds::set_update_event);
232 fgTie("/sim/rendering/lightning-enable", &sgEnviro,
233 &SGEnviro::get_lightning_enable_state,
234 &SGEnviro::set_lightning_enable_state);
235 fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
236 &SGEnviro::get_turbulence_enable_state,
237 &SGEnviro::set_turbulence_enable_state);
238 sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
242 FGEnvironmentMgr::unbind ()
244 fgUntie("/environment/visibility-m");
245 fgUntie("/environment/effective-visibility-m");
246 fgUntie("/environment/temperature-sea-level-degc");
247 fgUntie("/environment/temperature-degc");
248 fgUntie("/environment/dewpoint-sea-level-degc");
249 fgUntie("/environment/dewpoint-degc");
250 fgUntie("/environment/pressure-sea-level-inhg");
251 fgUntie("/environment/pressure-inhg");
252 fgUntie("/environment/density-inhg");
253 fgUntie("/environment/relative-humidity");
254 fgUntie("/environment/atmosphere/density-tropo-avg");
255 fgUntie("/environment/wind-from-north-fps");
256 fgUntie("/environment/wind-from-east-fps");
257 fgUntie("/environment/wind-from-down-fps");
259 fgUntie("/environment/thermal-lift-fps");
260 fgUntie("/environment/ridge-lift-fps");
261 fgUntie("/environment/local-weather-lift");
263 fgUntie("/environment/atmosphere/altitude-half-to-sun");
264 fgUntie("/environment/atmosphere/altitude-troposphere-top");
265 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
267 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
269 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
271 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
273 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
275 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
278 fgUntie("/sim/rendering/clouds3d-enable");
279 fgUntie("/sim/rendering/clouds3d-vis-range");
280 fgUntie("/sim/rendering/clouds3d-density");
281 fgUntie("/sim/rendering/precipitation-enable");
282 fgUntie("/environment/rebuild-layers");
283 fgUntie("/environment/weather-scenario");
284 fgUntie("/sim/rendering/lightning-enable");
285 fgUntie("/environment/turbulence/use-cloud-turbulence");
286 SGSubsystemGroup::unbind();
290 FGEnvironmentMgr::update (double dt)
292 SGSubsystemGroup::update(dt);
294 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
295 _environment->set_local_weather_lift_fps(fgGetDouble("/local-weather/current/thermal-lift"));
296 osg::Vec3 windVec(-_environment->get_wind_from_north_fps(),
297 -_environment->get_wind_from_east_fps(),
298 _environment->get_wind_from_down_fps());
299 simgear::Particles::setWindVector(windVec * SG_FEET_TO_METER);
303 FGEnvironmentMgr::getEnvironment () const
305 return *_environment;
309 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
311 // Always returns the same environment
312 // for now; we'll make it interesting
314 FGEnvironment env = *_environment;
315 env.set_elevation_ft(alt);
320 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
322 // Always returns the same environment
323 // for now; we'll make it interesting
325 FGEnvironment env = *_environment;
326 env.set_elevation_ft(aPos.getElevationFt());
332 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
334 return thesky->get_cloud_layer(index)->getSpan_m();
338 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
340 thesky->get_cloud_layer(index)->setSpan_m(span_m);
344 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
346 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
350 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
352 FGEnvironment env = *_environment;
353 env.set_elevation_ft(elevation_ft);
355 thesky->get_cloud_layer(index)
356 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
358 thesky->get_cloud_layer(index)
359 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
361 thesky->get_cloud_layer(index)
362 ->setDirection(env.get_wind_from_heading_deg());
366 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
368 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
372 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
374 thesky->get_cloud_layer(index)
375 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
379 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
381 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
385 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
386 double transition_ft)
388 thesky->get_cloud_layer(index)
389 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
393 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
395 switch (thesky->get_cloud_layer(index)->getCoverage()) {
396 case SGCloudLayer::SG_CLOUD_OVERCAST:
398 case SGCloudLayer::SG_CLOUD_BROKEN:
400 case SGCloudLayer::SG_CLOUD_SCATTERED:
402 case SGCloudLayer::SG_CLOUD_FEW:
404 case SGCloudLayer::SG_CLOUD_CIRRUS:
406 case SGCloudLayer::SG_CLOUD_CLEAR:
414 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
415 const char * coverage_name)
417 SGCloudLayer::Coverage coverage;
418 if (!strcmp(coverage_name, "overcast"))
419 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
420 else if (!strcmp(coverage_name, "broken"))
421 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
422 else if (!strcmp(coverage_name, "scattered"))
423 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
424 else if (!strcmp(coverage_name, "few"))
425 coverage = SGCloudLayer::SG_CLOUD_FEW;
426 else if (!strcmp(coverage_name, "cirrus"))
427 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
428 else if (!strcmp(coverage_name, "clear"))
429 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
431 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
432 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
434 thesky->get_cloud_layer(index)->setCoverage(coverage);
439 // end of environment-mgr.cxx