]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/bfi.cxx
Blame this on David Megginson ... :-)
[flightgear.git] / src / Main / bfi.cxx
index 93a12c98bbd1e49d89628f56e78b10660e0a053a..3c54cae184e1a056474e2077feb9f6137a1acba8 100644 (file)
@@ -107,8 +107,8 @@ reinit ()
   double apAltitude = FGBFI::getAPAltitude();
   const string &targetAirport = FGBFI::getTargetAirport();
   bool gpsLock = FGBFI::getGPSLock();
-  double gpsLatitude = FGBFI::getGPSTargetLatitude();
-  double gpsLongitude = FGBFI::getGPSTargetLongitude();
+  // double gpsLatitude = FGBFI::getGPSTargetLatitude();
+  // double gpsLongitude = FGBFI::getGPSTargetLongitude();
 
   FGBFI::setTargetAirport("");
   cout << "Target airport is " << globals->get_options()->get_airport_id() << endl;
@@ -146,6 +146,34 @@ reinit ()
   cout << "BFI: end reinit\n";
 }
 
+// BEGIN: kludge 2000-12-07
+// This is a kludge around a LaRCsim problem; see setAltitude()
+// for details.
+static int _altitude_countdown = 0;
+static double _requested_altitude = -9999;
+static bool _saved_freeze = false;
+static inline void _check_altitude ()
+{
+  if (_altitude_countdown > 0) {
+    _altitude_countdown--;
+    if (_altitude_countdown == 0) {
+      current_aircraft.fdm_state->set_Altitude(_requested_altitude);
+      globals->set_freeze(_saved_freeze);
+    }
+  }
+}
+
+static int _lighting_countdown = 0;
+static inline void _check_lighting ()
+{
+  if (_lighting_countdown > 0) {
+    _lighting_countdown--;
+    if (_lighting_countdown == 0)
+      fgUpdateSkyAndLightingParams();
+  }
+}
+// END: kludge
+
 
 \f
 ////////////////////////////////////////////////////////////////////////
@@ -171,6 +199,8 @@ FGBFI::init ()
   current_properties.tieString("/sim/aircraft-dir",
                               getAircraftDir, setAircraftDir);
   // TODO: timeGMT
+  current_properties.tieString("/sim/time/gmt",
+                              getDateString, setDateString);
   current_properties.tieString("/sim/time/gmt-string",
                               getGMTString, 0);
   current_properties.tieBool("/sim/hud/visibility",
@@ -190,8 +220,8 @@ FGBFI::init ()
   current_properties.tieDouble("/position/longitude",
                               getLongitude, setLongitude);
   current_properties.tieDouble("/position/altitude",
-                              // getAltitude, setAltitude);
-                              getAltitude, setAltitude, false);
+                              getAltitude, setAltitude);
+//                            getAltitude, setAltitude, false);
   current_properties.tieDouble("/position/altitude-agl",
                               getAGL, 0);
 
@@ -335,6 +365,8 @@ FGBFI::init ()
 void
 FGBFI::update ()
 {
+  _check_altitude();
+  _check_lighting();
   if (_needReinit) {
     reinit();
   }
@@ -425,10 +457,17 @@ FGBFI::setAircraftDir (const string &dir)
 /**
  * Return the current Zulu time.
  */
-time_t
-FGBFI::getTimeGMT ()
+const string &
+FGBFI::getDateString ()
 {
-  return globals->get_time_params()->get_cur_time();
+  static string out;           // FIXME: not thread-safe
+  char buf[64];
+  struct tm * t = globals->get_time_params()->getGmt();
+  sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
+         t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
+         t->tm_hour, t->tm_min, t->tm_sec);
+  out = buf;
+  return out;
 }
 
 
@@ -436,18 +475,46 @@ FGBFI::getTimeGMT ()
  * Set the current Zulu time.
  */
 void
