1 // TimeManager.cxx -- simulation-wide time management
3 // Written by James Turner, started July 2010.
5 // Copyright (C) 2010 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
25 #include "TimeManager.hxx"
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>
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
36 #include <Time/sunsolver.hxx>
40 static bool do_timeofday (const SGPropertyNode * arg)
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);
51 self->setTimeOffset(offset_type, offset);
55 TimeManager::TimeManager() :
59 globals->get_commands()->addCommand("timeofday", do_timeofday);
62 TimeManager::~TimeManager()
64 globals->get_commands()->removeCommand("timeofday");
67 void TimeManager::init()
70 // time manager has to be initialised early, so needs to be defensive
71 // about multiple initialisation
78 _adjustWarpOnUnfreeze = false;
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);
86 _warpDelta = fgGetNode("/sim/time/warp-delta", true);
88 SGPath zone(globals->get_fg_root());
89 zone.append("Timezone");
91 _impl = new SGTime(globals->get_aircraft_position(), zone, _timeOverride->getLongValue());
93 _warpDelta->setDoubleValue(0.0);
95 globals->get_event_mgr()->addTask("updateLocalTime", this,
96 &TimeManager::updateLocalTime, 30*60 );
99 _impl->update(globals->get_aircraft_position(), _timeOverride->getLongValue(),
100 _warp->getIntValue());
101 globals->set_time_params(_impl);
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);
108 _frameLatencyMax = 0.0;
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);
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);
123 void TimeManager::unbind()
125 _maxDtPerFrame.clear();
126 _clockFreeze.clear();
127 _timeOverride.clear();
131 _frameLatency.clear();
132 _frameRateWorst.clear();
134 _sceneryLoaded.clear();
137 _simTimeDelta.clear();
138 _simTimeFactor.clear();
141 void TimeManager::postinit()
146 void TimeManager::reinit()
153 void TimeManager::shutdown()
155 _warp->removeChangeListener(this);
157 globals->set_time_params(NULL);
161 globals->get_event_mgr()->removeTask("updateLocalTime");
164 void TimeManager::valueChanged(SGPropertyNode* aProp)
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
171 _adjustWarpOnUnfreeze = false;
174 _impl->update(globals->get_aircraft_position(),
175 _timeOverride->getLongValue(),
176 _warp->getIntValue());
180 void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
182 // Update the elapsed time.
185 _firstUpdate = false;
186 _lastClockFreeze = _clockFreeze->getBoolValue();
189 bool wait_for_scenery = !_sceneryLoaded->getBoolValue();
190 if (!wait_for_scenery) {
191 throttleUpdateRate();
195 // suppress framerate while initial scenery isn't loaded yet (splash screen still active)
200 SGTimeStamp currentStamp;
201 currentStamp.stamp();
202 double dt = (currentStamp - _lastStamp).toSecs();
203 if (dt > _frameLatencyMax)
204 _frameLatencyMax = dt;
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) {
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);
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.
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;
234 if (_clockFreeze->getBoolValue() || wait_for_scenery) {
237 // sim time can be scaled
238 simDt = dt * _simTimeFactor->getDoubleValue();
241 _lastStamp = currentStamp;
242 globals->inc_sim_time_sec(simDt);
244 // These are useful, especially for Nasal scripts.
245 _timeDelta->setDoubleValue(realDt);
246 _simTimeDelta->setDoubleValue(simDt);
249 void TimeManager::update(double dt)
251 bool freeze = _clockFreeze->getBoolValue();
252 time_t now = time(NULL);
255 // clock freeze requested
256 if (_timeOverride->getLongValue() == 0) {
257 _timeOverride->setLongValue(now);
258 _adjustWarpOnUnfreeze = true;
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
268 int adjust = _timeOverride->getLongValue() - now;
269 SG_LOG(SG_GENERAL, SG_DEBUG, "adjusting on un-freeze:" << adjust);
270 _warp->setIntValue(_warp->getIntValue() + adjust);
272 _timeOverride->setLongValue(0);
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);
287 } // of sim not frozen
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();
293 _adjustWarpOnUnfreeze = false;
294 double warpOffset = warpDelta * _timeDelta->getDoubleValue();
295 _warp->setDoubleValue(_warp->getDoubleValue() + warpOffset);
298 _lastClockFreeze = freeze;
299 _impl->update(globals->get_aircraft_position(),
300 _timeOverride->getLongValue(),
301 _warp->getIntValue());
306 void TimeManager::computeFrameRate()
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);
315 _frameLatencyMax = 0.0;
318 _lastFrameTime = _impl->get_cur_time();
322 void TimeManager::throttleUpdateRate()
324 // common case, no throttle requested
325 double throttle_hz = fgGetDouble("/sim/frame-rate-throttle-hz", 0.0);
326 if (throttle_hz <= 0)
329 // sleep for exactly 1/hz seconds relative to the past valid timestamp
330 SGTimeStamp::sleepUntil(_lastStamp + SGTimeStamp::fromSec(1/throttle_hz));
333 // periodic time updater wrapper
334 void TimeManager::updateLocalTime()
336 SGPath zone(globals->get_fg_root());
337 zone.append("Timezone");
338 _impl->updateLocal(globals->get_aircraft_position(), zone.str());
341 void TimeManager::initTimeOffset()
344 long int offset = fgGetLong("/sim/startup/time-offset");
345 string offset_type = fgGetString("/sim/startup/time-offset-type");
346 setTimeOffset(offset_type, offset);
349 void TimeManager::setTimeOffset(const std::string& offset_type, long int offset)
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() ) );
359 // Okay, we now have several possible scenarios
360 SGGeod loc = globals->get_aircraft_position();
363 if ( offset_type == "real" ) {
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" ) {
382 } else if ( offset_type == "gmt-offset" ) {
383 warp = offset - (currGMT - systemLocalTime);
385 } else if ( offset_type == "latitude-offset" ) {
386 warp = offset - (aircraftLocalTime - systemLocalTime);
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;
395 SG_LOG( SG_GENERAL, SG_ALERT,
396 "TimeManager::setTimeOffset: unsupported offset: " << offset_type );
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" );
404 n.setDoubleValue( orig_warp + warp );
405 _warp->interpolate( "numeric", n, duration, easing );
407 _warp->setIntValue( orig_warp + warp );
410 SG_LOG( SG_GENERAL, SG_INFO, "After TimeManager::setTimeOffset(): warp = "
411 << _warp->getIntValue() );