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