From d27c548710405917bef52f5a53b6b9904676302f Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sun, 18 Jul 2010 18:29:16 +0200 Subject: [PATCH] implemented passing of time --- apps/openmw/engine.cpp | 3 ++ apps/openmw/mwscript/interpretercontext.cpp | 22 +++++++++-- apps/openmw/mwworld/world.cpp | 44 +++++++++++++++++++++ apps/openmw/mwworld/world.hpp | 6 +++ 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 0b336e26b1..95653d3a39 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -51,6 +51,9 @@ bool OMW::Engine::frameStarted(const Ogre::FrameEvent& evt) // global scripts mEnvironment.mGlobalScripts->run (mEnvironment); + // passing of time (30 times as fast as RL time) + mEnvironment.mWorld->advanceTime ((mEnvironment.mFrameDuration*30)/3600); + return true; } diff --git a/apps/openmw/mwscript/interpretercontext.cpp b/apps/openmw/mwscript/interpretercontext.cpp index eb0f90deb2..7ea9e31165 100644 --- a/apps/openmw/mwscript/interpretercontext.cpp +++ b/apps/openmw/mwscript/interpretercontext.cpp @@ -133,18 +133,32 @@ namespace MWScript void InterpreterContext::setGlobalShort (const std::string& name, int value) { - mEnvironment.mWorld->getGlobalVariable (name).mShort = value; + if (name=="gamehour") + mEnvironment.mWorld->setHour (value); + else if (name=="day") + mEnvironment.mWorld->setDay (value); + else + mEnvironment.mWorld->getGlobalVariable (name).mShort = value; } void InterpreterContext::setGlobalLong (const std::string& name, int value) { - // a global long is internally a float. - mEnvironment.mWorld->getGlobalVariable (name).mLong = value; + if (name=="gamehour") + mEnvironment.mWorld->setHour (value); + else if (name=="day") + mEnvironment.mWorld->setDay (value); + else + mEnvironment.mWorld->getGlobalVariable (name).mLong = value; } void InterpreterContext::setGlobalFloat (const std::string& name, float value) { - mEnvironment.mWorld->getGlobalVariable (name).mFloat = value; + if (name=="gamehour") + mEnvironment.mWorld->setHour (value); + else if (name=="day") + mEnvironment.mWorld->setDay (value); + else + mEnvironment.mWorld->getGlobalVariable (name).mFloat = value; } bool InterpreterContext::isScriptRunning (const std::string& name) const diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index 14be609d8e..d2d787e1cd 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -1,6 +1,7 @@ #include "world.hpp" +#include #include #include "components/bsa/bsa_archive.hpp" @@ -278,4 +279,47 @@ namespace MWWorld } } } + + void World::advanceTime (double hours) + { + hours += mGlobalVariables->getFloat ("gamehour"); + setHour (hours); + } + + void World::setHour (double hour) + { + if (hour<0) + hour = 0; + + int days = hour / 24; + + hour = std::fmod (hour, 24); + + mGlobalVariables->setFloat ("gamehour", hour); + + if (days>0) + { + setDay (days + mGlobalVariables->getInt ("year")); + + days += mGlobalVariables->getInt ("dayspassed"); + mGlobalVariables->setInt ("dayspassed", days); + } + } + + void World::setDay (int day) + { + if (day<0) + day = 0; + + int year = day / 365; + day = day % 365; + + mGlobalVariables->setInt ("day", day); + + if (year>0) + { + year += mGlobalVariables->getInt ("year"); + mGlobalVariables->setInt ("year", year); + } + } } diff --git a/apps/openmw/mwworld/world.hpp b/apps/openmw/mwworld/world.hpp index d58e254dab..0929b3ffdc 100644 --- a/apps/openmw/mwworld/world.hpp +++ b/apps/openmw/mwworld/world.hpp @@ -88,6 +88,12 @@ namespace MWWorld void enable (Ptr reference); void disable (Ptr reference); + + void advanceTime (double hours); + + void setHour (double hour); + + void setDay (int day); }; }