]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment_mgr.cxx
Minor file mode issue.
[flightgear.git] / src / Environment / environment_mgr.cxx
index 6cae314fad06505fe1a3687fde335f347e9b5b82..3182f1159d24f95fe7579eb5077e5ff9e12cda20 100644 (file)
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-//
-// $Id$
 
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
+#include <cstring>
+
+#include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/scene/sky/sky.hxx>
 #include <simgear/environment/visual_enviro.hxx>
+#include <simgear/scene/model/particles.hxx>
 
 #include <Main/main.hxx>
 #include <Main/fg_props.hxx>
-#include <Aircraft/aircraft.hxx>
+#include <FDM/flight.hxx>
 
 #include "environment.hxx"
-#include "environment_ctrl.hxx"
 #include "environment_mgr.hxx"
+#include "environment_ctrl.hxx"
+#include "realwx_ctrl.hxx"
 #include "fgclouds.hxx"
+#include "precipitation_mgr.hxx"
+#include "ridge_lift.hxx"
+#include "terrainsampler.hxx"
 
 class SGSky;
 extern SGSky *thesky;
 
-
-
-FGEnvironmentMgr::FGEnvironmentMgr ()
-  : _environment(new FGEnvironment)
+FGEnvironmentMgr::FGEnvironmentMgr () :
+  _environment(new FGEnvironment()),
+  fgClouds(new FGClouds()),
+  _altitudeNode(fgGetNode("/position/altitude-ft", true)),
+  _cloudLayersDirty(true)
 {
+  set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
+  set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
 
-   if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
-       _controller = new FGMetarEnvironmentCtrl;
-   else
-       _controller = new FGInterpolateEnvironmentCtrl;
-
-  _controller->setEnvironment(_environment);
-  set_subsystem("controller", _controller, 0.5);
-  fgClouds = new FGClouds( _controller );
+  set_subsystem("precipitation", new FGPrecipitationMgr);
+  set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
+  set_subsystem("ridgelift", new FGRidgeLift);
 }
 
 FGEnvironmentMgr::~FGEnvironmentMgr ()
 {
-  delete _environment;
+  SGSubsystem * subsys;
+
+  subsys = get_subsystem( "ridgelift" );
+  remove_subsystem( "ridgelift" );
+  delete subsys;
+
+  subsys = get_subsystem( "terrainsampler" );
+  remove_subsystem( "terrainsampler" );
+  delete subsys;
+
+  subsys = get_subsystem( "precipitation" );
+  remove_subsystem("precipitation");
+  delete subsys;
+
+  subsys = get_subsystem("realwx");
+  remove_subsystem("realwx");
+  delete subsys;
+
+  subsys = get_subsystem("controller");
   remove_subsystem("controller");
-  delete _controller;
+  delete subsys;
+
   delete fgClouds;
+  delete _environment;
 }
 
 void
@@ -69,7 +93,6 @@ FGEnvironmentMgr::init ()
 {
   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
   SGSubsystemGroup::init();
-  _update_fdm();
 }
 
 void
