1
0
Fork 0

Factor timeofday command into the TimeManager

This commit is contained in:
James Turner 2011-05-25 08:23:11 +01:00
parent 5bae2e3d37
commit 436abe1e62
3 changed files with 29 additions and 86 deletions

View 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 },

View file

@ -35,16 +35,32 @@
#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() );
}

View file

@ -43,6 +43,8 @@ public:
// SGPropertyChangeListener overrides
virtual void valueChanged(SGPropertyNode *);
void setTimeOffset(const std::string& offset_type, int offset);
private:
/**