From 2383016b1d673fcf0ad69a08bba62d7ce36cdd76 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 30 Nov 2013 14:22:26 +0100 Subject: [PATCH] Added the real tick duration to the OnWorldTick hook. --- src/LuaState.h | 16 ++++++++++++++++ src/Plugin.h | 2 +- src/PluginLua.cpp | 4 ++-- src/PluginLua.h | 2 +- src/PluginManager.cpp | 4 ++-- src/PluginManager.h | 2 +- src/World.cpp | 19 ++++++++++--------- src/World.h | 2 +- 8 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/LuaState.h b/src/LuaState.h index 5e7f3d180..15b0cdeff 100644 --- a/src/LuaState.h +++ b/src/LuaState.h @@ -224,6 +224,22 @@ public: return CallFunction(0); } + /// Call any 3-param 0-return Lua function in a single line: + template< + typename FnT, typename ArgT1, typename ArgT2, typename ArgT3 + > + bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3) + { + if (!PushFunction(a_FnName)) + { + return false; + } + Push(a_Arg1); + Push(a_Arg2); + Push(a_Arg3); + return CallFunction(0); + } + /// Call any 1-param 1-return Lua function in a single line: template< typename FnT, typename ArgT1, typename RetT1 diff --git a/src/Plugin.h b/src/Plugin.h index 06e5819df..ec55e256d 100644 --- a/src/Plugin.h +++ b/src/Plugin.h @@ -94,7 +94,7 @@ public: virtual bool OnUpdatingSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, cPlayer * a_Player) = 0; virtual bool OnWeatherChanged (cWorld & a_World) = 0; virtual bool OnWeatherChanging (cWorld & a_World, eWeather & a_NewWeather) = 0; - virtual bool OnWorldTick (cWorld & a_World, float a_Dt) = 0; + virtual bool OnWorldTick (cWorld & a_World, float a_Dt, int a_LastTickDurationMSec) = 0; /** Handles the command split into a_Split, issued by player a_Player. Command permissions have already been checked. diff --git a/src/PluginLua.cpp b/src/PluginLua.cpp index 4ddf191ac..110010087 100644 --- a/src/PluginLua.cpp +++ b/src/PluginLua.cpp @@ -1143,13 +1143,13 @@ bool cPluginLua::OnWeatherChanging(cWorld & a_World, eWeather & a_NewWeather) -bool cPluginLua::OnWorldTick(cWorld & a_World, float a_Dt) +bool cPluginLua::OnWorldTick(cWorld & a_World, float a_Dt, int a_LastTickDurationMSec) { cCSLock Lock(m_CriticalSection); cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_WORLD_TICK]; for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr) { - m_LuaState.Call((int)(**itr), &a_World, a_Dt); + m_LuaState.Call((int)(**itr), &a_World, a_Dt, a_LastTickDurationMSec); } return false; } diff --git a/src/PluginLua.h b/src/PluginLua.h index 908466966..6d135ab75 100644 --- a/src/PluginLua.h +++ b/src/PluginLua.h @@ -90,7 +90,7 @@ public: virtual bool OnUpdatingSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, cPlayer * a_Player) override; virtual bool OnWeatherChanged (cWorld & a_World) override; virtual bool OnWeatherChanging (cWorld & a_World, eWeather & a_NewWeather) override; - virtual bool OnWorldTick (cWorld & a_World, float a_Dt) override; + virtual bool OnWorldTick (cWorld & a_World, float a_Dt, int a_LastTickDurationMSec) override; virtual bool HandleCommand(const AStringVector & a_Split, cPlayer * a_Player) override; diff --git a/src/PluginManager.cpp b/src/PluginManager.cpp index bb6f2a24b..b57a9d7d8 100644 --- a/src/PluginManager.cpp +++ b/src/PluginManager.cpp @@ -1210,7 +1210,7 @@ bool cPluginManager::CallHookWeatherChanging(cWorld & a_World, eWeather & a_NewW -bool cPluginManager::CallHookWorldTick(cWorld & a_World, float a_Dt) +bool cPluginManager::CallHookWorldTick(cWorld & a_World, float a_Dt, int a_LastTickDurationMSec) { HookMap::iterator Plugins = m_Hooks.find(HOOK_WORLD_TICK); if (Plugins == m_Hooks.end()) @@ -1219,7 +1219,7 @@ bool cPluginManager::CallHookWorldTick(cWorld & a_World, float a_Dt) } for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr) { - if ((*itr)->OnWorldTick(a_World, a_Dt)) + if ((*itr)->OnWorldTick(a_World, a_Dt, a_LastTickDurationMSec)) { return true; } diff --git a/src/PluginManager.h b/src/PluginManager.h index 4140bffb5..12e4da71b 100644 --- a/src/PluginManager.h +++ b/src/PluginManager.h @@ -191,7 +191,7 @@ public: // tolua_export bool CallHookUpdatingSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, cPlayer * a_Player); bool CallHookWeatherChanged (cWorld & a_World); bool CallHookWeatherChanging (cWorld & a_World, eWeather & a_NewWeather); - bool CallHookWorldTick (cWorld & a_World, float a_Dt); + bool CallHookWorldTick (cWorld & a_World, float a_Dt, int a_LastTickDurationMSec); bool DisablePlugin(const AString & a_PluginName); // tolua_export bool LoadPlugin (const AString & a_PluginName); // tolua_export diff --git a/src/World.cpp b/src/World.cpp index 8ef4dc0f3..03efbdf32 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -197,20 +197,21 @@ void cWorld::cTickThread::Execute(void) { cTimer Timer; - long long msPerTick = 50; - long long LastTime = Timer.GetNowTime(); + const Int64 msPerTick = 50; + Int64 LastTime = Timer.GetNowTime(); + Int64 TickDuration = 50; while (!m_ShouldTerminate) { - long long NowTime = Timer.GetNowTime(); + Int64 NowTime = Timer.GetNowTime(); float DeltaTime = (float)(NowTime - LastTime); - m_World.Tick(DeltaTime); - long long TickTime = Timer.GetNowTime() - NowTime; + m_World.Tick(DeltaTime, (int)TickDuration); + TickDuration = Timer.GetNowTime() - NowTime; - if (TickTime < msPerTick) + if (TickDuration < msPerTick) { // Stretch tick time until it's at least msPerTick - cSleep::MilliSleep((unsigned int)(msPerTick - TickTime)); + cSleep::MilliSleep((unsigned int)(msPerTick - TickDuration)); } LastTime = NowTime; @@ -660,10 +661,10 @@ void cWorld::Stop(void) -void cWorld::Tick(float a_Dt) +void cWorld::Tick(float a_Dt, int a_LastTickDurationMSec) { // Call the plugins - cPluginManager::Get()->CallHookWorldTick(*this, a_Dt); + cPluginManager::Get()->CallHookWorldTick(*this, a_Dt, a_LastTickDurationMSec); // We need sub-tick precision here, that's why we store the time in seconds and calculate ticks off of it m_WorldAgeSecs += (double)a_Dt / 1000.0; diff --git a/src/World.h b/src/World.h index 958fc4255..a6b61f2e2 100644 --- a/src/World.h +++ b/src/World.h @@ -729,7 +729,7 @@ private: cWorld(const AString & a_WorldName); ~cWorld(); - void Tick(float a_Dt); + void Tick(float a_Dt, int a_LastTickDurationMSec); /// Handles the weather in each tick void TickWeather(float a_Dt);