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/debug/logstream.hxx>
28 #include <simgear/scene/sky/sky.hxx>
29 #include <simgear/environment/visual_enviro.hxx>
31 #include <Main/main.hxx>
32 #include <Main/fg_props.hxx>
33 #include <Aircraft/aircraft.hxx>
35 #include "environment.hxx"
36 #include "environment_ctrl.hxx"
37 #include "environment_mgr.hxx"
38 #include "fgclouds.hxx"
39 #include "precipitation_mgr.hxx"
46 FGEnvironmentMgr::FGEnvironmentMgr ()
47 : _environment(new FGEnvironment)
50 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
51 _controller = new FGMetarEnvironmentCtrl;
53 _controller = new FGInterpolateEnvironmentCtrl;
55 _controller->setEnvironment(_environment);
56 set_subsystem("controller", _controller, 0.5);
57 fgClouds = new FGClouds( _controller );
58 _precipitationManager = new FGPrecipitationMgr;
59 set_subsystem("precipitation", _precipitationManager);
62 FGEnvironmentMgr::~FGEnvironmentMgr ()
65 remove_subsystem("controller");
66 remove_subsystem("precipitation");
69 delete _precipitationManager;
73 FGEnvironmentMgr::init ()
75 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
76 SGSubsystemGroup::init();
81 FGEnvironmentMgr::reinit ()
83 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
84 SGSubsystemGroup::reinit();
89 FGEnvironmentMgr::bind ()
91 fgTie("/environment/visibility-m", _environment,
92 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
93 fgSetArchivable("/environment/visibility-m");
94 fgTie("/environment/effective-visibility-m", thesky,
95 &SGSky::get_visibility );
96 fgTie("/environment/temperature-sea-level-degc", _environment,
97 &FGEnvironment::get_temperature_sea_level_degc,
98 &FGEnvironment::set_temperature_sea_level_degc);
99 fgSetArchivable("/environment/temperature-sea-level-degc");
100 fgTie("/environment/temperature-degc", _environment,
101 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
102 fgTie("/environment/temperature-degf", _environment,
103 &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
104 fgTie("/environment/dewpoint-sea-level-degc", _environment,
105 &FGEnvironment::get_dewpoint_sea_level_degc,
106 &FGEnvironment::set_dewpoint_sea_level_degc);
107 fgSetArchivable("/environment/dewpoint-sea-level-degc");
108 fgTie("/environment/dewpoint-degc", _environment,
109 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
110 fgTie("/environment/pressure-sea-level-inhg", _environment,
111 &FGEnvironment::get_pressure_sea_level_inhg,
112 &FGEnvironment::set_pressure_sea_level_inhg);
113 fgSetArchivable("/environment/pressure-sea-level-inhg");
114 fgTie("/environment/pressure-inhg", _environment,
115 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
116 fgTie("/environment/density-slugft3", _environment,
117 &FGEnvironment::get_density_slugft3); // read-only
118 fgTie("/environment/relative-humidity", _environment,
119 &FGEnvironment::get_relative_humidity); //ro
120 fgTie("/environment/atmosphere/density-tropo-avg", _environment,
121 &FGEnvironment::get_density_tropo_avg_kgm3); //ro
122 fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
123 &FGEnvironment::get_altitude_half_to_sun_m,
124 &FGEnvironment::set_altitude_half_to_sun_m);
125 fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
126 &FGEnvironment::get_altitude_tropo_top_m,
127 &FGEnvironment::set_altitude_tropo_top_m);
128 fgTie("/environment/wind-from-heading-deg", _environment,
129 &FGEnvironment::get_wind_from_heading_deg,
130 &FGEnvironment::set_wind_from_heading_deg);
131 fgTie("/environment/wind-speed-kt", _environment,
132 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
133 fgTie("/environment/wind-from-north-fps", _environment,
134 &FGEnvironment::get_wind_from_north_fps,
135 &FGEnvironment::set_wind_from_north_fps);
136 fgSetArchivable("/environment/wind-from-north-fps");
137 fgTie("/environment/wind-from-east-fps", _environment,
138 &FGEnvironment::get_wind_from_east_fps,
139 &FGEnvironment::set_wind_from_east_fps);
140 fgSetArchivable("/environment/wind-from-east-fps");
141 fgTie("/environment/wind-from-down-fps", _environment,
142 &FGEnvironment::get_wind_from_down_fps,
143 &FGEnvironment::set_wind_from_down_fps);
144 fgSetArchivable("/environment/wind-from-down-fps");
145 fgTie("/environment/turbulence/magnitude-norm", _environment,
146 &FGEnvironment::get_turbulence_magnitude_norm,
147 &FGEnvironment::set_turbulence_magnitude_norm);
148 fgSetArchivable("/environment/turbulence/magnitude-norm");
149 fgTie("/environment/turbulence/rate-hz", _environment,
150 &FGEnvironment::get_turbulence_rate_hz,
151 &FGEnvironment::set_turbulence_rate_hz);
152 fgSetArchivable("/environment/turbulence/rate-hz");
154 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
156 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
158 &FGEnvironmentMgr::get_cloud_layer_span_m,
159 &FGEnvironmentMgr::set_cloud_layer_span_m);
160 fgSetArchivable(buf);
161 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
163 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
164 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
165 fgSetArchivable(buf);
166 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
168 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
169 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
170 fgSetArchivable(buf);
171 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
173 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
174 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
175 fgSetArchivable(buf);
176 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
178 &FGEnvironmentMgr::get_cloud_layer_coverage,
179 &FGEnvironmentMgr::set_cloud_layer_coverage);
180 fgSetArchivable(buf);
182 fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
183 &SGEnviro::get_clouds_enable_state,
184 &SGEnviro::set_clouds_enable_state);
185 fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
186 &SGEnviro::get_clouds_visibility,
187 &SGEnviro::set_clouds_visibility);
188 fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
189 &SGEnviro::get_clouds_density,
190 &SGEnviro::set_clouds_density);
191 fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
192 &SGEnviro::get_clouds_CacheSize,
193 &SGEnviro::set_clouds_CacheSize);
194 fgTie("/sim/rendering/clouds3d-cache-resolution", &sgEnviro,
195 &SGEnviro::get_CacheResolution,
196 &SGEnviro::set_CacheResolution);
197 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
198 &SGEnviro::get_precipitation_enable_state,
199 &SGEnviro::set_precipitation_enable_state);
200 fgTie("/environment/rebuild-layers", fgClouds,
201 &FGClouds::get_update_event,
202 &FGClouds::set_update_event);
203 fgTie("/sim/rendering/lightning-enable", &sgEnviro,
204 &SGEnviro::get_lightning_enable_state,
205 &SGEnviro::set_lightning_enable_state);
206 fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
207 &SGEnviro::get_turbulence_enable_state,
208 &SGEnviro::set_turbulence_enable_state);
209 sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
213 FGEnvironmentMgr::unbind ()
215 fgUntie("/environment/visibility-m");
216 fgUntie("/environment/effective-visibility-m");
217 fgUntie("/environment/temperature-sea-level-degc");
218 fgUntie("/environment/temperature-degc");
219 fgUntie("/environment/dewpoint-sea-level-degc");
220 fgUntie("/environment/dewpoint-degc");
221 fgUntie("/environment/pressure-sea-level-inhg");
222 fgUntie("/environment/pressure-inhg");
223 fgUntie("/environment/density-inhg");
224 fgUntie("/environment/relative-humidity");
225 fgUntie("/environment/atmosphere/density-tropo-avg");
226 fgUntie("/environment/wind-from-north-fps");
227 fgUntie("/environment/wind-from-east-fps");
228 fgUntie("/environment/wind-from-down-fps");
229 fgUntie("/environment/atmosphere/altitude-half-to-sun");
230 fgUntie("/environment/atmosphere/altitude-troposphere-top");
231 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
233 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
235 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
237 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
239 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
241 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
244 fgUntie("/sim/rendering/clouds3d-enable");
245 fgUntie("/sim/rendering/clouds3d-vis-range");
246 fgUntie("/sim/rendering/clouds3d-density");
247 fgUntie("/sim/rendering/clouds3d-cache-size");
248 fgUntie("/sim/rendering/clouds3d-cache-resolution");
249 fgUntie("/sim/rendering/precipitation-enable");
250 fgUntie("/environment/rebuild-layers");
251 fgUntie("/sim/rendering/lightning-enable");
252 fgUntie("/environment/turbulence/use-cloud-turbulence");
256 FGEnvironmentMgr::update (double dt)
258 SGSubsystemGroup::update(dt);
260 // FIXME: the FDMs should update themselves
261 current_aircraft.fdm_state
262 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
263 _environment->get_wind_from_east_fps(),
264 _environment->get_wind_from_down_fps());
265 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
271 FGEnvironmentMgr::getEnvironment () const
273 return *_environment;
277 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
279 // Always returns the same environment
280 // for now; we'll make it interesting
282 FGEnvironment env = *_environment;
283 env.set_elevation_ft(alt);
288 FGEnvironmentMgr::_update_fdm () const
291 // Pass atmosphere on to FDM
292 // FIXME: have FDMs read properties directly.
294 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
295 // convert from Rankine to Celsius
297 ->set_Static_temperature((9.0/5.0)
298 * (_environment->get_temperature_degc() + 273.15));
299 // convert from inHG to PSF
301 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
302 // keep in slugs/ft^3
304 ->set_Density(_environment->get_density_slugft3());
309 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
311 return thesky->get_cloud_layer(index)->getSpan_m();
315 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
317 thesky->get_cloud_layer(index)->setSpan_m(span_m);
321 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
323 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
327 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
329 FGEnvironment env = *_environment;
330 env.set_elevation_ft(elevation_ft);
332 thesky->get_cloud_layer(index)
333 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
335 thesky->get_cloud_layer(index)
336 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
338 thesky->get_cloud_layer(index)
339 ->setDirection(env.get_wind_from_heading_deg());
343 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
345 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
349 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
351 thesky->get_cloud_layer(index)
352 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
356 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
358 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
362 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
363 double transition_ft)
365 thesky->get_cloud_layer(index)
366 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
370 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
372 switch (thesky->get_cloud_layer(index)->getCoverage()) {
373 case SGCloudLayer::SG_CLOUD_OVERCAST:
375 case SGCloudLayer::SG_CLOUD_BROKEN:
377 case SGCloudLayer::SG_CLOUD_SCATTERED:
379 case SGCloudLayer::SG_CLOUD_FEW:
381 case SGCloudLayer::SG_CLOUD_CIRRUS:
383 case SGCloudLayer::SG_CLOUD_CLEAR:
391 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
392 const char * coverage_name)
394 SGCloudLayer::Coverage coverage;
395 if (!strcmp(coverage_name, "overcast"))
396 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
397 else if (!strcmp(coverage_name, "broken"))
398 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
399 else if (!strcmp(coverage_name, "scattered"))
400 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
401 else if (!strcmp(coverage_name, "few"))
402 coverage = SGCloudLayer::SG_CLOUD_FEW;
403 else if (!strcmp(coverage_name, "cirrus"))
404 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
405 else if (!strcmp(coverage_name, "clear"))
406 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
408 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
409 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
411 thesky->get_cloud_layer(index)->setCoverage(coverage);
416 // end of environment-mgr.cxx