]> git.mxchange.org Git - flightgear.git/commitdiff
Added an interface for an environment controller, together with a
authordavid <david>
Tue, 7 May 2002 00:03:54 +0000 (00:03 +0000)
committerdavid <david>
Tue, 7 May 2002 00:03:54 +0000 (00:03 +0000)
default implementation that uses user-supplied params.  Currently, the
only parameters are

  /environment/params/base-wind-speed-kt
  /environment/params/gust-wind-speed-kt

but others will show up soon (i.e. sheer, variable direction, variable
visibility, etc.).  To activate these properties, you have to
configure --with-new-environment.

The gusting function is simplistic and needs to be replaced with
something better, though it doesn't feel too far off.

src/Environment/Makefile.am
src/Environment/environment.hxx
src/Environment/environment_ctrl.cxx [new file with mode: 0644]
src/Environment/environment_ctrl.hxx [new file with mode: 0644]
src/Environment/environment_mgr.cxx
src/Environment/environment_mgr.hxx

index 7b01218f62fda664a2d127779592f9f64fe2ecb0..decc9a3d61a6f7963bf9174c8e3b0ac540d6c4cf 100644 (file)
@@ -5,7 +5,8 @@
 noinst_LIBRARIES = libEnvironment.a
 
 libEnvironment_a_SOURCES = environment.cxx environment.hxx \
-       environment_mgr.cxx environment_mgr.hxx
+                          environment_mgr.cxx environment_mgr.hxx \
+                           environment_ctrl.cxx environment_ctrl.hxx
 
 if OLD_AUTOMAKE
 INCLUDES += -I$(top_srcdir) -I$(top_srcdir)/src
index f3a77152de0e488070b9a1b00d1820882115f887..111af137eb900b2b5ff8f5d7fbc1cc696998dd65 100644 (file)
@@ -85,12 +85,6 @@ private:
   double wind_from_east_fps;
   double wind_from_down_fps;
 
-                               // Do these belong here?
-#if 0
-  GLfloat fog_exp_density;
-  GLfloat fog_exp2_density;
-#endif
-
 };
 
 #endif // _ENVIRONMENT_HXX
