]> git.mxchange.org Git - flightgear.git/blob - src/Time/TimeManager.cxx
Code cleanups, code updates and fix at least on (possible) devide-by-zero
[flightgear.git] / src / Time / TimeManager.cxx
1 // TimeManager.cxx -- simulation-wide time management
2 //
3 // Written by James Turner, started July 2010.
4 //
5 // Copyright (C) 2010  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "TimeManager.hxx"
26
27 #include <simgear/timing/sg_time.hxx>
28 #include <simgear/structure/event_mgr.hxx>
29 #include <simgear/misc/sg_path.hxx>
30 #include <simgear/timing/lowleveltime.h>
31 #include <simgear/structure/commands.hxx>
32 #include <simgear/math/SGMath.hxx>
33
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
36 #include <Time/sunsolver.hxx>
37
38 using std::string;
39
40 static bool do_timeofday (const SGPropertyNode * arg)
41 {
42     const string &offset_type = arg->getStringValue("timeofday", "noon");
43     int offset = arg->getIntValue("offset", 0);
44     TimeManager* self = (TimeManager*) globals->get_subsystem("time");
45     if (offset_type == "real") {
46     // without this, setting 'real' time is a no-op, since the current
47     // wrap value (orig_warp) is retained in setTimeOffset. Ick.
48         fgSetInt("/sim/time/warp", 0);
49     }
50     
51     self->setTimeOffset(offset_type, offset);
52     return true;
53 }
54
55 TimeManager::TimeManager() :
56   _inited(false),
57   _impl(NULL)
58 {
59   globals->get_commands()->addCommand("timeofday", do_timeofday);
60 }
61
62 TimeManager::~TimeManager()
63 {
64    globals->get_commands()->removeCommand("timeofday");
65 }
66
67 void TimeManager::init()
68 {
69   if (_inited) {
70     // time manager has to be initialised early, so needs to be defensive
71     // about multiple initialisation 
72     return; 
73   }
74   
75   _firstUpdate = true;
76   _inited = true;
77   _dtRemainder = 0.0;
78   _adjustWarpOnUnfreeze = false;
79   
80   _maxDtPerFrame = fgGetNode("/sim/max-simtime-per-frame", true);
81   _clockFreeze = fgGetNode("/sim/freeze/clock", true);
82   _timeOverride = fgGetNode("/sim/time/cur-time-override", true);
83   _warp = fgGetNode("/sim/time/warp", true);
84   _warp->addChangeListener(this);
85   
86   _warpDelta = fgGetNode("/sim/time/warp-delta", true);
87   
88   SGPath zone(globals->get_fg_root());
89   zone.append("Timezone");
90   
91   _impl = new SGTime(globals->get_aircraft_position(), zone, _timeOverride->getLongValue());
92   
93   _warpDelta->setDoubleValue(0.0);
94   
95   globals->get_event_mgr()->addTask("updateLocalTime", this,
96                             &TimeManager::updateLocalTime, 30*60 );
97   updateLocalTime();
98   
99   _impl->update(globals->get_aircraft_position(), _timeOverride->getLongValue(),
100                _warp->getIntValue());
101   globals->set_time_params(_impl);
102     
103   // frame-rate / worst-case latency / update-rate counters
104   _frameRate = fgGetNode("/sim/frame-rate", true);
105   _frameLatency = fgGetNode("/sim/frame-latency-max-ms", true);
106   _frameRateWorst = fgGetNode("/sim/frame-rate-worst", true);
107   _lastFrameTime = 0;
108   _frameLatencyMax = 0.0;
109   _frameCount = 0;
110     
111     _sceneryLoaded = fgGetNode("sim/sceneryloaded", true);
112     _modelHz = fgGetNode("sim/model-hz", true);
113     _timeDelta = fgGetNode("sim/time/delta-realtime-sec", true);
114     _simTimeDelta = fgGetNode("sim/time/delta-sec", true);
115
116     _simTimeFactor = fgGetNode("/sim/speed-up", true);
117     // use pre-set value but ensure we get a sane default
118     if (!_simTimeDelta->hasValue()) {
119         _simTimeFactor->setDoubleValue(1.0);
120     }
121 }
122
123 void TimeManager::unbind()
124 {
125     _maxDtPerFrame.clear();
126     _clockFreeze.clear();
127     _timeOverride.clear();
128     _warp.clear();
129     _warpDelta.clear();
130     _frameRate.clear();
131     _frameLatency.clear();
132     _frameRateWorst.clear();
133     
134     _sceneryLoaded.clear();
135     _modelHz.clear();
136     _timeDelta.clear();
137     _simTimeDelta.clear();
138     _simTimeFactor.clear();
139 }
140
141 void TimeManager::postinit()
142 {
143   initTimeOffset();
144 }
145
146 void TimeManager::reinit()
147 {
148   shutdown();
149   init();
150   postinit();
151 }
152
153 void TimeManager::shutdown()
154 {
155   _warp->removeChangeListener(this);
156   
157   globals->set_time_params(NULL);
158   delete _impl;
159   _impl = NULL;
160   _inited = false;
161   globals->get_event_mgr()->removeTask("updateLocalTime");
162 }
163
164 void TimeManager::valueChanged(SGPropertyNode* aProp)
165 {
166   if (aProp == _warp) {
167     if (_clockFreeze->getBoolValue()) {
168     // if the warp is changed manually while frozen, don't modify it when
169     // un-freezing - the user wants to unfreeze with exactly the warp
170     // they specified.
171       _adjustWarpOnUnfreeze = false;
172     }
173     
174     _impl->update(globals->get_aircraft_position(),
175                    _timeOverride->getLongValue(),
176                    _warp->getIntValue());
177   }
178 }
179
180 void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
181 {
182   // Update the elapsed time.
183   if (_firstUpdate) {
184     _lastStamp.stamp();
185     _firstUpdate = false;
186     _lastClockFreeze = _clockFreeze->getBoolValue();
187   }
188
189   bool wait_for_scenery = !_sceneryLoaded->getBoolValue();
190   if (!wait_for_scenery) {
191     throttleUpdateRate();
192   }
193   else
194   {
195       // suppress framerate while initial scenery isn't loaded yet (splash screen still active) 
196       _lastFrameTime=0;
197       _frameCount = 0;
198   }
199   
200   SGTimeStamp currentStamp;
201   currentStamp.stamp();
202   double dt = (currentStamp - _lastStamp).toSecs();
203   if (dt > _frameLatencyMax)
204       _frameLatencyMax = dt;
205
206 // Limit the time we need to spend in simulation loops
207 // That means, if the /sim/max-simtime-per-frame value is strictly positive
208 // you can limit the maximum amount of time you will do simulations for
209 // one frame to display. The cpu time spent in simulations code is roughly
210 // at least O(real_delta_time_sec). If this is (due to running debug
211 // builds or valgrind or something different blowing up execution times)
212 // larger than the real time you will no longer get any response
213 // from flightgear. This limits that effect. Just set to property from
214 // your .fgfsrc or commandline ...
215   double dtMax = _maxDtPerFrame->getDoubleValue();
216   if (0 < dtMax && dtMax < dt) {
217     dt = dtMax;
218   }
219     
220   SGSubsystemGroup* fdmGroup = 
221     globals->get_subsystem_mgr()->get_group(SGSubsystemMgr::FDM);
222   double modelHz = _modelHz->getDoubleValue();
223   fdmGroup->set_fixed_update_time(1.0 / modelHz);
224   
225 // round the real time down to a multiple of 1/model-hz.
226 // this way all systems are updated the _same_ amount of dt.
227   dt += _dtRemainder;
228   int multiLoop = long(floor(dt * modelHz));
229   multiLoop = SGMisc<long>::max(0, multiLoop);
230   _dtRemainder = dt - double(multiLoop)/modelHz;
231   dt = double(multiLoop)/modelHz;
232
233   realDt = dt;
234   if (_clockFreeze->getBoolValue() || wait_for_scenery) {
235     simDt = 0;
236   } else {
237       // sim time can be scaled
238       simDt = dt * _simTimeFactor->getDoubleValue();
239   }
240   
241   _lastStamp = currentStamp;
242   globals->inc_sim_time_sec(simDt);
243
244 // These are useful, especially for Nasal scripts.
245   _timeDelta->setDoubleValue(realDt);
246   _simTimeDelta->setDoubleValue(simDt);
247 }
248
249 void TimeManager::update(double dt)
250 {
251   bool freeze = _clockFreeze->getBoolValue();
252   time_t now = time(NULL);
253
254   if (freeze) {
255     // clock freeze requested
256     if (_timeOverride->getLongValue() == 0) {
257       _timeOverride->setLongValue(now);
258       _adjustWarpOnUnfreeze = true;
259     }
260   } else {
261     // no clock freeze requested
262     if (_lastClockFreeze) {
263       if (_adjustWarpOnUnfreeze) {
264       // clock just unfroze, let's set warp as the difference
265       // between frozen time and current time so we don't get a
266       // time jump (and corresponding sky object and lighting
267       // jump.)
268         int adjust = _timeOverride->getLongValue() - now;
269         SG_LOG(SG_GENERAL, SG_DEBUG, "adjusting on un-freeze:" << adjust);
270         _warp->setIntValue(_warp->getIntValue() + adjust);
271       }
272       _timeOverride->setLongValue(0);
273     }
274
275       // account for speed-up in warp value. This implies when speed-up is not
276       // 1.0 we need to continually adjust warp, either forwards for speed-up,
277       // or backwards for a slow-down. Eg for a speed up of 4x, we want to
278       // incease warp by 3 additional seconds per elapsed real second.
279       // for a 1/2x factor, we want to decrease warp by half a second per
280       // elapsed real second.
281       double speedUp = _simTimeFactor->getDoubleValue() - 1.0;
282       if (speedUp != 0.0) {
283           double realDt = _timeDelta->getDoubleValue();
284           double speedUpOffset = speedUp * realDt;
285           _warp->setDoubleValue(_warp->getDoubleValue() + speedUpOffset);
286       }
287   } // of sim not frozen
288
289     // scale warp-delta by real-dt, so rate is constant with frame-rate,
290     // but warping works while paused
291     int warpDelta = _warpDelta->getIntValue();
292     if (warpDelta) {
293         _adjustWarpOnUnfreeze = false;
294         double warpOffset = warpDelta * _timeDelta->getDoubleValue();
295         _warp->setDoubleValue(_warp->getDoubleValue() + warpOffset);
296     }
297
298   _lastClockFreeze = freeze;
299   _impl->update(globals->get_aircraft_position(),
300                _timeOverride->getLongValue(),
301                _warp->getIntValue());
302
303   computeFrameRate();
304 }
305
306 void TimeManager::computeFrameRate()
307 {
308   // Calculate frame rate average
309   if ((_impl->get_cur_time() != _lastFrameTime)) {
310     _frameRate->setIntValue(_frameCount);
311     _frameLatency->setDoubleValue(_frameLatencyMax*1000);
312     if (_frameLatencyMax>0)
313         _frameRateWorst->setIntValue(1/_frameLatencyMax);
314     _frameCount = 0;
315     _frameLatencyMax = 0.0;
316   }
317   
318   _lastFrameTime = _impl->get_cur_time();
319   ++_frameCount;
320 }
321
322 void TimeManager::throttleUpdateRate()
323 {
324   // common case, no throttle requested
325   double throttle_hz = fgGetDouble("/sim/frame-rate-throttle-hz", 0.0);
326   if (throttle_hz <= 0)
327     return; // no-op
328
329   // sleep for exactly 1/hz seconds relative to the past valid timestamp
330   SGTimeStamp::sleepUntil(_lastStamp + SGTimeStamp::fromSec(1/throttle_hz));
331 }
332
333 // periodic time updater wrapper
334 void TimeManager::updateLocalTime() 
335 {
336   SGPath zone(globals->get_fg_root());
337   zone.append("Timezone");
338   _impl->updateLocal(globals->get_aircraft_position(), zone.str());
339 }
340
341 void TimeManager::initTimeOffset()
342 {
343
344   long int offset = fgGetLong("/sim/startup/time-offset");
345   string offset_type = fgGetString("/sim/startup/time-offset-type");
346   setTimeOffset(offset_type, offset);
347 }
348
349 void TimeManager::setTimeOffset(const std::string& offset_type, long int offset)
350 {
351   // Handle potential user specified time offsets
352   int orig_warp = _warp->getIntValue();
353   time_t cur_time = _impl->get_cur_time();
354   time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
355   time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
356   time_t aircraftLocalTime = 
357       sgTimeGetGMT( fgLocaltime(&cur_time, _impl->get_zonename() ) );
358     
359   // Okay, we now have several possible scenarios
360   SGGeod loc = globals->get_aircraft_position();
361   int warp = 0;
362   
363   if ( offset_type == "real" ) {
364       warp = 0;
365   } else if ( offset_type == "dawn" ) {
366       warp = fgTimeSecondsUntilSunAngle( cur_time, loc, 90.0, true );
367   } else if ( offset_type == "morning" ) {
368      warp = fgTimeSecondsUntilSunAngle( cur_time, loc, 75.0, true ); 
369   } else if ( offset_type == "noon" ) {
370      warp = fgTimeSecondsUntilSunAngle( cur_time, loc, 0.0, true ); 
371   } else if ( offset_type == "afternoon" ) {
372     warp = fgTimeSecondsUntilSunAngle( cur_time, loc, 75.0, false );  
373   } else if ( offset_type == "dusk" ) {
374     warp = fgTimeSecondsUntilSunAngle( cur_time, loc, 90.0, false );
375   } else if ( offset_type == "evening" ) {
376     warp = fgTimeSecondsUntilSunAngle( cur_time, loc, 100.0, false );
377   } else if ( offset_type == "midnight" ) {
378     warp = fgTimeSecondsUntilSunAngle( cur_time, loc, 180.0, false );
379   } else if ( offset_type == "system-offset" ) {
380     warp = offset;
381     orig_warp = 0;
382   } else if ( offset_type == "gmt-offset" ) {
383     warp = offset - (currGMT - systemLocalTime);
384     orig_warp = 0;
385   } else if ( offset_type == "latitude-offset" ) {
386     warp = offset - (aircraftLocalTime - systemLocalTime);
387     orig_warp = 0;
388   } else if ( offset_type == "system" ) {
389     warp = offset - (systemLocalTime - currGMT) - cur_time;
390   } else if ( offset_type == "gmt" ) {
391       warp = offset - cur_time;
392   } else if ( offset_type == "latitude" ) {
393       warp = offset - (aircraftLocalTime - currGMT)- cur_time; 
394   } else {
395     SG_LOG( SG_GENERAL, SG_ALERT,
396           "TimeManager::setTimeOffset: unsupported offset: " << offset_type );
397      warp = 0;
398   }
399   
400   if( fgGetBool("/sim/time/warp-easing", false) ) {
401     double duration = fgGetDouble("/sim/time/warp-easing-duration-secs", 5.0 );
402     const string easing = fgGetString("/sim/time/warp-easing-method", "swing" );
403     SGPropertyNode n;
404     n.setDoubleValue( orig_warp + warp );
405     _warp->interpolate( "numeric", n, duration, easing );
406   } else {
407     _warp->setIntValue( orig_warp + warp );
408   }
409
410   SG_LOG( SG_GENERAL, SG_INFO, "After TimeManager::setTimeOffset(): warp = " 
411             << _warp->getIntValue() );
412 }