]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Move initial setup of cloud layers to Main/main.cxx to ensure that
[flightgear.git] / src / Environment / environment_mgr.cxx
1 // environment-mgr.cxx -- manager for natural environment information.
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/sky/sky.hxx>
25
26 #include <Main/fg_props.hxx>
27 #include <Aircraft/aircraft.hxx>
28
29 #include "environment.hxx"
30 #include "environment_ctrl.hxx"
31 #include "environment_mgr.hxx"
32
33 extern SGSky *thesky;           // FIXME: from main.cxx
34
35
36 FGEnvironmentMgr::FGEnvironmentMgr ()
37   : _environment(new FGEnvironment),
38     _controller(new FGUserDefEnvironmentCtrl)
39 {
40 }
41
42 FGEnvironmentMgr::~FGEnvironmentMgr ()
43 {
44   delete _environment;
45   delete _controller;
46 }
47
48 void
49 FGEnvironmentMgr::init ()
50 {
51   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
52   _controller->setEnvironment(_environment);
53   _controller->init();
54   _update_fdm();
55 }
56
57 void
58 FGEnvironmentMgr::bind ()
59 {
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,
69         &FGEnvironment::set_temperature_degc);
70   fgSetArchivable("/environment/temperature-degc");
71   fgTie("/environment/pressure-sea-level-inhg", _environment,
72         &FGEnvironment::get_pressure_sea_level_inhg,
73         &FGEnvironment::set_pressure_sea_level_inhg);
74   fgSetArchivable("/environment/pressure-sea-level-inhg");
75   fgTie("/environment/pressure-inhg", _environment,
76         &FGEnvironment::get_pressure_inhg,
77         &FGEnvironment::set_pressure_inhg);
78   fgSetArchivable("/environment/pressure-inhg");
79   fgTie("/environment/density-sea-level-slugft3", _environment,
80         &FGEnvironment::get_density_sea_level_slugft3,
81         &FGEnvironment::set_density_sea_level_slugft3);
82   fgSetArchivable("/environment/density-sea-level-slugft3");
83   fgTie("/environment/density-slugft3", _environment,
84         &FGEnvironment::get_density_slugft3,
85         &FGEnvironment::set_density_slugft3);
86   fgSetArchivable("/environment/density-inhg");
87   fgTie("/environment/wind-from-heading-deg", _environment,
88         &FGEnvironment::get_wind_from_heading_deg,
89         &FGEnvironment::set_wind_from_heading_deg);
90   fgTie("/environment/wind-speed-kt", _environment,
91         &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
92   fgTie("/environment/wind-from-north-fps", _environment,
93         &FGEnvironment::get_wind_from_north_fps,
94         &FGEnvironment::set_wind_from_north_fps);
95   fgSetArchivable("/environment/wind-from-north-fps");
96   fgTie("/environment/wind-from-east-fps", _environment,
97         &FGEnvironment::get_wind_from_east_fps,
98         &FGEnvironment::set_wind_from_east_fps);
99   fgSetArchivable("/environment/wind-from-east-fps");
100   fgTie("/environment/wind-from-down-fps", _environment,
101         &FGEnvironment::get_wind_from_down_fps,
102         &FGEnvironment::set_wind_from_down_fps);
103   fgSetArchivable("/environment/wind-from-down-fps");
104
105   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
106     char buf[128];
107     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
108     fgTie(buf, this, i,
109           &FGEnvironmentMgr::get_cloud_layer_span_m,
110           &FGEnvironmentMgr::set_cloud_layer_span_m);
111     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
112     fgTie(buf, this, i,
113           &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
114           &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
115     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
116     fgTie(buf, this, i,
117           &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
118           &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
119     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
120     fgTie(buf, this, i,
121           &FGEnvironmentMgr::get_cloud_layer_transition_ft,
122           &FGEnvironmentMgr::set_cloud_layer_transition_ft);
123     sprintf(buf, "/environment/clouds/layer[%d]/type", i);
124     fgTie(buf, this, i,
125           &FGEnvironmentMgr::get_cloud_layer_type,
126           &FGEnvironmentMgr::set_cloud_layer_type);
127   }
128 }
129
130 void
131 FGEnvironmentMgr::unbind ()
132 {
133   fgUntie("/environment/visibility-m");
134   fgUntie("/environment/wind-from-heading-deg");
135   fgUntie("/environment/wind-speed-kt");
136   fgUntie("/environment/wind-from-north-fps");
137   fgUntie("/environment/wind-from-east-fps");
138   fgUntie("/environment/wind-from-down-fps");
139   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
140     char buf[128];
141     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
142     fgUntie(buf);
143     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
144     fgUntie(buf);
145     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
146     fgUntie(buf);
147     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
148     fgUntie(buf);
149     sprintf(buf, "/environment/clouds/layer[%d]/type", i);
150     fgUntie(buf);
151   }
152 }
153
154 void
155 FGEnvironmentMgr::update (double dt)
156 {
157   _controller->update(dt);
158                                 // FIXME: the FDMs should update themselves
159   current_aircraft.fdm_state
160     ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
161                                    _environment->get_wind_from_east_fps(),
162                                    _environment->get_wind_from_down_fps());
163   _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
164
165   _update_fdm();
166 }
167
168 FGEnvironment
169 FGEnvironmentMgr::getEnvironment () const
170 {
171   return *_environment;
172 }
173
174 FGEnvironment
175 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
176 {
177                                 // Always returns the same environment
178                                 // for now; we'll make it interesting
179                                 // later.
180   FGEnvironment env = *_environment;
181   env.set_elevation_ft(alt);
182   return env;
183 }
184
185 void
186 FGEnvironmentMgr::_update_fdm () const
187 {
188   //
189   // Pass atmosphere on to FDM
190   // FIXME: have FDMs read properties directly.
191   //
192   if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
193                                 // convert from Rankine to Celsius
194     cur_fdm_state
195       ->set_Static_temperature((9.0/5.0)
196                                * _environment->get_temperature_degc() + 492.0);
197                                 // convert from inHG to PSF
198     cur_fdm_state
199       ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
200                                 // keep in slugs/ft^3
201     cur_fdm_state
202       ->set_Density(_environment->get_density_slugft3());
203   }
204 }
205
206 double
207 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
208 {
209   return thesky->get_cloud_layer(index)->getSpan_m();
210 }
211
212 void
213 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
214 {
215   thesky->get_cloud_layer(index)->setSpan_m(span_m);
216 }
217
218 double
219 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
220 {
221   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
222 }
223
224 void
225 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
226 {
227   thesky->get_cloud_layer(index)
228     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
229 }
230
231 double
232 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
233 {
234   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
235 }
236
237 void
238 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
239 {
240   thesky->get_cloud_layer(index)
241     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
242 }
243
244 double
245 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
246 {
247   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
248 }
249
250 void
251 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
252                                                  double transition_ft)
253 {
254   thesky->get_cloud_layer(index)
255     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
256 }
257
258 const char *
259 FGEnvironmentMgr::get_cloud_layer_type (int index) const
260 {
261   switch (thesky->get_cloud_layer(index)->getType()) {
262   case SGCloudLayer::SG_CLOUD_OVERCAST:
263     return "overcast";
264   case SGCloudLayer::SG_CLOUD_MOSTLY_CLOUDY:
265     return "mostly-cloudy";
266   case SGCloudLayer::SG_CLOUD_MOSTLY_SUNNY:
267     return "mostly-sunny";
268   case SGCloudLayer::SG_CLOUD_CIRRUS:
269     return "cirrus";
270   case SGCloudLayer::SG_CLOUD_CLEAR:
271     return "clear";
272   default:
273     return "unknown";
274   }
275 }
276
277 void
278 FGEnvironmentMgr::set_cloud_layer_type (int index, const char * type_name)
279 {
280   SGCloudLayer::Type type;
281   if (!strcmp(type_name, "overcast"))
282     type = SGCloudLayer::SG_CLOUD_OVERCAST;
283   else if (!strcmp(type_name, "mostly-cloudy"))
284     type = SGCloudLayer::SG_CLOUD_MOSTLY_CLOUDY;
285   else if (!strcmp(type_name, "mostly-sunny"))
286     type = SGCloudLayer::SG_CLOUD_MOSTLY_SUNNY;
287   else if (!strcmp(type_name, "cirrus"))
288     type = SGCloudLayer::SG_CLOUD_CIRRUS;
289   else if (!strcmp(type_name, "clear"))
290     type = SGCloudLayer::SG_CLOUD_CLEAR;
291   else {
292     SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << type_name);
293     type = SGCloudLayer::SG_CLOUD_CLEAR;
294   }
295   thesky->get_cloud_layer(index)->setType(type);
296 }
297
298
299
300 // end of environment-mgr.cxx