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 <Main/renderer.hxx>
30 #include <Aircraft/aircraft.hxx>
32 #include "environment.hxx"
33 #include "environment_ctrl.hxx"
34 #include "environment_mgr.hxx"
37 FGEnvironmentMgr::FGEnvironmentMgr ()
38 : _environment(new FGEnvironment)
41 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
42 _controller = new FGMetarEnvironmentCtrl;
44 _controller = new FGInterpolateEnvironmentCtrl;
46 _controller->setEnvironment(_environment);
47 set_subsystem("controller", _controller, 0.5);
50 FGEnvironmentMgr::~FGEnvironmentMgr ()
57 FGEnvironmentMgr::init ()
59 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
60 SGSubsystemGroup::init();
65 FGEnvironmentMgr::reinit ()
67 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
68 SGSubsystemGroup::reinit();
73 FGEnvironmentMgr::bind ()
75 fgTie("/environment/visibility-m", _environment,
76 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
77 fgSetArchivable("/environment/visibility-m");
78 fgTie("/environment/temperature-sea-level-degc", _environment,
79 &FGEnvironment::get_temperature_sea_level_degc,
80 &FGEnvironment::set_temperature_sea_level_degc);
81 fgSetArchivable("/environment/temperature-sea-level-degc");
82 fgTie("/environment/temperature-degc", _environment,
83 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
84 fgTie("/environment/dewpoint-sea-level-degc", _environment,
85 &FGEnvironment::get_dewpoint_sea_level_degc,
86 &FGEnvironment::set_dewpoint_sea_level_degc);
87 fgSetArchivable("/environment/dewpoint-sea-level-degc");
88 fgTie("/environment/dewpoint-degc", _environment,
89 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
90 fgTie("/environment/pressure-sea-level-inhg", _environment,
91 &FGEnvironment::get_pressure_sea_level_inhg,
92 &FGEnvironment::set_pressure_sea_level_inhg);
93 fgSetArchivable("/environment/pressure-sea-level-inhg");
94 fgTie("/environment/pressure-inhg", _environment,
95 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
96 fgTie("/environment/density-slugft3", _environment,
97 &FGEnvironment::get_density_slugft3); // read-only
98 fgTie("/environment/wind-from-heading-deg", _environment,
99 &FGEnvironment::get_wind_from_heading_deg,
100 &FGEnvironment::set_wind_from_heading_deg);
101 fgTie("/environment/wind-speed-kt", _environment,
102 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
103 fgTie("/environment/wind-from-north-fps", _environment,
104 &FGEnvironment::get_wind_from_north_fps,
105 &FGEnvironment::set_wind_from_north_fps);
106 fgSetArchivable("/environment/wind-from-north-fps");
107 fgTie("/environment/wind-from-east-fps", _environment,
108 &FGEnvironment::get_wind_from_east_fps,
109 &FGEnvironment::set_wind_from_east_fps);
110 fgSetArchivable("/environment/wind-from-east-fps");
111 fgTie("/environment/wind-from-down-fps", _environment,
112 &FGEnvironment::get_wind_from_down_fps,
113 &FGEnvironment::set_wind_from_down_fps);
114 fgSetArchivable("/environment/wind-from-down-fps");
115 fgTie("/environment/turbulence/magnitude-norm", _environment,
116 &FGEnvironment::get_turbulence_magnitude_norm,
117 &FGEnvironment::set_turbulence_magnitude_norm);
118 fgSetArchivable("/environment/turbulence/magnitude-norm");
119 fgTie("/environment/turbulence/rate-hz", _environment,
120 &FGEnvironment::get_turbulence_rate_hz,
121 &FGEnvironment::set_turbulence_rate_hz);
122 fgSetArchivable("/environment/turbulence/rate-hz");
124 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
126 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
128 &FGEnvironmentMgr::get_cloud_layer_span_m,
129 &FGEnvironmentMgr::set_cloud_layer_span_m);
130 fgSetArchivable(buf);
131 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
133 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
134 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
135 fgSetArchivable(buf);
136 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
138 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
139 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
140 fgSetArchivable(buf);
141 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
143 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
144 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
145 fgSetArchivable(buf);
146 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
148 &FGEnvironmentMgr::get_cloud_layer_coverage,
149 &FGEnvironmentMgr::set_cloud_layer_coverage);
150 fgSetArchivable(buf);
152 fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
153 &SGEnviro::get_clouds_enable_state,
154 &SGEnviro::set_clouds_enable_state);
155 fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
156 &SGEnviro::get_clouds_visibility,
157 &SGEnviro::set_clouds_visibility);
158 fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
159 &SGEnviro::get_clouds_density,
160 &SGEnviro::set_clouds_density);
161 fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
162 &SGEnviro::get_clouds_CacheSize,
163 &SGEnviro::set_clouds_CacheSize);
164 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
165 &SGEnviro::get_precipitation_enable_state,
166 &SGEnviro::set_precipitation_enable_state);
170 FGEnvironmentMgr::unbind ()
172 fgUntie("/environment/visibility-m");
173 fgUntie("/environment/temperature-sea-level-degc");
174 fgUntie("/environment/temperature-degc");
175 fgUntie("/environment/dewpoint-sea-level-degc");
176 fgUntie("/environment/dewpoint-degc");
177 fgUntie("/environment/pressure-sea-level-inhg");
178 fgUntie("/environment/pressure-inhg");
179 fgUntie("/environment/density-inhg");
180 fgUntie("/environment/wind-from-north-fps");
181 fgUntie("/environment/wind-from-east-fps");
182 fgUntie("/environment/wind-from-down-fps");
183 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
185 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
187 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
189 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
191 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
193 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
199 FGEnvironmentMgr::update (double dt)
201 SGSubsystemGroup::update(dt);
203 // FIXME: the FDMs should update themselves
204 current_aircraft.fdm_state
205 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
206 _environment->get_wind_from_east_fps(),
207 _environment->get_wind_from_down_fps());
208 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
214 FGEnvironmentMgr::getEnvironment () const
216 return *_environment;
220 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
222 // Always returns the same environment
223 // for now; we'll make it interesting
225 FGEnvironment env = *_environment;
226 env.set_elevation_ft(alt);
231 FGEnvironmentMgr::_update_fdm () const
234 // Pass atmosphere on to FDM
235 // FIXME: have FDMs read properties directly.
237 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
238 // convert from Rankine to Celsius
240 ->set_Static_temperature((9.0/5.0)
241 * (_environment->get_temperature_degc() + 273.15));
242 // convert from inHG to PSF
244 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
245 // keep in slugs/ft^3
247 ->set_Density(_environment->get_density_slugft3());
252 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
254 return thesky->get_cloud_layer(index)->getSpan_m();
258 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
260 thesky->get_cloud_layer(index)->setSpan_m(span_m);
264 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
266 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
270 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
272 FGEnvironment env = *_environment;
273 env.set_elevation_ft(elevation_ft);
275 thesky->get_cloud_layer(index)
276 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
278 thesky->get_cloud_layer(index)
279 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
281 thesky->get_cloud_layer(index)
282 ->setDirection(env.get_wind_from_heading_deg());
286 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
288 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
292 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
294 thesky->get_cloud_layer(index)
295 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
299 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
301 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
305 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
306 double transition_ft)
308 thesky->get_cloud_layer(index)
309 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
313 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
315 switch (thesky->get_cloud_layer(index)->getCoverage()) {
316 case SGCloudLayer::SG_CLOUD_OVERCAST:
318 case SGCloudLayer::SG_CLOUD_BROKEN:
320 case SGCloudLayer::SG_CLOUD_SCATTERED:
322 case SGCloudLayer::SG_CLOUD_FEW:
324 case SGCloudLayer::SG_CLOUD_CIRRUS:
326 case SGCloudLayer::SG_CLOUD_CLEAR:
334 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
335 const char * coverage_name)
337 SGCloudLayer::Coverage coverage;
338 if (!strcmp(coverage_name, "overcast"))
339 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
340 else if (!strcmp(coverage_name, "broken"))
341 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
342 else if (!strcmp(coverage_name, "scattered"))
343 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
344 else if (!strcmp(coverage_name, "few"))
345 coverage = SGCloudLayer::SG_CLOUD_FEW;
346 else if (!strcmp(coverage_name, "cirrus"))
347 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
348 else if (!strcmp(coverage_name, "clear"))
349 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
351 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
352 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
354 thesky->get_cloud_layer(index)->setCoverage(coverage);
359 // end of environment-mgr.cxx