]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment_mgr.cxx
Patch from Cameron Moore:
[flightgear.git] / src / Environment / environment_mgr.cxx
index fc0a1a04f3c28ee3221cd3a9e7f05068ad21e1eb..51ccfd986d794cc04bb1226a02dabdbb8ded9047 100644 (file)
@@ -21,6 +21,7 @@
 // $Id$
 
 #include <simgear/debug/logstream.hxx>
+#include <simgear/sky/sky.hxx>
 
 #include <Main/fg_props.hxx>
 #include <Aircraft/aircraft.hxx>
@@ -29,6 +30,8 @@
 #include "environment_ctrl.hxx"
 #include "environment_mgr.hxx"
 
+extern SGSky *thesky;          // FIXME: from main.cxx
+
 
 FGEnvironmentMgr::FGEnvironmentMgr ()
   : _environment(new FGEnvironment),
@@ -48,6 +51,7 @@ FGEnvironmentMgr::init ()
   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
   _controller->setEnvironment(_environment);
   _controller->init();
+  _update_fdm();
 }
 
 void
@@ -64,6 +68,14 @@ FGEnvironmentMgr::bind ()
        &FGEnvironment::get_temperature_degc,
        &FGEnvironment::set_temperature_degc);
   fgSetArchivable("/environment/temperature-degc");
+  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,
+       &FGEnvironment::set_dewpoint_degc);
+  fgSetArchivable("/environment/dewpoint-degc");
   fgTie("/environment/pressure-sea-level-inhg", _environment,
        &FGEnvironment::get_pressure_sea_level_inhg,
        &FGEnvironment::set_pressure_sea_level_inhg);
@@ -72,6 +84,8 @@ FGEnvironmentMgr::bind ()
        &FGEnvironment::get_pressure_inhg,
        &FGEnvironment::set_pressure_inhg);
   fgSetArchivable("/environment/pressure-inhg");
+  fgTie("/environment/density-slugft3", _environment,
+       &FGEnvironment::get_density_slugft3); // read-only
   fgTie("/environment/wind-from-heading-deg", _environment,
        &FGEnvironment::get_wind_from_heading_deg,
        &FGEnvironment::set_wind_from_heading_deg);
@@ -89,17 +103,64 @@ FGEnvironmentMgr::bind ()
        &FGEnvironment::get_wind_from_down_fps,
        &FGEnvironment::set_wind_from_down_fps);
   fgSetArchivable("/environment/wind-from-down-fps");
+
+  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,
+         &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,
+         &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,
+         &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,
+         &FGEnvironmentMgr::get_cloud_layer_transition_ft,
+         &FGEnvironmentMgr::set_cloud_layer_transition_ft);
+    fgSetArchivable(buf);
+    sprintf(buf, "/environment/clouds/layer[%d]/type", i);
+    fgTie(buf, this, i,
+         &FGEnvironmentMgr::get_cloud_layer_type,
+         &FGEnvironmentMgr::set_cloud_layer_type);
+    fgSetArchivable(buf);
+  }
 }
 
 void
 FGEnvironmentMgr::unbind ()
 {
   fgUntie("/environment/visibility-m");
-  fgUntie("/environment/wind-from-heading-deg");
-  fgUntie("/environment/wind-speed-kt");
+  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/wind-from-north-fps");
   fgUntie("/environment/wind-from-east-fps");
   fgUntie("/environment/wind-from-down-fps");
+  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);
+  }
 }
 
 void
@@ -112,6 +173,8 @@ FGEnvironmentMgr::update (double dt)
                                   _environment->get_wind_from_east_fps(),
                                   _environment->get_wind_from_down_fps());
   _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
+
+  _update_fdm();
 }
 
 FGEnvironment
@@ -131,4 +194,119 @@ FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
   return env;
 }
 
+void
+FGEnvironmentMgr::_update_fdm () 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());
+  }
+}
+
+double
+FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
+{
+  return thesky->get_cloud_layer(index)->getSpan_m();
+}
+
+void
+FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
+{
+  thesky->get_cloud_layer(index)->setSpan_m(span_m);
+}
+
+double
+FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
+{
+  return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
+}
+
+void
+FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
+{
+  thesky->get_cloud_layer(index)
+    ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
+}
+
+double
+FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
+{
+  return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
+}
+
+void
+FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
+{
+  thesky->get_cloud_layer(index)
+    ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
+}
+
+double
+FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
+{
+  return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
+}
+
+void
+FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
+                                                double transition_ft)
+{
+  thesky->get_cloud_layer(index)
+    ->setTransition_m(transition_ft * SG_FEET_TO_METER);
+}
+
+const char *
+FGEnvironmentMgr::get_cloud_layer_type (int index) const
+{
+  switch (thesky->get_cloud_layer(index)->getType()) {
+  case SGCloudLayer::SG_CLOUD_OVERCAST:
+    return "overcast";
+  case SGCloudLayer::SG_CLOUD_MOSTLY_CLOUDY:
+    return "mostly-cloudy";
+  case SGCloudLayer::SG_CLOUD_MOSTLY_SUNNY:
+    return "mostly-sunny";
+  case SGCloudLayer::SG_CLOUD_CIRRUS:
+    return "cirrus";
+  case SGCloudLayer::SG_CLOUD_CLEAR:
+    return "clear";
+  default:
+    return "unknown";
+  }
+}
+
+void
+FGEnvironmentMgr::set_cloud_layer_type (int index, const char * type_name)
+{
+  SGCloudLayer::Type type;
+  if (!strcmp(type_name, "overcast"))
+    type = SGCloudLayer::SG_CLOUD_OVERCAST;
+  else if (!strcmp(type_name, "mostly-cloudy"))
+    type = SGCloudLayer::SG_CLOUD_MOSTLY_CLOUDY;
+  else if (!strcmp(type_name, "mostly-sunny"))
+    type = SGCloudLayer::SG_CLOUD_MOSTLY_SUNNY;
+  else if (!strcmp(type_name, "cirrus"))
+    type = SGCloudLayer::SG_CLOUD_CIRRUS;
+  else if (!strcmp(type_name, "clear"))
+    type = SGCloudLayer::SG_CLOUD_CLEAR;
+  else {
+    SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << type_name);
+    type = SGCloudLayer::SG_CLOUD_CLEAR;
+  }
+  thesky->get_cloud_layer(index)->setType(type);
+}
+
+
+
 // end of environment-mgr.cxx