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