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