]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
04208d3043fedfde8a4a3dc07f71fa5749c33258
[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
46 class SGSky;
47 extern SGSky *thesky;
48
49 FGEnvironmentMgr::FGEnvironmentMgr () :
50   _environment(new FGEnvironment()),
51   fgClouds(new FGClouds()),
52   _altitudeNode(fgGetNode("/position/altitude-ft", true)),
53   _cloudLayersDirty(true)
54 {
55   set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
56   set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
57
58   set_subsystem("precipitation", new FGPrecipitationMgr);
59   set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
60   set_subsystem("ridgelift", new FGRidgeLift);
61 }
62
63 FGEnvironmentMgr::~FGEnvironmentMgr ()
64 {
65   SGSubsystem * subsys;
66
67   subsys = get_subsystem( "ridgelift" );
68   remove_subsystem( "ridgelift" );
69   delete subsys;
70
71   subsys = get_subsystem( "terrainsampler" );
72   remove_subsystem( "terrainsampler" );
73   delete subsys;
74
75   subsys = get_subsystem( "precipitation" );
76   remove_subsystem("precipitation");
77   delete subsys;
78
79   subsys = get_subsystem("metarfetcher");
80   remove_subsystem("metarfetcher");
81   delete subsys;
82
83   subsys = get_subsystem("metarcontroller");
84   remove_subsystem("metarcontroller");
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
213 FGEnvironment
214 FGEnvironmentMgr::getEnvironment () const
215 {
216   return *_environment;
217 }
218
219 FGEnvironment
220 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
221 {
222   // Always returns the same environment
223   // for now; we'll make it interesting
224   // later.
225   FGEnvironment env = *_environment;
226   env.set_elevation_ft(alt);
227   return env;
228 }
229
230 FGEnvironment
231 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
232 {
233   // Always returns the same environment
234   // for now; we'll make it interesting
235   // later.
236   FGEnvironment env = *_environment;
237   env.set_elevation_ft(aPos.getElevationFt());
238   return env;
239
240 }
241
242 double
243 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
244 {
245   return thesky->get_cloud_layer(index)->getSpan_m();
246 }
247
248 void
249 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
250 {
251   thesky->get_cloud_layer(index)->setSpan_m(span_m);
252 }
253
254 double
255 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
256 {
257   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
258 }
259
260 void
261 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
262 {
263   FGEnvironment env = *_environment;
264   env.set_elevation_ft(elevation_ft);
265
266   thesky->get_cloud_layer(index)
267     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
268
269   thesky->get_cloud_layer(index)
270     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
271
272   thesky->get_cloud_layer(index)
273     ->setDirection(env.get_wind_from_heading_deg());
274 }
275
276 double
277 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
278 {
279   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
280 }
281
282 void
283 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
284 {
285   thesky->get_cloud_layer(index)
286     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
287 }
288
289 double
290 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
291 {
292   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
293 }
294
295 void
296 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
297                                                  double transition_ft)
298 {
299   thesky->get_cloud_layer(index)
300     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
301 }
302
303 const char *
304 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
305 {
306   return thesky->get_cloud_layer(index)->getCoverageString().c_str();
307 }
308
309 void
310 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
311                                             const char * coverage_name)
312 {
313   if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
314     return;
315
316   thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
317   _cloudLayersDirty = true;
318 }
319
320 int
321 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
322 {
323   return thesky->get_cloud_layer(index)->getCoverage();
324 }
325
326 double
327 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
328 {
329     return thesky->get_cloud_layer(index)->getVisibility_m();
330 }
331
332 void
333 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
334 {
335     thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
336 }
337
338 double
339 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
340 {
341     return thesky->get_cloud_layer(index)->getMaxAlpha();
342 }
343
344 void
345 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
346 {
347     thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
348 }
349
350
351
352
353 void
354 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
355 {
356   if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
357     SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
358     return;
359   }
360
361   if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
362     return;
363
364   thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
365   _cloudLayersDirty = true;
366 }
367
368
369 // end of environment-mgr.cxx