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