]> git.mxchange.org Git - flightgear.git/commitdiff
replay system: protect from recording multiple records for identical time
authorThorstenB <brehmt@gmail.com>
Sun, 11 Nov 2012 16:36:10 +0000 (17:36 +0100)
committerThorstenB <brehmt@gmail.com>
Sun, 11 Nov 2012 16:36:10 +0000 (17:36 +0100)
And avoid div by zero when dt==0 for recorded data.

src/Aircraft/flightrecorder.cxx
src/Aircraft/replay.cxx

index a229f6814ebe39535b0eac05bcfc793846de69af..5e37b2c3686847ad4d266acc225e6750193f4d3d 100644 (file)
@@ -462,16 +462,20 @@ FGFlightRecorder::replay(double SimTime, const FGReplayData* _pNextBuffer, const
         return;
 
     int Offset = 0;
-    double ratio;
+    double ratio = 1.0;
     if (pLastBuffer)
     {
         double NextSimTime = _pNextBuffer->sim_time;
         double LastSimTime = _pLastBuffer->sim_time;
-        ratio = (SimTime - LastSimTime) / (NextSimTime - LastSimTime);
-    }
-    else
-    {
-        ratio = 1.0;
+        double Numerator = SimTime - LastSimTime;
+        double dt = NextSimTime - LastSimTime;
+        // avoid divide by zero and other quirks
+        if ((Numerator > 0.0)&&(dt != 0.0))
+        {
+            ratio = Numerator / dt;
+            if (ratio > 1.0)
+                ratio = 1.0;
+        }
     }
 
     Offset += sizeof(double);
index 959437ef9f932b665669765c6fc82c64bda85f23..cea3bf2637a0f2d4d542bf65fa7e5655a2af8c9d 100644 (file)
@@ -472,11 +472,19 @@ FGReplay::update( double dt )
 
     // flight recording
 
-    sim_time += dt * speed_up->getDoubleValue();
-
     // sanity check, don't collect data if FDM data isn't good
-    if (!fgGetBool("/sim/fdm-initialized", false)) {
+    if ((!fgGetBool("/sim/fdm-initialized", false))||(dt==0.0))
         return;
+
+    {
+        double new_sim_time = sim_time + dt * speed_up->getDoubleValue();
+        // don't record multiple records with the same timestamp (or go backwards in time)
+        if (new_sim_time <= sim_time)
+        {
+            SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Time warp detected!");
+            return;
+        }
+        sim_time = new_sim_time;
     }
 
     FGReplayData* r = record(sim_time);
@@ -870,7 +878,7 @@ FGReplay::saveTape(const SGPropertyNode* ConfigData)
     meta->setStringValue("closest-airport-id",      fgGetString("/sim/airport/closest-airport-id", ""));
     const char* aircraft_version = fgGetString("/sim/aircraft-version", "");
     if (aircraft_version[0]==0)
-        aircraft_version = "(unknown aircraft version)";
+        aircraft_version = "(undefined)";
     meta->setStringValue("aircraft-version", aircraft_version);
 
     // add information on the tape's recording duration