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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/scene/sky/sky.hxx>
25 #include <simgear/environment/visual_enviro.hxx>
27 #include <Main/main.hxx>
28 #include <Main/fg_props.hxx>
29 #include <Aircraft/aircraft.hxx>
31 #include "environment.hxx"
32 #include "environment_ctrl.hxx"
33 #include "environment_mgr.hxx"
34 #include "fgclouds.hxx"
41 FGEnvironmentMgr::FGEnvironmentMgr ()
42 : _environment(new FGEnvironment)
45 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
46 _controller = new FGMetarEnvironmentCtrl;
48 _controller = new FGInterpolateEnvironmentCtrl;
50 _controller->setEnvironment(_environment);
51 set_subsystem("controller", _controller, 0.5);
52 fgClouds = new FGClouds;
55 FGEnvironmentMgr::~FGEnvironmentMgr ()
63 FGEnvironmentMgr::init ()
65 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
66 SGSubsystemGroup::init();
71 FGEnvironmentMgr::reinit ()
73 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
74 SGSubsystemGroup::reinit();
79 FGEnvironmentMgr::bind ()
81 fgTie("/environment/visibility-m", _environment,
82 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
83 fgSetArchivable("/environment/visibility-m");
84 fgTie("/environment/temperature-sea-level-degc", _environment,
85 &FGEnvironment::get_temperature_sea_level_degc,
86 &FGEnvironment::set_temperature_sea_level_degc);
87 fgSetArchivable("/environment/temperature-sea-level-degc");
88 fgTie("/environment/temperature-degc", _environment,
89 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
90 fgTie("/environment/dewpoint-sea-level-degc", _environment,
91 &FGEnvironment::get_dewpoint_sea_level_degc,
92 &FGEnvironment::set_dewpoint_sea_level_degc);
93 fgSetArchivable("/environment/dewpoint-sea-level-degc");
94 fgTie("/environment/dewpoint-degc", _environment,
95 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
96 fgTie("/environment/pressure-sea-level-inhg", _environment,
97 &FGEnvironment::get_pressure_sea_level_inhg,
98 &FGEnvironment::set_pressure_sea_level_inhg);
99 fgSetArchivable("/environment/pressure-sea-level-inhg");
100 fgTie("/environment/pressure-inhg", _environment,
101 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
102 fgTie("/environment/density-slugft3", _environment,
103 &FGEnvironment::get_density_slugft3); // read-only
104 fgTie("/environment/wind-from-heading-deg", _environment,
105 &FGEnvironment::get_wind_from_heading_deg,
106 &FGEnvironment::set_wind_from_heading_deg);
107 fgTie("/environment/wind-speed-kt", _environment,
108 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
109 fgTie("/environment/wind-from-north-fps", _environment,
110 &FGEnvironment::get_wind_from_north_fps,
111 &FGEnvironment::set_wind_from_north_fps);
112 fgSetArchivable("/environment/wind-from-north-fps");
113 fgTie("/environment/wind-from-east-fps", _environment,
114 &FGEnvironment::get_wind_from_east_fps,
115 &FGEnvironment::set_wind_from_east_fps);
116 fgSetArchivable("/environment/wind-from-east-fps");
117 fgTie("/environment/wind-from-down-fps", _environment,
118 &FGEnvironment::get_wind_from_down_fps,
119 &FGEnvironment::set_wind_from_down_fps);
120 fgSetArchivable("/environment/wind-from-down-fps");
121 fgTie("/environment/turbulence/magnitude-norm", _environment,
122 &FGEnvironment::get_turbulence_magnitude_norm,
123 &FGEnvironment::set_turbulence_magnitude_norm);
124 fgSetArchivable("/environment/turbulence/magnitude-norm");
125 fgTie("/environment/turbulence/rate-hz", _environment,
126 &FGEnvironment::get_turbulence_rate_hz,
127 &FGEnvironment::set_turbulence_rate_hz);
128 fgSetArchivable("/environment/turbulence/rate-hz");
130 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
132 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
134 &FGEnvironmentMgr::get_cloud_layer_span_m,
135 &FGEnvironmentMgr::set_cloud_layer_span_m);
136 fgSetArchivable(buf);
137 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
139 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
140 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
141 fgSetArchivable(buf);
142 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
144 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
145 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
146 fgSetArchivable(buf);
147 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
149 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
150 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
151 fgSetArchivable(buf);
152 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
154 &FGEnvironmentMgr::get_cloud_layer_coverage,
155 &FGEnvironmentMgr::set_cloud_layer_coverage);
156 fgSetArchivable(buf);
158 fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
159 &SGEnviro::get_clouds_enable_state,
160 &SGEnviro::set_clouds_enable_state);
161 fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
162 &SGEnviro::get_clouds_visibility,
163 &SGEnviro::set_clouds_visibility);
164 fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
165 &SGEnviro::get_clouds_density,
166 &SGEnviro::set_clouds_density);
167 fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
168 &SGEnviro::get_clouds_CacheSize,
169 &SGEnviro::set_clouds_CacheSize);
170 fgTie("/sim/rendering/clouds3d-cache-resolution", &sgEnviro,
171 &SGEnviro::get_CacheResolution,
172 &SGEnviro::set_CacheResolution);
173 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
174 &SGEnviro::get_precipitation_enable_state,
175 &SGEnviro::set_precipitation_enable_state);
176 fgTie("/environment/rebuild_layers", fgClouds,
177 &FGClouds::get_update_event,
178 &FGClouds::set_update_event);
179 fgTie("/sim/rendering/lightning-enable", &sgEnviro,
180 &SGEnviro::get_lightning_enable_state,
181 &SGEnviro::set_lightning_enable_state);
182 fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
183 &SGEnviro::get_turbulence_enable_state,
184 &SGEnviro::set_turbulence_enable_state);
188 FGEnvironmentMgr::unbind ()
190 fgUntie("/environment/visibility-m");
191 fgUntie("/environment/temperature-sea-level-degc");
192 fgUntie("/environment/temperature-degc");
193 fgUntie("/environment/dewpoint-sea-level-degc");
194 fgUntie("/environment/dewpoint-degc");
195 fgUntie("/environment/pressure-sea-level-inhg");
196 fgUntie("/environment/pressure-inhg");
197 fgUntie("/environment/density-inhg");
198 fgUntie("/environment/wind-from-north-fps");
199 fgUntie("/environment/wind-from-east-fps");
200 fgUntie("/environment/wind-from-down-fps");
201 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
203 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
205 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
207 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
209 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
211 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
214 fgUntie("/sim/rendering/clouds3d-enable");
215 fgUntie("/sim/rendering/clouds3d-vis-range");
216 fgUntie("/sim/rendering/clouds3d-density");
217 fgUntie("/sim/rendering/clouds3d-cache-size");
218 fgUntie("/sim/rendering/clouds3d-cache-resolution");
219 fgUntie("/sim/rendering/precipitation-enable");
220 fgUntie("/environment/rebuild_layers");
221 fgUntie("/sim/rendering/lightning-enable");
222 fgUntie("/environment/turbulence/use-cloud-turbulence");
226 FGEnvironmentMgr::update (double dt)
228 SGSubsystemGroup::update(dt);
230 // FIXME: the FDMs should update themselves
231 current_aircraft.fdm_state
232 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
233 _environment->get_wind_from_east_fps(),
234 _environment->get_wind_from_down_fps());
235 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
241 FGEnvironmentMgr::getEnvironment () const
243 return *_environment;
247 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
249 // Always returns the same environment
250 // for now; we'll make it interesting
252 FGEnvironment env = *_environment;
253 env.set_elevation_ft(alt);
258 FGEnvironmentMgr::_update_fdm () const
261 // Pass atmosphere on to FDM
262 // FIXME: have FDMs read properties directly.
264 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
265 // convert from Rankine to Celsius
267 ->set_Static_temperature((9.0/5.0)
268 * (_environment->get_temperature_degc() + 273.15));
269 // convert from inHG to PSF
271 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
272 // keep in slugs/ft^3
274 ->set_Density(_environment->get_density_slugft3());
279 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
281 return thesky->get_cloud_layer(index)->getSpan_m();
285 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
287 thesky->get_cloud_layer(index)->setSpan_m(span_m);
291 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
293 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
297 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
299 FGEnvironment env = *_environment;
300 env.set_elevation_ft(elevation_ft);
302 thesky->get_cloud_layer(index)
303 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
305 thesky->get_cloud_layer(index)
306 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
308 thesky->get_cloud_layer(index)
309 ->setDirection(env.get_wind_from_heading_deg());
313 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
315 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
319 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
321 thesky->get_cloud_layer(index)
322 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
326 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
328 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
332 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
333 double transition_ft)
335 thesky->get_cloud_layer(index)
336 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
340 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
342 switch (thesky->get_cloud_layer(index)->getCoverage()) {
343 case SGCloudLayer::SG_CLOUD_OVERCAST:
345 case SGCloudLayer::SG_CLOUD_BROKEN:
347 case SGCloudLayer::SG_CLOUD_SCATTERED:
349 case SGCloudLayer::SG_CLOUD_FEW:
351 case SGCloudLayer::SG_CLOUD_CIRRUS:
353 case SGCloudLayer::SG_CLOUD_CLEAR:
361 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
362 const char * coverage_name)
364 SGCloudLayer::Coverage coverage;
365 if (!strcmp(coverage_name, "overcast"))
366 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
367 else if (!strcmp(coverage_name, "broken"))
368 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
369 else if (!strcmp(coverage_name, "scattered"))
370 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
371 else if (!strcmp(coverage_name, "few"))
372 coverage = SGCloudLayer::SG_CLOUD_FEW;
373 else if (!strcmp(coverage_name, "cirrus"))
374 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
375 else if (!strcmp(coverage_name, "clear"))
376 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
378 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
379 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
381 thesky->get_cloud_layer(index)->setCoverage(coverage);
386 // end of environment-mgr.cxx