@@ -77,186 +100,109 @@ FGEnvironmentMgr::reinit ()
 {
   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
   SGSubsystemGroup::reinit();
-  _update_fdm();
 }
 
 void
 FGEnvironmentMgr::bind ()
 {
-  fgTie("/environment/visibility-m", _environment,
-       &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
-  fgSetArchivable("/environment/visibility-m");
-  fgTie("/environment/temperature-sea-level-degc", _environment,
-       &FGEnvironment::get_temperature_sea_level_degc,
-       &FGEnvironment::set_temperature_sea_level_degc);
-  fgSetArchivable("/environment/temperature-sea-level-degc");
-  fgTie("/environment/temperature-degc", _environment,
-       &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
-  fgTie("/environment/temperature-degf", _environment,
-       &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
-  fgTie("/environment/dewpoint-sea-level-degc", _environment,
-       &FGEnvironment::get_dewpoint_sea_level_degc,
-       &FGEnvironment::set_dewpoint_sea_level_degc);
-  fgSetArchivable("/environment/dewpoint-sea-level-degc");
-  fgTie("/environment/dewpoint-degc", _environment,
-       &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
-  fgTie("/environment/pressure-sea-level-inhg", _environment,
-       &FGEnvironment::get_pressure_sea_level_inhg,
-       &FGEnvironment::set_pressure_sea_level_inhg);
-  fgSetArchivable("/environment/pressure-sea-level-inhg");
-  fgTie("/environment/pressure-inhg", _environment,
-       &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
-  fgTie("/environment/density-slugft3", _environment,
-       &FGEnvironment::get_density_slugft3); // read-only
-  fgTie("/environment/relative-humidity", _environment,
-       &FGEnvironment::get_relative_humidity); //ro
-  fgTie("/environment/atmosphere/density-tropo-avg", _environment,
-       &FGEnvironment::get_density_tropo_avg_kgm3); //ro
-  fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
-       &FGEnvironment::get_altitude_half_to_sun_m, 
-       &FGEnvironment::set_altitude_half_to_sun_m);
-  fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
-        &FGEnvironment::get_altitude_tropo_top_m,
-        &FGEnvironment::set_altitude_tropo_top_m);
-  fgTie("/environment/wind-from-heading-deg", _environment,
-       &FGEnvironment::get_wind_from_heading_deg,
-       &FGEnvironment::set_wind_from_heading_deg);
-  fgTie("/environment/wind-speed-kt", _environment,
-       &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
-  fgTie("/environment/wind-from-north-fps", _environment,
-       &FGEnvironment::get_wind_from_north_fps,
-       &FGEnvironment::set_wind_from_north_fps);
-  fgSetArchivable("/environment/wind-from-north-fps");
-  fgTie("/environment/wind-from-east-fps", _environment,
-       &FGEnvironment::get_wind_from_east_fps,
-       &FGEnvironment::set_wind_from_east_fps);
-  fgSetArchivable("/environment/wind-from-east-fps");
-  fgTie("/environment/wind-from-down-fps", _environment,
-       &FGEnvironment::get_wind_from_down_fps,
-       &FGEnvironment::set_wind_from_down_fps);
-  fgSetArchivable("/environment/wind-from-down-fps");
-  fgTie("/environment/turbulence/magnitude-norm", _environment,
-        &FGEnvironment::get_turbulence_magnitude_norm,
-        &FGEnvironment::set_turbulence_magnitude_norm);
-  fgSetArchivable("/environment/turbulence/magnitude-norm");
-  fgTie("/environment/turbulence/rate-hz", _environment,
-        &FGEnvironment::get_turbulence_rate_hz,
-        &FGEnvironment::set_turbulence_rate_hz);
-  fgSetArchivable("/environment/turbulence/rate-hz");
+  SGSubsystemGroup::bind();
+  _environment->Tie( fgGetNode("/environment", true ) );
+  
+  _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
+
+  _tiedProperties.Tie( "effective-visibility-m", thesky,
+       &SGSky::get_visibility );
+
+  _tiedProperties.Tie("rebuild-layers", fgClouds,
+      &FGClouds::get_update_event,
+      &FGClouds::set_update_event);
+
+  _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
+      &SGEnviro::get_turbulence_enable_state,
+      &SGEnviro::set_turbulence_enable_state);
 
   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
-    char buf[128];
-    sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
-    fgTie(buf, this, i,
+    SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
+
+    _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i, 
          &FGEnvironmentMgr::get_cloud_layer_span_m,
          &FGEnvironmentMgr::set_cloud_layer_span_m);
-    fgSetArchivable(buf);
-    sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
-    fgTie(buf, this, i,
+
+    _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i, 
          &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
          &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
-    fgSetArchivable(buf);
-    sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
-    fgTie(buf, this, i,
+
+    _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i, 
          &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
          &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
-    fgSetArchivable(buf);
-    sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
-    fgTie(buf, this, i,
+
+    _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i, 
          &FGEnvironmentMgr::get_cloud_layer_transition_ft,
          &FGEnvironmentMgr::set_cloud_layer_transition_ft);
-    fgSetArchivable(buf);
-    sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
-    fgTie(buf, this, i,
+
+    _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i, 
          &FGEnvironmentMgr::get_cloud_layer_coverage,
          &FGEnvironmentMgr::set_cloud_layer_coverage);
-    fgSetArchivable(buf);
+
+    _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i, 
+         &FGEnvironmentMgr::get_cloud_layer_coverage_type,
+         &FGEnvironmentMgr::set_cloud_layer_coverage_type);
+
+    _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
+        &FGEnvironmentMgr::get_cloud_layer_visibility_m,
+        &FGEnvironmentMgr::set_cloud_layer_visibility_m);
+
+    _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
+        &FGEnvironmentMgr::get_cloud_layer_maxalpha,
+        &FGEnvironmentMgr::set_cloud_layer_maxalpha);
   }
-  fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
-         &SGEnviro::get_clouds_enable_state,
-         &SGEnviro::set_clouds_enable_state);
-  fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
-         &SGEnviro::get_clouds_visibility,
-         &SGEnviro::set_clouds_visibility);
-  fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
-         &SGEnviro::get_clouds_density,
-         &SGEnviro::set_clouds_density);
-  fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
-         &SGEnviro::get_clouds_CacheSize,
-         &SGEnviro::set_clouds_CacheSize);
-  fgTie("/sim/rendering/clouds3d-cache-resolution", &sgEnviro,
-         &SGEnviro::get_CacheResolution,
-         &SGEnviro::set_CacheResolution);
-  fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
-         &SGEnviro::get_precipitation_enable_state,
+
+  _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
+
+  _tiedProperties.Tie( "clouds3d-enable", fgClouds,
+         &FGClouds::get_3dClouds,
+         &FGClouds::set_3dClouds);
+
+  _tiedProperties.Tie( "clouds3d-density", thesky,
+         &SGSky::get_3dCloudDensity,
+         &SGSky::set_3dCloudDensity);
+
+  _tiedProperties.Tie("clouds3d-vis-range", thesky,
+        &SGSky::get_3dCloudVisRange,
+        &SGSky::set_3dCloudVisRange);
+  
+  _tiedProperties.Tie("precipitation-enable", &sgEnviro,
+         &SGEnviro::get_precipitation_enable_state, 
          &SGEnviro::set_precipitation_enable_state);
