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/sky/sky.hxx>
26 #include <Main/fg_props.hxx>
27 #include <Aircraft/aircraft.hxx>
29 #include "environment.hxx"
30 #include "environment_ctrl.hxx"
31 #include "environment_mgr.hxx"
33 extern SGSky *thesky; // FIXME: from main.cxx
36 FGEnvironmentMgr::FGEnvironmentMgr ()
37 : _environment(new FGEnvironment),
38 _controller(new FGUserDefEnvironmentCtrl)
42 FGEnvironmentMgr::~FGEnvironmentMgr ()
49 FGEnvironmentMgr::init ()
51 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
52 _controller->setEnvironment(_environment);
58 FGEnvironmentMgr::bind ()
60 fgTie("/environment/visibility-m", _environment,
61 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
62 fgSetArchivable("/environment/visibility-m");
63 fgTie("/environment/temperature-sea-level-degc", _environment,
64 &FGEnvironment::get_temperature_sea_level_degc,
65 &FGEnvironment::set_temperature_sea_level_degc);
66 fgSetArchivable("/environment/temperature-sea-level-degc");
67 fgTie("/environment/temperature-degc", _environment,
68 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
69 fgTie("/environment/dewpoint-sea-level-degc", _environment,
70 &FGEnvironment::get_dewpoint_sea_level_degc,
71 &FGEnvironment::set_dewpoint_sea_level_degc);
72 fgSetArchivable("/environment/dewpoint-sea-level-degc");
73 fgTie("/environment/dewpoint-degc", _environment,
74 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
75 fgTie("/environment/pressure-sea-level-inhg", _environment,
76 &FGEnvironment::get_pressure_sea_level_inhg,
77 &FGEnvironment::set_pressure_sea_level_inhg);
78 fgSetArchivable("/environment/pressure-sea-level-inhg");
79 fgTie("/environment/pressure-inhg", _environment,
80 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
81 fgTie("/environment/density-slugft3", _environment,
82 &FGEnvironment::get_density_slugft3); // read-only
83 fgTie("/environment/wind-from-heading-deg", _environment,
84 &FGEnvironment::get_wind_from_heading_deg,
85 &FGEnvironment::set_wind_from_heading_deg);
86 fgTie("/environment/wind-speed-kt", _environment,
87 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
88 fgTie("/environment/wind-from-north-fps", _environment,
89 &FGEnvironment::get_wind_from_north_fps,
90 &FGEnvironment::set_wind_from_north_fps);
91 fgSetArchivable("/environment/wind-from-north-fps");
92 fgTie("/environment/wind-from-east-fps", _environment,
93 &FGEnvironment::get_wind_from_east_fps,
94 &FGEnvironment::set_wind_from_east_fps);
95 fgSetArchivable("/environment/wind-from-east-fps");
96 fgTie("/environment/wind-from-down-fps", _environment,
97 &FGEnvironment::get_wind_from_down_fps,
98 &FGEnvironment::set_wind_from_down_fps);
99 fgSetArchivable("/environment/wind-from-down-fps");
101 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
103 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
105 &FGEnvironmentMgr::get_cloud_layer_span_m,
106 &FGEnvironmentMgr::set_cloud_layer_span_m);
107 fgSetArchivable(buf);
108 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
110 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
111 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
112 fgSetArchivable(buf);
113 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
115 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
116 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
117 fgSetArchivable(buf);
118 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
120 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
121 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
122 fgSetArchivable(buf);
123 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
125 &FGEnvironmentMgr::get_cloud_layer_type,
126 &FGEnvironmentMgr::set_cloud_layer_type);
127 fgSetArchivable(buf);
132 FGEnvironmentMgr::unbind ()
134 fgUntie("/environment/visibility-m");
135 fgUntie("/environment/temperature-sea-level-degc");
136 fgUntie("/environment/temperature-degc");
137 fgUntie("/environment/dewpoint-sea-level-degc");
138 fgUntie("/environment/dewpoint-degc");
139 fgUntie("/environment/pressure-sea-level-inhg");
140 fgUntie("/environment/pressure-inhg");
141 fgUntie("/environment/density-inhg");
142 fgUntie("/environment/wind-from-north-fps");
143 fgUntie("/environment/wind-from-east-fps");
144 fgUntie("/environment/wind-from-down-fps");
145 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
147 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
149 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
151 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
153 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
155 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
161 FGEnvironmentMgr::update (double dt)
163 _controller->update(dt);
164 // FIXME: the FDMs should update themselves
165 current_aircraft.fdm_state
166 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
167 _environment->get_wind_from_east_fps(),
168 _environment->get_wind_from_down_fps());
169 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
175 FGEnvironmentMgr::getEnvironment () const
177 return *_environment;
181 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
183 // Always returns the same environment
184 // for now; we'll make it interesting
186 FGEnvironment env = *_environment;
187 env.set_elevation_ft(alt);
192 FGEnvironmentMgr::_update_fdm () const
195 // Pass atmosphere on to FDM
196 // FIXME: have FDMs read properties directly.
198 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
199 // convert from Rankine to Celsius
201 ->set_Static_temperature((9.0/5.0)
202 * (_environment->get_temperature_degc() + 273.15));
203 // convert from inHG to PSF
205 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
206 // keep in slugs/ft^3
208 ->set_Density(_environment->get_density_slugft3());
213 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
215 return thesky->get_cloud_layer(index)->getSpan_m();
219 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
221 thesky->get_cloud_layer(index)->setSpan_m(span_m);
225 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
227 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
231 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
233 thesky->get_cloud_layer(index)
234 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
238 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
240 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
244 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
246 thesky->get_cloud_layer(index)
247 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
251 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
253 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
257 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
258 double transition_ft)
260 thesky->get_cloud_layer(index)
261 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
265 FGEnvironmentMgr::get_cloud_layer_type (int index) const
267 switch (thesky->get_cloud_layer(index)->getType()) {
268 case SGCloudLayer::SG_CLOUD_OVERCAST:
270 case SGCloudLayer::SG_CLOUD_MOSTLY_CLOUDY:
271 return "mostly-cloudy";
272 case SGCloudLayer::SG_CLOUD_MOSTLY_SUNNY:
273 return "mostly-sunny";
274 case SGCloudLayer::SG_CLOUD_CIRRUS:
276 case SGCloudLayer::SG_CLOUD_CLEAR:
284 FGEnvironmentMgr::set_cloud_layer_type (int index, const char * type_name)
286 SGCloudLayer::Type type;
287 if (!strcmp(type_name, "overcast"))
288 type = SGCloudLayer::SG_CLOUD_OVERCAST;
289 else if (!strcmp(type_name, "mostly-cloudy"))
290 type = SGCloudLayer::SG_CLOUD_MOSTLY_CLOUDY;
291 else if (!strcmp(type_name, "mostly-sunny"))
292 type = SGCloudLayer::SG_CLOUD_MOSTLY_SUNNY;
293 else if (!strcmp(type_name, "cirrus"))
294 type = SGCloudLayer::SG_CLOUD_CIRRUS;
295 else if (!strcmp(type_name, "clear"))
296 type = SGCloudLayer::SG_CLOUD_CLEAR;
298 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << type_name);
299 type = SGCloudLayer::SG_CLOUD_CLEAR;
301 thesky->get_cloud_layer(index)->setType(type);
306 // end of environment-mgr.cxx