]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Bertrand Coconnier: bug fix for #184, JSBSim: command line arguments
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <cstring>
26
27 #include <simgear/constants.h>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/scene/sky/sky.hxx>
30 #include <simgear/scene/model/particles.hxx>
31
32 #include <Main/main.hxx>
33 #include <Main/fg_props.hxx>
34 #include <FDM/flight.hxx>
35
36 #include "environment.hxx"
37 #include "environment_mgr.hxx"
38 #include "environment_ctrl.hxx"
39 #include "realwx_ctrl.hxx"
40 #include "fgclouds.hxx"
41 #include "precipitation_mgr.hxx"
42 #include "ridge_lift.hxx"
43 #include "terrainsampler.hxx"
44 #include "Airports/simple.hxx"
45
46 class SGSky;
47 extern SGSky *thesky;
48
49 FGEnvironmentMgr::FGEnvironmentMgr () :
50   _environment(new FGEnvironment()),
51   fgClouds(new FGClouds()),
52   _cloudLayersDirty(true),
53   _altitudeNode(fgGetNode("/position/altitude-ft", true)),
54   _longitude_n(fgGetNode( "/position/longitude-deg", true )),
55   _latitude_n( fgGetNode( "/position/latitude-deg", true )),
56   _positionTimeToLive(0.0)
57 {
58   set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
59   set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
60
61   set_subsystem("precipitation", new FGPrecipitationMgr);
62   set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
63   set_subsystem("ridgelift", new FGRidgeLift);
64 }
65
66 FGEnvironmentMgr::~FGEnvironmentMgr ()
67 {
68   SGSubsystem * subsys;
69
70   subsys = get_subsystem( "ridgelift" );
71   remove_subsystem( "ridgelift" );
72   delete subsys;
73
74   subsys = get_subsystem( "terrainsampler" );
75   remove_subsystem( "terrainsampler" );
76   delete subsys;
77
78   subsys = get_subsystem( "precipitation" );
79   remove_subsystem("precipitation");
80   delete subsys;
81
82   subsys = get_subsystem("realwx");
83   remove_subsystem("realwx");
84   delete subsys;
85
86   subsys = get_subsystem("controller");
87   remove_subsystem("controller");
88   delete subsys;
89
90   delete fgClouds;
91   delete _environment;
92 }
93
94 void
95 FGEnvironmentMgr::init ()
96 {
97   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
98   SGSubsystemGroup::init();
99   fgClouds->Init();
100
101   // Initialize the longitude, latitude and altitude to the initial position
102   // of the aircraft so that the atmospheric properties (pressure, temperature
103   // and density) can be initialized accordingly.
104   _altitudeNode->setDoubleValue(fgGetDouble("/sim/presets/altitude-ft"));
105   _longitude_n->setDoubleValue(fgGetDouble("/sim/presets/longitude-deg"));
106   _latitude_n->setDoubleValue(fgGetDouble("/sim/presets/latitude-deg"));
107 }
108
109 void
110 FGEnvironmentMgr::reinit ()
111 {
112   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
113   SGSubsystemGroup::reinit();
114 }
115
116 void
117 FGEnvironmentMgr::bind ()
118 {
119   SGSubsystemGroup::bind();
120   _environment->Tie( fgGetNode("/environment", true ) );
121
122   _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
123
124   _tiedProperties.Tie( "effective-visibility-m", thesky,
125           &SGSky::get_visibility );
126
127   _tiedProperties.Tie("rebuild-layers", fgClouds,
128           &FGClouds::get_update_event,
129           &FGClouds::set_update_event);
130
131 //  _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
132 //          &SGEnviro::get_turbulence_enable_state,
133 //          &SGEnviro::set_turbulence_enable_state);
134
135   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
136       SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
137
138       _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
139               &FGEnvironmentMgr::get_cloud_layer_span_m,
140               &FGEnvironmentMgr::set_cloud_layer_span_m);
141
142       _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
143               &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
144               &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
145
146       _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
147               &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
148               &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
149
150       _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
151               &FGEnvironmentMgr::get_cloud_layer_transition_ft,
152               &FGEnvironmentMgr::set_cloud_layer_transition_ft);
153
154       _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
155               &FGEnvironmentMgr::get_cloud_layer_coverage,
156               &FGEnvironmentMgr::set_cloud_layer_coverage);
157
158       _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
159               &FGEnvironmentMgr::get_cloud_layer_coverage_type,
160               &FGEnvironmentMgr::set_cloud_layer_coverage_type);
161
162       _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
163               &FGEnvironmentMgr::get_cloud_layer_visibility_m,
164               &FGEnvironmentMgr::set_cloud_layer_visibility_m);
165
166       _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
167               &FGEnvironmentMgr::get_cloud_layer_maxalpha,
168               &FGEnvironmentMgr::set_cloud_layer_maxalpha);
169   }
170
171   _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
172
173   _tiedProperties.Tie( "clouds3d-enable", fgClouds,
174           &FGClouds::get_3dClouds,
175           &FGClouds::set_3dClouds);
176
177   _tiedProperties.Tie( "clouds3d-density", thesky,
178           &SGSky::get_3dCloudDensity,
179           &SGSky::set_3dCloudDensity);
180
181   _tiedProperties.Tie("clouds3d-vis-range", thesky,
182           &SGSky::get_3dCloudVisRange,
183           &SGSky::set_3dCloudVisRange);
184
185 //  _tiedProperties.Tie("lightning-enable", &sgEnviro,
186 //          &SGEnviro::get_lightning_enable_state,
187 //          &SGEnviro::set_lightning_enable_state);
188
189 }
190
191 void
192 FGEnvironmentMgr::unbind ()
193 {
194   _tiedProperties.Untie();
195   _environment->Untie();
196   SGSubsystemGroup::unbind();
197 }
198
199 void
200 FGEnvironmentMgr::update (double dt)
201 {
202   SGSubsystemGroup::update(dt);
203
204   _environment->set_elevation_ft( _altitudeNode->getDoubleValue() );
205
206   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
207                                    _environment->get_wind_speed_kt() );
208   if( _cloudLayersDirty ) {
209     _cloudLayersDirty = false;
210     fgClouds->set_update_event( fgClouds->get_update_event()+1 );
211   }
212
213   _positionTimeToLive -= dt;
214   if( _positionTimeToLive <= 0.0 )
215   {
216       // update closest airport information
217       _positionTimeToLive = 30.0;
218
219       SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
220
221       SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
222                                    _latitude_n->getDoubleValue());
223
224       FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
225       if( nearestAirport == NULL )
226       {
227           SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
228       }
229       else
230       {
231           const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
232           if (currentId != nearestAirport->ident())
233           {
234               fgSetString("/sim/airport/closest-airport-id",
235                       nearestAirport->ident().c_str());
236           }
237       }
238   }
239 }
240
241 FGEnvironment
242 FGEnvironmentMgr::getEnvironment () const
243 {
244   return *_environment;
245 }
246
247 FGEnvironment
248 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
249 {
250   // Always returns the same environment
251   // for now; we'll make it interesting
252   // later.
253   FGEnvironment env = *_environment;
254   env.set_elevation_ft(alt);
255   return env;
256 }
257
258 FGEnvironment
259 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
260 {
261   // Always returns the same environment
262   // for now; we'll make it interesting
263   // later.
264   FGEnvironment env = *_environment;
265   env.set_elevation_ft(aPos.getElevationFt());
266   return env;
267
268 }
269
270 double
271 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
272 {
273   return thesky->get_cloud_layer(index)->getSpan_m();
274 }
275
276 void
277 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
278 {
279   thesky->get_cloud_layer(index)->setSpan_m(span_m);
280 }
281
282 double
283 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
284 {
285   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
286 }
287
288 void
289 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
290 {
291   FGEnvironment env = *_environment;
292   env.set_elevation_ft(elevation_ft);
293
294   thesky->get_cloud_layer(index)
295     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
296
297   thesky->get_cloud_layer(index)
298     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
299
300   thesky->get_cloud_layer(index)
301     ->setDirection(env.get_wind_from_heading_deg());
302 }
303
304 double
305 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
306 {
307   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
308 }
309
310 void
311 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
312 {
313   thesky->get_cloud_layer(index)
314     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
315 }
316
317 double
318 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
319 {
320   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
321 }
322
323 void
324 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
325                                                  double transition_ft)
326 {
327   thesky->get_cloud_layer(index)
328     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
329 }
330
331 const char *
332 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
333 {
334   return thesky->get_cloud_layer(index)->getCoverageString().c_str();
335 }
336
337 void
338 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
339                                             const char * coverage_name)
340 {
341   if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
342     return;
343
344   thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
345   _cloudLayersDirty = true;
346 }
347
348 int
349 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
350 {
351   return thesky->get_cloud_layer(index)->getCoverage();
352 }
353
354 double
355 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
356 {
357     return thesky->get_cloud_layer(index)->getVisibility_m();
358 }
359
360 void
361 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
362 {
363     thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
364 }
365
366 double
367 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
368 {
369     return thesky->get_cloud_layer(index)->getMaxAlpha();
370 }
371
372 void
373 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
374 {
375     thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
376 }
377
378 void
379 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
380 {
381   if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
382     SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
383     return;
384   }
385
386   if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
387     return;
388
389   thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
390   _cloudLayersDirty = true;
391 }
392
393
394 // end of environment-mgr.cxx