-  fgTie("/environment/rebuild_layers", fgClouds,
-      &FGClouds::get_update_event,
-      &FGClouds::set_update_event);
-  fgTie("/sim/rendering/lightning-enable", &sgEnviro,
+
+  _tiedProperties.Tie("lightning-enable", &sgEnviro,
       &SGEnviro::get_lightning_enable_state,
       &SGEnviro::set_lightning_enable_state);
-  fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
-      &SGEnviro::get_turbulence_enable_state,
-      &SGEnviro::set_turbulence_enable_state);
+
   sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
 }
 
 void
 FGEnvironmentMgr::unbind ()
 {
-  fgUntie("/environment/visibility-m");
-  fgUntie("/environment/temperature-sea-level-degc");
-  fgUntie("/environment/temperature-degc");
-  fgUntie("/environment/dewpoint-sea-level-degc");
-  fgUntie("/environment/dewpoint-degc");
-  fgUntie("/environment/pressure-sea-level-inhg");
-  fgUntie("/environment/pressure-inhg");
-  fgUntie("/environment/density-inhg");
-  fgUntie("/environment/relative_humidity");
-  fgUntie("/environment/atmosphere/density_tropo_avg");
-  fgUntie("/environment/wind-from-north-fps");
-  fgUntie("/environment/wind-from-east-fps");
-  fgUntie("/environment/wind-from-down-fps");
-  fgUntie("/environment/atmosphere/altitude_half_to_sun");
-  fgUntie("/environment/atmosphere/altitude_troposphere_top");
-  for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
-    char buf[128];
-    sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
-    fgUntie(buf);
-    sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
-    fgUntie(buf);
-    sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
-    fgUntie(buf);
-    sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
-    fgUntie(buf);
-    sprintf(buf, "/environment/clouds/layer[%d]/type", i);
-    fgUntie(buf);
-  }
-  fgUntie("/sim/rendering/clouds3d-enable");
-  fgUntie("/sim/rendering/clouds3d-vis-range");
-  fgUntie("/sim/rendering/clouds3d-density");
-  fgUntie("/sim/rendering/clouds3d-cache-size");
-  fgUntie("/sim/rendering/clouds3d-cache-resolution");
-  fgUntie("/sim/rendering/precipitation-enable");
-  fgUntie("/environment/rebuild_layers");
-  fgUntie("/sim/rendering/lightning-enable");
-  fgUntie("/environment/turbulence/use-cloud-turbulence");
+  _tiedProperties.Untie();
+  _environment->Untie();
+  SGSubsystemGroup::unbind();
 }
 
 void
 FGEnvironmentMgr::update (double dt)
 {
   SGSubsystemGroup::update(dt);
-
-                               // FIXME: the FDMs should update themselves
-  current_aircraft.fdm_state
-    ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
-                                  _environment->get_wind_from_east_fps(),
-                                  _environment->get_wind_from_down_fps());
-  _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
-
-  _update_fdm();
+  
+  _environment->set_elevation_ft( _altitudeNode->getDoubleValue() );
+
+  simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
+                                  _environment->get_wind_speed_kt() );
+  if( _cloudLayersDirty ) {
+    _cloudLayersDirty = false;
+    fgClouds->set_update_event( fgClouds->get_update_event()+1 );
+  }
 }
 
 FGEnvironment
