]> git.mxchange.org Git - flightgear.git/blob - src/Time/TimeManager.cxx
Handle scenery-override in a single place only.
[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
33 #include <Main/fg_props.hxx>
34 #include <Main/globals.hxx>
35 #include <Time/sunsolver.hxx>
36
37 using std::string;
38
39 static bool do_timeofday (const SGPropertyNode * arg)
40 {
41     const string &offset_type = arg->getStringValue("timeofday", "noon");
42     int offset = arg->getIntValue("offset", 0);
43     TimeManager* self = (TimeManager*) globals->get_subsystem("time");
44     if (offset_type == "real") {
45     // without this, setting 'real' time is a no-op, since the current
46     // wrap value (orig_warp) is retained in setTimeOffset. Ick.
47         fgSetInt("/sim/time/warp", 0);
48     }
49     
50     self->setTimeOffset(offset_type, offset);
51     return true;
52 }
53
54 TimeManager::TimeManager() :
55   _inited(false),
56   _impl(NULL),
57   _sceneryLoaded("sim/sceneryloaded"),
58   _modelHz("sim/model-hz"),
59   _timeDelta("sim/time/delta-realtime-sec"),
60   _simTimeDelta("sim/time/delta-sec")
61 {
62   SGCommandMgr::instance()->addCommand("timeofday", do_timeofday);
63 }
64
65 void TimeManager::init()
66 {
67   if (_inited) {
68     // time manager has to be initialised early, so needs to be defensive
69     // about multiple initialisation 
70     return; 
71   }
72   
73   _firstUpdate = true;
74   _inited = true;
75   _dtRemainder = 0.0;
76   _adjustWarpOnUnfreeze = false;
77   
78   _maxDtPerFrame = fgGetNode("/sim/max-simtime-per-frame", true);
79   _clockFreeze = fgGetNode("/sim/freeze/clock", true);
80   _timeOverride = fgGetNode("/sim/time/cur-time-override", true);
81   _warp = fgGetNode("/sim/time/warp", true);
82   _warp->addChangeListener(this);
83   
84   _warpDelta = fgGetNode("/sim/time/warp-delta", true);
85   
86   _longitudeDeg = fgGetNode("/position/longitude-deg", true);
87   _latitudeDeg = fgGetNode("/position/latitude-deg", true);
88   
89   SGPath zone(globals->get_fg_root());
90   zone.append("Timezone");
91   double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
92   double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
93   
94   _impl = new SGTime(lon, lat, zone.str(), _timeOverride->getLongValue());
95   
96   _warpDelta->setIntValue(0);
97   
98   globals->get_event_mgr()->addTask("updateLocalTime", this,
99                             &TimeManager::updateLocalTime, 30*60 );
100   updateLocalTime();
101   
102   _impl->update(lon, lat, _timeOverride->getLongValue(),
103                _warp->getIntValue());
104   globals->set_time_params(_impl);
105     
106   // frame-rate / worst-case latency / update-rate counters
107   _frameRate = fgGetNode("/sim/frame-rate", true);
108   _frameLatency = fgGetNode("/sim/frame-latency-max-ms", true);
109   _frameRateWorst = fgGetNode("/sim/frame-rate-worst", true);
110   _lastFrameTime = 0;
111   _frameLatencyMax = 0.0;
112   _frameCount = 0;
113 }
114
115 void TimeManager::postinit()
116 {
117   initTimeOffset();
118 }
119
120 void TimeManager::reinit()
121 {
122   shutdown();
123   init();
124   postinit();
125 }
126
127 void TimeManager::shutdown()
128 {
129   _warp->removeChangeListener(this);
130   
131   globals->set_time_params(NULL);
132   delete _impl;
133   _impl = NULL;
134   _inited = false;
135   globals->get_event_mgr()->removeTask("updateLocalTime");
136 }
137
138 void TimeManager::valueChanged(SGPropertyNode* aProp)
139 {
140   if (aProp == _warp) {
141     if (_clockFreeze->getBoolValue()) {
142     // if the warp is changed manually while frozen, don't modify it when
143     // un-freezing - the user wants to unfreeze with exactly the warp
144     // they specified.
145       _adjustWarpOnUnfreeze = false;
146     }
147     
148     double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
149     double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
150     _impl->update(lon, lat,
151                    _timeOverride->getLongValue(),
152                    _warp->getIntValue());
153   }
154 }
155
156 void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
157 {
158   // Update the elapsed time.
159   if (_firstUpdate) {
160     _lastStamp.stamp();
161     _firstUpdate = false;
162     _lastClockFreeze = _clockFreeze->getBoolValue();
163   }
164
165   bool wait_for_scenery = !_sceneryLoaded;
166   if (!wait_for_scenery) {
167     throttleUpdateRate();
168   }
169   else
170   {
171       // suppress framerate while initial scenery isn't loaded yet (splash screen still active) 
172       _lastFrameTime=0;
173       _frameCount = 0;
174   }
175   
176   SGTimeStamp currentStamp;
177   currentStamp.stamp();
178   double dt = (currentStamp - _lastStamp).toSecs();
179   if (dt > _frameLatencyMax)
180       _frameLatencyMax = dt;
181
182 // Limit the time we need to spend in simulation loops
183 // That means, if the /sim/max-simtime-per-frame value is strictly positive
184 // you can limit the maximum amount of time you will do simulations for
185 // one frame to display. The cpu time spent in simulations code is roughly
186 // at least O(real_delta_time_sec). If this is (due to running debug
187 // builds or valgrind or something different blowing up execution times)
188 // larger than the real time you will no longer get any response
189 // from flightgear. This limits that effect. Just set to property from
190 // your .fgfsrc or commandline ...
191   double dtMax = _maxDtPerFrame->getDoubleValue();
192   if (0 < dtMax && dtMax < dt) {
193     dt = dtMax;
194   }
195     
196   SGSubsystemGroup* fdmGroup = 
197     globals->get_subsystem_mgr()->get_group(SGSubsystemMgr::FDM);
198   fdmGroup->set_fixed_update_time(1.0 / _modelHz);
199   
200 // round the real time down to a multiple of 1/model-hz.
201 // this way all systems are updated the _same_ amount of dt.
202   dt += _dtRemainder;
203   int multiLoop = long(floor(dt * _modelHz));
204   multiLoop = SGMisc<long>::max(0, multiLoop);
205   _dtRemainder = dt - double(multiLoop)/double(_modelHz);
206   dt = double(multiLoop)/double(_modelHz);
207
208   realDt = dt;
209   if (_clockFreeze->getBoolValue() || wait_for_scenery) {
210     simDt = 0;
211   } else {
212     simDt = dt;
213   }
214   
215   _lastStamp = currentStamp;
216   globals->inc_sim_time_sec(simDt);
217
218 // These are useful, especially for Nasal scripts.
219   _timeDelta = realDt;
220   _simTimeDelta = simDt;
221 }
222
223 void TimeManager::update(double dt)
224 {
225   bool freeze = _clockFreeze->getBoolValue();
226   time_t now = time(NULL);
227   
228   if (freeze) {
229     // clock freeze requested
230     if (_timeOverride->getLongValue() == 0) {
231       _timeOverride->setLongValue(now);
232       _adjustWarpOnUnfreeze = true;
233     }
234   } else {
235     // no clock freeze requested
236     if (_lastClockFreeze) {
237       if (_adjustWarpOnUnfreeze) {
238       // clock just unfroze, let's set warp as the difference
239       // between frozen time and current time so we don't get a
240       // time jump (and corresponding sky object and lighting
241       // jump.)
242         int adjust = _timeOverride->getLongValue() - now;
243         SG_LOG(SG_GENERAL, SG_INFO, "adjusting on un-freeze:" << adjust);
244         _warp->setIntValue(_warp->getIntValue() + adjust);
245       }
246       _timeOverride->setLongValue(0);
247     }
248     
249     int warpDelta = _warpDelta->getIntValue();
250     if (warpDelta != 0) {
251       _warp->setIntValue(_warp->getIntValue() + warpDelta);
252     }
253   }
254
255   _lastClockFreeze = freeze;
256   double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
257   double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
258   _impl->update(lon, lat,
259                _timeOverride->getLongValue(),
260                _warp->getIntValue());
261
262   computeFrameRate();
263 }
264
265 void TimeManager::computeFrameRate()
266 {
267   // Calculate frame rate average
268   if ((_impl->get_cur_time() != _lastFrameTime)) {
269     _frameRate->setIntValue(_frameCount);
270     _frameLatency->setDoubleValue(_frameLatencyMax*1000);
271     if (_frameLatencyMax>0)
272         _frameRateWorst->setIntValue(1/_frameLatencyMax);
273     _frameCount = 0;
274     _frameLatencyMax = 0.0;
275   }
276   
277   _lastFrameTime = _impl->get_cur_time();
278   ++_frameCount;
279 }
280
281 void TimeManager::throttleUpdateRate()
282 {
283   // common case, no throttle requested
284   double throttle_hz = fgGetDouble("/sim/frame-rate-throttle-hz", 0.0);
285   if (throttle_hz <= 0)
286     return; // no-op
287
288   // sleep for exactly 1/hz seconds relative to the past valid timestamp
289   SGTimeStamp::sleepUntil(_lastStamp + SGTimeStamp::fromSec(1/throttle_hz));
290 }
291
292 // periodic time updater wrapper
293 void TimeManager::updateLocalTime() 
294 {
295   SGPath zone(globals->get_fg_root());
296   zone.append("Timezone");
297   
298   double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
299   double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
300   
301   SG_LOG(SG_GENERAL, SG_INFO, "updateLocal(" << lon << ", " << lat << ", " << zone.str() << ")");
302   _impl->updateLocal(lon, lat, zone.str());
303 }
304
305 void TimeManager::initTimeOffset()
306 {
307
308   long int offset = fgGetLong("/sim/startup/time-offset");
309   string offset_type = fgGetString("/sim/startup/time-offset-type");
310   setTimeOffset(offset_type, offset);
311 }
312
313 void TimeManager::setTimeOffset(const std::string& offset_type, long int offset)
314 {
315   // Handle potential user specified time offsets
316   int orig_warp = _warp->getIntValue();
317   time_t cur_time = _impl->get_cur_time();
318   time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
319   time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
320   time_t aircraftLocalTime = 
321       sgTimeGetGMT( fgLocaltime(&cur_time, _impl->get_zonename() ) );
322     
323   // Okay, we now have several possible scenarios
324   double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
325   double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
326   int warp = 0;
327   
328   if ( offset_type == "real" ) {
329       warp = 0;
330   } else if ( offset_type == "dawn" ) {
331       warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 90.0, true ); 
332   } else if ( offset_type == "morning" ) {
333      warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 75.0, true ); 
334   } else if ( offset_type == "noon" ) {
335      warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 0.0, true ); 
336   } else if ( offset_type == "afternoon" ) {
337     warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 75.0, false );  
338   } else if ( offset_type == "dusk" ) {
339     warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 90.0, false );
340   } else if ( offset_type == "evening" ) {
341     warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 100.0, false );
342   } else if ( offset_type == "midnight" ) {
343     warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 180.0, false );
344   } else if ( offset_type == "system-offset" ) {
345     warp = offset;
346     orig_warp = 0;
347   } else if ( offset_type == "gmt-offset" ) {
348     warp = offset - (currGMT - systemLocalTime);
349     orig_warp = 0;
350   } else if ( offset_type == "latitude-offset" ) {
351     warp = offset - (aircraftLocalTime - systemLocalTime);
352     orig_warp = 0;
353   } else if ( offset_type == "system" ) {
354     warp = offset - (systemLocalTime - currGMT) - cur_time;
355   } else if ( offset_type == "gmt" ) {
356       warp = offset - cur_time;
357   } else if ( offset_type == "latitude" ) {
358       warp = offset - (aircraftLocalTime - currGMT)- cur_time; 
359   } else {
360     SG_LOG( SG_GENERAL, SG_ALERT,
361           "TimeManager::setTimeOffset: unsupported offset: " << offset_type );
362      warp = 0;
363   }
364   
365   _warp->setIntValue( orig_warp + warp );
366
367   SG_LOG( SG_GENERAL, SG_INFO, "After TimeManager::setTimeOffset(): warp = " 
368             << _warp->getIntValue() );
369 }