]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Basics on catalog list model in the GUI.
[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 #include <simgear/structure/event_mgr.hxx>
32
33 #include <Main/main.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Viewer/renderer.hxx>
36 #include <FDM/flight.hxx>
37
38 #include "environment.hxx"
39 #include "environment_mgr.hxx"
40 #include "environment_ctrl.hxx"
41 #include "realwx_ctrl.hxx"
42 #include "fgclouds.hxx"
43 #include "precipitation_mgr.hxx"
44 #include "ridge_lift.hxx"
45 #include "terrainsampler.hxx"
46 #include "Airports/airport.hxx"
47 #include "gravity.hxx"
48 #include "magvarmanager.hxx"
49
50 class FG3DCloudsListener : public SGPropertyChangeListener {
51 public:
52   FG3DCloudsListener( FGClouds * fgClouds );
53   virtual ~FG3DCloudsListener();
54
55   virtual void valueChanged (SGPropertyNode * node);
56
57 private:
58   FGClouds * _fgClouds;
59   SGPropertyNode_ptr _enableNode;
60 };
61
62 FG3DCloudsListener::FG3DCloudsListener( FGClouds * fgClouds ) :
63     _fgClouds( fgClouds ) 
64 {
65   _enableNode = fgGetNode( "/sim/rendering/clouds3d-enable", true );
66   _enableNode->addChangeListener( this );
67
68   valueChanged( _enableNode );
69 }
70
71 FG3DCloudsListener::~FG3DCloudsListener()
72 {
73   _enableNode->removeChangeListener( this );
74 }
75
76 void FG3DCloudsListener::valueChanged( SGPropertyNode * node )
77 {
78   _fgClouds->set_3dClouds( _enableNode->getBoolValue() );
79 }
80
81 FGEnvironmentMgr::FGEnvironmentMgr () :
82   _environment(new FGEnvironment()),
83   fgClouds(new FGClouds()),
84   _cloudLayersDirty(true),
85   _3dCloudsEnableListener(new FG3DCloudsListener(fgClouds) ),
86   _sky(globals->get_renderer()->getSky())
87 {
88   set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
89   set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
90
91   set_subsystem("precipitation", new FGPrecipitationMgr);
92   set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
93   set_subsystem("ridgelift", new FGRidgeLift);
94   
95   set_subsystem("magvar", new FGMagVarManager);
96 }
97
98 FGEnvironmentMgr::~FGEnvironmentMgr ()
99 {
100   remove_subsystem( "ridgelift" );
101   remove_subsystem( "terrainsampler" );
102   remove_subsystem("precipitation");
103   remove_subsystem("realwx");
104   remove_subsystem("controller");
105   remove_subsystem("magvar");
106   
107   delete fgClouds;
108   delete _environment;
109
110   delete _3dCloudsEnableListener;
111 }
112
113 SGSubsystem::InitStatus FGEnvironmentMgr::incrementalInit()
114 {
115   
116   InitStatus r = SGSubsystemGroup::incrementalInit();
117   if (r == INIT_DONE) {
118     fgClouds->Init();
119     globals->get_event_mgr()->addTask("updateClosestAirport", this,
120                                       &FGEnvironmentMgr::updateClosestAirport, 30 );
121   }
122   
123   return r;
124 }
125
126 void
127 FGEnvironmentMgr::shutdown()
128 {
129   globals->get_event_mgr()->removeTask("updateClosestAirport");
130   SGSubsystemGroup::shutdown();
131 }
132
133 void
134 FGEnvironmentMgr::reinit ()
135 {
136   SG_LOG( SG_ENVIRONMENT, SG_INFO, "Reinitializing environment subsystem");
137   SGSubsystemGroup::reinit();
138 }
139
140 void
141 FGEnvironmentMgr::bind ()
142 {
143   SGSubsystemGroup::bind();
144   _environment->Tie( fgGetNode("/environment", true ) );
145
146   _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
147
148   _tiedProperties.Tie( "effective-visibility-m", _sky,
149           &SGSky::get_visibility );
150
151   _tiedProperties.Tie("rebuild-layers", fgClouds,
152           &FGClouds::get_update_event,
153           &FGClouds::set_update_event);
154
155 //  _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
156 //          &SGEnviro::get_turbulence_enable_state,
157 //          &SGEnviro::set_turbulence_enable_state);
158
159   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
160       SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
161
162       _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
163               &FGEnvironmentMgr::get_cloud_layer_span_m,
164               &FGEnvironmentMgr::set_cloud_layer_span_m);
165
166       _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
167               &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
168               &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
169
170       _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
171               &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
172               &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
173
174       _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
175               &FGEnvironmentMgr::get_cloud_layer_transition_ft,
176               &FGEnvironmentMgr::set_cloud_layer_transition_ft);
177
178       _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
179               &FGEnvironmentMgr::get_cloud_layer_coverage,
180               &FGEnvironmentMgr::set_cloud_layer_coverage);
181
182       _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
183               &FGEnvironmentMgr::get_cloud_layer_coverage_type,
184               &FGEnvironmentMgr::set_cloud_layer_coverage_type);
185
186       _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
187               &FGEnvironmentMgr::get_cloud_layer_visibility_m,
188               &FGEnvironmentMgr::set_cloud_layer_visibility_m);
189
190       _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
191               &FGEnvironmentMgr::get_cloud_layer_maxalpha,
192               &FGEnvironmentMgr::set_cloud_layer_maxalpha);
193   }
194
195   _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
196
197   _tiedProperties.Tie( "clouds3d-density", _sky,
198           &SGSky::get_3dCloudDensity,
199           &SGSky::set_3dCloudDensity);
200
201   _tiedProperties.Tie("clouds3d-vis-range", _sky,
202           &SGSky::get_3dCloudVisRange,
203           &SGSky::set_3dCloudVisRange);
204
205   _tiedProperties.Tie("clouds3d-impostor-range", _sky,
206           &SGSky::get_3dCloudImpostorDistance,
207           &SGSky::set_3dCloudImpostorDistance);
208
209   _tiedProperties.Tie("clouds3d-lod1-range", _sky,
210           &SGSky::get_3dCloudLoD1Range,
211           &SGSky::set_3dCloudLoD1Range);
212
213   _tiedProperties.Tie("clouds3d-lod2-range", _sky,
214           &SGSky::get_3dCloudLoD2Range,
215           &SGSky::set_3dCloudLoD2Range);
216
217   _tiedProperties.Tie("clouds3d-wrap", _sky,
218           &SGSky::get_3dCloudWrap,
219           &SGSky::set_3dCloudWrap);
220
221   _tiedProperties.Tie("clouds3d-use-impostors", _sky,
222           &SGSky::get_3dCloudUseImpostors,
223           &SGSky::set_3dCloudUseImpostors);
224
225 }
226
227 void
228 FGEnvironmentMgr::unbind ()
229 {
230   _tiedProperties.Untie();
231   _environment->Untie();
232   SGSubsystemGroup::unbind();
233 }
234
235 void
236 FGEnvironmentMgr::update (double dt)
237 {
238   SGSubsystemGroup::update(dt);
239
240     SGGeod aircraftPos(globals->get_aircraft_position());
241   _environment->set_elevation_ft( aircraftPos.getElevationFt() );
242
243   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
244                                    _environment->get_wind_speed_kt() );
245   if( _cloudLayersDirty ) {
246     _cloudLayersDirty = false;
247     fgClouds->set_update_event( fgClouds->get_update_event()+1 );
248   }
249
250
251   fgSetDouble( "/environment/gravitational-acceleration-mps2", 
252     Environment::Gravity::instance()->getGravity(aircraftPos));
253 }
254
255 void
256 FGEnvironmentMgr::updateClosestAirport()
257 {
258   SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "FGEnvironmentMgr::update: updating closest airport");
259   
260   SGGeod pos = globals->get_aircraft_position();
261   FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
262   if( nearestAirport == NULL )
263   {
264     SG_LOG(SG_ENVIRONMENT,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
265   }
266   else
267   {
268     const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
269     if (currentId != nearestAirport->ident())
270     {
271       SG_LOG(SG_ENVIRONMENT, SG_INFO, "FGEnvironmentMgr::updateClosestAirport: selected:" << nearestAirport->ident());
272       fgSetString("/sim/airport/closest-airport-id",
273                   nearestAirport->ident().c_str());
274     }
275   }
276
277 }
278
279 FGEnvironment
280 FGEnvironmentMgr::getEnvironment () const
281 {
282   return *_environment;
283 }
284
285 FGEnvironment
286 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
287 {
288   // Always returns the same environment
289   // for now; we'll make it interesting
290   // later.
291   FGEnvironment env = *_environment;
292   env.set_elevation_ft(alt);
293   return env;
294 }
295
296 FGEnvironment
297 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
298 {
299   // Always returns the same environment
300   // for now; we'll make it interesting
301   // later.
302   FGEnvironment env = *_environment;
303   env.set_elevation_ft(aPos.getElevationFt());
304   return env;
305
306 }
307
308 double
309 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
310 {
311   return _sky->get_cloud_layer(index)->getSpan_m();
312 }
313
314 void
315 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
316 {
317   _sky->get_cloud_layer(index)->setSpan_m(span_m);
318 }
319
320 double
321 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
322 {
323   return _sky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
324 }
325
326 void
327 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
328 {
329   FGEnvironment env = *_environment;
330   env.set_elevation_ft(elevation_ft);
331
332   _sky->get_cloud_layer(index)
333     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
334
335   _sky->get_cloud_layer(index)
336     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
337
338   _sky->get_cloud_layer(index)
339     ->setDirection(env.get_wind_from_heading_deg());
340 }
341
342 double
343 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
344 {
345   return _sky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
346 }
347
348 void
349 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
350 {
351   _sky->get_cloud_layer(index)
352     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
353 }
354
355 double
356 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
357 {
358   return _sky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
359 }
360
361 void
362 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
363                                                  double transition_ft)
364 {
365   _sky->get_cloud_layer(index)
366     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
367 }
368
369 const char *
370 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
371 {
372   return _sky->get_cloud_layer(index)->getCoverageString().c_str();
373 }
374
375 void
376 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
377                                             const char * coverage_name)
378 {
379   if( _sky->get_cloud_layer(index)->getCoverageString() == coverage_name )
380     return;
381
382   _sky->get_cloud_layer(index)->setCoverageString(coverage_name);
383   _cloudLayersDirty = true;
384 }
385
386 int
387 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
388 {
389   return _sky->get_cloud_layer(index)->getCoverage();
390 }
391
392 double
393 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
394 {
395     return _sky->get_cloud_layer(index)->getVisibility_m();
396 }
397
398 void
399 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
400 {
401     _sky->get_cloud_layer(index)->setVisibility_m(visibility_m);
402 }
403
404 double
405 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
406 {
407     return _sky->get_cloud_layer(index)->getMaxAlpha();
408 }
409
410 void
411 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
412 {
413     _sky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
414 }
415
416 void
417 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
418 {
419   if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
420     SG_LOG(SG_ENVIRONMENT,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
421     return;
422   }
423
424   if( static_cast<SGCloudLayer::Coverage>(type) == _sky->get_cloud_layer(index)->getCoverage() )
425     return;
426
427   _sky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
428   _cloudLayersDirty = true;
429 }
430
431
432 // end of environment-mgr.cxx