From 7dfbcf091880ab9d9ed24917f50e1b1920c6a811 Mon Sep 17 00:00:00 2001
From: jano <jean.pellotier@wanadoo.fr>
Date: Fri, 11 Dec 2020 23:11:58 +0100
Subject: [PATCH] Make the steady clock and MP clock init at modelHz boundary.

This removing any fractional (of a modelHz step) part when initialising.
This allow us to sync time in different FG instances, if FPS allow.
---
 src/Time/TimeManager.cxx | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/src/Time/TimeManager.cxx b/src/Time/TimeManager.cxx
index 856adbcf0..1722ff72a 100644
--- a/src/Time/TimeManager.cxx
+++ b/src/Time/TimeManager.cxx
@@ -197,24 +197,31 @@ void TimeManager::valueChanged(SGPropertyNode* aProp)
 
 void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
 {
-  // Update the elapsed time.
-  if (_firstUpdate) {
-    _lastStamp.stamp();
+    const double modelHz = _modelHz->getDoubleValue();
 
-    // we initialise the mp protocol clock  with the system clock.
-    _systemStamp.systemClockHoursAndMinutes();
-    _steadyClock = _systemStamp.toSecs();
+    // Update the elapsed time.
+    if (_firstUpdate) {
+        _lastStamp.stamp();
 
-    _firstUpdate = false;
-    _lastClockFreeze = _clockFreeze->getBoolValue();
+        // Initialise the mp protocol / steady  clock with the system clock.
+        // later, the clock follows steps of 1/modelHz (120 by default),
+        // so the MP clock remains aligned to these boundaries
+
+        _systemStamp.systemClockHoursAndMinutes();
+        const double systemStamp = _systemStamp.toSecs();
+        _steadyClock = floor(systemStamp * modelHz) / modelHz;
+
+        // initialize the remainder with offset from the system clock
+        _dtRemainder = systemStamp - _steadyClock;
+
+        _firstUpdate = false;
+        _lastClockFreeze = _clockFreeze->getBoolValue();
   }
 
   bool wait_for_scenery = !_sceneryLoaded->getBoolValue();
   if (!wait_for_scenery) {
     throttleUpdateRate();
-  }
-  else
-  {
+  } else {
       // suppress framerate while initial scenery isn't loaded yet (splash screen still active) 
       _lastFrameTime=0;
       _frameCount = 0;
@@ -258,7 +265,6 @@ void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
     
   SGSubsystemGroup* fdmGroup = 
     globals->get_subsystem_mgr()->get_group(SGSubsystemMgr::FDM);
-  double modelHz = _modelHz->getDoubleValue();
   fdmGroup->set_fixed_update_time(1.0 / modelHz);
 
 // round the real time down to a multiple of 1/model-hz.