]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Merge branch 'next' of git://gitorious.org/fg/flightgear into next
[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 }
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
160   }
161
162   _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
163
164   _tiedProperties.Tie( "clouds3d-enable", fgClouds,
165           &FGClouds::get_3dClouds,
166           &FGClouds::set_3dClouds);
167
168   _tiedProperties.Tie( "clouds3d-density", thesky,
169           &SGSky::get_3dCloudDensity,
170           &SGSky::set_3dCloudDensity);
171
172   _tiedProperties.Tie("clouds3d-vis-range", thesky,
173         &SGSky::get_3dCloudVisRange,
174         &SGSky::set_3dCloudVisRange);
175   
176   _tiedProperties.Tie("precipitation-enable", &sgEnviro,
177           &SGEnviro::get_precipitation_enable_state, 
178           &SGEnviro::set_precipitation_enable_state);
179
180   _tiedProperties.Tie("lightning-enable", &sgEnviro,
181       &SGEnviro::get_lightning_enable_state,
182       &SGEnviro::set_lightning_enable_state);
183
184   sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
185 }
186
187 void
188 FGEnvironmentMgr::unbind ()
189 {
190   _tiedProperties.Untie();
191   _environment->Untie();
192   SGSubsystemGroup::unbind();
193 }
194
195 void
196 FGEnvironmentMgr::update (double dt)
197 {
198   SGSubsystemGroup::update(dt);
199   
200   _environment->set_elevation_ft( _altitudeNode->getDoubleValue() );
201
202   simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
203                                    _environment->get_wind_speed_kt() );
204   if( _cloudLayersDirty ) {
205     _cloudLayersDirty = false;
206     fgClouds->set_update_event( fgClouds->get_update_event()+1 );
207   }
208 }
209
210 FGEnvironment
211 FGEnvironmentMgr::getEnvironment () const
212 {
213   return *_environment;
214 }
215
216 FGEnvironment
217 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
218 {
219   // Always returns the same environment
220   // for now; we'll make it interesting
221   // later.
222   FGEnvironment env = *_environment;
223   env.set_elevation_ft(alt);
224   return env;
225 }
226
227 FGEnvironment
228 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
229 {
230   // Always returns the same environment
231   // for now; we'll make it interesting
232   // later.
233   FGEnvironment env = *_environment;
234   env.set_elevation_ft(aPos.getElevationFt());
235   return env;
236
237 }
238
239 double
240 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
241 {
242   return thesky->get_cloud_layer(index)->getSpan_m();
243 }
244
245 void
246 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
247 {
248   thesky->get_cloud_layer(index)->setSpan_m(span_m);
249 }
250
251 double
252 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
253 {
254   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
255 }
256
257 void
258 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
259 {
260   FGEnvironment env = *_environment;
261   env.set_elevation_ft(elevation_ft);
262
263   thesky->get_cloud_layer(index)
264     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
265
266   thesky->get_cloud_layer(index)
267     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
268
269   thesky->get_cloud_layer(index)
270     ->setDirection(env.get_wind_from_heading_deg());
271 }
272
273 double
274 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
275 {
276   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
277 }
278
279 void
280 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
281 {
282   thesky->get_cloud_layer(index)
283     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
284 }
285
286 double
287 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
288 {
289   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
290 }
291
292 void
293 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
294                                                  double transition_ft)
295 {
296   thesky->get_cloud_layer(index)
297     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
298 }
299
300 const char *
301 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
302 {
303   return thesky->get_cloud_layer(index)->getCoverageString().c_str();
304 }
305
306 void
307 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
308                                             const char * coverage_name)
309 {
310   if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
311     return;
312
313   thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
314   _cloudLayersDirty = true;
315 }
316
317 int
318 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
319 {
320   return thesky->get_cloud_layer(index)->getCoverage();
321 }
322
323 double 
324 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
325 {
326     return thesky->get_cloud_layer(index)->getVisibility_m();
327 }
328
329 void 
330 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
331 {
332     thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
333 }
334
335
336
337 void 
338 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
339 {
340   if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
341     SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
342     return;
343   }
344
345   if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
346     return;
347
348   thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
349   _cloudLayersDirty = true;
350 }
351
352
353 // end of environment-mgr.cxx