diff --git a/src/Environment/environment_ctrl.cxx b/src/Environment/environment_ctrl.cxx
new file mode 100644 (file)
index 0000000..0d57267
--- /dev/null
@@ -0,0 +1,154 @@
+// environment_ctrl.cxx -- manager for natural environment information.
+//
+// Written by David Megginson, started February 2002.
+//
+// Copyright (C) 2002  David Megginson - david@megginson.com
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+// $Id$
+
+#include <simgear/debug/logstream.hxx>
+
+#include <stdlib.h>
+#include <Main/fg_props.hxx>
+
+#include "environment_ctrl.hxx"
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of FGEnvironmentCtrl abstract base class.
+////////////////////////////////////////////////////////////////////////
+
+FGEnvironmentCtrl::FGEnvironmentCtrl ()
+  : _environment(0),
+    _lon_deg(0),
+    _lat_deg(0),
+    _elev_ft(0)
+{
+}
+
+FGEnvironmentCtrl::~FGEnvironmentCtrl ()
+{
+}
+
+void
+FGEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
+{
+  _environment = environment;
+}
+
+void
+FGEnvironmentCtrl::setLongitudeDeg (double lon_deg)
+{
+  _lon_deg = lon_deg;
+}
+
+void
+FGEnvironmentCtrl::setLatitudeDeg (double lat_deg)
+{
+  _lat_deg = lat_deg;
+}
+
+void
+FGEnvironmentCtrl::setElevationFt (double elev_ft)
+{
+  _elev_ft = elev_ft;
+}
+
+void
+FGEnvironmentCtrl::setPosition (double lon_deg, double lat_deg, double elev_ft)
+{
+  _lon_deg = lon_deg;
+  _lat_deg = lat_deg;
+  _elev_ft = elev_ft;
+}
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of FGUserDefEnvironmentCtrl.
+////////////////////////////////////////////////////////////////////////
+
+FGUserDefEnvironmentCtrl::FGUserDefEnvironmentCtrl ()
+  : _base_wind_speed_node(0),
+    _gust_wind_speed_node(0),
+    _current_wind_speed_kt(0),
+    _delta_wind_speed_kt(0)
+{
+}
+
+FGUserDefEnvironmentCtrl::~FGUserDefEnvironmentCtrl ()
+{
+}
+
+void
+FGUserDefEnvironmentCtrl::init ()
+{
+                               // Fill in some defaults.
+  if (!fgHasNode("/environment/params/base-wind-speed-kt"))
+    fgSetDouble("/environment/params/base-wind-speed-kt",
+               fgGetDouble("/environment/wind-speed-kt"));
+  if (!fgHasNode("/environment/params/gust-wind-speed-kt"))
+    fgSetDouble("/environment/params/gust-wind-speed-kt",
+               fgGetDouble("/environment/params/base-wind-speed-kt"));
+
+  _base_wind_speed_node =
+    fgGetNode("/environment/params/base-wind-speed-kt", true);
+  _gust_wind_speed_node =
+    fgGetNode("/environment/params/gust-wind-speed-kt", true);
+
+  _current_wind_speed_kt = _base_wind_speed_node->getDoubleValue();
+  _delta_wind_speed_kt = 0.1;
+}
+
+void
+FGUserDefEnvironmentCtrl::update (int dt)
+{
+  double base_wind_speed = _base_wind_speed_node->getDoubleValue();
+  double gust_wind_speed = _gust_wind_speed_node->getDoubleValue();
+
+  if (base_wind_speed == gust_wind_speed) {
+    _current_wind_speed_kt = base_wind_speed;
+  } else {
+    int rn = rand() % 128;
+    int sign = (_delta_wind_speed_kt < 0 ? -1 : 1);
+    double gust = _current_wind_speed_kt - base_wind_speed;
+    double incr = gust / 50;
+
+    if (rn == 0)
+      _delta_wind_speed_kt = - _delta_wind_speed_kt;
+    else if (rn < 4)
+      _delta_wind_speed_kt -= incr * sign;
+    else if (rn < 16)
+      _delta_wind_speed_kt += incr * sign;
+
+    _current_wind_speed_kt += _delta_wind_speed_kt;
+
+    if (_current_wind_speed_kt < base_wind_speed) {
+      _current_wind_speed_kt = base_wind_speed;
+      _delta_wind_speed_kt = 0.01;
+    } else if (_current_wind_speed_kt > gust_wind_speed) {
+      _current_wind_speed_kt = gust_wind_speed;
+      _delta_wind_speed_kt = -0.01;
+    }
+  }
+  
+  if (_environment != 0)
+    _environment->set_wind_speed_kt(_current_wind_speed_kt);
+}
+
+// end of environment_ctrl.cxx
diff --git a/src/Environment/environment_ctrl.hxx b/src/Environment/environment_ctrl.hxx
new file mode 100644 (file)
index 0000000..d50dba7
--- /dev/null
@@ -0,0 +1,100 @@
+// environment-ctrl.hxx -- controller for environment information.
+//
+// Written by David Megginson, started May 2002.
+//
+// Copyright (C) 2002  David Megginson - david@megginson.com
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+// $Id$
+
+#ifndef _ENVIRONMENT_CTRL_HXX
+#define _ENVIRONMENT_CTRL_HXX
+
+#include <simgear/compiler.h>
+
+#ifdef SG_HAVE_STD_INCLUDES
+#  include <cmath>
+#else
+#  include <math.h>
+#endif
+
+class SGPropertyNode;
+
+#include "environment.hxx"
+
+
+\f
+/**
+ * Interface to control environment information for a specific location.
+ */
+class FGEnvironmentCtrl
+{
+
+public:
+
+  FGEnvironmentCtrl ();
+  virtual ~FGEnvironmentCtrl ();
+
+  virtual void setEnvironment (FGEnvironment * environment);
+
+  virtual FGEnvironment * getEnvironment () const { return _environment; }
+
+  virtual void setLongitudeDeg (double lon_deg);
+  virtual void setLatitudeDeg (double lat_deg);
+  virtual void setElevationFt (double elev_ft);
+  virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
+
+  virtual double getLongitudeDeg () const { return _lon_deg; }
+  virtual double getLatitudeDeg () const { return _lat_deg; }
+  virtual double getElevationFt () const { return _elev_ft; }
+
+  virtual void init () = 0;
+  virtual void update (int dt) = 0;
+
+protected:
+
+  FGEnvironment * _environment;
+  double _lon_deg;
+  double _lat_deg;
+  double _elev_ft;
+
+};
+
+
+\f
+/**
+ * Environment controller using user-supplied parameters.
+ */
+class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
+{
+public:
+  FGUserDefEnvironmentCtrl ();
+  virtual ~FGUserDefEnvironmentCtrl ();
+
+  virtual void init ();
+  virtual void update (int dt);
+
+private:
+
+  SGPropertyNode * _base_wind_speed_node;
+  SGPropertyNode * _gust_wind_speed_node;
+
+  double _current_wind_speed_kt;
+  double _delta_wind_speed_kt;
+
+};
+
+#endif // _ENVIRONMENT_CTRL_HXX
index 188d73a208402dde1a075393917df821d7188b21..4f338895478ab5f406b6c5c595de38b0db2fbd0a 100644 (file)
 #include <Main/fg_props.hxx>
 #include <Aircraft/aircraft.hxx>
 
+#include "environment.hxx"
+#include "environment_ctrl.hxx"
 #include "environment_mgr.hxx"
 
 
 FGEnvironmentMgr::FGEnvironmentMgr ()
+  : _environment(new FGEnvironment),
+    _controller(new FGUserDefEnvironmentCtrl)
 {
-  _environment = new FGEnvironment();
 }
 
 FGEnvironmentMgr::~FGEnvironmentMgr ()
 {
   delete _environment;
+  delete _controller;
 }
 
 void
 FGEnvironmentMgr::init ()
 {
   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
+  _controller->setEnvironment(_environment);
+  _controller->init();
 }
 
 void
@@ -91,6 +97,7 @@ FGEnvironmentMgr::unbind ()
 void
 FGEnvironmentMgr::update (int dt)
 {
+  _controller->update(dt);
                                // FIXME: the FDMs should update themselves
   current_aircraft.fdm_state
     ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
index 3238c9bf99114f71a92ce438400d44ad9fa2dc7d..d8757900bb33b237d1ad4a9ccded515643275fec 100644 (file)
@@ -33,7 +33,8 @@
 #  include <math.h>
 #endif
 
-#include "environment.hxx"
+class FGEnvironment;
+class FGEnvironmentCtrl;
 
 
 /**
@@ -67,6 +68,7 @@ public:
 private:
 
   FGEnvironment * _environment;        // always the same, for now
+  FGEnvironmentCtrl * _controller; // always the same, for now
 
 };