@@ -268,33 +214,24 @@ FGEnvironmentMgr::getEnvironment () const
 FGEnvironment
 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
 {
-                               // Always returns the same environment
-                               // for now; we'll make it interesting
-                               // later.
+  // Always returns the same environment
+  // for now; we'll make it interesting
+  // later.
   FGEnvironment env = *_environment;
   env.set_elevation_ft(alt);
   return env;
 }
 
-void
-FGEnvironmentMgr::_update_fdm () const
+FGEnvironment
+FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
 {
-  //
-  // Pass atmosphere on to FDM
-  // FIXME: have FDMs read properties directly.
-  //
-  if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
-                               // convert from Rankine to Celsius
-    cur_fdm_state
-      ->set_Static_temperature((9.0/5.0)
-                              * (_environment->get_temperature_degc() + 273.15));
-                               // convert from inHG to PSF
-    cur_fdm_state
-      ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
-                               // keep in slugs/ft^3
-    cur_fdm_state
-      ->set_Density(_environment->get_density_slugft3());
-  }
+  // Always returns the same environment
+  // for now; we'll make it interesting
+  // later.
+  FGEnvironment env = *_environment;
+  env.set_elevation_ft(aPos.getElevationFt());
+  return env;
+
 }
 
 double
@@ -361,48 +298,64 @@ FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
 const char *
 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
 {
-  switch (thesky->get_cloud_layer(index)->getCoverage()) {
-  case SGCloudLayer::SG_CLOUD_OVERCAST:
-    return "overcast";
-  case SGCloudLayer::SG_CLOUD_BROKEN:
-    return "broken";
-  case SGCloudLayer::SG_CLOUD_SCATTERED:
-    return "scattered";
-  case SGCloudLayer::SG_CLOUD_FEW:
-    return "few";
-  case SGCloudLayer::SG_CLOUD_CIRRUS:
-    return "cirrus";
-  case SGCloudLayer::SG_CLOUD_CLEAR:
-    return "clear";
-  default:
-    return "unknown";
-  }
+  return thesky->get_cloud_layer(index)->getCoverageString().c_str();
 }
 
 void
 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
                                             const char * coverage_name)
 {
-  SGCloudLayer::Coverage coverage;
-  if (!strcmp(coverage_name, "overcast"))
-    coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
-  else if (!strcmp(coverage_name, "broken"))
-    coverage = SGCloudLayer::SG_CLOUD_BROKEN;
-  else if (!strcmp(coverage_name, "scattered"))
-    coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
-  else if (!strcmp(coverage_name, "few"))
-    coverage = SGCloudLayer::SG_CLOUD_FEW;
-  else if (!strcmp(coverage_name, "cirrus"))
-    coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
-  else if (!strcmp(coverage_name, "clear"))
-    coverage = SGCloudLayer::SG_CLOUD_CLEAR;
-  else {
-    SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
-    coverage = SGCloudLayer::SG_CLOUD_CLEAR;
-  }
-  thesky->get_cloud_layer(index)->setCoverage(coverage);
+  if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name )
+    return;
+
+  thesky->get_cloud_layer(index)->setCoverageString(coverage_name);
+  _cloudLayersDirty = true;
 }
 
+int
+FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
+{
+  return thesky->get_cloud_layer(index)->getCoverage();
+}
+
+double 
+FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
+{
+    return thesky->get_cloud_layer(index)->getVisibility_m();
+}
+
+void 
+FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
+{
+    thesky->get_cloud_layer(index)->setVisibility_m(visibility_m);
+}
+
+double 
+FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
+{
+    return thesky->get_cloud_layer(index)->getMaxAlpha();
+}
+
+void 
+FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
+{
+    thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
+}
+
+void 
+FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
+{
+  if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
+    SG_LOG(SG_ALL,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
+    return;
+  }
+
+  if( static_cast<SGCloudLayer::Coverage>(type) == thesky->get_cloud_layer(index)->getCoverage() )
+    return;
+
+  thesky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
+  _cloudLayersDirty = true;
+}
 
 
 // end of environment-mgr.cxx