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>
34 #include <FDM/flight.hxx>
36 #include "environment.hxx"
37 #include "environment_ctrl.hxx"
38 #include "environment_mgr.hxx"
39 #include "fgclouds.hxx"
40 #include "precipitation_mgr.hxx"
47 FGEnvironmentMgr::FGEnvironmentMgr ()
48 : _environment(new FGEnvironment)
51 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
52 _controller = new FGMetarEnvironmentCtrl;
54 _controller = new FGInterpolateEnvironmentCtrl;
56 _controller->setEnvironment(_environment);
57 set_subsystem("controller", _controller, 0.5);
58 fgClouds = new FGClouds( _controller );
59 _precipitationManager = new FGPrecipitationMgr;
60 set_subsystem("precipitation", _precipitationManager);
63 FGEnvironmentMgr::~FGEnvironmentMgr ()
66 remove_subsystem("controller");
67 remove_subsystem("precipitation");
70 delete _precipitationManager;
74 FGEnvironmentMgr::init ()
76 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
77 SGSubsystemGroup::init();
82 FGEnvironmentMgr::reinit ()
84 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
85 SGSubsystemGroup::reinit();
90 FGEnvironmentMgr::bind ()
92 fgTie("/environment/visibility-m", _environment,
93 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
94 fgSetArchivable("/environment/visibility-m");
95 fgTie("/environment/effective-visibility-m", thesky,
96 &SGSky::get_visibility );
97 fgTie("/environment/temperature-sea-level-degc", _environment,
98 &FGEnvironment::get_temperature_sea_level_degc,
99 &FGEnvironment::set_temperature_sea_level_degc);
100 fgSetArchivable("/environment/temperature-sea-level-degc");
101 fgTie("/environment/temperature-degc", _environment,
102 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
103 fgTie("/environment/temperature-degf", _environment,
104 &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
105 fgTie("/environment/dewpoint-sea-level-degc", _environment,
106 &FGEnvironment::get_dewpoint_sea_level_degc,
107 &FGEnvironment::set_dewpoint_sea_level_degc);
108 fgSetArchivable("/environment/dewpoint-sea-level-degc");
109 fgTie("/environment/dewpoint-degc", _environment,
110 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
111 fgTie("/environment/pressure-sea-level-inhg", _environment,
112 &FGEnvironment::get_pressure_sea_level_inhg,
113 &FGEnvironment::set_pressure_sea_level_inhg);
114 fgSetArchivable("/environment/pressure-sea-level-inhg");
115 fgTie("/environment/pressure-inhg", _environment,
116 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
117 fgTie("/environment/density-slugft3", _environment,
118 &FGEnvironment::get_density_slugft3); // read-only
119 fgTie("/environment/relative-humidity", _environment,
120 &FGEnvironment::get_relative_humidity); //ro
121 fgTie("/environment/atmosphere/density-tropo-avg", _environment,
122 &FGEnvironment::get_density_tropo_avg_kgm3); //ro
123 fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
124 &FGEnvironment::get_altitude_half_to_sun_m,
125 &FGEnvironment::set_altitude_half_to_sun_m);
126 fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
127 &FGEnvironment::get_altitude_tropo_top_m,
128 &FGEnvironment::set_altitude_tropo_top_m);
129 fgTie("/environment/wind-from-heading-deg", _environment,
130 &FGEnvironment::get_wind_from_heading_deg,
131 &FGEnvironment::set_wind_from_heading_deg);
132 fgTie("/environment/wind-speed-kt", _environment,
133 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
134 fgTie("/environment/wind-from-north-fps", _environment,
135 &FGEnvironment::get_wind_from_north_fps,
136 &FGEnvironment::set_wind_from_north_fps);
137 fgSetArchivable("/environment/wind-from-north-fps");
138 fgTie("/environment/wind-from-east-fps", _environment,
139 &FGEnvironment::get_wind_from_east_fps,
140 &FGEnvironment::set_wind_from_east_fps);
141 fgSetArchivable("/environment/wind-from-east-fps");
142 fgTie("/environment/wind-from-down-fps", _environment,
143 &FGEnvironment::get_wind_from_down_fps,
144 &FGEnvironment::set_wind_from_down_fps);
145 fgSetArchivable("/environment/wind-from-down-fps");
146 fgTie("/environment/turbulence/magnitude-norm", _environment,
147 &FGEnvironment::get_turbulence_magnitude_norm,
148 &FGEnvironment::set_turbulence_magnitude_norm);
149 fgSetArchivable("/environment/turbulence/magnitude-norm");
150 fgTie("/environment/turbulence/rate-hz", _environment,
151 &FGEnvironment::get_turbulence_rate_hz,
152 &FGEnvironment::set_turbulence_rate_hz);
153 fgSetArchivable("/environment/turbulence/rate-hz");
155 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
157 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
159 &FGEnvironmentMgr::get_cloud_layer_span_m,
160 &FGEnvironmentMgr::set_cloud_layer_span_m);
161 fgSetArchivable(buf);
162 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
164 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
165 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
166 fgSetArchivable(buf);
167 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
169 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
170 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
171 fgSetArchivable(buf);
172 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
174 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
175 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
176 fgSetArchivable(buf);
177 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
179 &FGEnvironmentMgr::get_cloud_layer_coverage,
180 &FGEnvironmentMgr::set_cloud_layer_coverage);
181 fgSetArchivable(buf);
183 fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
184 &SGEnviro::get_clouds_enable_state,
185 &SGEnviro::set_clouds_enable_state);
186 fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
187 &SGEnviro::get_clouds_visibility,
188 &SGEnviro::set_clouds_visibility);
189 fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
190 &SGEnviro::get_clouds_density,
191 &SGEnviro::set_clouds_density);
192 fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
193 &SGEnviro::get_clouds_CacheSize,
194 &SGEnviro::set_clouds_CacheSize);
195 fgTie("/sim/rendering/clouds3d-cache-resolution", &sgEnviro,
196 &SGEnviro::get_CacheResolution,
197 &SGEnviro::set_CacheResolution);
198 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
199 &SGEnviro::get_precipitation_enable_state,
200 &SGEnviro::set_precipitation_enable_state);
201 fgTie("/environment/rebuild-layers", fgClouds,
202 &FGClouds::get_update_event,
203 &FGClouds::set_update_event);
204 fgTie("/sim/rendering/lightning-enable", &sgEnviro,
205 &SGEnviro::get_lightning_enable_state,
206 &SGEnviro::set_lightning_enable_state);
207 fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
208 &SGEnviro::get_turbulence_enable_state,
209 &SGEnviro::set_turbulence_enable_state);
210 sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
214 FGEnvironmentMgr::unbind ()
216 fgUntie("/environment/visibility-m");
217 fgUntie("/environment/effective-visibility-m");
218 fgUntie("/environment/temperature-sea-level-degc");
219 fgUntie("/environment/temperature-degc");
220 fgUntie("/environment/dewpoint-sea-level-degc");
221 fgUntie("/environment/dewpoint-degc");
222 fgUntie("/environment/pressure-sea-level-inhg");
223 fgUntie("/environment/pressure-inhg");
224 fgUntie("/environment/density-inhg");
225 fgUntie("/environment/relative-humidity");
226 fgUntie("/environment/atmosphere/density-tropo-avg");
227 fgUntie("/environment/wind-from-north-fps");
228 fgUntie("/environment/wind-from-east-fps");
229 fgUntie("/environment/wind-from-down-fps");
230 fgUntie("/environment/atmosphere/altitude-half-to-sun");
231 fgUntie("/environment/atmosphere/altitude-troposphere-top");
232 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
234 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
236 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
238 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
240 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
242 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
245 fgUntie("/sim/rendering/clouds3d-enable");
246 fgUntie("/sim/rendering/clouds3d-vis-range");
247 fgUntie("/sim/rendering/clouds3d-density");
248 fgUntie("/sim/rendering/clouds3d-cache-size");
249 fgUntie("/sim/rendering/clouds3d-cache-resolution");
250 fgUntie("/sim/rendering/precipitation-enable");
251 fgUntie("/environment/rebuild-layers");
252 fgUntie("/sim/rendering/lightning-enable");
253 fgUntie("/environment/turbulence/use-cloud-turbulence");
257 FGEnvironmentMgr::update (double dt)
259 SGSubsystemGroup::update(dt);
261 // FIXME: the FDMs should update themselves
262 current_aircraft.fdm_state
263 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
264 _environment->get_wind_from_east_fps(),
265 _environment->get_wind_from_down_fps());
266 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
272 FGEnvironmentMgr::getEnvironment () const
274 return *_environment;
278 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
280 // Always returns the same environment
281 // for now; we'll make it interesting
283 FGEnvironment env = *_environment;
284 env.set_elevation_ft(alt);
289 FGEnvironmentMgr::_update_fdm () const
292 // Pass atmosphere on to FDM
293 // FIXME: have FDMs read properties directly.
295 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
296 // convert from Rankine to Celsius
298 ->set_Static_temperature((9.0/5.0)
299 * (_environment->get_temperature_degc() + 273.15));
300 // convert from inHG to PSF
302 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
303 // keep in slugs/ft^3
305 ->set_Density(_environment->get_density_slugft3());
310 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
312 return thesky->get_cloud_layer(index)->getSpan_m();
316 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
318 thesky->get_cloud_layer(index)->setSpan_m(span_m);
322 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
324 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
328 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
330 FGEnvironment env = *_environment;
331 env.set_elevation_ft(elevation_ft);
333 thesky->get_cloud_layer(index)
334 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
336 thesky->get_cloud_layer(index)
337 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
339 thesky->get_cloud_layer(index)
340 ->setDirection(env.get_wind_from_heading_deg());
344 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
346 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
350 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
352 thesky->get_cloud_layer(index)
353 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
357 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
359 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
363 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
364 double transition_ft)
366 thesky->get_cloud_layer(index)
367 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
371 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
373 switch (thesky->get_cloud_layer(index)->getCoverage()) {
374 case SGCloudLayer::SG_CLOUD_OVERCAST:
376 case SGCloudLayer::SG_CLOUD_BROKEN:
378 case SGCloudLayer::SG_CLOUD_SCATTERED:
380 case SGCloudLayer::SG_CLOUD_FEW:
382 case SGCloudLayer::SG_CLOUD_CIRRUS:
384 case SGCloudLayer::SG_CLOUD_CLEAR:
392 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
393 const char * coverage_name)
395 SGCloudLayer::Coverage coverage;
396 if (!strcmp(coverage_name, "overcast"))
397 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
398 else if (!strcmp(coverage_name, "broken"))
399 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
400 else if (!strcmp(coverage_name, "scattered"))
401 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
402 else if (!strcmp(coverage_name, "few"))
403 coverage = SGCloudLayer::SG_CLOUD_FEW;
404 else if (!strcmp(coverage_name, "cirrus"))
405 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
406 else if (!strcmp(coverage_name, "clear"))
407 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
409 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
410 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
412 thesky->get_cloud_layer(index)->setCoverage(coverage);
417 // end of environment-mgr.cxx