-FGBFI::setTimeGMT (time_t time)
-{
-  if (getTimeGMT() != time) {
-                               // FIXME: need to update lighting
-                               // and solar system
-    globals->get_options()->set_time_offset(time);
-    globals->get_options()->set_time_offset_type(FGOptions::FG_TIME_GMT_ABSOLUTE);
-    globals->get_time_params()->update( cur_fdm_state->get_Longitude(),
-                                       cur_fdm_state->get_Latitude(),
-                                       globals->get_warp() );
-    needReinit();
+FGBFI::setDateString (const string &date_string)
+// FGBFI::setTimeGMT (time_t time)
+{
+  SGTime * st = globals->get_time_params();
+  struct tm * current_time = st->getGmt();
+  struct tm new_time;
+
+                               // Scan for basic ISO format
+                               // YYYY-MM-DDTHH:MM:SS
+  int ret = sscanf(date_string.c_str(), "%d-%d-%dT%d:%d:%d",
+                  &(new_time.tm_year), &(new_time.tm_mon),
+                  &(new_time.tm_mday), &(new_time.tm_hour),
+                  &(new_time.tm_min), &(new_time.tm_sec));
+
+                               // Be pretty picky about this, so
+                               // that strange things don't happen
+                               // if the save file has been edited
+                               // by hand.
+  if (ret != 6) {
+    FG_LOG(FG_INPUT, FG_ALERT, "Date/time string " << date_string
+          << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
+    return;
   }
+
+                               // OK, it looks like we got six
+                               // values, one way or another.
+  new_time.tm_year -= 1900;
+  new_time.tm_mon -= 1;
+
+                               // Now, tell flight gear to use
+                               // the new time.  This was far
+                               // too difficult, by the way.
+  long int warp =
+    mktime(&new_time) - mktime(current_time) + globals->get_warp();
+  double lon = current_aircraft.fdm_state->get_Longitude();
+  double lat = current_aircraft.fdm_state->get_Latitude();
+  double alt = current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER;
+  globals->set_warp(warp);
+  st->update(lon, lat, warp);
+  fgUpdateSkyAndLightingParams();
 }
 
 
@@ -582,6 +649,9 @@ void
 FGBFI::setLatitude (double latitude)
 {
   current_aircraft.fdm_state->set_Latitude(latitude * DEG_TO_RAD);
+  fgUpdateSkyAndLightingParams();
+  if (_lighting_countdown <= 0)
+    _lighting_countdown = 5;
 }
 
 
@@ -602,6 +672,9 @@ void
 FGBFI::setLongitude (double longitude)
 {
   current_aircraft.fdm_state->set_Longitude(longitude * DEG_TO_RAD);
+  fgUpdateSkyAndLightingParams();
+  if (_lighting_countdown <= 0)
+    _lighting_countdown = 5;
 }
 
 
@@ -634,6 +707,22 @@ void
 FGBFI::setAltitude (double altitude)
 {
   current_aircraft.fdm_state->set_Altitude(altitude);
+
+                               // 2000-12-07
+                               // This is an ugly kludge around a
+                               // LaRCsim problem; if the
+                               // requested altitude cannot be
+                               // set right away (because it's
+                               // below the last-calculated ground
+                               // level), pause FGFS, wait for
+                               // five frames, and then try again.
+  if (_altitude_countdown <= 0 &&
+      fabs(getAltitude() - altitude) > 5.0) {
+    _altitude_countdown = 5;
+    _requested_altitude = altitude;
+    _saved_freeze = globals->get_freeze();
+    globals->set_freeze(true);
+  }
 }
 
 
@@ -753,6 +842,8 @@ FGBFI::getEGT ()
 {
   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
       return current_aircraft.fdm_state->get_engine(0)->get_EGT();
+  } else {
+      return 0.0;
   }
 }
 
@@ -765,6 +856,8 @@ FGBFI::getCHT ()
 {
   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
       return current_aircraft.fdm_state->get_engine(0)->get_CHT();
+  } else {
+      return 0.0;
   }
 }
 
@@ -777,6 +870,8 @@ FGBFI::getMP ()
 {
   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
       return current_aircraft.fdm_state->get_engine(0)->get_Manifold_Pressure();
+  } else {
+      return 0.0;
   }
 }