]> git.mxchange.org Git - flightgear.git/commitdiff
Factor timeofday command into the TimeManager
authorJames Turner <zakalawe@mac.com>
Wed, 25 May 2011 07:23:11 +0000 (08:23 +0100)
committerJames Turner <zakalawe@mac.com>
Wed, 25 May 2011 11:13:25 +0000 (12:13 +0100)
src/Main/fg_commands.cxx
src/Time/TimeManager.cxx
src/Time/TimeManager.hxx

index 607c11c88aa97e04c1bd90eaa67eddf39224fa94..c2058ba1978b06b80a38ac4c481e535a5727d735 100644 (file)
@@ -33,7 +33,6 @@
 #include <Scenery/scenery.hxx>
 #include <Scripting/NasalSys.hxx>
 #include <Sound/sample_queue.hxx>
-#include <Time/sunsolver.hxx>
 #include <Airports/xmlloader.hxx>
 
 #include "fg_init.hxx"
@@ -699,85 +698,6 @@ do_set_dewpoint_degc (const SGPropertyNode * arg)
     return do_set_dewpoint_sea_level_degc(dummy.get_dewpoint_sea_level_degc());
 }
 #endif
-/**
- * Update the lighting manually.
- */
-static bool
-do_timeofday (const SGPropertyNode * arg)
-{
-    const string &offset_type = arg->getStringValue("timeofday", "noon");
-
-    static const SGPropertyNode *longitude
-        = fgGetNode("/position/longitude-deg");
-    static const SGPropertyNode *latitude
-        = fgGetNode("/position/latitude-deg");
-
-    int orig_warp = globals->get_warp();
-    SGTime *t = globals->get_time_params();
-    time_t cur_time = t->get_cur_time();
-    // cout << "cur_time = " << cur_time << endl;
-    // cout << "orig_warp = " << orig_warp << endl;
-
-    int warp = 0;
-    if ( offset_type == "real" ) {
-        warp = -orig_warp;
-    } else if ( offset_type == "dawn" ) {
-        warp = fgTimeSecondsUntilSunAngle( cur_time,
-                                           longitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           latitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           90.0, true ); 
-    } else if ( offset_type == "morning" ) {
-        warp = fgTimeSecondsUntilSunAngle( cur_time,
-                                           longitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           latitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           75.0, true ); 
-    } else if ( offset_type == "noon" ) {
-        warp = fgTimeSecondsUntilSunAngle( cur_time,
-                                           longitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           latitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           0.0, true ); 
-    } else if ( offset_type == "afternoon" ) {
-        warp = fgTimeSecondsUntilSunAngle( cur_time,
-                                           longitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           latitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           60.0, false ); 
-     } else if ( offset_type == "dusk" ) {
-        warp = fgTimeSecondsUntilSunAngle( cur_time,
-                                           longitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           latitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           90.0, false ); 
-     } else if ( offset_type == "evening" ) {
-        warp = fgTimeSecondsUntilSunAngle( cur_time,
-                                           longitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           latitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           100.0, false ); 
-    } else if ( offset_type == "midnight" ) {
-        warp = fgTimeSecondsUntilSunAngle( cur_time,
-                                           longitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           latitude->getDoubleValue()
-                                             * SGD_DEGREES_TO_RADIANS,
-                                           180.0, false ); 
-    }
-    
-
-    fgSetInt("/sim/time/warp", orig_warp + warp);
-    
-    return true;
-}
-
 
 /**
  * Built-in command: toggle a bool property value.
@@ -1468,7 +1388,6 @@ static struct {
     { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
     { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
     */
-    { "timeofday", do_timeofday },
     { "property-toggle", do_property_toggle },
     { "property-assign", do_property_assign },
     { "property-adjust", do_property_adjust },
index 4c4b28a76bc40f5500a1979bb5cdca8079262299..856ee7599964b104883f4b5e0b5a5a557c0eadc1 100644 (file)
 #include <simgear/structure/event_mgr.hxx>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/timing/lowleveltime.h>
+#include <simgear/structure/commands.hxx>
 
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
 #include <Time/sunsolver.hxx>
 
+static bool do_timeofday (const SGPropertyNode * arg)
+{
+    const string &offset_type = arg->getStringValue("timeofday", "noon");
+    int offset = arg->getIntValue("offset", 0);
+    TimeManager* self = (TimeManager*) globals->get_subsystem("time");
+    if (offset_type == "real") {
+    // without this, setting 'real' time is a no-op, since the current
+    // wrap value (orig_warp) is retained in setTimeOffset. Ick.
+        fgSetInt("/sim/time/warp", 0);
+    }
+    
+    self->setTimeOffset(offset_type, offset);
+    return true;
+}
+
 TimeManager::TimeManager() :
   _inited(false),
   _impl(NULL)
 {
-  
+  SGCommandMgr::instance()->addCommand("timeofday", do_timeofday);
 }
 
 void TimeManager::init()
@@ -345,6 +361,14 @@ void TimeManager::updateLocalTime()
 }
 
 void TimeManager::initTimeOffset()
+{
+
+  int offset = fgGetInt("/sim/startup/time-offset");
+  string offset_type = fgGetString("/sim/startup/time-offset-type");
+  setTimeOffset(offset_type, offset);
+}
+
+void TimeManager::setTimeOffset(const std::string& offset_type, int offset)
 {
   // Handle potential user specified time offsets
   int orig_warp = _warp->getIntValue();
@@ -355,8 +379,6 @@ void TimeManager::initTimeOffset()
       sgTimeGetGMT( fgLocaltime(&cur_time, _impl->get_zonename() ) );
     
   // Okay, we now have several possible scenarios
-  int offset = fgGetInt("/sim/startup/time-offset");
-  string offset_type = fgGetString("/sim/startup/time-offset-type");
   double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
   double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
   int warp = 0;
@@ -394,12 +416,12 @@ void TimeManager::initTimeOffset()
       warp = offset - (aircraftLocalTime - currGMT)- cur_time; 
   } else {
     SG_LOG( SG_GENERAL, SG_ALERT,
-          "TimeManager::initTimeOffset: unsupported offset: " << offset_type );
+          "TimeManager::setTimeOffset: unsupported offset: " << offset_type );
      warp = 0;
   }
   
   _warp->setIntValue( orig_warp + warp );
 
-  SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = " 
+  SG_LOG( SG_GENERAL, SG_INFO, "After TimeManager::setTimeOffset(): warp = " 
             << _warp->getIntValue() );
 }
index 53dffd51b1caf9b952663e2370b3e574de606dd9..7ee53ecb4e888dc3124cf2143a16bba9cfabbbf7 100644 (file)
@@ -43,6 +43,8 @@ public:
   
 // SGPropertyChangeListener overrides
   virtual void valueChanged(SGPropertyNode *);
+  
+  void setTimeOffset(const std::string& offset_type, int offset);
 private:
   
   /**