]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
c6451eef1690f14b0727d6568273d9c7d6cb1fc9
[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/environment/visual_enviro.hxx>
31 #include <simgear/scene/model/particles.hxx>
32
33 #include <Main/main.hxx>
34 #include <Main/fg_props.hxx>
35 #include <FDM/flight.hxx>
36
37 #include "environment.hxx"
38 #include "environment_mgr.hxx"
39 #include "environment_ctrl.hxx"
40 #include "realwx_ctrl.hxx"
41 #include "fgclouds.hxx"
42 #include "precipitation_mgr.hxx"
43 #include "ridge_lift.hxx"
44 #include "terrainsampler.hxx"
45 #include "Airports/simple.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   _altitudeNode(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
103 void
104 FGEnvironmentMgr::reinit ()
105 {
106   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
107   SGSubsystemGroup::reinit();
108 }
109
110 void
111 FGEnvironmentMgr::bind ()
112 {
113   SGSubsystemGroup::bind();
114   _environment->Tie( fgGetNode("/environment", true ) );
115
116   _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
117
118   _tiedProperties.Tie( "effective-visibility-m", thesky,
119           &SGSky::get_visibility );
120
121   _tiedProperties.Tie("rebuild-layers", fgClouds,
122           &FGClouds::get_update_event,
123           &FGClouds::set_update_event);
124
125   _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
126           &SGEnviro::get_turbulence_enable_state,
127           &SGEnviro::set_turbulence_enable_state);
128
129   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
130       SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
131
132       _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
133               &FGEnvironmentMgr::get_cloud_layer_span_m,
134               &FGEnvironmentMgr::set_cloud_layer_span_m);
135
136       _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
137               &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
138               &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
139
140       _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
141               &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
142               &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
143
144       _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
145               &FGEnvironmentMgr::get_cloud_layer_transition_ft,
146               &FGEnvironmentMgr::set_cloud_layer_transition_ft);
147
148       _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
149               &FGEnvironmentMgr::get_cloud_layer_coverage,
150               &FGEnvironmentMgr::set_cloud_layer_coverage);
151
152       _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
153               &FGEnvironmentMgr::get_cloud_layer_coverage_type,
154               &FGEnvironmentMgr::set_cloud_layer_coverage_type);
155
156       _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
157               &FGEnvironmentMgr::get_cloud_layer_visibility_m,
158               &FGEnvironmentMgr::set_cloud_layer_visibility_m);
159
160       _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
161               &FGEnvironmentMgr::get_cloud_layer_maxalpha,
162               &FGEnvironmentMgr::set_cloud_layer_maxalpha);
163   }
164
165   _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
166
167   _tiedProperties.Tie( "clouds3d-enable", fgClouds,
168           &FGClouds::get_3dClouds,
169           &FGClouds::set_3dClouds);
170
171   _tiedProperties.Tie( "clouds3d-density", thesky,
172           &SGSky::get_3dCloudDensity,
173           &SGSky::set_3dCloudDensity);
174
175   _tiedProperties.Tie("clouds3d-vis-range", thesky,
176           &SGSky::get_3dCloudVisRange,
177           &SGSky::set_3dCloudVisRange);
178
179   _tiedProperties.Tie("precipitation-enable", &sgEnviro,
180           &SGEnviro::get_precipitation_enable_state,
181           &SGEnviro::set_precipitation_enable_state);
182
183   _tiedProperties.Tie("lightning-enable", &sgEnviro,
184           &SGEnviro::get_lightning_enable_state,
185           &SGEnviro::set_lightning_enable_state);
186
187   sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
188 }
189
190 void
191 FGEnvironmentMgr::unbind ()
192 {
193   _tiedProperties.Untie();
194   _environment->Untie();
195   SGSubsystemGroup::unbind();
196 }
197
198 void
199 FGEnvironmentMgr::update (double dt)
200 {
201   SGSubsystemGroup::update(dt);
202
203   _environment->set_elevation_ft( _altitudeNode->getDoubleValue() );
204
205   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
206                                    _environment->get_wind_speed_kt() );
207   if( _cloudLayersDirty ) {
208     _cloudLayersDirty = false;
209     fgClouds->set_update_event( fgClouds->get_update_event()+1 );
210   }
211
212   _positionTimeToLive -= dt;
213   if( _positionTimeToLive <= 0.0 )
214   {
215       // update closest airport information
216       _positionTimeToLive = 30.0;
217
218       SG_LOG(SG_ALL, SG_INFO, "FGEnvironmentMgr::update: updating closest airport");
219
220       SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(),
221                                    _latitude_n->getDoubleValue());
222
223       FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
224       if( nearestAirport == NULL )
225       {
226           SG_LOG(SG_ALL,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
227       }
228       else
229       {
230           const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
231           if (currentId != nearestAirport->ident())
232           {
233               fgSetString("/sim/airport/closest-airport-id",
234                       nearestAirport->ident().c_str());
235           }
236       }
237   }
238 }
239
240 FGEnvironment
241 FGEnvironmentMgr::getEnvironment () const
242 {
243   return *_environment;
244 }
245
246 FGEnvironment
247 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
248 {
249   // Always returns the same environment
250   // for now; we'll make it interesting
251   // later.
252   FGEnvironment env = *_environment;
253   env.set_elevation_ft(alt);
254   return env;
255 }
256
257 FGEnvironment
258 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) 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(aPos.getElevationFt());
265   return env;
266
267 }
268
269 double
270 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
271 {
272   return thesky->get_cloud_layer(index)->getSpan_m();
273 }
274
275 void
276 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
277 {
278   thesky->get_cloud_layer(index)->setSpan_m(span_m);
279 }
280
281 double
282 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
283 {
284   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
285 }
286
287 void
288 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
289 {
290   FGEnvironment env = *_environment;
291   env.set_elevation_ft(elevation_ft);
292
293   thesky->get_cloud_layer(index)
294     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
295
296   thesky->get_cloud_layer(index)
297     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
298
299   thesky->get_cloud_layer(index)
300     ->setDirection(env.get_wind_from_heading_deg());
301 }
302
303 double
304 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
305 {
306   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
307 }
308
309 void
310 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
311 {
312   thesky->get_cloud_layer(index)
313     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
314 }
315
316 double
317 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
318 {
319   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
320 }
321
322 void
323 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
324                                                  double transition_ft)
325 {
326   thesky->get_cloud_layer(index)
327     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
328 }
329
330 const char *
331 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
332 {
333   return thesky->get_cloud_layer(index)->getCoverageString().c_str();
334 }
335
336 void
337 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
338                                             const char * coverage_name)
339 {
340   if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
341     return;
342
343   thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
344   _cloudLayersDirty = true;
345 }
346
347 int
348 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
349 {
350   return thesky->get_cloud_layer(index)->getCoverage();
351 }
352
353 double
354 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
355 {
356     return thesky->get_cloud_layer(index)->getVisibility_m();
357 }
358
359 void
360 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
361 {
362     thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
363 }
364
365 double
366 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
367 {
368     return thesky->get_cloud_layer(index)->getMaxAlpha();
369 }
370
371 void
372 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
373 {
374     thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
375 }
376
377 void
378 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
379 {
380   if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
381     SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
382     return;
383   }
384
385   if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
386     return;
387
388   thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
389   _cloudLayersDirty = true;
390 }
391
392
393 // end of environment-mgr.cxx