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"
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);
54 FGEnvironmentMgr::~FGEnvironmentMgr ()
61 FGEnvironmentMgr::init ()
63 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
64 SGSubsystemGroup::init();
69 FGEnvironmentMgr::reinit ()
71 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
72 SGSubsystemGroup::reinit();
77 FGEnvironmentMgr::bind ()
79 fgTie("/environment/visibility-m", _environment,
80 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
81 fgSetArchivable("/environment/visibility-m");
82 fgTie("/environment/temperature-sea-level-degc", _environment,
83 &FGEnvironment::get_temperature_sea_level_degc,
84 &FGEnvironment::set_temperature_sea_level_degc);
85 fgSetArchivable("/environment/temperature-sea-level-degc");
86 fgTie("/environment/temperature-degc", _environment,
87 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
88 fgTie("/environment/dewpoint-sea-level-degc", _environment,
89 &FGEnvironment::get_dewpoint_sea_level_degc,
90 &FGEnvironment::set_dewpoint_sea_level_degc);
91 fgSetArchivable("/environment/dewpoint-sea-level-degc");
92 fgTie("/environment/dewpoint-degc", _environment,
93 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
94 fgTie("/environment/pressure-sea-level-inhg", _environment,
95 &FGEnvironment::get_pressure_sea_level_inhg,
96 &FGEnvironment::set_pressure_sea_level_inhg);
97 fgSetArchivable("/environment/pressure-sea-level-inhg");
98 fgTie("/environment/pressure-inhg", _environment,
99 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
100 fgTie("/environment/density-slugft3", _environment,
101 &FGEnvironment::get_density_slugft3); // read-only
102 fgTie("/environment/wind-from-heading-deg", _environment,
103 &FGEnvironment::get_wind_from_heading_deg,
104 &FGEnvironment::set_wind_from_heading_deg);
105 fgTie("/environment/wind-speed-kt", _environment,
106 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
107 fgTie("/environment/wind-from-north-fps", _environment,
108 &FGEnvironment::get_wind_from_north_fps,
109 &FGEnvironment::set_wind_from_north_fps);
110 fgSetArchivable("/environment/wind-from-north-fps");
111 fgTie("/environment/wind-from-east-fps", _environment,
112 &FGEnvironment::get_wind_from_east_fps,
113 &FGEnvironment::set_wind_from_east_fps);
114 fgSetArchivable("/environment/wind-from-east-fps");
115 fgTie("/environment/wind-from-down-fps", _environment,
116 &FGEnvironment::get_wind_from_down_fps,
117 &FGEnvironment::set_wind_from_down_fps);
118 fgSetArchivable("/environment/wind-from-down-fps");
119 fgTie("/environment/turbulence/magnitude-norm", _environment,
120 &FGEnvironment::get_turbulence_magnitude_norm,
121 &FGEnvironment::set_turbulence_magnitude_norm);
122 fgSetArchivable("/environment/turbulence/magnitude-norm");
123 fgTie("/environment/turbulence/rate-hz", _environment,
124 &FGEnvironment::get_turbulence_rate_hz,
125 &FGEnvironment::set_turbulence_rate_hz);
126 fgSetArchivable("/environment/turbulence/rate-hz");
128 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
130 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
132 &FGEnvironmentMgr::get_cloud_layer_span_m,
133 &FGEnvironmentMgr::set_cloud_layer_span_m);
134 fgSetArchivable(buf);
135 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
137 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
138 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
139 fgSetArchivable(buf);
140 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
142 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
143 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
144 fgSetArchivable(buf);
145 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
147 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
148 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
149 fgSetArchivable(buf);
150 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
152 &FGEnvironmentMgr::get_cloud_layer_coverage,
153 &FGEnvironmentMgr::set_cloud_layer_coverage);
154 fgSetArchivable(buf);
156 fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
157 &SGEnviro::get_clouds_enable_state,
158 &SGEnviro::set_clouds_enable_state);
159 fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
160 &SGEnviro::get_clouds_visibility,
161 &SGEnviro::set_clouds_visibility);
162 fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
163 &SGEnviro::get_clouds_density,
164 &SGEnviro::set_clouds_density);
165 fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
166 &SGEnviro::get_clouds_CacheSize,
167 &SGEnviro::set_clouds_CacheSize);
168 fgTie("/sim/rendering/clouds3d-cache-resolution", &sgEnviro,
169 &SGEnviro::get_CacheResolution,
170 &SGEnviro::set_CacheResolution);
171 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
172 &SGEnviro::get_precipitation_enable_state,
173 &SGEnviro::set_precipitation_enable_state);
177 FGEnvironmentMgr::unbind ()
179 fgUntie("/environment/visibility-m");
180 fgUntie("/environment/temperature-sea-level-degc");
181 fgUntie("/environment/temperature-degc");
182 fgUntie("/environment/dewpoint-sea-level-degc");
183 fgUntie("/environment/dewpoint-degc");
184 fgUntie("/environment/pressure-sea-level-inhg");
185 fgUntie("/environment/pressure-inhg");
186 fgUntie("/environment/density-inhg");
187 fgUntie("/environment/wind-from-north-fps");
188 fgUntie("/environment/wind-from-east-fps");
189 fgUntie("/environment/wind-from-down-fps");
190 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
192 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
194 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
196 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
198 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
200 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
206 FGEnvironmentMgr::update (double dt)
208 SGSubsystemGroup::update(dt);
210 // FIXME: the FDMs should update themselves
211 current_aircraft.fdm_state
212 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
213 _environment->get_wind_from_east_fps(),
214 _environment->get_wind_from_down_fps());
215 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
221 FGEnvironmentMgr::getEnvironment () const
223 return *_environment;
227 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
229 // Always returns the same environment
230 // for now; we'll make it interesting
232 FGEnvironment env = *_environment;
233 env.set_elevation_ft(alt);
238 FGEnvironmentMgr::_update_fdm () const
241 // Pass atmosphere on to FDM
242 // FIXME: have FDMs read properties directly.
244 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
245 // convert from Rankine to Celsius
247 ->set_Static_temperature((9.0/5.0)
248 * (_environment->get_temperature_degc() + 273.15));
249 // convert from inHG to PSF
251 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
252 // keep in slugs/ft^3
254 ->set_Density(_environment->get_density_slugft3());
259 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
261 return thesky->get_cloud_layer(index)->getSpan_m();
265 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
267 thesky->get_cloud_layer(index)->setSpan_m(span_m);
271 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
273 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
277 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
279 FGEnvironment env = *_environment;
280 env.set_elevation_ft(elevation_ft);
282 thesky->get_cloud_layer(index)
283 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
285 thesky->get_cloud_layer(index)
286 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
288 thesky->get_cloud_layer(index)
289 ->setDirection(env.get_wind_from_heading_deg());
293 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
295 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
299 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
301 thesky->get_cloud_layer(index)
302 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
306 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
308 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
312 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
313 double transition_ft)
315 thesky->get_cloud_layer(index)
316 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
320 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
322 switch (thesky->get_cloud_layer(index)->getCoverage()) {
323 case SGCloudLayer::SG_CLOUD_OVERCAST:
325 case SGCloudLayer::SG_CLOUD_BROKEN:
327 case SGCloudLayer::SG_CLOUD_SCATTERED:
329 case SGCloudLayer::SG_CLOUD_FEW:
331 case SGCloudLayer::SG_CLOUD_CIRRUS:
333 case SGCloudLayer::SG_CLOUD_CLEAR:
341 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
342 const char * coverage_name)
344 SGCloudLayer::Coverage coverage;
345 if (!strcmp(coverage_name, "overcast"))
346 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
347 else if (!strcmp(coverage_name, "broken"))
348 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
349 else if (!strcmp(coverage_name, "scattered"))
350 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
351 else if (!strcmp(coverage_name, "few"))
352 coverage = SGCloudLayer::SG_CLOUD_FEW;
353 else if (!strcmp(coverage_name, "cirrus"))
354 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
355 else if (!strcmp(coverage_name, "clear"))
356 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
358 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
359 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
361 thesky->get_cloud_layer(index)->setCoverage(coverage);
366 // end of environment-mgr.cxx